blob: b33a585c57e2b7664413eecc6d6e6a322380be5c [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)