Use the stub agent for SSH agent client communications faking
Marco Ricci

Marco Ricci commited on 2026-06-20 14:39:49
Zeige 1 geänderte Dateien mit 5 Einfügungen und 13 Löschungen.


Instead of stubbing all I/O to the stub agent ourselves, use the stub
agent's facilities for this.
... ...
@@ -10,12 +10,11 @@ import base64
10 10
 import contextlib
11 11
 import ctypes
12 12
 import importlib.metadata
13
-import io
14 13
 import re
15 14
 import socket
16 15
 import sys
17 16
 import types
18
-from typing import TYPE_CHECKING, Final
17
+from typing import TYPE_CHECKING, Final, cast
19 18
 
20 19
 import click
21 20
 import hypothesis
... ...
@@ -1247,6 +1246,7 @@ class TestOtherConstructorFeatures:
1247 1246
         self,
1248 1247
         spawn_ssh_agent: data.SpawnedSSHAgentInfo,
1249 1248
     ) -> None:
1249
+        """The constructor accepts existing sockets."""
1250 1250
         conn = spawn_ssh_agent.client._connection
1251 1251
         ssh_agent.SSHAgentClient(socket=conn)
1252 1252
 
... ...
@@ -1294,18 +1294,10 @@ class TestAgentErrorResponses:
1294 1294
     ) -> None:
1295 1295
         """Fail on truncated responses from the SSH agent."""
1296 1296
         client = ssh_agent.SSHAgentClient()
1297
-        response_stream = io.BytesIO(response)
1298
-
1299
-        class PseudoSocket:
1300
-            def sendall(self, *args: Any, **kwargs: Any) -> Any:  # noqa: ARG002
1301
-                return None
1302
-
1303
-            def recv(self, *args: Any, **kwargs: Any) -> Any:
1304
-                return response_stream.read(*args, **kwargs)
1305
-
1306
-        pseudo_socket = PseudoSocket()
1297
+        agent = cast("machinery.StubbedSSHAgentSocket", client._connection)
1307 1298
         with pytest.MonkeyPatch.context() as monkeypatch:
1308
-            monkeypatch.setattr(client, "_connection", pseudo_socket)
1299
+            monkeypatch.setattr(agent, "send_to_client", bytearray(response))
1300
+            monkeypatch.setattr(agent, "sendall", lambda *_a, **_kw: None)
1309 1301
             with pytest.raises(EOFError):
1310 1302
                 client.request(255, b"")
1311 1303
 
1312 1304