tests: Add helper functions for starting hostapd AP

Signed-hostap: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2013-03-29 20:33:25 +02:00
parent 0165c4be10
commit e259d186cf
2 changed files with 54 additions and 52 deletions

View file

@ -92,3 +92,50 @@ class Hostapd:
def disable(self): def disable(self):
if not "OK" in self.ctrl.request("ENABLE"): if not "OK" in self.ctrl.request("ENABLE"):
raise Exception("Failed to disable hostapd interface " + self.ifname) 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

View file

@ -14,58 +14,13 @@ logger = logging.getLogger(__name__)
import hwsim_utils import hwsim_utils
from hostapd import HostapdGlobal from hostapd import HostapdGlobal
from hostapd import Hostapd from hostapd import Hostapd
import hostapd
ap_ifname = 'wlan2' ap_ifname = 'wlan2'
def start_ap_wpa2_psk(ifname): def start_ap_wpa2_psk(ifname):
logger.info("Starting WPA2-PSK AP " + ifname) params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
hapd_global = HostapdGlobal() hostapd.add_ap(ifname, params)
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()
def connect_sta(sta, ssid, psk=None, proto=None, key_mgmt=None, wep_key0=None): def connect_sta(sta, ssid, psk=None, proto=None, key_mgmt=None, wep_key0=None):
logger.info("Connect STA " + sta.ifname + " to AP") 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): def test_ap_wpa_tdls(dev):
"""WPA-PSK AP and two stations using TDLS""" """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" bssid = "02:00:00:00:02:00"
wlantest_setup() wlantest_setup()
connect_2sta_wpa_psk(dev) connect_2sta_wpa_psk(dev)
@ -296,7 +251,7 @@ def test_ap_wpa_tdls(dev):
def test_ap_wpa_mixed_tdls(dev): def test_ap_wpa_mixed_tdls(dev):
"""WPA+WPA2-PSK AP and two stations using TDLS""" """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" bssid = "02:00:00:00:02:00"
wlantest_setup() wlantest_setup()
connect_2sta_wpa_psk_mixed(dev) connect_2sta_wpa_psk_mixed(dev)
@ -306,7 +261,7 @@ def test_ap_wpa_mixed_tdls(dev):
def test_ap_wep_tdls(dev): def test_ap_wep_tdls(dev):
"""WEP AP and two stations using TDLS""" """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" bssid = "02:00:00:00:02:00"
wlantest_setup() wlantest_setup()
connect_2sta_wep(dev) connect_2sta_wep(dev)
@ -316,7 +271,7 @@ def test_ap_wep_tdls(dev):
def test_ap_open_tdls(dev): def test_ap_open_tdls(dev):
"""Open AP and two stations using TDLS""" """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" bssid = "02:00:00:00:02:00"
wlantest_setup() wlantest_setup()
connect_2sta_open(dev) connect_2sta_open(dev)