thegreathir il y a 1 an
Parent
commit
1f0f66fe0f
1 fichiers modifiés avec 60 ajouts et 0 suppressions
  1. 60 0
      src/wire/ipsec_esp.rs

+ 60 - 0
src/wire/ipsec_esp.rs

@@ -108,3 +108,63 @@ impl Repr {
         packet.set_sequence_number(self.sequence_number);
     }
 }
+
+#[cfg(test)]
+mod test {
+    use super::*;
+
+    static PACKET_BYTES: [u8; 136] = [
+        0xfb, 0x51, 0x28, 0xa6, 0x00, 0x00, 0x00, 0x02, 0x5d, 0xbe, 0x2d, 0x56, 0xd4, 0x6a, 0x57,
+        0xf5, 0xfc, 0x69, 0x8b, 0x3c, 0xa6, 0xb6, 0x88, 0x3a, 0x6c, 0xc1, 0x33, 0x92, 0xdb, 0x40,
+        0xab, 0x11, 0x54, 0xb4, 0x0f, 0x22, 0x4d, 0x37, 0x3a, 0x06, 0x94, 0x1e, 0xd4, 0x25, 0xaf,
+        0xf0, 0xb0, 0x11, 0x1f, 0x07, 0x96, 0x2a, 0xa7, 0x20, 0xb1, 0xf5, 0x52, 0xb2, 0x12, 0x46,
+        0xd6, 0xa5, 0x13, 0x4e, 0x97, 0x75, 0x44, 0x19, 0xc7, 0x29, 0x35, 0xc5, 0xed, 0xa4, 0x0c,
+        0xe7, 0x87, 0xec, 0x9c, 0xb1, 0x12, 0x42, 0x74, 0x7c, 0x12, 0x3c, 0x7f, 0x44, 0x9c, 0x6b,
+        0x46, 0x27, 0x28, 0xd2, 0x0e, 0xb1, 0x28, 0xd3, 0xd8, 0xc2, 0xd1, 0xac, 0x25, 0xfe, 0xef,
+        0xed, 0x13, 0xfd, 0x8f, 0x18, 0x9c, 0x2d, 0xb1, 0x0e, 0x50, 0xe9, 0xaa, 0x65, 0x93, 0x56,
+        0x40, 0x43, 0xa3, 0x72, 0x54, 0xba, 0x1b, 0xb1, 0xaf, 0xca, 0x04, 0x15, 0xf9, 0xef, 0xb7,
+        0x1d,
+    ];
+
+    #[test]
+    fn test_deconstruct() {
+        let packet = Packet::new_unchecked(&PACKET_BYTES[..]);
+        assert_eq!(packet.security_parameters_index(), 0xfb5128a6);
+        assert_eq!(packet.sequence_number(), 2);
+    }
+
+    #[test]
+    fn test_construct() {
+        let mut bytes = vec![0xa5; 8];
+        let mut packet: Packet<&mut Vec<u8>> = Packet::new_unchecked(&mut bytes);
+        packet.set_security_parameters_index(0xfb5128a6);
+        packet.set_sequence_number(2);
+        assert_eq!(&*packet.into_inner(), &PACKET_BYTES[..8]);
+    }
+    #[test]
+    fn test_check_len() {
+        assert!(matches!(Packet::new_checked(&PACKET_BYTES[..7]), Err(_)));
+        assert!(matches!(Packet::new_checked(&PACKET_BYTES[..]), Ok(_)));
+    }
+
+    fn packet_repr() -> Repr {
+        Repr {
+            security_parameters_index: 0xfb5128a6,
+            sequence_number: 2,
+        }
+    }
+
+    #[test]
+    fn test_parse() {
+        let packet = Packet::new_unchecked(&PACKET_BYTES[..]);
+        assert_eq!(Repr::parse(&packet).unwrap(), packet_repr());
+    }
+
+    #[test]
+    fn test_emit() {
+        let mut bytes = vec![0x17; 8];
+        let mut packet = Packet::new_unchecked(&mut bytes);
+        packet_repr().emit(&mut packet);
+        assert_eq!(&*packet.into_inner(), &PACKET_BYTES[..8]);
+    }
+}