blob: 1b4f37d7516ddce58f6f0e9c7579ef97e7bb6b76 [file] [log] [blame]
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -08001import unittest
2from nose.tools import *
3from nose.twistedtools import reactor, deferred
4from twisted.internet import defer
5from scapy.all import *
6import time
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -08007import copy
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -08008from DHCP import DHCPTest
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -08009from OnosCtrl import OnosCtrl
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080010log.setLevel('INFO')
11
12class dhcp_exchange(unittest.TestCase):
13
14 dhcp_server_config = {
15 "ip": "10.1.11.50",
16 "mac": "ca:fe:ca:fe:ca:fe",
17 "subnet": "255.255.252.0",
18 "broadcast": "10.1.11.255",
19 "router": "10.1.8.1",
20 "domain": "8.8.8.8",
21 "ttl": "63",
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080022 "delay": "2",
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080023 "startip": "10.1.11.51",
24 "endip": "10.1.11.100"
25 }
26
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080027 app = 'org.onosproject.dhcp'
28
29 def setUp(self):
30 ''' Activate the dhcp app'''
Chetan Gaonkerc0566b52016-03-09 11:31:51 -080031 self.maxDiff = None ##for assert_equal compare outputs on failure
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080032 self.onos_ctrl = OnosCtrl(self.app)
33 status, _ = self.onos_ctrl.activate()
34 assert_equal(status, True)
35 time.sleep(3)
36
37 def teardown(self):
38 '''Deactivate the dhcp app'''
39 self.onos_ctrl.deactivate()
40
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080041 def onos_load_config(self, config):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070042 status, code = OnosCtrl.config(config)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080043 if status is False:
44 log.info('JSON request returned status %d' %code)
45 assert_equal(status, True)
Chetan Gaonkerc0566b52016-03-09 11:31:51 -080046 time.sleep(3)
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080047
48 def onos_dhcp_table_load(self, config = None):
49 dhcp_dict = {'apps' : { 'org.onosproject.dhcp' : { 'dhcp' : copy.copy(self.dhcp_server_config) } } }
50 dhcp_config = dhcp_dict['apps']['org.onosproject.dhcp']['dhcp']
51 if config:
52 for k in config.keys():
53 if dhcp_config.has_key(k):
54 dhcp_config[k] = config[k]
55 self.onos_load_config(dhcp_dict)
56
Chetan Gaonker6c68e912016-04-15 17:22:14 -070057 def send_recv(self, mac = None, update_seed = False, validate = True):
58 cip, sip = self.dhcp.discover(mac = mac, update_seed = update_seed)
59 if validate:
60 assert_not_equal(cip, None)
61 assert_not_equal(sip, None)
62 log.info('Got dhcp client IP %s from server %s for mac %s' %
63 (cip, sip, self.dhcp.get_mac(cip)[0]))
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080064 return cip,sip
65
66 def test_dhcp_1request(self, iface = 'veth0'):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080067 config = {'startip':'10.10.10.20', 'endip':'10.10.10.69',
68 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe",
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080069 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'}
70 self.onos_dhcp_table_load(config)
71 self.dhcp = DHCPTest(seed_ip = '10.10.10.1', iface = iface)
72 self.send_recv()
73
74 def test_dhcp_Nrequest(self, iface = 'veth0'):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080075 config = {'startip':'192.168.1.20', 'endip':'192.168.1.69',
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080076 'ip':'192.168.1.2', 'mac': "ca:fe:ca:fe:cc:fe",
77 'subnet': '255.255.255.0', 'broadcast':'192.168.1.255', 'router': '192.168.1.1'}
78 self.onos_dhcp_table_load(config)
79 self.dhcp = DHCPTest(seed_ip = '192.169.1.1', iface = iface)
80 ip_map = {}
81 for i in range(10):
82 cip, sip = self.send_recv(update_seed = True)
83 if ip_map.has_key(cip):
84 log.info('IP %s given out multiple times' %cip)
85 assert_equal(False, ip_map.has_key(cip))
86 ip_map[cip] = sip
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -080087
88 def test_dhcp_1release(self, iface = 'veth0'):
Chetan Gaonkerc0566b52016-03-09 11:31:51 -080089 config = {'startip':'10.10.100.20', 'endip':'10.10.100.21',
90 'ip':'10.10.100.2', 'mac': "ca:fe:ca:fe:8a:fe",
91 'subnet': '255.255.255.0', 'broadcast':'10.10.100.255', 'router':'10.10.100.1'}
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -080092 self.onos_dhcp_table_load(config)
Chetan Gaonkerc0566b52016-03-09 11:31:51 -080093 self.dhcp = DHCPTest(seed_ip = '10.10.100.10', iface = iface)
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -080094 cip, sip = self.send_recv()
95 log.info('Releasing ip %s to server %s' %(cip, sip))
96 assert_equal(self.dhcp.release(cip), True)
97 log.info('Triggering DHCP discover again after release')
98 cip2, sip2 = self.send_recv(update_seed = True)
99 log.info('Verifying released IP was given back on rediscover')
100 assert_equal(cip, cip2)
101 log.info('Test done. Releasing ip %s to server %s' %(cip2, sip2))
102 assert_equal(self.dhcp.release(cip2), True)
103
104 def test_dhcp_Nrelease(self, iface = 'veth0'):
Chetan Gaonkerc0566b52016-03-09 11:31:51 -0800105 config = {'startip':'192.170.1.20', 'endip':'192.170.1.30',
106 'ip':'192.170.1.2', 'mac': "ca:fe:ca:fe:9a:fe",
107 'subnet': '255.255.255.0', 'broadcast':'192.170.1.255', 'router': '192.170.1.1'}
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800108 self.onos_dhcp_table_load(config)
Chetan Gaonkerc0566b52016-03-09 11:31:51 -0800109 self.dhcp = DHCPTest(seed_ip = '192.170.1.10', iface = iface)
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800110 ip_map = {}
111 for i in range(10):
112 cip, sip = self.send_recv(update_seed = True)
113 if ip_map.has_key(cip):
114 log.info('IP %s given out multiple times' %cip)
115 assert_equal(False, ip_map.has_key(cip))
116 ip_map[cip] = sip
117
118 for ip in ip_map.keys():
119 log.info('Releasing IP %s' %ip)
120 assert_equal(self.dhcp.release(ip), True)
121
122 ip_map2 = {}
123 log.info('Triggering DHCP discover again after release')
124 for i in range(len(ip_map.keys())):
125 cip, sip = self.send_recv(update_seed = True)
126 ip_map2[cip] = sip
127
128 log.info('Verifying released IPs were given back on rediscover')
Chetan Gaonkerc0566b52016-03-09 11:31:51 -0800129 if ip_map != ip_map2:
130 log.info('Map before release %s' %ip_map)
131 log.info('Map after release %s' %ip_map2)
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800132 assert_equal(ip_map, ip_map2)
Chetan Gaonker6c68e912016-04-15 17:22:14 -0700133
134
135 def test_dhcp_starvation(self, iface = 'veth0'):
136 config = {'startip':'193.170.1.20', 'endip':'193.170.1.69',
137 'ip':'193.170.1.2', 'mac': "ca:fe:c2:fe:cc:fe",
138 'subnet': '255.255.255.0', 'broadcast':'192.168.1.255', 'router': '192.168.1.1'}
139 self.onos_dhcp_table_load(config)
140 self.dhcp = DHCPTest(seed_ip = '192.169.1.1', iface = iface)
141 ip_map = {}
142 for i in range(10):
143 cip, sip = self.send_recv(update_seed = True)
144 if ip_map.has_key(cip):
145 log.info('IP %s given out multiple times' %cip)
146 assert_equal(False, ip_map.has_key(cip))
147 ip_map[cip] = sip
148
149
150 def test_dhcp_starvation(self, iface = 'veth0'):
151 '''DHCP starve'''
152 config = {'startip':'182.17.0.20', 'endip':'182.17.0.69',
153 'ip':'182.17.0.2', 'mac': "ca:fe:c3:fe:ca:fe",
154 'subnet': '255.255.255.0', 'broadcast':'182.17.0.255', 'router':'182.17.0.1'}
155 self.onos_dhcp_table_load(config)
156 self.dhcp = DHCPTest(seed_ip = '182.17.0.1', iface = iface)
157 log.info('Verifying 1 ')
158 for x in xrange(50):
159 mac = RandMAC()._fix()
160 self.send_recv(mac = mac)
161 log.info('Verifying 2 ')
162 cip, sip = self.send_recv(update_seed = True, validate = False)
163 assert_equal(cip, None)
164 assert_equal(sip, None)
Chetan Gaonkerf72ca402016-05-02 16:29:32 -0700165
166
167 def test_dhcp_same_client_multiple_discover(self, iface = 'veth0'):
168 ''' DHCP Client sending multiple discover . '''
169 config = {'startip':'10.10.10.20', 'endip':'10.10.10.69',
170 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe",
171 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'}
172 self.onos_dhcp_table_load(config)
173 self.dhcp = DHCPTest(seed_ip = '10.10.10.1', iface = iface)
174 cip, sip, mac = self.dhcp.only_discover()
175 log.info('Got dhcp client IP %s from server %s for mac %s . Not going to send DHCPREQUEST.' %
176 (cip, sip, mac) )
177 log.info('Triggering DHCP discover again.')
178 new_cip, new_sip, new_mac = self.dhcp.only_discover()
179 if cip == new_cip:
180 log.info('Got same ip for 2nd DHCP discover for client IP %s from server %s for mac %s. Triggering DHCP Request. '
181 % (new_cip, new_sip, new_mac) )
182 elif cip != new_cip:
183 log.info('Ip after 1st discover %s' %cip)
184 log.info('Map after 2nd discover %s' %new_cip)
185 assert_equal(cip, new_cip)
186
187
188 def test_dhcp_same_client_multiple_request(self, iface = 'veth0'):
189 ''' DHCP Client sending multiple repeat DHCP requests. '''
190 config = {'startip':'10.10.10.20', 'endip':'10.10.10.69',
191 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe",
192 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'}
193 self.onos_dhcp_table_load(config)
194 self.dhcp = DHCPTest(seed_ip = '10.10.10.1', iface = iface)
195 log.info('Sending DHCP discover and DHCP request.')
196 cip, sip = self.send_recv()
197 mac = self.dhcp.get_mac(cip)[0]
198 log.info("Sending DHCP request again.")
199 new_cip, new_sip = self.dhcp.only_request(cip, mac)
200 if (new_cip,new_sip) == (cip,sip):
201
202 log.info('Got same ip for 2nd DHCP Request for client IP %s from server %s for mac %s.'
203 % (new_cip, new_sip, mac) )
204 elif (new_cip,new_sip):
205
206 log.info('No DHCP ACK')
207 assert_equal(new_cip, None)
208 assert_equal(new_sip, None)
209 else:
210 print "Something went wrong."
211
212 def test_dhcp_client_desired_address(self, iface = 'veth0'):
213 '''DHCP Client asking for desired IP address.'''
214 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
215 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
216 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
217 self.onos_dhcp_table_load(config)
218 self.dhcp = DHCPTest(seed_ip = '20.20.20.31', iface = iface)
219 cip, sip, mac = self.dhcp.only_discover(desired = True)
220 log.info('Got dhcp client IP %s from server %s for mac %s .' %
221 (cip, sip, mac) )
222 if cip == self.dhcp.seed_ip:
223 log.info('Got dhcp client IP %s from server %s for mac %s as desired .' %
224 (cip, sip, mac) )
225 elif cip != self.dhcp.seed_ip:
226 log.info('Got dhcp client IP %s from server %s for mac %s .' %
227 (cip, sip, mac) )
228 log.info('The desired ip was: %s .' % self.dhcp.seed_ip)
229 assert_equal(cip, self.dhcp.seed_ip)
230
231
232 def test_dhcp_client_desired_address_out_of_pool(self, iface = 'veth0'):
233 '''DHCP Client asking for desired IP address from out of pool.'''
234 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
235 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
236 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
237 self.onos_dhcp_table_load(config)
238 self.dhcp = DHCPTest(seed_ip = '20.20.20.35', iface = iface)
239 cip, sip, mac = self.dhcp.only_discover(desired = True)
240 log.info('Got dhcp client IP %s from server %s for mac %s .' %
241 (cip, sip, mac) )
242 if cip == self.dhcp.seed_ip:
243 log.info('Got dhcp client IP %s from server %s for mac %s as desired .' %
244 (cip, sip, mac) )
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700245 assert_equal(cip, self.dhcp.seed_ip) #Negative Test Case
246
Chetan Gaonkerf72ca402016-05-02 16:29:32 -0700247 elif cip != self.dhcp.seed_ip:
248 log.info('Got dhcp client IP %s from server %s for mac %s .' %
249 (cip, sip, mac) )
250 log.info('The desired ip was: %s .' % self.dhcp.seed_ip)
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700251 assert_not_equal(cip, self.dhcp.seed_ip)
252
Chetan Gaonkerf72ca402016-05-02 16:29:32 -0700253 elif cip == None:
254 log.info('Got DHCP NAK')
255
256
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700257 def test_dhcp_server_nak_packet(self, iface = 'veth0'):
258 ''' Client sends DHCP Request for ip that is different from DHCP offer packet.'''
259 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
260 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
261 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
262 self.onos_dhcp_table_load(config)
263 self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
264 cip, sip, mac = self.dhcp.only_discover()
265 log.info('Got dhcp client IP %s from server %s for mac %s .' %
266 (cip, sip, mac) )
267
268 log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
269 if (cip == None and mac != None):
270 log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
271 assert_not_equal(cip, None)
272 else:
273 new_cip, new_sip = self.dhcp.only_request('20.20.20.31', mac)
274 if new_cip == None:
275
276 log.info("Got DHCP server NAK.")
277 assert_equal(new_cip, None) #Negative Test Case
278
279
280 def test_dhcp_lease_packet(self, iface = 'veth0'):
281 ''' Client sends DHCP Discover packet for particular lease time.'''
282 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
283 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
284 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
285 self.onos_dhcp_table_load(config)
286 self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
287 log.info('Sending DHCP discover with lease time of 700')
288 cip, sip, mac, lval = self.dhcp.only_discover(lease_time = True)
289
290 log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
291 if (cip == None and mac != None):
292 log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
293 assert_not_equal(cip, None)
294 elif lval != 700:
295 log.info('Getting dhcp client IP %s from server %s for mac %s with lease time %s. That is not 700.' %
296 (cip, sip, mac, lval) )
297 assert_not_equal(lval, 700)
298
299 def test_dhcp_client_request_after_reboot(self, iface = 'veth0'):
300 #''' Client sends DHCP Request after reboot.'''
301
302 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
303 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
304 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
305 self.onos_dhcp_table_load(config)
306 self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
307 cip, sip, mac = self.dhcp.only_discover()
308 log.info('Got dhcp client IP %s from server %s for mac %s .' %
309 (cip, sip, mac) )
310
311 log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
312
313 if (cip == None and mac != None):
314 log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
315 assert_not_equal(cip, None)
316
317 else:
318 new_cip, new_sip = self.dhcp.only_request(cip, mac)
319 if new_cip == None:
320 log.info("Got DHCP server NAK.")
321 os.system('ifconfig '+iface+' down')
322 log.info('Client goes down.')
323 log.info('Delay for 5 seconds.')
324
325 time.sleep(5)
326
327 os.system('ifconfig '+iface+' up')
328 log.info('Client is up now.')
329
330 new_cip, new_sip = self.dhcp.only_request(cip, mac)
331 if new_cip == None:
332 log.info("Got DHCP server NAK.")
333 assert_not_equal(new_cip, None)
334 elif new_cip != None:
335 log.info("Got DHCP ACK.")
336
337
338
339 def test_dhcp_server_after_reboot(self, iface = 'veth0'):
340 ''' DHCP server goes down.'''
341 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
342 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
343 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
344 self.onos_dhcp_table_load(config)
345 self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
346 cip, sip, mac = self.dhcp.only_discover()
347 log.info('Got dhcp client IP %s from server %s for mac %s .' %
348 (cip, sip, mac) )
349
350 log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
351
352 if (cip == None and mac != None):
353 log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
354 assert_not_equal(cip, None)
355
356 else:
357 new_cip, new_sip = self.dhcp.only_request(cip, mac)
358 if new_cip == None:
359 log.info("Got DHCP server NAK.")
360 assert_not_equal(new_cip, None)
361 log.info('Getting DHCP server Down.')
362
363 self.onos_ctrl.deactivate()
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700364
365 for i in range(0,4):
366 log.info("Sending DHCP Request.")
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700367 log.info('')
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700368 new_cip, new_sip = self.dhcp.only_request(cip, mac)
369 if new_cip == None and new_sip == None:
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700370 log.info('')
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700371 log.info("DHCP Request timed out.")
372 elif new_cip and new_sip:
373 log.info("Got Reply from DHCP server.")
374 assert_equal(new_cip,None) #Neagtive Test Case
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700375
376 log.info('Getting DHCP server Up.')
377
378 status, _ = self.onos_ctrl.activate()
379 assert_equal(status, True)
380 time.sleep(3)
381
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700382 for i in range(0,4):
383 log.info("Sending DHCP Request after DHCP server is up.")
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700384 log.info('')
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700385 new_cip, new_sip = self.dhcp.only_request(cip, mac)
386 if new_cip == None and new_sip == None:
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700387 log.info('')
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700388 log.info("DHCP Request timed out.")
389 elif new_cip and new_sip:
390 log.info("Got Reply from DHCP server.")
391 assert_equal(new_cip,None) #Neagtive Test Case
392
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700393
394
395
Chetan Gaonkerf72ca402016-05-02 16:29:32 -0700396