Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 1 | import unittest |
| 2 | from nose.tools import * |
| 3 | from nose.twistedtools import reactor, deferred |
| 4 | from twisted.internet import defer |
| 5 | from scapy.all import * |
| 6 | import time |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 7 | import copy |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 8 | from DHCP import DHCPTest |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 9 | from OnosCtrl import OnosCtrl |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 10 | log.setLevel('INFO') |
| 11 | |
| 12 | class 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 Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 22 | "delay": "2", |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 23 | "startip": "10.1.11.51", |
| 24 | "endip": "10.1.11.100" |
| 25 | } |
| 26 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 27 | app = 'org.onosproject.dhcp' |
| 28 | |
| 29 | def setUp(self): |
| 30 | ''' Activate the dhcp app''' |
Chetan Gaonker | c0566b5 | 2016-03-09 11:31:51 -0800 | [diff] [blame] | 31 | self.maxDiff = None ##for assert_equal compare outputs on failure |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 32 | 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 Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 41 | def onos_load_config(self, config): |
Chetan Gaonker | a2b87df | 2016-03-31 15:41:31 -0700 | [diff] [blame] | 42 | status, code = OnosCtrl.config(config) |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 43 | if status is False: |
| 44 | log.info('JSON request returned status %d' %code) |
| 45 | assert_equal(status, True) |
Chetan Gaonker | c0566b5 | 2016-03-09 11:31:51 -0800 | [diff] [blame] | 46 | time.sleep(3) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 47 | |
| 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 Gaonker | 6c68e91 | 2016-04-15 17:22:14 -0700 | [diff] [blame] | 57 | 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 Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 64 | return cip,sip |
| 65 | |
| 66 | def test_dhcp_1request(self, iface = 'veth0'): |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 67 | 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 Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 69 | '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 Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 75 | config = {'startip':'192.168.1.20', 'endip':'192.168.1.69', |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 76 | '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 Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 87 | |
| 88 | def test_dhcp_1release(self, iface = 'veth0'): |
Chetan Gaonker | c0566b5 | 2016-03-09 11:31:51 -0800 | [diff] [blame] | 89 | 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 Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 92 | self.onos_dhcp_table_load(config) |
Chetan Gaonker | c0566b5 | 2016-03-09 11:31:51 -0800 | [diff] [blame] | 93 | self.dhcp = DHCPTest(seed_ip = '10.10.100.10', iface = iface) |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 94 | 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 Gaonker | c0566b5 | 2016-03-09 11:31:51 -0800 | [diff] [blame] | 105 | 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 Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 108 | self.onos_dhcp_table_load(config) |
Chetan Gaonker | c0566b5 | 2016-03-09 11:31:51 -0800 | [diff] [blame] | 109 | self.dhcp = DHCPTest(seed_ip = '192.170.1.10', iface = iface) |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 110 | 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 Gaonker | c0566b5 | 2016-03-09 11:31:51 -0800 | [diff] [blame] | 129 | if ip_map != ip_map2: |
| 130 | log.info('Map before release %s' %ip_map) |
| 131 | log.info('Map after release %s' %ip_map2) |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 132 | assert_equal(ip_map, ip_map2) |
Chetan Gaonker | 6c68e91 | 2016-04-15 17:22:14 -0700 | [diff] [blame] | 133 | |
| 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 Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 165 | |
| 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 | |