diff options
Diffstat (limited to 'test/with_dummyserver/test_socketlevel.py')
| -rw-r--r-- | test/with_dummyserver/test_socketlevel.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/test/with_dummyserver/test_socketlevel.py b/test/with_dummyserver/test_socketlevel.py index 0a6a8ae3..809c6852 100644 --- a/test/with_dummyserver/test_socketlevel.py +++ b/test/with_dummyserver/test_socketlevel.py @@ -2,6 +2,7 @@ # rather than the socket level-ness of it. from __future__ import annotations +import contextlib import errno import io import os @@ -966,6 +967,63 @@ class TestSocketClosing(SocketDummyServerTestCase): assert pool.pool.qsize() == 1 assert response.connection is None + def test_socket_close_socket_then_file(self) -> None: + def consume_ssl_socket(listener: socket.socket) -> None: + try: + with listener.accept()[0] as sock, original_ssl_wrap_socket( + sock, + server_side=True, + keyfile=DEFAULT_CERTS["keyfile"], + certfile=DEFAULT_CERTS["certfile"], + ca_certs=DEFAULT_CA, + ) as ssl_sock: + consume_socket(ssl_sock) + except (ConnectionResetError, ConnectionAbortedError, OSError): + pass + + self._start_server(consume_ssl_socket) + with socket.create_connection( + (self.host, self.port) + ) as sock, contextlib.closing( + ssl_wrap_socket(sock, server_hostname=self.host, ca_certs=DEFAULT_CA) + ) as ssl_sock, ssl_sock.makefile( + "rb" + ) as f: + ssl_sock.close() + f.close() + # SecureTransport is supposed to raise OSError but raises + # ssl.SSLError when closed because ssl_sock.context is None + with pytest.raises((OSError, ssl.SSLError)): + ssl_sock.sendall(b"hello") + assert ssl_sock.fileno() == -1 + + def test_socket_close_stays_open_with_makefile_open(self) -> None: + def consume_ssl_socket(listener: socket.socket) -> None: + try: + with listener.accept()[0] as sock, original_ssl_wrap_socket( + sock, + server_side=True, + keyfile=DEFAULT_CERTS["keyfile"], + certfile=DEFAULT_CERTS["certfile"], + ca_certs=DEFAULT_CA, + ) as ssl_sock: + consume_socket(ssl_sock) + except (ConnectionResetError, ConnectionAbortedError, OSError): + pass + + self._start_server(consume_ssl_socket) + with socket.create_connection( + (self.host, self.port) + ) as sock, contextlib.closing( + ssl_wrap_socket(sock, server_hostname=self.host, ca_certs=DEFAULT_CA) + ) as ssl_sock, ssl_sock.makefile( + "rb" + ): + ssl_sock.close() + ssl_sock.close() + ssl_sock.sendall(b"hello") + assert ssl_sock.fileno() > 0 + class TestProxyManager(SocketDummyServerTestCase): def test_simple(self) -> None: |
