]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_ap_roam.py
tests: Python coding style cleanup (pylint3 bad-whitespace)
[thirdparty/hostap.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_reconnect_auth_timeout(dev, apdev, params):
69 """Reconnect to 2nd AP 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 wpas.scan_for_bss(bssid0, freq=2412)
79 id = wpas.connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
80 hwsim_utils.test_connectivity(wpas, hapd0)
81
82 hapd1 = hostapd.add_ap(apdev[1], params)
83 bssid1 = hapd1.own_addr()
84
85 wpas.request("BLACKLIST " + bssid0)
86
87 wpas.scan_for_bss(bssid1, freq=2412)
88 wpas.request("DISCONNECT")
89 if "OK" not in wpas.request("SET ignore_auth_resp 1"):
90 raise Exception("SET ignore_auth_resp failed")
91 if "OK" not in wpas.request("ENABLE_NETWORK " + str(id)):
92 raise Exception("ENABLE_NETWORK failed")
93 if "OK" not in wpas.request("SELECT_NETWORK " + str(id)):
94 raise Exception("SELECT_NETWORK failed")
95
96 logger.info("Wait ~10s for auth timeout...")
97 time.sleep(10)
98 ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], 12)
99 if not ev:
100 raise Exception("CTRL-EVENT-SCAN-STARTED not seen")
101
102 b = get_blacklist(wpas)
103 if '00:00:00:00:00:00' in b:
104 raise Exception("Unexpected blacklist contents: " + str(b))
105 if bssid1 not in b:
106 raise Exception("Unexpected blacklist contents: " + str(b))
107
108 def test_ap_roam_with_reassoc_auth_timeout(dev, apdev, params):
109 """Roam using reassoc between two APs and authentication times out"""
110 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
111 wpas.interface_add("wlan5",
112 drv_params="force_connect_cmd=1,force_bss_selection=1")
113
114 params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
115 hapd0 = hostapd.add_ap(apdev[0], params)
116 bssid0 = hapd0.own_addr()
117
118 id = wpas.connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
119 hwsim_utils.test_connectivity(wpas, hapd0)
120
121 hapd1 = hostapd.add_ap(apdev[1], params)
122 bssid1 = hapd1.own_addr()
123 wpas.scan_for_bss(bssid1, freq=2412)
124
125 if "OK" not in wpas.request("SET_NETWORK " + str(id) + " bssid " + bssid1):
126 raise Exception("SET_NETWORK failed")
127 if "OK" not in wpas.request("SET ignore_auth_resp 1"):
128 raise Exception("SET ignore_auth_resp failed")
129 if "OK" not in wpas.request("REASSOCIATE"):
130 raise Exception("REASSOCIATE failed")
131
132 logger.info("Wait ~10s for auth timeout...")
133 time.sleep(10)
134 ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], 12)
135 if not ev:
136 raise Exception("CTRL-EVENT-SCAN-STARTED not seen")
137
138 b = get_blacklist(wpas)
139 if bssid0 in b:
140 raise Exception("Unexpected blacklist contents: " + str(b))
141
142 def test_ap_roam_wpa2_psk_failed(dev, apdev, params):
143 """Roam failure with WPA2-PSK AP due to wrong passphrase"""
144 params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
145 hapd0 = hostapd.add_ap(apdev[0], params)
146 id = dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
147 hwsim_utils.test_connectivity(dev[0], hapd0)
148 params['wpa_passphrase'] = "22345678"
149 hapd1 = hostapd.add_ap(apdev[1], params)
150 bssid = hapd1.own_addr()
151 dev[0].scan_for_bss(bssid, freq=2412)
152
153 dev[0].dump_monitor()
154 if "OK" not in dev[0].request("ROAM " + bssid):
155 raise Exception("ROAM failed")
156
157 ev = dev[0].wait_event(["CTRL-EVENT-SSID-TEMP-DISABLED",
158 "CTRL-EVENT-CONNECTED"], 5)
159 if "CTRL-EVENT-CONNECTED" in ev:
160 raise Exception("Got unexpected CTRL-EVENT-CONNECTED")
161 if "CTRL-EVENT-SSID-TEMP-DISABLED" not in ev:
162 raise Exception("CTRL-EVENT-SSID-TEMP-DISABLED not seen")
163
164 if "OK" not in dev[0].request("SELECT_NETWORK id=" + str(id)):
165 raise Exception("SELECT_NETWORK failed")
166
167 ev = dev[0].wait_event(["CTRL-EVENT-SSID-REENABLED"], 3)
168 if not ev:
169 raise Exception("CTRL-EVENT-SSID-REENABLED not seen")
170
171 dev[0].wait_connected(timeout=5)
172 hwsim_utils.test_connectivity(dev[0], hapd0)
173
174 @remote_compatible
175 def test_ap_reassociation_to_same_bss(dev, apdev):
176 """Reassociate to the same BSS"""
177 hapd = hostapd.add_ap(apdev[0], {"ssid": "test-open"})
178 dev[0].connect("test-open", key_mgmt="NONE")
179
180 dev[0].request("REASSOCIATE")
181 dev[0].wait_connected(timeout=10, error="Reassociation timed out")
182 hwsim_utils.test_connectivity(dev[0], hapd)
183
184 dev[0].request("REATTACH")
185 dev[0].wait_connected(timeout=10, error="Reattach timed out")
186 hwsim_utils.test_connectivity(dev[0], hapd)
187
188 # Wait for previous scan results to expire to trigger new scan
189 time.sleep(5)
190 dev[0].request("REATTACH")
191 dev[0].wait_connected(timeout=10, error="Reattach timed out")
192 hwsim_utils.test_connectivity(dev[0], hapd)
193
194 @remote_compatible
195 def test_ap_roam_set_bssid(dev, apdev):
196 """Roam control"""
197 hostapd.add_ap(apdev[0], {"ssid": "test-open"})
198 hostapd.add_ap(apdev[1], {"ssid": "test-open"})
199 id = dev[0].connect("test-open", key_mgmt="NONE", bssid=apdev[1]['bssid'],
200 scan_freq="2412")
201 if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
202 raise Exception("Unexpected BSS")
203 # for now, these are just verifying that the code path to indicate
204 # within-ESS roaming changes can be executed; the actual results of those
205 # operations are not currently verified (that would require a test driver
206 # that does BSS selection)
207 dev[0].set_network(id, "bssid", "")
208 dev[0].set_network(id, "bssid", apdev[0]['bssid'])
209 dev[0].set_network(id, "bssid", apdev[1]['bssid'])
210
211 @remote_compatible
212 def test_ap_roam_wpa2_psk_race(dev, apdev):
213 """Roam between two WPA2-PSK APs and try to hit a disconnection race"""
214 params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
215 hapd0 = hostapd.add_ap(apdev[0], params)
216 dev[0].connect("test-wpa2-psk", psk="12345678", scan_freq="2412")
217 hwsim_utils.test_connectivity(dev[0], hapd0)
218
219 params['channel'] = '2'
220 hapd1 = hostapd.add_ap(apdev[1], params)
221 dev[0].scan_for_bss(apdev[1]['bssid'], freq=2417)
222 dev[0].roam(apdev[1]['bssid'])
223 hwsim_utils.test_connectivity(dev[0], hapd1)
224 dev[0].roam(apdev[0]['bssid'])
225 hwsim_utils.test_connectivity(dev[0], hapd0)
226 # Wait at least two seconds to trigger the previous issue with the
227 # disconnection callback.
228 for i in range(3):
229 time.sleep(0.8)
230 hwsim_utils.test_connectivity(dev[0], hapd0)