]> git.ipfire.org Git - thirdparty/hostap.git/blobdiff - tests/hwsim/wpasupplicant.py
tests: P2P_FIND with freq parameter to scan a single channel
[thirdparty/hostap.git] / tests / hwsim / wpasupplicant.py
index 71ec092606023896e6f966422839f13596790769..9565b9d4f8094f945f0d3b4edb1d2157919c4b74 100644 (file)
@@ -30,6 +30,15 @@ class WpaSupplicant:
             self.global_ctrl = wpaspy.Ctrl(global_iface)
             self.global_mon = wpaspy.Ctrl(global_iface)
             self.global_mon.attach()
+        else:
+            self.global_mon = None
+
+    def close_ctrl(self):
+        if self.global_mon:
+            self.global_mon.detach()
+            self.global_mon = None
+            self.global_ctrl = None
+        self.remove_ifname()
 
     def set_ifname(self, ifname):
         self.ifname = ifname
@@ -44,7 +53,8 @@ class WpaSupplicant:
             self.ctrl = None
             self.ifname = None
 
-    def interface_add(self, ifname, config="", driver="nl80211", drv_params=None):
+    def interface_add(self, ifname, config="", driver="nl80211",
+                      drv_params=None, br_ifname=None):
         try:
             groups = subprocess.check_output(["id"])
             group = "admin" if "(admin)" in groups else "adm"
@@ -53,6 +63,10 @@ class WpaSupplicant:
         cmd = "INTERFACE_ADD " + ifname + "\t" + config + "\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group
         if drv_params:
             cmd = cmd + '\t' + drv_params
+        if br_ifname:
+            if not drv_params:
+                cmd += '\t'
+            cmd += '\t' + br_ifname
         if "FAIL" in self.global_request(cmd):
             raise Exception("Failed to add a dynamic wpa_supplicant interface")
         self.set_ifname(ifname)
@@ -61,9 +75,9 @@ class WpaSupplicant:
         self.remove_ifname()
         self.global_request("INTERFACE_REMOVE " + ifname)
 
-    def request(self, cmd):
+    def request(self, cmd, timeout=10):
         logger.debug(self.ifname + ": CTRL: " + cmd)
-        return self.ctrl.request(cmd)
+        return self.ctrl.request(cmd, timeout=timeout)
 
     def global_request(self, cmd):
         if self.global_iface is None:
@@ -91,8 +105,6 @@ class WpaSupplicant:
         res = self.request("FLUSH")
         if not "OK" in res:
             logger.info("FLUSH to " + self.ifname + " failed: " + res)
-        self.request("WPS_ER_STOP")
-        self.request("SET pmf 0")
         self.request("SET p2p_add_cli_chan 0")
         self.request("SET p2p_no_go_freq ")
         self.request("SET p2p_pref_chan ")
@@ -121,13 +133,13 @@ class WpaSupplicant:
             logger.error(self.ifname + ": Driver scan state did not clear")
             print "Trying to clear cfg80211/mac80211 scan state"
             try:
-                cmd = ["sudo", "ifconfig", self.ifname, "down"]
+                cmd = ["ifconfig", self.ifname, "down"]
                 subprocess.call(cmd)
             except subprocess.CalledProcessError, e:
                 logger.info("ifconfig failed: " + str(e.returncode))
                 logger.info(e.output)
             try:
-                cmd = ["sudo", "ifconfig", self.ifname, "up"]
+                cmd = ["ifconfig", self.ifname, "up"]
                 subprocess.call(cmd)
             except subprocess.CalledProcessError, e:
                 logger.info("ifconfig failed: " + str(e.returncode))
@@ -195,6 +207,12 @@ class WpaSupplicant:
         else:
             self.request("SET auto_interworking 0")
 
+    def interworking_add_network(self, bssid):
+        id = self.request("INTERWORKING_ADD_NETWORK " + bssid)
+        if "FAIL" in id or "OK" in id:
+            raise Exception("INTERWORKING_ADD_NETWORK failed")
+        return int(id)
+
     def add_cred(self):
         id = self.request("ADD_CRED")
         if "FAIL" in id:
@@ -246,7 +264,7 @@ class WpaSupplicant:
 
     def select_network(self, id, freq=None):
         if freq:
-            extra = " freq=" + freq
+            extra = " freq=" + str(freq)
         else:
             extra = ""
         id = self.request("SELECT_NETWORK " + str(id) + extra)
@@ -269,9 +287,7 @@ class WpaSupplicant:
     def connect_network(self, id, timeout=10):
         self.dump_monitor()
         self.select_network(id)
-        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
-        if ev is None:
-            raise Exception("Association with the AP timed out")
+        self.wait_connected(timeout=timeout)
         self.dump_monitor()
 
     def get_status(self, extra=None):
@@ -370,7 +386,8 @@ class WpaSupplicant:
     def p2p_listen(self):
         return self.global_request("P2P_LISTEN")
 
