| 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 |
|
| 49 |
class SmtpSSLTest(unittest.TestCase): |
| 50 |
testServer = SMTP_TEST_SERVER |
| 51 |
remotePort = 465 |
| 52 |
|
| 53 |
def test_connect(self): |
| 54 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 55 |
with socket_helper.transient_internet(self.testServer): |
| 56 |
server = smtplib.SMTP_SSL(self.testServer, self.remotePort) |
| 57 |
server.ehlo() |
| 58 |
server.quit() |
| 59 |
|
| 60 |
def test_connect_default_port(self): |
| 61 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 62 |
with socket_helper.transient_internet(self.testServer): |
| 63 |
server = smtplib.SMTP_SSL(self.testServer) |
| 64 |
server.ehlo() |
| 65 |
server.quit() |
| 66 |
|
| 67 |
@support.requires_resource('walltime') |
| 68 |
def test_connect_using_sslcontext(self): |
| 69 |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 70 |
context.check_hostname = False |
| 71 |
context.verify_mode = ssl.CERT_NONE |
| 72 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 73 |
with socket_helper.transient_internet(self.testServer): |
| 74 |
server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context) |
| 75 |
server.ehlo() |
| 76 |
server.quit() |
| 77 |
|
| 78 |
def test_connect_using_sslcontext_verified(self): |
| 79 |
with socket_helper.transient_internet(self.testServer): |
| 80 |
can_verify = check_ssl_verifiy(self.testServer, self.remotePort) |
| 81 |
if not can_verify: |
| 82 |
self.skipTest("SSL certificate can't be verified") |
| 83 |
|
| 84 |
support.get_attribute(smtplib, 'SMTP_SSL') |
| 85 |
context = ssl.create_default_context() |
| 86 |
with socket_helper.transient_internet(self.testServer): |
| 87 |
server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context) |
| 88 |
server.ehlo() |
| 89 |
server.quit() |
| 90 |
|
| 91 |
|
| 92 |
if __name__ == "__main__": |
| 93 |
unittest.main() |