Debian Codegraph @

tests/test_timeouts.py

raw
1 """
2 Timeout tests.
3 """
4
5 import asyncio
6 import socket
7 import ssl
8
9 import pytest
10
11 from aiosmtplib import (
12 SMTP,
13 SMTPConnectTimeoutError,
14 SMTPServerDisconnected,
15 SMTPTimeoutError,
16 )
17 from aiosmtplib.protocol import SMTPProtocol
18
19 from .compat import cleanup_server
20 from .smtpd import mock_response_delayed_ok, mock_response_delayed_read
21
22
23 @pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_delayed_ok)
24 async def test_command_timeout_error(smtp_client: SMTP) -> None:
25 await smtp_client.connect()
26
27 with pytest.raises(SMTPTimeoutError):
28 await smtp_client.ehlo(hostname="example.com", timeout=0.0)
29
30 # A read timeout closes the connection, since the protocol is now desynced.
31 assert smtp_client.protocol is None
32 assert smtp_client.transport is None
33 assert not smtp_client.is_connected
34
35
36 @pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_delayed_ok)
37 async def test_command_timeout_closes_connection(smtp_client: SMTP) -> None:
38 await smtp_client.connect()
39
40 with pytest.raises(SMTPTimeoutError):
41 await smtp_client.ehlo(hostname="example.com", timeout=0.0)
42
43 # Reusing a timed-out connection must fail cleanly rather than mispair a
44 # late server response with the next command.
45 with pytest.raises(SMTPServerDisconnected):
46 await smtp_client.noop()
47
48
49 @pytest.mark.smtpd_mocks(smtp_DATA=mock_response_delayed_ok)
50 async def test_data_timeout_error(smtp_client: SMTP) -> None:
51 await smtp_client.connect()
52 await smtp_client.ehlo()
53 await smtp_client.mail("[email protected]")
54 await smtp_client.rcpt("[email protected]")
55 with pytest.raises(SMTPTimeoutError):
56 await smtp_client.data("HELLO WORLD", timeout=0.0)
57
58 assert smtp_client.protocol is None
59 assert smtp_client.transport is None
60 assert not smtp_client.is_connected
61
62
63 @pytest.mark.smtpd_mocks(_handle_client=mock_response_delayed_ok)
64 async def test_timeout_error_on_connect(smtp_client: SMTP) -> None:
65 with pytest.raises(SMTPTimeoutError):
66 await smtp_client.connect(timeout=0.0)
67
68 assert smtp_client.transport is None
69 assert smtp_client.protocol is None
70
71
72 @pytest.mark.smtpd_mocks(_handle_client=mock_response_delayed_read)
73 async def test_timeout_on_initial_read(smtp_client: SMTP) -> None:
74 with pytest.raises(SMTPTimeoutError):
75 # We need to use a timeout > 0 here to avoid timing out on connect
76 await smtp_client.connect(timeout=0.01)
77
78
79 @pytest.mark.smtpd_mocks(smtp_STARTTLS=mock_response_delayed_ok)
80 async def test_timeout_on_starttls(smtp_client: SMTP) -> None:
81 await smtp_client.connect()
82 await smtp_client.ehlo()
83
84 with pytest.raises(SMTPTimeoutError):
85 await smtp_client.starttls(timeout=0.0)
86
87 assert smtp_client.protocol is None
88 assert smtp_client.transport is None
89 assert not smtp_client.is_connected
90
91
92 async def test_protocol_read_response_with_timeout_times_out(
93 echo_server: asyncio.AbstractServer,
94 hostname: str,
95 echo_server_port: int,
96 ) -> None:
97 event_loop = asyncio.get_running_loop()
98
99 connect_future = event_loop.create_connection(
100 SMTPProtocol, host=hostname, port=echo_server_port
101 )
102
103 transport, protocol = await asyncio.wait_for(connect_future, timeout=1.0)
104
105 with pytest.raises(SMTPTimeoutError) as exc:
106 await protocol.read_response(timeout=0.0) # type: ignore
107
108 transport.close()
109
110 assert str(exc.value) == "Timed out waiting for server response"
111
112
113 async def test_connect_timeout_error(hostname: str, unused_tcp_port: int) -> None:
114 client = SMTP(hostname=hostname, port=unused_tcp_port, timeout=0.0)
115
116 with pytest.raises(SMTPConnectTimeoutError) as exc:
117 await client.connect()
118
119 expected_message = f"Timed out connecting to {hostname} on port {unused_tcp_port}"
120 assert str(exc.value) == expected_message
121
122
123 async def test_server_disconnected_error_after_connect_timeout(
124 hostname: str,
125 unused_tcp_port: int,
126 sender_str: str,
127 recipient_str: str,
128 message_str: str,
129 ) -> None:
130 client = SMTP(hostname=hostname, port=unused_tcp_port)
131
132 with pytest.raises(SMTPConnectTimeoutError):
133 await client.connect(timeout=0.0)
134
135 with pytest.raises(SMTPServerDisconnected):
136 await client.sendmail(sender_str, [recipient_str], message_str)
137
138
139 async def test_protocol_timeout_on_starttls(
140 bind_address: str,
141 hostname: str,
142 client_tls_context: ssl.SSLContext,
143 ) -> None:
144 event_loop = asyncio.get_running_loop()
145
146 async def client_connected(
147 reader: asyncio.StreamReader, writer: asyncio.StreamWriter
148 ) -> None:
149 await asyncio.sleep(1.0)
150
151 server = await asyncio.start_server(
152 client_connected, host=bind_address, port=0, family=socket.AF_INET
153 )
154 server_port = server.sockets[0].getsockname()[1] if server.sockets else 0
155
156 connect_future = event_loop.create_connection(
157 SMTPProtocol, host=hostname, port=server_port
158 )
159
160 _, protocol = await asyncio.wait_for(connect_future, timeout=1.0)
161
162 with pytest.raises(SMTPTimeoutError):
163 # STARTTLS timeout must be > 0
164 await protocol.start_tls(client_tls_context, timeout=0.00001) # type: ignore
165
166 server.close()
167 await cleanup_server(server)
168
169
170 async def test_protocol_connection_aborted_on_starttls(
171 hostname: str,
172 smtpd_server_port: int,
173 client_tls_context: ssl.SSLContext,
174 monkeypatch: pytest.MonkeyPatch,
175 ) -> None:
176 event_loop = asyncio.get_running_loop()
177
178 connect_future = event_loop.create_connection(
179 SMTPProtocol, host=hostname, port=smtpd_server_port
180 )
181 transport, protocol = await asyncio.wait_for(connect_future, timeout=1.0)
182
183 def mock_start_tls(*args, **kwargs) -> None:
184 raise ConnectionAbortedError("Connection was aborted")
185
186 monkeypatch.setattr(event_loop, "start_tls", mock_start_tls)
187
188 with pytest.raises(SMTPTimeoutError):
189 await protocol.start_tls(client_tls_context)
190
191 transport.close()