Skip to contentSkip to conversation

auto-t: allow multiple PSKAgent's to be registered

PATCH In review James Prestwood 2 participants 3 messages Jun 29, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 auto-t: allow multiple PSKAgent's to be registered

By creating a new bus connection for each agent we can register multiple with IWD. This did mean the agent interface needs to be unique for each agent (removing _agent_manager_if) as well as tracking multiple agents in a list.

---
 autotests/util/iwd.py | 51 ++++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 27 deletions(-)
autotests/util/iwd.py +24 -27
index b74ba440..5591d7e1 100755
@@ -865,10 +865,11 @@ class PSKAgent(dbus.service.Object):
             users = [users]
         self.users = users
         self._path = '/test/agent/%s' % agent_count
+        self._bus = dbus.bus.BusConnection(address_or_type=namespace.dbus_address)
 
         agent_count += 1
 
-        dbus.service.Object.__init__(self, namespace.get_bus(), self._path)
+        dbus.service.Object.__init__(self, self._bus, self._path)
 
     @property
     def path(self):
@@ -1086,11 +1087,10 @@ class IWD(AsyncOpAbstract):
         start_iwd_daemon=True)
     '''
     _object_manager_if = None
-    _agent_manager_if = None
     _iwd_proc = None
     _devices = None
     _default_instance = None
-    psk_agent = None
+    psk_agents = []
 
     def __init__(self, start_iwd_daemon = False, iwd_config_dir = '/tmp',
                             iwd_storage_dir = IWD_STORAGE_DIR, namespace=ctx):
@@ -1114,11 +1114,12 @@ class IWD(AsyncOpAbstract):
             IWD._default_instance = weakref.ref(self)
 
     def __del__(self):
-        if self.psk_agent:
-            self.unregister_psk_agent(self.psk_agent)
+        for agent in self.psk_agents:
+            self.unregister_psk_agent(agent)
+
+        self.psk_agents = []
 
         self._object_manager_if = None
-        self._agent_manager_if = None
         self._known_networks = None
         self._devices = None
 
@@ -1137,15 +1138,6 @@ class IWD(AsyncOpAbstract):
                                       DBUS_OBJECT_MANAGER)
         return self._object_manager_if
 
-    @property
-    def _agent_manager(self):
-        if self._agent_manager_if is None:
-            self._agent_manager_if =\
-                dbus.Interface(self._bus.get_object(IWD_SERVICE,
-                                                    IWD_AGENT_MANAGER_PATH),
-                               IWD_AGENT_MANAGER_INTERFACE)
-        return self._agent_manager_if
-
     @staticmethod
     def _wait_for_object_condition(obj, condition_str, max_wait = 50):
         def _eval_wrap(obj, condition_str):
@@ -1271,22 +1263,27 @@ class IWD(AsyncOpAbstract):
         return known_network_list
 
     def register_psk_agent(self, psk_agent):
-        self._agent_manager.RegisterAgent(
-                                     psk_agent.path,
-                                     dbus_interface=IWD_AGENT_MANAGER_INTERFACE,
-                                     reply_handler=self._success,
-                                     error_handler=self._failure)
+        iface = dbus.Interface(psk_agent._bus.get_object(IWD_SERVICE,
+                                                IWD_AGENT_MANAGER_PATH),
+                                                IWD_AGENT_MANAGER_INTERFACE)
+        iface.RegisterAgent(psk_agent.path,
+                            dbus_interface=IWD_AGENT_MANAGER_INTERFACE,
+                            reply_handler=self._success,
+                            error_handler=self._failure)
+
         self._wait_for_async_op()
-        self.psk_agent = psk_agent
+        self.psk_agents.append(psk_agent)
 
     def unregister_psk_agent(self, psk_agent):
-        self._agent_manager.UnregisterAgent(
-                                     psk_agent.path,
-                                     dbus_interface=IWD_AGENT_MANAGER_INTERFACE,
-                                     reply_handler=self._success,
-                                     error_handler=self._failure)
+        iface = dbus.Interface(psk_agent._bus.get_object(IWD_SERVICE,
+                                                IWD_AGENT_MANAGER_PATH),
+                                                IWD_AGENT_MANAGER_INTERFACE)
+        iface.UnregisterAgent(psk_agent.path,
+                                dbus_interface=IWD_AGENT_MANAGER_INTERFACE,
+                                reply_handler=self._success,
+                                error_handler=self._failure)
         self._wait_for_async_op()
-        self.psk_agent = None
+        self.psk_agents.remove(psk_agent)
 
     @staticmethod
     def get_instance():
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 auto-t: fix unpredictability/pointless test from testAgent

testAgent had a few tests which weren't reliable, and one was not actually testing anything, or at least not what the name implied it should be testing.

The first issue was using iwctl in the first place. There is not a reliable way to know when iwctl has registered its agent so relying on that with a sleep, or waiting for the service to become available isn't 100% fool proof. To fix this use the updated PSKAgent which allows multiple to be registered. This ensures the agent is ready for requests. This test was also renamed to be consistent with what its actually testing: that IWD uses the first agent registered.

This removes test_connection_with_other_agent as well because this test case is covered by the client test itself. There is no need to re-test iwctl's agent functionality here.

---
 autotests/testAgent/agent_test.py | 40 ++++---------------------------
 1 file changed, 4 insertions(+), 36 deletions(-)
autotests/testAgent/agent_test.py +4 -36
index c1dc8c7c..fc988f21 100644
@@ -56,51 +56,19 @@ class Test(unittest.TestCase):
 
         IWD.clear_storage()
 
-    def test_connection_with_other_agent(self):
-        def wait_for_service_pid(pid):
-            dbus_object = ctx._bus.get_object('org.freedesktop.DBus',
-                                 '/org/freedesktop/DBus')
-            dbus_iface = dbus.Interface(dbus_object, 'org.freedesktop.DBus')
-
-            services = dbus_iface.ListNames()
-
-            for service in services:
-                bus_iface = dbus.Interface(dbus_object, "org.freedesktop.DBus")
-                if pid == int(bus_iface.GetConnectionUnixProcessID(service)):
-                    return True
-
-            return False
-
+    def test_connection_use_first_from_multiple_registered(self):
         wd = IWD()
 
-        iwctl = ctx.start_process(['iwctl', '-P', 'secret_ssid2'])
-
-        # Let iwctl to start and register its agent.
-        ctx.non_block_wait(wait_for_service_pid, 10, iwctl.pid)
-
-        self.check_connection(wd, 'ssid2')
-
-        iwctl.kill()
-
-        IWD.clear_storage()
-
-    def test_connection_use_own_agent_from_multiple_registered(self):
-
-        wd = IWD()
-
-        iwctl = ctx.start_process(['iwctl', '-P', 'secret_ssid2'])
-        # Let iwctl to start and register its agent.
-        wd.wait(2)
-
         psk_agent = PSKAgent("secret_ssid1")
         wd.register_psk_agent(psk_agent)
 
+        psk_agent = PSKAgent("secret_ssid2")
+        wd.register_psk_agent(psk_agent)
+
         self.check_connection(wd, 'ssid1')
 
         wd.unregister_psk_agent(psk_agent)
 
-        iwctl.kill()
-
         IWD.clear_storage()
 
     @classmethod
Signature · 1 line
2.34.1
#2 lore raw
In reply to James Prestwood

Hi James,

Quoted 8 lines · James Prestwood

By creating a new bus connection for each agent we can register multiple with IWD. This did mean the agent interface needs to be unique for each agent (removing _agent_manager_if) as well as tracking multiple agents in a list.

---
  autotests/util/iwd.py | 51 ++++++++++++++++++++-----------------------
  1 file changed, 24 insertions(+), 27 deletions(-)

Both applied, thanks.

Regards,
-Denis