blob: 76416de12db7651833508b8080f6b5099bbb3b26 [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) )
245 elif cip != self.dhcp.seed_ip:
246 log.info('Got dhcp client IP %s from server %s for mac %s .' %
247 (cip, sip, mac) )
248 log.info('The desired ip was: %s .' % self.dhcp.seed_ip)
249 assert_equal(cip, self.dhcp.seed_ip)
250 elif cip == None:
251 log.info('Got DHCP NAK')
252
253
254