From 6e917c3e254aa533f3e3db250be835b5236faf7c Mon Sep 17 00:00:00 2001
From: Jouni Malinen <j@w1.fi>
Date: Tue, 31 Dec 2013 00:17:02 +0200
Subject: [PATCH] tests: Increase nl80211 test coverage with monitor/connect

Add test cases to use connect command instead of auth+assoc commands and
AP mode operations using the old monitor interface design.

Signed-hostap: Jouni Malinen <j@w1.fi>
---
 tests/hwsim/test_connect_cmd.py       | 35 +++++++++++++++++++++
 tests/hwsim/test_monitor_interface.py | 45 +++++++++++++++++++++++++++
 tests/hwsim/wpasupplicant.py          |  4 ++-
 3 files changed, 83 insertions(+), 1 deletion(-)
 create mode 100644 tests/hwsim/test_connect_cmd.py
 create mode 100644 tests/hwsim/test_monitor_interface.py

diff --git a/tests/hwsim/test_connect_cmd.py b/tests/hwsim/test_connect_cmd.py
new file mode 100644
index 000000000..5a359c054
--- /dev/null
+++ b/tests/hwsim/test_connect_cmd.py
@@ -0,0 +1,35 @@
+#!/usr/bin/python
+#
+# cfg80211 connect command (SME in the driver/firmware)
+# Copyright (c) 2013, Jouni Malinen <j@w1.fi>
+#
+# 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_connect_cmd_open(dev, apdev):
+    """Open connection using cfg80211 connect command"""
+    params = { "ssid": "sta-connect" }
+    hostapd.add_ap(apdev[0]['ifname'], params)
+
+    wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
+    wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
+    wpas.connect("sta-connect", key_mgmt="NONE", scan_freq="2412")
+    wpas.request("DISCONNECT")
+
+def test_connect_cmd_wpa2_psk(dev, apdev):
+    """WPA2-PSK connection using cfg80211 connect command"""
+    params = hostapd.wpa2_params(ssid="sta-connect", passphrase="12345678")
+    hostapd.add_ap(apdev[0]['ifname'], params)
+
+    wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
+    wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
+    wpas.connect("sta-connect", psk="12345678", scan_freq="2412")
+    wpas.request("DISCONNECT")
diff --git a/tests/hwsim/test_monitor_interface.py b/tests/hwsim/test_monitor_interface.py
new file mode 100644
index 000000000..2fce1a128
--- /dev/null
+++ b/tests/hwsim/test_monitor_interface.py
@@ -0,0 +1,45 @@
+#!/usr/bin/python
+#
+# AP mode using the older monitor interface design
+# Copyright (c) 2013, Jouni Malinen <j@w1.fi>
+#
+# 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_monitor_iface_open(dev, apdev):
+    """Open connection using cfg80211 monitor interface on AP"""
+    wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
+    wpas.interface_add("wlan5", drv_params="use_monitor=1")
+    id = wpas.add_network()
+    wpas.set_network(id, "mode", "2")
+    wpas.set_network_quoted(id, "ssid", "monitor-iface")
+    wpas.set_network(id, "key_mgmt", "NONE")
+    wpas.set_network(id, "frequency", "2412")
+    wpas.connect_network(id)
+
+    dev[0].connect("monitor-iface", key_mgmt="NONE", scan_freq="2412")
+
+def test_monitor_iface_wpa2_psk(dev, apdev):
+    """WPA2-PSK connection using cfg80211 monitor interface on AP"""
+    wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
+    wpas.interface_add("wlan5", drv_params="use_monitor=1")
+    id = wpas.add_network()
+    wpas.set_network(id, "mode", "2")
+    wpas.set_network_quoted(id, "ssid", "monitor-iface-wpa2")
+    wpas.set_network(id, "proto", "WPA2")
+    wpas.set_network(id, "key_mgmt", "WPA-PSK")
+    wpas.set_network_quoted(id, "psk", "12345678")
+    wpas.set_network(id, "pairwise", "CCMP")
+    wpas.set_network(id, "group", "CCMP")
+    wpas.set_network(id, "frequency", "2412")
+    wpas.connect_network(id)
+
+    dev[0].connect("monitor-iface-wpa2", psk="12345678", scan_freq="2412")
diff --git a/tests/hwsim/wpasupplicant.py b/tests/hwsim/wpasupplicant.py
index a5926d466..b5e506884 100644
--- a/tests/hwsim/wpasupplicant.py
+++ b/tests/hwsim/wpasupplicant.py
@@ -43,13 +43,15 @@ class WpaSupplicant:
             self.ctrl = None
             self.ifname = None
 
-    def interface_add(self, ifname, driver="nl80211"):
+    def interface_add(self, ifname, driver="nl80211", drv_params=None):
         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 drv_params:
+            cmd = cmd + '\t' + drv_params
         if "FAIL" in self.global_request(cmd):
             raise Exception("Failed to add a dynamic wpa_supplicant interface")
         self.set_ifname(ifname)