All of lore.kernel.org
 help / color / mirror / Atom feed
From: James Prestwood <prestwoj@gmail.com>
To: iwd@lists.01.org
Subject: [PATCH v5 03/26] auto-t: hostapd.py: update to work with test-runner rewrite
Date: Thu, 10 Sep 2020 16:12:24 -0700	[thread overview]
Message-ID: <20200910231248.4995-3-prestwoj@gmail.com> (raw)
In-Reply-To: <20200910231248.4995-1-prestwoj@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 8731 bytes --]

Before hostapd was initialized using the wiphy_map which has now
gone away. Instead we have a global config module which contains
a single 'ctx'. This is the centeral store for all test information.

This patch converts hostapd.py to lookup instances by already
initialized Hostapd object. The interface parameter was removed
since all tests have been converted to use config= instead.

In addition HostapdCLI was changed to allow no parameters if there
is only a single hostapd instance.
---
 autotests/util/hostapd.py | 100 +++++++++++++++++---------------------
 1 file changed, 44 insertions(+), 56 deletions(-)

diff --git a/autotests/util/hostapd.py b/autotests/util/hostapd.py
index 0ed4e004..3f0edd9e 100644
--- a/autotests/util/hostapd.py
+++ b/autotests/util/hostapd.py
@@ -1,11 +1,11 @@
 #!/usr/bin/python3
 import os, os.path
-from wiphy import wiphy_map
 import re
 import socket
 import select
 import time
 from gi.repository import GLib
