tests: Try to work around bogus errors in wpas_ctrl_country
[mech_eap.git] / tests / hwsim / test_wpas_ctrl.py
1 # wpa_supplicant control interface
2 # Copyright (c) 2014, Qualcomm Atheros, Inc.
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import subprocess
8 import time
9
10 import hostapd
11
12 def test_wpas_ctrl_network(dev):
13     """wpa_supplicant ctrl_iface network set/get"""
14     id = dev[0].add_network()
15
16     tests = (("key_mgmt", "WPA-PSK WPA-EAP IEEE8021X NONE WPA-NONE FT-PSK FT-EAP WPA-PSK-SHA256 WPA-EAP-SHA256"),
17              ("pairwise", "CCMP-256 GCMP-256 CCMP GCMP TKIP"),
18              ("group", "CCMP-256 GCMP-256 CCMP GCMP TKIP WEP104 WEP40"),
19              ("auth_alg", "OPEN SHARED LEAP"),
20              ("scan_freq", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"),
21              ("freq_list", "2412 2417"),
22              ("scan_ssid", "1"),
23              ("bssid", "00:11:22:33:44:55"),
24              ("proto", "WPA RSN OSEN"),
25              ("eap", "TLS"),
26              ("go_p2p_dev_addr", "22:33:44:55:66:aa"),
27              ("p2p_client_list", "22:33:44:55:66:bb 02:11:22:33:44:55"))
28
29     dev[0].set_network_quoted(id, "ssid", "test")
30     for field, value in tests:
31         dev[0].set_network(id, field, value)
32         res = dev[0].get_network(id, field)
33         if res != value:
34             raise Exception("Unexpected response for '" + field + "': '" + res + "'")
35
36     q_tests = (("identity", "hello"),
37                ("anonymous_identity", "foo@nowhere.com"))
38     for field, value in q_tests:
39         dev[0].set_network_quoted(id, field, value)
40         res = dev[0].get_network(id, field)
41         if res != '"' + value + '"':
42             raise Exception("Unexpected quoted response for '" + field + "': '" + res + "'")
43
44     get_tests = (("foo", None), ("ssid", '"test"'))
45     for field, value in get_tests:
46         res = dev[0].get_network(id, field)
47         if res != value:
48             raise Exception("Unexpected response for '" + field + "': '" + res + "'")
49
50     if dev[0].get_network(id, "password"):
51         raise Exception("Unexpected response for 'password'")
52     dev[0].set_network_quoted(id, "password", "foo")
53     if dev[0].get_network(id, "password") != '*':
54         raise Exception("Unexpected response for 'password' (expected *)")
55     dev[0].set_network(id, "password", "hash:12345678901234567890123456789012")
56     if dev[0].get_network(id, "password") != '*':
57         raise Exception("Unexpected response for 'password' (expected *)")
58     dev[0].set_network(id, "password", "NULL")
59     if dev[0].get_network(id, "password"):
60         raise Exception("Unexpected response for 'password'")
61     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + " password hash:12"):
62         raise Exception("Unexpected success for invalid password hash")
63     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + " password hash:123456789012345678x0123456789012"):
64         raise Exception("Unexpected success for invalid password hash")
65
66     dev[0].set_network(id, "identity", "414243")
67     if dev[0].get_network(id, "identity") != '"ABC"':
68         raise Exception("Unexpected identity hex->text response")
69
70     dev[0].set_network(id, "identity", 'P"abc\ndef"')
71     if dev[0].get_network(id, "identity") != "6162630a646566":
72         raise Exception("Unexpected identity printf->hex response")
73
74     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' identity P"foo'):
75         raise Exception("Unexpected success for invalid identity string")
76
77     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' identity 12x3'):
78         raise Exception("Unexpected success for invalid identity string")
79
80     for i in range(0, 4):
81         if "FAIL" in dev[0].request("SET_NETWORK " + str(id) + ' wep_key' + str(i) + ' aabbccddee'):
82             raise Exception("Unexpected wep_key set failure")
83         if dev[0].get_network(id, "wep_key" + str(i)) != '*':
84             raise Exception("Unexpected wep_key get failure")
85
86     if "FAIL" in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:22:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'):
87         raise Exception("Unexpected failure for psk_list string")
88
89     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list 00:11:x2:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'):
90         raise Exception("Unexpected success for invalid psk_list string")
91
92     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:x2:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'):
93         raise Exception("Unexpected success for invalid psk_list string")
94
95     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:22:33:44:55+0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'):
96         raise Exception("Unexpected success for invalid psk_list string")
97
98     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:22:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde'):
99         raise Exception("Unexpected success for invalid psk_list string")
100
101     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:22:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdex'):
102         raise Exception("Unexpected success for invalid psk_list string")
103
104     if dev[0].get_network(id, "psk_list"):
105         raise Exception("Unexpected psk_list get response")
106
107     if dev[0].list_networks()[0]['ssid'] != "test":
108         raise Exception("Unexpected ssid in LIST_NETWORKS")
109     dev[0].set_network(id, "ssid", "NULL")
110     if dev[0].list_networks()[0]['ssid'] != "":
111         raise Exception("Unexpected ssid in LIST_NETWORKS after clearing it")
112
113     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' ssid "0123456789abcdef0123456789abcdef0"'):
114         raise Exception("Too long SSID accepted")
115     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' scan_ssid qwerty'):
116         raise Exception("Invalid integer accepted")
117     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' scan_ssid 2'):
118         raise Exception("Too large integer accepted")
119     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' psk 12345678'):
120         raise Exception("Invalid PSK accepted")
121     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' psk "1234567"'):
122         raise Exception("Too short PSK accepted")
123     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' psk "1234567890123456789012345678901234567890123456789012345678901234"'):
124         raise Exception("Too long PSK accepted")
125     dev[0].set_network_quoted(id, "psk", "123456768");
126     dev[0].set_network_quoted(id, "psk", "123456789012345678901234567890123456789012345678901234567890123");
127     if dev[0].get_network(id, "psk") != '*':
128         raise Exception("Unexpected psk read result");
129
130     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' eap UNKNOWN'):
131         raise Exception("Unknown EAP method accepted")
132
133     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' password "foo'):
134         raise Exception("Invalid password accepted")
135
136     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' wep_key0 "foo'):
137         raise Exception("Invalid WEP key accepted")
138     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' wep_key0 "12345678901234567"'):
139         raise Exception("Too long WEP key accepted")
140     # too short WEP key is ignored
141     dev[0].set_network_quoted(id, "wep_key0", "1234")
142     dev[0].set_network_quoted(id, "wep_key1", "12345")
143     dev[0].set_network_quoted(id, "wep_key2", "1234567890123")
144     dev[0].set_network_quoted(id, "wep_key3", "1234567890123456")
145
146     dev[0].set_network(id, "go_p2p_dev_addr", "any")
147     if dev[0].get_network(id, "go_p2p_dev_addr") is not None:
148         raise Exception("Unexpected go_p2p_dev_addr value")
149     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' go_p2p_dev_addr 00:11:22:33:44'):
150         raise Exception("Invalid go_p2p_dev_addr accepted")
151     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' p2p_client_list 00:11:22:33:44'):
152         raise Exception("Invalid p2p_client_list accepted")
153     if "FAIL" in dev[0].request('SET_NETWORK ' + str(id) + ' p2p_client_list 00:11:22:33:44:55 00:1'):
154         raise Exception("p2p_client_list truncation workaround failed")
155     if dev[0].get_network(id, "p2p_client_list") != "00:11:22:33:44:55":
156         raise Exception("p2p_client_list truncation workaround did not work")
157
158     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' auth_alg '):
159         raise Exception("Empty auth_alg accepted")
160     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' auth_alg FOO'):
161         raise Exception("Invalid auth_alg accepted")
162
163     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' proto '):
164         raise Exception("Empty proto accepted")
165     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' proto FOO'):
166         raise Exception("Invalid proto accepted")
167
168     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' pairwise '):
169         raise Exception("Empty pairwise accepted")
170     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' pairwise FOO'):
171         raise Exception("Invalid pairwise accepted")
172     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' pairwise WEP40'):
173         raise Exception("Invalid pairwise accepted")
174
175     if "OK" not in dev[0].request('BSSID ' + str(id) + ' 00:11:22:33:44:55'):
176         raise Exception("Unexpected BSSID failure")
177     if dev[0].request("GET_NETWORK 0 bssid") != '00:11:22:33:44:55':
178         raise Exception("BSSID command did not set network bssid")
179     if "OK" not in dev[0].request('BSSID ' + str(id) + ' 00:00:00:00:00:00'):
180         raise Exception("Unexpected BSSID failure")
181     if "FAIL" not in dev[0].request("GET_NETWORK 0 bssid"):
182         raise Exception("bssid claimed configured after clearing")
183     if "FAIL" not in dev[0].request('BSSID 123 00:11:22:33:44:55'):
184         raise Exception("Unexpected BSSID success")
185     if "FAIL" not in dev[0].request('BSSID ' + str(id) + ' 00:11:22:33:44'):
186         raise Exception("Unexpected BSSID success")
187
188 def add_cred(dev):
189     id = dev.add_cred()
190     ev = dev.wait_event(["CRED-ADDED"])
191     if ev is None:
192         raise Exception("Missing CRED-ADDED event")
193     if " " + str(id) not in ev:
194         raise Exception("CRED-ADDED event without matching id")
195     return id
196
197 def set_cred(dev, id, field, value):
198     dev.set_cred(id, field, value)
199     ev = dev.wait_event(["CRED-MODIFIED"])
200     if ev is None:
201         raise Exception("Missing CRED-MODIFIED event")
202     if " " + str(id) + " " not in ev:
203         raise Exception("CRED-MODIFIED event without matching id")
204     if field not in ev:
205         raise Exception("CRED-MODIFIED event without matching field")
206
207 def set_cred_quoted(dev, id, field, value):
208     dev.set_cred_quoted(id, field, value)
209     ev = dev.wait_event(["CRED-MODIFIED"])
210     if ev is None:
211         raise Exception("Missing CRED-MODIFIED event")
212     if " " + str(id) + " " not in ev:
213         raise Exception("CRED-MODIFIED event without matching id")
214     if field not in ev:
215         raise Exception("CRED-MODIFIED event without matching field")
216
217 def remove_cred(dev, id):
218     dev.remove_cred(id)
219     ev = dev.wait_event(["CRED-REMOVED"])
220     if ev is None:
221         raise Exception("Missing CRED-REMOVED event")
222     if " " + str(id) not in ev:
223         raise Exception("CRED-REMOVED event without matching id")
224
225 def test_wpas_ctrl_cred(dev):
226     """wpa_supplicant ctrl_iface cred set"""
227     id1 = add_cred(dev[0])
228     id = add_cred(dev[0])
229     id2 = add_cred(dev[0])
230     set_cred(dev[0], id, "temporary", "1")
231     set_cred(dev[0], id, "priority", "1")
232     set_cred(dev[0], id, "pcsc", "1")
233     set_cred_quoted(dev[0], id, "private_key_passwd", "test")
234     set_cred_quoted(dev[0], id, "domain_suffix_match", "test")
235     set_cred_quoted(dev[0], id, "phase1", "test")
236     set_cred_quoted(dev[0], id, "phase2", "test")
237
238     if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " eap FOO"):
239         raise Exception("Unexpected success on unknown EAP method")
240
241     if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " username 12xa"):
242         raise Exception("Unexpected success on invalid string")
243
244     for i in ("11", "1122", "112233445566778899aabbccddeeff00"):
245         if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " roaming_consortium " + i):
246             raise Exception("Unexpected success on invalid roaming_consortium")
247
248     dev[0].set_cred(id, "excluded_ssid", "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff")
249     if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " excluded_ssid 00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00"):
250         raise Exception("Unexpected success on invalid excluded_ssid")
251
252     if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " foo 4142"):
253         raise Exception("Unexpected success on unknown field")
254
255     id3 = add_cred(dev[0])
256     id4 = add_cred(dev[0])
257     if len(dev[0].request("LIST_CREDS").splitlines()) != 6:
258         raise Exception("Unexpected LIST_CREDS result(1)")
259
260     remove_cred(dev[0], id1)
261     remove_cred(dev[0], id3)
262     remove_cred(dev[0], id4)
263     remove_cred(dev[0], id2)
264     remove_cred(dev[0], id)
265     if "FAIL" not in dev[0].request("REMOVE_CRED 1"):
266         raise Exception("Unexpected success on invalid remove cred")
267     if len(dev[0].request("LIST_CREDS").splitlines()) != 1:
268         raise Exception("Unexpected LIST_CREDS result(2)")
269
270     id = add_cred(dev[0])
271     values = [ ("temporary", "1", False),
272                ("temporary", "0", False),
273                ("pcsc", "1", False),
274                ("realm", "example.com", True),
275                ("username", "user@example.com", True),
276                ("password", "foo", True, "*"),
277                ("ca_cert", "ca.pem", True),
278                ("client_cert", "user.pem", True),
279                ("private_key", "key.pem", True),
280                ("private_key_passwd", "foo", True, "*"),
281                ("imsi", "310026-000000000", True),
282                ("milenage", "foo", True, "*"),
283                ("domain_suffix_match", "example.com", True),
284                ("domain", "example.com", True),
285                ("domain", "example.org", True, "example.com\nexample.org"),
286                ("roaming_consortium", "0123456789", False),
287                ("required_roaming_consortium", "456789", False),
288                ("eap", "TTLS", False),
289                ("phase1", "foo=bar1", True),
290                ("phase2", "foo=bar2", True),
291                ("excluded_ssid", "test", True),
292                ("excluded_ssid", "foo", True, "test\nfoo"),
293                ("roaming_partner", "example.com,0,4,*", True),
294                ("roaming_partner", "example.org,1,2,US", True,
295                 "example.com,0,4,*\nexample.org,1,2,US"),
296                ("update_identifier", "4", False),
297                ("provisioning_sp", "sp.example.com", True),
298                ("sp_priority", "7", False),
299                ("min_dl_bandwidth_home", "100", False),
300                ("min_ul_bandwidth_home", "101", False),
301                ("min_dl_bandwidth_roaming", "102", False),
302                ("min_ul_bandwidth_roaming", "103", False),
303                ("max_bss_load", "57", False),
304                ("req_conn_capab", "6:22,80,443", False),
305                ("req_conn_capab", "17:500", False, "6:22,80,443\n17:500"),
306                ("req_conn_capab", "50", False, "6:22,80,443\n17:500\n50"),
307                ("ocsp", "1", False) ]
308     for v in values:
309         if v[2]:
310             set_cred_quoted(dev[0], id, v[0], v[1])
311         else:
312             set_cred(dev[0], id, v[0], v[1])
313         val = dev[0].get_cred(id, v[0])
314         if len(v) == 4:
315             expect = v[3]
316         else:
317             expect = v[1]
318         if val != expect:
319             raise Exception("Unexpected GET_CRED value for {}: {} != {}".format(v[0], val, expect))
320     creds = dev[0].request("LIST_CREDS").splitlines()
321     if len(creds) != 2:
322         raise Exception("Unexpected LIST_CREDS result(3)")
323     if creds[1] != "0\texample.com\tuser@example.com\texample.com\t310026-000000000":
324         raise Exception("Unexpected LIST_CREDS value")
325     remove_cred(dev[0], id)
326     if len(dev[0].request("LIST_CREDS").splitlines()) != 1:
327         raise Exception("Unexpected LIST_CREDS result(4)")
328
329 def test_wpas_ctrl_pno(dev):
330     """wpa_supplicant ctrl_iface pno"""
331     if "FAIL" not in dev[0].request("SET pno 1"):
332         raise Exception("Unexpected success in enabling PNO without enabled network blocks")
333     id = dev[0].add_network()
334     dev[0].set_network_quoted(id, "ssid", "test")
335     dev[0].set_network(id, "key_mgmt", "NONE")
336     dev[0].request("ENABLE_NETWORK " + str(id) + " no-connect")
337     #mac80211_hwsim does not yet support PNO, so this fails
338     if "FAIL" not in dev[0].request("SET pno 1"):
339         raise Exception("Unexpected success in enabling PNO")
340     if "FAIL" not in dev[0].request("SET pno 1 freq=2000-3000,5180"):
341         raise Exception("Unexpected success in enabling PNO")
342     if "FAIL" not in dev[0].request("SET pno 1 freq=0-6000"):
343         raise Exception("Unexpected success in enabling PNO")
344     if "FAIL" in dev[0].request("SET pno 0"):
345         raise Exception("Unexpected failure in disabling PNO")
346
347 def test_wpas_ctrl_get(dev):
348     """wpa_supplicant ctrl_iface get"""
349     if "FAIL" in dev[0].request("GET version"):
350         raise Exception("Unexpected get failure for version")
351     if "FAIL" in dev[0].request("GET wifi_display"):
352         raise Exception("Unexpected get failure for wifi_display")
353     if "FAIL" not in dev[0].request("GET foo"):
354         raise Exception("Unexpected success on get command")
355
356 def test_wpas_ctrl_preauth(dev):
357     """wpa_supplicant ctrl_iface preauth"""
358     if "FAIL" not in dev[0].request("PREAUTH "):
359         raise Exception("Unexpected success on invalid PREAUTH")
360     if "FAIL" in dev[0].request("PREAUTH 00:11:22:33:44:55"):
361         raise Exception("Unexpected failure on PREAUTH")
362
363 def test_wpas_ctrl_stkstart(dev):
364     """wpa_supplicant ctrl_iface strkstart"""
365     if "FAIL" not in dev[0].request("STKSTART "):
366         raise Exception("Unexpected success on invalid STKSTART")
367     if "FAIL" not in dev[0].request("STKSTART 00:11:22:33:44:55"):
368         raise Exception("Unexpected success on STKSTART")
369
370 def test_wpas_ctrl_tdls_discover(dev):
371     """wpa_supplicant ctrl_iface tdls_discover"""
372     if "FAIL" not in dev[0].request("TDLS_DISCOVER "):
373         raise Exception("Unexpected success on invalid TDLS_DISCOVER")
374     if "FAIL" not in dev[0].request("TDLS_DISCOVER 00:11:22:33:44:55"):
375         raise Exception("Unexpected success on TDLS_DISCOVER")
376
377 def test_wpas_ctrl_addr(dev):
378     """wpa_supplicant ctrl_iface invalid address"""
379     if "FAIL" not in dev[0].request("TDLS_SETUP "):
380         raise Exception("Unexpected success on invalid TDLS_SETUP")
381     if "FAIL" not in dev[0].request("TDLS_TEARDOWN "):
382         raise Exception("Unexpected success on invalid TDLS_TEARDOWN")
383     if "FAIL" not in dev[0].request("FT_DS "):
384         raise Exception("Unexpected success on invalid FT_DS")
385     if "FAIL" not in dev[0].request("WPS_PBC 00:11:22:33:44"):
386         raise Exception("Unexpected success on invalid WPS_PBC")
387     if "FAIL" not in dev[0].request("WPS_PIN 00:11:22:33:44"):
388         raise Exception("Unexpected success on invalid WPS_PIN")
389     if "FAIL" not in dev[0].request("WPS_NFC 00:11:22:33:44"):
390         raise Exception("Unexpected success on invalid WPS_NFC")
391     if "FAIL" not in dev[0].request("WPS_REG 12345670 00:11:22:33:44"):
392         raise Exception("Unexpected success on invalid WPS_REG")
393     if "FAIL" not in dev[0].request("IBSS_RSN 00:11:22:33:44"):
394         raise Exception("Unexpected success on invalid IBSS_RSN")
395
396 def test_wpas_ctrl_config_parser(dev):
397     """wpa_supplicant ctrl_iface SET config parser"""
398     if "FAIL" not in dev[0].request("SET pbc_in_m1 qwerty"):
399         raise Exception("Non-number accepted as integer")
400     if "FAIL" not in dev[0].request("SET eapol_version 0"):
401         raise Exception("Out-of-range value accepted")
402     if "FAIL" not in dev[0].request("SET eapol_version 10"):
403         raise Exception("Out-of-range value accepted")
404
405     if "FAIL" not in dev[0].request("SET serial_number 0123456789abcdef0123456789abcdef0"):
406         raise Exception("Too long string accepted")
407
408 def test_wpas_ctrl_mib(dev):
409     """wpa_supplicant ctrl_iface MIB"""
410     mib = dev[0].get_mib()
411     if "dot11RSNAOptionImplemented" not in mib:
412         raise Exception("Missing MIB entry")
413     if mib["dot11RSNAOptionImplemented"] != "TRUE":
414         raise Exception("Unexpected dot11RSNAOptionImplemented value")
415
416 def test_wpas_ctrl_set_wps_params(dev):
417     """wpa_supplicant ctrl_iface SET config_methods"""
418     ts = [ "config_methods label virtual_display virtual_push_button keypad",
419            "device_type 1-0050F204-1",
420            "os_version 01020300",
421            "uuid 12345678-9abc-def0-1234-56789abcdef0" ]
422     for t in ts:
423         if "OK" not in dev[2].request("SET " + t):
424             raise Exception("SET failed for: " + t)
425
426 def test_wpas_ctrl_level(dev):
427     """wpa_supplicant ctrl_iface LEVEL"""
428     try:
429         if "FAIL" not in dev[2].request("LEVEL 3"):
430             raise Exception("Unexpected LEVEL success")
431         if "OK" not in dev[2].mon.request("LEVEL 2"):
432             raise Exception("Unexpected LEVEL failure")
433         dev[2].request("SCAN freq=2412")
434         ev = dev[2].wait_event(["State:"], timeout=5)
435         if ev is None:
436             raise Exception("No debug message received")
437         dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=5)
438     finally:
439         dev[2].mon.request("LEVEL 3")
440
441 def test_wpas_ctrl_bssid_filter(dev, apdev):
442     """wpa_supplicant bssid_filter"""
443     try:
444         if "OK" not in dev[2].request("SET bssid_filter " + apdev[0]['bssid']):
445             raise Exception("Failed to set bssid_filter")
446         params = { "ssid": "test" }
447         hostapd.add_ap(apdev[0]['ifname'], params)
448         hostapd.add_ap(apdev[1]['ifname'], params)
449         dev[2].scan(freq="2412")
450         bss = dev[2].get_bss(apdev[0]['bssid'])
451         if len(bss) == 0:
452             raise Exception("Missing BSS data")
453         bss = dev[2].get_bss(apdev[1]['bssid'])
454         if len(bss) != 0:
455             raise Exception("Unexpected BSS data")
456         dev[2].request("SET bssid_filter ")
457         dev[2].scan(freq="2412")
458         bss = dev[2].get_bss(apdev[0]['bssid'])
459         if len(bss) == 0:
460             raise Exception("Missing BSS data")
461         bss = dev[2].get_bss(apdev[1]['bssid'])
462         if len(bss) == 0:
463             raise Exception("Missing BSS data(2)")
464         res = dev[2].request("SCAN_RESULTS").splitlines()
465         if "test" not in res[1] or "test" not in res[2]:
466             raise Exception("SSID missing from SCAN_RESULTS")
467         if apdev[0]['bssid'] not in res[1] and apdev[1]['bssid'] not in res[1]:
468             raise Exception("BSS1 missing from SCAN_RESULTS")
469         if apdev[0]['bssid'] not in res[2] and apdev[1]['bssid'] not in res[2]:
470             raise Exception("BSS1 missing from SCAN_RESULTS")
471
472         if "FAIL" not in dev[2].request("SET bssid_filter 00:11:22:33:44:55 00:11:22:33:44"):
473             raise Exception("Unexpected success for invalid SET bssid_filter")
474     finally:
475         dev[2].request("SET bssid_filter ")
476
477 def test_wpas_ctrl_disallow_aps(dev, apdev):
478     """wpa_supplicant ctrl_iface disallow_aps"""
479     params = { "ssid": "test" }
480     hostapd.add_ap(apdev[0]['ifname'], params)
481
482     if "FAIL" not in dev[0].request("SET disallow_aps bssid "):
483         raise Exception("Unexpected success on invalid disallow_aps")
484     if "FAIL" not in dev[0].request("SET disallow_aps bssid 00:11:22:33:44"):
485         raise Exception("Unexpected success on invalid disallow_aps")
486     if "FAIL" not in dev[0].request("SET disallow_aps ssid 0"):
487         raise Exception("Unexpected success on invalid disallow_aps")
488     if "FAIL" not in dev[0].request("SET disallow_aps ssid 4q"):
489         raise Exception("Unexpected success on invalid disallow_aps")
490     if "FAIL" not in dev[0].request("SET disallow_aps bssid 00:11:22:33:44:55 ssid 112233 ssid 123"):
491         raise Exception("Unexpected success on invalid disallow_aps")
492     if "FAIL" not in dev[0].request("SET disallow_aps ssid 000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f00"):
493         raise Exception("Unexpected success on invalid disallow_aps")
494     if "FAIL" not in dev[0].request("SET disallow_aps foo 112233445566"):
495         raise Exception("Unexpected success on invalid disallow_aps")
496
497     dev[0].connect("test", key_mgmt="NONE", scan_freq="2412")
498     hostapd.add_ap(apdev[1]['ifname'], params)
499     dev[0].dump_monitor()
500     if "OK" not in dev[0].request("SET disallow_aps bssid 00:11:22:33:44:55 bssid 00:22:33:44:55:66"):
501         raise Exception("Failed to set disallow_aps")
502     if "OK" not in dev[0].request("SET disallow_aps bssid " + apdev[0]['bssid']):
503         raise Exception("Failed to set disallow_aps")
504     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
505     if ev is None:
506         raise Exception("Reassociation timed out")
507     if apdev[1]['bssid'] not in ev:
508         raise Exception("Unexpected BSSID")
509
510     dev[0].dump_monitor()
511     if "OK" not in dev[0].request("SET disallow_aps ssid " + "test".encode("hex")):
512         raise Exception("Failed to set disallow_aps")
513     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
514     if ev is None:
515         raise Exception("Disconnection not seen")
516     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
517     if ev is not None:
518         raise Exception("Unexpected reassociation")
519
520 def test_wpas_ctrl_blob(dev):
521     """wpa_supplicant ctrl_iface SET blob"""
522     if "FAIL" not in dev[0].request("SET blob foo"):
523         raise Exception("Unexpected SET success")
524     if "FAIL" not in dev[0].request("SET blob foo 0"):
525         raise Exception("Unexpected SET success")
526     if "FAIL" not in dev[0].request("SET blob foo 0q"):
527         raise Exception("Unexpected SET success")
528     if "OK" not in dev[0].request("SET blob foo 00"):
529         raise Exception("Unexpected SET failure")
530     if "OK" not in dev[0].request("SET blob foo 0011"):
531         raise Exception("Unexpected SET failure")
532
533 def test_wpas_ctrl_set_uapsd(dev):
534     """wpa_supplicant ctrl_iface SET uapsd"""
535     if "FAIL" not in dev[0].request("SET uapsd foo"):
536         raise Exception("Unexpected SET success")
537     if "FAIL" not in dev[0].request("SET uapsd 0,0,0"):
538         raise Exception("Unexpected SET success")
539     if "FAIL" not in dev[0].request("SET uapsd 0,0"):
540         raise Exception("Unexpected SET success")
541     if "FAIL" not in dev[0].request("SET uapsd 0"):
542         raise Exception("Unexpected SET success")
543     if "OK" not in dev[0].request("SET uapsd 1,1,1,1;1"):
544         raise Exception("Unexpected SET failure")
545     if "OK" not in dev[0].request("SET uapsd 0,0,0,0;0"):
546         raise Exception("Unexpected SET failure")
547     if "OK" not in dev[0].request("SET uapsd disable"):
548         raise Exception("Unexpected SET failure")
549
550 def test_wpas_ctrl_set(dev):
551     """wpa_supplicant ctrl_iface SET"""
552     vals = [ "foo",
553              "dot11RSNAConfigPMKLifetime 0",
554              "dot11RSNAConfigPMKReauthThreshold 101",
555              "dot11RSNAConfigSATimeout 0",
556              "wps_version_number -1",
557              "wps_version_number 256" ]
558     for val in vals:
559         if "FAIL" not in dev[0].request("SET " + val):
560             raise Exception("Unexpected SET success for " + val)
561
562     vals = [ "EAPOL::heldPeriod 60",
563              "EAPOL::authPeriod 30",
564              "EAPOL::startPeriod 30",
565              "EAPOL::maxStart 3",
566              "dot11RSNAConfigSATimeout 60",
567              "tdls_disabled 1",
568              "tdls_disabled 0" ]
569     for val in vals:
570         if "OK" not in dev[0].request("SET " + val):
571             raise Exception("Unexpected SET failure for " + val)
572
573 def test_wpas_ctrl_get_capability(dev):
574     """wpa_supplicant ctrl_iface GET_CAPABILITY"""
575     res = dev[0].get_capability("eap")
576     if "TTLS" not in res:
577         raise Exception("Unexpected GET_CAPABILITY eap response: " + str(res))
578
579     res = dev[0].get_capability("pairwise")
580     if "CCMP" not in res:
581         raise Exception("Unexpected GET_CAPABILITY pairwise response: " + str(res))
582
583     res = dev[0].get_capability("group")
584     if "CCMP" not in res:
585         raise Exception("Unexpected GET_CAPABILITY group response: " + str(res))
586
587     res = dev[0].get_capability("key_mgmt")
588     if "WPA-PSK" not in res or "WPA-EAP" not in res:
589         raise Exception("Unexpected GET_CAPABILITY key_mgmt response: " + str(res))
590
591     res = dev[0].get_capability("proto")
592     if "WPA" not in res or "RSN" not in res:
593         raise Exception("Unexpected GET_CAPABILITY proto response: " + str(res))
594
595     res = dev[0].get_capability("auth_alg")
596     if "OPEN" not in res or "SHARED" not in res:
597         raise Exception("Unexpected GET_CAPABILITY auth_alg response: " + str(res))
598
599     res = dev[0].get_capability("modes")
600     if "IBSS" not in res or "AP" not in res:
601         raise Exception("Unexpected GET_CAPABILITY modes response: " + str(res))
602
603     res = dev[0].get_capability("channels")
604     if "8" not in res or "36" not in res:
605         raise Exception("Unexpected GET_CAPABILITY channels response: " + str(res))
606
607     res = dev[0].get_capability("freq")
608     if "2457" not in res or "5180" not in res:
609         raise Exception("Unexpected GET_CAPABILITY freq response: " + str(res))
610
611     res = dev[0].get_capability("tdls")
612     if "EXTERNAL" not in res[0]:
613         raise Exception("Unexpected GET_CAPABILITY tdls response: " + str(res))
614
615     if dev[0].get_capability("foo") is not None:
616         raise Exception("Unexpected GET_CAPABILITY foo response: " + str(res))
617
618 def test_wpas_ctrl_nfc_report_handover(dev):
619     """wpa_supplicant ctrl_iface NFC_REPORT_HANDOVER"""
620     vals = [ "FOO",
621              "ROLE freq=12345",
622              "ROLE TYPE",
623              "ROLE TYPE REQ",
624              "ROLE TYPE REQ SEL",
625              "ROLE TYPE 0Q SEL",
626              "ROLE TYPE 00 SEL",
627              "ROLE TYPE 00 0Q",
628              "ROLE TYPE 00 00" ]
629     for v in vals:
630         if "FAIL" not in dev[0].request("NFC_REPORT_HANDOVER " + v):
631             raise Exception("Unexpected NFC_REPORT_HANDOVER success for " + v)
632
633 def test_wpas_ctrl_nfc_tag_read(dev):
634     """wpa_supplicant ctrl_iface WPS_NFC_TAG_READ"""
635     vals = [ "FOO", "0Q", "00", "000000", "10000001", "10000000", "00000000",
636              "100e0000", "100e0001ff", "100e000411110000", "100e0004100e0001" ]
637     for v in vals:
638         if "FAIL" not in dev[0].request("WPS_NFC_TAG_READ " + v):
639             raise Exception("Unexpected WPS_NFC_TAG_READ success for " + v)
640
641 def test_wpas_ctrl_nfc_get_handover(dev):
642     """wpa_supplicant ctrl_iface NFC_GET_HANDOVER"""
643     vals = [ "FOO", "FOO BAR", "WPS WPS", "WPS WPS-CR", "WPS FOO", "NDEF P2P" ]
644     for v in vals:
645         if "FAIL" not in dev[0].request("NFC_GET_HANDOVER_REQ " + v):
646             raise Exception("Unexpected NFC_GET_HANDOVER_REQ success for " + v)
647
648     vals = [ "NDEF WPS", "NDEF P2P-CR", "WPS P2P-CR" ]
649     for v in vals:
650         if "FAIL" in dev[0].request("NFC_GET_HANDOVER_REQ " + v):
651             raise Exception("Unexpected NFC_GET_HANDOVER_REQ failure for " + v)
652
653     vals = [ "FOO", "FOO BAR", "WPS WPS", "WPS WPS-CR", "WPS FOO", "NDEF P2P",
654              "NDEF WPS", "NDEF WPS uuid" ]
655     for v in vals:
656         if "FAIL" not in dev[0].request("NFC_GET_HANDOVER_SEL " + v):
657             raise Exception("Unexpected NFC_GET_HANDOVER_SEL success for " + v)
658
659     vals = [ "NDEF P2P-CR", "WPS P2P-CR", "NDEF P2P-CR-TAG",
660              "WPS P2P-CR-TAG" ]
661     for v in vals:
662         if "FAIL" in dev[0].request("NFC_GET_HANDOVER_SEL " + v):
663             raise Exception("Unexpected NFC_GET_HANDOVER_SEL failure for " + v)
664
665 def get_blacklist(dev):
666     return dev.request("BLACKLIST").splitlines()
667
668 def test_wpas_ctrl_blacklist(dev):
669     """wpa_supplicant ctrl_iface BLACKLIST"""
670     if "OK" not in dev[0].request("BLACKLIST clear"):
671         raise Exception("BLACKLIST clear failed")
672     b = get_blacklist(dev[0])
673     if len(b) != 0:
674         raise Exception("Unexpected blacklist contents: " + str(b))
675     if "OK" not in dev[0].request("BLACKLIST 00:11:22:33:44:55"):
676         raise Exception("BLACKLIST add failed")
677     b = get_blacklist(dev[0])
678     if "00:11:22:33:44:55" not in b:
679         raise Exception("Unexpected blacklist contents: " + str(b))
680     if "OK" not in dev[0].request("BLACKLIST 00:11:22:33:44:56"):
681         raise Exception("BLACKLIST add failed")
682     b = get_blacklist(dev[0])
683     if "00:11:22:33:44:55" not in b or "00:11:22:33:44:56" not in b:
684         raise Exception("Unexpected blacklist contents: " + str(b))
685     if "OK" not in dev[0].request("BLACKLIST 00:11:22:33:44:56"):
686         raise Exception("BLACKLIST add failed")
687     b = get_blacklist(dev[0])
688     if "00:11:22:33:44:55" not in b or "00:11:22:33:44:56" not in b or len(b) != 2:
689         raise Exception("Unexpected blacklist contents: " + str(b))
690
691     if "OK" not in dev[0].request("BLACKLIST clear"):
692         raise Exception("BLACKLIST clear failed")
693     if dev[0].request("BLACKLIST") != "":
694         raise Exception("Unexpected blacklist contents")
695
696 def test_wpas_ctrl_log_level(dev):
697     """wpa_supplicant ctrl_iface LOG_LEVEL"""
698     level = dev[2].request("LOG_LEVEL")
699     if "Current level: MSGDUMP" not in level:
700         raise Exception("Unexpected debug level(1): " + level)
701     if "Timestamp: 1" not in level:
702         raise Exception("Unexpected timestamp(1): " + level)
703
704     if "OK" not in dev[2].request("LOG_LEVEL  MSGDUMP  0"):
705         raise Exception("LOG_LEVEL failed")
706     level = dev[2].request("LOG_LEVEL")
707     if "Current level: MSGDUMP" not in level:
708         raise Exception("Unexpected debug level(2): " + level)
709     if "Timestamp: 0" not in level:
710         raise Exception("Unexpected timestamp(2): " + level)
711
712     if "OK" not in dev[2].request("LOG_LEVEL  MSGDUMP  1"):
713         raise Exception("LOG_LEVEL failed")
714     level = dev[2].request("LOG_LEVEL")
715     if "Current level: MSGDUMP" not in level:
716         raise Exception("Unexpected debug level(3): " + level)
717     if "Timestamp: 1" not in level:
718         raise Exception("Unexpected timestamp(3): " + level)
719
720     if "FAIL" not in dev[2].request("LOG_LEVEL FOO"):
721         raise Exception("Invalid LOG_LEVEL accepted")
722
723     for lev in [ "EXCESSIVE", "MSGDUMP", "DEBUG", "INFO", "WARNING", "ERROR" ]:
724         if "OK" not in dev[2].request("LOG_LEVEL " + lev):
725             raise Exception("LOG_LEVEL failed for " + lev)
726         level = dev[2].request("LOG_LEVEL")
727         if "Current level: " + lev not in level:
728             raise Exception("Unexpected debug level: " + level)
729
730     if "OK" not in dev[2].request("LOG_LEVEL  MSGDUMP  1"):
731         raise Exception("LOG_LEVEL failed")
732     level = dev[2].request("LOG_LEVEL")
733     if "Current level: MSGDUMP" not in level:
734         raise Exception("Unexpected debug level(3): " + level)
735     if "Timestamp: 1" not in level:
736         raise Exception("Unexpected timestamp(3): " + level)
737
738 def test_wpas_ctrl_enable_disable_network(dev, apdev):
739     """wpa_supplicant ctrl_iface ENABLE/DISABLE_NETWORK"""
740     params = { "ssid": "test" }
741     hostapd.add_ap(apdev[0]['ifname'], params)
742
743     id = dev[0].connect("test", key_mgmt="NONE", scan_freq="2412",
744                         only_add_network=True)
745     if "OK" not in dev[0].request("DISABLE_NETWORK " + str(id)):
746         raise Exception("Failed to disable network")
747     if "OK" not in dev[0].request("ENABLE_NETWORK " + str(id) + " no-connect"):
748         raise Exception("Failed to enable network")
749     if "OK" not in dev[0].request("DISABLE_NETWORK all"):
750         raise Exception("Failed to disable networks")
751     if "OK" not in dev[0].request("ENABLE_NETWORK " + str(id)):
752         raise Exception("Failed to enable network")
753     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
754     if ev is None:
755         raise Exception("Association with the AP timed out")
756     if "OK" not in dev[0].request("DISABLE_NETWORK " + str(id)):
757         raise Exception("Failed to disable network")
758     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
759     if ev is None:
760         raise Exception("Disconnection with the AP timed out")
761     time.sleep(0.1)
762
763     if "OK" not in dev[0].request("ENABLE_NETWORK all"):
764         raise Exception("Failed to enable network")
765     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
766     if ev is None:
767         raise Exception("Association with the AP timed out")
768     if "OK" not in dev[0].request("DISABLE_NETWORK all"):
769         raise Exception("Failed to disable network")
770     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
771     if ev is None:
772         raise Exception("Disconnection with the AP timed out")
773
774 def test_wpas_ctrl_country(dev, apdev):
775     """wpa_supplicant SET/GET country code"""
776     try:
777         # work around issues with possible pending regdom event from the end of
778         # the previous test case
779         time.sleep(0.2)
780         dev[0].dump_monitor()
781
782         if "OK" not in dev[0].request("SET country FI"):
783             raise Exception("Failed to set country code")
784         if dev[0].request("GET country") != "FI":
785             raise Exception("Country code set failed")
786         ev = dev[0].wait_event(["CTRL-EVENT-REGDOM-CHANGE"])
787         if ev is None:
788             raise Exception("regdom change event not seen")
789         if "init=USER type=COUNTRY alpha2=FI" not in ev:
790             raise Exception("Unexpected event contents: " + ev)
791         dev[0].request("SET country 00")
792         if dev[0].request("GET country") != "00":
793             raise Exception("Country code set failed")
794         ev = dev[0].wait_event(["CTRL-EVENT-REGDOM-CHANGE"])
795         if ev is None:
796             raise Exception("regdom change event not seen")
797         if "init=DRIVER type=WORLD" not in ev:
798             raise Exception("Unexpected event contents: " + ev)
799     finally:
800         subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])