Browse Source

Add test for `buffer_len`

hkalbasi 1 year ago
parent
commit
55ac2cfbbd
5 changed files with 26 additions and 11 deletions
  1. 2 1
      Cargo.toml
  2. 2 2
      src/wire/ip.rs
  3. 10 3
      src/wire/ipsec_ah.rs
  4. 10 3
      src/wire/ipsec_esp.rs
  5. 2 2
      src/wire/mod.rs

+ 2 - 1
Cargo.toml

@@ -57,6 +57,7 @@ defmt = [ "dep:defmt", "heapless/defmt", "heapless/defmt-impl" ]
 "proto-sixlowpan" = ["proto-ipv6"]
 "proto-sixlowpan-fragmentation" = ["proto-sixlowpan", "_proto-fragmentation"]
 "proto-dns" = []
+"proto-ipsec" = ["proto-ipsec-ah", "proto-ipsec-esp"]
 "proto-ipsec-ah"= []
 "proto-ipsec-esp"= []
 
@@ -78,7 +79,7 @@ default = [
   "medium-ethernet", "medium-ip", "medium-ieee802154",
   "phy-raw_socket", "phy-tuntap_interface",
   "proto-ipv4", "proto-igmp", "proto-dhcpv4", "proto-ipv6", "proto-dns",
-  "proto-ipv4-fragmentation", "proto-sixlowpan-fragmentation", "proto-ipsec-ah", "proto-ipsec-esp",
+  "proto-ipv4-fragmentation", "proto-sixlowpan-fragmentation", "proto-ipsec",
   "socket-raw", "socket-icmp", "socket-udp", "socket-tcp", "socket-dhcpv4", "socket-dns", "socket-mdns",
   "packetmeta-id", "async"
 ]

+ 2 - 2
src/wire/ip.rs

@@ -73,8 +73,8 @@ impl fmt::Display for Protocol {
             Protocol::Udp => write!(f, "UDP"),
             Protocol::Ipv6Route => write!(f, "IPv6-Route"),
             Protocol::Ipv6Frag => write!(f, "IPv6-Frag"),
-            Protocol::Esp => write!(f, "ESP"),
-            Protocol::Ah => write!(f, "AH"),
+            Protocol::Esp => write!(f, "IPsec-ESP"),
+            Protocol::Ah => write!(f, "IPsec-AH"),
             Protocol::Icmpv6 => write!(f, "ICMPv6"),
             Protocol::Ipv6NoNxt => write!(f, "IPv6-NoNxt"),
             Protocol::Ipv6Opts => write!(f, "IPv6-Opts"),

+ 10 - 3
src/wire/ipsec_ah.rs

@@ -233,7 +233,7 @@ mod test {
     #[test]
     fn test_construct() {
         let mut bytes = vec![0xa5; 24];
-        let mut packet: Packet<&mut Vec<u8>> = Packet::new_unchecked(&mut bytes);
+        let mut packet = Packet::new_unchecked(&mut bytes);
         packet.set_next_header(IpProtocol::Esp);
         packet.set_payload_len(4);
         packet.clear_reserved();
@@ -243,7 +243,7 @@ mod test {
             0xaf, 0xd2, 0xe7, 0xa1, 0x73, 0xd3, 0x29, 0x0b, 0xfe, 0x6b, 0x63, 0x73,
         ];
         packet.integrity_check_value_mut().copy_from_slice(&ICV);
-        assert_eq!(&*packet.into_inner(), &PACKET_BYTES2[..]);
+        assert_eq!(bytes, PACKET_BYTES2);
     }
     #[test]
     fn test_check_len() {
@@ -274,6 +274,13 @@ mod test {
         let mut bytes = vec![0x17; 24];
         let mut packet = Packet::new_unchecked(&mut bytes);
         packet_repr().emit(&mut packet);
-        assert_eq!(&*packet.into_inner(), &PACKET_BYTES2[..]);
+        assert_eq!(bytes, PACKET_BYTES2);
+    }
+
+    #[test]
+    fn test_buffer_len() {
+        let header = Packet::new_unchecked(&PACKET_BYTES1[..]);
+        let repr = Repr::parse(&header).unwrap();
+        assert_eq!(repr.buffer_len(), PACKET_BYTES1.len());
     }
 }

+ 10 - 3
src/wire/ipsec_esp.rs

@@ -136,10 +136,10 @@ mod test {
     #[test]
     fn test_construct() {
         let mut bytes = vec![0xa5; 8];
-        let mut packet: Packet<&mut Vec<u8>> = Packet::new_unchecked(&mut bytes);
+        let mut packet = Packet::new_unchecked(&mut bytes);
         packet.set_security_parameters_index(0xfb5128a6);
         packet.set_sequence_number(2);
-        assert_eq!(&*packet.into_inner(), &PACKET_BYTES[..8]);
+        assert_eq!(&bytes, &PACKET_BYTES[..8]);
     }
     #[test]
     fn test_check_len() {
@@ -165,6 +165,13 @@ mod test {
         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]);
+        assert_eq!(&bytes, &PACKET_BYTES[..8]);
+    }
+
+    #[test]
+    fn test_buffer_len() {
+        let header = Packet::new_unchecked(&PACKET_BYTES[..]);
+        let repr = Repr::parse(&header).unwrap();
+        assert_eq!(repr.buffer_len(), 8);
     }
 }

+ 2 - 2
src/wire/mod.rs

@@ -279,10 +279,10 @@ pub use self::dns::{
 };
 
 #[cfg(feature = "proto-ipsec-ah")]
-pub use self::ipsec_ah::{Packet as IPSecAuthHeaderPacket, Repr as IPSecAuthHeaderRepr};
+pub use self::ipsec_ah::{Packet as IpSecAuthHeaderPacket, Repr as IpSecAuthHeaderRepr};
 
 #[cfg(feature = "proto-ipsec-esp")]
-pub use self::ipsec_esp::{Packet as IPSecEspPacket, Repr as IPSecEspRepr};
+pub use self::ipsec_esp::{Packet as IpSecEspPacket, Repr as IpSecEspRepr};
 
 /// Parsing a packet failed.
 ///