File: test_tls.py

package info (click to toggle)
aiosmtplib 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 572 kB
  • sloc: python: 5,516; makefile: 20; sh: 6
file content (414 lines) | stat: -rw-r--r-- 12,657 bytes parent folder | download | duplicates (2)
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
"""
TLS and STARTTLS handling.
"""

import copy
import ssl

import pytest

from aiosmtplib import (
    SMTP,
    SMTPConnectError,
    SMTPException,
    SMTPResponseException,
    SMTPServerDisconnected,
    SMTPStatus,
)

from .smtpd import (
    mock_response_ehlo_minimal,
    mock_response_tls_not_available,
    mock_response_tls_ready_disconnect,
    mock_response_unrecognized_command,
)


@pytest.mark.smtpd_options(tls=True)
async def test_tls_connection(smtp_client: SMTP) -> None:
    """
    Use an explicit connect/quit here, as other tests use the context manager.
    """
    await smtp_client.connect(use_tls=True)
    assert smtp_client.is_connected

    await smtp_client.quit()
    assert not smtp_client.is_connected


@pytest.mark.smtpd_options(tls=False)
async def test_starttls(smtp_client: SMTP) -> None:
    async with smtp_client:
        response = await smtp_client.starttls()

        assert response.code == SMTPStatus.ready

        # Make sure our state has been cleared
        assert not smtp_client.esmtp_extensions
        assert not smtp_client.supported_auth_methods
        assert not smtp_client.supports_esmtp

        # Make sure our connection was actually upgraded. ssl protocol transport is
        # private in UVloop, so just check the class name.
        assert "SSL" in type(smtp_client.transport).__name__

        response = await smtp_client.ehlo()
        assert response.code == SMTPStatus.completed


async def test_starttls_init_kwarg(
    hostname: str, smtpd_server_port: int, client_tls_context: ssl.SSLContext
) -> None:
    smtp_client = SMTP(
        hostname=hostname,
        port=smtpd_server_port,
        start_tls=True,
        tls_context=client_tls_context,
        timeout=1.0,
    )

    async with smtp_client:
        # Make sure our connection was actually upgraded. ssl protocol transport is
        # private in UVloop, so just check the class name.
        assert "SSL" in type(smtp_client.transport).__name__


@pytest.mark.smtpd_options(tls=False)
async def test_starttls_connect_kwarg(smtp_client: SMTP) -> None:
    await smtp_client.connect(start_tls=True)

    # Make sure our connection was actually upgraded. ssl protocol transport is
    # private in UVloop, so just check the class name.
    assert "SSL" in type(smtp_client.transport).__name__

    await smtp_client.quit()


async def test_starttls_auto(
    hostname: str, smtpd_server_port: int, client_tls_context: ssl.SSLContext
) -> None:
    smtp_client = SMTP(
        hostname=hostname,
        port=smtpd_server_port,
        start_tls=None,
        tls_context=client_tls_context,
        timeout=1.0,
    )

    async with smtp_client:
        # Make sure our connection was actually upgraded. ssl protocol transport is
        # private in UVloop, so just check the class name.
        assert "SSL" in type(smtp_client.transport).__name__


async def test_starttls_auto_connect_kwarg(
    hostname: str,
    smtpd_server_port: int,
    client_tls_context: ssl.SSLContext,
) -> None:
    smtp_client = SMTP(
        hostname=hostname,
        port=smtpd_server_port,
        start_tls=False,
        tls_context=client_tls_context,
        timeout=1.0,
    )

    await smtp_client.connect(start_tls=None)

    # Make sure our connection was actually upgraded. ssl protocol transport is
    # private in UVloop, so just check the class name.
    assert "SSL" in type(smtp_client.transport).__name__

    await smtp_client.quit()


