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