| 1 |
import unittest |
| 2 |
from test import support |
| 3 |
from test.support import import_helper |
| 4 |
from test.support import socket_helper |
| 5 |
import os |
| 6 |
import smtplib |
| 7 |
import socket |
| 8 |
|
| 9 |
ssl = import_helper.import_module("ssl") |
| 10 |
|
| 11 |
support.requires("network") |
| 12 |
|
| 13 |
SMTP_TEST_SERVER = os.getenv('CPYTHON_TEST_SMTP_SERVER', 'smtp.gmail.com') |
| 14 |
|
| 15 |
def check_ssl_verifiy(host, port): |
| 16 |
context = ssl.create_default_context() |
| 17 |
with socket.create_connection((host, port)) as sock: |
| 18 |
try: |
| 19 |
sock = context.wrap_socket(sock, server_hostname=host) |
| 20 |
except Exception: |
| 21 |
return False |
| 22 |
else: |
| 23 |
sock.close() |
| 24 |
return True |
| 25 |
|
| 26 |
|
| 27 |
class SmtpTest(unittest.TestCase): |
| 28 |
testServer = SMTP_TEST_SERVER |
| 29 |
remotePort = 587 |
| 30 |
|
| 31 |
def test_connect_starttls(self): |
| 32 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 33 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 34 |
context.check_hostname = False |
| 35 |
context.verify_mode = ssl.CERT_NONE |
| 36 |
with socket_helper.transient_internet(self.testServer): |
| 37 |
server = smtplib.SMTP(self.testServer, self.remotePort) |
| 38 |
try: |
| 39 |
server.starttls(context=context) |
| 40 |
except smtplib.SMTPException as e: |
| 41 |
if e.args[0] == 'STARTTLS extension not supported by server.': |
| 42 |
unittest.skip(e.args[0]) |
| 43 |
else: |
| 44 |
raise |
| 45 |
server.ehlo() |
| 46 |
server.quit() |
| 47 |
|
| 48 |
def test_connect_host_port_starttls(self): |
| 49 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 50 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 51 |
context.check_hostname = False |
| 52 |
context.verify_mode = ssl.CERT_NONE |
| 53 |
with socket_helper.transient_internet(self.testServer): |
| 54 |
server = smtplib.SMTP(f'{self.testServer}:{self.remotePort}') |
| 55 |
try: |
| 56 |
server.starttls(context=context) |
| 57 |
except smtplib.SMTPException as e: |
| 58 |
if e.args[0] == 'STARTTLS extension not supported by server.': |
| 59 |
unittest.skip(e.args[0]) |
| 60 |
else: |
| 61 |
raise |
| 62 |
server.ehlo() |
| 63 |
server.quit() |
| 64 |
|
| 65 |
def test_explicit_connect_starttls(self): |
| 66 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 67 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 68 |
context.check_hostname = False |
| 69 |
context.verify_mode = ssl.CERT_NONE |
| 70 |
with socket_helper.transient_internet(self.testServer): |
| 71 |
server = smtplib.SMTP() |
| 72 |
server.connect(self.testServer, self.remotePort) |
| 73 |
try: |
| 74 |
server.starttls(context=context) |
| 75 |
except smtplib.SMTPException as e: |
| 76 |
if e.args[0] == 'STARTTLS extension not supported by server.': |
| 77 |
unittest.skip(e.args[0]) |
| 78 |
else: |
| 79 |
raise |
| 80 |
server.ehlo() |
| 81 |
server.quit() |
| 82 |
|
| 83 |
def test_explicit_connect_host_port_starttls(self): |
| 84 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 85 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 86 |
context.check_hostname = False |
| 87 |
context.verify_mode = ssl.CERT_NONE |
| 88 |
with socket_helper.transient_internet(self.testServer): |
| 89 |
server = smtplib.SMTP() |
| 90 |
server.connect(f'{self.testServer}:{self.remotePort}') |
| 91 |
try: |
| 92 |
server.starttls(context=context) |
| 93 |
except smtplib.SMTPException as e: |
| 94 |
if e.args[0] == 'STARTTLS extension not supported by server.': |
| 95 |
unittest.skip(e.args[0]) |
| 96 |
else: |
| 97 |
raise |
| 98 |
server.ehlo() |
| 99 |
server.quit() |
| 100 |
|
| 101 |
|
| 102 |
class SmtpSSLTest(unittest.TestCase): |
| 103 |
testServer = SMTP_TEST_SERVER |
| 104 |
remotePort = 465 |
| 105 |
|
| 106 |
def test_connect(self): |
| 107 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 108 |
with socket_helper.transient_internet(self.testServer): |
| 109 |
server = smtplib.SMTP_SSL(self.testServer, self.remotePort) |
| 110 |
server.ehlo() |
| 111 |
server.quit() |
| 112 |
|
| 113 |
def test_connect_default_port(self): |
| 114 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 115 |
with socket_helper.transient_internet(self.testServer): |
| 116 |
server = smtplib.SMTP_SSL(self.testServer) |
| 117 |
server.ehlo() |
| 118 |
server.quit() |
| 119 |
|
| 120 |
@support.requires_resource('walltime') |
| 121 |
def test_connect_using_sslcontext(self): |
| 122 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 123 |
context.check_hostname = False |
| 124 |
context.verify_mode = ssl.CERT_NONE |
| 125 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 126 |
with socket_helper.transient_internet(self.testServer): |
| 127 |
server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context) |
| 128 |
server.ehlo() |
| 129 |
server.quit() |
| 130 |
|
| 131 |
def test_connect_using_sslcontext_verified(self): |
| 132 |
with socket_helper.transient_internet(self.testServer): |
| 133 |
can_verify = check_ssl_verifiy(self.testServer, self.remotePort) |
| 134 |
if not can_verify: |
| 135 |
self.skipTest("SSL certificate can't be verified") |
| 136 |
|
| 137 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 138 |
context = ssl.create_default_context() |
| 139 |
with socket_helper.transient_internet(self.testServer): |
| 140 |
server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context) |
| 141 |
server.ehlo() |
| 142 |
server.quit() |
| 143 |
|
| 144 |
|
| 145 |
if __name__ == "__main__": |
| 146 |
unittest.main() |