static_test.py (7872B)
1 #!/usr/bin/python3 2 3 import unittest 4 import sys 5 6 sys.path.append('../util') 7 import iwd 8 from iwd import IWD 9 from iwd import PSKAgent 10 from iwd import NetworkType 11 from hostapd import HostapdCLI 12 import testutil 13 from config import ctx 14 import os 15 import socket 16 import gc 17 18 class Test(unittest.TestCase): 19 20 def test_connection_success(self): 21 # Use a non-default storage_dir for one of the instances, the default for the other one 22 iwd_main = IWD(True, iwd_storage_dir='/tmp/storage-main') 23 psk_agent_main = PSKAgent("secret123") 24 iwd_main.register_psk_agent(psk_agent_main) 25 dev1 = iwd_main.list_devices(1)[0] 26 27 ordered_network = dev1.get_ordered_network('ap-main') 28 29 self.assertEqual(ordered_network.type, NetworkType.psk) 30 31 condition = 'not obj.connected' 32 iwd_main.wait_for_object_condition(ordered_network.network_object, condition) 33 34 ordered_network.network_object.connect() 35 36 condition = 'obj.state == DeviceState.connected' 37 iwd_main.wait_for_object_condition(dev1, condition) 38 39 testutil.test_iface_operstate() 40 testutil.test_ifaces_connected() 41 42 testutil.test_ip_address_match(dev1.name, '192.168.1.10', 25) 43 testutil.test_ip_address_match(dev1.name, '3ffe:501:ffff:200::10', 80) 44 45 ifname = str(dev1.name) 46 # Since we're in an isolated VM with freshly created interfaces we know any routes 47 # will have been created by IWD and don't have to allow for pre-existing routes 48 # in the table. 49 # Flags: 1=RTF_UP, 2=RTF_GATEWAY 50 expected_routes4 = { 51 testutil.RouteInfo(gw=socket.inet_pton(socket.AF_INET, '192.168.1.3'), 52 flags=3, ifname=ifname), 53 testutil.RouteInfo(dst=socket.inet_pton(socket.AF_INET, '192.168.1.0'), plen=25, 54 flags=1, ifname=ifname) 55 } 56 expected_routes6 = { 57 testutil.RouteInfo(gw=socket.inet_pton(socket.AF_INET6, '3ffe:501:ffff:200::3'), 58 flags=3, ifname=ifname), 59 testutil.RouteInfo(dst=socket.inet_pton(socket.AF_INET6, '3ffe:501:ffff:200::'), plen=80, 60 flags=1, ifname=ifname), 61 } 62 63 self.maxDiff = None 64 self.assertEqual(expected_routes4, set(testutil.get_routes4(ifname))) 65 self.assertEqual(expected_routes6, set(testutil.get_routes6(ifname))) 66 67 rclog = open('/tmp/resolvconf.log', 'r') 68 entries = rclog.readlines() 69 rclog.close() 70 expected_rclog = ['-a %s.dns\n' % ifname, 'nameserver 192.168.1.4\n', 'nameserver 3ffe:501:ffff:200::4\n'] 71 # Every resolvconf -a run overwrites the previous settings. Check the last three lines 72 # of the log since we care about the end result here. 73 self.assertEqual(expected_rclog, entries[-3:]) 74 75 # Run our second client in a separate namespace to allow ACD (ARP) to 76 # work, and also be able to set identical IPs on both interfaces for 77 # the next part of this test. 78 ns0 = ctx.get_namespace('ns0') 79 80 iwd_ns0_1 = IWD(True, namespace=ns0, iwd_storage_dir='/tmp/storage-ns0-1') 81 psk_agent_ns0_1 = PSKAgent("secret123", namespace=ns0) 82 iwd_ns0_1.register_psk_agent(psk_agent_ns0_1) 83 dev2 = iwd_ns0_1.list_devices(1)[0] 84 85 ordered_network = dev2.get_ordered_network('ap-main') 86 87 condition = 'not obj.connected' 88 iwd_ns0_1.wait_for_object_condition(ordered_network.network_object, condition) 89 90 # Attempt a connection to the same AP that iwd_main is connected to 91 # using the same static config. The new client's ACD client should 92 # detect an IP conflict and not allow the device to reach the 93 # "connected" state although the DBus .Connect call will succeed. 94 with self.assertRaises(iwd.FailedEx): 95 ordered_network.network_object.connect(timeout=500) 96 97 condition = 'obj.state == DeviceState.disconnected' 98 iwd_ns0_1.wait_for_object_condition(dev2, condition, max_wait=21) 99 100 iwd_ns0_1.unregister_psk_agent(psk_agent_ns0_1) 101 del dev2 102 # Note: if any references to iwd_ns0_1 are left, the "del iwd_ns0_1" 103 # will not kill the IWD process the iwd_ns0_2 initialization will raise 104 # an exception. The iwd_ns0_1.wait_for_object_condition() above 105 # creates a circular reference (which is not wrong in itself) and 106 # gc.collect() gets rid of it. The actual solution is to eventually 107 # avoid executing anything important in .__del__ (which is wrong.) 108 gc.collect() 109 del iwd_ns0_1 110 111 iwd_ns0_2 = IWD(True, namespace=ns0, iwd_storage_dir='/tmp/storage-ns0-2') 112 psk_agent_ns0_2 = PSKAgent("secret123", namespace=ns0) 113 iwd_ns0_2.register_psk_agent(psk_agent_ns0_2) 114 dev2 = iwd_ns0_2.list_devices(1)[0] 115 116 ordered_network = dev2.get_ordered_network('ap-main') 117 118 condition = 'not obj.connected' 119 iwd_ns0_2.wait_for_object_condition(ordered_network.network_object, condition) 120 121 # Connect to the same network from a dynamically configured client. We 122 # block ICMP pings so that the DHCP server can't confirm that 123 # 192.168.1.10 is in use by dev1 and if it assigns dev2 the lowest 124 # available address, that's going to be 192.168.1.10. We also keep the 125 # second client's netdev in a separate namespace so that the kernel 126 # lets us assign the same IP. dev1's ACD implementation should then 127 # stop using this address. Yes, a quite unrealistic scenario but this 128 # lets us test our reaction to a conflict appearing after successful 129 # initial setup. 130 os.system("echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all") 131 ordered_network.network_object.connect() 132 133 condition = 'obj.state == DeviceState.connected' 134 iwd_ns0_2.wait_for_object_condition(dev2, condition) 135 136 iwd_main.wait(1) 137 # Check dev1 is now disconnected or without its IPv4 address 138 if dev1.state == iwd.DeviceState.connected: 139 testutil.test_ip_address_match(dev1.name, None) 140 141 dev1.disconnect() 142 dev2.disconnect() 143 144 condition = 'not obj.connected' 145 iwd_main.wait_for_object_condition(ordered_network.network_object, condition) 146 147 iwd_main.unregister_psk_agent(psk_agent_main) 148 149 @classmethod 150 def setUpClass(cls): 151 def remove_lease(): 152 try: 153 os.remove('/tmp/dhcpd.leases') 154 os.remove('/tmp/dhcpd.leases~') 155 except: 156 pass 157 158 hapd = HostapdCLI('ap-main.conf') 159 # TODO: This could be moved into test-runner itself if other tests ever 160 # require this functionality (p2p, FILS, etc.). Since it's simple 161 # enough it can stay here for now. 162 ctx.start_process(['ip', 'addr','add', '192.168.1.1/255.255.128.0', 163 'dev', hapd.ifname]).wait() 164 ctx.start_process(['touch', '/tmp/dhcpd.leases']).wait() 165 cls.dhcpd_pid = ctx.start_process(['dhcpd', '-f', '-cf', '/tmp/dhcpd.conf', 166 '-lf', '/tmp/dhcpd.leases', 167 hapd.ifname], cleanup=remove_lease) 168 IWD.copy_to_storage('static.psk', '/tmp/storage-main', 'ap-main.psk') 169 IWD.copy_to_storage('static.psk', '/tmp/storage-ns0-1', 'ap-main.psk') 170 171 cls.orig_path = os.environ['PATH'] 172 os.environ['PATH'] = '/tmp/test-bin:' + os.environ['PATH'] 173 IWD.copy_to_storage('resolvconf', '/tmp/test-bin') 174 175 @classmethod 176 def tearDownClass(cls): 177 cls.dhcpd_pid.kill() 178 os.system('rm -rf /tmp/resolvconf.log /tmp/test-bin /tmp/storage-*') 179 os.environ['PATH'] = cls.orig_path 180 181 if __name__ == '__main__': 182 unittest.main(exit=True)