blob: 9a492f627fe4ef38050b3dfd279eaaf007dc98f9 [file] [log] [blame]
Facundo Batista16ed5b42007-07-24 21:20:42 +00001import asyncore
Facundo Batista1bc8d632007-08-21 16:57:18 +00002import email.utils
Facundo Batista366d6262007-03-28 18:25:54 +00003import socket
4import threading
Facundo Batista16ed5b42007-07-24 21:20:42 +00005import smtpd
Facundo Batista366d6262007-03-28 18:25:54 +00006import smtplib
Facundo Batista16ed5b42007-07-24 21:20:42 +00007import StringIO
8import sys
Facundo Batista366d6262007-03-28 18:25:54 +00009import time
Facundo Batista16ed5b42007-07-24 21:20:42 +000010import select
Facundo Batista366d6262007-03-28 18:25:54 +000011
12from unittest import TestCase
13from test import test_support
14
Trent Nelsone41b0062008-04-08 23:47:30 +000015HOST = test_support.HOST
Facundo Batista366d6262007-03-28 18:25:54 +000016
Trent Nelsone41b0062008-04-08 23:47:30 +000017def server(evt, buf, serv):
Neal Norwitz75992ed2008-02-26 08:04:59 +000018 serv.listen(5)
19 evt.set()
Facundo Batista366d6262007-03-28 18:25:54 +000020 try:
21 conn, addr = serv.accept()
22 except socket.timeout:
23 pass
24 else:
Facundo Batista412b8b62007-08-01 23:18:36 +000025 n = 500
Facundo Batista16ed5b42007-07-24 21:20:42 +000026 while buf and n > 0:
27 r, w, e = select.select([], [conn], [])
28 if w:
29 sent = conn.send(buf)
30 buf = buf[sent:]
31
32 n -= 1
Facundo Batista16ed5b42007-07-24 21:20:42 +000033
Facundo Batista366d6262007-03-28 18:25:54 +000034 conn.close()
35 finally:
36 serv.close()
37 evt.set()
38
39class GeneralTests(TestCase):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000040
Facundo Batista366d6262007-03-28 18:25:54 +000041 def setUp(self):
Antoine Pitroua763c062009-10-27 19:47:30 +000042 self._threads = test_support.threading_setup()
Facundo Batista366d6262007-03-28 18:25:54 +000043 self.evt = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +000044 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
45 self.sock.settimeout(15)
46 self.port = test_support.bind_port(self.sock)
47 servargs = (self.evt, "220 Hola mundo\n", self.sock)
Antoine Pitroua763c062009-10-27 19:47:30 +000048 self.thread = threading.Thread(target=server, args=servargs)
49 self.thread.start()
Neal Norwitz75992ed2008-02-26 08:04:59 +000050 self.evt.wait()
51 self.evt.clear()
Facundo Batista366d6262007-03-28 18:25:54 +000052
53 def tearDown(self):
54 self.evt.wait()
Antoine Pitroua763c062009-10-27 19:47:30 +000055 self.thread.join()
56 test_support.threading_cleanup(*self._threads)
Facundo Batista366d6262007-03-28 18:25:54 +000057
Facundo Batista16ed5b42007-07-24 21:20:42 +000058 def testBasic1(self):
Facundo Batista366d6262007-03-28 18:25:54 +000059 # connects
Trent Nelsone41b0062008-04-08 23:47:30 +000060 smtp = smtplib.SMTP(HOST, self.port)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000061 smtp.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000062
Facundo Batista16ed5b42007-07-24 21:20:42 +000063 def testBasic2(self):
64 # connects, include port in host name
Trent Nelsone41b0062008-04-08 23:47:30 +000065 smtp = smtplib.SMTP("%s:%s" % (HOST, self.port))
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000066 smtp.close()
Facundo Batista16ed5b42007-07-24 21:20:42 +000067
68 def testLocalHostName(self):
69 # check that supplied local_hostname is used
Trent Nelsone41b0062008-04-08 23:47:30 +000070 smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost")
Facundo Batista16ed5b42007-07-24 21:20:42 +000071 self.assertEqual(smtp.local_hostname, "testhost")
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000072 smtp.close()
Facundo Batista16ed5b42007-07-24 21:20:42 +000073
Facundo Batista366d6262007-03-28 18:25:54 +000074 def testTimeoutDefault(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000075 self.assertTrue(socket.getdefaulttimeout() is None)
76 socket.setdefaulttimeout(30)
77 try:
78 smtp = smtplib.SMTP(HOST, self.port)
79 finally:
80 socket.setdefaulttimeout(None)
Facundo Batista366d6262007-03-28 18:25:54 +000081 self.assertEqual(smtp.sock.gettimeout(), 30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000082 smtp.close()
Facundo Batista366d6262007-03-28 18:25:54 +000083
84 def testTimeoutNone(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000085 self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista366d6262007-03-28 18:25:54 +000086 socket.setdefaulttimeout(30)
87 try:
Trent Nelsone41b0062008-04-08 23:47:30 +000088 smtp = smtplib.SMTP(HOST, self.port, timeout=None)
Facundo Batista366d6262007-03-28 18:25:54 +000089 finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000090 socket.setdefaulttimeout(None)
91 self.assertTrue(smtp.sock.gettimeout() is None)
92 smtp.close()
93
94 def testTimeoutValue(self):
95 smtp = smtplib.SMTP(HOST, self.port, timeout=30)
Facundo Batista366d6262007-03-28 18:25:54 +000096 self.assertEqual(smtp.sock.gettimeout(), 30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000097 smtp.close()
Facundo Batista366d6262007-03-28 18:25:54 +000098
99
Facundo Batista1bc8d632007-08-21 16:57:18 +0000100# Test server thread using the specified SMTP server class
Trent Nelsone41b0062008-04-08 23:47:30 +0000101def debugging_server(serv, serv_evt, client_evt):
Neal Norwitz75992ed2008-02-26 08:04:59 +0000102 serv_evt.set()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000103
104 try:
Facundo Batista412b8b62007-08-01 23:18:36 +0000105 if hasattr(select, 'poll'):
106 poll_fun = asyncore.poll2
107 else:
108 poll_fun = asyncore.poll
109
110 n = 1000
111 while asyncore.socket_map and n > 0:
112 poll_fun(0.01, asyncore.socket_map)
113
114 # when the client conversation is finished, it will
115 # set client_evt, and it's then ok to kill the server
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000116 if client_evt.is_set():
Facundo Batista412b8b62007-08-01 23:18:36 +0000117 serv.close()
118 break
119
120 n -= 1
121
Facundo Batista16ed5b42007-07-24 21:20:42 +0000122 except socket.timeout:
123 pass
124 finally:
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000125 if not client_evt.is_set():
Neal Norwitz75992ed2008-02-26 08:04:59 +0000126 # allow some time for the client to read the result
127 time.sleep(0.5)
128 serv.close()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000129 asyncore.close_all()
Facundo Batista412b8b62007-08-01 23:18:36 +0000130 serv_evt.set()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000131
132MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
133MSG_END = '------------ END MESSAGE ------------\n'
134
Facundo Batista1bc8d632007-08-21 16:57:18 +0000135# NOTE: Some SMTP objects in the tests below are created with a non-default
136# local_hostname argument to the constructor, since (on some systems) the FQDN
137# lookup caused by the default local_hostname sometimes takes so long that the
Facundo Batista412b8b62007-08-01 23:18:36 +0000138# test server times out, causing the test to fail.
Facundo Batista1bc8d632007-08-21 16:57:18 +0000139
140# Test behavior of smtpd.DebuggingServer
Facundo Batista16ed5b42007-07-24 21:20:42 +0000141class DebuggingServerTests(TestCase):
142
143 def setUp(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000144 # temporarily replace sys.stdout to capture DebuggingServer output
Facundo Batista16ed5b42007-07-24 21:20:42 +0000145 self.old_stdout = sys.stdout
146 self.output = StringIO.StringIO()
147 sys.stdout = self.output
148
Antoine Pitroua763c062009-10-27 19:47:30 +0000149 self._threads = test_support.threading_setup()
Facundo Batista412b8b62007-08-01 23:18:36 +0000150 self.serv_evt = threading.Event()
151 self.client_evt = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +0000152 self.port = test_support.find_unused_port()
153 self.serv = smtpd.DebuggingServer((HOST, self.port), ('nowhere', -1))
154 serv_args = (self.serv, self.serv_evt, self.client_evt)
Antoine Pitroua763c062009-10-27 19:47:30 +0000155 self.thread = threading.Thread(target=debugging_server, args=serv_args)
156 self.thread.start()
Facundo Batista412b8b62007-08-01 23:18:36 +0000157
158 # wait until server thread has assigned a port number
Neal Norwitz75992ed2008-02-26 08:04:59 +0000159 self.serv_evt.wait()
160 self.serv_evt.clear()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000161
162 def tearDown(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000163 # indicate that the client is finished
164 self.client_evt.set()
165 # wait for the server thread to terminate
166 self.serv_evt.wait()
Antoine Pitroua763c062009-10-27 19:47:30 +0000167 self.thread.join()
168 test_support.threading_cleanup(*self._threads)
Facundo Batista412b8b62007-08-01 23:18:36 +0000169 # restore sys.stdout
Facundo Batista16ed5b42007-07-24 21:20:42 +0000170 sys.stdout = self.old_stdout
171
172 def testBasic(self):
173 # connect
Trent Nelsone41b0062008-04-08 23:47:30 +0000174 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista412b8b62007-08-01 23:18:36 +0000175 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000176
Facundo Batista1bc8d632007-08-21 16:57:18 +0000177 def testNOOP(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000178 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000179 expected = (250, 'Ok')
180 self.assertEqual(smtp.noop(), expected)
181 smtp.quit()
182
183 def testRSET(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000184 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000185 expected = (250, 'Ok')
186 self.assertEqual(smtp.rset(), expected)
187 smtp.quit()
188
189 def testNotImplemented(self):
190 # EHLO isn't implemented in DebuggingServer
Trent Nelsone41b0062008-04-08 23:47:30 +0000191 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista412b8b62007-08-01 23:18:36 +0000192 expected = (502, 'Error: command "EHLO" not implemented')
193 self.assertEqual(smtp.ehlo(), expected)
194 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000195
Facundo Batista1bc8d632007-08-21 16:57:18 +0000196 def testVRFY(self):
197 # VRFY isn't implemented in DebuggingServer
Trent Nelsone41b0062008-04-08 23:47:30 +0000198 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000199 expected = (502, 'Error: command "VRFY" not implemented')
200 self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
201 self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
202 smtp.quit()
203
204 def testSecondHELO(self):
205 # check that a second HELO returns a message that it's a duplicate
206 # (this behavior is specific to smtpd.SMTPChannel)
Trent Nelsone41b0062008-04-08 23:47:30 +0000207 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000208 smtp.helo()
209 expected = (503, 'Duplicate HELO/EHLO')
210 self.assertEqual(smtp.helo(), expected)
211 smtp.quit()
212
Facundo Batista16ed5b42007-07-24 21:20:42 +0000213 def testHELP(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000214 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000215 self.assertEqual(smtp.help(), 'Error: command "HELP" not implemented')
Facundo Batista412b8b62007-08-01 23:18:36 +0000216 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000217
218 def testSend(self):
219 # connect and send mail
220 m = 'A test message'
Trent Nelsone41b0062008-04-08 23:47:30 +0000221 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000222 smtp.sendmail('John', 'Sally', m)
Neal Norwitze39be532008-08-25 03:52:40 +0000223 # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
224 # in asyncore. This sleep might help, but should really be fixed
225 # properly by using an Event variable.
226 time.sleep(0.01)
Facundo Batista412b8b62007-08-01 23:18:36 +0000227 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000228
Facundo Batista412b8b62007-08-01 23:18:36 +0000229 self.client_evt.set()
230 self.serv_evt.wait()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000231 self.output.flush()
232 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
233 self.assertEqual(self.output.getvalue(), mexpect)
234
235
Neal Norwitz75992ed2008-02-26 08:04:59 +0000236class NonConnectingTests(TestCase):
237
238 def testNotConnected(self):
239 # Test various operations on an unconnected SMTP object that
240 # should raise exceptions (at present the attempt in SMTP.send
241 # to reference the nonexistent 'sock' attribute of the SMTP object
242 # causes an AttributeError)
243 smtp = smtplib.SMTP()
244 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
245 self.assertRaises(smtplib.SMTPServerDisconnected,
246 smtp.send, 'test msg')
247
248 def testNonnumericPort(self):
249 # check that non-numeric port raises socket.error
250 self.assertRaises(socket.error, smtplib.SMTP,
251 "localhost", "bogus")
252 self.assertRaises(socket.error, smtplib.SMTP,
253 "localhost:bogus")
254
255
Facundo Batista1bc8d632007-08-21 16:57:18 +0000256# test response of client to a non-successful HELO message
Facundo Batista16ed5b42007-07-24 21:20:42 +0000257class BadHELOServerTests(TestCase):
258
259 def setUp(self):
260 self.old_stdout = sys.stdout
261 self.output = StringIO.StringIO()
262 sys.stdout = self.output
263
Antoine Pitroua763c062009-10-27 19:47:30 +0000264 self._threads = test_support.threading_setup()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000265 self.evt = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +0000266 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
267 self.sock.settimeout(15)
268 self.port = test_support.bind_port(self.sock)
269 servargs = (self.evt, "199 no hello for you!\n", self.sock)
Antoine Pitroua763c062009-10-27 19:47:30 +0000270 self.thread = threading.Thread(target=server, args=servargs)
271 self.thread.start()
Neal Norwitz75992ed2008-02-26 08:04:59 +0000272 self.evt.wait()
273 self.evt.clear()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000274
275 def tearDown(self):
276 self.evt.wait()
Antoine Pitroua763c062009-10-27 19:47:30 +0000277 self.thread.join()
278 test_support.threading_cleanup(*self._threads)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000279 sys.stdout = self.old_stdout
280
281 def testFailingHELO(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000282 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
Trent Nelsone41b0062008-04-08 23:47:30 +0000283 HOST, self.port, 'localhost', 3)
Facundo Batista366d6262007-03-28 18:25:54 +0000284
Facundo Batista1bc8d632007-08-21 16:57:18 +0000285
286sim_users = {'Mr.A@somewhere.com':'John A',
287 'Ms.B@somewhere.com':'Sally B',
288 'Mrs.C@somewhereesle.com':'Ruth C',
289 }
290
R. David Murray3724d6c2009-05-23 21:48:06 +0000291sim_auth = ('Mr.A@somewhere.com', 'somepassword')
R. David Murray8de212b2009-05-28 18:49:23 +0000292sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
293 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
294sim_auth_credentials = {
295 'login': 'TXIuQUBzb21ld2hlcmUuY29t',
296 'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=',
297 'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ'
298 'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'),
299 }
300sim_auth_login_password = 'C29TZXBHC3N3B3JK'
R. David Murray3724d6c2009-05-23 21:48:06 +0000301
Facundo Batista1bc8d632007-08-21 16:57:18 +0000302sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
303 'list-2':['Ms.B@somewhere.com',],
304 }
305
306# Simulated SMTP channel & server
307class SimSMTPChannel(smtpd.SMTPChannel):
R. David Murray8de212b2009-05-28 18:49:23 +0000308
R. David Murray6b985442009-05-29 17:31:05 +0000309 def __init__(self, extra_features, *args, **kw):
310 self._extrafeatures = ''.join(
311 [ "250-{0}\r\n".format(x) for x in extra_features ])
R. David Murray8de212b2009-05-28 18:49:23 +0000312 smtpd.SMTPChannel.__init__(self, *args, **kw)
313
Facundo Batista1bc8d632007-08-21 16:57:18 +0000314 def smtp_EHLO(self, arg):
R. David Murray8de212b2009-05-28 18:49:23 +0000315 resp = ('250-testhost\r\n'
316 '250-EXPN\r\n'
317 '250-SIZE 20000000\r\n'
318 '250-STARTTLS\r\n'
319 '250-DELIVERBY\r\n')
320 resp = resp + self._extrafeatures + '250 HELP'
Facundo Batista1bc8d632007-08-21 16:57:18 +0000321 self.push(resp)
322
323 def smtp_VRFY(self, arg):
Facundo Batista1bc8d632007-08-21 16:57:18 +0000324 raw_addr = email.utils.parseaddr(arg)[1]
325 quoted_addr = smtplib.quoteaddr(arg)
326 if raw_addr in sim_users:
327 self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr))
328 else:
329 self.push('550 No such user: %s' % arg)
330
331 def smtp_EXPN(self, arg):
Facundo Batista1bc8d632007-08-21 16:57:18 +0000332 list_name = email.utils.parseaddr(arg)[1].lower()
333 if list_name in sim_lists:
334 user_list = sim_lists[list_name]
335 for n, user_email in enumerate(user_list):
336 quoted_addr = smtplib.quoteaddr(user_email)
337 if n < len(user_list) - 1:
338 self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
339 else:
340 self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
341 else:
342 self.push('550 No access for you!')
343
R. David Murray3724d6c2009-05-23 21:48:06 +0000344 def smtp_AUTH(self, arg):
R. David Murray8de212b2009-05-28 18:49:23 +0000345 if arg.strip().lower()=='cram-md5':
346 self.push('334 {0}'.format(sim_cram_md5_challenge))
347 return
R. David Murray3724d6c2009-05-23 21:48:06 +0000348 mech, auth = arg.split()
R. David Murray8de212b2009-05-28 18:49:23 +0000349 mech = mech.lower()
350 if mech not in sim_auth_credentials:
R. David Murray3724d6c2009-05-23 21:48:06 +0000351 self.push('504 auth type unimplemented')
R. David Murray8de212b2009-05-28 18:49:23 +0000352 return
353 if mech == 'plain' and auth==sim_auth_credentials['plain']:
354 self.push('235 plain auth ok')
355 elif mech=='login' and auth==sim_auth_credentials['login']:
356 self.push('334 Password:')
357 else:
358 self.push('550 No access for you!')
R. David Murray3724d6c2009-05-23 21:48:06 +0000359
Facundo Batista1bc8d632007-08-21 16:57:18 +0000360
361class SimSMTPServer(smtpd.SMTPServer):
R. David Murray8de212b2009-05-28 18:49:23 +0000362
R. David Murray6b985442009-05-29 17:31:05 +0000363 def __init__(self, *args, **kw):
364 self._extra_features = []
365 smtpd.SMTPServer.__init__(self, *args, **kw)
366
Facundo Batista1bc8d632007-08-21 16:57:18 +0000367 def handle_accept(self):
368 conn, addr = self.accept()
R. David Murray6b985442009-05-29 17:31:05 +0000369 self._SMTPchannel = SimSMTPChannel(self._extra_features,
370 self, conn, addr)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000371
372 def process_message(self, peer, mailfrom, rcpttos, data):
373 pass
374
R. David Murray8de212b2009-05-28 18:49:23 +0000375 def add_feature(self, feature):
R. David Murray6b985442009-05-29 17:31:05 +0000376 self._extra_features.append(feature)
R. David Murray8de212b2009-05-28 18:49:23 +0000377
Facundo Batista1bc8d632007-08-21 16:57:18 +0000378
379# Test various SMTP & ESMTP commands/behaviors that require a simulated server
380# (i.e., something with more features than DebuggingServer)
381class SMTPSimTests(TestCase):
382
383 def setUp(self):
Antoine Pitroua763c062009-10-27 19:47:30 +0000384 self._threads = test_support.threading_setup()
Facundo Batista1bc8d632007-08-21 16:57:18 +0000385 self.serv_evt = threading.Event()
386 self.client_evt = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +0000387 self.port = test_support.find_unused_port()
388 self.serv = SimSMTPServer((HOST, self.port), ('nowhere', -1))
389 serv_args = (self.serv, self.serv_evt, self.client_evt)
Antoine Pitroua763c062009-10-27 19:47:30 +0000390 self.thread = threading.Thread(target=debugging_server, args=serv_args)
391 self.thread.start()
Facundo Batista1bc8d632007-08-21 16:57:18 +0000392
393 # wait until server thread has assigned a port number
Neal Norwitz75992ed2008-02-26 08:04:59 +0000394 self.serv_evt.wait()
395 self.serv_evt.clear()
Facundo Batista1bc8d632007-08-21 16:57:18 +0000396
397 def tearDown(self):
398 # indicate that the client is finished
399 self.client_evt.set()
400 # wait for the server thread to terminate
401 self.serv_evt.wait()
Antoine Pitroua763c062009-10-27 19:47:30 +0000402 self.thread.join()
403 test_support.threading_cleanup(*self._threads)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000404
405 def testBasic(self):
406 # smoke test
Trent Nelsone41b0062008-04-08 23:47:30 +0000407 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000408 smtp.quit()
409
410 def testEHLO(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000411 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000412
413 # no features should be present before the EHLO
414 self.assertEqual(smtp.esmtp_features, {})
415
416 # features expected from the test server
417 expected_features = {'expn':'',
418 'size': '20000000',
419 'starttls': '',
420 'deliverby': '',
421 'help': '',
422 }
423
424 smtp.ehlo()
425 self.assertEqual(smtp.esmtp_features, expected_features)
426 for k in expected_features:
427 self.assertTrue(smtp.has_extn(k))
428 self.assertFalse(smtp.has_extn('unsupported-feature'))
429 smtp.quit()
430
431 def testVRFY(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000432 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000433
434 for email, name in sim_users.items():
435 expected_known = (250, '%s %s' % (name, smtplib.quoteaddr(email)))
436 self.assertEqual(smtp.vrfy(email), expected_known)
437
438 u = 'nobody@nowhere.com'
439 expected_unknown = (550, 'No such user: %s' % smtplib.quoteaddr(u))
440 self.assertEqual(smtp.vrfy(u), expected_unknown)
441 smtp.quit()
442
443 def testEXPN(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000444 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000445
446 for listname, members in sim_lists.items():
447 users = []
448 for m in members:
449 users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
450 expected_known = (250, '\n'.join(users))
451 self.assertEqual(smtp.expn(listname), expected_known)
452
453 u = 'PSU-Members-List'
454 expected_unknown = (550, 'No access for you!')
455 self.assertEqual(smtp.expn(u), expected_unknown)
456 smtp.quit()
457
R. David Murray8de212b2009-05-28 18:49:23 +0000458 def testAUTH_PLAIN(self):
R. David Murray8de212b2009-05-28 18:49:23 +0000459 self.serv.add_feature("AUTH PLAIN")
R. David Murray6b985442009-05-29 17:31:05 +0000460 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murray3724d6c2009-05-23 21:48:06 +0000461
R. David Murray8de212b2009-05-28 18:49:23 +0000462 expected_auth_ok = (235, b'plain auth ok')
R. David Murray3724d6c2009-05-23 21:48:06 +0000463 self.assertEqual(smtp.login(sim_auth[0], sim_auth[1]), expected_auth_ok)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000464
R. David Murray8de212b2009-05-28 18:49:23 +0000465 # SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they
466 # require a synchronous read to obtain the credentials...so instead smtpd
467 # sees the credential sent by smtplib's login method as an unknown command,
468 # which results in smtplib raising an auth error. Fortunately the error
469 # message contains the encoded credential, so we can partially check that it
470 # was generated correctly (partially, because the 'word' is uppercased in
471 # the error message).
472
473 def testAUTH_LOGIN(self):
R. David Murray8de212b2009-05-28 18:49:23 +0000474 self.serv.add_feature("AUTH LOGIN")
R. David Murray6b985442009-05-29 17:31:05 +0000475 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murray8de212b2009-05-28 18:49:23 +0000476 try: smtp.login(sim_auth[0], sim_auth[1])
477 except smtplib.SMTPAuthenticationError as err:
478 if sim_auth_login_password not in str(err):
479 raise "expected encoded password not found in error message"
480
481 def testAUTH_CRAM_MD5(self):
R. David Murray8de212b2009-05-28 18:49:23 +0000482 self.serv.add_feature("AUTH CRAM-MD5")
R. David Murray6b985442009-05-29 17:31:05 +0000483 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murray8de212b2009-05-28 18:49:23 +0000484
485 try: smtp.login(sim_auth[0], sim_auth[1])
486 except smtplib.SMTPAuthenticationError as err:
487 if sim_auth_credentials['cram-md5'] not in str(err):
488 raise "expected encoded credentials not found in error message"
489
490 #TODO: add tests for correct AUTH method fallback now that the
491 #test infrastructure can support it.
492
Facundo Batista1bc8d632007-08-21 16:57:18 +0000493
Facundo Batista366d6262007-03-28 18:25:54 +0000494def test_main(verbose=None):
Facundo Batista412b8b62007-08-01 23:18:36 +0000495 test_support.run_unittest(GeneralTests, DebuggingServerTests,
Neal Norwitz75992ed2008-02-26 08:04:59 +0000496 NonConnectingTests,
Facundo Batista1bc8d632007-08-21 16:57:18 +0000497 BadHELOServerTests, SMTPSimTests)
Facundo Batista366d6262007-03-28 18:25:54 +0000498
499if __name__ == '__main__':
500 test_main()