Merge pull request #20 from cgaonker/master

Adding DHCP options test cases.
diff --git a/src/test/dhcp/dhcpTest.py b/src/test/dhcp/dhcpTest.py
index 430b123..935fe97 100644
--- a/src/test/dhcp/dhcpTest.py
+++ b/src/test/dhcp/dhcpTest.py
@@ -340,7 +340,9 @@
 			assert_not_equal(new_cip, None)
 		elif new_cip != None:
 			log.info("Got DHCP ACK.")
-    
+		os.system('ifconfig '+iface+' up')
+		
+
 	
      
     def test_dhcp_server_after_reboot(self, iface = 'veth0'):
@@ -545,7 +547,7 @@
         self.onos_dhcp_table_load(config)
         self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
 	expected_subnet = '255.255.255.0'
-	self.dhcp.return_subnet = True
+	self.dhcp.return_option = 'subnet'
 	
 	cip, sip, mac, subnet_value = self.dhcp.only_discover()
 	log.info('Got dhcp client IP %s from server %s for mac %s .' %  
@@ -562,8 +564,225 @@
 			log.info("Got same subnet as passed in DHCP server configuration.")
 	
 		elif expected_subnet != subnet_value:
-			log.info("Not got same subnet as passed in DHCP server configuration.")
+			log.info("Not getting same subnet as passed in DHCP server configuration.")
 			assert_equal(expected_subnet, subnet_value)
 
 
+    def test_dhcp_client_sends_dhcp_request_with_wrong_subnet_mask(self, iface = 'veth0'):
+	
+	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) 
+	
+	elif cip and sip and mac:
+
+		self.dhcp.send_different_option = 'subnet'
+		log.info("Sending DHCP Request with wrong subnet mask.")
+		new_cip, new_sip = self.dhcp.only_request(cip, mac)
+		if new_cip == None:
+			
+			log.info("Got DHCP NAK.")
+			assert_not_equal(new_cip, None)
+
+		elif new_cip and new_sip:
+	
+			log.info("Got DHCP Ack despite of specifying wrong Subnet Mask in DHCP Request.")
+			log.info("Getting subnet mask as per server 's configuration.")
+		
+
+    def test_dhcp_client_expected_router_address(self, iface = 'veth0'):
+	
+	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)
+	expected_router_address = '20.20.20.1'
+	self.dhcp.return_option = 'router'
+	
+	cip, sip, mac, router_address_value = 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) 
+	
+	elif cip and sip and mac:
+		
+		if expected_router_address == router_address_value:
+			log.info("Got same router address as passed in DHCP server configuration.")
+	
+		elif expected_router_address != router_address_value:
+			log.info("Not getting same router address as passed in DHCP server configuration.")
+			assert_equal(expected_router_address, router_address_value)
+
+
+    def test_dhcp_client_sends_dhcp_request_with_wrong_router_address(self, iface = 'veth0'):
+	
+	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) 
+	
+	elif cip and sip and mac:
+
+		self.dhcp.send_different_option = 'router'
+		log.info("Sending DHCP Request with wrong router address.")
+		new_cip, new_sip = self.dhcp.only_request(cip, mac)
+		if new_cip == None:
+			
+			log.info("Got DHCP NAK.")
+			assert_not_equal(new_cip, None)
+
+		elif new_cip and new_sip:
+	
+			log.info("Got DHCP Ack despite of specifying wrong Router Address in DHCP Request.")
+			log.info("Getting Router Address as per server 's configuration.")
+		
+
+    def test_dhcp_client_expected_broadcast_address(self, iface = 'veth0'):
+	
+	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)
+	expected_broadcast_address = '20.20.20.255'
+	self.dhcp.return_option = 'broadcast_address'
+	
+	cip, sip, mac, broadcast_address_value = 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) 
+	
+	elif cip and sip and mac:
+		
+		if expected_broadcast_address == broadcast_address_value:
+			log.info("Got same router address as passed in DHCP server configuration.")
+	
+		elif expected_broadcast_address != broadcast_address_value:
+			log.info("Not getting same router address as passed in DHCP server configuration.")
+			assert_equal(expected_broadcast_address, broadcast_address_value)
+
+
+    def test_dhcp_client_sends_dhcp_request_with_wrong_broadcast_address(self, iface = 'veth0'):
+	
+	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) 
+	
+	elif cip and sip and mac:
+
+		self.dhcp.send_different_option = 'broadcast_address'
+		log.info("Sending DHCP Request with wrong broadcast address.")
+		new_cip, new_sip = self.dhcp.only_request(cip, mac)
+		if new_cip == None:
+			
+			log.info("Got DHCP NAK.")
+			assert_not_equal(new_cip, None)
+
+		elif new_cip and new_sip:
+	
+			log.info("Got DHCP Ack despite of specifying wrong Broadcast Address in DHCP Request.")
+			log.info("Getting Broadcast Address as per server 's configuration.")
+		
+    def test_dhcp_client_expected_dns_address(self, iface = 'veth0'):
+	
+	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', 'domain':'8.8.8.8'}
+        self.onos_dhcp_table_load(config)
+        self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
+	expected_dns_address = '8.8.8.8'
+	self.dhcp.return_option = 'dns'
+	
+	cip, sip, mac, dns_address_value = 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) 
+	
+	elif cip and sip and mac:
+		
+		if expected_dns_address == dns_address_value:
+			log.info("Got same DNS address as passed in DHCP server configuration.")
+	
+		elif expected_dns_address != dns_address_value:
+			log.info("Not getting same DNS address as passed in DHCP server configuration.")
+			assert_equal(expected_dns_address, dns_address_value)
+
+
+    def test_dhcp_client_sends_dhcp_request_with_wrong_dns_address(self, iface = 'veth0'):
+	
+	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', 'domain':'8.8.8.8'}
+        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) 
+	
+	elif cip and sip and mac:
+
+		self.dhcp.send_different_option = 'dns'
+		log.info("Sending DHCP Request with wrong DNS address.")
+		new_cip, new_sip = self.dhcp.only_request(cip, mac)
+		if new_cip == None:
+			
+			log.info("Got DHCP NAK.")
+			assert_not_equal(new_cip, None)
+
+		elif new_cip and new_sip:
+	
+			log.info("Got DHCP Ack despite of specifying wrong DNS Address in DHCP Request.")
+			log.info("Getting DNS Address as per server 's configuration.")
+		
 