@pytest.mark.smtp_client_options(start_tls=None)
@pytest.mark.smtpd_options(tls=False)
@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_ehlo_minimal)
async def test_starttls_auto_connect_not_supported(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.ehlo()

        # Make sure our connection was nul upgraded. ssl protocol transport is
        # private in UVloop, so just check the class name.
        assert "SSL" not in type(smtp_client.transport).__name__


@pytest.mark.smtpd_options(tls=False)
async def test_starttls_with_explicit_server_hostname(
    smtp_client: SMTP,
    hostname: str,
) -> None:
    async with smtp_client:
        await smtp_client.ehlo()

        await smtp_client.starttls(server_hostname=hostname)


@pytest.mark.smtpd_options(tls=False)
@pytest.mark.smtpd_mocks(smtp_EHLO=mock_response_ehlo_minimal)
async def test_starttls_not_supported(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.ehlo()

        with pytest.raises(SMTPException, match="STARTTLS extension not supported"):
            await smtp_client.starttls()


@pytest.mark.smtpd_options(tls=False)
@pytest.mark.smtpd_mocks(smtp_STARTTLS=mock_response_tls_not_available)
async def test_starttls_advertised_but_not_supported(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.ehlo()

        with pytest.raises(SMTPException):
            await smtp_client.starttls()


@pytest.mark.smtpd_options(tls=False)
@pytest.mark.smtpd_mocks(smtp_STARTTLS=mock_response_tls_ready_disconnect)
async def test_starttls_disconnect_before_upgrade(smtp_client: SMTP) -> None:
    async with smtp_client:
        with pytest.raises(SMTPServerDisconnected):
            await smtp_client.starttls()


@pytest.mark.smtpd_options(tls=False)
@pytest.mark.smtpd_mocks(smtp_STARTTLS=mock_response_unrecognized_command)
async def test_starttls_invalid_responses(smtp_client: SMTP) -> None:
    async with smtp_client:
        await smtp_client.ehlo()

        old_extensions = copy.copy(smtp_client.esmtp_extensions)

        with pytest.raises(SMTPResponseException) as exception_info:
            await smtp_client.starttls()

        assert exception_info.value.code == SMTPStatus.unrecognized_command
        # Make sure our state has been _not_ been cleared
        assert smtp_client.esmtp_extensions == old_extensions
        assert smtp_client.supports_esmtp is True

        # Make sure our connection was not upgraded. ssl protocol transport is
        # private in UVloop, so just check the class name.
        assert "SSL" not in type(smtp_client.transport).__name__


@pytest.mark.smtpd_options(tls=False)
async def test_starttls_with_client_cert(
    hostname: str,
    smtpd_server_port: int,
    ca_cert_path: str,
    valid_cert_path: str,
    valid_key_path: str,
) -> None:
    smtp_client = SMTP(
        hostname=hostname, port=smtpd_server_port, start_tls=False, timeout=1.0
    )
    async with smtp_client:
        response = await smtp_client.starttls(
            client_cert=valid_cert_path,
            client_key=valid_key_path,
            cert_bundle=ca_cert_path,
        )

        assert response.code == SMTPStatus.ready
        assert smtp_client.client_cert == valid_cert_path
        assert smtp_client.client_key == valid_key_path
        assert smtp_client.cert_bundle == ca_cert_path


@pytest.mark.smtpd_options(tls=False)
async def test_starttls_with_invalid_client_cert(
    hostname: str,
    smtpd_server_port: int,
    invalid_cert_path: str,
    invalid_key_path: str,
) -> None:
    smtp_client = SMTP(
        hostname=hostname, port=smtpd_server_port, start_tls=False, timeout=1.0
    )
    async with smtp_client:
        with pytest.raises(ssl.SSLError):
            await smtp_client.starttls(
                client_cert=invalid_cert_path,
                client_key=invalid_key_path,
                cert_bundle=invalid_cert_path,
            )


@pytest.mark.smtpd_options(tls=False)
async def test_starttls_with_invalid_client_cert_no_validate(
    hostname: str,
    smtpd_server_port: int,
    invalid_cert_path: str,
    invalid_key_path: str,
) -> None:
    smtp_client = SMTP(
        hostname=hostname, port=smtpd_server_port, start_tls=False, timeout=1.0
    )
    async with smtp_client:
        response = await smtp_client.starttls(
            client_cert=invalid_cert_path,
            client_key=invalid_key_path,
            cert_bundle=invalid_cert_path,
            validate_certs=False,
            timeout=1.0,
        )

        assert response.code == SMTPStatus.ready
        assert smtp_client.client_cert == invalid_cert_path
        assert smtp_client.client_key == invalid_key_path
        assert smtp_client.cert_bundle == invalid_cert_path


@pytest.mark.smtpd_options(tls=False)
async def test_starttls_cert_error(
    hostname: str,
    smtpd_server_port: int,
    unknown_client_tls_context: ssl.SSLContext,
) -> None:
    smtp_client = SMTP(
        hostname=hostname,
        port=smtpd_server_port,
        start_tls=False,
        tls_context=unknown_client_tls_context,
        timeout=1.0,
    )
    async with smtp_client:
        with pytest.raises(ssl.SSLError):
            await smtp_client.starttls()


@pytest.mark.smtpd_options(tls=False)
async def test_starttls_already_upgraded_error(
    hostname: str,
    smtpd_server_port: int,
    client_tls_context: ssl.SSLContext,
) -> None:
    smtp_client = SMTP(
        hostname=hostname,
        port=smtpd_server_port,
        tls_context=client_tls_context,
        timeout=1.0,
    )
    async with smtp_client:
        with pytest.raises(SMTPException, match="Connection already using TLS"):
            await smtp_client.starttls()


@pytest.mark.smtpd_options(tls=False)
async def test_starttls_cert_no_validate(hostname: str, smtpd_server_port: int) -> None:
    smtp_client = SMTP(
        hostname=hostname,
        port=smtpd_server_port,
        start_tls=False,
        validate_certs=False,
        timeout=1.0,
    )
    async with smtp_client:
        response = await smtp_client.starttls()

    assert response.code == SMTPStatus.ready


@pytest.mark.smtpd_options(tls=True)
@pytest.mark.smtp_client_options(use_tls=True)
async def test_tls_get_transport_info(
    smtp_client: SMTP, smtpd_server_port: int
) -> None:
    async with smtp_client:
        compression = smtp_client.get_transport_info("compression")
        assert compression is None  # Compression is not used here

        peername = smtp_client.get_transport_info("peername")
        assert peername[0] in ("127.0.0.1", "::1")  # IP v4 and 6
        assert peername[1] == smtpd_server_port

        sock = smtp_client.get_transport_info("socket")
        assert sock is not None

        sockname = smtp_client.get_transport_info("sockname")
        assert sockname is not None

        cipher = smtp_client.get_transport_info("cipher")
        assert cipher is not None

        peercert = smtp_client.get_transport_info("peercert")
        assert peercert is not None

        sslcontext = smtp_client.get_transport_info("sslcontext")
        assert sslcontext is not None

        sslobj = smtp_client.get_transport_info("ssl_object")
        assert sslobj is not None


@pytest.mark.smtpd_options(tls=False)
async def test_tls_smtp_connect_to_non_tls_server(
    smtp_client: SMTP,
    smtpd_server_port: int,
) -> None:
    with pytest.raises(SMTPConnectError):
        await smtp_client.connect(use_tls=True, port=smtpd_server_port)
    assert not smtp_client.is_connected


@pytest.mark.smtpd_options(tls=True)
async def test_tls_connection_with_existing_sslcontext(
    smtp_client: SMTP,
    client_tls_context: ssl.SSLContext,
) -> None:
    await smtp_client.connect(use_tls=True, tls_context=client_tls_context)
    assert smtp_client.is_connected

    assert smtp_client.tls_context is client_tls_context

    await smtp_client.quit()
    assert not smtp_client.is_connected


@pytest.mark.smtpd_options(tls=True)
async def test_tls_connection_with_client_cert(
    hostname: str,
    smtpd_server_port: int,
    ca_cert_path: str,
    valid_cert_path: str,
    valid_key_path: str,
) -> None:
    smtp_client = SMTP(
        hostname=hostname, port=smtpd_server_port, use_tls=True, timeout=1.0
    )
    await smtp_client.connect(
        hostname=hostname,
        client_cert=valid_cert_path,
        client_key=valid_key_path,
        cert_bundle=ca_cert_path,
    )
    assert smtp_client.is_connected

    await smtp_client.quit()
    assert not smtp_client.is_connected


@pytest.mark.smtpd_options(tls=True)
async def test_tls_connection_with_cert_error(
    hostname: str,
    smtpd_server_port: int,
) -> None:
    smtp_client = SMTP(
        hostname=hostname, port=smtpd_server_port, use_tls=True, timeout=1.0
    )

    with pytest.raises(SMTPConnectError) as exception_info:
        await smtp_client.connect()

    assert "CERTIFICATE" in str(exception_info.value).upper()


async def test_starttls_when_disconnected() -> None:
    client = SMTP(timeout=1.0)

    with pytest.raises(SMTPServerDisconnected):
        await client.starttls()