From e259d186cfdbc26fb8b3257d68ab4d8feeee6dd8 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Fri, 29 Mar 2013 20:33:25 +0200 Subject: [PATCH] tests: Add helper functions for starting hostapd AP Signed-hostap: Jouni Malinen --- tests/hwsim/hostapd.py | 47 +++++++++++++++++++++++++++++ tests/hwsim/test_ap_tdls.py | 59 +++++-------------------------------- 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/tests/hwsim/hostapd.py b/tests/hwsim/hostapd.py index 35f86c393..15a98f39a 100644 --- a/tests/hwsim/hostapd.py +++ b/tests/hwsim/hostapd.py @@ -92,3 +92,50 @@ class Hostapd: def disable(self): if not "OK" in self.ctrl.request("ENABLE"): raise Exception("Failed to disable hostapd interface " + self.ifname) + +def add_ap(ifname, params): + logger.info("Starting AP " + ifname) + hapd_global = HostapdGlobal() + hapd_global.remove(ifname) + hapd_global.add(ifname) + hapd = Hostapd(ifname) + if not hapd.ping(): + raise Exception("Could not ping hostapd") + hapd.set_defaults() + fields = [ "ssid", "wpa_passphrase", "wpa", "wpa_key_mgmt", + "wpa_pairwise", "rsn_pairwise", "wep_key0" ] + for field in fields: + if field in params: + hapd.set(field, params[field]) + hapd.enable() + +def wpa2_params(ssid=None, passphrase=None): + params = { "wpa": "2", + "wpa_key_mgmt": "WPA-PSK", + "rsn_pairwise": "CCMP" } + if ssid: + params["ssid"] = ssid + if passphrase: + params["wpa_passphrase"] = passphrase + return params + +def wpa_params(ssid=None, passphrase=None): + params = { "wpa": "1", + "wpa_key_mgmt": "WPA-PSK", + "wpa_pairwise": "TKIP" } + if ssid: + params["ssid"] = ssid + if passphrase: + params["wpa_passphrase"] = passphrase + return params + +def wpa_mixed_params(ssid=None, passphrase=None): + params = { "wpa": "3", + "wpa_key_mgmt": "WPA-PSK", + "wpa_pairwise": "TKIP", + "rsn_pairwise": "CCMP" } + if ssid: + params["ssid"] = ssid + if passphrase: + params["wpa_passphrase"] = passphrase + return params diff --git a/tests/hwsim/test_ap_tdls.py b/tests/hwsim/test_ap_tdls.py index abc87839b..24f031ea4 100644 --- a/tests/hwsim/test_ap_tdls.py +++ b/tests/hwsim/test_ap_tdls.py @@ -14,58 +14,13 @@ logger = logging.getLogger(__name__) import hwsim_utils from hostapd import HostapdGlobal from hostapd import Hostapd +import hostapd ap_ifname = 'wlan2' def start_ap_wpa2_psk(ifname): - logger.info("Starting WPA2-PSK AP " + ifname) - hapd_global = HostapdGlobal() - hapd_global.add(ifname) - hapd = Hostapd(ifname) - if not hapd.ping(): - raise Exception("Could not ping hostapd") - hapd.set_wpa2_psk("test-wpa2-psk", "12345678") - hapd.enable() - -def start_ap_wpa_psk(ifname): - logger.info("Starting WPA-PSK AP " + ifname) - hapd_global = HostapdGlobal() - hapd_global.add(ifname) - hapd = Hostapd(ifname) - if not hapd.ping(): - raise Exception("Could not ping hostapd") - hapd.set_wpa_psk("test-wpa-psk", "12345678") - hapd.enable() - -def start_ap_wpa_mixed_psk(ifname): - logger.info("Starting WPA+WPA2-PSK AP " + ifname) - hapd_global = HostapdGlobal() - hapd_global.add(ifname) - hapd = Hostapd(ifname) - if not hapd.ping(): - raise Exception("Could not ping hostapd") - hapd.set_wpa_psk_mixed("test-wpa-mixed-psk", "12345678") - hapd.enable() - -def start_ap_wep(ifname): - logger.info("Starting WEP AP " + ifname) - hapd_global = HostapdGlobal() - hapd_global.add(ifname) - hapd = Hostapd(ifname) - if not hapd.ping(): - raise Exception("Could not ping hostapd") - hapd.set_wep("test-wep", '"hello"') - hapd.enable() - -def start_ap_open(ifname): - logger.info("Starting open AP " + ifname) - hapd_global = HostapdGlobal() - hapd_global.add(ifname) - hapd = Hostapd(ifname) - if not hapd.ping(): - raise Exception("Could not ping hostapd") - hapd.set_open("test-open") - hapd.enable() + params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678") + hostapd.add_ap(ifname, params) def connect_sta(sta, ssid, psk=None, proto=None, key_mgmt=None, wep_key0=None): logger.info("Connect STA " + sta.ifname + " to AP") @@ -286,7 +241,7 @@ def test_ap_wpa2_tdls_diff_rsnie(dev): def test_ap_wpa_tdls(dev): """WPA-PSK AP and two stations using TDLS""" - start_ap_wpa_psk(ap_ifname) + hostapd.add_ap(ap_ifname, hostapd.wpa_params(ssid="test-wpa-psk", passphrase="12345678")) bssid = "02:00:00:00:02:00" wlantest_setup() connect_2sta_wpa_psk(dev) @@ -296,7 +251,7 @@ def test_ap_wpa_tdls(dev): def test_ap_wpa_mixed_tdls(dev): """WPA+WPA2-PSK AP and two stations using TDLS""" - start_ap_wpa_mixed_psk(ap_ifname) + hostapd.add_ap(ap_ifname, hostapd.wpa_mixed_params(ssid="test-wpa-mixed-psk", passphrase="12345678")) bssid = "02:00:00:00:02:00" wlantest_setup() connect_2sta_wpa_psk_mixed(dev) @@ -306,7 +261,7 @@ def test_ap_wpa_mixed_tdls(dev): def test_ap_wep_tdls(dev): """WEP AP and two stations using TDLS""" - start_ap_wep(ap_ifname) + hostapd.add_ap(ap_ifname, { "ssid": "test-wep", "wep_key0": '"hello"' }) bssid = "02:00:00:00:02:00" wlantest_setup() connect_2sta_wep(dev) @@ -316,7 +271,7 @@ def test_ap_wep_tdls(dev): def test_ap_open_tdls(dev): """Open AP and two stations using TDLS""" - start_ap_open(ap_ifname) + hostapd.add_ap(ap_ifname, { "ssid": "test-open" }) bssid = "02:00:00:00:02:00" wlantest_setup() connect_2sta_open(dev)