| 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 |
import logging |
| 29 |
import ssl |
| 30 |
from unittest import mock |
| 31 |
|
| 32 |
import pytest |
| 33 |
|
| 34 |
from apprise.exception import AppriseException, AppriseInvalidData |
| 35 |
from apprise.utils.socket import AppriseSocketError, SocketTransport |
| 36 |
|
| 37 |
|
| 38 |
logging.disable(logging.CRITICAL) |
| 39 |
|
| 40 |
|
| 41 |
class _DummyFile: |
| 42 |
def __init__(self) -> None: |
| 43 |
self.flushed = 0 |
| 44 |
self.closed = 0 |
| 45 |
|
| 46 |
def flush(self) -> None: |
| 47 |
self.flushed += 1 |
| 48 |
|
| 49 |
def close(self) -> None: |
| 50 |
self.closed += 1 |
| 51 |
|
| 52 |
|
| 53 |
class _DummySocket: |
| 54 |
""" |
| 55 |
Minimal fake socket object, supports the subset used by SocketTransport. |
| 56 |
""" |
| 57 |
|
| 58 |
def __init__(self) -> None: |
| 59 |
self.blocking = True |
| 60 |
self.timeout = None |
| 61 |
self.closed = False |
| 62 |
self.shutdown_called = 0 |
| 63 |
|
| 64 |
self._recv_side_effect = None |
| 65 |
self._send_side_effect = None |
| 66 |
|
| 67 |
def setsockopt(self, *_args, **_kwargs) -> None: |
| 68 |
return None |
| 69 |
|
| 70 |
def bind(self, *_args, **_kwargs) -> None: |
| 71 |
return None |
| 72 |
|
| 73 |
def fileno(self) -> int: |
| 74 |
return 1 |
| 75 |
|
| 76 |
def settimeout(self, value): |
| 77 |
self.timeout = value |
| 78 |
|
| 79 |
def setblocking(self, value): |
| 80 |
self.blocking = bool(value) |
| 81 |
|
| 82 |
def connect(self, *_args, **_kwargs) -> None: |
| 83 |
return None |
| 84 |
|
| 85 |
def shutdown(self, *_args, **_kwargs) -> None: |
| 86 |
self.shutdown_called += 1 |
| 87 |
|
| 88 |
def close(self) -> None: |
| 89 |
self.closed = True |
| 90 |
|
| 91 |
def getsockname(self): |
| 92 |
return ("127.0.0.1", 12345) |
| 93 |
|
| 94 |
def getpeername(self): |
| 95 |
return ("203.0.113.10", 6667) |
| 96 |
|
| 97 |
def makefile(self, *_args, **_kwargs): |
| 98 |
return _DummyFile() |
| 99 |
|
| 100 |
def recv(self, *_args, **_kwargs): |
| 101 |
if self._recv_side_effect is not None: |
| 102 |
raise self._recv_side_effect |
| 103 |
return b"data" |
| 104 |
|
| 105 |
def send(self, *_args, **_kwargs): |
| 106 |
if self._send_side_effect is not None: |
| 107 |
raise self._send_side_effect |
| 108 |
return 4 |
| 109 |
|
| 110 |
|
| 111 |
def test_utils_socket_timeout_coerce(): |
| 112 |
"""SocketTransport() Timeout Coercion.""" |
| 113 |
|
| 114 |
s = SocketTransport("example.com", 1, timeout=None) |
| 115 |
assert s._connect_timeout is None |
| 116 |
assert s._read_timeout is None |
| 117 |
|
| 118 |
|
| 119 |
s = SocketTransport("example.com", 1, timeout=2.5) |
| 120 |
assert s._connect_timeout == pytest.approx(2.5) |
| 121 |
assert s._read_timeout == pytest.approx(2.5) |
| 122 |
|
| 123 |
|
| 124 |
s = SocketTransport("example.com", 1, timeout=(1.0, 3.0)) |
| 125 |
assert s._connect_timeout == pytest.approx(1.0) |
| 126 |
assert s._read_timeout == pytest.approx(3.0) |
| 127 |
|
| 128 |
|
| 129 |
s = SocketTransport("example.com", 1, timeout=(None, 3.0)) |
| 130 |
assert s._connect_timeout is None |
| 131 |
assert s._read_timeout == pytest.approx(3.0) |
| 132 |
|
| 133 |
|
| 134 |
with pytest.raises(AppriseInvalidData): |
| 135 |
SocketTransport("example.com", 1, timeout="bad") |
| 136 |
|
| 137 |
with pytest.raises(AppriseInvalidData): |
| 138 |
SocketTransport("example.com", 1, timeout=(1.0, 2.0, 3.0)) |
| 139 |
|
| 140 |
with pytest.raises(AppriseInvalidData): |
| 141 |
SocketTransport("example.com", 1, timeout=-1.0) |
| 142 |
|
| 143 |
|
| 144 |
def test_utils_socket_properties_and_close_paths(): |
| 145 |
"""SocketTransport() Properties And Close Paths.""" |
| 146 |
s = SocketTransport("example.com", 1) |
| 147 |
assert s.connected is False |
| 148 |
assert s.is_tls is False |
| 149 |
|
| 150 |
|
| 151 |
s._sock = _DummySocket() |
| 152 |
s._rfile = _DummyFile() |
| 153 |
s._wfile = _DummyFile() |
| 154 |
s._is_tls = True |
| 155 |
s.local_addr = ("1.1.1.1", 1) |
| 156 |
s.remote_addr = ("2.2.2.2", 2) |
| 157 |
|
| 158 |
s.close() |
| 159 |
assert s._sock is None |
| 160 |
assert s._rfile is None |
| 161 |
assert s._wfile is None |
| 162 |
assert s._is_tls is False |
| 163 |
assert s.local_addr is None |
| 164 |
assert s.remote_addr is None |
| 165 |
|
| 166 |
|
| 167 |
def test_utils_socket_read_write_no_socket(): |
| 168 |
"""SocketTransport() Can Read/Write - No Socket.""" |
| 169 |
s = SocketTransport("example.com", 1) |
| 170 |
assert s.can_read() is None |
| 171 |
assert s.can_write() is None |
| 172 |
|
| 173 |
|
| 174 |
def test_utils_socket_can_read_write_select_error(): |
| 175 |
"""SocketTransport() Can Read/Write - select() Error.""" |
| 176 |
s = SocketTransport("example.com", 1) |
| 177 |
s._sock = _DummySocket() |
| 178 |
|
| 179 |
with mock.patch("select.select", side_effect=OSError()): |
| 180 |
assert s.can_read() is None |
| 181 |
assert s._sock is None |
| 182 |
|
| 183 |
s._sock = _DummySocket() |
| 184 |
with mock.patch("select.select", side_effect=OSError()): |
| 185 |
assert s.can_write() is None |
| 186 |
assert s._sock is None |
| 187 |
|
| 188 |
|
| 189 |
def test_utils_socket_can_read_write_socket_close(): |
| 190 |
"""SocketTransport() Can Read/Write - Socket Close.""" |
| 191 |
s = SocketTransport("example.com", 1) |
| 192 |
s._sock = _DummySocket() |
| 193 |
|
| 194 |
|
| 195 |
with mock.patch("select.select", return_value=([], [], [s._sock])): |
| 196 |
assert s.can_read() is None |
| 197 |
assert s._sock is None |
| 198 |
|
| 199 |
s._sock = _DummySocket() |
| 200 |
with mock.patch("select.select", return_value=([], [], [s._sock])): |
| 201 |
assert s.can_write() is None |
| 202 |
assert s._sock is None |
| 203 |
|
| 204 |
|
| 205 |
def test_utils_socket_server_hostname_for_tls(): |
| 206 |
"""SocketTransport() Server Hostname For TLS.""" |
| 207 |
s = SocketTransport("irc.example.com", 1, verify=True) |
| 208 |
assert s._server_hostname_for_tls() == "irc.example.com" |
| 209 |
|
| 210 |
s = SocketTransport("irc.example.com", 1, verify=False) |
| 211 |
assert s._server_hostname_for_tls() == "irc.example.com" |
| 212 |
|
| 213 |
|
| 214 |
def test_utils_socket_server_hostname_for_tls_ip_reverse(): |
| 215 |
"""SocketTransport() Server Hostname For TLS IP Reverse DNS""" |
| 216 |
s = SocketTransport("203.0.113.10", 1, verify=True) |
| 217 |
|
| 218 |
with mock.patch( |
| 219 |
"socket.gethostbyaddr", return_value=("irc.example.com.", [], []) |
| 220 |
): |
| 221 |
assert s._server_hostname_for_tls() == "irc.example.com" |
| 222 |
|
| 223 |
with mock.patch("socket.gethostbyaddr", side_effect=OSError()): |
| 224 |
assert s._server_hostname_for_tls() == "203.0.113.10" |
| 225 |
|
| 226 |
|
| 227 |
s = SocketTransport("203.0.113.10", 1, verify=False) |
| 228 |
with mock.patch("socket.gethostbyaddr") as m: |
| 229 |
assert s._server_hostname_for_tls() == "203.0.113.10" |
| 230 |
m.assert_not_called() |
| 231 |
|
| 232 |
|
| 233 |
def test_utils_socket_build_ssl_context(): |
| 234 |
"""SocketTransport() Build SSL Context.""" |
| 235 |
|
| 236 |
class _DummySSLContext: |
| 237 |
def __init__(self, *, raise_on_minimum: bool) -> None: |
| 238 |
self.raise_on_minimum = raise_on_minimum |
| 239 |
self.options = 0 |
| 240 |
self.check_hostname = None |
| 241 |
self.verify_mode = None |
| 242 |
self._minimum_version = None |
| 243 |
|
| 244 |
@property |
| 245 |
def minimum_version(self): |
| 246 |
return self._minimum_version |
| 247 |
|
| 248 |
@minimum_version.setter |
| 249 |
def minimum_version(self, value): |
| 250 |
if self.raise_on_minimum: |
| 251 |
raise RuntimeError("minimum_version not supported") |
| 252 |
self._minimum_version = value |
| 253 |
|
| 254 |
|
| 255 |
s = SocketTransport("example.com", 1, verify=True) |
| 256 |
ctx = _DummySSLContext(raise_on_minimum=False) |
| 257 |
|
| 258 |
with ( |
| 259 |
mock.patch("certifi.where", return_value="/tmp/ca.pem"), |
| 260 |
mock.patch("ssl.create_default_context", return_value=ctx) as m, |
| 261 |
): |
| 262 |
result = s._build_ssl_context() |
| 263 |
assert result is ctx |
| 264 |
m.assert_called_once() |
| 265 |
assert ctx.check_hostname is True |
| 266 |
assert ctx.verify_mode == ssl.CERT_REQUIRED |
| 267 |
|
| 268 |
|
| 269 |
assert ctx.minimum_version == ssl.TLSVersion.TLSv1_2 |
| 270 |
|
| 271 |
|
| 272 |
if hasattr(ssl, "OP_NO_COMPRESSION"): |
| 273 |
assert (ctx.options & ssl.OP_NO_COMPRESSION) != 0 |
| 274 |
|
| 275 |
|
| 276 |
s = SocketTransport("example.com", 1, verify=False) |
| 277 |
ctx = _DummySSLContext(raise_on_minimum=False) |
| 278 |
|
| 279 |
with mock.patch("ssl.create_default_context", return_value=ctx): |
| 280 |
result = s._build_ssl_context() |
| 281 |
assert result is ctx |
| 282 |
assert ctx.check_hostname is False |
| 283 |
assert ctx.verify_mode == ssl.CERT_NONE |
| 284 |
assert ctx.minimum_version == ssl.TLSVersion.TLSv1_2 |
| 285 |
|
| 286 |
|
| 287 |
s = SocketTransport("example.com", 1, verify=True) |
| 288 |
ctx = _DummySSLContext(raise_on_minimum=True) |
| 289 |
|
| 290 |
with ( |
| 291 |
mock.patch("certifi.where", return_value="/tmp/ca.pem"), |
| 292 |
mock.patch("ssl.create_default_context", return_value=ctx), |
| 293 |
): |
| 294 |
result = s._build_ssl_context() |
| 295 |
assert result is ctx |
| 296 |
|
| 297 |
|
| 298 |
assert ctx.minimum_version is None |
| 299 |
|
| 300 |
|
| 301 |
if hasattr(ssl, "OP_NO_TLSv1"): |
| 302 |
assert (ctx.options & ssl.OP_NO_TLSv1) != 0 |
| 303 |
if hasattr(ssl, "OP_NO_TLSv1_1"): |
| 304 |
assert (ctx.options & ssl.OP_NO_TLSv1_1) != 0 |
| 305 |
|
| 306 |
|
| 307 |
def test_utils_socket_start_tls_no_socket_raises(): |
| 308 |
"""SocketTransport() Start TLS No Socket Raises.""" |
| 309 |
s = SocketTransport("example.com", 1, secure=True) |
| 310 |
with pytest.raises(AppriseSocketError): |
| 311 |
s.start_tls() |
| 312 |
|
| 313 |
|
| 314 |
def test_utils_socket_start_tls_already_tls_noop(): |
| 315 |
"""SocketTransport() Start TLS Already TLS Noop.""" |
| 316 |
s = SocketTransport("example.com", 1, secure=True) |
| 317 |
s._sock = _DummySocket() |
| 318 |
s._is_tls = True |
| 319 |
s.start_tls() |
| 320 |
assert s._sock is not None |
| 321 |
assert s._is_tls is True |
| 322 |
|
| 323 |
|
| 324 |
def test_utils_socket_tls_ssl_init_errors(): |
| 325 |
"""SocketTransport() Start TLS/SSL Initialization Errors.""" |
| 326 |
s = SocketTransport("example.com", 1, secure=True) |
| 327 |
s._sock = _DummySocket() |
| 328 |
|
| 329 |
with mock.patch.object(s, "_build_ssl_context") as mctx: |
| 330 |
ctx = mock.Mock() |
| 331 |
ctx.wrap_socket.side_effect = ssl.SSLError("boom") |
| 332 |
mctx.return_value = ctx |
| 333 |
|
| 334 |
with pytest.raises(AppriseSocketError): |
| 335 |
s.start_tls() |
| 336 |
assert s._sock is None |
| 337 |
|
| 338 |
s._sock = _DummySocket() |
| 339 |
with mock.patch.object(s, "_build_ssl_context") as mctx: |
| 340 |
ctx = mock.Mock() |
| 341 |
ctx.wrap_socket.side_effect = OSError("boom") |
| 342 |
mctx.return_value = ctx |
| 343 |
|
| 344 |
with pytest.raises(AppriseSocketError): |
| 345 |
s.start_tls() |
| 346 |
assert s._sock is None |
| 347 |
|
| 348 |
|
| 349 |
def test_utils_socket_tls_tests(): |
| 350 |
"""SocketTransport() TLS Coverage""" |
| 351 |
s = SocketTransport("example.com", 1, secure=True) |
| 352 |
base_sock = _DummySocket() |
| 353 |
s._sock = base_sock |
| 354 |
|
| 355 |
tls_sock = _DummySocket() |
| 356 |
ctx = mock.Mock() |
| 357 |
ctx.wrap_socket.return_value = tls_sock |
| 358 |
|
| 359 |
with ( |
| 360 |
mock.patch.object(s, "_build_ssl_context", return_value=ctx), |
| 361 |
mock.patch.object( |
| 362 |
s, "_server_hostname_for_tls", return_value="example.com" |
| 363 |
), |
| 364 |
): |
| 365 |
s.start_tls() |
| 366 |
|
| 367 |
assert s._sock is tls_sock |
| 368 |
assert s.is_tls is True |
| 369 |
assert s.local_addr == ("127.0.0.1", 12345) |
| 370 |
assert s.remote_addr == ("203.0.113.10", 6667) |
| 371 |
assert s._rfile is not None |
| 372 |
assert s._wfile is not None |
| 373 |
|
| 374 |
|
| 375 |
def test_utils_socket_connect(): |
| 376 |
"""SocketTransport() Connect() Coverage.""" |
| 377 |
s = SocketTransport("example.com", 6667, secure=False, timeout=(1.0, 2.0)) |
| 378 |
|
| 379 |
dummy = _DummySocket() |
| 380 |
|
| 381 |
with mock.patch("socket.socket", return_value=dummy): |
| 382 |
s.connect() |
| 383 |
assert s.connected is True |
| 384 |
assert s.is_tls is False |
| 385 |
assert s.local_addr == ("127.0.0.1", 12345) |
| 386 |
assert s.remote_addr == ("203.0.113.10", 6667) |
| 387 |
|
| 388 |
|
| 389 |
s = SocketTransport("example.com", 6667, secure=False, timeout=(1.0, 2.0)) |
| 390 |
dummy = _DummySocket() |
| 391 |
dummy.connect = mock.Mock(side_effect=OSError("fail")) |
| 392 |
|
| 393 |
with mock.patch("socket.socket", return_value=dummy): |
| 394 |
with pytest.raises(AppriseSocketError): |
| 395 |
s.connect() |
| 396 |
assert s.connected is False |
| 397 |
|
| 398 |
|
| 399 |
def test_utils_socket_read_nonblocking(): |
| 400 |
"""SocketTransport() Read Nonblocking.""" |
| 401 |
s = SocketTransport("example.com", 1) |
| 402 |
sock = _DummySocket() |
| 403 |
s._sock = sock |
| 404 |
|
| 405 |
|
| 406 |
assert s.read(blocking=False) == b"data" |
| 407 |
|
| 408 |
|
| 409 |
sock._recv_side_effect = BlockingIOError() |
| 410 |
assert s.read(blocking=False) == b"" |
| 411 |
|
| 412 |
|
| 413 |
sock._recv_side_effect = OSError("boom") |
| 414 |
with pytest.raises(AppriseSocketError): |
| 415 |
s.read(blocking=False) |
| 416 |
assert s._sock is None |
| 417 |
|
| 418 |
|
| 419 |
def test_utils_socket_read_blocking_timeout(): |
| 420 |
"""SocketTransport() Read Blocking Timeout.""" |
| 421 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.1)) |
| 422 |
sock = _DummySocket() |
| 423 |
s._sock = sock |
| 424 |
|
| 425 |
|
| 426 |
with mock.patch.object(s, "can_read", return_value=False): |
| 427 |
assert s.read(blocking=True) == b"" |
| 428 |
|
| 429 |
|
| 430 |
s = SocketTransport("example.com", 1, timeout=None) |
| 431 |
sock = _DummySocket() |
| 432 |
s._sock = sock |
| 433 |
|
| 434 |
side_effect = [False, False, True] |
| 435 |
with mock.patch.object(s, "can_read", side_effect=side_effect): |
| 436 |
assert s.read(blocking=True) == b"data" |
| 437 |
|
| 438 |
|
| 439 |
def test_utils_socket_read_blocking_edge_cases() -> None: |
| 440 |
"""SocketTransport() Read Blocking Edge Cases""" |
| 441 |
s = SocketTransport("example.com", 1, timeout=None) |
| 442 |
|
| 443 |
bad_sock = _DummySocket() |
| 444 |
good_sock = _DummySocket() |
| 445 |
good_sock.recv = mock.Mock(return_value=b"data") |
| 446 |
|
| 447 |
s._sock = bad_sock |
| 448 |
s._had_io = True |
| 449 |
|
| 450 |
def _close_side_effect() -> None: |
| 451 |
|
| 452 |
if s._sock is not None: |
| 453 |
s._sock = None |
| 454 |
s._rfile = None |
| 455 |
s._wfile = None |
| 456 |
s._is_tls = False |
| 457 |
s.local_addr = None |
| 458 |
s.remote_addr = None |
| 459 |
|
| 460 |
def _connect_side_effect() -> None: |
| 461 |
s._sock = good_sock |
| 462 |
s._refresh_wrappers() |
| 463 |
|
| 464 |
with ( |
| 465 |
mock.patch.object(s, "close", side_effect=_close_side_effect), |
| 466 |
mock.patch.object(s, "connect", side_effect=_connect_side_effect), |
| 467 |
mock.patch.object(s, "can_read", return_value=None), |
| 468 |
): |
| 469 |
|
| 470 |
assert s.read(blocking=True, retries=1) == b"data" |
| 471 |
|
| 472 |
|
| 473 |
def test_utils_socket_write_with_no_socket(): |
| 474 |
"""SocketTransport() write() - No Socket.""" |
| 475 |
s = SocketTransport("example.com", 1) |
| 476 |
with pytest.raises(AppriseSocketError): |
| 477 |
s.write(b"hi") |
| 478 |
|
| 479 |
s._sock = _DummySocket() |
| 480 |
with pytest.raises(AppriseInvalidData): |
| 481 |
s.write("hi") |
| 482 |
|
| 483 |
|
| 484 |
def test_utils_socket_write_flush(): |
| 485 |
"""SocketTransport() write() - Flush.""" |
| 486 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.2)) |
| 487 |
sock = _DummySocket() |
| 488 |
s._sock = sock |
| 489 |
s._wfile = _DummyFile() |
| 490 |
|
| 491 |
|
| 492 |
with mock.patch.object(s, "can_write", return_value=True): |
| 493 |
assert s.write(b"test", flush=True) == 4 |
| 494 |
assert s._wfile.flushed >= 1 |
| 495 |
|
| 496 |
|
| 497 |
sock = _DummySocket() |
| 498 |
sock.send = mock.Mock(return_value=0) |
| 499 |
s._sock = sock |
| 500 |
with pytest.raises(AppriseSocketError): |
| 501 |
s.write(b"test", timeout=0.1) |
| 502 |
|
| 503 |
|
| 504 |
sock = _DummySocket() |
| 505 |
s._sock = sock |
| 506 |
with ( |
| 507 |
mock.patch.object(s, "can_write", return_value=False), |
| 508 |
pytest.raises(AppriseSocketError), |
| 509 |
): |
| 510 |
s.write(b"test", timeout=0.1) |
| 511 |
|
| 512 |
|
| 513 |
sock = _DummySocket() |
| 514 |
sock._send_side_effect = OSError("boom") |
| 515 |
s._sock = sock |
| 516 |
with pytest.raises(AppriseSocketError): |
| 517 |
s.write(b"test", timeout=0.1) |
| 518 |
assert s._sock is None |
| 519 |
|
| 520 |
|
| 521 |
def test_utils_socket_read_timeouts(): |
| 522 |
"""SocketTransport() Read Timeouts""" |
| 523 |
|
| 524 |
with pytest.raises(AppriseInvalidData): |
| 525 |
SocketTransport("example.com", 1, timeout=(-0.1, 1.0)) |
| 526 |
|
| 527 |
|
| 528 |
with pytest.raises(AppriseInvalidData): |
| 529 |
SocketTransport("example.com", 1, timeout=(1.0, -0.1)) |
| 530 |
|
| 531 |
|
| 532 |
s = SocketTransport("example.com", 1, timeout=(1.0, None)) |
| 533 |
assert s._connect_timeout == pytest.approx(1.0) |
| 534 |
assert s._read_timeout is None |
| 535 |
|
| 536 |
|
| 537 |
def test_utils_socket_exceptions(): |
| 538 |
"""SocketTransport() Exception Testing.""" |
| 539 |
assert issubclass(AppriseSocketError, AppriseException) |
| 540 |
|
| 541 |
|
| 542 |
def test_utils_socket_close_exceptions(): |
| 543 |
"""SocketTransport() Close Exceptions.""" |
| 544 |
|
| 545 |
class _BadShutdownSocket(_DummySocket): |
| 546 |
def shutdown(self, *_args, **_kwargs) -> None: |
| 547 |
raise Exception("shutdown fail") |
| 548 |
|
| 549 |
class _BadFile(_DummyFile): |
| 550 |
def flush(self) -> None: |
| 551 |
raise Exception("flush fail") |
| 552 |
|
| 553 |
def close(self) -> None: |
| 554 |
raise Exception("close fail") |
| 555 |
|
| 556 |
s = SocketTransport("example.com", 1) |
| 557 |
s._sock = _BadShutdownSocket() |
| 558 |
s._wfile = _BadFile() |
| 559 |
s._rfile = _BadFile() |
| 560 |
|
| 561 |
|
| 562 |
s.close() |
| 563 |
|
| 564 |
assert s._sock is None |
| 565 |
assert s._wfile is None |
| 566 |
assert s._rfile is None |
| 567 |
|
| 568 |
|
| 569 |
def test_utils_socket_refresh(): |
| 570 |
"""SocketTransport() Refresh.""" |
| 571 |
s = SocketTransport("example.com", 1) |
| 572 |
s._sock = None |
| 573 |
s._rfile = _DummyFile() |
| 574 |
s._wfile = _DummyFile() |
| 575 |
|
| 576 |
s._refresh_wrappers() |
| 577 |
|
| 578 |
assert s._rfile is None |
| 579 |
assert s._wfile is None |
| 580 |
|
| 581 |
|
| 582 |
def test_utils_socket_can_read_returns_bool(): |
| 583 |
"""SocketTransport() Can Read Returns Bool.""" |
| 584 |
s = SocketTransport("example.com", 1) |
| 585 |
s._sock = _DummySocket() |
| 586 |
|
| 587 |
with mock.patch("select.select", return_value=([s._sock], [], [])): |
| 588 |
assert s.can_read(0.01) is True |
| 589 |
|
| 590 |
with mock.patch("select.select", return_value=([], [], [])): |
| 591 |
assert s.can_read(0.01) is False |
| 592 |
|
| 593 |
|
| 594 |
def test_utils_socket_connect_bind(): |
| 595 |
"""SocketTransport() bind() tests""" |
| 596 |
s = SocketTransport( |
| 597 |
"example.com", 6667, bind_addr="127.0.0.1", bind_port=0 |
| 598 |
) |
| 599 |
dummy = _DummySocket() |
| 600 |
dummy.bind = mock.Mock() |
| 601 |
|
| 602 |
with mock.patch("socket.socket", return_value=dummy): |
| 603 |
s.connect() |
| 604 |
|
| 605 |
dummy.bind.assert_called_once() |
| 606 |
|
| 607 |
|
| 608 |
def test_utils_socket_connect_settimeout_handling(): |
| 609 |
"""SocketTransport() Connect settimeout() handling.""" |
| 610 |
s = SocketTransport("example.com", 6667, timeout=None) |
| 611 |
dummy = _DummySocket() |
| 612 |
dummy.settimeout = mock.Mock() |
| 613 |
|
| 614 |
with mock.patch("socket.socket", return_value=dummy): |
| 615 |
s.connect() |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
assert dummy.settimeout.call_args_list == [mock.call(None)] |
| 620 |
|
| 621 |
|
| 622 |
def test_utils_socket_secure_connect(): |
| 623 |
"""SocketTransport() Secure connect().""" |
| 624 |
s = SocketTransport("example.com", 6667, secure=True) |
| 625 |
dummy = _DummySocket() |
| 626 |
|
| 627 |
with ( |
| 628 |
mock.patch("socket.socket", return_value=dummy), |
| 629 |
mock.patch.object(s, "start_tls", autospec=True) as m, |
| 630 |
): |
| 631 |
s.connect() |
| 632 |
m.assert_called_once() |
| 633 |
|
| 634 |
|
| 635 |
def test_utils_socket_connect_exceptions(): |
| 636 |
"""SocketTransport() Connect Exceptions.""" |
| 637 |
|
| 638 |
class _BadCloseSocket(_DummySocket): |
| 639 |
def connect(self, *_args, **_kwargs) -> None: |
| 640 |
raise OSError("connect fail") |
| 641 |
|
| 642 |
def close(self) -> None: |
| 643 |
raise Exception("close fail") |
| 644 |
|
| 645 |
s = SocketTransport("example.com", 6667, secure=False) |
| 646 |
dummy = _BadCloseSocket() |
| 647 |
|
| 648 |
with ( |
| 649 |
mock.patch("socket.socket", return_value=dummy), |
| 650 |
pytest.raises(AppriseSocketError), |
| 651 |
): |
| 652 |
s.connect() |
| 653 |
|
| 654 |
|
| 655 |
assert s.connected is False |
| 656 |
|
| 657 |
|
| 658 |
def test_utils_socket_write_deadline(): |
| 659 |
"""SocketTransport() Write Deadline.""" |
| 660 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.2)) |
| 661 |
sock = _DummySocket() |
| 662 |
s._sock = sock |
| 663 |
|
| 664 |
with mock.patch.object(s, "can_write", return_value=True) as m_can_write: |
| 665 |
written = s.write(b"test", timeout=0.1, flush=False) |
| 666 |
assert written == 4 |
| 667 |
assert m_can_write.called |
| 668 |
|
| 669 |
|
| 670 |
def test_utils_socket_write_timeout(): |
| 671 |
"""SocketTransport() write() timeout.""" |
| 672 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.2)) |
| 673 |
s._sock = _DummySocket() |
| 674 |
|
| 675 |
|
| 676 |
with mock.patch("time.monotonic", side_effect=[1000.0, 1000.5]): |
| 677 |
with pytest.raises(AppriseSocketError) as e: |
| 678 |
s.write(b"test", timeout=0.1, flush=False) |
| 679 |
assert "Timed out during write" in str(e.value) |
| 680 |
|
| 681 |
s = SocketTransport("example.com", 1, timeout=None) |
| 682 |
sock = _DummySocket() |
| 683 |
s._sock = sock |
| 684 |
s._wfile = _DummyFile() |
| 685 |
|
| 686 |
|
| 687 |
with mock.patch.object(s, "can_write") as m_can_write: |
| 688 |
result = s.write(b"test", flush=True, timeout=None) |
| 689 |
assert result == 4 |
| 690 |
m_can_write.assert_not_called() |
| 691 |
|
| 692 |
|
| 693 |
def test_utils_socket_write_flush_edge_cases(): |
| 694 |
"""SocketTransport() write() flush() edge cases.""" |
| 695 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.2)) |
| 696 |
s._sock = _DummySocket() |
| 697 |
s._wfile = _DummyFile() |
| 698 |
|
| 699 |
with mock.patch.object(s._wfile, "flush") as m: |
| 700 |
with mock.patch.object(s, "can_write", return_value=True): |
| 701 |
written = s.write(b"test", flush=False, timeout=0.1) |
| 702 |
assert written == 4 |
| 703 |
m.assert_not_called() |
| 704 |
|
| 705 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.2)) |
| 706 |
s._sock = _DummySocket() |
| 707 |
s._wfile = None |
| 708 |
|
| 709 |
|
| 710 |
with mock.patch.object(s, "can_write", return_value=True): |
| 711 |
assert s.write(b"test", flush=True, timeout=0.1) == 4 |
| 712 |
|
| 713 |
|
| 714 |
def test_utils_socket_recv_error(): |
| 715 |
"""SocketTransport() recv() errors |
| 716 |
|
| 717 |
Covers blocking read() OSError handling after readiness check. |
| 718 |
""" |
| 719 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.5)) |
| 720 |
sock = _DummySocket() |
| 721 |
sock._recv_side_effect = OSError("recv failed") |
| 722 |
s._sock = sock |
| 723 |
|
| 724 |
with ( |
| 725 |
mock.patch.object(s, "can_read", return_value=True), |
| 726 |
pytest.raises(AppriseSocketError) as e, |
| 727 |
): |
| 728 |
s.read(blocking=True) |
| 729 |
|
| 730 |
assert "recv failed" in str(e.value) |
| 731 |
assert s._sock is None |
| 732 |
|
| 733 |
|
| 734 |
def test_utils_socket_write_empty_payload_does_not_set_had_io() -> None: |
| 735 |
"""SocketTransport() Write Empty Payload Does Not Set Had Io. |
| 736 |
|
| 737 |
Covers the branch where total_sent == 0, so _had_io is not updated. |
| 738 |
""" |
| 739 |
s = SocketTransport("example.com", 1, timeout=None) |
| 740 |
s._sock = _DummySocket() |
| 741 |
s._wfile = _DummyFile() |
| 742 |
|
| 743 |
|
| 744 |
sent = s.write(b"", flush=False, timeout=None) |
| 745 |
assert sent == 0 |
| 746 |
assert s._had_io is False |
| 747 |
|
| 748 |
|
| 749 |
def test_utils_socket_write_handling() -> None: |
| 750 |
"""SocketTransport() Write handling""" |
| 751 |
s = SocketTransport("example.com", 1, timeout=None) |
| 752 |
s._sock = _DummySocket() |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
s._had_io = True |
| 757 |
|
| 758 |
|
| 759 |
s._sock.send = mock.Mock(side_effect=AppriseSocketError("boom")) |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
with ( |
| 764 |
mock.patch.object(s, "_attempt_reconnect", return_value=True), |
| 765 |
pytest.raises(AppriseSocketError) as e, |
| 766 |
): |
| 767 |
s.write(b"test", timeout=None, retries=0) |
| 768 |
|
| 769 |
assert "Socket write failed" in str(e.value) |
| 770 |
|
| 771 |
|
| 772 |
def test_utils_socket_write_reconnect_continue_path_is_reached() -> None: |
| 773 |
"""SocketTransport() Write Reconnect Continue Path Is Reached. |
| 774 |
|
| 775 |
Covers the `continue` path in write() when _attempt_reconnect returns True. |
| 776 |
Trigger AppriseSocketError (not OSError) so we do not reset _had_io via. |
| 777 |
close() before reconnect eligibility is checked. |
| 778 |
""" |
| 779 |
s = SocketTransport("example.com", 1, timeout=None) |
| 780 |
|
| 781 |
bad_sock = _DummySocket() |
| 782 |
|
| 783 |
bad_sock.send = mock.Mock(return_value=0) |
| 784 |
|
| 785 |
good_sock = _DummySocket() |
| 786 |
good_sock.send = mock.Mock(return_value=4) |
| 787 |
|
| 788 |
s._sock = bad_sock |
| 789 |
s._had_io = True |
| 790 |
|
| 791 |
def _connect_side_effect() -> None: |
| 792 |
s._sock = good_sock |
| 793 |
s._refresh_wrappers() |
| 794 |
|
| 795 |
with mock.patch.object(s, "connect", side_effect=_connect_side_effect): |
| 796 |
assert s.write(b"test", timeout=None, retries=1, flush=False) == 4 |
| 797 |
|
| 798 |
|
| 799 |
def test_utils_socket_read_reconnect_continue_path_is_reached() -> None: |
| 800 |
"""SocketTransport() Read Reconnect Continue Path Is Reached. |
| 801 |
|
| 802 |
Covers the `continue` path in read() when _attempt_reconnect returns True. |
| 803 |
Trigger AppriseSocketError (not OSError) so we do not reset _had_io via. |
| 804 |
close() before reconnect eligibility is checked. |
| 805 |
""" |
| 806 |
s = SocketTransport("example.com", 1, timeout=None) |
| 807 |
|
| 808 |
bad_sock = _DummySocket() |
| 809 |
|
| 810 |
bad_sock.recv = mock.Mock(return_value=b"") |
| 811 |
|
| 812 |
good_sock = _DummySocket() |
| 813 |
good_sock.recv = mock.Mock(return_value=b"data") |
| 814 |
|
| 815 |
s._sock = bad_sock |
| 816 |
s._had_io = True |
| 817 |
|
| 818 |
def _connect_side_effect() -> None: |
| 819 |
s._sock = good_sock |
| 820 |
s._refresh_wrappers() |
| 821 |
|
| 822 |
with mock.patch.object(s, "connect", side_effect=_connect_side_effect): |
| 823 |
assert s.read(blocking=False, retries=1) == b"data" |
| 824 |
|
| 825 |
|
| 826 |
def test_utils_socket_attempt_reconnect_retries_zero_returns_false() -> None: |
| 827 |
"""SocketTransport() Attempt Reconnect Retries Zero Returns False. |
| 828 |
|
| 829 |
Covers _attempt_reconnect() early return when retries <= 0. |
| 830 |
""" |
| 831 |
s = SocketTransport("example.com", 1, timeout=None) |
| 832 |
s._had_io = True |
| 833 |
assert ( |
| 834 |
s._attempt_reconnect( |
| 835 |
retries=0, |
| 836 |
action="read", |
| 837 |
exc=Exception("boom"), |
| 838 |
) |
| 839 |
is False |
| 840 |
) |
| 841 |
|
| 842 |
|
| 843 |
def test_utils_socket_read_blocking_connection() -> None: |
| 844 |
"""SocketTransport() Read Blocking Connection.""" |
| 845 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.5)) |
| 846 |
sock = _DummySocket() |
| 847 |
sock.recv = mock.Mock(return_value=b"") |
| 848 |
s._sock = sock |
| 849 |
|
| 850 |
|
| 851 |
s._had_io = True |
| 852 |
|
| 853 |
with ( |
| 854 |
mock.patch.object(s, "can_read", return_value=True), |
| 855 |
pytest.raises(AppriseSocketError) as e, |
| 856 |
): |
| 857 |
s.read(blocking=True, retries=0) |
| 858 |
|
| 859 |
assert "Connection lost during read" in str(e.value) |
| 860 |
|
| 861 |
|
| 862 |
def test_utils_socket_read_blocking() -> None: |
| 863 |
"""SocketTransport() Read Blocking""" |
| 864 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.5)) |
| 865 |
|
| 866 |
bad_sock = _DummySocket() |
| 867 |
bad_sock.recv = mock.Mock(return_value=b"") |
| 868 |
good_sock = _DummySocket() |
| 869 |
good_sock.recv = mock.Mock(return_value=b"data") |
| 870 |
|
| 871 |
s._sock = bad_sock |
| 872 |
s._had_io = True |
| 873 |
|
| 874 |
def _connect_side_effect() -> None: |
| 875 |
s._sock = good_sock |
| 876 |
s._refresh_wrappers() |
| 877 |
|
| 878 |
with ( |
| 879 |
mock.patch.object(s, "can_read", return_value=True), |
| 880 |
mock.patch.object(s, "connect", side_effect=_connect_side_effect), |
| 881 |
): |
| 882 |
assert s.read(blocking=True, retries=1) == b"data" |
| 883 |
|
| 884 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.5)) |
| 885 |
sock = _DummySocket() |
| 886 |
s._sock = sock |
| 887 |
|
| 888 |
with mock.patch.object(s, "can_read", return_value=True): |
| 889 |
assert s.read(blocking=True) == b"data" |
| 890 |
|
| 891 |
s = SocketTransport("example.com", 1, timeout=None) |
| 892 |
s._sock = _DummySocket() |
| 893 |
|
| 894 |
with mock.patch.object(s, "can_read", return_value=None): |
| 895 |
with pytest.raises(AppriseSocketError) as e: |
| 896 |
s.read(blocking=True) |
| 897 |
assert "Socket closed" in str(e.value) |
| 898 |
|
| 899 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.5)) |
| 900 |
s._sock = _DummySocket() |
| 901 |
|
| 902 |
with mock.patch.object(s, "can_read", return_value=False): |
| 903 |
assert s.read(blocking=True) == b"" |
| 904 |
|
| 905 |
|
| 906 |
def test_utils_socket_read_edge_cases(): |
| 907 |
"""SocketTransport() read() edge case tests.""" |
| 908 |
s = SocketTransport("example.com", 1) |
| 909 |
s._sock = None |
| 910 |
assert s.read() == b"" |
| 911 |
|
| 912 |
s = SocketTransport("example.com", 1, timeout=None) |
| 913 |
|
| 914 |
sock = _DummySocket() |
| 915 |
sock.recv = mock.Mock(return_value=b"") |
| 916 |
s._sock = sock |
| 917 |
s._had_io = True |
| 918 |
|
| 919 |
with ( |
| 920 |
mock.patch.object(s, "_attempt_reconnect", return_value=True), |
| 921 |
pytest.raises(AppriseSocketError) as e, |
| 922 |
): |
| 923 |
|
| 924 |
s.read(blocking=False, retries=0) |
| 925 |
|
| 926 |
assert "Socket read failed" in str(e.value) |
| 927 |
|
| 928 |
|
| 929 |
def test_utils_socket_attempt_reconnect_no_io() -> None: |
| 930 |
"""SocketTransport() _attempt_reconnect() returns False""" |
| 931 |
s = SocketTransport("example.com", 1, timeout=None) |
| 932 |
|
| 933 |
|
| 934 |
s._had_io = False |
| 935 |
|
| 936 |
with ( |
| 937 |
mock.patch.object(s, "close") as m_close, |
| 938 |
mock.patch.object(s, "connect") as m_connect, |
| 939 |
): |
| 940 |
assert ( |
| 941 |
s._attempt_reconnect( |
| 942 |
retries=1, |
| 943 |
action="read", |
| 944 |
exc=Exception("boom"), |
| 945 |
) |
| 946 |
is False |
| 947 |
) |
| 948 |
|
| 949 |
|
| 950 |
m_close.assert_not_called() |
| 951 |
m_connect.assert_not_called() |
| 952 |
|
| 953 |
|
| 954 |
def test_utils_socket_attempt_reconnect_connect_exception() -> None: |
| 955 |
"""SocketTransport() _attempt_reconnect() handles exception.""" |
| 956 |
s = SocketTransport("example.com", 1, timeout=None) |
| 957 |
|
| 958 |
|
| 959 |
s._had_io = True |
| 960 |
|
| 961 |
with ( |
| 962 |
mock.patch.object(s, "close") as m_close, |
| 963 |
mock.patch.object( |
| 964 |
s, "connect", side_effect=Exception("nope") |
| 965 |
) as m_connect, |
| 966 |
): |
| 967 |
assert ( |
| 968 |
s._attempt_reconnect( |
| 969 |
retries=1, |
| 970 |
action="write", |
| 971 |
exc=Exception("boom"), |
| 972 |
) |
| 973 |
is False |
| 974 |
) |
| 975 |
|
| 976 |
m_close.assert_called_once() |
| 977 |
m_connect.assert_called_once() |
| 978 |
|
| 979 |
|
| 980 |
def test_utils_socket_ssl_read_blocking() -> None: |
| 981 |
"""SocketTransport() read() blocking""" |
| 982 |
s = SocketTransport("example.com", 1, timeout=None) |
| 983 |
sock = _DummySocket() |
| 984 |
|
| 985 |
|
| 986 |
sock.recv = mock.Mock(side_effect=[ssl.SSLWantReadError(), b"data"]) |
| 987 |
s._sock = sock |
| 988 |
|
| 989 |
|
| 990 |
with mock.patch.object(s, "can_read", return_value=True): |
| 991 |
assert s.read(blocking=True, timeout=None) == b"data" |
| 992 |
|
| 993 |
|
| 994 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.5)) |
| 995 |
sock = _DummySocket() |
| 996 |
|
| 997 |
|
| 998 |
sock.recv = mock.Mock(side_effect=ssl.SSLWantWriteError()) |
| 999 |
s._sock = sock |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
with mock.patch.object(s, "can_read", side_effect=[True, False]): |
| 1004 |
assert s.read(blocking=True, timeout=0.5) == b"" |
| 1005 |
|
| 1006 |
s = SocketTransport("example.com", 1, timeout=(1.0, 0.5)) |
| 1007 |
sock = _DummySocket() |
| 1008 |
|
| 1009 |
|
| 1010 |
sock.recv = mock.Mock(side_effect=[ssl.SSLWantWriteError(), b"data"]) |
| 1011 |
s._sock = sock |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
with mock.patch.object(s, "can_read", side_effect=[True, True]): |
| 1017 |
assert s.read(blocking=True, timeout=0.5) == b"data" |
| 1018 |
|
| 1019 |
s = SocketTransport("example.com", 1, timeout=None) |
| 1020 |
bad_sock = _DummySocket() |
| 1021 |
|
| 1022 |
|
| 1023 |
bad_sock.recv = mock.Mock(return_value=b"") |
| 1024 |
|
| 1025 |
good_sock = _DummySocket() |
| 1026 |
|
| 1027 |
good_sock.recv = mock.Mock(return_value=b"") |
| 1028 |
|
| 1029 |
s._sock = bad_sock |
| 1030 |
s._had_io = True |
| 1031 |
|
| 1032 |
def _connect_side_effect() -> None: |
| 1033 |
s._sock = good_sock |
| 1034 |
s._refresh_wrappers() |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
with ( |
| 1039 |
mock.patch.object(s, "can_read", return_value=True), |
| 1040 |
mock.patch.object(s, "connect", side_effect=_connect_side_effect), |
| 1041 |
): |
| 1042 |
with pytest.raises(AppriseSocketError) as e: |
| 1043 |
s.read(blocking=True, retries=1) |
| 1044 |
assert "Connection lost during read" in str(e.value) |
| 1045 |
|
| 1046 |
s = SocketTransport("example.com", 1, timeout=None) |
| 1047 |
|
| 1048 |
bad_sock = _DummySocket() |
| 1049 |
bad_sock.recv = mock.Mock(return_value=b"") |
| 1050 |
s._sock = bad_sock |
| 1051 |
s._had_io = True |
| 1052 |
|
| 1053 |
good_sock = _DummySocket() |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
good_sock.recv = mock.Mock(side_effect=[BlockingIOError(), b"data"]) |
| 1058 |
|
| 1059 |
def _connect_side_effect() -> None: |
| 1060 |
s._sock = good_sock |
| 1061 |
s._refresh_wrappers() |
| 1062 |
|
| 1063 |
|
| 1064 |
with ( |
| 1065 |
mock.patch.object(s, "can_read", return_value=True), |
| 1066 |
mock.patch.object(s, "connect", side_effect=_connect_side_effect), |
| 1067 |
): |
| 1068 |
assert s.read(blocking=True, retries=1) == b"data" |