2010-03-11 18:53:45 -04:00
|
|
|
#!/usr/bin/env python3
|
2009-05-22 22:30:26 -03:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
from test import support
|
|
|
|
import smtplib
|
2011-05-18 15:02:50 -03:00
|
|
|
|
|
|
|
ssl = support.import_module("ssl")
|
2009-05-22 22:30:26 -03:00
|
|
|
|
2009-05-22 23:36:15 -03:00
|
|
|
support.requires("network")
|
2009-05-22 22:30:26 -03:00
|
|
|
|
2011-05-18 13:03:09 -03:00
|
|
|
|
|
|
|
class SmtpTest(unittest.TestCase):
|
|
|
|
testServer = 'smtp.gmail.com'
|
|
|
|
remotePort = 25
|
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
|
|
|
|
|
|
|
|
def test_connect_starttls(self):
|
|
|
|
support.get_attribute(smtplib, 'SMTP_SSL')
|
|
|
|
with support.transient_internet(self.testServer):
|
|
|
|
server = smtplib.SMTP(self.testServer, self.remotePort)
|
2011-07-30 18:46:54 -03:00
|
|
|
try:
|
|
|
|
server.starttls(context=self.context)
|
|
|
|
except smtplib.SMTPException as e:
|
|
|
|
if e.args[0] == 'STARTTLS extension not supported by server.':
|
|
|
|
unittest.skip(e.args[0])
|
|
|
|
else:
|
|
|
|
raise
|
2011-05-18 13:04:04 -03:00
|
|
|
server.ehlo()
|
|
|
|
server.quit()
|
2011-05-18 13:03:09 -03:00
|
|
|
|
|
|
|
|
2009-05-22 22:30:26 -03:00
|
|
|
class SmtpSSLTest(unittest.TestCase):
|
|
|
|
testServer = 'smtp.gmail.com'
|
|
|
|
remotePort = 465
|
2011-05-18 13:03:09 -03:00
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
|
2009-05-22 22:30:26 -03:00
|
|
|
|
|
|
|
def test_connect(self):
|
2009-05-22 23:36:15 -03:00
|
|
|
support.get_attribute(smtplib, 'SMTP_SSL')
|
2010-10-13 14:14:16 -03:00
|
|
|
with support.transient_internet(self.testServer):
|
|
|
|
server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
|
2011-05-18 13:04:04 -03:00
|
|
|
server.ehlo()
|
|
|
|
server.quit()
|
2009-05-22 22:30:26 -03:00
|
|
|
|
2011-05-07 14:39:37 -03:00
|
|
|
def test_connect_default_port(self):
|
|
|
|
support.get_attribute(smtplib, 'SMTP_SSL')
|
|
|
|
with support.transient_internet(self.testServer):
|
|
|
|
server = smtplib.SMTP_SSL(self.testServer)
|
2011-05-18 13:04:04 -03:00
|
|
|
server.ehlo()
|
|
|
|
server.quit()
|
2011-05-07 14:39:37 -03:00
|
|
|
|
2011-05-18 13:03:09 -03:00
|
|
|
def test_connect_using_sslcontext(self):
|
|
|
|
support.get_attribute(smtplib, 'SMTP_SSL')
|
|
|
|
with support.transient_internet(self.testServer):
|
|
|
|
server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=self.context)
|
2011-05-18 13:04:04 -03:00
|
|
|
server.ehlo()
|
|
|
|
server.quit()
|
2011-05-18 13:03:09 -03:00
|
|
|
|
|
|
|
|
2009-05-22 22:30:26 -03:00
|
|
|
def test_main():
|
2011-05-18 13:03:09 -03:00
|
|
|
support.run_unittest(SmtpTest, SmtpSSLTest)
|
2009-05-22 22:30:26 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|