connection_test.py (3231B)
1 #! /usr/bin/python3 2 3 import unittest 4 import sys 5 import time 6 7 sys.path.append('../util') 8 import iwd 9 from iwd import IWD 10 from iwd import NetworkType 11 from hwsim import Hwsim 12 13 class TstAgent(iwd.SignalAgent): 14 def handle_new_level(self, path, level): 15 self.device_path = path 16 self.level = level 17 self.calls += 1 18 19 class Test(unittest.TestCase): 20 def test_rssi_agent(self): 21 rule = Hwsim().rules.create() 22 rule.signal = -4000 23 rule.enabled = True 24 25 wd = IWD() 26 27 device = wd.list_devices(1)[0] 28 29 # Register agent early to catch any unexpected notifications 30 agent = TstAgent() 31 agent.calls = 0 32 device.register_signal_agent(agent, [-20, -40, -60, -80]) 33 34 ordered_network = device.get_ordered_network('TestOpen', full_scan=True) 35 36 self.assertEqual(ordered_network.type, NetworkType.open) 37 self.assertEqual(ordered_network.signal_strength, -4000) 38 39 condition = 'not obj.connected' 40 wd.wait_for_object_condition(ordered_network.network_object, condition) 41 42 ordered_network.network_object.connect() 43 44 condition = 'obj.state == DeviceState.connected' 45 wd.wait_for_object_condition(device, condition) 46 47 # Test with 5 signal strength levels first, then with 3 48 49 def test_list(rssi_pairs): 50 # Roughly test big jumps in RSSI value that cross from one 51 # level range to another, then with small changes that shouldn't 52 # cause any notification. 53 # Allow 3 secs for the agent to receive the final signal 54 # strength level number and allow more than one notification 55 # until it reaches the target value because the kernel (mac80211 56 # at least) uses a moving-window average value over the last RSSI 57 # measurements received from the driver and it changes gradually. 58 # Normally 1 second is enough. 59 for centre, level in rssi_pairs: 60 rule.signal = centre 61 agent.level = -1 62 condition = 'obj.level == ' + str(level) 63 wd.wait_for_object_condition(agent, condition) 64 65 self.assertTrue(agent.calls > 0) 66 self.assertEqual(agent.device_path, device.device_path) 67 agent.calls = 0 68 69 for offset in [-900, 500, -100, 900]: 70 rule.signal = centre - offset 71 wd.wait(0.5) 72 73 self.assertEqual(agent.calls, 0) 74 75 test_list([(-7000, 3), (-1000, 0), (-3000, 1), (-5000, 2), 76 (-7000, 3), (-1000, 0), (-5000, 2)]) 77 device.unregister_signal_agent(agent) 78 device.register_signal_agent(agent, [-35, -65]) 79 test_list([(-1500, 0), (-5000, 1), (-7500, 2), (-1500, 0)]) 80 81 device.disconnect() 82 83 condition = 'not obj.connected' 84 wd.wait_for_object_condition(ordered_network.network_object, condition) 85 86 self.assertEqual(agent.calls, 0) 87 device.unregister_signal_agent(agent) 88 89 @classmethod 90 def setUpClass(cls): 91 pass 92 93 @classmethod 94 def tearDownClass(cls): 95 IWD.clear_storage() 96 97 if __name__ == '__main__': 98 unittest.main(exit=True)