tests: wpa_supplicant ctrl_iface CTRL-RSP-
[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 logging
8 logger = logging.getLogger()
9 import subprocess
10 import time
11
12 import hostapd
13 from wpasupplicant import WpaSupplicant
14
15 def test_wpas_ctrl_network(dev):
16     """wpa_supplicant ctrl_iface network set/get"""
17     id = dev[0].add_network()
18
19     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id)):
20         raise Exception("Unexpected success for invalid SET_NETWORK")
21     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + " name"):
22         raise Exception("Unexpected success for invalid SET_NETWORK")
23     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id + 1) + " proto OPEN"):
24         raise Exception("Unexpected success for invalid network id")
25     if "FAIL" not in dev[0].request("GET_NETWORK " + str(id)):
26         raise Exception("Unexpected success for invalid GET_NETWORK")
27     if "FAIL" not in dev[0].request("GET_NETWORK " + str(id + 1) + " proto"):
28         raise Exception("Unexpected success for invalid network id")
29
30     tests = (("key_mgmt", "WPA-PSK WPA-EAP IEEE8021X NONE WPA-NONE FT-PSK FT-EAP WPA-PSK-SHA256 WPA-EAP-SHA256"),
31              ("pairwise", "CCMP-256 GCMP-256 CCMP GCMP TKIP"),
32              ("group", "CCMP-256 GCMP-256 CCMP GCMP TKIP WEP104 WEP40"),
33              ("auth_alg", "OPEN SHARED LEAP"),
34              ("scan_freq", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"),
35              ("freq_list", "2412 2417"),
36              ("scan_ssid", "1"),
37              ("bssid", "00:11:22:33:44:55"),
38              ("proto", "WPA RSN OSEN"),
39              ("eap", "TLS"),
40              ("go_p2p_dev_addr", "22:33:44:55:66:aa"),
41              ("p2p_client_list", "22:33:44:55:66:bb 02:11:22:33:44:55"))
42
43     dev[0].set_network_quoted(id, "ssid", "test")
44     for field, value in tests:
45         dev[0].set_network(id, field, value)
46         res = dev[0].get_network(id, field)
47         if res != value:
48             raise Exception("Unexpected response for '" + field + "': '" + res + "'")
49
50     q_tests = (("identity", "hello"),
51                ("anonymous_identity", "foo@nowhere.com"))
52     for field, value in q_tests:
53         dev[0].set_network_quoted(id, field, value)
54         res = dev[0].get_network(id, field)
55         if res != '"' + value + '"':
56             raise Exception("Unexpected quoted response for '" + field + "': '" + res + "'")
57
58     get_tests = (("foo", None), ("ssid", '"test"'))
59     for field, value in get_tests:
60         res = dev[0].get_network(id, field)
61         if res != value:
62             raise Exception("Unexpected response for '" + field + "': '" + res + "'")
63
64     if dev[0].get_network(id, "password"):
65         raise Exception("Unexpected response for 'password'")
66     dev[0].set_network_quoted(id, "password", "foo")
67     if dev[0].get_network(id, "password") != '*':
68         raise Exception("Unexpected response for 'password' (expected *)")
69     dev[0].set_network(id, "password", "hash:12345678901234567890123456789012")
70     if dev[0].get_network(id, "password") != '*':
71         raise Exception("Unexpected response for 'password' (expected *)")
72     dev[0].set_network(id, "password", "NULL")
73     if dev[0].get_network(id, "password"):
74         raise Exception("Unexpected response for 'password'")
75     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + " password hash:12"):
76         raise Exception("Unexpected success for invalid password hash")
77     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + " password hash:123456789012345678x0123456789012"):
78         raise Exception("Unexpected success for invalid password hash")
79
80     dev[0].set_network(id, "identity", "414243")
81     if dev[0].get_network(id, "identity") != '"ABC"':
82         raise Exception("Unexpected identity hex->text response")
83
84     dev[0].set_network(id, "identity", 'P"abc\ndef"')
85     if dev[0].get_network(id, "identity") != "6162630a646566":
86         raise Exception("Unexpected identity printf->hex response")
87
88     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' identity P"foo'):
89         raise Exception("Unexpected success for invalid identity string")
90
91     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' identity 12x3'):
92         raise Exception("Unexpected success for invalid identity string")
93
94     for i in range(0, 4):
95         if "FAIL" in dev[0].request("SET_NETWORK " + str(id) + ' wep_key' + str(i) + ' aabbccddee'):
96             raise Exception("Unexpected wep_key set failure")
97         if dev[0].get_network(id, "wep_key" + str(i)) != '*':
98             raise Exception("Unexpected wep_key get failure")
99
100     if "FAIL" in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:22:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'):
101         raise Exception("Unexpected failure for psk_list string")
102
103     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list 00:11:x2:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'):
104         raise Exception("Unexpected success for invalid psk_list string")
105
106     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:x2:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'):
107         raise Exception("Unexpected success for invalid psk_list string")
108
109     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:22:33:44:55+0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'):
110         raise Exception("Unexpected success for invalid psk_list string")
111
112     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:22:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde'):
113         raise Exception("Unexpected success for invalid psk_list string")
114
115     if "FAIL" not in dev[0].request("SET_NETWORK " + str(id) + ' psk_list P2P-00:11:22:33:44:55-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdex'):
116         raise Exception("Unexpected success for invalid psk_list string")
117
118     if dev[0].get_network(id, "psk_list"):
119         raise Exception("Unexpected psk_list get response")
120
121     if dev[0].list_networks()[0]['ssid'] != "test":
122         raise Exception("Unexpected ssid in LIST_NETWORKS")
123     dev[0].set_network(id, "ssid", "NULL")
124     if dev[0].list_networks()[0]['ssid'] != "":
125         raise Exception("Unexpected ssid in LIST_NETWORKS after clearing it")
126
127     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' ssid "0123456789abcdef0123456789abcdef0"'):
128         raise Exception("Too long SSID accepted")
129     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' scan_ssid qwerty'):
130         raise Exception("Invalid integer accepted")
131     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' scan_ssid 2'):
132         raise Exception("Too large integer accepted")
133     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' psk 12345678'):
134         raise Exception("Invalid PSK accepted")
135     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' psk "1234567"'):
136         raise Exception("Too short PSK accepted")
137     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' psk "1234567890123456789012345678901234567890123456789012345678901234"'):
138         raise Exception("Too long PSK accepted")
139     dev[0].set_network_quoted(id, "psk", "123456768");
140     dev[0].set_network_quoted(id, "psk", "123456789012345678901234567890123456789012345678901234567890123");
141     if dev[0].get_network(id, "psk") != '*':
142         raise Exception("Unexpected psk read result");
143
144     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' eap UNKNOWN'):
145         raise Exception("Unknown EAP method accepted")
146
147     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' password "foo'):
148         raise Exception("Invalid password accepted")
149
150     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' wep_key0 "foo'):
151         raise Exception("Invalid WEP key accepted")
152     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' wep_key0 "12345678901234567"'):
153         raise Exception("Too long WEP key accepted")
154     # too short WEP key is ignored
155     dev[0].set_network_quoted(id, "wep_key0", "1234")
156     dev[0].set_network_quoted(id, "wep_key1", "12345")
157     dev[0].set_network_quoted(id, "wep_key2", "1234567890123")
158     dev[0].set_network_quoted(id, "wep_key3", "1234567890123456")
159
160     dev[0].set_network(id, "go_p2p_dev_addr", "any")
161     if dev[0].get_network(id, "go_p2p_dev_addr") is not None:
162         raise Exception("Unexpected go_p2p_dev_addr value")
163     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' go_p2p_dev_addr 00:11:22:33:44'):
164         raise Exception("Invalid go_p2p_dev_addr accepted")
165     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' p2p_client_list 00:11:22:33:44'):
166         raise Exception("Invalid p2p_client_list accepted")
167     if "FAIL" in dev[0].request('SET_NETWORK ' + str(id) + ' p2p_client_list 00:11:22:33:44:55 00:1'):
168         raise Exception("p2p_client_list truncation workaround failed")
169     if dev[0].get_network(id, "p2p_client_list") != "00:11:22:33:44:55":
170         raise Exception("p2p_client_list truncation workaround did not work")
171
172     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' auth_alg '):
173         raise Exception("Empty auth_alg accepted")
174     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' auth_alg FOO'):
175         raise Exception("Invalid auth_alg accepted")
176
177     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' proto '):
178         raise Exception("Empty proto accepted")
179     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' proto FOO'):
180         raise Exception("Invalid proto accepted")
181
182     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' pairwise '):
183         raise Exception("Empty pairwise accepted")
184     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' pairwise FOO'):
185         raise Exception("Invalid pairwise accepted")
186     if "FAIL" not in dev[0].request('SET_NETWORK ' + str(id) + ' pairwise WEP40'):
187         raise Exception("Invalid pairwise accepted")
188
189     if "OK" not in dev[0].request('BSSID ' + str(id) + ' 00:11:22:33:44:55'):
190         raise Exception("Unexpected BSSID failure")
191     if dev[0].request("GET_NETWORK 0 bssid") != '00:11:22:33:44:55':
192         raise Exception("BSSID command did not set network bssid")
193     if "OK" not in dev[0].request('BSSID ' + str(id) + ' 00:00:00:00:00:00'):
194         raise Exception("Unexpected BSSID failure")
195     if "FAIL" not in dev[0].request("GET_NETWORK 0 bssid"):
196         raise Exception("bssid claimed configured after clearing")
197     if "FAIL" not in dev[0].request('BSSID 123 00:11:22:33:44:55'):
198         raise Exception("Unexpected BSSID success")
199     if "FAIL" not in dev[0].request('BSSID ' + str(id) + ' 00:11:22:33:44'):
200         raise Exception("Unexpected BSSID success")
201
202 def test_wpas_ctrl_many_networks(dev, apdev):
203     """wpa_supplicant ctrl_iface LIST_NETWORKS with huge number of networks"""
204     for i in range(1000):
205         id = dev[0].add_network()
206     res = dev[0].request("LIST_NETWORKS")
207     if str(id) in res:
208         raise Exception("Last added network was unexpectedly included")
209     res = dev[0].request("LIST_NETWORKS LAST_ID=%d" % (id - 2))
210     if str(id) not in res:
211         raise Exception("Last added network was not present when using LAST_ID")
212
213 def test_wpas_ctrl_dup_network(dev, apdev):
214     """wpa_supplicant ctrl_iface DUP_NETWORK"""
215     ssid = "target"
216     passphrase = 'qwertyuiop'
217     params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
218     hostapd.add_ap(apdev[0]['ifname'], params)
219
220     src = dev[0].connect("another", psk=passphrase, scan_freq="2412",
221                          only_add_network=True)
222     id = dev[0].add_network()
223     dev[0].set_network_quoted(id, "ssid", ssid)
224     for f in [ "key_mgmt", "psk", "scan_freq" ]:
225         res = dev[0].request("DUP_NETWORK {} {} {}".format(src, id, f))
226         if "OK" not in res:
227             raise Exception("DUP_NETWORK failed")
228     dev[0].connect_network(id)
229
230 def add_cred(dev):
231     id = dev.add_cred()
232     ev = dev.wait_event(["CRED-ADDED"])
233     if ev is None:
234         raise Exception("Missing CRED-ADDED event")
235     if " " + str(id) not in ev:
236         raise Exception("CRED-ADDED event without matching id")
237     return id
238
239 def set_cred(dev, id, field, value):
240     dev.set_cred(id, field, value)
241     ev = dev.wait_event(["CRED-MODIFIED"])
242     if ev is None:
243         raise Exception("Missing CRED-MODIFIED event")
244     if " " + str(id) + " " not in ev:
245         raise Exception("CRED-MODIFIED event without matching id")
246     if field not in ev:
247         raise Exception("CRED-MODIFIED event without matching field")
248
249 def set_cred_quoted(dev, id, field, value):
250     dev.set_cred_quoted(id, field, value)
251     ev = dev.wait_event(["CRED-MODIFIED"])
252     if ev is None:
253         raise Exception("Missing CRED-MODIFIED event")
254     if " " + str(id) + " " not in ev:
255         raise Exception("CRED-MODIFIED event without matching id")
256     if field not in ev:
257         raise Exception("CRED-MODIFIED event without matching field")
258
259 def remove_cred(dev, id):
260     dev.remove_cred(id)
261     ev = dev.wait_event(["CRED-REMOVED"])
262     if ev is None:
263         raise Exception("Missing CRED-REMOVED event")
264     if " " + str(id) not in ev:
265         raise Exception("CRED-REMOVED event without matching id")
266
267 def test_wpas_ctrl_cred(dev):
268     """wpa_supplicant ctrl_iface cred set"""
269     id1 = add_cred(dev[0])
270     if "FAIL" not in dev[0].request("SET_CRED " + str(id1 + 1) + " temporary 1"):
271         raise Exception("SET_CRED succeeded unexpectedly on unknown cred id")
272     if "FAIL" not in dev[0].request("SET_CRED " + str(id1)):
273         raise Exception("Invalid SET_CRED succeeded unexpectedly")
274     if "FAIL" not in dev[0].request("SET_CRED " + str(id1) + " temporary"):
275         raise Exception("Invalid SET_CRED succeeded unexpectedly")
276     if "FAIL" not in dev[0].request("GET_CRED " + str(id1 + 1) + " temporary"):
277         raise Exception("GET_CRED succeeded unexpectedly on unknown cred id")
278     if "FAIL" not in dev[0].request("GET_CRED " + str(id1)):
279         raise Exception("Invalid GET_CRED succeeded unexpectedly")
280     if "FAIL" not in dev[0].request("GET_CRED " + str(id1) + " foo"):
281         raise Exception("Invalid GET_CRED succeeded unexpectedly")
282     id = add_cred(dev[0])
283     id2 = add_cred(dev[0])
284     set_cred(dev[0], id, "temporary", "1")
285     set_cred(dev[0], id, "priority", "1")
286     set_cred(dev[0], id, "pcsc", "1")
287     set_cred_quoted(dev[0], id, "private_key_passwd", "test")
288     set_cred_quoted(dev[0], id, "domain_suffix_match", "test")
289     set_cred_quoted(dev[0], id, "phase1", "test")
290     set_cred_quoted(dev[0], id, "phase2", "test")
291
292     if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " eap FOO"):
293         raise Exception("Unexpected success on unknown EAP method")
294
295     if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " username 12xa"):
296         raise Exception("Unexpected success on invalid string")
297
298     for i in ("11", "1122", "112233445566778899aabbccddeeff00"):
299         if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " roaming_consortium " + i):
300             raise Exception("Unexpected success on invalid roaming_consortium")
301
302     dev[0].set_cred(id, "excluded_ssid", "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff")
303     if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " excluded_ssid 00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00"):
304         raise Exception("Unexpected success on invalid excluded_ssid")
305
306     if "FAIL" not in dev[0].request("SET_CRED " + str(id) + " foo 4142"):
307         raise Exception("Unexpected success on unknown field")
308
309     id3 = add_cred(dev[0])
310     id4 = add_cred(dev[0])
311     if len(dev[0].request("LIST_CREDS").splitlines()) != 6:
312         raise Exception("Unexpected LIST_CREDS result(1)")
313
314     remove_cred(dev[0], id1)
315     remove_cred(dev[0], id3)
316     remove_cred(dev[0], id4)
317     remove_cred(dev[0], id2)
318     remove_cred(dev[0], id)
319     if "FAIL" not in dev[0].request("REMOVE_CRED 1"):
320         raise Exception("Unexpected success on invalid remove cred")
321     if len(dev[0].request("LIST_CREDS").splitlines()) != 1:
322         raise Exception("Unexpected LIST_CREDS result(2)")
323
324     id = add_cred(dev[0])
325     values = [ ("temporary", "1", False),
326                ("temporary", "0", False),
327                ("pcsc", "1", False),
328                ("realm", "example.com", True),
329                ("username", "user@example.com", True),
330                ("password", "foo", True, "*"),
331                ("ca_cert", "ca.pem", True),
332                ("client_cert", "user.pem", True),
333                ("private_key", "key.pem", True),
334                ("private_key_passwd", "foo", True, "*"),
335                ("imsi", "310026-000000000", True),
336                ("milenage", "foo", True, "*"),
337                ("domain_suffix_match", "example.com", True),
338                ("domain", "example.com", True),
339                ("domain", "example.org", True, "example.com\nexample.org"),
340                ("roaming_consortium", "0123456789", False),
341                ("required_roaming_consortium", "456789", False),
342                ("eap", "TTLS", False),
343                ("phase1", "foo=bar1", True),
344                ("phase2", "foo=bar2", True),
345                ("excluded_ssid", "test", True),
346                ("excluded_ssid", "foo", True, "test\nfoo"),
347                ("roaming_partner", "example.com,0,4,*", True),
348                ("roaming_partner", "example.org,1,2,US", True,
349                 "example.com,0,4,*\nexample.org,1,2,US"),
350                ("update_identifier", "4", False),
351                ("provisioning_sp", "sp.example.com", True),
352                ("sp_priority", "7", False),
353                ("min_dl_bandwidth_home", "100", False),
354                ("min_ul_bandwidth_home", "101", False),
355                ("min_dl_bandwidth_roaming", "102", False),
356                ("min_ul_bandwidth_roaming", "103", False),
357                ("max_bss_load", "57", False),
358                ("req_conn_capab", "6:22,80,443", False),
359                ("req_conn_capab", "17:500", False, "6:22,80,443\n17:500"),
360                ("req_conn_capab", "50", False, "6:22,80,443\n17:500\n50"),
361                ("ocsp", "1", False) ]
362     for v in values:
363         if v[2]:
364             set_cred_quoted(dev[0], id, v[0], v[1])
365         else:
366             set_cred(dev[0], id, v[0], v[1])
367         val = dev[0].get_cred(id, v[0])
368         if len(v) == 4:
369             expect = v[3]
370         else:
371             expect = v[1]
372         if val != expect:
373             raise Exception("Unexpected GET_CRED value for {}: {} != {}".format(v[0], val, expect))
374     creds = dev[0].request("LIST_CREDS").splitlines()
375     if len(creds) != 2:
376         raise Exception("Unexpected LIST_CREDS result(3)")
377     if creds[1] != "0\texample.com\tuser@example.com\texample.com\t310026-000000000":
378         raise Exception("Unexpected LIST_CREDS value")
379     remove_cred(dev[0], id)
380     if len(dev[0].request("LIST_CREDS").splitlines()) != 1:
381         raise Exception("Unexpected LIST_CREDS result(4)")
382
383     id = add_cred(dev[0])
384     set_cred_quoted(dev[0], id, "domain", "foo.example.com")
385     id = add_cred(dev[0])
386     set_cred_quoted(dev[0], id, "domain", "bar.example.com")
387     id = add_cred(dev[0])
388     set_cred_quoted(dev[0], id, "domain", "foo.example.com")
389     if "OK" not in dev[0].request("REMOVE_CRED sp_fqdn=foo.example.com"):
390         raise Exception("REMOVE_CRED failed")
391     creds = dev[0].request("LIST_CREDS")
392     if "foo.example.com" in creds:
393         raise Exception("REMOVE_CRED sp_fqdn did not remove cred")
394     if "bar.example.com" not in creds:
395         raise Exception("REMOVE_CRED sp_fqdn removed incorrect cred")
396     dev[0].request("REMOVE_CRED all")
397
398     id = add_cred(dev[0])
399     set_cred_quoted(dev[0], id, "domain", "foo.example.com")
400     set_cred_quoted(dev[0], id, "provisioning_sp", "sp.foo.example.com")
401     id = add_cred(dev[0])
402     set_cred_quoted(dev[0], id, "domain", "bar.example.com")
403     set_cred_quoted(dev[0], id, "provisioning_sp", "sp.bar.example.com")
404     id = add_cred(dev[0])
405     set_cred_quoted(dev[0], id, "domain", "foo.example.com")
406     set_cred_quoted(dev[0], id, "provisioning_sp", "sp.foo.example.com")
407     if "OK" not in dev[0].request("REMOVE_CRED provisioning_sp=sp.foo.example.com"):
408         raise Exception("REMOVE_CRED failed")
409     creds = dev[0].request("LIST_CREDS")
410     if "foo.example.com" in creds:
411         raise Exception("REMOVE_CRED provisioning_sp did not remove cred")
412     if "bar.example.com" not in creds:
413         raise Exception("REMOVE_CRED provisioning_sp removed incorrect cred")
414     dev[0].request("REMOVE_CRED all")
415
416     # Test large number of creds and LIST_CREDS truncation
417     dev[0].dump_monitor()
418     for i in range(0, 100):
419         id = add_cred(dev[0])
420         set_cred_quoted(dev[0], id, "realm", "relatively.long.realm.test%d.example.com" % i)
421         dev[0].dump_monitor()
422     creds = dev[0].request("LIST_CREDS")
423     for i in range(0, 100):
424         dev[0].remove_cred(i)
425         dev[0].dump_monitor()
426     if len(creds) < 3900 or len(creds) > 4100:
427         raise Exception("Unexpected LIST_CREDS length: %d" % len(creds))
428     if "test10.example.com" not in creds:
429         raise Exception("Missing credential")
430     if len(creds.splitlines()) > 95:
431         raise Exception("Too many LIST_CREDS entries in the buffer")
432
433 def test_wpas_ctrl_pno(dev):
434     """wpa_supplicant ctrl_iface pno"""
435     if "FAIL" not in dev[0].request("SET pno 1"):
436         raise Exception("Unexpected success in enabling PNO without enabled network blocks")
437     id = dev[0].add_network()
438     dev[0].set_network_quoted(id, "ssid", "test")
439     dev[0].set_network(id, "key_mgmt", "NONE")
440     dev[0].request("ENABLE_NETWORK " + str(id) + " no-connect")
441     #mac80211_hwsim does not yet support PNO, so this fails
442     if "FAIL" not in dev[0].request("SET pno 1"):
443         raise Exception("Unexpected success in enabling PNO")
444     if "FAIL" not in dev[0].request("SET pno 1 freq=2000-3000,5180"):
445         raise Exception("Unexpected success in enabling PNO")
446     if "FAIL" not in dev[0].request("SET pno 1 freq=0-6000"):
447         raise Exception("Unexpected success in enabling PNO")
448     if "FAIL" in dev[0].request("SET pno 0"):
449         raise Exception("Unexpected failure in disabling PNO")
450
451 def test_wpas_ctrl_get(dev):
452     """wpa_supplicant ctrl_iface get"""
453     if "FAIL" in dev[0].request("GET version"):
454         raise Exception("Unexpected get failure for version")
455     if "FAIL" in dev[0].request("GET wifi_display"):
456         raise Exception("Unexpected get failure for wifi_display")
457     if "FAIL" not in dev[0].request("GET foo"):
458         raise Exception("Unexpected success on get command")
459
460 def test_wpas_ctrl_preauth(dev):
461     """wpa_supplicant ctrl_iface preauth"""
462     if "FAIL" not in dev[0].request("PREAUTH "):
463         raise Exception("Unexpected success on invalid PREAUTH")
464     if "FAIL" in dev[0].request("PREAUTH 00:11:22:33:44:55"):
465         raise Exception("Unexpected failure on PREAUTH")
466
467 def test_wpas_ctrl_stkstart(dev):
468     """wpa_supplicant ctrl_iface strkstart"""
469     if "FAIL" not in dev[0].request("STKSTART "):
470         raise Exception("Unexpected success on invalid STKSTART")
471     if "FAIL" not in dev[0].request("STKSTART 00:11:22:33:44:55"):
472         raise Exception("Unexpected success on STKSTART")
473
474 def test_wpas_ctrl_tdls_discover(dev):
475     """wpa_supplicant ctrl_iface tdls_discover"""
476     if "FAIL" not in dev[0].request("TDLS_DISCOVER "):
477         raise Exception("Unexpected success on invalid TDLS_DISCOVER")
478     if "FAIL" not in dev[0].request("TDLS_DISCOVER 00:11:22:33:44:55"):
479         raise Exception("Unexpected success on TDLS_DISCOVER")
480
481 def test_wpas_ctrl_addr(dev):
482     """wpa_supplicant ctrl_iface invalid address"""
483     if "FAIL" not in dev[0].request("TDLS_SETUP "):
484         raise Exception("Unexpected success on invalid TDLS_SETUP")
485     if "FAIL" not in dev[0].request("TDLS_TEARDOWN "):
486         raise Exception("Unexpected success on invalid TDLS_TEARDOWN")
487     if "FAIL" not in dev[0].request("FT_DS "):
488         raise Exception("Unexpected success on invalid FT_DS")
489     if "FAIL" not in dev[0].request("WPS_PBC 00:11:22:33:44"):
490         raise Exception("Unexpected success on invalid WPS_PBC")
491     if "FAIL" not in dev[0].request("WPS_PIN 00:11:22:33:44"):
492         raise Exception("Unexpected success on invalid WPS_PIN")
493     if "FAIL" not in dev[0].request("WPS_NFC 00:11:22:33:44"):
494         raise Exception("Unexpected success on invalid WPS_NFC")
495     if "FAIL" not in dev[0].request("WPS_REG 00:11:22:33:44 12345670"):
496         raise Exception("Unexpected success on invalid WPS_REG")
497     if "FAIL" not in dev[0].request("IBSS_RSN 00:11:22:33:44"):
498         raise Exception("Unexpected success on invalid IBSS_RSN")
499     if "FAIL" not in dev[0].request("BLACKLIST 00:11:22:33:44"):
500         raise Exception("Unexpected success on invalid BLACKLIST")
501
502 def test_wpas_ctrl_wps_errors(dev):
503     """wpa_supplicant ctrl_iface WPS error cases"""
504     if "FAIL" not in dev[0].request("WPS_REG 00:11:22:33:44:55"):
505         raise Exception("Unexpected success on invalid WPS_REG")
506     if "FAIL" not in dev[0].request("WPS_REG 00:11:22:33:44:55 12345670 2233"):
507         raise Exception("Unexpected success on invalid WPS_REG")
508     if "FAIL" not in dev[0].request("WPS_REG 00:11:22:33:44:55 12345670 2233 OPEN"):
509         raise Exception("Unexpected success on invalid WPS_REG")
510     if "FAIL" not in dev[0].request("WPS_REG 00:11:22:33:44:55 12345670 2233 OPEN NONE"):
511         raise Exception("Unexpected success on invalid WPS_REG")
512
513     if "FAIL" not in dev[0].request("WPS_AP_PIN random"):
514         raise Exception("Unexpected success on WPS_AP_PIN in non-AP mode")
515
516     if "FAIL" not in dev[0].request("WPS_ER_PIN any"):
517         raise Exception("Unexpected success on invalid WPS_ER_PIN")
518
519     if "FAIL" not in dev[0].request("WPS_ER_LEARN 00:11:22:33:44:55"):
520         raise Exception("Unexpected success on invalid WPS_ER_LEARN")
521
522     if "FAIL" not in dev[0].request("WPS_ER_SET_CONFIG 00:11:22:33:44:55"):
523         raise Exception("Unexpected success on invalid WPS_ER_SET_CONFIG")
524
525     if "FAIL" not in dev[0].request("WPS_ER_CONFIG 00:11:22:33:44:55"):
526         raise Exception("Unexpected success on invalid WPS_ER_CONFIG")
527     if "FAIL" not in dev[0].request("WPS_ER_CONFIG 00:11:22:33:44:55 12345670"):
528         raise Exception("Unexpected success on invalid WPS_ER_CONFIG")
529     if "FAIL" not in dev[0].request("WPS_ER_CONFIG 00:11:22:33:44:55 12345670 2233"):
530         raise Exception("Unexpected success on invalid WPS_ER_CONFIG")
531     if "FAIL" not in dev[0].request("WPS_ER_CONFIG 00:11:22:33:44:55 12345670 2233 OPEN"):
532         raise Exception("Unexpected success on invalid WPS_ER_CONFIG")
533     if "FAIL" not in dev[0].request("WPS_ER_CONFIG 00:11:22:33:44:55 12345670 2233 OPEN NONE"):
534         raise Exception("Unexpected success on invalid WPS_ER_CONFIG")
535
536     if "FAIL" not in dev[0].request("WPS_ER_NFC_CONFIG_TOKEN WPS"):
537         raise Exception("Unexpected success on invalid WPS_ER_NFC_CONFIG_TOKEN")
538     if "FAIL" not in dev[0].request("WPS_ER_NFC_CONFIG_TOKEN FOO 00:11:22:33:44:55"):
539         raise Exception("Unexpected success on invalid WPS_ER_NFC_CONFIG_TOKEN")
540     if "FAIL" not in dev[0].request("WPS_ER_NFC_CONFIG_TOKEN NDEF 00:11:22:33:44:55"):
541         raise Exception("Unexpected success on invalid WPS_ER_NFC_CONFIG_TOKEN")
542
543     if "FAIL" not in dev[0].request("WPS_NFC_CONFIG_TOKEN FOO"):
544         raise Exception("Unexpected success on invalid WPS_NFC_CONFIG_TOKEN")
545     if "FAIL" not in dev[0].request("WPS_NFC_CONFIG_TOKEN WPS FOO"):
546         raise Exception("Unexpected success on invalid WPS_NFC_CONFIG_TOKEN")
547     if "FAIL" not in dev[0].request("WPS_NFC_TOKEN FOO"):
548         raise Exception("Unexpected success on invalid WPS_NFC_TOKEN")
549
550 def test_wpas_ctrl_config_parser(dev):
551     """wpa_supplicant ctrl_iface SET config parser"""
552     if "FAIL" not in dev[0].request("SET pbc_in_m1 qwerty"):
553         raise Exception("Non-number accepted as integer")
554     if "FAIL" not in dev[0].request("SET eapol_version 0"):
555         raise Exception("Out-of-range value accepted")
556     if "FAIL" not in dev[0].request("SET eapol_version 10"):
557         raise Exception("Out-of-range value accepted")
558
559     if "FAIL" not in dev[0].request("SET serial_number 0123456789abcdef0123456789abcdef0"):
560         raise Exception("Too long string accepted")
561
562 def test_wpas_ctrl_mib(dev):
563     """wpa_supplicant ctrl_iface MIB"""
564     mib = dev[0].get_mib()
565     if "dot11RSNAOptionImplemented" not in mib:
566         raise Exception("Missing MIB entry")
567     if mib["dot11RSNAOptionImplemented"] != "TRUE":
568         raise Exception("Unexpected dot11RSNAOptionImplemented value")
569
570 def test_wpas_ctrl_set_wps_params(dev):
571     """wpa_supplicant ctrl_iface SET config_methods"""
572     ts = [ "config_methods label virtual_display virtual_push_button keypad",
573            "device_type 1-0050F204-1",
574            "os_version 01020300",
575            "uuid 12345678-9abc-def0-1234-56789abcdef0" ]
576     for t in ts:
577         if "OK" not in dev[2].request("SET " + t):
578             raise Exception("SET failed for: " + t)
579
580 def test_wpas_ctrl_level(dev):
581     """wpa_supplicant ctrl_iface LEVEL"""
582     try:
583         if "FAIL" not in dev[2].request("LEVEL 3"):
584             raise Exception("Unexpected LEVEL success")
585         if "OK" not in dev[2].mon.request("LEVEL 2"):
586             raise Exception("Unexpected LEVEL failure")
587         dev[2].request("SCAN freq=2412")
588         ev = dev[2].wait_event(["State:"], timeout=5)
589         if ev is None:
590             raise Exception("No debug message received")
591         dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=5)
592     finally:
593         dev[2].mon.request("LEVEL 3")
594
595 def test_wpas_ctrl_bssid_filter(dev, apdev):
596     """wpa_supplicant bssid_filter"""
597     try:
598         if "OK" not in dev[2].request("SET bssid_filter " + apdev[0]['bssid']):
599             raise Exception("Failed to set bssid_filter")
600         params = { "ssid": "test" }
601         hostapd.add_ap(apdev[0]['ifname'], params)
602         hostapd.add_ap(apdev[1]['ifname'], params)
603         dev[2].scan_for_bss(apdev[0]['bssid'], freq="2412")
604         dev[2].scan(freq="2412")
605         bss = dev[2].get_bss(apdev[0]['bssid'])
606         if bss is None or len(bss) == 0:
607             raise Exception("Missing BSS data")
608         bss = dev[2].get_bss(apdev[1]['bssid'])
609         if bss and len(bss) != 0:
610             raise Exception("Unexpected BSS data")
611         dev[2].request("SET bssid_filter ")
612         dev[2].scan(freq="2412")
613         bss = dev[2].get_bss(apdev[0]['bssid'])
614         if bss is None or len(bss) == 0:
615             raise Exception("Missing BSS data")
616         bss = dev[2].get_bss(apdev[1]['bssid'])
617         if bss is None or len(bss) == 0:
618             raise Exception("Missing BSS data(2)")
619         res = dev[2].request("SCAN_RESULTS").splitlines()
620         if "test" not in res[1] or "test" not in res[2]:
621             raise Exception("SSID missing from SCAN_RESULTS")
622         if apdev[0]['bssid'] not in res[1] and apdev[1]['bssid'] not in res[1]:
623             raise Exception("BSS1 missing from SCAN_RESULTS")
624         if apdev[0]['bssid'] not in res[2] and apdev[1]['bssid'] not in res[2]:
625             raise Exception("BSS1 missing from SCAN_RESULTS")
626
627         if "FAIL" not in dev[2].request("SET bssid_filter 00:11:22:33:44:55 00:11:22:33:44"):
628             raise Exception("Unexpected success for invalid SET bssid_filter")
629     finally:
630         dev[2].request("SET bssid_filter ")
631
632 def test_wpas_ctrl_disallow_aps(dev, apdev):
633     """wpa_supplicant ctrl_iface disallow_aps"""
634     params = { "ssid": "test" }
635     hostapd.add_ap(apdev[0]['ifname'], params)
636
637     if "FAIL" not in dev[0].request("SET disallow_aps bssid "):
638         raise Exception("Unexpected success on invalid disallow_aps")
639     if "FAIL" not in dev[0].request("SET disallow_aps bssid 00:11:22:33:44"):
640         raise Exception("Unexpected success on invalid disallow_aps")
641     if "FAIL" not in dev[0].request("SET disallow_aps ssid 0"):
642         raise Exception("Unexpected success on invalid disallow_aps")
643     if "FAIL" not in dev[0].request("SET disallow_aps ssid 4q"):
644         raise Exception("Unexpected success on invalid disallow_aps")
645     if "FAIL" not in dev[0].request("SET disallow_aps bssid 00:11:22:33:44:55 ssid 112233 ssid 123"):
646         raise Exception("Unexpected success on invalid disallow_aps")
647     if "FAIL" not in dev[0].request("SET disallow_aps ssid 000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f00"):
648         raise Exception("Unexpected success on invalid disallow_aps")
649     if "FAIL" not in dev[0].request("SET disallow_aps foo 112233445566"):
650         raise Exception("Unexpected success on invalid disallow_aps")
651
652     dev[0].connect("test", key_mgmt="NONE", scan_freq="2412")
653     hostapd.add_ap(apdev[1]['ifname'], params)
654     dev[0].dump_monitor()
655     if "OK" not in dev[0].request("SET disallow_aps bssid 00:11:22:33:44:55 bssid 00:22:33:44:55:66"):
656         raise Exception("Failed to set disallow_aps")
657     if "OK" not in dev[0].request("SET disallow_aps bssid " + apdev[0]['bssid']):
658         raise Exception("Failed to set disallow_aps")
659     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
660     if ev is None:
661         raise Exception("Reassociation timed out")
662     if apdev[1]['bssid'] not in ev:
663         raise Exception("Unexpected BSSID")
664
665     dev[0].dump_monitor()
666     if "OK" not in dev[0].request("SET disallow_aps ssid " + "test".encode("hex")):
667         raise Exception("Failed to set disallow_aps")
668     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
669     if ev is None:
670         raise Exception("Disconnection not seen")
671     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
672     if ev is not None:
673         raise Exception("Unexpected reassociation")
674
675     dev[0].request("DISCONNECT")
676     dev[0].p2p_start_go(freq=2412)
677     if "OK" not in dev[0].request("SET disallow_aps "):
678         raise Exception("Failed to set disallow_aps")
679
680 def test_wpas_ctrl_blob(dev):
681     """wpa_supplicant ctrl_iface SET blob"""
682     if "FAIL" not in dev[0].request("SET blob foo"):
683         raise Exception("Unexpected SET success")
684     if "FAIL" not in dev[0].request("SET blob foo 0"):
685         raise Exception("Unexpected SET success")
686     if "FAIL" not in dev[0].request("SET blob foo 0q"):
687         raise Exception("Unexpected SET success")
688     if "OK" not in dev[0].request("SET blob foo 00"):
689         raise Exception("Unexpected SET failure")
690     if "OK" not in dev[0].request("SET blob foo 0011"):
691         raise Exception("Unexpected SET failure")
692
693 def test_wpas_ctrl_set_uapsd(dev):
694     """wpa_supplicant ctrl_iface SET uapsd"""
695     if "FAIL" not in dev[0].request("SET uapsd foo"):
696         raise Exception("Unexpected SET success")
697     if "FAIL" not in dev[0].request("SET uapsd 0,0,0"):
698         raise Exception("Unexpected SET success")
699     if "FAIL" not in dev[0].request("SET uapsd 0,0"):
700         raise Exception("Unexpected SET success")
701     if "FAIL" not in dev[0].request("SET uapsd 0"):
702         raise Exception("Unexpected SET success")
703     if "OK" not in dev[0].request("SET uapsd 1,1,1,1;1"):
704         raise Exception("Unexpected SET failure")
705     if "OK" not in dev[0].request("SET uapsd 0,0,0,0;0"):
706         raise Exception("Unexpected SET failure")
707     if "OK" not in dev[0].request("SET uapsd disable"):
708         raise Exception("Unexpected SET failure")
709
710 def test_wpas_ctrl_set(dev):
711     """wpa_supplicant ctrl_iface SET"""
712     vals = [ "foo",
713              "ampdu 0",
714              "radio_disable 0",
715              "ps 10",
716              "ps 1",
717              "dot11RSNAConfigPMKLifetime 0",
718              "dot11RSNAConfigPMKReauthThreshold 101",
719              "dot11RSNAConfigSATimeout 0",
720              "wps_version_number -1",
721              "wps_version_number 256" ]
722     for val in vals:
723         if "FAIL" not in dev[0].request("SET " + val):
724             raise Exception("Unexpected SET success for " + val)
725
726     vals = [ "EAPOL::heldPeriod 60",
727              "EAPOL::authPeriod 30",
728              "EAPOL::startPeriod 30",
729              "EAPOL::maxStart 3",
730              "dot11RSNAConfigSATimeout 60",
731              "ps -1",
732              "ps 0",
733              "no_keep_alive 0",
734              "tdls_disabled 1",
735              "tdls_disabled 0" ]
736     for val in vals:
737         if "OK" not in dev[0].request("SET " + val):
738             raise Exception("Unexpected SET failure for " + val)
739
740 def test_wpas_ctrl_get_capability(dev):
741     """wpa_supplicant ctrl_iface GET_CAPABILITY"""
742     if "FAIL" not in dev[0].request("GET_CAPABILITY 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"):
743         raise Exception("Unexpected success on invalid GET_CAPABILITY")
744     if "AP" not in dev[0].request("GET_CAPABILITY modes strict"):
745         raise Exception("Unexpected GET_CAPABILITY response")
746     res = dev[0].get_capability("eap")
747     if "TTLS" not in res:
748         raise Exception("Unexpected GET_CAPABILITY eap response: " + str(res))
749
750     res = dev[0].get_capability("pairwise")
751     if "CCMP" not in res:
752         raise Exception("Unexpected GET_CAPABILITY pairwise response: " + str(res))
753
754     res = dev[0].get_capability("group")
755     if "CCMP" not in res:
756         raise Exception("Unexpected GET_CAPABILITY group response: " + str(res))
757
758     res = dev[0].get_capability("key_mgmt")
759     if "WPA-PSK" not in res or "WPA-EAP" not in res:
760         raise Exception("Unexpected GET_CAPABILITY key_mgmt response: " + str(res))
761
762     res = dev[0].get_capability("proto")
763     if "WPA" not in res or "RSN" not in res:
764         raise Exception("Unexpected GET_CAPABILITY proto response: " + str(res))
765
766     res = dev[0].get_capability("auth_alg")
767     if "OPEN" not in res or "SHARED" not in res:
768         raise Exception("Unexpected GET_CAPABILITY auth_alg response: " + str(res))
769
770     res = dev[0].get_capability("modes")
771     if "IBSS" not in res or "AP" not in res:
772         raise Exception("Unexpected GET_CAPABILITY modes response: " + str(res))
773
774     res = dev[0].get_capability("channels")
775     if "8" not in res or "36" not in res:
776         raise Exception("Unexpected GET_CAPABILITY channels response: " + str(res))
777
778     res = dev[0].get_capability("freq")
779     if "2457" not in res or "5180" not in res:
780         raise Exception("Unexpected GET_CAPABILITY freq response: " + str(res))
781
782     res = dev[0].get_capability("tdls")
783     if "EXTERNAL" not in res[0]:
784         raise Exception("Unexpected GET_CAPABILITY tdls response: " + str(res))
785
786     if dev[0].get_capability("foo") is not None:
787         raise Exception("Unexpected GET_CAPABILITY foo response: " + str(res))
788
789 def test_wpas_ctrl_nfc_report_handover(dev):
790     """wpa_supplicant ctrl_iface NFC_REPORT_HANDOVER"""
791     vals = [ "FOO",
792              "ROLE freq=12345",
793              "ROLE TYPE",
794              "ROLE TYPE REQ",
795              "ROLE TYPE REQ SEL",
796              "ROLE TYPE 0Q SEL",
797              "ROLE TYPE 00 SEL",
798              "ROLE TYPE 00 0Q",
799              "ROLE TYPE 00 00" ]
800     for v in vals:
801         if "FAIL" not in dev[0].request("NFC_REPORT_HANDOVER " + v):
802             raise Exception("Unexpected NFC_REPORT_HANDOVER success for " + v)
803
804 def test_wpas_ctrl_nfc_tag_read(dev):
805     """wpa_supplicant ctrl_iface WPS_NFC_TAG_READ"""
806     vals = [ "FOO", "0Q", "00", "000000", "10000001", "10000000", "00000000",
807              "100e0000", "100e0001ff", "100e000411110000", "100e0004100e0001" ]
808     for v in vals:
809         if "FAIL" not in dev[0].request("WPS_NFC_TAG_READ " + v):
810             raise Exception("Unexpected WPS_NFC_TAG_READ success for " + v)
811
812 def test_wpas_ctrl_nfc_get_handover(dev):
813     """wpa_supplicant ctrl_iface NFC_GET_HANDOVER"""
814     vals = [ "FOO", "FOO BAR", "WPS WPS", "WPS WPS-CR", "WPS FOO", "NDEF P2P" ]
815     for v in vals:
816         if "FAIL" not in dev[0].request("NFC_GET_HANDOVER_REQ " + v):
817             raise Exception("Unexpected NFC_GET_HANDOVER_REQ success for " + v)
818
819     vals = [ "NDEF WPS", "NDEF P2P-CR", "WPS P2P-CR" ]
820     for v in vals:
821         if "FAIL" in dev[0].request("NFC_GET_HANDOVER_REQ " + v):
822             raise Exception("Unexpected NFC_GET_HANDOVER_REQ failure for " + v)
823
824     vals = [ "FOO", "FOO BAR", "WPS WPS", "WPS WPS-CR", "WPS FOO", "NDEF P2P",
825              "NDEF WPS", "NDEF WPS uuid" ]
826     for v in vals:
827         if "FAIL" not in dev[0].request("NFC_GET_HANDOVER_SEL " + v):
828             raise Exception("Unexpected NFC_GET_HANDOVER_SEL success for " + v)
829
830     vals = [ "NDEF P2P-CR", "WPS P2P-CR", "NDEF P2P-CR-TAG",
831              "WPS P2P-CR-TAG" ]
832     for v in vals:
833         if "FAIL" in dev[0].request("NFC_GET_HANDOVER_SEL " + v):
834             raise Exception("Unexpected NFC_GET_HANDOVER_SEL failure for " + v)
835
836 def get_blacklist(dev):
837     return dev.request("BLACKLIST").splitlines()
838
839 def test_wpas_ctrl_blacklist(dev):
840     """wpa_supplicant ctrl_iface BLACKLIST"""
841     if "OK" not in dev[0].request("BLACKLIST clear"):
842         raise Exception("BLACKLIST clear failed")
843     b = get_blacklist(dev[0])
844     if len(b) != 0:
845         raise Exception("Unexpected blacklist contents: " + str(b))
846     if "OK" not in dev[0].request("BLACKLIST 00:11:22:33:44:55"):
847         raise Exception("BLACKLIST add failed")
848     b = get_blacklist(dev[0])
849     if "00:11:22:33:44:55" not in b:
850         raise Exception("Unexpected blacklist contents: " + str(b))
851     if "OK" not in dev[0].request("BLACKLIST 00:11:22:33:44:56"):
852         raise Exception("BLACKLIST add failed")
853     b = get_blacklist(dev[0])
854     if "00:11:22:33:44:55" not in b or "00:11:22:33:44:56" not in b:
855         raise Exception("Unexpected blacklist contents: " + str(b))
856     if "OK" not in dev[0].request("BLACKLIST 00:11:22:33:44:56"):
857         raise Exception("BLACKLIST add failed")
858     b = get_blacklist(dev[0])
859     if "00:11:22:33:44:55" not in b or "00:11:22:33:44:56" not in b or len(b) != 2:
860         raise Exception("Unexpected blacklist contents: " + str(b))
861
862     if "OK" not in dev[0].request("BLACKLIST clear"):
863         raise Exception("BLACKLIST clear failed")
864     if dev[0].request("BLACKLIST") != "":
865         raise Exception("Unexpected blacklist contents")
866
867 def test_wpas_ctrl_log_level(dev):
868     """wpa_supplicant ctrl_iface LOG_LEVEL"""
869     level = dev[2].request("LOG_LEVEL")
870     if "Current level: MSGDUMP" not in level:
871         raise Exception("Unexpected debug level(1): " + level)
872     if "Timestamp: 1" not in level:
873         raise Exception("Unexpected timestamp(1): " + level)
874
875     if "OK" not in dev[2].request("LOG_LEVEL  MSGDUMP  0"):
876         raise Exception("LOG_LEVEL failed")
877     level = dev[2].request("LOG_LEVEL")
878     if "Current level: MSGDUMP" not in level:
879         raise Exception("Unexpected debug level(2): " + level)
880     if "Timestamp: 0" not in level:
881         raise Exception("Unexpected timestamp(2): " + level)
882
883     if "OK" not in dev[2].request("LOG_LEVEL  MSGDUMP  1"):
884         raise Exception("LOG_LEVEL failed")
885     level = dev[2].request("LOG_LEVEL")
886     if "Current level: MSGDUMP" not in level:
887         raise Exception("Unexpected debug level(3): " + level)
888     if "Timestamp: 1" not in level:
889         raise Exception("Unexpected timestamp(3): " + level)
890
891     if "FAIL" not in dev[2].request("LOG_LEVEL FOO"):
892         raise Exception("Invalid LOG_LEVEL accepted")
893
894     for lev in [ "EXCESSIVE", "MSGDUMP", "DEBUG", "INFO", "WARNING", "ERROR" ]:
895         if "OK" not in dev[2].request("LOG_LEVEL " + lev):
896             raise Exception("LOG_LEVEL failed for " + lev)
897         level = dev[2].request("LOG_LEVEL")
898         if "Current level: " + lev not in level:
899             raise Exception("Unexpected debug level: " + level)
900
901     if "OK" not in dev[2].request("LOG_LEVEL  MSGDUMP  1"):
902         raise Exception("LOG_LEVEL failed")
903     level = dev[2].request("LOG_LEVEL")
904     if "Current level: MSGDUMP" not in level:
905         raise Exception("Unexpected debug level(3): " + level)
906     if "Timestamp: 1" not in level:
907         raise Exception("Unexpected timestamp(3): " + level)
908
909 def test_wpas_ctrl_enable_disable_network(dev, apdev):
910     """wpa_supplicant ctrl_iface ENABLE/DISABLE_NETWORK"""
911     params = { "ssid": "test" }
912     hostapd.add_ap(apdev[0]['ifname'], params)
913
914     id = dev[0].connect("test", key_mgmt="NONE", scan_freq="2412",
915                         only_add_network=True)
916     if "OK" not in dev[0].request("DISABLE_NETWORK " + str(id)):
917         raise Exception("Failed to disable network")
918     if "OK" not in dev[0].request("ENABLE_NETWORK " + str(id) + " no-connect"):
919         raise Exception("Failed to enable network")
920     if "OK" not in dev[0].request("DISABLE_NETWORK all"):
921         raise Exception("Failed to disable networks")
922     if "OK" not in dev[0].request("ENABLE_NETWORK " + str(id)):
923         raise Exception("Failed to enable network")
924     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
925     if ev is None:
926         raise Exception("Association with the AP timed out")
927     if "OK" not in dev[0].request("DISABLE_NETWORK " + str(id)):
928         raise Exception("Failed to disable network")
929     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
930     if ev is None:
931         raise Exception("Disconnection with the AP timed out")
932     time.sleep(0.1)
933
934     if "OK" not in dev[0].request("ENABLE_NETWORK all"):
935         raise Exception("Failed to enable network")
936     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
937     if ev is None:
938         raise Exception("Association with the AP timed out")
939     if "OK" not in dev[0].request("DISABLE_NETWORK all"):
940         raise Exception("Failed to disable network")
941     ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=10)
942     if ev is None:
943         raise Exception("Disconnection with the AP timed out")
944
945 def test_wpas_ctrl_country(dev, apdev):
946     """wpa_supplicant SET/GET country code"""
947     try:
948         # work around issues with possible pending regdom event from the end of
949         # the previous test case
950         time.sleep(0.2)
951         dev[0].dump_monitor()
952
953         if "OK" not in dev[0].request("SET country FI"):
954             raise Exception("Failed to set country code")
955         if dev[0].request("GET country") != "FI":
956             raise Exception("Country code set failed")
957         ev = dev[0].wait_event(["CTRL-EVENT-REGDOM-CHANGE"])
958         if ev is None:
959             raise Exception("regdom change event not seen")
960         if "init=USER type=COUNTRY alpha2=FI" not in ev:
961             raise Exception("Unexpected event contents: " + ev)
962         dev[0].request("SET country 00")
963         if dev[0].request("GET country") != "00":
964             raise Exception("Country code set failed")
965         ev = dev[0].wait_event(["CTRL-EVENT-REGDOM-CHANGE"])
966         if ev is None:
967             raise Exception("regdom change event not seen")
968         if "init=CORE type=WORLD" not in ev:
969             raise Exception("Unexpected event contents: " + ev)
970     finally:
971         subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
972
973 def test_wpas_ctrl_suspend_resume(dev):
974     """wpa_supplicant SUSPEND/RESUME"""
975     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
976     wpas.interface_add("wlan5")
977     if "OK" not in wpas.global_request("SUSPEND"):
978         raise Exception("SUSPEND failed")
979     time.sleep(1)
980     if "OK" not in wpas.global_request("RESUME"):
981         raise Exception("RESUME failed")
982     if "OK" not in wpas.request("SUSPEND"):
983         raise Exception("Per-interface SUSPEND failed")
984     if "OK" not in wpas.request("RESUME"):
985         raise Exception("Per-interface RESUME failed")
986     ev = wpas.wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
987     if ev is None:
988         raise Exception("Scan not completed")
989
990 def test_wpas_ctrl_global(dev):
991     """wpa_supplicant global control interface"""
992     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
993     wpas.interface_add("wlan5")
994
995     if "PONG" not in wpas.global_request("PING"):
996         raise Exception("PING failed")
997     if "wlan5" not in wpas.global_request("INTERFACES"):
998         raise Exception("Interface not found")
999     if "UNKNOWN COMMAND" not in wpas.global_request("FOO"):
1000         raise Exception("Unexpected response to unknown command")
1001     if "PONG" not in wpas.global_request("IFNAME=wlan5 PING"):
1002         raise Exception("Per-interface PING failed")
1003     if "FAIL-NO-IFNAME-MATCH" not in wpas.global_request("IFNAME=notfound PING"):
1004         raise Exception("Unknown interface not reported correctly")
1005     if "FAIL" not in wpas.global_request("SAVE_CONFIG"):
1006         raise Exception("SAVE_CONFIG succeeded unexpectedly")
1007     if "OK" not in wpas.global_request("SET wifi_display 0"):
1008         raise Exception("SET failed")
1009     if "wifi_display=0" not in wpas.global_request("STATUS"):
1010         raise Exception("wifi_display not disabled")
1011     if "OK" not in wpas.global_request("SET wifi_display 1"):
1012         raise Exception("SET failed")
1013     if "wifi_display=1" not in wpas.global_request("STATUS"):
1014         raise Exception("wifi_display not enabled")
1015     if "FAIL" not in wpas.global_request("SET foo 1"):
1016         raise Exception("SET succeeded unexpectedly")
1017
1018     if "p2p_state=IDLE" not in wpas.global_request("STATUS"):
1019         raise Exception("P2P was disabled")
1020     wpas.request("P2P_SET disabled 1")
1021     if "p2p_state=DISABLED" not in wpas.global_request("STATUS"):
1022         raise Exception("P2P was not disabled")
1023     wpas.request("P2P_SET disabled 0")
1024     if "p2p_state=IDLE" not in wpas.global_request("STATUS"):
1025         raise Exception("P2P was not enabled")
1026
1027     # driver_nl80211.c does not support interface list, so do not fail because
1028     # of that
1029     logger.debug(wpas.global_request("INTERFACE_LIST"))
1030
1031     if "FAIL" not in wpas.global_request("INTERFACE_ADD "):
1032         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1033     if "FAIL" not in wpas.global_request("INTERFACE_ADD FOO"):
1034         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1035     if "FAIL" not in wpas.global_request("INTERFACE_ADD FOO     conf"):
1036         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1037     if "FAIL" not in wpas.global_request("INTERFACE_ADD FOO     conf    driver"):
1038         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1039     if "FAIL" not in wpas.global_request("INTERFACE_ADD FOO     conf    driver  ctrliface"):
1040         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1041     if "FAIL" not in wpas.global_request("INTERFACE_ADD FOO     conf    driver  ctrliface       driverparam"):
1042         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1043     if "FAIL" not in wpas.global_request("INTERFACE_ADD FOO     conf    driver  ctrliface       driverparam     bridge"):
1044         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1045     if "FAIL" not in wpas.global_request("INTERFACE_ADD FOO     conf    driver  ctrliface       driverparam     bridge  foo"):
1046         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1047     if "FAIL" not in wpas.global_request("INTERFACE_ADD FOO                                     "):
1048         raise Exception("INTERFACE_ADD succeeded unexpectedly")
1049
1050 def test_wpas_ctrl_roam(dev, apdev):
1051     """wpa_supplicant ctrl_iface ROAM error cases"""
1052     if "FAIL" not in dev[0].request("ROAM 00:11:22:33:44"):
1053         raise Exception("Unexpected success")
1054     if "FAIL" not in dev[0].request("ROAM 00:11:22:33:44:55"):
1055         raise Exception("Unexpected success")
1056     params = { "ssid": "test" }
1057     hostapd.add_ap(apdev[0]['ifname'], params)
1058     id = dev[0].connect("test", key_mgmt="NONE", scan_freq="2412")
1059     if "FAIL" not in dev[0].request("ROAM 00:11:22:33:44:55"):
1060         raise Exception("Unexpected success")
1061
1062 def test_wpas_ctrl_ipaddr(dev, apdev):
1063     """wpa_supplicant IP address in STATUS"""
1064     try:
1065         subprocess.call(['ip', 'addr', 'add', '10.174.65.207/32', 'dev',
1066                          dev[0].ifname])
1067         ipaddr = dev[0].get_status_field('ip_address')
1068         if ipaddr != '10.174.65.207':
1069             raise Exception("IP address not in STATUS output")
1070     finally:
1071         subprocess.call(['ip', 'addr', 'del', '10.174.65.207/32', 'dev',
1072                          dev[0].ifname])
1073
1074 def test_wpas_ctrl_neighbor_rep_req(dev, apdev):
1075     """wpa_supplicant ctrl_iface NEIGHBOR_REP_REQUEST"""
1076     params = { "ssid": "test" }
1077     hostapd.add_ap(apdev[0]['ifname'], params)
1078     dev[0].connect("test", key_mgmt="NONE", scan_freq="2412")
1079     if "FAIL" not in dev[0].request("NEIGHBOR_REP_REQUEST"):
1080         raise Exception("Request succeeded unexpectedly")
1081     if "FAIL" not in dev[0].request("NEIGHBOR_REP_REQUEST ssid=abcdef"):
1082         raise Exception("Request succeeded unexpectedly")
1083
1084 def test_wpas_ctrl_rsp(dev, apdev):
1085     """wpa_supplicant ctrl_iface CTRL-RSP-"""
1086     if "FAIL" not in dev[0].request("CTRL-RSP-"):
1087         raise Exception("Request succeeded unexpectedly")
1088     if "FAIL" not in dev[0].request("CTRL-RSP-foo-"):
1089         raise Exception("Request succeeded unexpectedly")
1090     if "FAIL" not in dev[0].request("CTRL-RSP-foo-1234567"):
1091         raise Exception("Request succeeded unexpectedly")
1092     if "FAIL" not in dev[0].request("CTRL-RSP-foo-1234567:"):
1093         raise Exception("Request succeeded unexpectedly")
1094     id = dev[0].add_network()
1095     if "FAIL" not in dev[0].request("CTRL-RSP-foo-%d:" % id):
1096         raise Exception("Request succeeded unexpectedly")
1097     for req in [ "IDENTITY", "PASSWORD", "NEW_PASSWORD", "PIN", "OTP",
1098                  "PASSPHRASE", "SIM" ]:
1099         if "OK" not in dev[0].request("CTRL-RSP-%s-%d:" % (req, id)):
1100             raise Exception("Request failed unexpectedly")
1101         if "OK" not in dev[0].request("CTRL-RSP-%s-%d:" % (req, id)):
1102             raise Exception("Request failed unexpectedly")