connection_test.py (11571B)
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 iwd import IWD_CONFIG_DIR 12 13 from hostapd import HostapdCLI 14 from hwsim import Hwsim 15 16 import time 17 18 class Test(unittest.TestCase): 19 def test_temp_blacklist(self): 20 rule0 = self.rule0 21 rule1 = self.rule1 22 rule2 = self.rule2 23 24 bss_hostapd = self.bss_hostapd 25 wd = self.wd 26 27 rule0.signal = -8000 28 rule1.signal = -7000 29 rule2.signal = -2000 30 31 psk_agent = PSKAgent("secret123") 32 wd.register_psk_agent(psk_agent) 33 34 dev1, dev2 = wd.list_devices(2) 35 36 ordered_network = dev1.get_ordered_network("TestBlacklist", full_scan=True) 37 38 self.assertEqual(ordered_network.type, NetworkType.psk) 39 40 condition = 'not obj.connected' 41 wd.wait_for_object_condition(ordered_network.network_object, condition) 42 43 ordered_network.network_object.connect() 44 45 condition = 'obj.state == DeviceState.connected' 46 wd.wait_for_object_condition(dev1, condition) 47 48 bss_hostapd[2].wait_for_event('AP-STA-CONNECTED %s' % dev1.address) 49 50 # dev1 now connected, this should max out the first AP, causing the next 51 # connection to fail to this AP. 52 53 ordered_network = dev2.get_ordered_network("TestBlacklist", full_scan=True) 54 55 self.assertEqual(ordered_network.type, NetworkType.psk) 56 57 condition = 'not obj.connected' 58 wd.wait_for_object_condition(ordered_network.network_object, condition) 59 60 ordered_network.network_object.connect() 61 62 condition = 'obj.state == DeviceState.connected' 63 wd.wait_for_object_condition(dev2, condition) 64 65 # We should have temporarily blacklisted the first BSS, and connected 66 # to this one. 67 bss_hostapd[1].wait_for_event('AP-STA-CONNECTED %s' % dev2.address) 68 69 # Now check that the first BSS is still not blacklisted. We can 70 # disconnect dev1, opening up the AP for more connections 71 dev1.disconnect() 72 dev2.disconnect() 73 74 ordered_network = dev2.get_ordered_network("TestBlacklist") 75 76 self.assertEqual(ordered_network.type, NetworkType.psk) 77 78 condition = 'not obj.connected' 79 wd.wait_for_object_condition(ordered_network.network_object, condition) 80 81 ordered_network.network_object.connect() 82 83 condition = 'obj.state == DeviceState.connected' 84 wd.wait_for_object_condition(dev2, condition) 85 86 bss_hostapd[2].wait_for_event('AP-STA-CONNECTED %s' % dev2.address) 87 88 wd.unregister_psk_agent(psk_agent) 89 90 def test_all_blacklisted(self): 91 wd = self.wd 92 bss_hostapd = self.bss_hostapd 93 94 rule0 = self.rule0 95 rule1 = self.rule1 96 rule2 = self.rule2 97 98 psk_agent = PSKAgent(["secret123", 'secret123']) 99 wd.register_psk_agent(psk_agent) 100 101 devices = wd.list_devices(1) 102 device = devices[0] 103 104 ordered_network = device.get_ordered_network("TestBlacklist", full_scan=True) 105 106 self.assertEqual(ordered_network.type, NetworkType.psk) 107 108 condition = 'not obj.connected' 109 wd.wait_for_object_condition(ordered_network.network_object, condition) 110 111 # Have both APs drop all packets, both should get blacklisted 112 rule0.drop = True 113 rule0.enabled = True 114 rule1.drop = True 115 rule1.enabled = True 116 rule2.drop = True 117 rule2.enabled = True 118 119 with self.assertRaises(iwd.FailedEx): 120 ordered_network.network_object.connect() 121 122 rule0.drop = False 123 rule1.drop = False 124 rule2.drop = False 125 126 # Wait for scanning (likely a quick-scan) to finish, otherwise we will 127 # may not have all BSS's in the list. 128 condition = 'not obj.scanning' 129 wd.wait_for_object_condition(device, condition) 130 131 # This connect should work 132 ordered_network.network_object.connect() 133 134 condition = 'obj.state == DeviceState.connected' 135 wd.wait_for_object_condition(device, condition) 136 137 bss_hostapd[0].wait_for_event('AP-STA-CONNECTED %s' % device.address) 138 139 wd.unregister_psk_agent(psk_agent) 140 141 def test_invalid_password(self): 142 wd = self.wd 143 bss_hostapd = self.bss_hostapd 144 145 psk_agent = PSKAgent("wrong_password") 146 wd.register_psk_agent(psk_agent) 147 148 devices = wd.list_devices(1) 149 device = devices[0] 150 151 ordered_network = device.get_ordered_network("TestBlacklist", full_scan=True) 152 153 self.assertEqual(ordered_network.type, NetworkType.psk) 154 155 condition = 'not obj.connected' 156 wd.wait_for_object_condition(ordered_network.network_object, condition) 157 158 with self.assertRaises(iwd.FailedEx): 159 ordered_network.network_object.connect() 160 161 wd.unregister_psk_agent(psk_agent) 162 163 psk_agent = PSKAgent("secret123") 164 wd.register_psk_agent(psk_agent) 165 166 ordered_network.network_object.connect() 167 168 # We failed to connect bss_hostapd[0], but with a bad password. Verify 169 # that this did not trigger a blacklist and that we did reconnect 170 # successfully to bss_hostapd[0] 171 self.assertIn(device.address, bss_hostapd[0].list_sta()) 172 173 condition = 'obj.state == DeviceState.connected' 174 wd.wait_for_object_condition(device, condition) 175 176 wd.unregister_psk_agent(psk_agent) 177 178 def test_connection_success(self): 179 wd = self.wd 180 bss_hostapd = self.bss_hostapd 181 rule0 = self.rule0 182 183 psk_agent = PSKAgent("secret123") 184 wd.register_psk_agent(psk_agent) 185 186 devices = wd.list_devices(2) 187 device = devices[0] 188 189 devices[1].disconnect() 190 191 ordered_network = device.get_ordered_network("TestBlacklist", full_scan=True) 192 193 self.assertEqual(ordered_network.type, NetworkType.psk) 194 195 condition = 'not obj.connected' 196 wd.wait_for_object_condition(ordered_network.network_object, condition) 197 198 rule0.drop = True 199 200 ordered_network.network_object.connect(wait=False) 201 202 # Have AP1 drop all packets, should result in a connection timeout 203 rule0.drop = True 204 205 # Note the time so later we don't sleep any longer than required 206 start = time.time() 207 208 condition = 'obj.state == DeviceState.connected' 209 wd.wait_for_object_condition(device, condition) 210 211 # IWD should have attempted to connect to bss_hostapd[0], since its 212 # signal strength was highest. But since we dropped all packets this 213 # connect should fail, and the BSS should be blacklisted. Then we 214 # should automatically try the next BSS in the list, which is 215 # bss_hostapd[1] 216 self.assertNotIn(device.address, bss_hostapd[0].list_sta()) 217 bss_hostapd[1].wait_for_event('AP-STA-CONNECTED %s' % device.address) 218 219 rule0.drop = False 220 221 device.disconnect() 222 device.autoconnect = True 223 224 condition = 'not obj.connected' 225 wd.wait_for_object_condition(ordered_network.network_object, condition) 226 227 # Now we wait... AutoConnect should take over 228 229 condition = 'obj.state == DeviceState.connecting' 230 wd.wait_for_object_condition(device, condition) 231 232 condition = 'obj.state == DeviceState.connected' 233 wd.wait_for_object_condition(device, condition) 234 235 # Same as before, make sure we didn't connect to the blacklisted AP. 236 self.assertNotIn(device.address, bss_hostapd[0].list_sta()) 237 bss_hostapd[1].wait_for_event('AP-STA-CONNECTED %s' % device.address) 238 239 # Wait for the blacklist to expire (10 seconds) 240 elapsed = time.time() - start 241 242 if elapsed < 15: 243 wd.wait(15 - elapsed) 244 245 device.disconnect() 246 247 ordered_network = device.get_ordered_network("TestBlacklist", full_scan=True) 248 249 self.assertEqual(ordered_network.type, NetworkType.psk) 250 251 condition = 'not obj.connected' 252 wd.wait_for_object_condition(ordered_network.network_object, condition) 253 254 ordered_network.network_object.connect() 255 256 condition = 'obj.state == DeviceState.connected' 257 wd.wait_for_object_condition(device, condition) 258 259 bss_hostapd[0].wait_for_event('AP-STA-CONNECTED %s' % device.address) 260 261 self.wd.unregister_psk_agent(psk_agent) 262 263 def test_blacklist_disabled(self): 264 wd = self.wd 265 bss_hostapd = self.bss_hostapd 266 267 rule0 = self.rule0 268 rule1 = self.rule1 269 rule2 = self.rule2 270 271 psk_agent = PSKAgent(["secret123", 'secret123']) 272 wd.register_psk_agent(psk_agent) 273 274 devices = wd.list_devices(1) 275 device = devices[0] 276 277 rule0.drop = True 278 rule0.enabled = True 279 280 device.autoconnect = True 281 282 condition = 'obj.state == DeviceState.connected' 283 wd.wait_for_object_condition(device, condition) 284 285 ordered_network = device.get_ordered_network("TestBlacklist", full_scan=True) 286 287 self.assertEqual(ordered_network.type, NetworkType.psk) 288 289 # The first BSS should fail, and we should connect to the second. This 290 # should not result in a connection blacklist though since its disabled. 291 bss_hostapd[1].wait_for_event('AP-STA-CONNECTED %s' % device.address) 292 293 device.disconnect() 294 295 rule0.drop = False 296 device.autoconnect = True 297 298 # Verify the first BSS wasn't blacklisted. 299 bss_hostapd[0].wait_for_event('AP-STA-CONNECTED %s' % device.address) 300 301 def setUp(self): 302 _, _, name = self.id().split(".") 303 304 # TODO: If we have this pattern elsewhere it might be nice to turn this 305 # into a decorator e.g. 306 # 307 # @config("main.conf.disabled") 308 # @profile("TestBlacklist.psk") 309 # def test_blacklist_disabled(self) 310 # ... 311 # 312 if name == "test_blacklist_disabled": 313 IWD.copy_to_storage("main.conf.disabled", IWD_CONFIG_DIR, "main.conf") 314 IWD.copy_to_storage("TestBlacklist.psk") 315 else: 316 IWD.copy_to_storage("main.conf.default", IWD_CONFIG_DIR, "main.conf") 317 318 self.wd = IWD(True) 319 320 def tearDown(self): 321 IWD.clear_storage() 322 self.wd = None 323 self.rule0.drop = False 324 self.rule1.drop = False 325 self.rule2.drop = False 326 327 @classmethod 328 def setUpClass(cls): 329 cls.hwsim = Hwsim() 330 331 cls.bss_hostapd = [ HostapdCLI(config='ssid1.conf'), 332 HostapdCLI(config='ssid2.conf'), 333 HostapdCLI(config='ssid3.conf') ] 334 cls.bss_radio = [ cls.hwsim.get_radio('rad0'), 335 cls.hwsim.get_radio('rad1'), 336 cls.hwsim.get_radio('rad2') ] 337 338 cls.rule0 = cls.hwsim.rules.create() 339 cls.rule0.source = cls.bss_radio[0].addresses[0] 340 cls.rule0.bidirectional = True 341 cls.rule0.signal = -2000 342 cls.rule0.enabled = True 343 344 cls.rule1 = cls.hwsim.rules.create() 345 cls.rule1.source = cls.bss_radio[1].addresses[0] 346 cls.rule1.bidirectional = True 347 cls.rule1.signal = -7000 348 cls.rule1.enabled = True 349 350 cls.rule2 = cls.hwsim.rules.create() 351 cls.rule2.source = cls.bss_radio[2].addresses[0] 352 cls.rule2.bidirectional = True 353 cls.rule2.signal = -8000 354 cls.rule2.enabled = True 355 356 @classmethod 357 def tearDownClass(cls): 358 IWD.clear_storage() 359 360 cls.hwsim.rules.remove_all() 361 362 if __name__ == '__main__': 363 unittest.main(exit=True)