-    def p2p_find(self, social=False, progressive=False, dev_id=None, dev_type=None):
+    def p2p_find(self, social=False, progressive=False, dev_id=None,
+                 dev_type=None, delay=None, freq=None):
         cmd = "P2P_FIND"
         if social:
             cmd = cmd + " type=social"
@@ -380,6 +397,10 @@ class WpaSupplicant:
             cmd = cmd + " dev_id=" + dev_id
         if dev_type:
             cmd = cmd + " dev_type=" + dev_type
+        if delay:
+            cmd = cmd + " delay=" + str(delay)
+        if freq:
+            cmd = cmd + " freq=" + str(freq)
         return self.global_request(cmd)
 
     def p2p_stop_find(self):
@@ -493,7 +514,10 @@ class WpaSupplicant:
         if not self.discover_peer(peer):
             raise Exception("Peer " + peer + " not found")
         self.dump_monitor()
-        cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
+        if pin:
+            cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
+        else:
+            cmd = "P2P_CONNECT " + peer + " " + method + " auth"
         if go_intent:
             cmd = cmd + ' go_intent=' + str(go_intent)
         if freq:
@@ -630,7 +654,7 @@ class WpaSupplicant:
             except:
                 pass
             self.gctrl_mon = None
-        ev = self.wait_event(["P2P-GROUP-REMOVED"], timeout=3)
+        ev = self.wait_global_event(["P2P-GROUP-REMOVED"], timeout=3)
         if ev is None:
             raise Exception("Group removal event timed out")
         if "reason=GO_ENDING_SESSION" not in ev:
@@ -640,7 +664,7 @@ class WpaSupplicant:
         while self.mon.pending():
             ev = self.mon.recv()
             logger.debug(self.ifname + ": " + ev)
-        while self.global_mon.pending():
+        while self.global_mon and self.global_mon.pending():
             ev = self.global_mon.recv()
             logger.debug(self.ifname + "(global): " + ev)
 
@@ -657,7 +681,7 @@ class WpaSupplicant:
             raise Exception("Group could not be removed")
         self.group_ifname = None
 
-    def p2p_start_go(self, persistent=None, freq=None):
+    def p2p_start_go(self, persistent=None, freq=None, no_event_clear=False):
         self.dump_monitor()
         cmd = "P2P_GROUP_ADD"
         if persistent is None:
@@ -672,7 +696,8 @@ class WpaSupplicant:
             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
             if ev is None:
                 raise Exception("GO start up timed out")
-            self.dump_monitor()
+            if not no_event_clear:
+                self.dump_monitor()
             return self.group_form_result(ev)
         raise Exception("P2P_GROUP_ADD failed")
 
@@ -721,26 +746,49 @@ class WpaSupplicant:
             raise Exception("Failed to request TDLS teardown")
         return None
 
-    def add_ts(self, tsid, up):
+    def tspecs(self):
+        """Return (tsid, up) tuples representing current tspecs"""
+        res = self.request("WMM_AC_STATUS")
+        tspecs = re.findall(r"TSID=(\d+) UP=(\d+)", res)
+        tspecs = [tuple(map(int, tspec)) for tspec in tspecs]
+
+        logger.debug("tspecs: " + str(tspecs))
+        return tspecs
+
+    def add_ts(self, tsid, up, direction="downlink", expect_failure=False,
+               extra=None):
         params = {
             "sba": 9000,
             "nominal_msdu_size": 1500,
             "min_phy_rate": 6000000,
             "mean_data_rate": 1500,
         }
-        cmd = "WMM_AC_ADDTS downlink tsid=%d up=%d" % (tsid, up)
+        cmd = "WMM_AC_ADDTS %s tsid=%d up=%d" % (direction, tsid, up)
         for (key, value) in params.iteritems():
             cmd += " %s=%d" % (key, value)
+        if extra:
+            cmd += " " + extra
 
         if self.request(cmd).strip() != "OK":
             raise Exception("ADDTS failed (tsid=%d up=%d)" % (tsid, up))
 
+        if expect_failure:
+            ev = self.wait_event(["TSPEC-REQ-FAILED"], timeout=2)
+            if ev is None:
+                raise Exception("ADDTS failed (time out while waiting failure)")
+            if "tsid=%d" % (tsid) not in ev:
+                raise Exception("ADDTS failed (invalid tsid in TSPEC-REQ-FAILED")
+            return
+
         ev = self.wait_event(["TSPEC-ADDED"], timeout=1)
         if ev is None:
             raise Exception("ADDTS failed (time out)")
         if "tsid=%d" % (tsid) not in ev:
             raise Exception("ADDTS failed (invalid tsid in TSPEC-ADDED)")
 
+        if not (tsid, up) in self.tspecs():
+            raise Exception("ADDTS failed (tsid not in tspec list)")
+
     def del_ts(self, tsid):
         if self.request("WMM_AC_DELTS %d" % (tsid)).strip() != "OK":
             raise Exception("DELTS failed")
@@ -751,6 +799,10 @@ class WpaSupplicant:
         if "tsid=%d" % (tsid) not in ev:
             raise Exception("DELTS failed (invalid tsid in TSPEC-REMOVED)")
 
