autoconnect_test.py (3839B)
1 #!/usr/bin/python3 2 3 import unittest 4 import sys 5 import os 6 7 sys.path.append('../util') 8 from iwd import IWD 9 from iwd import IWD_CONFIG_DIR 10 from iwd import NetworkType 11 from hostapd import HostapdCLI 12 import testutil 13 14 class Test(unittest.TestCase): 15 16 def test_connection_success(self): 17 wd = self.wd 18 19 hapd_hotspot = HostapdCLI(config='ssidHotspot.conf') 20 hapd_wpa = HostapdCLI(config='ssidWPA2-1.conf') 21 22 self.assertEqual(len(wd.list_known_networks()), 2) 23 24 devices = wd.list_devices(1) 25 device = devices[0] 26 device.autoconnect = True 27 28 condition = 'obj.scanning' 29 wd.wait_for_object_condition(device, condition) 30 31 condition = 'not obj.scanning' 32 wd.wait_for_object_condition(device, condition) 33 34 wpa_network = device.get_ordered_network("ssidWPA2-1") 35 self.assertEqual(wpa_network.type, NetworkType.psk) 36 37 # 38 # First make sure we can connect to a provisioned, non-Hotspot network, 39 # while there are hotspot networks in range. This should result in 40 # autoconnect *after* ANQP is performed 41 # 42 condition = 'obj.state == DeviceState.connected' 43 wd.wait_for_object_condition(device, condition) 44 45 testutil.test_iface_operstate() 46 testutil.test_ifaces_connected(device.name, hapd_wpa.ifname) 47 48 # 49 # Remove provisioning file, this should cause a disconnect. 50 # 51 os.remove("/tmp/iwd/ssidWPA2-1.psk") 52 53 condition = 'obj.state == DeviceState.disconnected' 54 wd.wait_for_object_condition(device, condition) 55 56 condition = 'len(obj.list_known_networks()) == 1' 57 wd.wait_for_object_condition(wd, condition) 58 59 condition = 'obj.scanning' 60 wd.wait_for_object_condition(device, condition) 61 62 condition = 'not obj.scanning' 63 wd.wait_for_object_condition(device, condition) 64 65 hotspot_network = device.get_ordered_network("Hotspot") 66 self.assertEqual(hotspot_network.type, NetworkType.eap) 67 68 # 69 # Since there are no other provisioned networks, we should do ANQP and 70 # autoconnect to the hotspot network. 71 # 72 condition = 'obj.state == DeviceState.connected' 73 wd.wait_for_object_condition(device, condition) 74 75 testutil.test_iface_operstate() 76 testutil.test_ifaces_connected(device.name, hapd_hotspot.ifname) 77 78 os.remove('/tmp/iwd/hotspot/autoconnect.conf') 79 80 # 81 # make sure removal of hotspot conf file resulted in disconnect 82 # 83 condition = 'obj.state == DeviceState.disconnected' 84 wd.wait_for_object_condition(device, condition) 85 86 IWD.copy_to_storage('ssidWPA2-1.psk') 87 88 condition = 'len(obj.list_known_networks()) == 1' 89 wd.wait_for_object_condition(wd, condition) 90 91 condition = 'obj.scanning' 92 wd.wait_for_object_condition(device, condition) 93 94 condition = 'not obj.scanning' 95 wd.wait_for_object_condition(device, condition) 96 97 hotspot_network = device.get_ordered_network("ssidWPA2-1") 98 self.assertEqual(hotspot_network.type, NetworkType.psk) 99 100 condition = 'obj.state == DeviceState.connected' 101 wd.wait_for_object_condition(device, condition) 102 103 testutil.test_iface_operstate() 104 testutil.test_ifaces_connected(device.name, hapd_wpa.ifname) 105 106 device.disconnect() 107 108 @classmethod 109 def setUpClass(cls): 110 IWD.copy_to_hotspot('autoconnect.conf') 111 IWD.copy_to_storage('ssidWPA2-1.psk') 112 IWD.copy_to_storage('anqp_enabled.conf', storage_dir=IWD_CONFIG_DIR, name='main.conf') 113 114 cls.wd = IWD(True) 115 116 @classmethod 117 def tearDownClass(cls): 118 IWD.clear_storage() 119 os.remove('/tmp/main.conf') 120 121 cls.wd = None 122 123 if __name__ == '__main__': 124 unittest.main(exit=True)