1# hostapd configuration tests
2# Copyright (c) 2014-2016, Jouni Malinen <j@w1.fi>
3#
4# This software may be distributed under the terms of the BSD license.
5# See README for more details.
6
7import os
8import signal
9import time
10import logging
11logger = logging.getLogger(__name__)
12import subprocess
13
14from remotehost import remote_compatible
15import hostapd
16from utils import alloc_fail, fail_test
17
18@remote_compatible
19def test_ap_config_errors(dev, apdev):
20    """Various hostapd configuration errors"""
21
22    # IEEE 802.11d without country code
23    params = {"ssid": "foo", "ieee80211d": "1"}
24    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
25    if "FAIL" not in hapd.request("ENABLE"):
26        raise Exception("Unexpected ENABLE success (ieee80211d without country_code)")
27    hostapd.remove_bss(apdev[0])
28
29    # IEEE 802.11h without IEEE 802.11d
30    params = {"ssid": "foo", "ieee80211h": "1"}
31    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
32    if "FAIL" not in hapd.request("ENABLE"):
33        raise Exception("Unexpected ENABLE success (ieee80211h without ieee80211d")
34    hostapd.remove_bss(apdev[0])
35
36    # Power Constraint without IEEE 802.11d
37    params = {"ssid": "foo", "local_pwr_constraint": "1"}
38    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
39    if "FAIL" not in hapd.request("ENABLE"):
40        raise Exception("Unexpected ENABLE success (local_pwr_constraint without ieee80211d)")
41    hostapd.remove_bss(apdev[0])
42
43    # Spectrum management without Power Constraint
44    params = {"ssid": "foo", "spectrum_mgmt_required": "1"}
45    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
46    if "FAIL" not in hapd.request("ENABLE"):
47        raise Exception("Unexpected ENABLE success (spectrum_mgmt_required without local_pwr_constraint)")
48    hostapd.remove_bss(apdev[0])
49
50    # IEEE 802.1X without authentication server
51    params = {"ssid": "foo", "ieee8021x": "1"}
52    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
53    if "FAIL" not in hapd.request("ENABLE"):
54        raise Exception("Unexpected ENABLE success (ieee8021x)")
55    hostapd.remove_bss(apdev[0])
56
57    # RADIUS-PSK without macaddr_acl=2
58    params = hostapd.wpa2_params(ssid="foo", passphrase="12345678")
59    params["wpa_psk_radius"] = "1"
60    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
61    if "FAIL" not in hapd.request("ENABLE"):
62        raise Exception("Unexpected ENABLE success (wpa_psk_radius)")
63    hostapd.remove_bss(apdev[0])
64
65    # FT without NAS-Identifier
66    params = {"wpa": "2",
67              "wpa_key_mgmt": "FT-PSK",
68              "rsn_pairwise": "CCMP",
69              "wpa_passphrase": "12345678"}
70    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
71    if "FAIL" not in hapd.request("ENABLE"):
72        raise Exception("Unexpected ENABLE success (FT without nas_identifier)")
73    hostapd.remove_bss(apdev[0])
74
75    # Hotspot 2.0 without WPA2/CCMP
76    params = hostapd.wpa2_params(ssid="foo")
77    params['wpa_key_mgmt'] = "WPA-EAP"
78    params['ieee8021x'] = "1"
79    params['auth_server_addr'] = "127.0.0.1"
80    params['auth_server_port'] = "1812"
81    params['auth_server_shared_secret'] = "radius"
82    params['interworking'] = "1"
83    params['hs20'] = "1"
84    params['wpa'] = "1"
85    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
86    if "FAIL" not in hapd.request("ENABLE"):
87        raise Exception("Unexpected ENABLE success (HS 2.0 without WPA2/CCMP)")
88    hostapd.remove_bss(apdev[0])
89
90def test_ap_config_reload(dev, apdev, params):
91    """hostapd configuration reload"""
92    hapd = hostapd.add_ap(apdev[0], {"ssid": "foo"})
93    hapd.set("ssid", "foobar")
94    with open(os.path.join(params['logdir'], 'hostapd-test.pid'), "r") as f:
95        pid = int(f.read())
96    os.kill(pid, signal.SIGHUP)
97    time.sleep(0.1)
98    dev[0].connect("foobar", key_mgmt="NONE", scan_freq="2412")
99    hapd.set("ssid", "foo")
100    os.kill(pid, signal.SIGHUP)
101    dev[0].wait_disconnected()
102    dev[0].request("DISCONNECT")
103
104def test_ap_config_reload_file(dev, apdev, params):
105    """hostapd configuration reload from file"""
106    hapd = hostapd.add_iface(apdev[0], "bss-1.conf")
107    hapd.enable()
108    hapd.set("ssid", "foobar")
109    with open(os.path.join(params['logdir'], 'hostapd-test.pid'), "r") as f:
110        pid = int(f.read())
111    os.kill(pid, signal.SIGHUP)
112    time.sleep(0.1)
113    dev[0].connect("foobar", key_mgmt="NONE", scan_freq="2412")
114    hapd.set("ssid", "foo")
115    os.kill(pid, signal.SIGHUP)
116    dev[0].wait_disconnected()
117    dev[0].request("DISCONNECT")
118
119def test_ap_config_reload_file_while_disabled(dev, apdev, params):
120    """hostapd configuration reload from file when disabled"""
121    hapd = hostapd.add_iface(apdev[0], "bss-1.conf")
122    hapd.enable()
123    ev = hapd.wait_event(["AP-ENABLED"], timeout=3)
124    if ev is None:
125        raise Exception("AP-ENABLED event not reported")
126    hapd.set("ssid", "foobar")
127    with open(os.path.join(params['logdir'], 'hostapd-test.pid'), "r") as f:
128        pid = int(f.read())
129    hapd.disable()
130    ev = hapd.wait_event(["AP-DISABLED"], timeout=3)
131    if ev is None:
132        raise Exception("AP-DISABLED event not reported")
133    hapd.dump_monitor()
134    os.kill(pid, signal.SIGHUP)
135    time.sleep(0.1)
136    hapd.enable()
137    dev[0].connect("foobar", key_mgmt="NONE", scan_freq="2412")
138
139def write_hostapd_config(conffile, ifname, ssid, ht=True, bss2=False):
140    with open(conffile, "w") as f:
141        f.write("driver=nl80211\n")
142        f.write("hw_mode=g\n")
143        f.write("channel=1\n")
144        if ht:
145            f.write("ieee80211n=1\n")
146        f.write("interface=" + ifname + "\n")
147        f.write("ssid=" + ssid + "\n")
148        if bss2:
149            f.write("bss=" + ifname + "_2\n")
150            f.write("ssid=" + ssid + "-2\n")
151
152def test_ap_config_reload_on_sighup(dev, apdev, params):
153    """hostapd configuration reload modification from file on SIGHUP"""
154    run_ap_config_reload_on_sighup(dev, apdev, params)
155
156def test_ap_config_reload_on_sighup_no_ht(dev, apdev, params):
157    """hostapd configuration reload modification from file on SIGHUP (no HT)"""
158    run_ap_config_reload_on_sighup(dev, apdev, params, ht=False)
159
160def run_ap_config_reload_on_sighup(dev, apdev, params, ht=True):
161    name = "ap_config_reload_on_sighup"
162    if not ht:
163        name += "_no_ht"
164    pidfile = params['prefix'] + ".hostapd.pid"
165    logfile = params['prefix'] + ".hostapd.log"
166    conffile = params['prefix'] + ".hostapd.conf"
167    prg = os.path.join(params['logdir'], 'alt-hostapd/hostapd/hostapd')
168    if not os.path.exists(prg):
169        prg = '../../hostapd/hostapd'
170    write_hostapd_config(conffile, apdev[0]['ifname'], "test-1", ht=ht)
171    cmd = [prg, '-B', '-dddt', '-P', pidfile, '-f', logfile, conffile]
172    res = subprocess.check_call(cmd)
173    if res != 0:
174        raise Exception("Could not start hostapd: %s" % str(res))
175
176    dev[0].connect("test-1", key_mgmt="NONE", scan_freq="2412")
177    dev[0].request("REMOVE_NETWORK all")
178    dev[0].wait_disconnected()
179
180    write_hostapd_config(conffile, apdev[0]['ifname'], "test-2", ht=ht)
181    with open(pidfile, "r") as f:
182        pid = int(f.read())
183    os.kill(pid, signal.SIGHUP)
184
185    time.sleep(0.1)
186    dev[0].flush_scan_cache()
187
188    dev[0].connect("test-2", key_mgmt="NONE", scan_freq="2412")
189    bss = dev[0].get_bss(apdev[0]['bssid'])
190    dev[0].request("REMOVE_NETWORK all")
191    dev[0].wait_disconnected()
192
193    os.kill(pid, signal.SIGTERM)
194    removed = False
195    for i in range(20):
196        time.sleep(0.1)
197        if not os.path.exists(pidfile):
198            removed = True
199            break
200    if not removed:
201        raise Exception("hostapd PID file not removed on SIGTERM")
202
203    if ht and "dd180050f202" not in bss['ie']:
204            raise Exception("Missing WMM IE after reload")
205    if not ht and "dd180050f202" in bss['ie']:
206            raise Exception("Unexpected WMM IE after reload")
207
208def test_ap_config_reload_on_sighup_bss_changes(dev, apdev, params):
209    """hostapd configuration reload modification from file on SIGHUP with bss remove/add"""
210    pidfile = params['prefix'] + ".hostapd.pid"
211    logfile = params['prefix'] + ".hostapd-log"
212    conffile = params['prefix'] + ".hostapd.conf"
213    prg = os.path.join(params['logdir'], 'alt-hostapd/hostapd/hostapd')
214    if not os.path.exists(prg):
215        prg = '../../hostapd/hostapd'
216    write_hostapd_config(conffile, apdev[0]['ifname'], "test", bss2=True)
217    cmd = [prg, '-B', '-dddt', '-P', pidfile, '-f', logfile, conffile]
218    res = subprocess.check_call(cmd)
219    if res != 0:
220        raise Exception("Could not start hostapd: %s" % str(res))
221
222    dev[0].connect("test", key_mgmt="NONE", scan_freq="2412",
223                   wait_connect=False)
224    dev[1].connect("test-2", key_mgmt="NONE", scan_freq="2412")
225    dev[0].wait_connected()
226    dev[0].request("REMOVE_NETWORK all")
227    dev[1].request("REMOVE_NETWORK all")
228    dev[0].wait_disconnected()
229    dev[1].wait_disconnected()
230    dev[0].dump_monitor()
231    dev[1].dump_monitor()
232
233    write_hostapd_config(conffile, apdev[0]['ifname'], "test-a", bss2=False)
234    with open(pidfile, "r") as f:
235        pid = int(f.read())
236    os.kill(pid, signal.SIGHUP)
237
238    time.sleep(0.5)
239    dev[0].flush_scan_cache()
240
241    dev[0].connect("test-a", key_mgmt="NONE", scan_freq="2412")
242    dev[0].request("REMOVE_NETWORK all")
243    dev[0].wait_disconnected()
244    dev[0].dump_monitor()
245
246    write_hostapd_config(conffile, apdev[0]['ifname'], "test-b", bss2=True)
247    os.kill(pid, signal.SIGHUP)
248
249    time.sleep(0.5)
250    dev[0].flush_scan_cache()
251    dev[1].flush_scan_cache()
252
253    dev[0].connect("test-b", key_mgmt="NONE", scan_freq="2412",
254                   wait_connect=False)
255    dev[1].connect("test-b-2", key_mgmt="NONE", scan_freq="2412")
256    dev[0].wait_connected()
257    dev[0].request("REMOVE_NETWORK all")
258    dev[1].request("REMOVE_NETWORK all")
259    dev[0].wait_disconnected()
260    dev[1].wait_disconnected()
261    dev[0].dump_monitor()
262    dev[1].dump_monitor()
263
264    os.kill(pid, signal.SIGTERM)
265
266def test_ap_config_reload_before_enable(dev, apdev, params):
267    """hostapd configuration reload before enable"""
268    hapd = hostapd.add_iface(apdev[0], "bss-1.conf")
269    with open(os.path.join(params['logdir'], 'hostapd-test.pid'), "r") as f:
270        pid = int(f.read())
271    os.kill(pid, signal.SIGHUP)
272    hapd.ping()
273
274def test_ap_config_sigusr1(dev, apdev, params):
275    """hostapd SIGUSR1"""
276    hapd = hostapd.add_ap(apdev[0], {"ssid": "foobar"})
277    with open(os.path.join(params['logdir'], 'hostapd-test.pid'), "r") as f:
278        pid = int(f.read())
279    os.kill(pid, signal.SIGUSR1)
280    dev[0].connect("foobar", key_mgmt="NONE", scan_freq="2412")
281    os.kill(pid, signal.SIGUSR1)
282
283def test_ap_config_invalid_value(dev, apdev, params):
284    """Ignoring invalid hostapd configuration parameter updates"""
285    hapd = hostapd.add_ap(apdev[0], {"ssid": "test"}, no_enable=True)
286    not_exist = "/tmp/hostapd-test/does-not-exist"
287    tests = [("driver", "foobar"),
288             ("ssid2", "Q"),
289             ("macaddr_acl", "255"),
290             ("accept_mac_file", not_exist),
291             ("deny_mac_file", not_exist),
292             ("eapol_version", "255"),
293             ("eap_user_file", not_exist),
294             ("wep_key_len_broadcast", "-1"),
295             ("wep_key_len_unicast", "-1"),
296             ("wep_rekey_period", "-1"),
297             ("eap_rekey_period", "-1"),
298             ("radius_client_addr", "foo"),
299             ("acs_chan_bias", "-1:0.8"),
300             ("acs_chan_bias", "1"),
301             ("acs_chan_bias", "1:p"),
302             ("acs_chan_bias", "1:-0.8"),
303             ("acs_chan_bias", "1:0.8p"),
304             ("dtim_period", "0"),
305             ("bss_load_update_period", "-1"),
306             ("send_probe_response", "255"),
307             ("beacon_rate", "ht:-1"),
308             ("beacon_rate", "ht:32"),
309             ("beacon_rate", "vht:-1"),
310             ("beacon_rate", "vht:10"),
311             ("beacon_rate", "9"),
312             ("beacon_rate", "10001"),
313             ("vlan_file", not_exist),
314             ("bss", ""),
315             ("bssid", "foo"),
316             ("extra_cred", not_exist),
317             ("anqp_elem", "265"),
318             ("anqp_elem", "265"),
319             ("anqp_elem", "265:1"),
320             ("anqp_elem", "265:1q"),
321             ("fst_priority", ""),
322             ("fils_cache_id", "q"),
323             ("venue_url", "foo"),
324             ("venue_url", "1:" + 255*"a"),
325             ("sae_password", "secret|mac=qq"),
326             ("dpp_controller", "ipaddr=1"),
327             ("dpp_controller", "ipaddr=127.0.0.1 pkhash=q"),
328             ("dpp_controller", "ipaddr=127.0.0.1 pkhash=" + 32*"qq"),
329             ("dpp_controller", "pkhash=" + 32*"aa"),
330             ("check_cert_subject", ""),
331             ("eap_teap_auth", "-1"),
332             ("eap_teap_auth", "100"),
333             ("group_cipher", "foo"),
334             ("group_cipher", "NONE"),
335             ("chan_util_avg_period", "-1"),
336             ("multi_ap_backhaul_ssid", ""),
337             ("multi_ap_backhaul_ssid", '""'),
338             ("multi_ap_backhaul_ssid", "1"),
339             ("multi_ap_backhaul_ssid", '"' + 33*"A" + '"'),
340             ("multi_ap_backhaul_wpa_passphrase", ""),
341             ("multi_ap_backhaul_wpa_passphrase", 64*"q"),
342             ("multi_ap_backhaul_wpa_psk", "q"),
343             ("multi_ap_backhaul_wpa_psk", 63*"aa"),
344             ("hs20_release", "0"),
345             ("hs20_release", "255"),
346             ("dhcp_server", "::::::"),
347             ("dpp_netaccesskey", "q"),
348             ("dpp_csign", "q"),
349             ("owe_transition_bssid", "q"),
350             ("owe_transition_ssid", ""),
351             ("owe_transition_ssid", '""'),
352             ("owe_transition_ssid", '"' + 33*"a" + '"'),
353             ("multi_ap", "-1"),
354             ("multi_ap", "255"),
355             ("unknown-item", "foo")]
356    for field, val in tests:
357        if "FAIL" not in hapd.request("SET %s %s" % (field, val)):
358            raise Exception("Invalid %s accepted" % field)
359    hapd.enable()
360    dev[0].connect("test", key_mgmt="NONE", scan_freq="2412")
361
362def test_ap_config_eap_user_file_parsing(dev, apdev, params):
363    """hostapd eap_user_file parsing"""
364    tmp = params['prefix'] + '.tmp'
365    hapd = hostapd.add_ap(apdev[0], {"ssid": "foobar"})
366
367    for i in range(2):
368        if "OK" not in hapd.request("SET eap_user_file auth_serv/eap_user.conf"):
369            raise Exception("eap_user_file rejected")
370
371    tests = ["#\n\n*\tTLS\nradius_accept_attr=:",
372             "foo\n",
373             "\"foo\n",
374             "\"foo\"\n",
375             "\"foo\" FOOBAR\n",
376             "\"foo\" " + 10*"TLS," + "TLS \"\n",
377             "\"foo\" TLS \nfoo\n",
378             "\"foo\" PEAP hash:foo\n",
379             "\"foo\" PEAP hash:8846f7eaee8fb117ad06bdd830b7586q\n",
380             "\"foo\" PEAP 01020\n",
381             "\"foo\" PEAP 010q\n"
382             '"pwd" PWD ssha1:\n',
383             '"pwd" PWD ssha1:' + 20*'00' + '\n',
384             '"pwd" PWD ssha256:\n',
385             '"pwd" PWD ssha512:\n',
386             '"pwd" PWD ssha1:' + 20*'00' + 'qq\n',
387             '"pwd" PWD ssha1:' + 19*'00' + 'qq00\n',
388             "\"foo\" TLS\nradius_accept_attr=123:x:012\n",
389             "\"foo\" TLS\nradius_accept_attr=123:x:012q\n",
390             "\"foo\" TLS\nradius_accept_attr=123:Q:01\n",
391             "\"foo\" TLS\nradius_accept_attr=123\nfoo\n"]
392    for t in tests:
393        with open(tmp, "w") as f:
394            f.write(t)
395        if "FAIL" not in hapd.request("SET eap_user_file " + tmp):
396            raise Exception("Invalid eap_user_file accepted")
397
398    tests = [("\"foo\" TLS\n", 2, "hostapd_config_read_eap_user"),
399             ("\"foo\" PEAP \"foo\"\n", 3, "hostapd_config_read_eap_user"),
400             ("\"foo\" PEAP hash:8846f7eaee8fb117ad06bdd830b75861\n", 3,
401              "hostapd_config_read_eap_user"),
402             ("\"foo\" PEAP 0102\n", 3, "hostapd_config_read_eap_user"),
403             ("\"foo\" TLS\nradius_accept_attr=123\n", 1,
404              "=hostapd_parse_radius_attr"),
405             ("\"foo\" TLS\nradius_accept_attr=123\n", 1,
406              "wpabuf_alloc;hostapd_parse_radius_attr"),
407             ("\"foo\" TLS\nradius_accept_attr=123:s:foo\n", 2,
408              "hostapd_parse_radius_attr"),
409             ("\"foo\" TLS\nradius_accept_attr=123:x:0102\n", 2,
410              "hostapd_parse_radius_attr"),
411             ("\"foo\" TLS\nradius_accept_attr=123:d:1\n", 2,
412              "hostapd_parse_radius_attr"),
413             ('"pwd" PWD ssha1:046239e0660a59015231082a071c803e9f5848ae42eaccb4c08c97ae397bc879c4b071b9088ee715\n', 1, "hostapd_config_eap_user_salted"),
414             ('"pwd" PWD ssha1:046239e0660a59015231082a071c803e9f5848ae42eaccb4c08c97ae397bc879c4b071b9088ee715\n', 2, "hostapd_config_eap_user_salted"),
415             ("* TLS\n", 1, "hostapd_config_read_eap_user")]
416    for t, count, func in tests:
417        with alloc_fail(hapd, count, func):
418            with open(tmp, "w") as f:
419                f.write(t)
420            if "FAIL" not in hapd.request("SET eap_user_file " + tmp):
421                raise Exception("eap_user_file accepted during OOM")
422
423def test_ap_config_set_oom(dev, apdev):
424    """hostapd configuration parsing OOM"""
425    hapd = hostapd.add_ap(apdev[0], {"ssid": "foobar"})
426
427    tests = [(1, "hostapd_parse_das_client",
428              "SET radius_das_client 192.168.1.123 pw"),
429             (1, "hostapd_parse_chanlist", "SET chanlist 1 6 11-13"),
430             (1, "hostapd_config_bss", "SET bss foo"),
431             (2, "hostapd_config_bss", "SET bss foo"),
432             (3, "hostapd_config_bss", "SET bss foo"),
433             (1, "add_r0kh",
434              "SET r0kh 02:01:02:03:04:05 r0kh-1.example.com 000102030405060708090a0b0c0d0e0f"),
435             (1, "add_r1kh",
436              "SET r1kh 02:01:02:03:04:05 02:11:22:33:44:55 000102030405060708090a0b0c0d0e0f"),
437             (1, "parse_roaming_consortium", "SET roaming_consortium 021122"),
438             (1, "parse_lang_string", "SET venue_name eng:Example venue"),
439             (1, "parse_3gpp_cell_net",
440              "SET anqp_3gpp_cell_net 244,91;310,026;234,56"),
441             (1, "parse_nai_realm", "SET nai_realm 0,example.com;example.net"),
442             (2, "parse_nai_realm", "SET nai_realm 0,example.com;example.net"),
443             (1, "parse_anqp_elem", "SET anqp_elem 265:0000"),
444             (2, "parse_anqp_elem", "SET anqp_elem 266:000000"),
445             (1, "parse_venue_url", "SET venue_url 1:http://example.com/"),
446             (1, "hs20_parse_operator_icon", "SET operator_icon icon"),
447             (2, "hs20_parse_operator_icon", "SET operator_icon icon"),
448             (1, "hs20_parse_conn_capab", "SET hs20_conn_capab 1:0:2"),
449             (1, "hs20_parse_wan_metrics",
450              "SET hs20_wan_metrics 01:8000:1000:80:240:3000"),
451             (1, "hs20_parse_icon",
452              "SET hs20_icon 32:32:eng:image/png:icon32:/tmp/icon32.png"),
453             (1, "hs20_parse_osu_server_uri",
454              "SET osu_server_uri https://example.com/osu/"),
455             (1, "hostapd_config_parse_acs_chan_bias",
456              "SET acs_chan_bias 1:0.8 6:0.8 11:0.8"),
457             (2, "hostapd_config_parse_acs_chan_bias",
458              "SET acs_chan_bias 1:0.8 6:0.8 11:0.8"),
459             (1, "parse_wpabuf_hex", "SET vendor_elements 01020304"),
460             (1, "parse_fils_realm", "SET fils_realm example.com"),
461             (1, "parse_sae_password", "SET sae_password secret"),
462             (2, "parse_sae_password", "SET sae_password secret"),
463             (2, "parse_sae_password", "SET sae_password secret|id=pw"),
464             (3, "parse_sae_password", "SET sae_password secret|id=pw"),
465             (1, "hostapd_dpp_controller_parse", "SET dpp_controller ipaddr=127.0.0.1 pkhash=" + 32*"11"),
466             (1, "hostapd_config_fill", "SET check_cert_subject foo"),
467             (1, "hostapd_config_fill", "SET multi_ap_backhaul_wpa_psk " + 64*"00"),
468             (1, "hostapd_parse_intlist;hostapd_config_fill",
469              "SET owe_groups 19"),
470             (1, "hostapd_config_fill",
471              "SET pac_opaque_encr_key 000102030405060708090a0b0c0d0e0f"),
472             (1, "hostapd_config_fill", "SET eap_message hello"),
473             (1, "hostapd_config_fill",
474              "SET wpa_psk 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"),
475             (1, "hostapd_config_fill", "SET time_zone EST5"),
476             (1, "hostapd_config_fill",
477              "SET network_auth_type 02http://www.example.com/redirect/"),
478             (1, "hostapd_config_fill", "SET domain_name example.com"),
479             (1, "hostapd_config_fill", "SET hs20_operating_class 5173"),
480             (1, "hostapd_config_fill", "SET own_ie_override 11223344"),
481             (1, "hostapd_parse_intlist", "SET sae_groups 19 25"),
482             (1, "hostapd_parse_intlist", "SET basic_rates 10 20 55 110"),
483             (1, "hostapd_parse_intlist", "SET supported_rates 10 20 55 110")]
484    if "WEP40" in dev[0].get_capability("group"):
485        tests += [(1, "hostapd_config_read_wep", "SET wep_key0 \"hello\""),
486                  (1, "hostapd_config_read_wep", "SET wep_key0 0102030405")]
487    for count, func, cmd in tests:
488        with alloc_fail(hapd, count, func):
489            if "FAIL" not in hapd.request(cmd):
490                raise Exception("Command accepted during OOM: " + cmd)
491
492    hapd.set("hs20_icon", "32:32:eng:image/png:icon32:/tmp/icon32.png")
493    hapd.set("hs20_conn_capab", "1:0:2")
494    hapd.set("nai_realm", "0,example.com;example.net")
495    hapd.set("venue_name", "eng:Example venue")
496    hapd.set("roaming_consortium", "021122")
497    hapd.set("osu_server_uri", "https://example.com/osu/")
498    hapd.set("vendor_elements", "01020304")
499    hapd.set("vendor_elements", "01020304")
500    hapd.set("vendor_elements", "")
501    hapd.set("lci", "11223344")
502    hapd.set("civic", "11223344")
503    hapd.set("lci", "")
504    hapd.set("civic", "")
505
506    tests = [(1, "hs20_parse_icon",
507              "SET hs20_icon 32:32:eng:image/png:icon32:/tmp/icon32.png"),
508             (1, "parse_roaming_consortium", "SET roaming_consortium 021122"),
509             (2, "parse_nai_realm", "SET nai_realm 0,example.com;example.net"),
510             (1, "parse_lang_string", "SET venue_name eng:Example venue"),
511             (1, "hs20_parse_osu_server_uri",
512              "SET osu_server_uri https://example.com/osu/"),
513             (1, "hs20_parse_osu_nai", "SET osu_nai anonymous@example.com"),
514             (1, "hs20_parse_osu_nai2", "SET osu_nai2 anonymous@example.com"),
515             (1, "hostapd_parse_intlist", "SET osu_method_list 1 0"),
516             (1, "hs20_parse_osu_icon", "SET osu_icon icon32"),
517             (2, "hs20_parse_osu_icon", "SET osu_icon icon32"),
518             (2, "hs20_parse_osu_icon", "SET osu_icon icon32"),
519             (1, "hs20_parse_conn_capab", "SET hs20_conn_capab 1:0:2")]
520    for count, func, cmd in tests:
521        with alloc_fail(hapd, count, func):
522            if "FAIL" not in hapd.request(cmd):
523                raise Exception("Command accepted during OOM (2): " + cmd)
524
525    tests = [(1, "parse_fils_realm", "SET fils_realm example.com")]
526    for count, func, cmd in tests:
527        with fail_test(hapd, count, func):
528            if "FAIL" not in hapd.request(cmd):
529                raise Exception("Command accepted during FAIL_TEST: " + cmd)
530
531def test_ap_config_set_errors(dev, apdev):
532    """hostapd configuration parsing errors"""
533    hapd = hostapd.add_ap(apdev[0], {"ssid": "foobar"})
534    if "WEP40" in dev[0].get_capability("group"):
535        hapd.set("wep_key0", '"hello"')
536        hapd.set("wep_key1", '"hello"')
537        hapd.set("wep_key0", '')
538        hapd.set("wep_key0", '"hello"')
539        if "FAIL" not in hapd.request("SET wep_key1 \"hello\""):
540            raise Exception("SET wep_key1 allowed to override existing key")
541        hapd.set("wep_key1", '')
542        hapd.set("wep_key1", '"hello"')
543
544    hapd.set("auth_server_addr", "127.0.0.1")
545    hapd.set("acct_server_addr", "127.0.0.1")
546
547    hapd.set("fst_group_id", "hello")
548    if "FAIL" not in hapd.request("SET fst_group_id hello2"):
549        raise Exception("Duplicate fst_group_id accepted")
550
551    tests = ["SET eap_reauth_period -1",
552             "SET fst_llt ",
553             "SET auth_server_addr_replace foo",
554             "SET acct_server_addr_replace foo"]
555    for t in tests:
556        if "FAIL" not in hapd.request(t):
557            raise Exception("Invalid command accepted: " + t)
558
559    # Deprecated entries
560    hapd.set("tx_queue_after_beacon_aifs", '2')
561    hapd.set("tx_queue_beacon_aifs", '2')
562    hapd.set("tx_queue_data9_aifs", '2')
563    hapd.set("debug", '1')
564    hapd.set("dump_file", '/tmp/hostapd-test-dump')
565    hapd.set("eap_authenticator", '0')
566    hapd.set("radio_measurements", '0')
567    hapd.set("radio_measurements", '1')
568    hapd.set("peerkey", "0")
569
570    # Various extra coverage (not really errors)
571    hapd.set("logger_syslog_level", '1')
572    hapd.set("logger_syslog", '0')
573    hapd.set("ctrl_interface_group", '4')
574    hapd.set("tls_flags", "[ALLOW-SIGN-RSA-MD5][DISABLE-TIME-CHECKS][DISABLE-TLSv1.0]")
575
576    for i in range(50000):
577        if "OK" not in hapd.request("SET hs20_conn_capab 17:5060:0"):
578            logger.info("hs20_conn_capab limit at %d" % i)
579            break
580    if i < 1000 or i >= 49999:
581        raise Exception("hs20_conn_capab limit not seen")
582