Sanity-test the returned agent client with loaded test keys
Marco Ricci

Marco Ricci commited on 2025-12-25 14:44:46
Zeige 1 geänderte Dateien mit 23 Einfügungen und 0 Löschungen.


Add sanity checks for the constructed agent client with optimistically
loaded test keys.  The docstring asserts that the test fixture will skip
if no keys can be loaded, but that wasn't the case.  Furthermore, while
there is little point in sanity testing the client when it is first
received (from a different fixture, which does its own sanity checks),
it makes sense to sanity-test the client *after* attempting to load all
those test keys into the agent, because certain misbehaved agents
terminate upon encountering unsupported key formats (see inline
commentary), rendering the client non-functional.

However, do not check whether the stubbed test agent can load the test
keys.  The stubbed test agent accesses the test keys directly, and only
the test keys; whether the "load keys" operation succeeds or not is
completely irrelevant.  There is also little point in actually
implementing the load operation: it is just more code that needs
coverage, and dedicated tests.
... ...
@@ -882,6 +882,8 @@ def ssh_agent_client_with_test_keys_loaded(  # noqa: C901
882 882
     # `SSHAgentClient.list_keys`.  There is thus little point in
883 883
     # repeating the very same sanity test here, on an agent that was
884 884
     # already sanity-tested.
885
+    #
886
+    # (But see below, too.)
885 887
 
886 888
     def prepare_payload(
887 889
         payload: bytes | bytearray,
... ...
@@ -959,6 +961,27 @@ def ssh_agent_client_with_test_keys_loaded(  # noqa: C901
959 961
                 pass
960 962
             else:  # pragma: no cover [external]
961 963
                 successfully_loaded_keys.add(key_type)
964
+        if (
965
+            agent_type != data.KnownSSHAgent.StubbedSSHAgent
966
+            and not successfully_loaded_keys
967
+        ):  # pragma: no cover [external]
968
+            pytest.skip("Failed to load any test keys at all into the agent.")
969
+        # Sanity-test the agent.  It makes sense to do this here (unlike
970
+        # at the top of this fixture), because we ignore errors when
971
+        # adding keys above, but badly behaved SSH agents such as the
972
+        # Annoying OS port of OpenSSH 10.0p1 (Dec 2025) may have already
973
+        # closed the connection.
974
+        try:
975
+            client.list_keys()
976
+        except (
977
+            EOFError,
978
+            OSError,
979
+            ssh_agent.SSHAgentFailedError,
980
+        ):  # pragma: no cover [external]
981
+            pytest.skip(
982
+                "The agent became unusable during test fixture setup.  "
983
+                "(Sanity check failed.)"
984
+            )
962 985
         yield client
963 986
     finally:
964 987
         for key_type, key_struct in tests.data.ALL_KEYS.items():
965 988