+        tspecs = [(t, u) for (t, u) in self.tspecs() if t == tsid]
+        if tspecs:
+            raise Exception("DELTS failed (still in tspec list)")
+
     def connect(self, ssid=None, ssid2=None, **kwargs):
         logger.info("Connect STA " + self.ifname + " to AP")
         id = self.add_network()
@@ -764,7 +816,8 @@ class WpaSupplicant:
                    "private_key_passwd", "ca_cert2", "client_cert2",
                    "private_key2", "phase1", "phase2", "domain_suffix_match",
                    "altsubject_match", "subject_match", "pac_file", "dh_file",
-                   "bgscan", "ht_mcs", "id_str", "openssl_ciphers" ]
+                   "bgscan", "ht_mcs", "id_str", "openssl_ciphers",
+                   "domain_match" ]
         for field in quoted:
             if field in kwargs and kwargs[field]:
                 self.set_network_quoted(id, field, kwargs[field])
@@ -777,7 +830,8 @@ class WpaSupplicant:
                        "disable_max_amsdu", "ampdu_factor", "ampdu_density",
                        "disable_ht40", "disable_sgi", "disable_ldpc",
                        "ht40_intolerant", "update_identifier", "mac_addr",
-                       "erp" ]
+                       "erp", "bg_scan_period", "bssid_blacklist",
+                       "bssid_whitelist" ]
         for field in not_quoted:
             if field in kwargs and kwargs[field]:
                 self.set_network(id, field, kwargs[field])
@@ -823,18 +877,18 @@ class WpaSupplicant:
         if ev is None:
             raise Exception("Scan timed out")
 
-    def scan_for_bss(self, bssid, freq=None, force_scan=False):
+    def scan_for_bss(self, bssid, freq=None, force_scan=False, only_new=False):
         if not force_scan and self.get_bss(bssid) is not None:
             return
         for i in range(0, 10):
-            self.scan(freq=freq, type="ONLY")
+            self.scan(freq=freq, type="ONLY", only_new=only_new)
             if self.get_bss(bssid) is not None:
                 return
         raise Exception("Could not find BSS " + bssid + " in scan")
 
-    def flush_scan_cache(self):
+    def flush_scan_cache(self, freq=2417):
         self.request("BSS_FLUSH 0")
-        self.scan(freq=2412, only_new=True)
+        self.scan(freq=freq, only_new=True)
 
     def roam(self, bssid, fail_test=False):
         self.dump_monitor()
@@ -846,9 +900,7 @@ class WpaSupplicant:
                 raise Exception("Unexpected connection")
             self.dump_monitor()
             return
-        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
-        if ev is None:
-            raise Exception("Roaming with the AP timed out")
+        self.wait_connected(timeout=10, error="Roaming with the AP timed out")
         self.dump_monitor()
 
     def roam_over_ds(self, bssid, fail_test=False):
@@ -861,9 +913,7 @@ class WpaSupplicant:
                 raise Exception("Unexpected connection")
             self.dump_monitor()
             return
-        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
-        if ev is None:
-            raise Exception("Roaming with the AP timed out")
+        self.wait_connected(timeout=10, error="Roaming with the AP timed out")
         self.dump_monitor()
 
     def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
@@ -886,9 +936,7 @@ class WpaSupplicant:
             ev = self.wait_event(["WPS-FAIL"], timeout=15)
         if ev is None:
             raise Exception("WPS timed out")
-        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
-        if ev is None:
-            raise Exception("Association with the AP timed out")
+        self.wait_connected(timeout=15)
 
     def relog(self):
         self.global_request("RELOG")
@@ -906,8 +954,14 @@ class WpaSupplicant:
             return None
         return res.split(' ')
 
-    def get_bss(self, bssid):
-        res = self.request("BSS " + bssid)
+    def get_bss(self, bssid, ifname=None):
+       if not ifname or ifname == self.ifname:
+            res = self.request("BSS " + bssid)
+        elif ifname == self.group_ifname:
+            res = self.group_request("BSS " + bssid)
+        else:
+            return None
+
         if "FAIL" in res:
             return None
         lines = res.splitlines()
@@ -983,3 +1037,29 @@ class WpaSupplicant:
         msg['payload'] = frame[24:]
 
         return msg
+
+    def wait_connected(self, timeout=10, error="Connection timed out"):
+        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
+        if ev is None:
+            raise Exception(error)
+        return ev
+
+    def wait_disconnected(self, timeout=10, error="Disconnection timed out"):
+        ev = self.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=timeout)
+        if ev is None:
+            raise Exception(error)
+        return ev
+
+    def get_group_ifname(self):
+        return self.group_ifname if self.group_ifname else self.ifname
+
+    def get_config(self):
+        res = self.request("DUMP")
+        if res.startswith("FAIL"):
+            raise Exception("DUMP failed")
+        lines = res.splitlines()
+        vals = dict()
+        for l in lines:
+            [name,value] = l.split('=', 1)
+            vals[name] = value
+        return vals