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