Selaa lähdekoodia

Move more iface tests to test ipv6

Closes: #249
Approved by: whitequark
Dan Robertson 6 vuotta sitten
vanhempi
commit
94d7338aed
1 muutettua tiedostoa jossa 88 lisäystä ja 25 poistoa
  1. 88 25
      src/iface/ethernet.rs

+ 88 - 25
src/iface/ethernet.rs

@@ -1339,7 +1339,7 @@ mod test {
     use wire::{Ipv4Address, Ipv4Repr};
     #[cfg(feature = "proto-ipv4")]
     use wire::{Icmpv4Repr, Icmpv4DstUnreachable};
-    #[cfg(all(feature = "socket-udp", feature = "proto-ipv4"))]
+    #[cfg(all(feature = "socket-udp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
     use wire::{UdpPacket, UdpRepr};
     #[cfg(feature = "proto-ipv6")]
     use wire::{Ipv6Address, Ipv6Repr};
@@ -1391,17 +1391,17 @@ mod test {
     }
 
     #[test]
-    #[cfg(feature = "proto-ipv4")]
-    fn test_no_icmp_to_broadcast() {
+    fn test_no_icmp_no_unicast() {
         let (mut iface, mut socket_set) = create_loopback();
 
-        let mut eth_bytes = vec![0u8; 34];
+        let mut eth_bytes = vec![0u8; 54];
 
         // Unknown Ipv4 Protocol
         //
         // Because the destination is the broadcast address
         // this should not trigger and Destination Unreachable
         // response. See RFC 1122 § 3.2.2.
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
         let repr = IpRepr::Ipv4(Ipv4Repr {
             src_addr:    Ipv4Address([0x7f, 0x00, 0x00, 0x01]),
             dst_addr:    Ipv4Address::BROADCAST,
@@ -1409,6 +1409,14 @@ mod test {
             payload_len: 0,
             hop_limit:   0x40
         });
+        #[cfg(feature = "proto-ipv6")]
+        let repr = IpRepr::Ipv6(Ipv6Repr {
+            src_addr:    Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1),
+            dst_addr:    Ipv6Address::LINK_LOCAL_ALL_NODES,
+            next_header: IpProtocol::Unknown(0x0c),
+            payload_len: 0,
+            hop_limit:   0x40
+        });
 
         let frame = {
             let mut frame = EthernetFrame::new(&mut eth_bytes);
@@ -1422,8 +1430,12 @@ mod test {
         // Ensure that the unknown protocol frame does not trigger an
         // ICMP error response when the destination address is a
         // broadcast address
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
         assert_eq!(iface.inner.process_ipv4(&mut socket_set, Instant::from_millis(0), &frame),
                    Ok(Packet::None));
+        #[cfg(feature = "proto-ipv6")]
+        assert_eq!(iface.inner.process_ipv6(&mut socket_set, Instant::from_millis(0), &frame),
+                   Ok(Packet::None));
     }
 
     #[test]
@@ -1569,7 +1581,7 @@ mod test {
     }
 
     #[test]
-    #[cfg(all(feature = "socket-udp", feature = "proto-ipv4"))]
+    #[cfg(feature = "socket-udp")]
     fn test_handle_udp_broadcast() {
         use socket::{UdpSocket, UdpSocketBuffer, UdpPacketMetadata};
         use wire::IpEndpoint;
@@ -1588,7 +1600,10 @@ mod test {
 
         let socket_handle = socket_set.add(udp_socket);
 
-        let src_ip = Ipv4Address([0x7f, 0x00, 0x00, 0x02]);
+        #[cfg(feature = "proto-ipv6")]
+        let src_ip = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
+        #[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
+        let src_ip = Ipv4Address::new(0x7f, 0x00, 0x00, 0x02);
 
         let udp_repr = UdpRepr {
             src_port: 67,
@@ -1596,6 +1611,15 @@ mod test {
             payload:  &UDP_PAYLOAD
         };
 
+        #[cfg(feature = "proto-ipv6")]
+        let ip_repr = IpRepr::Ipv6(Ipv6Repr {
+            src_addr:    src_ip,
+            dst_addr:    Ipv6Address::LINK_LOCAL_ALL_NODES,
+            next_header: IpProtocol::Udp,
+            payload_len: udp_repr.buffer_len(),
+            hop_limit:   0x40
+        });
+        #[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
         let ip_repr = IpRepr::Ipv4(Ipv4Repr {
             src_addr:    src_ip,
             dst_addr:    Ipv4Address::BROADCAST,
@@ -1629,46 +1653,82 @@ mod test {
     }
 
     #[test]
-    #[cfg(all(feature = "socket-udp", feature = "proto-ipv4"))]
-    fn test_icmpv4_reply_size() {
-        use wire::IPV4_MIN_MTU;
+    #[cfg(feature = "socket-udp")]
+    fn test_icmp_reply_size() {
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
+        use wire::IPV4_MIN_MTU as MIN_MTU;
+        #[cfg(feature = "proto-ipv6")]
+        use wire::Icmpv6DstUnreachable;
+        #[cfg(feature = "proto-ipv6")]
+        use wire::IPV6_MIN_MTU as MIN_MTU;
+
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
+        const MAX_PAYLOAD_LEN: usize = 528;
+        #[cfg(feature = "proto-ipv6")]
+        const MAX_PAYLOAD_LEN: usize = 1192;
 
         let (iface, mut socket_set) = create_loopback();
 
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
         let src_addr = Ipv4Address([192, 168, 1, 1]);
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
         let dst_addr = Ipv4Address([192, 168, 1, 2]);
+        #[cfg(feature = "proto-ipv6")]
+        let src_addr = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
+        #[cfg(feature = "proto-ipv6")]
+        let dst_addr = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2);
 
         // UDP packet that if not tructated will cause a icmp port unreachable reply
-        // to exeed 576 bytes in length.
+        // to exeed the minimum mtu bytes in length.
         let udp_repr = UdpRepr {
             src_port: 67,
             dst_port: 68,
-            payload: &[0x2a; 524]
+            payload: &[0x2a; MAX_PAYLOAD_LEN]
         };
         let mut bytes = vec![0xff; udp_repr.buffer_len()];
         let mut packet = UdpPacket::new(&mut bytes[..]);
         udp_repr.emit(&mut packet, &src_addr.into(), &dst_addr.into(), &ChecksumCapabilities::default());
-        let ipv4_repr = Ipv4Repr {
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
+        let ip_repr = Ipv4Repr {
             src_addr: src_addr,
             dst_addr: dst_addr,
             protocol: IpProtocol::Udp,
             hop_limit: 64,
             payload_len: udp_repr.buffer_len()
         };
+        #[cfg(feature = "proto-ipv6")]
+        let ip_repr = Ipv6Repr {
+            src_addr: src_addr,
+            dst_addr: dst_addr,
+            next_header: IpProtocol::Udp,
+            hop_limit: 64,
+            payload_len: udp_repr.buffer_len()
+        };
         let payload = packet.into_inner();
 
         // Expected packets
-        let expected_icmpv4_repr = Icmpv4Repr::DstUnreachable {
+        #[cfg(feature = "proto-ipv6")]
+        let expected_icmp_repr = Icmpv6Repr::DstUnreachable {
+            reason: Icmpv6DstUnreachable::PortUnreachable,
+            header: ip_repr,
+            data:   &payload[..MAX_PAYLOAD_LEN]
+        };
+        #[cfg(feature = "proto-ipv6")]
+        let expected_ip_repr = Ipv6Repr {
+            src_addr: dst_addr,
+            dst_addr: src_addr,
+            next_header: IpProtocol::Icmpv6,
+            hop_limit: 64,
+            payload_len: expected_icmp_repr.buffer_len()
+        };
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
+        let expected_icmp_repr = Icmpv4Repr::DstUnreachable {
             reason: Icmpv4DstUnreachable::PortUnreachable,
-            header: ipv4_repr,
-            // We only include 520 bytes of the original payload
-            // in the expected packets payload. We must only send
-            // ICMPv4 replies that do not exceed 576 bytes in length.
-            //
-            // 528 + 2 * sizeof(IPv4 Header) + sizeof(DstUnreachable Header) = 576
-            data:   &payload[..528]
+            header: ip_repr,
+            data:   &payload[..MAX_PAYLOAD_LEN]
         };
-        let expected_ipv4_repr = Ipv4Repr {
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
+        let expected_ip_repr = Ipv4Repr {
             src_addr: dst_addr,
             dst_addr: src_addr,
             protocol: IpProtocol::Icmp,
@@ -1677,11 +1737,14 @@ mod test {
         };
 
         // The expected packet does not exceed the IPV4_MIN_MTU
-        assert_eq!(expected_ipv4_repr.buffer_len() + expected_icmpv4_repr.buffer_len(),
-                   IPV4_MIN_MTU);
+        assert_eq!(expected_ip_repr.buffer_len() + expected_icmp_repr.buffer_len(), MIN_MTU);
         // The expected packet and the generated packet are equal
-        assert_eq!(iface.inner.process_udp(&mut socket_set, ipv4_repr.into(), payload),
-                   Ok(Packet::Icmpv4((expected_ipv4_repr, expected_icmpv4_repr))));
+        #[cfg(all(feature = "proto-ipv4", not(feature = "proto-ipv6")))]
+        assert_eq!(iface.inner.process_udp(&mut socket_set, ip_repr.into(), payload),
+                   Ok(Packet::Icmpv4((expected_ip_repr, expected_icmp_repr))));
+        #[cfg(feature = "proto-ipv6")]
+        assert_eq!(iface.inner.process_udp(&mut socket_set, ip_repr.into(), payload),
+                   Ok(Packet::Icmpv6((expected_ip_repr, expected_icmp_repr))));
     }
 
     #[test]