connection_test.py (4796B)
1 #!/usr/bin/python3 2 3 import unittest 4 import sys 5 6 sys.path.append('../util') 7 import iwd 8 import os 9 from time import sleep 10 from iwd import IWD 11 from iwd import NetworkType 12 from iwd import PSKAgent 13 14 class Test(unittest.TestCase): 15 def profile_is_encrypted(self, profile): 16 with open('/tmp/iwd/' + profile) as f: 17 contents = f.read() 18 19 if 'Passphrase' in contents: 20 return False 21 22 return True 23 24 def validate(self, wd): 25 devices = wd.list_devices(1) 26 device = devices[0] 27 28 ordered_network = device.get_ordered_network('ssidCCMP') 29 30 self.assertEqual(ordered_network.type, NetworkType.psk) 31 32 condition = 'not obj.connected' 33 wd.wait_for_object_condition(ordered_network.network_object, condition) 34 35 ordered_network.network_object.connect() 36 37 condition = 'obj.state == DeviceState.connected' 38 wd.wait_for_object_condition(device, condition) 39 40 device.disconnect() 41 42 condition = 'not obj.connected' 43 wd.wait_for_object_condition(ordered_network.network_object, condition) 44 45 # Tests that an existing plaintext profile gets encrypted 46 def test_new_profile(self): 47 IWD.copy_to_storage('ssidCCMP.psk') 48 49 mtime = os.path.getmtime('/tmp/iwd/' + 'ssidCCMP.psk') 50 self.assertFalse(self.profile_is_encrypted('ssidCCMP.psk')) 51 52 sleep(1) 53 54 wd = IWD(True) 55 56 # Make sure profile was accepted 57 condition = 'len(obj.list_known_networks()) == 1' 58 wd.wait_for_object_condition(wd, condition) 59 60 # Check the file was modified (should be encrypted now) 61 self.assertNotEqual(mtime, os.path.getmtime('/tmp/iwd/' + 'ssidCCMP.psk')) 62 63 self.validate(wd) 64 65 self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk')) 66 67 # Tests that a new connection with agent gets written to an encrypted profile 68 def test_agent_profile(self): 69 wd = IWD(True) 70 71 psk_agent = PSKAgent("secret123") 72 wd.register_psk_agent(psk_agent) 73 74 with self.assertRaises(FileNotFoundError): 75 self.profile_is_encrypted('ssidCCMP.psk') 76 77 self.validate(wd) 78 79 self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk')) 80 81 wd.unregister_psk_agent(psk_agent) 82 83 # Tests that an invalid profile gets re-written after an agent request 84 def test_invalid_profile_rewritten(self): 85 bad_config = '[Security]\nPassphrase=incorrect\n' 86 os.system('echo "%s" > /tmp/iwd/ssidCCMP.psk' % bad_config) 87 88 wd = IWD(True) 89 90 condition = 'len(obj.list_known_networks()) == 1' 91 wd.wait_for_object_condition(wd, condition) 92 93 # IWD should still encrypt the profile automatically 94 self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk')) 95 96 # This should fail 97 with self.assertRaises(iwd.FailedEx): 98 self.validate(wd) 99 100 psk_agent = PSKAgent("secret123") 101 wd.register_psk_agent(psk_agent) 102 103 self.validate(wd) 104 105 self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk')) 106 107 # Tests that a profile that doesn't decrypt won't become a known network 108 def test_decryption_failure(self): 109 bad_config = \ 110 ''' 111 [Security] 112 EncryptedSalt=000102030405060708090a0b0c0d0e0f 113 EncryptedSecurity=aabbccddeeff00112233445566778899 114 ''' 115 os.system('echo "%s" > /tmp/iwd/ssidCCMP.psk' % bad_config) 116 117 wd = IWD(True) 118 119 self.assertEqual(wd.list_known_networks(), []) 120 121 # This test starts and stops IWD so quickly the DBus utilities don't 122 # even have a chance to set up the Device interface object which causes 123 # exceptions on the next test as the InterfaceAdded signals arrive. This 124 # allows the device interface to get set up before ending the test. 125 wd.list_devices(1) 126 127 def test_runtime_profile(self): 128 wd = IWD(True) 129 130 self.assertEqual(wd.list_known_networks(), []) 131 132 # Add profile after IWD starts 133 IWD.copy_to_storage('ssidCCMP.psk') 134 135 self.validate(wd) 136 137 # Should now be encrypted 138 self.assertTrue(self.profile_is_encrypted('ssidCCMP.psk')) 139 140 with open('/tmp/iwd/ssidCCMP.psk') as f: 141 profile = f.read() 142 143 # Edit the profile, corrupting it 144 profile.replace('EncryptedSecurity=', 'EncryptedSecurity=00') 145 146 devices = wd.list_devices(1) 147 device = devices[0] 148 condition = 'obj.state == DeviceState.disconnected' 149 wd.wait_for_object_condition(device, condition) 150 151 def tearDown(self): 152 IWD.clear_storage() 153 154 @classmethod 155 def setUpClass(cls): 156 os.environ['CREDENTIALS_DIRECTORY'] = '/tmp' 157 158 @classmethod 159 def tearDownClass(cls): 160 IWD.clear_storage() 161 162 if __name__ == '__main__': 163 unittest.main(exit=True)