ip: Protocol Independent IP Neighbors
[vpp.git] / src / plugins / dhcp / test / test_dhcp.py
index 9b55510..065683f 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
 import unittest
 import socket
@@ -18,8 +18,7 @@ from scapy.layers.dhcp import DHCP, BOOTP, DHCPTypes
 from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
     DHCP6_RelayReply, DHCP6_Advertise, DHCP6OptRelayMsg, DHCP6OptIfaceId, \
     DHCP6OptStatusCode, DHCP6OptVSS, DHCP6OptClientLinkLayerAddr, DHCP6_Request
-from socket import AF_INET, AF_INET6
-from scapy.utils import inet_pton, inet_ntop
+from socket import AF_INET, AF_INET6, inet_pton, inet_ntop
 from scapy.utils6 import in6_ptop
 from vpp_papi import mac_pton, VppEnum
 from vpp_sub_interface import VppDot1QSubint
@@ -251,6 +250,7 @@ class TestDHCP(VppTestCase):
         self.verify_dhcp_has_option(pkt, "hostname",
                                     hostname.encode('ascii'))
         if client_id:
+            client_id = '\x00' + client_id
             self.verify_dhcp_has_option(pkt, "client_id",
                                         client_id.encode('ascii'))
         bootp = pkt[BOOTP]
@@ -1297,12 +1297,6 @@ class TestDHCP(VppTestCase):
         self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
         self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
 
-        # remove the left over ARP entry
-        self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
-                                      self.pg3.remote_mac,
-                                      self.pg3.remote_ip4,
-                                      is_add=0)
-
         #
         # remove the DHCP config
         #
@@ -1464,6 +1458,25 @@ class TestDHCP(VppTestCase):
         self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
         self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
 
+        #
+        # read the DHCP client details from a dump
+        #
+        clients = self.vapi.dhcp_client_dump()
+
+        self.assertEqual(clients[0].client.sw_if_index,
+                         self.pg3.sw_if_index)
+        self.assertEqual(clients[0].lease.sw_if_index,
+                         self.pg3.sw_if_index)
+        self.assertEqual(clients[0].client.hostname, hostname)
+        self.assertEqual(clients[0].lease.hostname, hostname)
+        # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
+        self.assertEqual(clients[0].lease.state, 2)
+        self.assertEqual(clients[0].lease.mask_width, 24)
+        self.assertEqual(str(clients[0].lease.router_address),
+                         self.pg3.remote_ip4)
+        self.assertEqual(str(clients[0].lease.host_address),
+                         self.pg3.local_ip4)
+
         #
         # wait for the unicasted renewal
         #  the first attempt will be an ARP packet, since we have not yet
@@ -1492,6 +1505,25 @@ class TestDHCP(VppTestCase):
                                       l2_bc=False,
                                       broadcast=False)
 
+        # send an ACK with different data from the original offer *
+        self.pg3.generate_remote_hosts(4)
+        p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
+                 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
+                 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
+                 BOOTP(op=1, yiaddr=self.pg3.remote_hosts[3].ip4,
+                       chaddr=mac_pton(self.pg3.local_mac)) /
+                 DHCP(options=[('message-type', 'ack'),
+                               ('subnet_mask', "255.255.255.0"),
+                               ('router', self.pg3.remote_hosts[1].ip4),
+                               ('server_id', self.pg3.remote_hosts[2].ip4),
+                               ('lease_time', 43200),
+                               ('renewal_time', 2),
+                               'end']))
+
+        self.pg3.add_stream(p_ack)
+        self.pg_enable_capture(self.pg_interfaces)
+        self.pg_start()
+
         #
         # read the DHCP client details from a dump
         #
@@ -1501,23 +1533,15 @@ class TestDHCP(VppTestCase):
                          self.pg3.sw_if_index)
         self.assertEqual(clients[0].lease.sw_if_index,
                          self.pg3.sw_if_index)
-        self.assertEqual(clients[0].client.hostname.rstrip('\0'),
-                         hostname)
-        self.assertEqual(clients[0].lease.hostname.rstrip('\0'),
-                         hostname)
+        self.assertEqual(clients[0].client.hostname, hostname)
+        self.assertEqual(clients[0].lease.hostname, hostname)
         # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
         self.assertEqual(clients[0].lease.state, 2)
         self.assertEqual(clients[0].lease.mask_width, 24)
         self.assertEqual(str(clients[0].lease.router_address),
-                         self.pg3.remote_ip4)
+                         self.pg3.remote_hosts[1].ip4)
         self.assertEqual(str(clients[0].lease.host_address),
-                         self.pg3.local_ip4)
-
-        # remove the left over ARP entry
-        self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
-                                      self.pg3.remote_mac,
-                                      self.pg3.remote_ip4,
-                                      is_add=0)
+                         self.pg3.remote_hosts[3].ip4)
 
         #
         # remove the DHCP config
@@ -1532,6 +1556,8 @@ class TestDHCP(VppTestCase):
 
         #
         # Start the procedure again. Use requested lease time option.
+        # this time wait for the lease to expire and the client to
+        # self-destruct
         #
         hostname += "-2"
         self.pg3.admin_down()
@@ -1601,12 +1627,6 @@ class TestDHCP(VppTestCase):
         self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
         self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
 
-        # remove the left over ARP entry
-        self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
-                                      self.pg3.remote_mac,
-                                      self.pg3.remote_ip4,
-                                      is_add=0)
-
         #
         # the route should be gone after the lease expires
         #