diff --git a/src/test/utils/DHCP.py b/src/test/utils/DHCP.py
index c940aa3..34ca04b 100644
--- a/src/test/utils/DHCP.py
+++ b/src/test/utils/DHCP.py
@@ -29,9 +29,9 @@
 	self.bootpmac = None
 	self.dhcpresp = None
 	self.servermac = None
+	self.return_option = None
 	self.after_T2 = False
-	self.return_lease = False
-	self.return_subnet = False
+	self.send_different_option = None
 
     def is_mcast(self, ip):
         mcast_octet = (atol(ip) >> 24) & 0xff
@@ -123,25 +123,33 @@
            			print "In Attribute error."
             		 	print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
             		 	return (None, None, None)
-			if self.return_lease or self.return_subnet:
+
+			if self.return_option:
 				for x in resp.lastlayer().options:
         	    			if(x == 'end'):
                 				break
 	            			op,val = x
 		
 	        	    		if op == "lease_time":
-						if self.return_lease:
+						if self.return_option == 'lease':
 							return (srcIP, serverIP, mac, val)
 
 	        	    		elif op == "subnet_mask":
-						log.info("Got Field Subnet mask.")
-						if self.return_subnet:
-							log.info("Subnet Mask Returned.")
+						if self.return_option == 'subnet':
+							return (srcIP, serverIP, mac, val)		
+					elif op == "router":
+						if self.return_option == 'router':
+							return (srcIP, serverIP, mac, val)		
+					elif op == "broadcast_address":
+						if self.return_option == 'broadcast_address':
+							return (srcIP, serverIP, mac, val)		
+					elif op == "name_server":
+						if self.return_option == 'dns':
 							return (srcIP, serverIP, mac, val)		
 
 				
-			return (srcIP, serverIP, mac)
-		
+			else:
+				return (srcIP, serverIP, mac)
 		elif(val == 6):
 		
 			return (None, None, mac)
@@ -179,6 +187,21 @@
 	
 	if cl_reboot or self.after_T2:
         	L6 = DHCP(options=[("message-type","request"),("subnet_mask",subnet_mask), ("requested_addr",cip), "end"])		
+	elif self.send_different_option:
+		if self.send_different_option == 'subnet':
+	       		L6 = DHCP(options=[("message-type","request"),("server_id",server_id),
+        	                   	("subnet_mask",'255.255.252.0'), ("requested_addr",cip), "end"])
+		elif self.send_different_option == 'router':
+	       		L6 = DHCP(options=[("message-type","request"),("server_id",server_id),
+        	                   	("subnet_mask",subnet_mask), ("router",'1.1.1.1'), ("requested_addr",cip), "end"])
+		elif self.send_different_option == 'broadcast_address':
+	       		L6 = DHCP(options=[("message-type","request"),("server_id",server_id),
+        	                   	("subnet_mask",subnet_mask), ("broadcast_address",'1.1.1.1'), ("requested_addr",cip), "end"])
+
+		elif self.send_different_option == 'dns':
+	       		L6 = DHCP(options=[("message-type","request"),("server_id",server_id),
+        	                   	("subnet_mask",subnet_mask), ("name_server",'1.1.1.1'), ("requested_addr",cip), "end"])
+		
 	else:
        		L6 = DHCP(options=[("message-type","request"), ("server_id",server_id),
                            	("subnet_mask",subnet_mask), ("requested_addr",cip), "end"])
@@ -186,7 +209,10 @@
 	resp=srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
 	if resp == None:
         	return (None, None)
-        
+
+
+	self.servermac = resp.getlayer(Ether).src
+
 	for x in resp.lastlayer().options:
             	if(x == 'end'):
                 	break
@@ -194,7 +220,6 @@
             	if(op == "message-type"):
 	
 			if(val == 5):
-        	
 				try:
             				srcIP = resp.yiaddr
             				serverIP = resp.siaddr
@@ -230,6 +255,7 @@
 					return (srcIP, serverIP)
 			elif(val == 6):
 		
+				log.info("Got DHCP NAK.")
 				return (None, None)