From af9d8c758d9e8a5dab83e11822263fbd4c54eadd Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Sun, 23 Feb 2020 17:38:23 +0200 Subject: [PATCH] tests: Use proper temp files for dynamically created config Replace the hardcoded /tmp filenames for generated ACL and BSS configuration files with proper temporary files from tempfile.mkstemp() to avoid conflicts with existing files or with parallel uses. Remove ACL files from the local directory at the end of each test case. BSS files are currently left behind, but can be cleaned up separately if needed for non-VM testing (VM testing has those on ramdrive so they get dropped automatically at the end) and for remote devices. Signed-off-by: Jouni Malinen --- tests/hwsim/hostapd.py | 96 +++++++++++++++++------------------ tests/hwsim/test_ap_ft.py | 14 +++++ tests/hwsim/test_ap_params.py | 6 +++ tests/hwsim/test_ap_vlan.py | 16 ++++++ tests/hwsim/test_hapd_ctrl.py | 7 +++ 5 files changed, 91 insertions(+), 48 deletions(-) diff --git a/tests/hwsim/hostapd.py b/tests/hwsim/hostapd.py index d179fd045..62b3302be 100644 --- a/tests/hwsim/hostapd.py +++ b/tests/hwsim/hostapd.py @@ -10,6 +10,7 @@ import time import logging import binascii import struct +import tempfile import wpaspy import remotehost import utils @@ -764,33 +765,33 @@ def send_file(apdev, src, dst): return hapd_global.send_file(src, dst) def acl_file(dev, apdev, conf): - filename = os.path.join("/tmp", conf) + fd, filename = tempfile.mkstemp(dir='/tmp', prefix=conf + '-') + f = os.fdopen(fd, 'w') if conf == 'hostapd.macaddr': - with open(filename, 'w') as f: - mac0 = dev[0].get_status_field("address") - f.write(mac0 + '\n') - f.write("02:00:00:00:00:12\n") - f.write("02:00:00:00:00:34\n") - f.write("-02:00:00:00:00:12\n") - f.write("-02:00:00:00:00:34\n") - f.write("01:01:01:01:01:01\n") - f.write("03:01:01:01:01:03\n") + mac0 = dev[0].get_status_field("address") + f.write(mac0 + '\n') + f.write("02:00:00:00:00:12\n") + f.write("02:00:00:00:00:34\n") + f.write("-02:00:00:00:00:12\n") + f.write("-02:00:00:00:00:34\n") + f.write("01:01:01:01:01:01\n") + f.write("03:01:01:01:01:03\n") elif conf == 'hostapd.accept': - with open(filename, 'w') as f: - mac0 = dev[0].get_status_field("address") - mac1 = dev[1].get_status_field("address") - f.write(mac0 + " 1\n") - f.write(mac1 + " 2\n") + mac0 = dev[0].get_status_field("address") + mac1 = dev[1].get_status_field("address") + f.write(mac0 + " 1\n") + f.write(mac1 + " 2\n") elif conf == 'hostapd.accept2': - with open(filename, 'w') as f: - mac0 = dev[0].get_status_field("address") - mac1 = dev[1].get_status_field("address") - mac2 = dev[2].get_status_field("address") - f.write(mac0 + " 1\n") - f.write(mac1 + " 2\n") - f.write(mac2 + " 3\n") + mac0 = dev[0].get_status_field("address") + mac1 = dev[1].get_status_field("address") + mac2 = dev[2].get_status_field("address") + f.write(mac0 + " 1\n") + f.write(mac1 + " 2\n") + f.write(mac2 + " 3\n") else: + f.close() + os.unlink(filename) return conf return filename @@ -803,34 +804,33 @@ def bssid_inc(apdev, inc=1): return bssid def cfg_file(apdev, conf, ifname=None): - # put cfg file in /tmp directory - fname = os.path.join("/tmp", conf) - match = re.search(r'^bss-.+', conf) if match: - with open(fname, 'w') as f: - idx = ''.join(filter(str.isdigit, conf.split('-')[-1])) - if ifname is None: - ifname = apdev['ifname'] - if idx != '1': - ifname = ifname + '-' + idx + # put cfg file in /tmp directory + fd, fname = tempfile.mkstemp(dir='/tmp', prefix=conf + '-') + f = os.fdopen(fd, 'w') + idx = ''.join(filter(str.isdigit, conf.split('-')[-1])) + if ifname is None: + ifname = apdev['ifname'] + if idx != '1': + ifname = ifname + '-' + idx - f.write("driver=nl80211\n") - f.write("ctrl_interface=/var/run/hostapd\n") - f.write("hw_mode=g\n") - f.write("channel=1\n") - f.write("ieee80211n=1\n") - if conf.startswith('bss-ht40-'): - f.write("ht_capab=[HT40+]\n") - f.write("interface=%s\n" % ifname) + f.write("driver=nl80211\n") + f.write("ctrl_interface=/var/run/hostapd\n") + f.write("hw_mode=g\n") + f.write("channel=1\n") + f.write("ieee80211n=1\n") + if conf.startswith('bss-ht40-'): + f.write("ht_capab=[HT40+]\n") + f.write("interface=%s\n" % ifname) - f.write("ssid=bss-%s\n" % idx) - if conf == 'bss-2-dup.conf': - bssid = apdev['bssid'] - else: - bssid = bssid_inc(apdev, int(idx) - 1) - f.write("bssid=%s\n" % bssid) - else: - return conf + f.write("ssid=bss-%s\n" % idx) + if conf == 'bss-2-dup.conf': + bssid = apdev['bssid'] + else: + bssid = bssid_inc(apdev, int(idx) - 1) + f.write("bssid=%s\n" % bssid) - return fname + return fname + + return conf diff --git a/tests/hwsim/test_ap_ft.py b/tests/hwsim/test_ap_ft.py index d5721f415..e28ddd57a 100644 --- a/tests/hwsim/test_ap_ft.py +++ b/tests/hwsim/test_ap_ft.py @@ -353,6 +353,8 @@ def test_ap_ft_vlan(dev, apdev): run_roams(dev[0], apdev, hapd0, hapd1, ssid, passphrase, conndev="brvlan1") if "[WPA2-FT/PSK-CCMP]" not in dev[0].request("SCAN_RESULTS"): raise Exception("Scan results missing RSN element info") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_ft_vlan_disconnected(dev, apdev): """WPA2-PSK-FT AP with VLAN and local key generation""" @@ -377,6 +379,8 @@ def test_ap_ft_vlan_disconnected(dev, apdev): run_roams(dev[0], apdev, hapd0, hapd1, ssid, passphrase, conndev="brvlan1") if "[WPA2-FT/PSK-CCMP]" not in dev[0].request("SCAN_RESULTS"): raise Exception("Scan results missing RSN element info") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_ft_vlan_2(dev, apdev): """WPA2-PSK-FT AP with VLAN and dest-AP does not have VLAN info locally""" @@ -398,6 +402,8 @@ def test_ap_ft_vlan_2(dev, apdev): force_initial_conn_to_first_ap=True) if "[WPA2-FT/PSK-CCMP]" not in dev[0].request("SCAN_RESULTS"): raise Exception("Scan results missing RSN element info") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_ft_many(dev, apdev): """WPA2-PSK-FT AP multiple times""" @@ -431,6 +437,8 @@ def test_ap_ft_many_vlan(dev, apdev): run_roams(dev[0], apdev, hapd0, hapd1, ssid, passphrase, roams=50, conndev="brvlan1") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_ft_mixed(dev, apdev): """WPA2-PSK-FT mixed-mode AP""" @@ -735,6 +743,8 @@ def test_ap_ft_vlan_over_ds(dev, apdev): conndev="brvlan1") check_mib(dev[0], [("dot11RSNAAuthenticationSuiteRequested", "00-0f-ac-4"), ("dot11RSNAAuthenticationSuiteSelected", "00-0f-ac-4")]) + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_ft_over_ds_many(dev, apdev): """WPA2-PSK-FT AP over DS multiple times""" @@ -768,6 +778,8 @@ def test_ap_ft_vlan_over_ds_many(dev, apdev): run_roams(dev[0], apdev, hapd0, hapd1, ssid, passphrase, over_ds=True, roams=50, conndev="brvlan1") + if filename.startswith('/tmp/'): + os.unlink(filename) @remote_compatible def test_ap_ft_over_ds_unknown_target(dev, apdev): @@ -978,6 +990,8 @@ def test_ap_ft_over_ds_pull_vlan(dev, apdev): run_roams(dev[0], apdev, hapd0, hapd1, ssid, passphrase, over_ds=True, conndev="brvlan1") + if filename.startswith('/tmp/'): + os.unlink(filename) def start_ft_sae(dev, apdev, wpa_ptk_rekey=None, sae_pwe=None): if "SAE" not in dev.get_capability("auth_alg"): diff --git a/tests/hwsim/test_ap_params.py b/tests/hwsim/test_ap_params.py index 84debc13f..aa0b28128 100644 --- a/tests/hwsim/test_ap_params.py +++ b/tests/hwsim/test_ap_params.py @@ -158,6 +158,8 @@ def test_ap_acl_accept(dev, apdev): ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1) if ev is not None: raise Exception("Unexpected association") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_acl_deny(dev, apdev): """MAC ACL deny list""" @@ -175,6 +177,8 @@ def test_ap_acl_deny(dev, apdev): ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1) if ev is not None: raise Exception("Unexpected association") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_acl_mgmt(dev, apdev): """MAC ACL accept/deny management""" @@ -247,6 +251,8 @@ def test_ap_acl_mgmt(dev, apdev): hapd.request("DENY_ACL ADD_MAC " + dev[0].own_addr()) dev[0].wait_disconnected() dev[0].request("DISCONNECT") + if filename.startswith('/tmp/'): + os.unlink(filename) @remote_compatible def test_ap_wds_sta(dev, apdev): diff --git a/tests/hwsim/test_ap_vlan.py b/tests/hwsim/test_ap_vlan.py index 7fe9107f4..29f8f5322 100644 --- a/tests/hwsim/test_ap_vlan.py +++ b/tests/hwsim/test_ap_vlan.py @@ -39,6 +39,8 @@ def test_ap_vlan_open(dev, apdev): hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1") hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2") hwsim_utils.test_connectivity(dev[2], hapd) + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_vlan_file_open(dev, apdev): """AP VLAN with open network and vlan_file mapping""" @@ -56,6 +58,8 @@ def test_ap_vlan_file_open(dev, apdev): hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1") hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2") hwsim_utils.test_connectivity(dev[2], hapd) + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_vlan_file_open2(dev, apdev): """AP VLAN with open network and vlan_file mapping (2)""" @@ -73,6 +77,8 @@ def test_ap_vlan_file_open2(dev, apdev): hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1") hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2") hwsim_utils.test_connectivity_iface(dev[2], hapd, "hwsimbr3") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_vlan_file_parsing(dev, apdev, params): """hostapd vlan_file/mac_file parsing""" @@ -132,6 +138,8 @@ def test_ap_vlan_wpa2(dev, apdev): hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1") hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2") hwsim_utils.test_connectivity(dev[2], hapd) + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_vlan_wpa2_radius(dev, apdev): """AP VLAN with WPA2-Enterprise and RADIUS attributes""" @@ -201,6 +209,8 @@ def test_ap_vlan_wpa2_radius_local(dev, apdev): hwsim_utils.test_connectivity_iface(dev[0], hapd, "test_br_vlan1") hwsim_utils.test_connectivity_iface(dev[1], hapd, "test_br_vlan2") hwsim_utils.test_connectivity(dev[2], hapd) + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_vlan_wpa2_radius_id_change(dev, apdev): """AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID""" @@ -364,6 +374,8 @@ def test_ap_vlan_tagged(dev, apdev): hwsim_utils.test_connectivity_iface(dev[0], hapd, "brlo.1") hwsim_utils.test_connectivity_iface(dev[1], hapd, "brlo.2") hwsim_utils.test_connectivity(dev[2], hapd) + if filename.startswith('/tmp/'): + os.unlink(filename) def ap_vlan_iface_cleanup_multibss_cleanup(): subprocess.call(['ifconfig', 'dummy0', 'down'], @@ -632,6 +644,8 @@ def test_ap_vlan_without_station(dev, apdev, p): dev[0].request("DISCONNECT") dev[0].wait_disconnected() + if filename.startswith('/tmp/'): + os.unlink(filename) finally: subprocess.call(['ip', 'link', 'set', 'dev', 'brvlan1', 'down']) @@ -741,6 +755,8 @@ def test_ap_vlan_reconnect(dev, apdev): dev[0].connect("test-vlan", psk="12345678", scan_freq="2412") hapd.wait_sta() hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_ap_vlan_psk(dev, apdev, params): """AP VLAN based on PSK/passphrase""" diff --git a/tests/hwsim/test_hapd_ctrl.py b/tests/hwsim/test_hapd_ctrl.py index c61f4ef3d..a83b570ef 100644 --- a/tests/hwsim/test_hapd_ctrl.py +++ b/tests/hwsim/test_hapd_ctrl.py @@ -6,6 +6,7 @@ import logging logger = logging.getLogger() +import os from remotehost import remote_compatible import hostapd import hwsim_utils @@ -295,6 +296,8 @@ def test_hapd_ctrl_set_deny_mac_file(dev, apdev): ev = dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"], 1) if ev is not None: raise Exception("Unexpected disconnection") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_hapd_ctrl_set_accept_mac_file(dev, apdev): """hostapd and SET accept_mac_file ctrl_iface command""" @@ -312,6 +315,8 @@ def test_hapd_ctrl_set_accept_mac_file(dev, apdev): ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], 1) if ev is not None: raise Exception("Unexpected disconnection") + if filename.startswith('/tmp/'): + os.unlink(filename) def test_hapd_ctrl_set_accept_mac_file_vlan(dev, apdev): """hostapd and SET accept_mac_file ctrl_iface command (VLAN ID)""" @@ -327,6 +332,8 @@ def test_hapd_ctrl_set_accept_mac_file_vlan(dev, apdev): raise Exception("Unexpected SET failure") dev[1].wait_disconnected(timeout=15) dev[0].wait_disconnected(timeout=15) + if filename.startswith('/tmp/'): + os.unlink(filename) @remote_compatible def test_hapd_ctrl_set_error_cases(dev, apdev):