connection_test.py (8167B)
1 #!/usr/bin/python3 2 3 from typing import Iterable 4 import unittest 5 import sys 6 7 sys.path.append('../util') 8 from iwd import IWD 9 from iwd import NetworkType 10 from iwd import PSKAgent 11 from hlrauc import AuthCenter 12 from ofono import Ofono 13 from config import ctx 14 from hostapd import HostapdCLI 15 16 import testutil 17 import traceback 18 19 class Test(unittest.TestCase): 20 def copy_network(self, name): 21 IWD.copy_to_storage(name, name='ssidEAP.8021x') 22 self.wd.wait_for_object_condition(self.wd, 23 '"ssidEAP" in [n.name for n in obj.list_known_networks()]') 24 25 def remove_network(self): 26 networks = self.wd.list_known_networks() 27 [n.forget() for n in networks if n.name == 'ssidEAP'] 28 self.wd.wait_for_object_condition(self.wd, 29 '"ssidEAP" not in [n.name for n in obj.list_known_networks()]') 30 31 def validate_connection(self, wd, *secrets): 32 if secrets: 33 psk_agent = PSKAgent(*secrets) 34 wd.register_psk_agent(psk_agent) 35 36 devices = wd.list_devices(1) 37 self.assertIsNotNone(devices) 38 device = devices[0] 39 40 ordered_network = device.get_ordered_network('ssidEAP') 41 42 self.assertEqual(ordered_network.type, NetworkType.eap) 43 44 condition = 'not obj.connected' 45 wd.wait_for_object_condition(ordered_network.network_object, condition) 46 47 ordered_network.network_object.connect() 48 49 condition = 'obj.state == DeviceState.connected' 50 wd.wait_for_object_condition(device, condition) 51 52 testutil.test_iface_operstate() 53 testutil.test_ifaces_connected() 54 55 self.hostapd.eapol_reauth(device.address) 56 57 if secrets: 58 wd.unregister_psk_agent(psk_agent) 59 60 device.disconnect() 61 62 condition = 'not obj.connected' 63 wd.wait_for_object_condition(ordered_network.network_object, condition) 64 65 # 66 # EAP-AKA 67 # 68 def test_eap_aka(self): 69 if not ctx.is_process_running('ofonod'): 70 self.skipTest("ofono not running") 71 72 ofono = Ofono() 73 ofono.enable_modem('/phonesim') 74 ofono.wait_for_sim_auth() 75 76 auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim/aka.db') 77 78 self.copy_network('sim/ssidEAP-AKA.8021x') 79 80 try: 81 self.validate_connection(self.wd) 82 finally: 83 auth.stop() 84 85 # 86 # EAP-AKA' 87 # 88 def test_eap_aka_prime(self): 89 if not ctx.is_process_running('ofonod'): 90 self.skipTest("ofono not running") 91 92 ofono = Ofono() 93 ofono.enable_modem('/phonesim') 94 ofono.wait_for_sim_auth() 95 96 auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim/aka.db') 97 98 self.copy_network('sim/ssidEAP-AKA-prime.8021x') 99 100 try: 101 self.validate_connection(self.wd) 102 finally: 103 auth.stop() 104 105 # 106 # EAP-SIM 107 # 108 def test_eap_sim(self): 109 if not ctx.is_process_running('ofonod'): 110 self.skipTest("ofono not running") 111 112 ofono = Ofono() 113 ofono.enable_modem('/phonesim') 114 ofono.wait_for_sim_auth() 115 116 auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim/sim.db') 117 118 self.copy_network('sim/ssidEAP-SIM.8021x') 119 120 try: 121 self.validate_connection(self.wd) 122 finally: 123 auth.stop() 124 125 # 126 # EAP-MSCHAPv2 127 # 128 # * Credentials in 8021x file 129 # * Password-Hash in 8021x file 130 # * Agent request for password 131 # * Agent request for user + password 132 # 133 def test_eap_mschapv2(self): 134 self.copy_network('mschapv2/ssidEAP-MSCHAPV2.8021x') 135 self.validate_connection(self.wd) 136 137 self.copy_network('mschapv2/ssidEAP-MSCHAPV2-hash.8021x') 138 self.validate_connection(self.wd) 139 140 self.copy_network('mschapv2/ssidEAP-MSCHAPV2-nopass.8021x') 141 self.validate_connection(self.wd, [], ('mschapv2@example.com', 'Password')) 142 143 self.copy_network('mschapv2/ssidEAP-MSCHAPV2-nouserpass.8021x') 144 self.validate_connection(self.wd, [], ('mschapv2@example.com', 'Password')) 145 146 # 147 # EAP-PEAP 148 # 149 # * Test all combinations of PEAP, PEAPv0, PEAPv1 with MD5, GTC, MSCHAPv2 150 # 151 def test_eap_peap(self): 152 for ver in ['PEAP', 'PEAPv0', 'PEAPv1']: 153 for inner in ['MD5', 'GTC', 'MSCHAPv2']: 154 self.copy_network('peap/ssidEAP-%s-%s.8021x' % (ver, inner)) 155 156 try: 157 self.validate_connection(self.wd) 158 except Exception as e: 159 # Catch an error here and print the actual PEAP combo that failed 160 traceback.print_exc() 161 raise Exception("%s-%s test failed" % (ver, inner)) 162 163 self.remove_network() 164 165 # 166 # EAP-PEAP + SIM 167 # 168 # * Tests EAP-PEAP + SIM separately to allow skipping if ofono is not found 169 # 170 def test_eap_peap_sim(self): 171 if not ctx.is_process_running('ofonod'): 172 self.skipTest("ofono not running") 173 174 ofono = Ofono() 175 ofono.enable_modem('/phonesim') 176 ofono.wait_for_sim_auth() 177 178 auth = AuthCenter('/tmp/hlrauc.sock', '/tmp/sim/sim.db') 179 180 for ver in ['PEAP', 'PEAPv0', 'PEAPv1']: 181 self.copy_network('peap/ssidEAP-%s-SIM.8021x' % ver) 182 183 try: 184 self.validate_connection(self.wd) 185 except Exception as e: 186 # Catch an error here and print the actual PEAP combo that failed 187 traceback.print_exc() 188 auth.stop() 189 raise Exception("%s-SIM test failed" % ver) 190 191 self.remove_network() 192 193 auth.stop() 194 195 # 196 # EAP-PWD 197 # 198 def test_eap_pwd(self): 199 self.copy_network('ssidEAP-PWD.8021x') 200 201 self.validate_connection(self.wd) 202 203 # 204 # EAP-TLS 205 # 206 # * Encrypted private key, passphrase in 8021x file 207 # * Unencrypted private key 208 # * Encrypted private key, passphrase provided by agent 209 # * Embedded PEM inside 8021x file 210 # * KeyBundle 211 # 212 def test_eap_tls(self): 213 for name, secrets in [('keypass', None), ('nokeypass', None), 214 ('des-ede3', 'abc'), ('embedded', None), ('keybundle', None)]: 215 self.copy_network('tls/ssidEAP-TLS-%s.8021x' % name) 216 try: 217 self.validate_connection(self.wd, secrets) 218 except Exception as e: 219 traceback.print_exc() 220 raise Exception('EAP-TLS (%s) failed' % name) 221 222 self.remove_network() 223 224 # 225 # EAP-TTLS 226 # 227 # * CHAP, MD5, MSCHAPV2 as phase 2 228 # * Tunneled-MSCHAP, Tunneled-MSCHAPV2, Tunneled-PAP as phase 2 229 # 230 def test_eap_ttls(self): 231 for name, secrets in [('CHAP', ('ttls@example.com', ('ttls-chap-phase2@example.com', 'Password'))), 232 ('MD5', None), 233 ('MSCHAPV2', ('ttls@example.com', ('mschapv2-phase2@example.com', 'Password'))), 234 ('Tunneled-MSCHAP', ('ttls@example.com', ('ttls-mschap-phase2@example.com', 'Password'))), 235 ('Tunneled-MSCHAPV2', ('ttls@example.com', ('ttls-mschapv2-phase2@example.com', 'Password'))), 236 ('Tunneled-PAP', ('ttls@example.com', ('ttls-pap-phase2@example.com', 'Password')))]: 237 self.copy_network('ttls/ssidEAP-TTLS-%s.8021x' % name) 238 try: 239 if isinstance(secrets, Iterable): 240 self.validate_connection(self.wd, *secrets) 241 else: 242 self.validate_connection(self.wd, None) 243 except Exception as e: 244 traceback.print_exc() 245 raise Exception('EAP-TTLS (%s) failed' % name) 246 247 self.remove_network() 248 249 def setUp(self): 250 IWD.clear_storage() 251 252 def tearDown(self): 253 self.remove_network() 254 255 @classmethod 256 def setUpClass(cls): 257 cls.wd = IWD() 258 cls.hostapd = HostapdCLI() 259 260 @classmethod 261 def tearDownClass(cls): 262 cls.wd = None 263 IWD.clear_storage() 264 265 if __name__ == '__main__': 266 unittest.main(exit=True)