blob: f69b7762278c2dc97ed999838c7c87ea1b256f1c [file] [log] [blame]
Guido van Rossum806c2462007-08-06 23:33:07 +00001import asyncore
Guido van Rossum04110fb2007-08-24 16:32:05 +00002import email.utils
Guido van Rossumd8faa362007-04-27 19:54:29 +00003import socket
4import threading
Guido van Rossum806c2462007-08-06 23:33:07 +00005import smtpd
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import smtplib
Guido van Rossum806c2462007-08-06 23:33:07 +00007import io
8import sys
Guido van Rossumd8faa362007-04-27 19:54:29 +00009import time
Guido van Rossum806c2462007-08-06 23:33:07 +000010import select
Guido van Rossumd8faa362007-04-27 19:54:29 +000011
12from unittest import TestCase
Benjamin Petersonee8712c2008-05-20 21:35:26 +000013from test import support
Guido van Rossumd8faa362007-04-27 19:54:29 +000014
Benjamin Petersonee8712c2008-05-20 21:35:26 +000015HOST = support.HOST
Guido van Rossumd8faa362007-04-27 19:54:29 +000016
Josiah Carlsond74900e2008-07-07 04:15:08 +000017if sys.platform == 'darwin':
18 # select.poll returns a select.POLLHUP at the end of the tests
19 # on darwin, so just ignore it
20 def handle_expt(self):
21 pass
22 smtpd.SMTPChannel.handle_expt = handle_expt
23
24
Christian Heimes5e696852008-04-09 08:37:03 +000025def server(evt, buf, serv):
Christian Heimes380f7f22008-02-28 11:19:05 +000026 serv.listen(5)
27 evt.set()
Guido van Rossumd8faa362007-04-27 19:54:29 +000028 try:
29 conn, addr = serv.accept()
30 except socket.timeout:
31 pass
32 else:
Guido van Rossum806c2462007-08-06 23:33:07 +000033 n = 500
34 while buf and n > 0:
35 r, w, e = select.select([], [conn], [])
36 if w:
37 sent = conn.send(buf)
38 buf = buf[sent:]
39
40 n -= 1
Guido van Rossum806c2462007-08-06 23:33:07 +000041
Guido van Rossumd8faa362007-04-27 19:54:29 +000042 conn.close()
43 finally:
44 serv.close()
45 evt.set()
46
47class GeneralTests(TestCase):
48
49 def setUp(self):
50 self.evt = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +000051 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
52 self.sock.settimeout(15)
Benjamin Petersonee8712c2008-05-20 21:35:26 +000053 self.port = support.bind_port(self.sock)
Christian Heimes5e696852008-04-09 08:37:03 +000054 servargs = (self.evt, b"220 Hola mundo\n", self.sock)
Guido van Rossum806c2462007-08-06 23:33:07 +000055 threading.Thread(target=server, args=servargs).start()
Christian Heimes380f7f22008-02-28 11:19:05 +000056 self.evt.wait()
57 self.evt.clear()
Guido van Rossumd8faa362007-04-27 19:54:29 +000058
59 def tearDown(self):
60 self.evt.wait()
61
Guido van Rossum806c2462007-08-06 23:33:07 +000062 def testBasic1(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +000063 # connects
Christian Heimes5e696852008-04-09 08:37:03 +000064 smtp = smtplib.SMTP(HOST, self.port)
Georg Brandlf78e02b2008-06-10 17:40:04 +000065 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +000066
Guido van Rossum806c2462007-08-06 23:33:07 +000067 def testBasic2(self):
68 # connects, include port in host name
Christian Heimes5e696852008-04-09 08:37:03 +000069 smtp = smtplib.SMTP("%s:%s" % (HOST, self.port))
Georg Brandlf78e02b2008-06-10 17:40:04 +000070 smtp.close()
Guido van Rossum806c2462007-08-06 23:33:07 +000071
72 def testLocalHostName(self):
73 # check that supplied local_hostname is used
Christian Heimes5e696852008-04-09 08:37:03 +000074 smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost")
Guido van Rossum806c2462007-08-06 23:33:07 +000075 self.assertEqual(smtp.local_hostname, "testhost")
Georg Brandlf78e02b2008-06-10 17:40:04 +000076 smtp.close()
Guido van Rossum806c2462007-08-06 23:33:07 +000077
Guido van Rossumd8faa362007-04-27 19:54:29 +000078 def testTimeoutDefault(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +000079 self.assertTrue(socket.getdefaulttimeout() is None)
80 socket.setdefaulttimeout(30)
81 try:
82 smtp = smtplib.SMTP(HOST, self.port)
83 finally:
84 socket.setdefaulttimeout(None)
Guido van Rossumd8faa362007-04-27 19:54:29 +000085 self.assertEqual(smtp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +000086 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +000087
88 def testTimeoutNone(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +000089 self.assertTrue(socket.getdefaulttimeout() is None)
Guido van Rossumd8faa362007-04-27 19:54:29 +000090 socket.setdefaulttimeout(30)
91 try:
Christian Heimes5e696852008-04-09 08:37:03 +000092 smtp = smtplib.SMTP(HOST, self.port, timeout=None)
Guido van Rossumd8faa362007-04-27 19:54:29 +000093 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +000094 socket.setdefaulttimeout(None)
95 self.assertTrue(smtp.sock.gettimeout() is None)
96 smtp.close()
97
98 def testTimeoutValue(self):
99 smtp = smtplib.SMTP(HOST, self.port, timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000100 self.assertEqual(smtp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000101 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000102
103
Guido van Rossum04110fb2007-08-24 16:32:05 +0000104# Test server thread using the specified SMTP server class
Christian Heimes5e696852008-04-09 08:37:03 +0000105def debugging_server(serv, serv_evt, client_evt):
Christian Heimes380f7f22008-02-28 11:19:05 +0000106 serv_evt.set()
Guido van Rossum806c2462007-08-06 23:33:07 +0000107
108 try:
109 if hasattr(select, 'poll'):
110 poll_fun = asyncore.poll2
111 else:
112 poll_fun = asyncore.poll
113
114 n = 1000
115 while asyncore.socket_map and n > 0:
116 poll_fun(0.01, asyncore.socket_map)
117
118 # when the client conversation is finished, it will
119 # set client_evt, and it's then ok to kill the server
Benjamin Peterson672b8032008-06-11 19:14:14 +0000120 if client_evt.is_set():
Guido van Rossum806c2462007-08-06 23:33:07 +0000121 serv.close()
122 break
123
124 n -= 1
125
126 except socket.timeout:
127 pass
128 finally:
Benjamin Peterson672b8032008-06-11 19:14:14 +0000129 if not client_evt.is_set():
Christian Heimes380f7f22008-02-28 11:19:05 +0000130 # allow some time for the client to read the result
131 time.sleep(0.5)
132 serv.close()
Guido van Rossum806c2462007-08-06 23:33:07 +0000133 asyncore.close_all()
Guido van Rossum806c2462007-08-06 23:33:07 +0000134 serv_evt.set()
135
136MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
137MSG_END = '------------ END MESSAGE ------------\n'
138
Guido van Rossum04110fb2007-08-24 16:32:05 +0000139# NOTE: Some SMTP objects in the tests below are created with a non-default
140# local_hostname argument to the constructor, since (on some systems) the FQDN
141# lookup caused by the default local_hostname sometimes takes so long that the
Guido van Rossum806c2462007-08-06 23:33:07 +0000142# test server times out, causing the test to fail.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000143
144# Test behavior of smtpd.DebuggingServer
Guido van Rossum806c2462007-08-06 23:33:07 +0000145class DebuggingServerTests(TestCase):
146
147 def setUp(self):
148 # temporarily replace sys.stdout to capture DebuggingServer output
149 self.old_stdout = sys.stdout
150 self.output = io.StringIO()
151 sys.stdout = self.output
152
153 self.serv_evt = threading.Event()
154 self.client_evt = threading.Event()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000155 self.port = support.find_unused_port()
Christian Heimes5e696852008-04-09 08:37:03 +0000156 self.serv = smtpd.DebuggingServer((HOST, self.port), ('nowhere', -1))
157 serv_args = (self.serv, self.serv_evt, self.client_evt)
Guido van Rossum806c2462007-08-06 23:33:07 +0000158 threading.Thread(target=debugging_server, args=serv_args).start()
159
160 # wait until server thread has assigned a port number
Christian Heimes380f7f22008-02-28 11:19:05 +0000161 self.serv_evt.wait()
162 self.serv_evt.clear()
Guido van Rossum806c2462007-08-06 23:33:07 +0000163
164 def tearDown(self):
165 # indicate that the client is finished
166 self.client_evt.set()
167 # wait for the server thread to terminate
168 self.serv_evt.wait()
169 # restore sys.stdout
170 sys.stdout = self.old_stdout
171
172 def testBasic(self):
173 # connect
Christian Heimes5e696852008-04-09 08:37:03 +0000174 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000175 smtp.quit()
176
Guido van Rossum04110fb2007-08-24 16:32:05 +0000177 def testNOOP(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000178 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000179 expected = (250, b'Ok')
180 self.assertEqual(smtp.noop(), expected)
181 smtp.quit()
182
183 def testRSET(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000184 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000185 expected = (250, b'Ok')
186 self.assertEqual(smtp.rset(), expected)
187 smtp.quit()
188
189 def testNotImplemented(self):
190 # EHLO isn't implemented in DebuggingServer
Christian Heimes5e696852008-04-09 08:37:03 +0000191 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000192 expected = (502, b'Error: command "EHLO" not implemented')
193 self.assertEqual(smtp.ehlo(), expected)
194 smtp.quit()
195
Guido van Rossum04110fb2007-08-24 16:32:05 +0000196 def testVRFY(self):
197 # VRFY isn't implemented in DebuggingServer
Christian Heimes5e696852008-04-09 08:37:03 +0000198 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000199 expected = (502, b'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)
Christian Heimes5e696852008-04-09 08:37:03 +0000207 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000208 smtp.helo()
209 expected = (503, b'Duplicate HELO/EHLO')
210 self.assertEqual(smtp.helo(), expected)
211 smtp.quit()
212
Guido van Rossum806c2462007-08-06 23:33:07 +0000213 def testHELP(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000214 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000215 self.assertEqual(smtp.help(), b'Error: command "HELP" not implemented')
216 smtp.quit()
217
218 def testSend(self):
219 # connect and send mail
220 m = 'A test message'
Christian Heimes5e696852008-04-09 08:37:03 +0000221 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000222 smtp.sendmail('John', 'Sally', m)
Neal Norwitz25329672008-08-25 03:55:03 +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)
Guido van Rossum806c2462007-08-06 23:33:07 +0000227 smtp.quit()
228
229 self.client_evt.set()
230 self.serv_evt.wait()
231 self.output.flush()
232 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
233 self.assertEqual(self.output.getvalue(), mexpect)
234
235
Christian Heimes380f7f22008-02-28 11:19:05 +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
Guido van Rossum04110fb2007-08-24 16:32:05 +0000256# test response of client to a non-successful HELO message
Guido van Rossum806c2462007-08-06 23:33:07 +0000257class BadHELOServerTests(TestCase):
258
259 def setUp(self):
260 self.old_stdout = sys.stdout
261 self.output = io.StringIO()
262 sys.stdout = self.output
263
264 self.evt = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +0000265 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
266 self.sock.settimeout(15)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000267 self.port = support.bind_port(self.sock)
Christian Heimes5e696852008-04-09 08:37:03 +0000268 servargs = (self.evt, b"199 no hello for you!\n", self.sock)
Guido van Rossum806c2462007-08-06 23:33:07 +0000269 threading.Thread(target=server, args=servargs).start()
Christian Heimes380f7f22008-02-28 11:19:05 +0000270 self.evt.wait()
271 self.evt.clear()
Guido van Rossum806c2462007-08-06 23:33:07 +0000272
273 def tearDown(self):
274 self.evt.wait()
275 sys.stdout = self.old_stdout
276
277 def testFailingHELO(self):
278 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
Christian Heimes5e696852008-04-09 08:37:03 +0000279 HOST, self.port, 'localhost', 3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000280
Guido van Rossum04110fb2007-08-24 16:32:05 +0000281
282sim_users = {'Mr.A@somewhere.com':'John A',
283 'Ms.B@somewhere.com':'Sally B',
284 'Mrs.C@somewhereesle.com':'Ruth C',
285 }
286
R. David Murraycaa27b72009-05-23 18:49:56 +0000287sim_auth = ('Mr.A@somewhere.com', 'somepassword')
R. David Murrayfb123912009-05-28 18:19:00 +0000288sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
289 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
290sim_auth_credentials = {
291 'login': 'TXIuQUBzb21ld2hlcmUuY29t',
292 'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=',
293 'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ'
294 'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'),
295 }
296sim_auth_login_password = 'C29TZXBHC3N3B3JK'
R. David Murraycaa27b72009-05-23 18:49:56 +0000297
Guido van Rossum04110fb2007-08-24 16:32:05 +0000298sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
299 'list-2':['Ms.B@somewhere.com',],
300 }
301
302# Simulated SMTP channel & server
303class SimSMTPChannel(smtpd.SMTPChannel):
R. David Murrayfb123912009-05-28 18:19:00 +0000304
305 def __init__(self, *args, **kw):
306 self.__extrafeatures = []
307 super(SimSMTPChannel, self).__init__(*args, **kw)
308
309 @property
310 def _extrafeatures(self):
311 return ''.join([ "250-{}\r\n".format(x) for x in self.__extrafeatures ])
312
313 def add_feature(self, feature):
314 self.__extrafeatures.append(feature)
315
Guido van Rossum04110fb2007-08-24 16:32:05 +0000316 def smtp_EHLO(self, arg):
R. David Murrayfb123912009-05-28 18:19:00 +0000317 resp = ('250-testhost\r\n'
318 '250-EXPN\r\n'
319 '250-SIZE 20000000\r\n'
320 '250-STARTTLS\r\n'
321 '250-DELIVERBY\r\n')
322 resp = resp + self._extrafeatures + '250 HELP'
Guido van Rossum04110fb2007-08-24 16:32:05 +0000323 self.push(resp)
324
325 def smtp_VRFY(self, arg):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000326 raw_addr = email.utils.parseaddr(arg)[1]
327 quoted_addr = smtplib.quoteaddr(arg)
328 if raw_addr in sim_users:
329 self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr))
330 else:
331 self.push('550 No such user: %s' % arg)
332
333 def smtp_EXPN(self, arg):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000334 list_name = email.utils.parseaddr(arg)[1].lower()
335 if list_name in sim_lists:
336 user_list = sim_lists[list_name]
337 for n, user_email in enumerate(user_list):
338 quoted_addr = smtplib.quoteaddr(user_email)
339 if n < len(user_list) - 1:
340 self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
341 else:
342 self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
343 else:
344 self.push('550 No access for you!')
345
R. David Murraycaa27b72009-05-23 18:49:56 +0000346 def smtp_AUTH(self, arg):
R. David Murrayfb123912009-05-28 18:19:00 +0000347 if arg.strip().lower()=='cram-md5':
348 self.push('334 {}'.format(sim_cram_md5_challenge))
349 return
R. David Murraycaa27b72009-05-23 18:49:56 +0000350 mech, auth = arg.split()
R. David Murrayfb123912009-05-28 18:19:00 +0000351 mech = mech.lower()
352 if mech not in sim_auth_credentials:
R. David Murraycaa27b72009-05-23 18:49:56 +0000353 self.push('504 auth type unimplemented')
R. David Murrayfb123912009-05-28 18:19:00 +0000354 return
355 if mech == 'plain' and auth==sim_auth_credentials['plain']:
356 self.push('235 plain auth ok')
357 elif mech=='login' and auth==sim_auth_credentials['login']:
358 self.push('334 Password:')
359 else:
360 self.push('550 No access for you!')
R. David Murraycaa27b72009-05-23 18:49:56 +0000361
Guido van Rossum04110fb2007-08-24 16:32:05 +0000362
363class SimSMTPServer(smtpd.SMTPServer):
R. David Murrayfb123912009-05-28 18:19:00 +0000364
Guido van Rossum04110fb2007-08-24 16:32:05 +0000365 def handle_accept(self):
366 conn, addr = self.accept()
R. David Murrayfb123912009-05-28 18:19:00 +0000367 self._SMTPchannel = SimSMTPChannel(self, conn, addr)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000368
369 def process_message(self, peer, mailfrom, rcpttos, data):
370 pass
371
R. David Murrayfb123912009-05-28 18:19:00 +0000372 def add_feature(self, feature):
373 self._SMTPchannel.add_feature(feature)
374
Guido van Rossum04110fb2007-08-24 16:32:05 +0000375
376# Test various SMTP & ESMTP commands/behaviors that require a simulated server
377# (i.e., something with more features than DebuggingServer)
378class SMTPSimTests(TestCase):
379
380 def setUp(self):
381 self.serv_evt = threading.Event()
382 self.client_evt = threading.Event()
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000383 self.port = support.find_unused_port()
Christian Heimes5e696852008-04-09 08:37:03 +0000384 self.serv = SimSMTPServer((HOST, self.port), ('nowhere', -1))
385 serv_args = (self.serv, self.serv_evt, self.client_evt)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000386 threading.Thread(target=debugging_server, args=serv_args).start()
387
388 # wait until server thread has assigned a port number
Christian Heimes380f7f22008-02-28 11:19:05 +0000389 self.serv_evt.wait()
390 self.serv_evt.clear()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000391
392 def tearDown(self):
393 # indicate that the client is finished
394 self.client_evt.set()
395 # wait for the server thread to terminate
396 self.serv_evt.wait()
397
398 def testBasic(self):
399 # smoke test
Christian Heimes5e696852008-04-09 08:37:03 +0000400 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000401 smtp.quit()
402
403 def testEHLO(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000404 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000405
406 # no features should be present before the EHLO
407 self.assertEqual(smtp.esmtp_features, {})
408
409 # features expected from the test server
410 expected_features = {'expn':'',
411 'size': '20000000',
412 'starttls': '',
413 'deliverby': '',
414 'help': '',
415 }
416
417 smtp.ehlo()
418 self.assertEqual(smtp.esmtp_features, expected_features)
419 for k in expected_features:
420 self.assertTrue(smtp.has_extn(k))
421 self.assertFalse(smtp.has_extn('unsupported-feature'))
422 smtp.quit()
423
424 def testVRFY(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000425 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000426
427 for email, name in sim_users.items():
428 expected_known = (250, bytes('%s %s' %
Guido van Rossum5a23cc52007-08-30 14:02:43 +0000429 (name, smtplib.quoteaddr(email)),
430 "ascii"))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000431 self.assertEqual(smtp.vrfy(email), expected_known)
432
433 u = 'nobody@nowhere.com'
Thomas Wouters74e68c72007-08-31 00:20:14 +0000434 expected_unknown = (550, ('No such user: %s'
435 % smtplib.quoteaddr(u)).encode('ascii'))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000436 self.assertEqual(smtp.vrfy(u), expected_unknown)
437 smtp.quit()
438
439 def testEXPN(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000440 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000441
442 for listname, members in sim_lists.items():
443 users = []
444 for m in members:
445 users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
Guido van Rossum5a23cc52007-08-30 14:02:43 +0000446 expected_known = (250, bytes('\n'.join(users), "ascii"))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000447 self.assertEqual(smtp.expn(listname), expected_known)
448
449 u = 'PSU-Members-List'
450 expected_unknown = (550, b'No access for you!')
451 self.assertEqual(smtp.expn(u), expected_unknown)
452 smtp.quit()
453
R. David Murrayfb123912009-05-28 18:19:00 +0000454 def testAUTH_PLAIN(self):
R. David Murraycaa27b72009-05-23 18:49:56 +0000455 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murrayfb123912009-05-28 18:19:00 +0000456 self.serv.add_feature("AUTH PLAIN")
R. David Murraycaa27b72009-05-23 18:49:56 +0000457
R. David Murrayfb123912009-05-28 18:19:00 +0000458 expected_auth_ok = (235, b'plain auth ok')
R. David Murraycaa27b72009-05-23 18:49:56 +0000459 self.assertEqual(smtp.login(sim_auth[0], sim_auth[1]), expected_auth_ok)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000460
R. David Murrayfb123912009-05-28 18:19:00 +0000461 # SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they
462 # require a synchronous read to obtain the credentials...so instead smtpd
463 # sees the credential sent by smtplib's login method as an unknown command,
464 # which results in smtplib raising an auth error. Fortunately the error
465 # message contains the encoded credential, so we can partially check that it
466 # was generated correctly (partially, because the 'word' is uppercased in
467 # the error message).
468
469 def testAUTH_LOGIN(self):
470 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
471 self.serv.add_feature("AUTH LOGIN")
472 try: smtp.login(sim_auth[0], sim_auth[1])
473 except smtplib.SMTPAuthenticationError as err:
474 if sim_auth_login_password not in str(err):
475 raise "expected encoded password not found in error message"
476
477 def testAUTH_CRAM_MD5(self):
478 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
479 self.serv.add_feature("AUTH CRAM-MD5")
480
481 try: smtp.login(sim_auth[0], sim_auth[1])
482 except smtplib.SMTPAuthenticationError as err:
483 if sim_auth_credentials['cram-md5'] not in str(err):
484 raise "expected encoded credentials not found in error message"
485
486 #TODO: add tests for correct AUTH method fallback now that the
487 #test infrastructure can support it.
488
Guido van Rossum04110fb2007-08-24 16:32:05 +0000489
Guido van Rossumd8faa362007-04-27 19:54:29 +0000490def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000491 support.run_unittest(GeneralTests, DebuggingServerTests,
Christian Heimes380f7f22008-02-28 11:19:05 +0000492 NonConnectingTests,
Guido van Rossum04110fb2007-08-24 16:32:05 +0000493 BadHELOServerTests, SMTPSimTests)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000494
495if __name__ == '__main__':
496 test_main()