]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_wext.py
tests: Add wait_connected() and wait_disconnected() helpers
[thirdparty/hostap.git] / tests / hwsim / test_wext.py
1 # Deprecated WEXT driver interface in wpa_supplicant
2 # Copyright (c) 2013-2014, 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 import logging
8 logger = logging.getLogger()
9 import os
10 import subprocess
11
12 import hostapd
13 import hwsim_utils
14 from wpasupplicant import WpaSupplicant
15 from test_rfkill import get_rfkill_id
16
17 def get_wext_interface():
18 if not os.path.exists("/proc/net/wireless"):
19 logger.info("WEXT support not included in the kernel")
20 return
21
22 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
23 try:
24 wpas.interface_add("wlan5", driver="wext")
25 except Exception, e:
26 logger.info("WEXT driver support not included in wpa_supplicant")
27 return
28 return wpas
29
30 def test_wext_open(dev, apdev):
31 """WEXT driver interface with open network"""
32 wpas = get_wext_interface()
33 if not wpas:
34 return "skip"
35
36 params = { "ssid": "wext-open" }
37 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
38
39 wpas.connect("wext-open", key_mgmt="NONE")
40 hwsim_utils.test_connectivity(wpas, hapd)
41
42 def test_wext_wpa2_psk(dev, apdev):
43 """WEXT driver interface with WPA2-PSK"""
44 wpas = get_wext_interface()
45 if not wpas:
46 return "skip"
47
48 params = hostapd.wpa2_params(ssid="wext-wpa2-psk", passphrase="12345678")
49 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
50
51 wpas.connect("wext-wpa2-psk", psk="12345678")
52 hwsim_utils.test_connectivity(wpas, hapd)
53 if "RSSI=" not in wpas.request("SIGNAL_POLL"):
54 raise Exception("Missing RSSI from SIGNAL_POLL")
55
56 wpas.dump_monitor()
57 hapd.request("DEAUTHENTICATE " + wpas.p2p_interface_addr())
58 wpas.wait_disconnected(timeout=15)
59
60 def test_wext_wpa_psk(dev, apdev):
61 """WEXT driver interface with WPA-PSK"""
62 wpas = get_wext_interface()
63 if not wpas:
64 return "skip"
65
66 params = hostapd.wpa_params(ssid="wext-wpa-psk", passphrase="12345678")
67 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
68 testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
69 if not os.path.exists(testfile):
70 return "skip"
71
72 wpas.connect("wext-wpa-psk", psk="12345678")
73 hwsim_utils.test_connectivity(wpas, hapd)
74
75 with open(testfile, "w") as f:
76 f.write(wpas.p2p_interface_addr())
77 ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
78 if ev is not None:
79 raise Exception("Unexpected disconnection on first Michael MIC failure")
80
81 with open(testfile, "w") as f:
82 f.write("ff:ff:ff:ff:ff:ff")
83 ev = wpas.wait_disconnected(timeout=10,
84 error="No disconnection after two Michael MIC failures")
85 if "reason=14 locally_generated=1" not in ev:
86 raise Exception("Unexpected disconnection reason: " + ev)
87
88 def test_wext_pmksa_cache(dev, apdev):
89 """PMKSA caching with WEXT"""
90 wpas = get_wext_interface()
91 if not wpas:
92 return "skip"
93
94 params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
95 hostapd.add_ap(apdev[0]['ifname'], params)
96 bssid = apdev[0]['bssid']
97 wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
98 eap="GPSK", identity="gpsk user",
99 password="abcdefghijklmnop0123456789abcdef",
100 scan_freq="2412")
101 pmksa = wpas.get_pmksa(bssid)
102 if pmksa is None:
103 raise Exception("No PMKSA cache entry created")
104 if pmksa['opportunistic'] != '0':
105 raise Exception("Unexpected opportunistic PMKSA cache entry")
106
107 hostapd.add_ap(apdev[1]['ifname'], params)
108 bssid2 = apdev[1]['bssid']
109
110 wpas.dump_monitor()
111 logger.info("Roam to AP2")
112 # It can take some time for the second AP to become ready to reply to Probe
113 # Request frames especially under heavy CPU load, so allow couple of rounds
114 # of scanning to avoid reporting errors incorrectly just because of scans
115 # not having seen the target AP.
116 for i in range(3):
117 wpas.scan()
118 if wpas.get_bss(bssid2) is not None:
119 break
120 logger.info("Scan again to find target AP")
121 wpas.request("ROAM " + bssid2)
122 ev = wpas.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
123 if ev is None:
124 raise Exception("EAP success timed out")
125 wpas.wait_connected(timeout=10, error="Roaming timed out")
126 pmksa2 = wpas.get_pmksa(bssid2)
127 if pmksa2 is None:
128 raise Exception("No PMKSA cache entry found")
129 if pmksa2['opportunistic'] != '0':
130 raise Exception("Unexpected opportunistic PMKSA cache entry")
131
132 wpas.dump_monitor()
133 logger.info("Roam back to AP1")
134 wpas.scan()
135 wpas.request("ROAM " + bssid)
136 ev = wpas.wait_event(["CTRL-EVENT-EAP-STARTED",
137 "CTRL-EVENT-CONNECTED"], timeout=15)
138 if ev is None:
139 raise Exception("Roaming with the AP timed out")
140 if "CTRL-EVENT-EAP-STARTED" in ev:
141 raise Exception("Unexpected EAP exchange")
142 pmksa1b = wpas.get_pmksa(bssid)
143 if pmksa1b is None:
144 raise Exception("No PMKSA cache entry found")
145 if pmksa['pmkid'] != pmksa1b['pmkid']:
146 raise Exception("Unexpected PMKID change for AP1")
147
148 wpas.dump_monitor()
149 if "FAIL" in wpas.request("PMKSA_FLUSH"):
150 raise Exception("PMKSA_FLUSH failed")
151 if wpas.get_pmksa(bssid) is not None or wpas.get_pmksa(bssid2) is not None:
152 raise Exception("PMKSA_FLUSH did not remove PMKSA entries")
153 wpas.wait_disconnected(timeout=5)
154 wpas.wait_connected(timeout=15, error="Reconnection timed out")
155
156 def test_wext_wep_open_auth(dev, apdev):
157 """WEP Open System authentication"""
158 wpas = get_wext_interface()
159 if not wpas:
160 return "skip"
161
162 hapd = hostapd.add_ap(apdev[0]['ifname'],
163 { "ssid": "wep-open",
164 "wep_key0": '"hello"' })
165 wpas.connect("wep-open", key_mgmt="NONE", wep_key0='"hello"',
166 scan_freq="2412")
167 hwsim_utils.test_connectivity(wpas, hapd)
168 if "[WEP]" not in wpas.request("SCAN_RESULTS"):
169 raise Exception("WEP flag not indicated in scan results")
170
171 def test_wext_wep_shared_key_auth(dev, apdev):
172 """WEP Shared Key authentication"""
173 wpas = get_wext_interface()
174 if not wpas:
175 return "skip"
176
177 hapd = hostapd.add_ap(apdev[0]['ifname'],
178 { "ssid": "wep-shared-key",
179 "wep_key0": '"hello12345678"',
180 "auth_algs": "2" })
181 wpas.connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
182 wep_key0='"hello12345678"', scan_freq="2412")
183 hwsim_utils.test_connectivity(wpas, hapd)
184 wpas.request("REMOVE_NETWORK all")
185 wpas.wait_disconnected(timeout=5)
186 wpas.connect("wep-shared-key", key_mgmt="NONE", auth_alg="OPEN SHARED",
187 wep_key0='"hello12345678"', scan_freq="2412")
188
189 def test_wext_pmf(dev, apdev):
190 """WEXT driver interface with WPA2-PSK and PMF"""
191 wpas = get_wext_interface()
192 if not wpas:
193 return "skip"
194
195 params = hostapd.wpa2_params(ssid="wext-wpa2-psk", passphrase="12345678")
196 params["wpa_key_mgmt"] = "WPA-PSK-SHA256";
197 params["ieee80211w"] = "2";
198 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
199
200 wpas.connect("wext-wpa2-psk", psk="12345678", ieee80211w="1",
201 key_mgmt="WPA-PSK WPA-PSK-SHA256", proto="WPA2",
202 scan_freq="2412")
203 hwsim_utils.test_connectivity(wpas, hapd)
204
205 addr = wpas.p2p_interface_addr()
206 hapd.request("DEAUTHENTICATE " + addr)
207 wpas.wait_disconnected(timeout=5)
208
209 def test_wext_scan_hidden(dev, apdev):
210 """WEXT with hidden SSID"""
211 wpas = get_wext_interface()
212 if not wpas:
213 return "skip"
214
215 hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-scan",
216 "ignore_broadcast_ssid": "1" })
217 hostapd.add_ap(apdev[1]['ifname'], { "ssid": "test-scan2",
218 "ignore_broadcast_ssid": "1" })
219
220 id1 = wpas.connect("test-scan", key_mgmt="NONE", scan_ssid="1",
221 only_add_network=True)
222
223 wpas.request("SCAN scan_id=%d" % id1)
224
225 ev = wpas.wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
226 if ev is None:
227 raise Exception("Scan did not complete")
228
229 if "test-scan" not in wpas.request("SCAN_RESULTS"):
230 raise Exception("Did not find hidden SSID in scan")
231
232 id = wpas.connect("test-scan2", key_mgmt="NONE", scan_ssid="1",
233 only_add_network=True)
234 wpas.connect_network(id, timeout=30)
235
236 def test_wext_rfkill(dev, apdev):
237 """WEXT and rfkill block/unblock"""
238 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
239 wpas.interface_add("wlan5")
240 id = get_rfkill_id(wpas)
241 if id is None:
242 return "skip"
243 wpas.interface_remove("wlan5")
244
245 wpas = get_wext_interface()
246 if not wpas:
247 return "skip"
248
249 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
250 wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
251 try:
252 logger.info("rfkill block")
253 subprocess.call(['rfkill', 'block', id])
254 wpas.wait_disconnected(timeout=10,
255 error="Missing disconnection event on rfkill block")
256
257 logger.info("rfkill unblock")
258 subprocess.call(['rfkill', 'unblock', id])
259 wpas.wait_connected(timeout=20,
260 error="Missing connection event on rfkill unblock")
261 hwsim_utils.test_connectivity(wpas, hapd)
262 finally:
263 subprocess.call(['rfkill', 'unblock', id])