1 | n/a | import unittest |
---|
2 | n/a | from test import support |
---|
3 | n/a | import smtplib |
---|
4 | n/a | import socket |
---|
5 | n/a | |
---|
6 | n/a | ssl = support.import_module("ssl") |
---|
7 | n/a | |
---|
8 | n/a | support.requires("network") |
---|
9 | n/a | |
---|
10 | n/a | def check_ssl_verifiy(host, port): |
---|
11 | n/a | context = ssl.create_default_context() |
---|
12 | n/a | with socket.create_connection((host, port)) as sock: |
---|
13 | n/a | try: |
---|
14 | n/a | sock = context.wrap_socket(sock, server_hostname=host) |
---|
15 | n/a | except Exception: |
---|
16 | n/a | return False |
---|
17 | n/a | else: |
---|
18 | n/a | sock.close() |
---|
19 | n/a | return True |
---|
20 | n/a | |
---|
21 | n/a | |
---|
22 | n/a | class SmtpTest(unittest.TestCase): |
---|
23 | n/a | testServer = 'smtp.gmail.com' |
---|
24 | n/a | remotePort = 587 |
---|
25 | n/a | |
---|
26 | n/a | def test_connect_starttls(self): |
---|
27 | n/a | support.get_attribute(smtplib, 'SMTP_SSL') |
---|
28 | n/a | context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
---|
29 | n/a | with support.transient_internet(self.testServer): |
---|
30 | n/a | server = smtplib.SMTP(self.testServer, self.remotePort) |
---|
31 | n/a | try: |
---|
32 | n/a | server.starttls(context=context) |
---|
33 | n/a | except smtplib.SMTPException as e: |
---|
34 | n/a | if e.args[0] == 'STARTTLS extension not supported by server.': |
---|
35 | n/a | unittest.skip(e.args[0]) |
---|
36 | n/a | else: |
---|
37 | n/a | raise |
---|
38 | n/a | server.ehlo() |
---|
39 | n/a | server.quit() |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | class SmtpSSLTest(unittest.TestCase): |
---|
43 | n/a | testServer = 'smtp.gmail.com' |
---|
44 | n/a | remotePort = 465 |
---|
45 | n/a | |
---|
46 | n/a | def test_connect(self): |
---|
47 | n/a | support.get_attribute(smtplib, 'SMTP_SSL') |
---|
48 | n/a | with support.transient_internet(self.testServer): |
---|
49 | n/a | server = smtplib.SMTP_SSL(self.testServer, self.remotePort) |
---|
50 | n/a | server.ehlo() |
---|
51 | n/a | server.quit() |
---|
52 | n/a | |
---|
53 | n/a | def test_connect_default_port(self): |
---|
54 | n/a | support.get_attribute(smtplib, 'SMTP_SSL') |
---|
55 | n/a | with support.transient_internet(self.testServer): |
---|
56 | n/a | server = smtplib.SMTP_SSL(self.testServer) |
---|
57 | n/a | server.ehlo() |
---|
58 | n/a | server.quit() |
---|
59 | n/a | |
---|
60 | n/a | def test_connect_using_sslcontext(self): |
---|
61 | n/a | context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
---|
62 | n/a | support.get_attribute(smtplib, 'SMTP_SSL') |
---|
63 | n/a | with support.transient_internet(self.testServer): |
---|
64 | n/a | server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context) |
---|
65 | n/a | server.ehlo() |
---|
66 | n/a | server.quit() |
---|
67 | n/a | |
---|
68 | n/a | def test_connect_using_sslcontext_verified(self): |
---|
69 | n/a | with support.transient_internet(self.testServer): |
---|
70 | n/a | can_verify = check_ssl_verifiy(self.testServer, self.remotePort) |
---|
71 | n/a | if not can_verify: |
---|
72 | n/a | self.skipTest("SSL certificate can't be verified") |
---|
73 | n/a | |
---|
74 | n/a | support.get_attribute(smtplib, 'SMTP_SSL') |
---|
75 | n/a | context = ssl.create_default_context() |
---|
76 | n/a | with support.transient_internet(self.testServer): |
---|
77 | n/a | server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context) |
---|
78 | n/a | server.ehlo() |
---|
79 | n/a | server.quit() |
---|
80 | n/a | |
---|
81 | n/a | |
---|
82 | n/a | if __name__ == "__main__": |
---|
83 | n/a | unittest.main() |
---|