blob: 86224ef2e45beb669e3503f2300595bb515c1656 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
R. David Murray87e20742009-05-23 01:30:26 +00002
3import unittest
4from test import support
5import smtplib
Antoine Pitrou0d38f2e2011-05-18 20:02:50 +02006
7ssl = support.import_module("ssl")
R. David Murray87e20742009-05-23 01:30:26 +00008
R. David Murrayeccb2ce2009-05-23 02:36:15 +00009support.requires("network")
R. David Murray87e20742009-05-23 01:30:26 +000010
Antoine Pitroue0650202011-05-18 18:03:09 +020011
12class SmtpTest(unittest.TestCase):
13 testServer = 'smtp.gmail.com'
14 remotePort = 25
15 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
16
17 def test_connect_starttls(self):
18 support.get_attribute(smtplib, 'SMTP_SSL')
19 with support.transient_internet(self.testServer):
20 server = smtplib.SMTP(self.testServer, self.remotePort)
Nadeem Vawda3fc58682011-07-30 23:46:54 +020021 try:
22 server.starttls(context=self.context)
23 except smtplib.SMTPException as e:
24 if e.args[0] == 'STARTTLS extension not supported by server.':
25 unittest.skip(e.args[0])
26 else:
27 raise
Antoine Pitroucc9ca222011-05-18 18:04:04 +020028 server.ehlo()
29 server.quit()
Antoine Pitroue0650202011-05-18 18:03:09 +020030
31
R. David Murray87e20742009-05-23 01:30:26 +000032class SmtpSSLTest(unittest.TestCase):
33 testServer = 'smtp.gmail.com'
34 remotePort = 465
Antoine Pitroue0650202011-05-18 18:03:09 +020035 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
R. David Murray87e20742009-05-23 01:30:26 +000036
37 def test_connect(self):
R. David Murrayeccb2ce2009-05-23 02:36:15 +000038 support.get_attribute(smtplib, 'SMTP_SSL')
Antoine Pitrou6003ff72010-10-13 17:14:16 +000039 with support.transient_internet(self.testServer):
40 server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
Antoine Pitroucc9ca222011-05-18 18:04:04 +020041 server.ehlo()
42 server.quit()
R. David Murray87e20742009-05-23 01:30:26 +000043
Antoine Pitrouc1d52062011-05-07 19:39:37 +020044 def test_connect_default_port(self):
45 support.get_attribute(smtplib, 'SMTP_SSL')
46 with support.transient_internet(self.testServer):
47 server = smtplib.SMTP_SSL(self.testServer)
Antoine Pitroucc9ca222011-05-18 18:04:04 +020048 server.ehlo()
49 server.quit()
Antoine Pitrouc1d52062011-05-07 19:39:37 +020050
Antoine Pitroue0650202011-05-18 18:03:09 +020051 def test_connect_using_sslcontext(self):
52 support.get_attribute(smtplib, 'SMTP_SSL')
53 with support.transient_internet(self.testServer):
54 server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=self.context)
Antoine Pitroucc9ca222011-05-18 18:04:04 +020055 server.ehlo()
56 server.quit()
Antoine Pitroue0650202011-05-18 18:03:09 +020057
58
R. David Murray87e20742009-05-23 01:30:26 +000059def test_main():
Antoine Pitroue0650202011-05-18 18:03:09 +020060 support.run_unittest(SmtpTest, SmtpSSLTest)
R. David Murray87e20742009-05-23 01:30:26 +000061
62if __name__ == "__main__":
63 test_main()