connection_test.py (2339B)
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 hwsim import Hwsim 12 from hostapd import HostapdCLI 13 14 from time import sleep 15 16 class Test(unittest.TestCase): 17 18 def test_connection_success(self): 19 hwsim = Hwsim() 20 21 hostapd = HostapdCLI(config='ssidCCMP.conf') 22 radio = hwsim.get_radio('rad0') 23 24 wd = IWD() 25 26 psk_agent = PSKAgent("secret123") 27 wd.register_psk_agent(psk_agent) 28 29 devices = wd.list_devices(1) 30 device = devices[0] 31 32 ordered_network = device.get_ordered_network('ssidCCMP') 33 34 self.assertEqual(ordered_network.type, NetworkType.psk) 35 36 condition = 'not obj.connected' 37 wd.wait_for_object_condition(ordered_network.network_object, condition) 38 39 ordered_network.network_object.connect() 40 41 condition = 'obj.state == DeviceState.connected' 42 wd.wait_for_object_condition(device, condition) 43 44 # Ensure IWD is not scanning. This causes problems with mac80211_hwsim 45 # where CMD_FRAME will fail during a scan. This is due to the frame not 46 # having the same frequency as the radio (since hwsim is off-channel) 47 if device.scanning: 48 condition = 'not obj.scanning' 49 wd.wait_for_object_condition(device, condition) 50 51 # Spoof a disassociate frame. This will kick off SA Query procedure. 52 hwsim.spoof_disassociate(radio, hostapd.frequency, device.address) 53 54 # ensure hostapd responds and SA Query does not timeout 55 with self.assertRaises(TimeoutError): 56 condition = 'obj.state == DeviceState.disconnected' 57 wd.wait_for_object_condition(device, condition, 4) 58 59 # Since disassociate was spoofed we should still be connected 60 condition = 'obj.state == DeviceState.connected' 61 wd.wait_for_object_condition(device, condition) 62 63 device.disconnect() 64 65 condition = 'not obj.connected' 66 wd.wait_for_object_condition(ordered_network.network_object, condition) 67 68 wd.unregister_psk_agent(psk_agent) 69 70 @classmethod 71 def setUpClass(cls): 72 pass 73 74 @classmethod 75 def tearDownClass(cls): 76 IWD.clear_storage() 77 78 if __name__ == '__main__': 79 unittest.main(exit=True)