2019-09-12 18:53:37 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.append('../util')
|
|
|
|
from iwd import IWD
|
|
|
|
from iwd import PSKAgent
|
2024-01-26 21:22:43 +01:00
|
|
|
from hwsim import Hwsim
|
2019-09-12 18:53:37 +02:00
|
|
|
import os
|
|
|
|
from configparser import ConfigParser
|
|
|
|
|
|
|
|
class Test(unittest.TestCase):
|
2019-09-13 19:27:28 +02:00
|
|
|
def connect_network(self, wd, device, network):
|
2024-01-26 21:22:43 +01:00
|
|
|
ordered_network = device.get_ordered_network(network, full_scan=True)
|
2019-09-13 19:27:28 +02:00
|
|
|
|
|
|
|
condition = 'not obj.connected'
|
|
|
|
wd.wait_for_object_condition(ordered_network.network_object, condition)
|
|
|
|
|
|
|
|
ordered_network.network_object.connect()
|
|
|
|
|
2020-09-14 23:02:33 +02:00
|
|
|
condition = 'obj.state == DeviceState.connected'
|
|
|
|
wd.wait_for_object_condition(device, condition)
|
2019-09-13 19:27:28 +02:00
|
|
|
|
|
|
|
device.disconnect()
|
|
|
|
|
|
|
|
condition = 'not obj.connected'
|
|
|
|
wd.wait_for_object_condition(ordered_network.network_object, condition)
|
2019-09-12 18:53:37 +02:00
|
|
|
|
|
|
|
def test_connection_success(self):
|
2024-01-26 21:22:43 +01:00
|
|
|
wd = self.wd
|
2019-09-12 18:53:37 +02:00
|
|
|
|
|
|
|
psk_agent = PSKAgent("secret123")
|
|
|
|
wd.register_psk_agent(psk_agent)
|
|
|
|
|
|
|
|
devices = wd.list_devices(1)
|
|
|
|
device = devices[0]
|
|
|
|
|
2024-01-26 21:22:43 +01:00
|
|
|
|
|
|
|
# Set the signals so that the 2.4GHz ranking will be higher
|
|
|
|
self.ssidccmp_2g_rule.signal = -2000
|
|
|
|
self.ssidccmp_5g_rule.signal = -8000
|
|
|
|
|
2019-09-13 19:27:28 +02:00
|
|
|
#
|
|
|
|
# Connect to the PSK network, then Hotspot so IWD creates 2 entries in
|
|
|
|
# the known frequency file.
|
|
|
|
#
|
2019-09-12 18:53:37 +02:00
|
|
|
|
2019-09-13 19:27:28 +02:00
|
|
|
self.connect_network(wd, device, 'ssidCCMP')
|
2019-09-12 18:53:37 +02:00
|
|
|
|
|
|
|
wd.unregister_psk_agent(psk_agent)
|
|
|
|
|
|
|
|
psk_agent = PSKAgent('abc', ('domain\\user', 'testpasswd'))
|
|
|
|
wd.register_psk_agent(psk_agent)
|
|
|
|
|
2019-09-13 19:27:28 +02:00
|
|
|
self.connect_network(wd, device, 'Hotspot')
|
2019-09-12 18:53:37 +02:00
|
|
|
|
|
|
|
wd.unregister_psk_agent(psk_agent)
|
|
|
|
|
|
|
|
psk_freqs = None
|
2019-09-13 19:27:28 +02:00
|
|
|
psk_uuid = None
|
2019-09-12 18:53:37 +02:00
|
|
|
hs20_freqs = None
|
2019-09-13 19:27:28 +02:00
|
|
|
hs20_uuid = None
|
2019-09-12 18:53:37 +02:00
|
|
|
config = ConfigParser()
|
2020-11-03 20:06:40 +01:00
|
|
|
config.read('/tmp/iwd/.known_network.freq')
|
2019-09-12 18:53:37 +02:00
|
|
|
for s in config.sections():
|
|
|
|
if os.path.basename(config[s]['name']) == 'ssidCCMP.psk':
|
|
|
|
psk_freqs = config[s]['list']
|
|
|
|
psk_freqs = psk_freqs.split(' ')
|
2019-09-13 19:27:28 +02:00
|
|
|
psk_uuid = s
|
2019-09-12 18:53:37 +02:00
|
|
|
elif os.path.basename(config[s]['name']) == 'example.conf':
|
|
|
|
hs20_freqs = config[s]['list']
|
|
|
|
hs20_freqs = hs20_freqs.split(' ')
|
2019-09-13 19:27:28 +02:00
|
|
|
hs20_uuid = s
|
2019-09-12 18:53:37 +02:00
|
|
|
|
2019-09-13 19:27:28 +02:00
|
|
|
#
|
|
|
|
# Verify the frequencies are what we expect
|
|
|
|
#
|
2019-09-12 18:53:37 +02:00
|
|
|
self.assertIsNotNone(psk_freqs)
|
2019-09-13 19:27:28 +02:00
|
|
|
self.assertIsNotNone(psk_uuid)
|
2024-01-26 21:22:43 +01:00
|
|
|
|
|
|
|
# The 2.4GHz frequency should come first, as it was ranked higher
|
|
|
|
self.assertEqual('2412', psk_freqs[0])
|
|
|
|
self.assertEqual('5180', psk_freqs[1])
|
2019-09-12 18:53:37 +02:00
|
|
|
|
|
|
|
self.assertIsNotNone(hs20_freqs)
|
2019-09-13 19:27:28 +02:00
|
|
|
self.assertIsNotNone(hs20_uuid)
|
2019-09-12 18:53:37 +02:00
|
|
|
self.assertIn('2412', hs20_freqs)
|
|
|
|
|
2019-09-13 19:27:28 +02:00
|
|
|
#
|
|
|
|
# Forget all know networks, this should remove all entries in the
|
|
|
|
# known frequencies file.
|
|
|
|
#
|
|
|
|
for n in wd.list_known_networks():
|
|
|
|
n.forget()
|
|
|
|
|
|
|
|
psk_agent = PSKAgent("secret123")
|
|
|
|
wd.register_psk_agent(psk_agent)
|
|
|
|
|
2024-01-26 21:22:43 +01:00
|
|
|
# Now set the signals so that the 5GHz ranking will be higher
|
|
|
|
self.ssidccmp_2g_rule.signal = -8000
|
|
|
|
self.ssidccmp_5g_rule.signal = -2000
|
|
|
|
|
2019-09-13 19:27:28 +02:00
|
|
|
#
|
|
|
|
# Reconnect, this should generate a completely new UUID since we
|
|
|
|
# previously forgot the network.
|
|
|
|
#
|
|
|
|
self.connect_network(wd, device, 'ssidCCMP')
|
|
|
|
|
|
|
|
wd.unregister_psk_agent(psk_agent)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Ensure that a new UUID was created and that we still have the same
|
|
|
|
# frequencies listed.
|
|
|
|
#
|
|
|
|
psk_freqs = None
|
|
|
|
psk_uuid2 = None
|
|
|
|
hs20_freqs = None
|
|
|
|
config = ConfigParser()
|
2020-11-03 20:06:40 +01:00
|
|
|
config.read('/tmp/iwd/.known_network.freq')
|
2019-09-13 19:27:28 +02:00
|
|
|
for s in config.sections():
|
|
|
|
self.assertNotEqual(os.path.basename(config[s]['name']),
|
|
|
|
'example.conf')
|
|
|
|
if os.path.basename(config[s]['name']) == 'ssidCCMP.psk':
|
|
|
|
psk_freqs = config[s]['list']
|
|
|
|
psk_freqs = psk_freqs.split(' ')
|
|
|
|
psk_uuid2 = s
|
|
|
|
|
|
|
|
self.assertIsNotNone(psk_freqs)
|
|
|
|
self.assertIsNotNone(psk_uuid2)
|
|
|
|
self.assertNotEqual(psk_uuid, psk_uuid2)
|
2024-01-26 21:22:43 +01:00
|
|
|
# Now the 5GHz frequency should be first
|
|
|
|
self.assertEqual('5180', psk_freqs[0])
|
|
|
|
self.assertEqual('2412', psk_freqs[1])
|
|
|
|
|
|
|
|
def test_maximum_frequencies(self):
|
|
|
|
psk_agent = PSKAgent("secret123")
|
|
|
|
self.wd.register_psk_agent(psk_agent)
|
|
|
|
|
|
|
|
devices = self.wd.list_devices(1)
|
|
|
|
device = devices[0]
|
|
|
|
|
|
|
|
# Connect and generate a known frequencies file
|
|
|
|
self.connect_network(self.wd, device, 'ssidCCMP')
|
|
|
|
|
|
|
|
self.wd.unregister_psk_agent(psk_agent)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Rewrite the known frequencies file to move the valid network
|
|
|
|
# frequencies to the end, past the maximum for a quick scan
|
|
|
|
#
|
|
|
|
config = ConfigParser()
|
|
|
|
config.read('/tmp/iwd/.known_network.freq')
|
|
|
|
for s in config.sections():
|
|
|
|
if os.path.basename(config[s]['name']) == 'ssidCCMP.psk':
|
|
|
|
config.set(s, 'list', "2417 2422 2427 2432 2437 2442 2447 2452 2457 2462 2467 2472 2484 2412 5180")
|
|
|
|
break
|
|
|
|
|
|
|
|
self.wd.stop()
|
|
|
|
|
|
|
|
with open('/tmp/iwd/.known_network.freq', 'w') as f:
|
|
|
|
config.write(f)
|
|
|
|
|
|
|
|
self.wd = IWD(True)
|
|
|
|
|
|
|
|
devices = self.wd.list_devices(1)
|
|
|
|
device = devices[0]
|
|
|
|
|
|
|
|
device.autoconnect = True
|
|
|
|
|
|
|
|
device.wait_for_event("autoconnect_quick")
|
|
|
|
|
|
|
|
condition = "obj.scanning == True"
|
|
|
|
self.wd.wait_for_object_condition(device, condition)
|
|
|
|
|
|
|
|
condition = "obj.scanning == False"
|
|
|
|
self.wd.wait_for_object_condition(device, condition)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Check that the quick scan didn't return any results
|
|
|
|
#
|
|
|
|
with self.assertRaises(Exception):
|
|
|
|
device.get_ordered_network("ssidCCMP", scan_if_needed=False)
|
|
|
|
|
|
|
|
device.wait_for_event("autoconnect_full")
|
|
|
|
|
|
|
|
condition = "obj.scanning == True"
|
|
|
|
self.wd.wait_for_object_condition(device, condition)
|
|
|
|
|
|
|
|
condition = "obj.scanning == False"
|
|
|
|
self.wd.wait_for_object_condition(device, condition)
|
|
|
|
|
|
|
|
#
|
|
|
|
# The full scan should now see the network
|
|
|
|
#
|
|
|
|
device.get_ordered_network("ssidCCMP", scan_if_needed=False)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.wd = IWD(True)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.wd.stop()
|
|
|
|
self.wd = None
|
2019-09-13 19:27:28 +02:00
|
|
|
|
2019-09-12 18:53:37 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
IWD.copy_to_hotspot('example.conf')
|
2019-10-25 06:18:23 +02:00
|
|
|
conf = '[General]\nDisableANQP=0\n'
|
2019-09-12 18:53:37 +02:00
|
|
|
os.system('echo "%s" > /tmp/main.conf' % conf)
|
|
|
|
|
2024-01-26 21:22:43 +01:00
|
|
|
hwsim = Hwsim()
|
|
|
|
|
|
|
|
cls.ssidccmp_2g_rule = hwsim.rules.create()
|
|
|
|
cls.ssidccmp_2g_rule.source = hwsim.get_radio('rad1').addresses[0]
|
|
|
|
cls.ssidccmp_2g_rule.enabled = True
|
|
|
|
|
|
|
|
cls.ssidccmp_5g_rule = hwsim.rules.create()
|
|
|
|
cls.ssidccmp_5g_rule.source = hwsim.get_radio('rad2').addresses[0]
|
|
|
|
cls.ssidccmp_5g_rule.enabled = True
|
|
|
|
|
2019-09-12 18:53:37 +02:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
IWD.clear_storage()
|
|
|
|
os.remove('/tmp/main.conf')
|
|
|
|
|
2024-01-26 21:22:43 +01:00
|
|
|
cls.ssidccmp_2g_rule.remove()
|
|
|
|
cls.ssidccmp_5g_rule.remove()
|
|
|
|
|
2019-09-12 18:53:37 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(exit=True)
|