ยปCore Development>Code coverage>Lib/test/test_smtpnet.py

Python code coverage for Lib/test/test_smtpnet.py

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