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