frequency_test.py (7132B)
1 #!/usr/bin/python3 2 3 import unittest 4 import sys 5 6 sys.path.append('../util') 7 from iwd import IWD 8 from iwd import PSKAgent 9 from hwsim import Hwsim 10 import os 11 from configparser import ConfigParser 12 13 class Test(unittest.TestCase): 14 def connect_network(self, wd, device, network): 15 ordered_network = device.get_ordered_network(network, full_scan=True) 16 17 condition = 'not obj.connected' 18 wd.wait_for_object_condition(ordered_network.network_object, condition) 19 20 ordered_network.network_object.connect() 21 22 condition = 'obj.state == DeviceState.connected' 23 wd.wait_for_object_condition(device, condition) 24 25 device.disconnect() 26 27 condition = 'not obj.connected' 28 wd.wait_for_object_condition(ordered_network.network_object, condition) 29 30 def test_connection_success(self): 31 wd = self.wd 32 33 psk_agent = PSKAgent("secret123") 34 wd.register_psk_agent(psk_agent) 35 36 devices = wd.list_devices(1) 37 device = devices[0] 38 39 40 # Set the signals so that the 2.4GHz ranking will be higher 41 self.ssidccmp_2g_rule.signal = -2000 42 self.ssidccmp_5g_rule.signal = -8000 43 44 # 45 # Connect to the PSK network, then Hotspot so IWD creates 2 entries in 46 # the known frequency file. 47 # 48 49 self.connect_network(wd, device, 'ssidCCMP') 50 51 wd.unregister_psk_agent(psk_agent) 52 53 psk_agent = PSKAgent('abc', ('domain\\user', 'testpasswd')) 54 wd.register_psk_agent(psk_agent) 55 56 self.connect_network(wd, device, 'Hotspot') 57 58 wd.unregister_psk_agent(psk_agent) 59 60 psk_freqs = None 61 psk_uuid = None 62 hs20_freqs = None 63 hs20_uuid = None 64 config = ConfigParser() 65 config.read('/tmp/iwd/.known_network.freq') 66 for s in config.sections(): 67 if os.path.basename(config[s]['name']) == 'ssidCCMP.psk': 68 psk_freqs = config[s]['list'] 69 psk_freqs = psk_freqs.split(' ') 70 psk_uuid = s 71 elif os.path.basename(config[s]['name']) == 'example.conf': 72 hs20_freqs = config[s]['list'] 73 hs20_freqs = hs20_freqs.split(' ') 74 hs20_uuid = s 75 76 # 77 # Verify the frequencies are what we expect 78 # 79 self.assertIsNotNone(psk_freqs) 80 self.assertIsNotNone(psk_uuid) 81 82 # The 2.4GHz frequency should come first, as it was ranked higher 83 self.assertEqual('2412', psk_freqs[0]) 84 self.assertEqual('5180', psk_freqs[1]) 85 86 self.assertIsNotNone(hs20_freqs) 87 self.assertIsNotNone(hs20_uuid) 88 self.assertIn('2412', hs20_freqs) 89 90 # 91 # Forget all know networks, this should remove all entries in the 92 # known frequencies file. 93 # 94 for n in wd.list_known_networks(): 95 n.forget() 96 97 psk_agent = PSKAgent("secret123") 98 wd.register_psk_agent(psk_agent) 99 100 # Now set the signals so that the 5GHz ranking will be higher 101 self.ssidccmp_2g_rule.signal = -8000 102 self.ssidccmp_5g_rule.signal = -2000 103 104 # 105 # Reconnect, this should generate a completely new UUID since we 106 # previously forgot the network. 107 # 108 self.connect_network(wd, device, 'ssidCCMP') 109 110 wd.unregister_psk_agent(psk_agent) 111 112 # 113 # Ensure that a new UUID was created and that we still have the same 114 # frequencies listed. 115 # 116 psk_freqs = None 117 psk_uuid2 = None 118 hs20_freqs = None 119 config = ConfigParser() 120 config.read('/tmp/iwd/.known_network.freq') 121 for s in config.sections(): 122 self.assertNotEqual(os.path.basename(config[s]['name']), 123 'example.conf') 124 if os.path.basename(config[s]['name']) == 'ssidCCMP.psk': 125 psk_freqs = config[s]['list'] 126 psk_freqs = psk_freqs.split(' ') 127 psk_uuid2 = s 128 129 self.assertIsNotNone(psk_freqs) 130 self.assertIsNotNone(psk_uuid2) 131 self.assertNotEqual(psk_uuid, psk_uuid2) 132 # Now the 5GHz frequency should be first 133 self.assertEqual('5180', psk_freqs[0]) 134 self.assertEqual('2412', psk_freqs[1]) 135 136 def test_maximum_frequencies(self): 137 psk_agent = PSKAgent("secret123") 138 self.wd.register_psk_agent(psk_agent) 139 140 devices = self.wd.list_devices(1) 141 device = devices[0] 142 143 # Connect and generate a known frequencies file 144 self.connect_network(self.wd, device, 'ssidCCMP') 145 146 self.wd.unregister_psk_agent(psk_agent) 147 148 # 149 # Rewrite the known frequencies file to move the valid network 150 # frequencies to the end, past the maximum for a quick scan 151 # 152 config = ConfigParser() 153 config.read('/tmp/iwd/.known_network.freq') 154 for s in config.sections(): 155 if os.path.basename(config[s]['name']) == 'ssidCCMP.psk': 156 config.set(s, 'list', "2417 2422 2427 2432 2437 2442 2447 2452 2457 2462 2467 2472 2484 2412 5180") 157 break 158 159 self.wd.stop() 160 161 with open('/tmp/iwd/.known_network.freq', 'w') as f: 162 config.write(f) 163 164 self.wd = IWD(True) 165 166 devices = self.wd.list_devices(1) 167 device = devices[0] 168 169 device.autoconnect = True 170 171 device.wait_for_event("autoconnect_quick") 172 173 condition = "obj.scanning == True" 174 self.wd.wait_for_object_condition(device, condition) 175 176 condition = "obj.scanning == False" 177 self.wd.wait_for_object_condition(device, condition) 178 179 # 180 # Check that the quick scan didn't return any results 181 # 182 with self.assertRaises(Exception): 183 device.get_ordered_network("ssidCCMP", scan_if_needed=False) 184 185 device.wait_for_event("autoconnect_full") 186 187 condition = "obj.scanning == True" 188 self.wd.wait_for_object_condition(device, condition) 189 190 condition = "obj.scanning == False" 191 self.wd.wait_for_object_condition(device, condition) 192 193 # 194 # The full scan should now see the network 195 # 196 device.get_ordered_network("ssidCCMP", scan_if_needed=False) 197 198 def setUp(self): 199 self.wd = IWD(True) 200 201 def tearDown(self): 202 self.wd.stop() 203 self.wd = None 204 205 @classmethod 206 def setUpClass(cls): 207 IWD.copy_to_hotspot('example.conf') 208 conf = '[General]\nDisableANQP=0\n' 209 os.system('echo "%s" > /tmp/main.conf' % conf) 210 211 hwsim = Hwsim() 212 213 cls.ssidccmp_2g_rule = hwsim.rules.create() 214 cls.ssidccmp_2g_rule.source = hwsim.get_radio('rad1').addresses[0] 215 cls.ssidccmp_2g_rule.enabled = True 216 217 cls.ssidccmp_5g_rule = hwsim.rules.create() 218 cls.ssidccmp_5g_rule.source = hwsim.get_radio('rad2').addresses[0] 219 cls.ssidccmp_5g_rule.enabled = True 220 221 @classmethod 222 def tearDownClass(cls): 223 IWD.clear_storage() 224 os.remove('/tmp/main.conf') 225 226 cls.ssidccmp_2g_rule.remove() 227 cls.ssidccmp_5g_rule.remove() 228 229 if __name__ == '__main__': 230 unittest.main(exit=True)