Adding more DHCP test cases
diff --git a/src/test/dhcp/dhcpTest.py b/src/test/dhcp/dhcpTest.py
index 76416de..a8d7c7f 100644
--- a/src/test/dhcp/dhcpTest.py
+++ b/src/test/dhcp/dhcpTest.py
@@ -242,13 +242,159 @@
if cip == self.dhcp.seed_ip:
log.info('Got dhcp client IP %s from server %s for mac %s as desired .' %
(cip, sip, mac) )
+ assert_equal(cip, self.dhcp.seed_ip) #Negative Test Case
+
elif cip != self.dhcp.seed_ip:
log.info('Got dhcp client IP %s from server %s for mac %s .' %
(cip, sip, mac) )
log.info('The desired ip was: %s .' % self.dhcp.seed_ip)
- assert_equal(cip, self.dhcp.seed_ip)
+ assert_not_equal(cip, self.dhcp.seed_ip)
+
elif cip == None:
log.info('Got DHCP NAK')
+ def test_dhcp_server_nak_packet(self, iface = 'veth0'):
+ ''' Client sends DHCP Request for ip that is different from DHCP offer packet.'''
+ config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
+ 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
+ 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
+ self.onos_dhcp_table_load(config)
+ self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
+ cip, sip, mac = self.dhcp.only_discover()
+ log.info('Got dhcp client IP %s from server %s for mac %s .' %
+ (cip, sip, mac) )
+
+ log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
+ if (cip == None and mac != None):
+ log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
+ assert_not_equal(cip, None)
+ else:
+ new_cip, new_sip = self.dhcp.only_request('20.20.20.31', mac)
+ if new_cip == None:
+
+ log.info("Got DHCP server NAK.")
+ assert_equal(new_cip, None) #Negative Test Case
+
+
+ def test_dhcp_lease_packet(self, iface = 'veth0'):
+ ''' Client sends DHCP Discover packet for particular lease time.'''
+ config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
+ 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
+ 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
+ self.onos_dhcp_table_load(config)
+ self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
+ log.info('Sending DHCP discover with lease time of 700')
+ cip, sip, mac, lval = self.dhcp.only_discover(lease_time = True)
+
+ log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
+ if (cip == None and mac != None):
+ log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
+ assert_not_equal(cip, None)
+ elif lval != 700:
+ log.info('Getting dhcp client IP %s from server %s for mac %s with lease time %s. That is not 700.' %
+ (cip, sip, mac, lval) )
+ assert_not_equal(lval, 700)
+
+ def test_dhcp_client_request_after_reboot(self, iface = 'veth0'):
+ #''' Client sends DHCP Request after reboot.'''
+
+ config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
+ 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
+ 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
+ self.onos_dhcp_table_load(config)
+ self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
+ cip, sip, mac = self.dhcp.only_discover()
+ log.info('Got dhcp client IP %s from server %s for mac %s .' %
+ (cip, sip, mac) )
+
+ log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
+
+ if (cip == None and mac != None):
+ log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
+ assert_not_equal(cip, None)
+
+ else:
+ new_cip, new_sip = self.dhcp.only_request(cip, mac)
+ if new_cip == None:
+ log.info("Got DHCP server NAK.")
+ os.system('ifconfig '+iface+' down')
+ log.info('Client goes down.')
+ log.info('Delay for 5 seconds.')
+
+ time.sleep(5)
+
+ os.system('ifconfig '+iface+' up')
+ log.info('Client is up now.')
+
+ new_cip, new_sip = self.dhcp.only_request(cip, mac)
+ if new_cip == None:
+ log.info("Got DHCP server NAK.")
+ assert_not_equal(new_cip, None)
+ elif new_cip != None:
+ log.info("Got DHCP ACK.")
+
+
+
+ def test_dhcp_server_after_reboot(self, iface = 'veth0'):
+ ''' DHCP server goes down.'''
+ config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
+ 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
+ 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
+ self.onos_dhcp_table_load(config)
+ self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
+ cip, sip, mac = self.dhcp.only_discover()
+ log.info('Got dhcp client IP %s from server %s for mac %s .' %
+ (cip, sip, mac) )
+
+ log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
+
+ if (cip == None and mac != None):
+ log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
+ assert_not_equal(cip, None)
+
+ else:
+ new_cip, new_sip = self.dhcp.only_request(cip, mac)
+ if new_cip == None:
+ log.info("Got DHCP server NAK.")
+ assert_not_equal(new_cip, None)
+ log.info('Getting DHCP server Down.')
+
+ self.onos_ctrl.deactivate()
+
+ for i in range (0,4):
+ log.info('Checking DHCP request')
+ log.info('')
+
+ cip, sip = self.dhcp.only_request(new_cip, mac)
+
+ if cip == None and sip == None:
+ log.info('')
+ log.info('DHCP request timed out. DHCP server is down')
+ elif cip != None:
+ break
+
+ log.info('Getting DHCP server Up.')
+
+ status, _ = self.onos_ctrl.activate()
+ assert_equal(status, True)
+ time.sleep(3)
+
+ log.info('DHCP server is Up.')
+
+ for i in range (0,4):
+ log.info('Checking DHCP request')
+ log.info('')
+
+ cip, sip, mac = self.dhcp.only_discover()
+
+ if cip == None and sip == None and mac == None:
+ log.info('')
+ log.info('DHCP request timed out.')
+ elif cip != None:
+ log.info('Got reply from DHCP server. DHCP server is down')
+ break
+
+
+
diff --git a/src/test/utils/DHCP.py b/src/test/utils/DHCP.py
index 6c151ca..500c918 100644
--- a/src/test/utils/DHCP.py
+++ b/src/test/utils/DHCP.py
@@ -27,7 +27,7 @@
self.seed_ip = self.incIP(self.seed_ip)
self.seed_mac = self.ipToMac(self.seed_ip)
mac = self.seed_mac
-
+
chmac = self.macToChaddr(mac)
self.bootpmac = chmac
L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
@@ -53,61 +53,86 @@
subnet_mask = val
elif(op == 'server_id'):
server_id = val
-
+
L5 = BOOTP(chaddr=chmac, yiaddr=srcIP)
- L6 = DHCP(options=[("message-type","request"), ("server_id",server_id),
+ L6 = DHCP(options=[("message-type","request"), ("server_id",server_id),
("subnet_mask",subnet_mask), ("requested_addr",srcIP), "end"])
srp(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
self.mac_map[mac] = (srcIP, serverIP)
self.mac_inverse_map[srcIP] = (mac, serverIP)
return (srcIP, serverIP)
- def only_discover(self, mac = None, desired = False):
+ def only_discover(self, mac = None, desired = False, lease_time = False):
'''Send a DHCP discover'''
if mac is None:
mac = self.seed_mac
chmac = self.macToChaddr(mac)
+ self.bootpmac = chmac
L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
L3 = IP(src="0.0.0.0", dst="255.255.255.255")
L4 = UDP(sport=68, dport=67)
- L5 = BOOTP(ciaddr=self.seed_ip, chaddr=chmac)
+ L5 = BOOTP(chaddr=chmac)
if desired:
L6 = DHCP(options=[("message-type","discover"),("requested_addr",self.seed_ip),"end"])
- else:
+
+ elif time:
+ L6 = DHCP(options=[("message-type","discover"),("lease_time",700),"end"])
+
+ elif lease_time:
L6 = DHCP(options=[("message-type","discover"),"end"])
- resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
-
- if(resp.lastlayer().options [0][1] == 2):
- try:
- srcIP = resp.yiaddr
- serverIP = resp.siaddr
- except AttributeError:
- print "In Attribute error."
- print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
- return (None, None, None)
- return (srcIP, serverIP, mac)
-
- elif(resp.lastlayer().options [0][1] == 5):
- return (None, None, None)
+
+ resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
+ if resp == None:
+ return (None, None, None)
+
+ self.dhcpresp = resp
+ for x in resp.lastlayer().options:
+ if(x == 'end'):
+ break
+ op,val = x
+ if(op == "message-type"):
+
+ if(val == 2):
+
+ try:
+ srcIP = resp.yiaddr
+ serverIP = resp.siaddr
+ except AttributeError:
+ print "In Attribute error."
+ print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
+ return (None, None, None)
+
+ if lease_time == True:
+ for x in resp.lastlayer().options:
+ if(x == 'end'):
+ break
+ op,val = x
+ if(op == "lease_time"):
+ return (srcIP, serverIP, mac, val)
+ else:
+ return (srcIP, serverIP, mac)
+
+ elif(val == 6):
+
+ return (None, None, mac)
def only_request(self, cip, mac):
'''Send a DHCP offer'''
-
subnet_mask = "0.0.0.0"
for x in self.dhcpresp.lastlayer().options:
- if(x == 'end'):
- break
- op,val = x
- if(op == "subnet_mask"):
- subnet_mask = val
- elif(op == 'server_id'):
- server_id = val
+ if(x == 'end'):
+ break
+ op,val = x
+ if(op == "subnet_mask"):
+ subnet_mask = val
+ elif(op == 'server_id'):
+ server_id = val
L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
L3 = IP(src="0.0.0.0", dst="255.255.255.255")
@@ -116,18 +141,34 @@
L6 = DHCP(options=[("message-type","request"), ("server_id",server_id),
("subnet_mask",subnet_mask), ("requested_addr",cip), "end"])
resp=srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
- if(resp.lastlayer().options [0][1] == 5):
- try:
- srcIP = resp.yiaddr
- serverIP = resp.siaddr
- except AttributeError:
- print "In Attribute error."
- print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
- return (None, None)
- return (srcIP, serverIP)
- else:
- return (None, None)
+ if resp == None:
+ return (None, None)
+
+ for x in resp.lastlayer().options:
+ if(x == 'end'):
+ break
+ op,val = x
+ if(op == "message-type"):
+
+ if(val == 5):
+
+ try:
+ srcIP = resp.yiaddr
+ serverIP = resp.siaddr
+ except AttributeError:
+ print "In Attribute error."
+ print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
+ return (None, None)
+ self.mac_map[mac] = (srcIP, serverIP)
+ self.mac_inverse_map[srcIP] = (mac, serverIP)
+ return (srcIP, serverIP)
+
+ elif(val == 6):
+
+ return (None, None)
+
+
def discover_next(self):
'''Send next dhcp discover/request with updated mac'''
@@ -185,14 +226,14 @@
def incIP(self, ip, n=1):
'''Increment an IP'''
- if n < 1:
+ if n < 1:
return ip
o = ip.split(".")
for ii in range(3,-1,-1):
if int(o[ii]) < 255:
o[ii] = str(int(o[ii]) + 1)
break
- else:
+ else:
o[ii] = str(0)
n -= 1