blob: e97cf363bb68dfae7cda9531e6645e93c48c553f [file] [log] [blame]
R. David Murray87e20742009-05-23 01:30:26 +00001import unittest
2from test import support
3import smtplib
Antoine Pitrou0d38f2e2011-05-18 20:02:50 +02004
5ssl = support.import_module("ssl")
R. David Murray87e20742009-05-23 01:30:26 +00006
R. David Murrayeccb2ce2009-05-23 02:36:15 +00007support.requires("network")
R. David Murray87e20742009-05-23 01:30:26 +00008
Antoine Pitroue0650202011-05-18 18:03:09 +02009
10class SmtpTest(unittest.TestCase):
11 testServer = 'smtp.gmail.com'
12 remotePort = 25
13 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
14
15 def test_connect_starttls(self):
16 support.get_attribute(smtplib, 'SMTP_SSL')
17 with support.transient_internet(self.testServer):
18 server = smtplib.SMTP(self.testServer, self.remotePort)
Nadeem Vawda3fc58682011-07-30 23:46:54 +020019 try:
20 server.starttls(context=self.context)
21 except smtplib.SMTPException as e:
22 if e.args[0] == 'STARTTLS extension not supported by server.':
23 unittest.skip(e.args[0])
24 else:
25 raise
Antoine Pitroucc9ca222011-05-18 18:04:04 +020026 server.ehlo()
27 server.quit()
Antoine Pitroue0650202011-05-18 18:03:09 +020028
29
R. David Murray87e20742009-05-23 01:30:26 +000030class SmtpSSLTest(unittest.TestCase):
31 testServer = 'smtp.gmail.com'
32 remotePort = 465
Antoine Pitroue0650202011-05-18 18:03:09 +020033 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
R. David Murray87e20742009-05-23 01:30:26 +000034
35 def test_connect(self):
R. David Murrayeccb2ce2009-05-23 02:36:15 +000036 support.get_attribute(smtplib, 'SMTP_SSL')
Antoine Pitrou6003ff72010-10-13 17:14:16 +000037 with support.transient_internet(self.testServer):
38 server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
Antoine Pitroucc9ca222011-05-18 18:04:04 +020039 server.ehlo()
40 server.quit()
R. David Murray87e20742009-05-23 01:30:26 +000041
Antoine Pitrouc1d52062011-05-07 19:39:37 +020042 def test_connect_default_port(self):
43 support.get_attribute(smtplib, 'SMTP_SSL')
44 with support.transient_internet(self.testServer):
45 server = smtplib.SMTP_SSL(self.testServer)
Antoine Pitroucc9ca222011-05-18 18:04:04 +020046 server.ehlo()
47 server.quit()
Antoine Pitrouc1d52062011-05-07 19:39:37 +020048
Antoine Pitroue0650202011-05-18 18:03:09 +020049 def test_connect_using_sslcontext(self):
50 support.get_attribute(smtplib, 'SMTP_SSL')
51 with support.transient_internet(self.testServer):
52 server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=self.context)
Antoine Pitroucc9ca222011-05-18 18:04:04 +020053 server.ehlo()
54 server.quit()
Antoine Pitroue0650202011-05-18 18:03:09 +020055
56
R. David Murray87e20742009-05-23 01:30:26 +000057def test_main():
Antoine Pitroue0650202011-05-18 18:03:09 +020058 support.run_unittest(SmtpTest, SmtpSSLTest)
R. David Murray87e20742009-05-23 01:30:26 +000059
60if __name__ == "__main__":
61 test_main()