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