blob: b76caf71ad995209e397f68d99732194ad96f93f [file] [log] [blame]
Chetan Gaonkercb122cc2016-05-10 10:58:34 -07001#!/usr/bin/env python
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002#
3# Copyright 2016-present Ciena Corporation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080017import unittest
18from nose.tools import *
19from nose.twistedtools import reactor, deferred
20from twisted.internet import defer
21from scapy.all import *
22import time
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080023import copy
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080024from DHCP import DHCPTest
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080025from OnosCtrl import OnosCtrl
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080026log.setLevel('INFO')
27
28class dhcp_exchange(unittest.TestCase):
29
30 dhcp_server_config = {
31 "ip": "10.1.11.50",
32 "mac": "ca:fe:ca:fe:ca:fe",
33 "subnet": "255.255.252.0",
34 "broadcast": "10.1.11.255",
35 "router": "10.1.8.1",
36 "domain": "8.8.8.8",
37 "ttl": "63",
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080038 "delay": "2",
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080039 "startip": "10.1.11.51",
40 "endip": "10.1.11.100"
41 }
42
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080043 app = 'org.onosproject.dhcp'
44
45 def setUp(self):
46 ''' Activate the dhcp app'''
Chetan Gaonkerc0566b52016-03-09 11:31:51 -080047 self.maxDiff = None ##for assert_equal compare outputs on failure
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080048 self.onos_ctrl = OnosCtrl(self.app)
49 status, _ = self.onos_ctrl.activate()
50 assert_equal(status, True)
51 time.sleep(3)
52
53 def teardown(self):
54 '''Deactivate the dhcp app'''
55 self.onos_ctrl.deactivate()
56
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080057 def onos_load_config(self, config):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070058 status, code = OnosCtrl.config(config)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080059 if status is False:
60 log.info('JSON request returned status %d' %code)
61 assert_equal(status, True)
Chetan Gaonkerc0566b52016-03-09 11:31:51 -080062 time.sleep(3)
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080063
64 def onos_dhcp_table_load(self, config = None):
65 dhcp_dict = {'apps' : { 'org.onosproject.dhcp' : { 'dhcp' : copy.copy(self.dhcp_server_config) } } }
66 dhcp_config = dhcp_dict['apps']['org.onosproject.dhcp']['dhcp']
67 if config:
68 for k in config.keys():
69 if dhcp_config.has_key(k):
70 dhcp_config[k] = config[k]
71 self.onos_load_config(dhcp_dict)
72
Chetan Gaonker6c68e912016-04-15 17:22:14 -070073 def send_recv(self, mac = None, update_seed = False, validate = True):
74 cip, sip = self.dhcp.discover(mac = mac, update_seed = update_seed)
75 if validate:
76 assert_not_equal(cip, None)
77 assert_not_equal(sip, None)
78 log.info('Got dhcp client IP %s from server %s for mac %s' %
79 (cip, sip, self.dhcp.get_mac(cip)[0]))
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080080 return cip,sip
81
82 def test_dhcp_1request(self, iface = 'veth0'):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080083 config = {'startip':'10.10.10.20', 'endip':'10.10.10.69',
84 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe",
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080085 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'}
86 self.onos_dhcp_table_load(config)
87 self.dhcp = DHCPTest(seed_ip = '10.10.10.1', iface = iface)
88 self.send_recv()
89
90 def test_dhcp_Nrequest(self, iface = 'veth0'):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080091 config = {'startip':'192.168.1.20', 'endip':'192.168.1.69',
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080092 'ip':'192.168.1.2', 'mac': "ca:fe:ca:fe:cc:fe",
93 'subnet': '255.255.255.0', 'broadcast':'192.168.1.255', 'router': '192.168.1.1'}
94 self.onos_dhcp_table_load(config)
95 self.dhcp = DHCPTest(seed_ip = '192.169.1.1', iface = iface)
96 ip_map = {}
97 for i in range(10):
98 cip, sip = self.send_recv(update_seed = True)
99 if ip_map.has_key(cip):
100 log.info('IP %s given out multiple times' %cip)
101 assert_equal(False, ip_map.has_key(cip))
102 ip_map[cip] = sip
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800103
104 def test_dhcp_1release(self, iface = 'veth0'):
Chetan Gaonkerc0566b52016-03-09 11:31:51 -0800105 config = {'startip':'10.10.100.20', 'endip':'10.10.100.21',
106 'ip':'10.10.100.2', 'mac': "ca:fe:ca:fe:8a:fe",
107 'subnet': '255.255.255.0', 'broadcast':'10.10.100.255', 'router':'10.10.100.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 = '10.10.100.10', iface = iface)
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800110 cip, sip = self.send_recv()
111 log.info('Releasing ip %s to server %s' %(cip, sip))
112 assert_equal(self.dhcp.release(cip), True)
113 log.info('Triggering DHCP discover again after release')
114 cip2, sip2 = self.send_recv(update_seed = True)
115 log.info('Verifying released IP was given back on rediscover')
116 assert_equal(cip, cip2)
117 log.info('Test done. Releasing ip %s to server %s' %(cip2, sip2))
118 assert_equal(self.dhcp.release(cip2), True)
119
120 def test_dhcp_Nrelease(self, iface = 'veth0'):
Chetan Gaonkerc0566b52016-03-09 11:31:51 -0800121 config = {'startip':'192.170.1.20', 'endip':'192.170.1.30',
122 'ip':'192.170.1.2', 'mac': "ca:fe:ca:fe:9a:fe",
123 'subnet': '255.255.255.0', 'broadcast':'192.170.1.255', 'router': '192.170.1.1'}
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800124 self.onos_dhcp_table_load(config)
Chetan Gaonkerc0566b52016-03-09 11:31:51 -0800125 self.dhcp = DHCPTest(seed_ip = '192.170.1.10', iface = iface)
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800126 ip_map = {}
127 for i in range(10):
128 cip, sip = self.send_recv(update_seed = True)
129 if ip_map.has_key(cip):
130 log.info('IP %s given out multiple times' %cip)
131 assert_equal(False, ip_map.has_key(cip))
132 ip_map[cip] = sip
133
134 for ip in ip_map.keys():
135 log.info('Releasing IP %s' %ip)
136 assert_equal(self.dhcp.release(ip), True)
137
138 ip_map2 = {}
139 log.info('Triggering DHCP discover again after release')
140 for i in range(len(ip_map.keys())):
141 cip, sip = self.send_recv(update_seed = True)
142 ip_map2[cip] = sip
143
144 log.info('Verifying released IPs were given back on rediscover')
Chetan Gaonkerc0566b52016-03-09 11:31:51 -0800145 if ip_map != ip_map2:
146 log.info('Map before release %s' %ip_map)
147 log.info('Map after release %s' %ip_map2)
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800148 assert_equal(ip_map, ip_map2)
Chetan Gaonker6c68e912016-04-15 17:22:14 -0700149
150
151 def test_dhcp_starvation(self, iface = 'veth0'):
152 config = {'startip':'193.170.1.20', 'endip':'193.170.1.69',
153 'ip':'193.170.1.2', 'mac': "ca:fe:c2:fe:cc:fe",
154 'subnet': '255.255.255.0', 'broadcast':'192.168.1.255', 'router': '192.168.1.1'}
155 self.onos_dhcp_table_load(config)
156 self.dhcp = DHCPTest(seed_ip = '192.169.1.1', iface = iface)
157 ip_map = {}
158 for i in range(10):
159 cip, sip = self.send_recv(update_seed = True)
160 if ip_map.has_key(cip):
161 log.info('IP %s given out multiple times' %cip)
162 assert_equal(False, ip_map.has_key(cip))
163 ip_map[cip] = sip
164
165
166 def test_dhcp_starvation(self, iface = 'veth0'):
167 '''DHCP starve'''
168 config = {'startip':'182.17.0.20', 'endip':'182.17.0.69',
169 'ip':'182.17.0.2', 'mac': "ca:fe:c3:fe:ca:fe",
170 'subnet': '255.255.255.0', 'broadcast':'182.17.0.255', 'router':'182.17.0.1'}
171 self.onos_dhcp_table_load(config)
172 self.dhcp = DHCPTest(seed_ip = '182.17.0.1', iface = iface)
173 log.info('Verifying 1 ')
174 for x in xrange(50):
175 mac = RandMAC()._fix()
176 self.send_recv(mac = mac)
177 log.info('Verifying 2 ')
178 cip, sip = self.send_recv(update_seed = True, validate = False)
179 assert_equal(cip, None)
180 assert_equal(sip, None)
Chetan Gaonkerf72ca402016-05-02 16:29:32 -0700181
182
183 def test_dhcp_same_client_multiple_discover(self, iface = 'veth0'):
184 ''' DHCP Client sending multiple discover . '''
185 config = {'startip':'10.10.10.20', 'endip':'10.10.10.69',
186 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe",
187 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'}
188 self.onos_dhcp_table_load(config)
189 self.dhcp = DHCPTest(seed_ip = '10.10.10.1', iface = iface)
190 cip, sip, mac = self.dhcp.only_discover()
191 log.info('Got dhcp client IP %s from server %s for mac %s . Not going to send DHCPREQUEST.' %
192 (cip, sip, mac) )
193 log.info('Triggering DHCP discover again.')
194 new_cip, new_sip, new_mac = self.dhcp.only_discover()
195 if cip == new_cip:
196 log.info('Got same ip for 2nd DHCP discover for client IP %s from server %s for mac %s. Triggering DHCP Request. '
197 % (new_cip, new_sip, new_mac) )
198 elif cip != new_cip:
199 log.info('Ip after 1st discover %s' %cip)
200 log.info('Map after 2nd discover %s' %new_cip)
201 assert_equal(cip, new_cip)
202
203
204 def test_dhcp_same_client_multiple_request(self, iface = 'veth0'):
205 ''' DHCP Client sending multiple repeat DHCP requests. '''
206 config = {'startip':'10.10.10.20', 'endip':'10.10.10.69',
207 'ip':'10.10.10.2', 'mac': "ca:fe:ca:fe:ca:fe",
208 'subnet': '255.255.255.0', 'broadcast':'10.10.10.255', 'router':'10.10.10.1'}
209 self.onos_dhcp_table_load(config)
210 self.dhcp = DHCPTest(seed_ip = '10.10.10.1', iface = iface)
211 log.info('Sending DHCP discover and DHCP request.')
212 cip, sip = self.send_recv()
213 mac = self.dhcp.get_mac(cip)[0]
214 log.info("Sending DHCP request again.")
215 new_cip, new_sip = self.dhcp.only_request(cip, mac)
216 if (new_cip,new_sip) == (cip,sip):
217
218 log.info('Got same ip for 2nd DHCP Request for client IP %s from server %s for mac %s.'
219 % (new_cip, new_sip, mac) )
220 elif (new_cip,new_sip):
221
222 log.info('No DHCP ACK')
223 assert_equal(new_cip, None)
224 assert_equal(new_sip, None)
225 else:
226 print "Something went wrong."
227
228 def test_dhcp_client_desired_address(self, iface = 'veth0'):
229 '''DHCP Client asking for desired IP address.'''
230 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
231 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
232 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
233 self.onos_dhcp_table_load(config)
234 self.dhcp = DHCPTest(seed_ip = '20.20.20.31', iface = iface)
235 cip, sip, mac = self.dhcp.only_discover(desired = True)
236 log.info('Got dhcp client IP %s from server %s for mac %s .' %
237 (cip, sip, mac) )
238 if cip == self.dhcp.seed_ip:
239 log.info('Got dhcp client IP %s from server %s for mac %s as desired .' %
240 (cip, sip, mac) )
241 elif cip != self.dhcp.seed_ip:
242 log.info('Got dhcp client IP %s from server %s for mac %s .' %
243 (cip, sip, mac) )
244 log.info('The desired ip was: %s .' % self.dhcp.seed_ip)
245 assert_equal(cip, self.dhcp.seed_ip)
246
247
248 def test_dhcp_client_desired_address_out_of_pool(self, iface = 'veth0'):
249 '''DHCP Client asking for desired IP address from out of pool.'''
250 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
251 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
252 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
253 self.onos_dhcp_table_load(config)
254 self.dhcp = DHCPTest(seed_ip = '20.20.20.35', iface = iface)
255 cip, sip, mac = self.dhcp.only_discover(desired = True)
256 log.info('Got dhcp client IP %s from server %s for mac %s .' %
257 (cip, sip, mac) )
258 if cip == self.dhcp.seed_ip:
259 log.info('Got dhcp client IP %s from server %s for mac %s as desired .' %
260 (cip, sip, mac) )
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700261 assert_equal(cip, self.dhcp.seed_ip) #Negative Test Case
262
Chetan Gaonkerf72ca402016-05-02 16:29:32 -0700263 elif cip != self.dhcp.seed_ip:
264 log.info('Got dhcp client IP %s from server %s for mac %s .' %
265 (cip, sip, mac) )
266 log.info('The desired ip was: %s .' % self.dhcp.seed_ip)
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700267 assert_not_equal(cip, self.dhcp.seed_ip)
268
Chetan Gaonkerf72ca402016-05-02 16:29:32 -0700269 elif cip == None:
270 log.info('Got DHCP NAK')
271
272
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700273 def test_dhcp_server_nak_packet(self, iface = 'veth0'):
274 ''' Client sends DHCP Request for ip that is different from DHCP offer packet.'''
275 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
276 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
277 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
278 self.onos_dhcp_table_load(config)
279 self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
280 cip, sip, mac = self.dhcp.only_discover()
281 log.info('Got dhcp client IP %s from server %s for mac %s .' %
282 (cip, sip, mac) )
283
284 log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
285 if (cip == None and mac != None):
286 log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
287 assert_not_equal(cip, None)
288 else:
289 new_cip, new_sip = self.dhcp.only_request('20.20.20.31', mac)
290 if new_cip == None:
291
292 log.info("Got DHCP server NAK.")
293 assert_equal(new_cip, None) #Negative Test Case
294
295
296 def test_dhcp_lease_packet(self, iface = 'veth0'):
297 ''' Client sends DHCP Discover packet for particular lease time.'''
298 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
299 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
300 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
301 self.onos_dhcp_table_load(config)
302 self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
303 log.info('Sending DHCP discover with lease time of 700')
304 cip, sip, mac, lval = self.dhcp.only_discover(lease_time = True)
305
306 log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
307 if (cip == None and mac != None):
308 log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
309 assert_not_equal(cip, None)
310 elif lval != 700:
311 log.info('Getting dhcp client IP %s from server %s for mac %s with lease time %s. That is not 700.' %
312 (cip, sip, mac, lval) )
313 assert_not_equal(lval, 700)
314
315 def test_dhcp_client_request_after_reboot(self, iface = 'veth0'):
316 #''' Client sends DHCP Request after reboot.'''
317
318 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
319 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
320 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
321 self.onos_dhcp_table_load(config)
322 self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
323 cip, sip, mac = self.dhcp.only_discover()
324 log.info('Got dhcp client IP %s from server %s for mac %s .' %
325 (cip, sip, mac) )
326
327 log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
328
329 if (cip == None and mac != None):
330 log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
331 assert_not_equal(cip, None)
332
333 else:
334 new_cip, new_sip = self.dhcp.only_request(cip, mac)
335 if new_cip == None:
336 log.info("Got DHCP server NAK.")
337 os.system('ifconfig '+iface+' down')
338 log.info('Client goes down.')
339 log.info('Delay for 5 seconds.')
340
341 time.sleep(5)
342
343 os.system('ifconfig '+iface+' up')
344 log.info('Client is up now.')
345
346 new_cip, new_sip = self.dhcp.only_request(cip, mac)
347 if new_cip == None:
348 log.info("Got DHCP server NAK.")
349 assert_not_equal(new_cip, None)
350 elif new_cip != None:
351 log.info("Got DHCP ACK.")
352
353
354
355 def test_dhcp_server_after_reboot(self, iface = 'veth0'):
356 ''' DHCP server goes down.'''
357 config = {'startip':'20.20.20.30', 'endip':'20.20.20.69',
358 'ip':'20.20.20.2', 'mac': "ca:fe:ca:fe:ca:fe",
359 'subnet': '255.255.255.0', 'broadcast':'20.20.20.255', 'router':'20.20.20.1'}
360 self.onos_dhcp_table_load(config)
361 self.dhcp = DHCPTest(seed_ip = '20.20.20.45', iface = iface)
362 cip, sip, mac = self.dhcp.only_discover()
363 log.info('Got dhcp client IP %s from server %s for mac %s .' %
364 (cip, sip, mac) )
365
366 log.info("Verifying Client 's IP and mac in DHCP Offer packet. Those should not be none, which is expected.")
367
368 if (cip == None and mac != None):
369 log.info("Verified that Client 's IP and mac in DHCP Offer packet are none, which is not expected behavior.")
370 assert_not_equal(cip, None)
371
372 else:
373 new_cip, new_sip = self.dhcp.only_request(cip, mac)
374 if new_cip == None:
375 log.info("Got DHCP server NAK.")
376 assert_not_equal(new_cip, None)
377 log.info('Getting DHCP server Down.')
378
379 self.onos_ctrl.deactivate()
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700380
381 for i in range(0,4):
382 log.info("Sending DHCP Request.")
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700383 log.info('')
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700384 new_cip, new_sip = self.dhcp.only_request(cip, mac)
385 if new_cip == None and new_sip == None:
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700386 log.info('')
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700387 log.info("DHCP Request timed out.")
388 elif new_cip and new_sip:
389 log.info("Got Reply from DHCP server.")
390 assert_equal(new_cip,None) #Neagtive Test Case
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700391
392 log.info('Getting DHCP server Up.')
393
394 status, _ = self.onos_ctrl.activate()
395 assert_equal(status, True)
396 time.sleep(3)
397
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700398 for i in range(0,4):
399 log.info("Sending DHCP Request after DHCP server is up.")
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700400 log.info('')
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700401 new_cip, new_sip = self.dhcp.only_request(cip, mac)
402 if new_cip == None and new_sip == None:
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700403 log.info('')
Chetan Gaonkerb926c642016-05-10 08:19:03 -0700404 log.info("DHCP Request timed out.")
405 elif new_cip and new_sip:
406 log.info("Got Reply from DHCP server.")
407 assert_equal(new_cip,None) #Neagtive Test Case
408
Chetan Gaonkerf1483862016-05-06 14:14:31 -0700409
410
411
Chetan Gaonkerf72ca402016-05-02 16:29:32 -0700412