blob: 727ef834f3a99cdc70e0fee16cb00b087574cb74 [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()
Antoine Pitroua751c3f2010-04-30 23:23:38 +0000155 # Pick a random unused port by passing 0 for the port number
156 self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1))
157 # Keep a note of what port was assigned
158 self.port = self.serv.socket.getsockname()[1]
Christian Heimes5e696852008-04-09 08:37:03 +0000159 serv_args = (self.serv, self.serv_evt, self.client_evt)
Guido van Rossum806c2462007-08-06 23:33:07 +0000160 threading.Thread(target=debugging_server, args=serv_args).start()
161
162 # wait until server thread has assigned a port number
Christian Heimes380f7f22008-02-28 11:19:05 +0000163 self.serv_evt.wait()
164 self.serv_evt.clear()
Guido van Rossum806c2462007-08-06 23:33:07 +0000165
166 def tearDown(self):
167 # indicate that the client is finished
168 self.client_evt.set()
169 # wait for the server thread to terminate
170 self.serv_evt.wait()
171 # restore sys.stdout
172 sys.stdout = self.old_stdout
173
174 def testBasic(self):
175 # connect
Christian Heimes5e696852008-04-09 08:37:03 +0000176 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000177 smtp.quit()
178
Guido van Rossum04110fb2007-08-24 16:32:05 +0000179 def testNOOP(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000180 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000181 expected = (250, b'Ok')
182 self.assertEqual(smtp.noop(), expected)
183 smtp.quit()
184
185 def testRSET(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000186 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000187 expected = (250, b'Ok')
188 self.assertEqual(smtp.rset(), expected)
189 smtp.quit()
190
191 def testNotImplemented(self):
192 # EHLO isn't implemented in DebuggingServer
Christian Heimes5e696852008-04-09 08:37:03 +0000193 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000194 expected = (502, b'Error: command "EHLO" not implemented')
195 self.assertEqual(smtp.ehlo(), expected)
196 smtp.quit()
197
Guido van Rossum04110fb2007-08-24 16:32:05 +0000198 def testVRFY(self):
199 # VRFY isn't implemented in DebuggingServer
Christian Heimes5e696852008-04-09 08:37:03 +0000200 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000201 expected = (502, b'Error: command "VRFY" not implemented')
202 self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
203 self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
204 smtp.quit()
205
206 def testSecondHELO(self):
207 # check that a second HELO returns a message that it's a duplicate
208 # (this behavior is specific to smtpd.SMTPChannel)
Christian Heimes5e696852008-04-09 08:37:03 +0000209 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000210 smtp.helo()
211 expected = (503, b'Duplicate HELO/EHLO')
212 self.assertEqual(smtp.helo(), expected)
213 smtp.quit()
214
Guido van Rossum806c2462007-08-06 23:33:07 +0000215 def testHELP(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000216 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000217 self.assertEqual(smtp.help(), b'Error: command "HELP" not implemented')
218 smtp.quit()
219
220 def testSend(self):
221 # connect and send mail
222 m = 'A test message'
Christian Heimes5e696852008-04-09 08:37:03 +0000223 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000224 smtp.sendmail('John', 'Sally', m)
Neal Norwitz25329672008-08-25 03:55:03 +0000225 # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
226 # in asyncore. This sleep might help, but should really be fixed
227 # properly by using an Event variable.
228 time.sleep(0.01)
Guido van Rossum806c2462007-08-06 23:33:07 +0000229 smtp.quit()
230
231 self.client_evt.set()
232 self.serv_evt.wait()
233 self.output.flush()
234 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
235 self.assertEqual(self.output.getvalue(), mexpect)
236
237
Christian Heimes380f7f22008-02-28 11:19:05 +0000238class NonConnectingTests(TestCase):
239
240 def testNotConnected(self):
241 # Test various operations on an unconnected SMTP object that
242 # should raise exceptions (at present the attempt in SMTP.send
243 # to reference the nonexistent 'sock' attribute of the SMTP object
244 # causes an AttributeError)
245 smtp = smtplib.SMTP()
246 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
247 self.assertRaises(smtplib.SMTPServerDisconnected,
248 smtp.send, 'test msg')
249
250 def testNonnumericPort(self):
251 # check that non-numeric port raises socket.error
252 self.assertRaises(socket.error, smtplib.SMTP,
253 "localhost", "bogus")
254 self.assertRaises(socket.error, smtplib.SMTP,
255 "localhost:bogus")
256
257
Guido van Rossum04110fb2007-08-24 16:32:05 +0000258# test response of client to a non-successful HELO message
Guido van Rossum806c2462007-08-06 23:33:07 +0000259class BadHELOServerTests(TestCase):
260
261 def setUp(self):
262 self.old_stdout = sys.stdout
263 self.output = io.StringIO()
264 sys.stdout = self.output
265
266 self.evt = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +0000267 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
268 self.sock.settimeout(15)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000269 self.port = support.bind_port(self.sock)
Christian Heimes5e696852008-04-09 08:37:03 +0000270 servargs = (self.evt, b"199 no hello for you!\n", self.sock)
Guido van Rossum806c2462007-08-06 23:33:07 +0000271 threading.Thread(target=server, args=servargs).start()
Christian Heimes380f7f22008-02-28 11:19:05 +0000272 self.evt.wait()
273 self.evt.clear()
Guido van Rossum806c2462007-08-06 23:33:07 +0000274
275 def tearDown(self):
276 self.evt.wait()
277 sys.stdout = self.old_stdout
278
279 def testFailingHELO(self):
280 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
Christian Heimes5e696852008-04-09 08:37:03 +0000281 HOST, self.port, 'localhost', 3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000282
Guido van Rossum04110fb2007-08-24 16:32:05 +0000283
284sim_users = {'Mr.A@somewhere.com':'John A',
285 'Ms.B@somewhere.com':'Sally B',
286 'Mrs.C@somewhereesle.com':'Ruth C',
287 }
288
R. David Murraycaa27b72009-05-23 18:49:56 +0000289sim_auth = ('Mr.A@somewhere.com', 'somepassword')
R. David Murrayfb123912009-05-28 18:19:00 +0000290sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
291 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
292sim_auth_credentials = {
293 'login': 'TXIuQUBzb21ld2hlcmUuY29t',
294 'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=',
295 'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ'
296 'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'),
297 }
298sim_auth_login_password = 'C29TZXBHC3N3B3JK'
R. David Murraycaa27b72009-05-23 18:49:56 +0000299
Guido van Rossum04110fb2007-08-24 16:32:05 +0000300sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
301 'list-2':['Ms.B@somewhere.com',],
302 }
303
304# Simulated SMTP channel & server
305class SimSMTPChannel(smtpd.SMTPChannel):
R. David Murrayfb123912009-05-28 18:19:00 +0000306
R. David Murray23ddc0e2009-05-29 18:03:16 +0000307 def __init__(self, extra_features, *args, **kw):
308 self._extrafeatures = ''.join(
309 [ "250-{0}\r\n".format(x) for x in extra_features ])
R. David Murrayfb123912009-05-28 18:19:00 +0000310 super(SimSMTPChannel, self).__init__(*args, **kw)
311
Guido van Rossum04110fb2007-08-24 16:32:05 +0000312 def smtp_EHLO(self, arg):
R. David Murrayfb123912009-05-28 18:19:00 +0000313 resp = ('250-testhost\r\n'
314 '250-EXPN\r\n'
315 '250-SIZE 20000000\r\n'
316 '250-STARTTLS\r\n'
317 '250-DELIVERBY\r\n')
318 resp = resp + self._extrafeatures + '250 HELP'
Guido van Rossum04110fb2007-08-24 16:32:05 +0000319 self.push(resp)
320
321 def smtp_VRFY(self, arg):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000322 raw_addr = email.utils.parseaddr(arg)[1]
323 quoted_addr = smtplib.quoteaddr(arg)
324 if raw_addr in sim_users:
325 self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr))
326 else:
327 self.push('550 No such user: %s' % arg)
328
329 def smtp_EXPN(self, arg):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000330 list_name = email.utils.parseaddr(arg)[1].lower()
331 if list_name in sim_lists:
332 user_list = sim_lists[list_name]
333 for n, user_email in enumerate(user_list):
334 quoted_addr = smtplib.quoteaddr(user_email)
335 if n < len(user_list) - 1:
336 self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
337 else:
338 self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
339 else:
340 self.push('550 No access for you!')
341
R. David Murraycaa27b72009-05-23 18:49:56 +0000342 def smtp_AUTH(self, arg):
R. David Murrayfb123912009-05-28 18:19:00 +0000343 if arg.strip().lower()=='cram-md5':
344 self.push('334 {}'.format(sim_cram_md5_challenge))
345 return
R. David Murraycaa27b72009-05-23 18:49:56 +0000346 mech, auth = arg.split()
R. David Murrayfb123912009-05-28 18:19:00 +0000347 mech = mech.lower()
348 if mech not in sim_auth_credentials:
R. David Murraycaa27b72009-05-23 18:49:56 +0000349 self.push('504 auth type unimplemented')
R. David Murrayfb123912009-05-28 18:19:00 +0000350 return
351 if mech == 'plain' and auth==sim_auth_credentials['plain']:
352 self.push('235 plain auth ok')
353 elif mech=='login' and auth==sim_auth_credentials['login']:
354 self.push('334 Password:')
355 else:
356 self.push('550 No access for you!')
R. David Murraycaa27b72009-05-23 18:49:56 +0000357
Guido van Rossum04110fb2007-08-24 16:32:05 +0000358
359class SimSMTPServer(smtpd.SMTPServer):
R. David Murrayfb123912009-05-28 18:19:00 +0000360
R. David Murray23ddc0e2009-05-29 18:03:16 +0000361 def __init__(self, *args, **kw):
362 self._extra_features = []
363 smtpd.SMTPServer.__init__(self, *args, **kw)
364
Guido van Rossum04110fb2007-08-24 16:32:05 +0000365 def handle_accept(self):
366 conn, addr = self.accept()
R. David Murray23ddc0e2009-05-29 18:03:16 +0000367 self._SMTPchannel = SimSMTPChannel(self._extra_features,
368 self, conn, addr)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000369
370 def process_message(self, peer, mailfrom, rcpttos, data):
371 pass
372
R. David Murrayfb123912009-05-28 18:19:00 +0000373 def add_feature(self, feature):
R. David Murray23ddc0e2009-05-29 18:03:16 +0000374 self._extra_features.append(feature)
R. David Murrayfb123912009-05-28 18:19:00 +0000375
Guido van Rossum04110fb2007-08-24 16:32:05 +0000376
377# Test various SMTP & ESMTP commands/behaviors that require a simulated server
378# (i.e., something with more features than DebuggingServer)
379class SMTPSimTests(TestCase):
380
381 def setUp(self):
382 self.serv_evt = threading.Event()
383 self.client_evt = threading.Event()
Antoine Pitroua751c3f2010-04-30 23:23:38 +0000384 # Pick a random unused port by passing 0 for the port number
385 self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1))
386 # Keep a note of what port was assigned
387 self.port = self.serv.socket.getsockname()[1]
Christian Heimes5e696852008-04-09 08:37:03 +0000388 serv_args = (self.serv, self.serv_evt, self.client_evt)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000389 threading.Thread(target=debugging_server, args=serv_args).start()
390
391 # wait until server thread has assigned a port number
Christian Heimes380f7f22008-02-28 11:19:05 +0000392 self.serv_evt.wait()
393 self.serv_evt.clear()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000394
395 def tearDown(self):
396 # indicate that the client is finished
397 self.client_evt.set()
398 # wait for the server thread to terminate
399 self.serv_evt.wait()
400
401 def testBasic(self):
402 # smoke test
Christian Heimes5e696852008-04-09 08:37:03 +0000403 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000404 smtp.quit()
405
406 def testEHLO(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000407 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000408
409 # no features should be present before the EHLO
410 self.assertEqual(smtp.esmtp_features, {})
411
412 # features expected from the test server
413 expected_features = {'expn':'',
414 'size': '20000000',
415 'starttls': '',
416 'deliverby': '',
417 'help': '',
418 }
419
420 smtp.ehlo()
421 self.assertEqual(smtp.esmtp_features, expected_features)
422 for k in expected_features:
423 self.assertTrue(smtp.has_extn(k))
424 self.assertFalse(smtp.has_extn('unsupported-feature'))
425 smtp.quit()
426
427 def testVRFY(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000428 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000429
430 for email, name in sim_users.items():
431 expected_known = (250, bytes('%s %s' %
Guido van Rossum5a23cc52007-08-30 14:02:43 +0000432 (name, smtplib.quoteaddr(email)),
433 "ascii"))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000434 self.assertEqual(smtp.vrfy(email), expected_known)
435
436 u = 'nobody@nowhere.com'
Thomas Wouters74e68c72007-08-31 00:20:14 +0000437 expected_unknown = (550, ('No such user: %s'
438 % smtplib.quoteaddr(u)).encode('ascii'))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000439 self.assertEqual(smtp.vrfy(u), expected_unknown)
440 smtp.quit()
441
442 def testEXPN(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000443 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000444
445 for listname, members in sim_lists.items():
446 users = []
447 for m in members:
448 users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
Guido van Rossum5a23cc52007-08-30 14:02:43 +0000449 expected_known = (250, bytes('\n'.join(users), "ascii"))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000450 self.assertEqual(smtp.expn(listname), expected_known)
451
452 u = 'PSU-Members-List'
453 expected_unknown = (550, b'No access for you!')
454 self.assertEqual(smtp.expn(u), expected_unknown)
455 smtp.quit()
456
R. David Murrayfb123912009-05-28 18:19:00 +0000457 def testAUTH_PLAIN(self):
R. David Murrayfb123912009-05-28 18:19:00 +0000458 self.serv.add_feature("AUTH PLAIN")
R. David Murray23ddc0e2009-05-29 18:03:16 +0000459 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murraycaa27b72009-05-23 18:49:56 +0000460
R. David Murrayfb123912009-05-28 18:19:00 +0000461 expected_auth_ok = (235, b'plain auth ok')
R. David Murraycaa27b72009-05-23 18:49:56 +0000462 self.assertEqual(smtp.login(sim_auth[0], sim_auth[1]), expected_auth_ok)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000463
R. David Murrayfb123912009-05-28 18:19:00 +0000464 # SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they
465 # require a synchronous read to obtain the credentials...so instead smtpd
466 # sees the credential sent by smtplib's login method as an unknown command,
467 # which results in smtplib raising an auth error. Fortunately the error
468 # message contains the encoded credential, so we can partially check that it
469 # was generated correctly (partially, because the 'word' is uppercased in
470 # the error message).
471
472 def testAUTH_LOGIN(self):
R. David Murrayfb123912009-05-28 18:19:00 +0000473 self.serv.add_feature("AUTH LOGIN")
R. David Murray23ddc0e2009-05-29 18:03:16 +0000474 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murrayfb123912009-05-28 18:19:00 +0000475 try: smtp.login(sim_auth[0], sim_auth[1])
476 except smtplib.SMTPAuthenticationError as err:
477 if sim_auth_login_password not in str(err):
478 raise "expected encoded password not found in error message"
479
480 def testAUTH_CRAM_MD5(self):
R. David Murrayfb123912009-05-28 18:19:00 +0000481 self.serv.add_feature("AUTH CRAM-MD5")
R. David Murray23ddc0e2009-05-29 18:03:16 +0000482 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murrayfb123912009-05-28 18:19:00 +0000483
484 try: smtp.login(sim_auth[0], sim_auth[1])
485 except smtplib.SMTPAuthenticationError as err:
486 if sim_auth_credentials['cram-md5'] not in str(err):
487 raise "expected encoded credentials not found in error message"
488
489 #TODO: add tests for correct AUTH method fallback now that the
490 #test infrastructure can support it.
491
Guido van Rossum04110fb2007-08-24 16:32:05 +0000492
Guido van Rossumd8faa362007-04-27 19:54:29 +0000493def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000494 support.run_unittest(GeneralTests, DebuggingServerTests,
Christian Heimes380f7f22008-02-28 11:19:05 +0000495 NonConnectingTests,
Guido van Rossum04110fb2007-08-24 16:32:05 +0000496 BadHELOServerTests, SMTPSimTests)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000497
498if __name__ == '__main__':
499 test_main()