Repository navigation
Expand file tree
/
Copy pathtest_socket.py
More file actions
421 lines (327 loc) · 12.5 KB
/
Copy pathtest_socket.py
File metadata and controls
421 lines (327 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import os
import socket
import ssl
import struct
from unittest.mock import MagicMock
import pytest
from mocket import Mocket, MocketEntry, Mocketizer, mocketize
from mocket.mockhttp import Entry
from mocket.socket import MocketSocket
from mocket.ssl.context import MocketSSLContext, mock_wrap_socket
from mocket.ssl.socket import MocketSSLSocket
from mocket.urllib3 import mock_match_hostname
@pytest.mark.parametrize("blocking", (False, True))
def test_blocking_socket(blocking):
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("locahost", 1234))
sock.setblocking(blocking)
assert sock.getblocking() is blocking
@mocketize
def test_udp_socket():
host = "127.0.0.1"
port = 9999
request_data = b"ping"
response_data = b"pong"
Mocket.register(MocketEntry((host, port), [response_data]))
# Your UDP client code
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(request_data, (host, port))
data, address = sock.recvfrom(1024)
assert data == response_data
assert address == (host, port)
def test_recvmsg():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
test_data = b"hello world"
sock._io = type("MockIO", (), {"read": lambda self, n: test_data})()
data, ancdata = sock.recvmsg(1024)
assert data == test_data
assert ancdata == []
def test_recvmsg_into():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
test_data = b"foobar"
sock._io = type("MockIO", (), {"read": lambda self, n: test_data})()
buf = bytearray(10)
buf2 = bytearray(10)
buffers = [buf, buf2]
nbytes = sock.recvmsg_into(buffers)
assert nbytes == len(test_data)
assert buf[: len(test_data)] == test_data
def test_recvmsg_into_empty_buffers():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.recvmsg_into([])
assert result == 0
def test_accept():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
sock._host = "127.0.0.1"
sock._port = 8080
new_sock, addr = sock.accept()
assert isinstance(new_sock, MocketSocket)
assert new_sock is not sock
assert addr == ("127.0.0.1", 8080)
assert new_sock._host == "127.0.0.1"
assert new_sock._port == 8080
@mocketize
def test_sendmsg():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
sock._host = "127.0.0.1"
sock._port = 8080
response_data = b"pong"
Mocket.register(MocketEntry((sock._host, sock._port), [response_data]))
msg = [b"foo", b"bar", b"foobaz"]
total_sent = sock.sendmsg(msg)
assert total_sent == sum(len(m) for m in msg)
assert Mocket.last_request() == b"".join(msg)
def test_sendmsg_empty_buffers():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.sendmsg([])
assert result == 0
def test_recvmsg_no_data():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
# Mock _io.read to return empty bytes
sock._io = type("MockIO", (), {"read": lambda self, n: b""})()
data, ancdata = sock.recvmsg(1024)
assert data == b""
assert ancdata == []
def test_recvmsg_into_no_data():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
# Mock _io.read to return empty bytes
sock._io = type("MockIO", (), {"read": lambda self, n: b""})()
buf = bytearray(10)
nbytes = sock.recvmsg_into([buf])
assert nbytes == 0
assert buf == bytearray(10)
def test_getsockopt():
# getsockopt is a static method, so we can call it directly
result = MocketSocket.getsockopt(0, 0)
assert result == socket.SOCK_STREAM
@pytest.mark.parametrize(
("server_hostname", "expected_host"),
[
(b"httpbin.local", "httpbin.local"),
("httpbin.local", "httpbin.local"),
(None, "httpbin.local"),
("", ""),
(b"mocket-\xff.local", "mocket-�.local"),
],
)
def test_wrap_bio_uses_current_mocket_address(
monkeypatch, server_hostname, expected_host
):
monkeypatch.setattr(Mocket, "_address", ("httpbin.local", 443))
ssl_obj = MocketSSLContext().wrap_bio(
incoming=None,
outgoing=None,
server_hostname=server_hostname,
)
assert ssl_obj._host == expected_host
assert ssl_obj._port == 443
assert ssl_obj._address == (expected_host, 443)
def test_wrap_bio_preserves_empty_server_hostname_on_getpeercert(monkeypatch):
monkeypatch.setattr(Mocket, "_address", ("httpbin.local", 443))
ssl_obj = MocketSSLContext().wrap_bio(
incoming=None,
outgoing=None,
server_hostname="",
)
ssl_obj.getpeercert()
assert ssl_obj._host == ""
assert ssl_obj._address == ("", 443)
def test_wrap_bio_with_invalid_mocket_address(monkeypatch):
monkeypatch.setattr(Mocket, "_address", "invalid-address")
ssl_obj = MocketSSLContext().wrap_bio(
incoming=None,
outgoing=None,
server_hostname=None,
)
assert ssl_obj._host is None
assert ssl_obj._port is None
def test_mock_wrap_socket_delegates_to_context(monkeypatch):
expected = MocketSSLSocket()
def fake_wrap_socket(self, sock, *args, **kwargs):
return expected
monkeypatch.setattr(MocketSSLContext, "wrap_socket", fake_wrap_socket)
assert mock_wrap_socket(MocketSocket()) is expected
def test_mock_match_hostname_returns_none():
assert mock_match_hostname("example.org", object()) is None
def test_getpeercert_does_not_overwrite_empty_host_when_port_missing(monkeypatch):
monkeypatch.setattr(Mocket, "_address", ("httpbin.local", 443))
ssl_obj = MocketSSLSocket()
ssl_obj._host = ""
ssl_obj._port = None
ssl_obj._address = ("", None)
ssl_obj.getpeercert()
assert ssl_obj._host == ""
assert ssl_obj._port == 443
assert ssl_obj._address == ("", 443)
def test_recvfrom_into():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
test_data = b"abc123"
sock._io = type("MockIO", (), {"read": lambda self, n: test_data})()
buf = bytearray(10)
nbytes, addr = sock.recvfrom_into(buf)
assert nbytes == len(test_data)
assert buf[:nbytes] == test_data
assert addr == sock._address
def test_setsockopt_without_optlen():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
sock._true_socket = MagicMock()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock._true_socket.setsockopt.assert_called_once_with(
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1
)
def test_setsockopt_with_optlen():
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
sock._true_socket = MagicMock()
linger_value = struct.pack("ii", 1, 5)
sock.setsockopt(
socket.SOL_SOCKET, socket.SO_LINGER, linger_value, len(linger_value)
)
sock._true_socket.setsockopt.assert_called_once_with(
socket.SOL_SOCKET, socket.SO_LINGER, linger_value, len(linger_value)
)
def test_ssl_read_empty_after_handshake_returns_empty_bytes():
"""After handshake, empty SSL reads should not raise SSLWantReadError."""
sock = MocketSSLSocket()
sock._io = type("MockIO", (), {"read": lambda self, n: b""})()
sock._did_handshake = True
sock._has_written = True
assert sock.read(1024) == b""
def test_ssl_read_empty_after_handshake_before_write_raises_want_read():
"""After handshake but before writes, empty reads should signal WANT_READ."""
sock = MocketSSLSocket()
sock._io = type("MockIO", (), {"read": lambda self, n: b""})()
sock._did_handshake = True
sock._has_written = False
with pytest.raises(ssl.SSLWantReadError):
sock.read(1024)
def test_ssl_ciper_returns_mock_tuple():
"""Exercise the SSL mock cipher tuple branch for coverage."""
sock = MocketSSLSocket()
assert sock.ciper() == ("ADH", "AES256", "SHA")
def test_ssl_getpeercert_uses_mocket_address_when_unset(monkeypatch):
"""Cover getpeercert fallback when host/port are not yet assigned."""
monkeypatch.setattr(Mocket, "_address", ("example.local", 443))
sock = MocketSSLSocket()
cert = sock.getpeercert()
assert sock._host == "example.local"
assert sock._port == 443
assert sock._address == ("example.local", 443)
assert cert["subjectAltName"][1] == ("DNS", "example.local")
# ---------------------------------------------------------------------------
# New pipe-mechanism tests added to maintain coverage after the large-response
# deadlock fix.
# ---------------------------------------------------------------------------
def test_mocket_shared_io_same_address():
"""Two MocketSocket instances for the same address share one I/O buffer."""
addr = ("localhost", 9001)
with Mocketizer():
s1 = MocketSocket()
s1.connect(addr)
s2 = MocketSocket()
s2.connect(addr)
# Accessing .io lazily creates and registers the shared buffer
assert s1.io is s2.io
def test_mocket_shared_io_recreated_after_close():
"""If the shared buffer is closed, accessing .io creates a fresh one."""
addr = ("localhost", 9002)
with Mocketizer():
s = MocketSocket()
s.connect(addr)
old_io = s.io
old_io.close()
# Force re-evaluation by clearing the cached reference
s._io = None
new_io = s.io
assert not new_io.closed
assert new_io is not old_io
def test_mocket_pipe_uses_data_false_for_large_response():
"""Responses larger than _buflen use readiness-only signaling, not data in pipe."""
addr = ("localhost", 9003)
large_body = "x" * 70_000
Entry.single_register(
method=Entry.GET,
uri="http://localhost:9003/",
body=large_body,
)
with Mocketizer():
s = MocketSocket()
s.connect(addr)
request = b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"
s.sendall(request)
assert not Mocket.pipe_uses_data(addr), (
"large response must use readiness-only pipe"
)
@mocketize
def test_large_response_recv_drains_readiness_sentinel():
"""recv() for a large response drains readiness bytes and re-syncs the pipe.
This exercises the _sync_readable_pipe() re-sync call (socket.py line 638)
that runs after draining sentinel bytes from a readiness-only pipe.
"""
addr = ("localhost", 9004)
large_body = "y" * 70_000
Entry.single_register(
method=Entry.GET,
uri="http://localhost:9004/",
body=large_body,
)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(addr)
# Calling fileno() forces pipe creation so the readiness path is active.
s.fileno()
s.sendall(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
# The response is large → pipe_uses_data=False → readiness sentinels are used.
# Collecting the full body exercises the drain + re-sync branch on line 638.
received = b""
while True:
try:
chunk = s.recv(65536)
if not chunk:
break
received += chunk
except BlockingIOError:
break
s.close()
assert large_body.encode() in received
def test_mocket_get_set_io():
"""Mocket.get_io / set_io round-trip."""
from mocket.io import MocketSocketIO
addr = ("localhost", 9005)
buf = MocketSocketIO(addr)
Mocket.set_io(addr, buf)
assert Mocket.get_io(addr) is buf
def test_mocket_pipe_uses_data_flag():
"""Mocket.pipe_uses_data / set_pipe_uses_data round-trip."""
addr = ("localhost", 9006)
assert not Mocket.pipe_uses_data(addr)
Mocket.set_pipe_uses_data(addr, True)
assert Mocket.pipe_uses_data(addr)
Mocket.set_pipe_uses_data(addr, False)
assert not Mocket.pipe_uses_data(addr)
def test_mocket_reset_clears_shared_ios():
"""Mocket.reset() clears the shared I/O buffer registry."""
from mocket.io import MocketSocketIO
addr = ("localhost", 9007)
Mocket.set_io(addr, MocketSocketIO(addr))
Mocket.reset()
assert Mocket.get_io(addr) is None
def test_mirror_buffer_to_pipe_writes_data():
"""_mirror_buffer_to_pipe writes the unread buffer content into the pipe."""
addr = ("localhost", 9008)
s = MocketSocket()
s.connect(addr)
# Simulate a small response already buffered (no sendall needed)
response = b"hello pipe"
s.io.seek(0)
s.io.write(response)
s.io.seek(0)
# Create the pipe manually
r_fd, w_fd = os.pipe()
os.set_blocking(r_fd, False)
os.set_blocking(w_fd, False)
Mocket.set_pair(addr, (r_fd, w_fd))
s._mirror_buffer_to_pipe()
pipe_bytes = os.read(r_fd, 64)
os.close(r_fd)
os.close(w_fd)
Mocket._socket_pairs.pop(addr, None)
assert pipe_bytes == response