From 9489637b42f74236526b0731848bb61be7e47659 Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Mon, 30 Dec 2013 23:08:25 +0200 Subject: [PATCH] tests: Verify dynamic wpa_supplicant interface addition/removal Signed-hostap: Jouni Malinen --- tests/hwsim/run-tests.py | 15 +++++++++++- tests/hwsim/start.sh | 4 +++- tests/hwsim/stop.sh | 14 ++++++----- tests/hwsim/test_sta_dynamic.py | 26 +++++++++++++++++++++ tests/hwsim/vm/vm-run.sh | 2 +- tests/hwsim/wpasupplicant.py | 41 ++++++++++++++++++++++++++++----- 6 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 tests/hwsim/test_sta_dynamic.py diff --git a/tests/hwsim/run-tests.py b/tests/hwsim/run-tests.py index 90c99b3d1..faa380141 100755 --- a/tests/hwsim/run-tests.py +++ b/tests/hwsim/run-tests.py @@ -33,6 +33,13 @@ def reset_devs(dev, apdev): logger.info("Failed to reset device " + d.ifname) print str(e) ok = False + + try: + wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5') + wpas.interface_remove("wlan5") + except Exception, e: + pass + try: hapd = HostapdGlobal() hapd.remove('wlan3-3') @@ -318,6 +325,13 @@ def main(): logger.info("Failed to issue TEST-STOP after {} for {}".format(name, d.ifname)) logger.info(e) result = "FAIL" + try: + wpas = WpaSupplicant("wlan5", "/tmp/wpas-wlan5") + rename_log(args.logdir, 'log5', name, wpas) + if not args.no_reset: + wpas.remove_ifname() + except Exception, e: + pass if args.no_reset: print "Leaving devices in current state" else: @@ -325,7 +339,6 @@ def main(): for i in range(0, 3): rename_log(args.logdir, 'log' + str(i), name, dev[i]) - try: hapd = HostapdGlobal() except Exception, e: diff --git a/tests/hwsim/start.sh b/tests/hwsim/start.sh index 346ea2c08..b27cd7fb4 100755 --- a/tests/hwsim/start.sh +++ b/tests/hwsim/start.sh @@ -58,13 +58,15 @@ else fi $DIR/stop.sh -test -f /proc/modules && sudo modprobe mac80211_hwsim radios=5 +test -f /proc/modules && sudo modprobe mac80211_hwsim radios=6 sudo ifconfig hwsim0 up sudo $WLANTEST -i hwsim0 -n $LOGDIR/hwsim0.pcapng -c -dt -L $LOGDIR/hwsim0 & for i in 0 1 2; do sudo $(printf -- "$VALGRIND_WPAS" $i) $WPAS -g /tmp/wpas-wlan$i -G$GROUP -Dnl80211 -iwlan$i -c $LOGDIR/p2p$i.conf \ -ddKt$TRACE -f $LOGDIR/log$i & done +sudo $(printf -- "$VALGRIND_WPAS" 5) $WPAS -g /tmp/wpas-wlan5 -G$GROUP \ + -ddKt$TRACE -f $LOGDIR/log5 & sudo $VALGRIND_HAPD $HAPD -ddKt$TRACE -g /var/run/hostapd-global -G $GROUP -ddKt -f $LOGDIR/hostapd & sleep 1 diff --git a/tests/hwsim/stop.sh b/tests/hwsim/stop.sh index e9ed0bd97..90b7d57d8 100755 --- a/tests/hwsim/stop.sh +++ b/tests/hwsim/stop.sh @@ -56,14 +56,16 @@ for i in `pidof valgrind.bin`; do fi done -for i in /tmp/wpas-wlan0 /tmp/wpas-wlan1 /tmp/wpas-wlan2 /var/run/hostapd-global /tmp/hlr_auc_gw.sock /tmp/wpa_ctrl_* /tmp/eap_sim_db_*; do - if [ -e $i ]; then +count=0 +for i in /tmp/wpas-wlan0 /tmp/wpas-wlan1 /tmp/wpas-wlan2 /tmp/wpas-wlan5 /var/run/hostapd-global /tmp/hlr_auc_gw.sock /tmp/wpa_ctrl_* /tmp/eap_sim_db_*; do + count=$(($count + 1)) + if [ $count -lt 7 -a -e $i ]; then echo "Waiting for ctrl_iface $i to disappear" sleep 1 - if [ -e $i ]; then - echo "Control interface file $i exists - remove it" - sudo rm $i - fi + fi + if [ -e $i ]; then + echo "Control interface file $i exists - remove it" + sudo rm $i fi done diff --git a/tests/hwsim/test_sta_dynamic.py b/tests/hwsim/test_sta_dynamic.py new file mode 100644 index 000000000..ab9df4448 --- /dev/null +++ b/tests/hwsim/test_sta_dynamic.py @@ -0,0 +1,26 @@ +#!/usr/bin/python +# +# Dynamic wpa_supplicant interface +# Copyright (c) 2013, Jouni Malinen +# +# This software may be distributed under the terms of the BSD license. +# See README for more details. + +import logging +logger = logging.getLogger() +import time + +import hwsim_utils +import hostapd +from wpasupplicant import WpaSupplicant + +def test_sta_dynamic(dev, apdev): + """Dynamically added wpa_supplicant interface""" + params = hostapd.wpa2_params(ssid="sta-dynamic", passphrase="12345678") + hostapd.add_ap(apdev[0]['ifname'], params) + + logger.info("Create a dynamic wpa_supplicant interface and connect") + wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5') + wpas.interface_add("wlan5") + + wpas.connect("sta-dynamic", psk="12345678", scan_freq="2412") diff --git a/tests/hwsim/vm/vm-run.sh b/tests/hwsim/vm/vm-run.sh index a2b9187f1..a4924edac 100755 --- a/tests/hwsim/vm/vm-run.sh +++ b/tests/hwsim/vm/vm-run.sh @@ -105,7 +105,7 @@ kvm \ -fsdev local,security_model=none,id=fsdev-logs,path="$LOGDIR",writeout=immediate \ -device virtio-9p-pci,id=fs-logs,fsdev=fsdev-logs,mount_tag=logshare \ -monitor null -serial stdio -serial file:$LOGDIR/console \ - -append "mac80211_hwsim.channels=$CHANNELS mac80211_hwsim.radios=5 init=$CMD testdir=$TESTDIR console=$KVMOUT root=/dev/root rootflags=trans=virtio,version=9p2000.u ro rootfstype=9p EPATH=$EPATH ARGS=$*" + -append "mac80211_hwsim.channels=$CHANNELS mac80211_hwsim.radios=6 init=$CMD testdir=$TESTDIR console=$KVMOUT root=/dev/root rootflags=trans=virtio,version=9p2000.u ro rootfstype=9p EPATH=$EPATH ARGS=$*" if [ $CODECOV = "yes" ]; then mv $LOGDIR/alt-wpa_supplicant /tmp/logs diff --git a/tests/hwsim/wpasupplicant.py b/tests/hwsim/wpasupplicant.py index cd7af4848..a5926d466 100644 --- a/tests/hwsim/wpasupplicant.py +++ b/tests/hwsim/wpasupplicant.py @@ -17,12 +17,12 @@ logger = logging.getLogger() wpas_ctrl = '/var/run/wpa_supplicant' class WpaSupplicant: - def __init__(self, ifname, global_iface=None): - self.ifname = ifname + def __init__(self, ifname=None, global_iface=None): self.group_ifname = None - self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname)) - self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname)) - self.mon.attach() + if ifname: + self.set_ifname(ifname) + else: + self.ifname = None self.global_iface = global_iface if global_iface: @@ -30,6 +30,34 @@ class WpaSupplicant: self.global_mon = wpaspy.Ctrl(global_iface) self.global_mon.attach() + def set_ifname(self, ifname): + self.ifname = ifname + self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname)) + self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname)) + self.mon.attach() + + def remove_ifname(self): + if self.ifname: + self.mon.detach() + self.mon = None + self.ctrl = None + self.ifname = None + + def interface_add(self, ifname, driver="nl80211"): + try: + groups = subprocess.check_output(["id"]) + group = "admin" if "(admin)" in groups else "adm" + except Exception, e: + group = "admin" + cmd = "INTERFACE_ADD " + ifname + "\t\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group + if "FAIL" in self.global_request(cmd): + raise Exception("Failed to add a dynamic wpa_supplicant interface") + self.set_ifname(ifname) + + def interface_remove(self, ifname): + self.remove_ifname() + self.global_request("INTERFACE_REMOVE " + ifname) + def request(self, cmd): logger.debug(self.ifname + ": CTRL: " + cmd) return self.ctrl.request(cmd) @@ -38,7 +66,8 @@ class WpaSupplicant: if self.global_iface is None: self.request(cmd) else: - logger.debug(self.ifname + ": CTRL: " + cmd) + ifname = self.ifname or self.global_iface + logger.debug(ifname + ": CTRL: " + cmd) return self.global_ctrl.request(cmd) def group_request(self, cmd):