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) |