src/vrrpv2.rs
changeset 21 17afb5ba9c2b
parent 20 fd3474f42783
child 22 809a5a9e5dd9
equal deleted inserted replaced
20:fd3474f42783 21:17afb5ba9c2b
    81     let mut rdr = Cursor::new(bytes);
    81     let mut rdr = Cursor::new(bytes);
    82     let Ok(vertype) = rdr.read_u8() else {
    82     let Ok(vertype) = rdr.read_u8() else {
    83         return Err(VRRPv2Error::ParseError);
    83         return Err(VRRPv2Error::ParseError);
    84     };
    84     };
    85     if (vertype & 0xF) != 1 {
    85     if (vertype & 0xF) != 1 {
       
    86         return Err(VRRPv2Error::InvalidType);
       
    87     }
       
    88     if (vertype >> 4) != 2 {
    86         return Err(VRRPv2Error::InvalidVersion);
    89         return Err(VRRPv2Error::InvalidVersion);
    87     }
       
    88     if (vertype >> 4) != 2 {
       
    89         return Err(VRRPv2Error::InvalidType);
       
    90     }
    90     }
    91     let Ok(virtual_router_id) = rdr.read_u8() else {
    91     let Ok(virtual_router_id) = rdr.read_u8() else {
    92         return Err(VRRPv2Error::ParseError);
    92         return Err(VRRPv2Error::ParseError);
    93     };
    93     };
    94     let Ok(priority) = rdr.read_u8() else {
    94     let Ok(priority) = rdr.read_u8() else {
   186 }
   186 }
   187 
   187 
   188 #[test]
   188 #[test]
   189 fn test_invalid_version() {
   189 fn test_invalid_version() {
   190     let bytes = [
   190     let bytes = [
   191         0x20, 0x1, 0x2a, 0x0, 0x0, 0x1, 0xb5, 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
   191         0x31, 0x1, 0x2a, 0x0, 0x0, 0x1, 0xb5, 0xfd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
   192         0x0, 0x0, 0x0,
   192         0x0, 0x0, 0x0,
   193     ];
   193     ];
   194     assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidVersion));
   194     assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidVersion));
   195 }
   195 }
   196 
   196 
   197 #[test]
   197 #[test]
   198 fn test_invalid_type() {
   198 fn test_invalid_type() {
   199     let bytes = [
   199     let bytes = [
   200         0x31, 0x2a, 0x64, 0x1, 0x0, 0x1, 0xaa, 0x29, 0xc0, 0xa8, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
   200         0x20, 0x2a, 0x64, 0x1, 0x0, 0x1, 0xaa, 0x29, 0xc0, 0xa8, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
   201         0x0, 0x0, 0x0,
   201         0x0, 0x0, 0x0,
   202     ];
   202     ];
   203     assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidType));
   203     assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidType));
   204 }
   204 }
   205 
   205