+from config import ctx
 
 chan_freq_map = [
     None,
@@ -28,31 +28,26 @@ chan_freq_map = [
 ctrl_count = 0
 mainloop = GLib.MainLoop()
 
-hostapd_map = {ifname: intf for wname, wiphy in wiphy_map.items()
-        for ifname, intf in wiphy.interface_map.items()
-        if wiphy.use == 'hostapd'}
-
 class HostapdCLI:
-    def _init_hostapd(self, interface=None, config=None):
+    def _init_hostapd(self, config=None):
         global ctrl_count
+        interface = None
+
+        if not config and len(ctx.hostapd.instances) > 1:
+            raise Exception('config must be provided if more than one hostapd instance exists')
 
-        if not interface and not config:
-            raise Exception('interface or config must be provided')
+        hapd = ctx.hostapd[config]
 
-        if not interface:
-            for intf in hostapd_map.values():
-                if intf.config == config:
-                    interface = intf
-                    break
+        self.interface = hapd.intf
+        self.config = hapd.config
 
-        if not interface:
+        if not self.interface:
             raise Exception('config %s not found' % config)
 
-        self.ifname = interface.name
-        self.socket_path = os.path.dirname(interface.ctrl_interface)
+        self.ifname = self.interface.name
+        self.socket_path = os.path.dirname(self.interface.ctrl_interface)
 
-        self.cmdline = 'hostapd_cli -p"' + self.socket_path + '" -i"' + \
-                self.ifname + '"'
+        self.cmdline = ['hostapd_cli', '-p', self.socket_path, '-i', self.ifname]
 
         if not hasattr(self, '_hostapd_restarted'):
             self._hostapd_restarted = False
@@ -61,6 +56,7 @@ class HostapdCLI:
                             str(ctrl_count)
         self.ctrl_sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
         self.ctrl_sock.bind(self.local_ctrl)
+
         self.ctrl_sock.connect(self.socket_path + '/' + self.ifname)
 
         if 'OK' not in self._ctrl_request('ATTACH'):
@@ -68,8 +64,8 @@ class HostapdCLI:
 
         ctrl_count = ctrl_count + 1
 
-    def __init__(self, interface=None, config=None):
-        self._init_hostapd(interface, config)
+    def __init__(self, config=None):
+        self._init_hostapd(config)
 
     def wait_for_event(self, event, timeout=10):
         global mainloop
@@ -115,53 +111,49 @@ class HostapdCLI:
 
     def _del_hostapd(self, force=False):
         self.ctrl_sock.close()
+        os.remove(self.local_ctrl)
 
         if self._hostapd_restarted:
-            if force:
-                os.system('killall -9 hostapd')
-            else:
-                os.system('killall hostapd')
+            ctx.stop_process(ctx.hostapd.process, force)
 
-            os.system('ifconfig %s down' % self.ifname)
-            os.system('ifconfig %s up' % self.ifname)
+            self.interface.set_interface_state('down')
+            self.interface.set_interface_state('up')
 
     def __del__(self):
         self._del_hostapd()
 
     def wps_push_button(self):
-        os.system(self.cmdline + ' wps_pbc')
+        ctx.start_process(self.cmdline + ['wps_pbc'], wait=True)
 
     def wps_pin(self, pin):
-        os.system(self.cmdline + ' wps_pin any ' + pin)
+        cmd = self.cmdline + ['wps_pin', 'any', pin]
+        ctx.start_process(cmd, wait=True)
 
     def deauthenticate(self, client_address):
-        os.system(self.cmdline + ' deauthenticate ' + client_address)
+        cmd = self.cmdline + ['deauthenticate', client_address]
+        ctx.start_process(cmd, wait=True)
 
     def eapol_reauth(self, client_address):
         cmd = 'IFNAME=' + self.ifname + ' EAPOL_REAUTH ' + client_address
-        s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
-        s.connect(self.socket_path + '/' + self.ifname)
-        s.sendall(cmd.encode('utf-8'))
-        s.close()
+        self.ctrl_sock.sendall(cmd.encode('utf-8'))
 
     def reload(self):
         # Seemingly all three commands needed for the instance to notice
         # interface's address change
-        cmds = 'reload\ndisable\nenable\n'
-        proc = os.popen(self.cmdline, mode='w')
-        lines = proc.write(cmds)
-        proc.close()
+        ctx.start_process(self.cmdline + ['reload'], wait=True)
+        ctx.start_process(self.cmdline + ['disable'], wait=True)
+        ctx.start_process(self.cmdline + ['enable'], wait=True)
 
     def list_sta(self):
-        proc = os.popen(self.cmdline + ' list_sta')
-        lines = proc.read()
-        proc.close()
+        proc = ctx.start_process(self.cmdline + ['list_sta'])
+        proc.pid.wait()
+        lines = proc.pid.stdout.read().decode('utf-8')
 
         return [line for line in lines.split('\n') if line]
 
     def set_neighbor(self, addr, ssid, nr):
-        os.system(self.cmdline + ' set_neighbor ' + addr + ' ssid=\\""' + ssid +
-                    '"\\" nr=' + nr)
+        cmd = self.cmdline + ['set_neighbor', addr, 'ssid=\\""%s"\\"' % ssid, 'nr=%s' % nr]
+        ctx.start_process(cmd, wait=True)
 
     def send_bss_transition(self, device, nr_list):
         # Send a BSS transition to a station (device). nr_list should be an
@@ -170,7 +162,7 @@ class HostapdCLI:
         # consistent with the set_neighbor() API, i.e. the same neighbor report
         # string could be used in both API's.
         pref = 1
-        cmd = self.cmdline + ' bss_tm_req ' + device
+        cmd = self.cmdline + ['bss_tm_req', device]
         for i in nr_list:
             addr = i[0]
             nr = i[1]
@@ -180,19 +172,15 @@ class HostapdCLI:
             chan_num=nr[10:12]
             phy_num=nr[14:16]
 
-            cmd += ' pref=%s neighbor=%s,%s,%s,%s,%s' % \
-                    (str(pref), addr, bss_info, op_class, chan_num, phy_num)
+            cmd += ['pref=%s' % str(pref), 'neighbor=%s,%s,%s,%s,%s' % \
+                        (addr, bss_info, op_class, chan_num, phy_num)]
             pref += 1
 
-        os.system(cmd)
-
-    @staticmethod
-    def kill_all():
-        os.system('killall hostapd')
+        ctx.start_process(cmd, wait=True)
 
     def get_config_value(self, key):
         # first find the right config file
-        with open(hostapd_map[self.ifname].config, 'r') as f:
+        with open(self.config, 'r') as f:
             # read in config file and search for key
             cfg = f.read()
             match = re.search(r'%s=.*' % key, cfg)
@@ -200,6 +188,7 @@ class HostapdCLI:
                 return match.group(0).split('=')[1]
         return None
 
+
     def get_freq(self):
         return chan_freq_map[int(self.get_config_value('channel'))]
 
@@ -209,21 +198,20 @@ class HostapdCLI:
         '''
         # set flag so hostapd can be killed after the test
         self._hostapd_restarted = True
-        intf = hostapd_map[self.ifname]
 
         self._del_hostapd(force=True)
 
-        os.system('hostapd -g %s -i %s %s &' %
-                  (intf.ctrl_interface, intf.name, intf.config))
+        ctx.start_hostapd()
 
         # Give hostapd a second to start and initialize the control interface
         time.sleep(1)
 
         # New hostapd process, so re-init
-        self._init_hostapd(intf)
+        self._init_hostapd(config=self.config)
 
     def req_beacon(self, addr, request):
         '''
             Send a RRM Beacon request
         '''
-        os.system(self.cmdline + ' req_beacon ' + addr + ' ' + request)
+        cmd = self.cmdline + ['req_beacon', addr, request]
+        ctx.start_process(cmd, wait=True)
-- 
2.26.2

  parent reply	other threads:[~2020-09-10 23:12 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-10 23:12 [PATCH v5 01/26] auto-t: prepare autotests for test-runner re-write James Prestwood
2020-09-10 23:12 ` [PATCH v5 02/26] auto-t: introduce pure python " James Prestwood
2020-09-10 23:12 ` James Prestwood [this message]
2020-09-10 23:12 ` [PATCH v5 04/26] auto-t: testutil.py: update to work with test-runner rewrite James Prestwood
2020-09-10 23:12 ` [PATCH v5 05/26] auto-t: ofono.py: fix timeout cleanup and wait for service James Prestwood
2020-09-10 23:12 ` [PATCH v5 06/26] auto-t: iwd.py: update to work with test-runner rewrite James Prestwood
2020-09-10 23:12 ` [PATCH v5 07/26] auto-t: remove wiphy.py James Prestwood
2020-09-10 23:12 ` [PATCH v5 08/26] auto-t: fix hidden network test James Prestwood
2020-09-10 23:12 ` [PATCH v5 09/26] auto-t: fix testSAE autoconnect_test.py James Prestwood
2020-09-10 23:12 ` [PATCH v5 10/26] auto-t: skip ofono tests if ofonod isn't running James Prestwood
2020-09-10 23:12 ` [PATCH v5 11/26] auto-t: replace hard-coded interfaces James Prestwood
2020-09-10 23:12 ` [PATCH v5 12/26] auto-t: remove device.wait_for_connected James Prestwood
2020-09-10 23:12 ` [PATCH v5 13/26] tools: post test-runner rewrite cleanup James Prestwood
2020-09-10 23:12 ` [PATCH v5 14/26] doc: update test runner docs James Prestwood
2020-09-10 23:12 ` [PATCH v5 15/26] auto-t: hwsim: wait for method return James Prestwood
2020-09-10 23:12 ` [PATCH v5 16/26] auto-t: Increase all test timeouts James Prestwood
2020-09-10 23:12 ` [PATCH v5 17/26] auto-t: fix SAE FT test and update James Prestwood
2020-09-10 23:12 ` [PATCH v5 18/26] auto-t: update testClientNonInteractive to use TestContext James Prestwood
2020-09-10 23:12 ` [PATCH v5 19/26] auto-t: update testAgent " James Prestwood
2020-09-10 23:12 ` [PATCH v5 20/26] auto-t: fix testBSSBlacklist James Prestwood
2020-09-10 23:12 ` [PATCH v5 21/26] auto-t: fix testAP James Prestwood
2020-09-10 23:12 ` [PATCH v5 22/26] auto-t: fix testAPRoam James Prestwood
2020-09-10 23:12 ` [PATCH v5 23/26] auto-t: harden testHotspot James Prestwood
2020-09-10 23:12 ` [PATCH v5 24/26] auto-t: fix testWPA2 to wait for device object James Prestwood
2020-09-10 23:12 ` [PATCH v5 25/26] auto-t: fix testPreauth-roam James Prestwood
2020-09-10 23:12 ` [PATCH v5 26/26] auto-t: fix testFT-PSK-roam James Prestwood
2020-09-10 23:21 ` [PATCH v5 01/26] auto-t: prepare autotests for test-runner re-write Denis Kenzior

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200910231248.4995-3-prestwoj@gmail.com \
    --to=prestwoj@gmail.com \
    --cc=iwd@lists.01.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.