tests: Add testcase for (reassoc) roam failure due to auth timeout
[mech_eap.git] / tests / hwsim / test_ap_roam.py
1 # Roaming tests
2 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 from remotehost import remote_compatible
8 import time
9 import logging
10 logger = logging.getLogger()
11
12 import hwsim_utils
13 import hostapd
14 from wpasupplicant import WpaSupplicant
15
16 @remote_compatible
17 def test_ap_roam_open(dev, apdev):
18     """Roam between two open APs"""
19     hapd0 = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
20     dev[0].connect("test-open", key_mgmt="NONE")
21     hwsim_utils.test_connectivity(dev[0], hapd0)
22     hapd1 = hostapd.add_ap(apdev[1], { "ssid": "test-open" })
23     dev[0].scan(type="ONLY")
24     dev[0].roam(apdev[1]['bssid'])
25     hwsim_utils.test_connectivity(dev[0], hapd1)
26     dev[0].roam(apdev[0]['bssid'])
27     hwsim_utils.test_connectivity(dev[0], hapd0)
28
29 @remote_compatible
30 def test_ap_roam_open_failed(dev, apdev):
31     """Roam failure due to rejected authentication"""
32     hapd0 = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
33     dev[0].connect("test-open", key_mgmt="NONE", scan_freq="2412")
34     hwsim_utils.test_connectivity(dev[0], hapd0)
35     params = { "ssid": "test-open", "max_num_sta" : "0" }
36     hapd1 = hostapd.add_ap(apdev[1], params)
37     bssid = hapd1.own_addr()
38
39     dev[0].scan_for_bss(bssid, freq=2412)
40     dev[0].dump_monitor()
41     if "OK" not in dev[0].request("ROAM " + bssid):
42         raise Exception("ROAM failed")
43
44     ev = dev[0].wait_event(["CTRL-EVENT-AUTH-REJECT"], 1)
45     if not ev:
46         raise Exception("CTRL-EVENT-AUTH-REJECT was not seen")
47
48     dev[0].wait_connected(timeout=5)
49     hwsim_utils.test_connectivity(dev[0], hapd0)
50
51 @remote_compatible
52 def test_ap_roam_wpa2_psk(dev, apdev):
53     """Roam between two WPA2-PSK APs"""
54     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
55     hapd0 = hostapd.add_ap(apdev[0], params)
56     dev[0].connect("test-wpa2-psk", psk="12345678")
57     hwsim_utils.test_connectivity(dev[0], hapd0)
58     hapd1 = hostapd.add_ap(apdev[1], params)
59     dev[0].scan(type="ONLY")
60     dev[0].roam(apdev[1]['bssid'])
61     hwsim_utils.test_connectivity(dev[0], hapd1)
62     dev[0].roam(apdev[0]['bssid'])
63     hwsim_utils.test_connectivity(dev[0], hapd0)
64
65 def get_blacklist(dev):
66     return dev.request("BLACKLIST").splitlines()
67
68 def test_ap_roam_with_reassoc_auth_timeout(dev, apdev, params):
69     """Roam using reassoc between two APs and authentication times out"""
70     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
71     wpas.interface_add("wlan5",
72                        drv_params="force_connect_cmd=1,force_bss_selection=1")
73
74     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
75     hapd0 = hostapd.add_ap(apdev[0], params)
76     bssid0 = hapd0.own_addr()
77
78     id = wpas.connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
79     hwsim_utils.test_connectivity(wpas, hapd0)
80
81     hapd1 = hostapd.add_ap(apdev[1], params)
82     bssid1 = hapd1.own_addr()
83     wpas.scan_for_bss(bssid1, freq=2412)
84
85     if "OK" not in wpas.request("SET_NETWORK " + str(id) + " bssid " + bssid1):
86         raise Exception("SET_NETWORK failed")
87     if "OK" not in wpas.request("SET ignore_auth_resp 1"):
88         raise Exception("SET ignore_auth_resp failed")
89     if "OK" not in wpas.request("REASSOCIATE"):
90         raise Exception("REASSOCIATE failed")
91
92     logger.info("Wait ~10s for auth timeout...")
93     time.sleep(10)
94     ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], 12)
95     if not ev:
96         raise Exception("CTRL-EVENT-SCAN-STARTED not seen");
97
98     b = get_blacklist(wpas)
99     if bssid0 in b:
100         raise Exception("Unexpected blacklist contents: " + str(b))
101
102 def test_ap_roam_wpa2_psk_failed(dev, apdev, params):
103     """Roam failure with WPA2-PSK AP due to wrong passphrase"""
104     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
105     hapd0 = hostapd.add_ap(apdev[0], params)
106     id = dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
107     hwsim_utils.test_connectivity(dev[0], hapd0)
108     params['wpa_passphrase'] = "22345678"
109     hapd1 = hostapd.add_ap(apdev[1], params)
110     bssid = hapd1.own_addr()
111     dev[0].scan_for_bss(bssid, freq=2412)
112
113     dev[0].dump_monitor()
114     if "OK" not in dev[0].request("ROAM " + bssid):
115         raise Exception("ROAM failed")
116
117     ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED",
118                             "CTRL-EVENT-CONNECTED"], 5)
119     if "CTRL-EVENT-CONNECTED" in ev:
120         raise Exception("Got unexpected CTRL-EVENT-CONNECTED")
121     if "CTRL-EVENT-SSID-TEMP-DISABLED" not in ev:
122         raise Exception("CTRL-EVENT-SSID-TEMP-DISABLED not seen")
123
124     if "OK" not in dev[0].request("SELECT_NETWORK id=" + str(id)):
125         raise Exception("SELECT_NETWORK failed")
126
127     ev = dev[0].wait_event(["CTRL-EVENT-SSID-REENABLED"], 3)
128     if not ev:
129         raise Exception("CTRL-EVENT-SSID-REENABLED not seen")
130
131     dev[0].wait_connected(timeout=5)
132     hwsim_utils.test_connectivity(dev[0], hapd0)
133
134 @remote_compatible
135 def test_ap_reassociation_to_same_bss(dev, apdev):
136     """Reassociate to the same BSS"""
137     hapd = hostapd.add_ap(apdev[0], { "ssid": "test-open" })
138     dev[0].connect("test-open", key_mgmt="NONE")
139
140     dev[0].request("REASSOCIATE")
141     dev[0].wait_connected(timeout=10, error="Reassociation timed out")
142     hwsim_utils.test_connectivity(dev[0], hapd)
143
144     dev[0].request("REATTACH")
145     dev[0].wait_connected(timeout=10, error="Reattach timed out")
146     hwsim_utils.test_connectivity(dev[0], hapd)
147
148     # Wait for previous scan results to expire to trigger new scan
149     time.sleep(5)
150     dev[0].request("REATTACH")
151     dev[0].wait_connected(timeout=10, error="Reattach timed out")
152     hwsim_utils.test_connectivity(dev[0], hapd)
153
154 @remote_compatible
155 def test_ap_roam_set_bssid(dev, apdev):
156     """Roam control"""
157     hostapd.add_ap(apdev[0], { "ssid": "test-open" })
158     hostapd.add_ap(apdev[1], { "ssid": "test-open" })
159     id = dev[0].connect("test-open", key_mgmt="NONE", bssid=apdev[1]['bssid'],
160                         scan_freq="2412")
161     if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
162         raise Exception("Unexpected BSS")
163     # for now, these are just verifying that the code path to indicate
164     # within-ESS roaming changes can be executed; the actual results of those
165     # operations are not currently verified (that would require a test driver
166     # that does BSS selection)
167     dev[0].set_network(id, "bssid", "")
168     dev[0].set_network(id, "bssid", apdev[0]['bssid'])
169     dev[0].set_network(id, "bssid", apdev[1]['bssid'])
170
171 @remote_compatible
172 def test_ap_roam_wpa2_psk_race(dev, apdev):
173     """Roam between two WPA2-PSK APs and try to hit a disconnection race"""
174     params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
175     hapd0 = hostapd.add_ap(apdev[0], params)
176     dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
177     hwsim_utils.test_connectivity(dev[0], hapd0)
178
179     params['channel'] = '2'
180     hapd1 = hostapd.add_ap(apdev[1], params)
181     dev[0].scan_for_bss(apdev[1]['bssid'], freq=2417)
182     dev[0].roam(apdev[1]['bssid'])
183     hwsim_utils.test_connectivity(dev[0], hapd1)
184     dev[0].roam(apdev[0]['bssid'])
185     hwsim_utils.test_connectivity(dev[0], hapd0)
186     # Wait at least two seconds to trigger the previous issue with the
187     # disconnection callback.
188     for i in range(3):
189         time.sleep(0.8)
190         hwsim_utils.test_connectivity(dev[0], hapd0)