blob: 644cbf7be6ff956e57b57b730a5a2347bcd51202 [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):
42 self.evt = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +000043 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
44 self.sock.settimeout(15)
45 self.port = test_support.bind_port(self.sock)
46 servargs = (self.evt, "220 Hola mundo\n", self.sock)
Facundo Batista16ed5b42007-07-24 21:20:42 +000047 threading.Thread(target=server, args=servargs).start()
Neal Norwitz75992ed2008-02-26 08:04:59 +000048 self.evt.wait()
49 self.evt.clear()
Facundo Batista366d6262007-03-28 18:25:54 +000050
51 def tearDown(self):
52 self.evt.wait()
53
Facundo Batista16ed5b42007-07-24 21:20:42 +000054 def testBasic1(self):
Facundo Batista366d6262007-03-28 18:25:54 +000055 # connects
Trent Nelsone41b0062008-04-08 23:47:30 +000056 smtp = smtplib.SMTP(HOST, self.port)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000057 smtp.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000058
Facundo Batista16ed5b42007-07-24 21:20:42 +000059 def testBasic2(self):
60 # connects, include port in host name
Trent Nelsone41b0062008-04-08 23:47:30 +000061 smtp = smtplib.SMTP("%s:%s" % (HOST, self.port))
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000062 smtp.close()
Facundo Batista16ed5b42007-07-24 21:20:42 +000063
64 def testLocalHostName(self):
65 # check that supplied local_hostname is used
Trent Nelsone41b0062008-04-08 23:47:30 +000066 smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost")
Facundo Batista16ed5b42007-07-24 21:20:42 +000067 self.assertEqual(smtp.local_hostname, "testhost")
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000068 smtp.close()
Facundo Batista16ed5b42007-07-24 21:20:42 +000069
Facundo Batista366d6262007-03-28 18:25:54 +000070 def testTimeoutDefault(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000071 self.assertTrue(socket.getdefaulttimeout() is None)
72 socket.setdefaulttimeout(30)
73 try:
74 smtp = smtplib.SMTP(HOST, self.port)
75 finally:
76 socket.setdefaulttimeout(None)
Facundo Batista366d6262007-03-28 18:25:54 +000077 self.assertEqual(smtp.sock.gettimeout(), 30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000078 smtp.close()
Facundo Batista366d6262007-03-28 18:25:54 +000079
80 def testTimeoutNone(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000081 self.assertTrue(socket.getdefaulttimeout() is None)
Facundo Batista366d6262007-03-28 18:25:54 +000082 socket.setdefaulttimeout(30)
83 try:
Trent Nelsone41b0062008-04-08 23:47:30 +000084 smtp = smtplib.SMTP(HOST, self.port, timeout=None)
Facundo Batista366d6262007-03-28 18:25:54 +000085 finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000086 socket.setdefaulttimeout(None)
87 self.assertTrue(smtp.sock.gettimeout() is None)
88 smtp.close()
89
90 def testTimeoutValue(self):
91 smtp = smtplib.SMTP(HOST, self.port, timeout=30)
Facundo Batista366d6262007-03-28 18:25:54 +000092 self.assertEqual(smtp.sock.gettimeout(), 30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000093 smtp.close()
Facundo Batista366d6262007-03-28 18:25:54 +000094
95
Facundo Batista1bc8d632007-08-21 16:57:18 +000096# Test server thread using the specified SMTP server class
Trent Nelsone41b0062008-04-08 23:47:30 +000097def debugging_server(serv, serv_evt, client_evt):
Neal Norwitz75992ed2008-02-26 08:04:59 +000098 serv_evt.set()
Facundo Batista16ed5b42007-07-24 21:20:42 +000099
100 try:
Facundo Batista412b8b62007-08-01 23:18:36 +0000101 if hasattr(select, 'poll'):
102 poll_fun = asyncore.poll2
103 else:
104 poll_fun = asyncore.poll
105
106 n = 1000
107 while asyncore.socket_map and n > 0:
108 poll_fun(0.01, asyncore.socket_map)
109
110 # when the client conversation is finished, it will
111 # set client_evt, and it's then ok to kill the server
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000112 if client_evt.is_set():
Facundo Batista412b8b62007-08-01 23:18:36 +0000113 serv.close()
114 break
115
116 n -= 1
117
Facundo Batista16ed5b42007-07-24 21:20:42 +0000118 except socket.timeout:
119 pass
120 finally:
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000121 if not client_evt.is_set():
Neal Norwitz75992ed2008-02-26 08:04:59 +0000122 # allow some time for the client to read the result
123 time.sleep(0.5)
124 serv.close()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000125 asyncore.close_all()
Facundo Batista412b8b62007-08-01 23:18:36 +0000126 serv_evt.set()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000127
128MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
129MSG_END = '------------ END MESSAGE ------------\n'
130
Facundo Batista1bc8d632007-08-21 16:57:18 +0000131# NOTE: Some SMTP objects in the tests below are created with a non-default
132# local_hostname argument to the constructor, since (on some systems) the FQDN
133# lookup caused by the default local_hostname sometimes takes so long that the
Facundo Batista412b8b62007-08-01 23:18:36 +0000134# test server times out, causing the test to fail.
Facundo Batista1bc8d632007-08-21 16:57:18 +0000135
136# Test behavior of smtpd.DebuggingServer
Facundo Batista16ed5b42007-07-24 21:20:42 +0000137class DebuggingServerTests(TestCase):
138
139 def setUp(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000140 # temporarily replace sys.stdout to capture DebuggingServer output
Facundo Batista16ed5b42007-07-24 21:20:42 +0000141 self.old_stdout = sys.stdout
142 self.output = StringIO.StringIO()
143 sys.stdout = self.output
144
Facundo Batista412b8b62007-08-01 23:18:36 +0000145 self.serv_evt = threading.Event()
146 self.client_evt = threading.Event()
Antoine Pitrou838c1ee2010-04-30 23:10:44 +0000147 # Pick a random unused port by passing 0 for the port number
148 self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1))
149 # Keep a note of what port was assigned
150 self.port = self.serv.socket.getsockname()[1]
Trent Nelsone41b0062008-04-08 23:47:30 +0000151 serv_args = (self.serv, self.serv_evt, self.client_evt)
Facundo Batista412b8b62007-08-01 23:18:36 +0000152 threading.Thread(target=debugging_server, args=serv_args).start()
153
154 # wait until server thread has assigned a port number
Neal Norwitz75992ed2008-02-26 08:04:59 +0000155 self.serv_evt.wait()
156 self.serv_evt.clear()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000157
158 def tearDown(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000159 # indicate that the client is finished
160 self.client_evt.set()
161 # wait for the server thread to terminate
162 self.serv_evt.wait()
163 # restore sys.stdout
Facundo Batista16ed5b42007-07-24 21:20:42 +0000164 sys.stdout = self.old_stdout
165
166 def testBasic(self):
167 # connect
Trent Nelsone41b0062008-04-08 23:47:30 +0000168 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista412b8b62007-08-01 23:18:36 +0000169 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000170
Facundo Batista1bc8d632007-08-21 16:57:18 +0000171 def testNOOP(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000172 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000173 expected = (250, 'Ok')
174 self.assertEqual(smtp.noop(), expected)
175 smtp.quit()
176
177 def testRSET(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.rset(), expected)
181 smtp.quit()
182
183 def testNotImplemented(self):
184 # EHLO isn't implemented in DebuggingServer
Trent Nelsone41b0062008-04-08 23:47:30 +0000185 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista412b8b62007-08-01 23:18:36 +0000186 expected = (502, 'Error: command "EHLO" not implemented')
187 self.assertEqual(smtp.ehlo(), expected)
188 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000189
Facundo Batista1bc8d632007-08-21 16:57:18 +0000190 def testVRFY(self):
191 # VRFY isn't implemented in DebuggingServer
Trent Nelsone41b0062008-04-08 23:47:30 +0000192 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000193 expected = (502, 'Error: command "VRFY" not implemented')
194 self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
195 self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
196 smtp.quit()
197
198 def testSecondHELO(self):
199 # check that a second HELO returns a message that it's a duplicate
200 # (this behavior is specific to smtpd.SMTPChannel)
Trent Nelsone41b0062008-04-08 23:47:30 +0000201 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000202 smtp.helo()
203 expected = (503, 'Duplicate HELO/EHLO')
204 self.assertEqual(smtp.helo(), expected)
205 smtp.quit()
206
Facundo Batista16ed5b42007-07-24 21:20:42 +0000207 def testHELP(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000208 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000209 self.assertEqual(smtp.help(), 'Error: command "HELP" not implemented')
Facundo Batista412b8b62007-08-01 23:18:36 +0000210 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000211
212 def testSend(self):
213 # connect and send mail
214 m = 'A test message'
Trent Nelsone41b0062008-04-08 23:47:30 +0000215 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000216 smtp.sendmail('John', 'Sally', m)
Neal Norwitze39be532008-08-25 03:52:40 +0000217 # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
218 # in asyncore. This sleep might help, but should really be fixed
219 # properly by using an Event variable.
220 time.sleep(0.01)
Facundo Batista412b8b62007-08-01 23:18:36 +0000221 smtp.quit()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000222
Facundo Batista412b8b62007-08-01 23:18:36 +0000223 self.client_evt.set()
224 self.serv_evt.wait()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000225 self.output.flush()
226 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
227 self.assertEqual(self.output.getvalue(), mexpect)
228
229
Neal Norwitz75992ed2008-02-26 08:04:59 +0000230class NonConnectingTests(TestCase):
231
232 def testNotConnected(self):
233 # Test various operations on an unconnected SMTP object that
234 # should raise exceptions (at present the attempt in SMTP.send
235 # to reference the nonexistent 'sock' attribute of the SMTP object
236 # causes an AttributeError)
237 smtp = smtplib.SMTP()
238 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
239 self.assertRaises(smtplib.SMTPServerDisconnected,
240 smtp.send, 'test msg')
241
242 def testNonnumericPort(self):
243 # check that non-numeric port raises socket.error
244 self.assertRaises(socket.error, smtplib.SMTP,
245 "localhost", "bogus")
246 self.assertRaises(socket.error, smtplib.SMTP,
247 "localhost:bogus")
248
249
Facundo Batista1bc8d632007-08-21 16:57:18 +0000250# test response of client to a non-successful HELO message
Facundo Batista16ed5b42007-07-24 21:20:42 +0000251class BadHELOServerTests(TestCase):
252
253 def setUp(self):
254 self.old_stdout = sys.stdout
255 self.output = StringIO.StringIO()
256 sys.stdout = self.output
257
258 self.evt = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +0000259 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
260 self.sock.settimeout(15)
261 self.port = test_support.bind_port(self.sock)
262 servargs = (self.evt, "199 no hello for you!\n", self.sock)
Facundo Batista16ed5b42007-07-24 21:20:42 +0000263 threading.Thread(target=server, args=servargs).start()
Neal Norwitz75992ed2008-02-26 08:04:59 +0000264 self.evt.wait()
265 self.evt.clear()
Facundo Batista16ed5b42007-07-24 21:20:42 +0000266
267 def tearDown(self):
268 self.evt.wait()
269 sys.stdout = self.old_stdout
270
271 def testFailingHELO(self):
Facundo Batista412b8b62007-08-01 23:18:36 +0000272 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
Trent Nelsone41b0062008-04-08 23:47:30 +0000273 HOST, self.port, 'localhost', 3)
Facundo Batista366d6262007-03-28 18:25:54 +0000274
Facundo Batista1bc8d632007-08-21 16:57:18 +0000275
276sim_users = {'Mr.A@somewhere.com':'John A',
277 'Ms.B@somewhere.com':'Sally B',
278 'Mrs.C@somewhereesle.com':'Ruth C',
279 }
280
R. David Murray7a458e82009-05-24 14:48:53 +0000281sim_auth = ('Mr.A@somewhere.com', 'somepassword')
R. David Murray4ba681e2009-05-28 19:20:58 +0000282sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
283 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
284sim_auth_credentials = {
285 'login': 'TXIuQUBzb21ld2hlcmUuY29t',
286 'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=',
287 'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ'
288 'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'),
289 }
290sim_auth_login_password = 'C29TZXBHC3N3B3JK'
R. David Murray7a458e82009-05-24 14:48:53 +0000291
Facundo Batista1bc8d632007-08-21 16:57:18 +0000292sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
293 'list-2':['Ms.B@somewhere.com',],
294 }
295
296# Simulated SMTP channel & server
297class SimSMTPChannel(smtpd.SMTPChannel):
R. David Murray4ba681e2009-05-28 19:20:58 +0000298
R. David Murray7e9d75a2009-05-29 17:38:16 +0000299 def __init__(self, extra_features, *args, **kw):
300 self._extrafeatures = ''.join(
301 [ "250-{0}\r\n".format(x) for x in extra_features ])
R. David Murray4ba681e2009-05-28 19:20:58 +0000302 smtpd.SMTPChannel.__init__(self, *args, **kw)
303
Facundo Batista1bc8d632007-08-21 16:57:18 +0000304 def smtp_EHLO(self, arg):
R. David Murray4ba681e2009-05-28 19:20:58 +0000305 resp = ('250-testhost\r\n'
306 '250-EXPN\r\n'
307 '250-SIZE 20000000\r\n'
308 '250-STARTTLS\r\n'
309 '250-DELIVERBY\r\n')
310 resp = resp + self._extrafeatures + '250 HELP'
Facundo Batista1bc8d632007-08-21 16:57:18 +0000311 self.push(resp)
312
313 def smtp_VRFY(self, arg):
Facundo Batista1bc8d632007-08-21 16:57:18 +0000314 raw_addr = email.utils.parseaddr(arg)[1]
315 quoted_addr = smtplib.quoteaddr(arg)
316 if raw_addr in sim_users:
317 self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr))
318 else:
319 self.push('550 No such user: %s' % arg)
320
321 def smtp_EXPN(self, arg):
Facundo Batista1bc8d632007-08-21 16:57:18 +0000322 list_name = email.utils.parseaddr(arg)[1].lower()
323 if list_name in sim_lists:
324 user_list = sim_lists[list_name]
325 for n, user_email in enumerate(user_list):
326 quoted_addr = smtplib.quoteaddr(user_email)
327 if n < len(user_list) - 1:
328 self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
329 else:
330 self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
331 else:
332 self.push('550 No access for you!')
333
R. David Murray7a458e82009-05-24 14:48:53 +0000334 def smtp_AUTH(self, arg):
R. David Murray4ba681e2009-05-28 19:20:58 +0000335 if arg.strip().lower()=='cram-md5':
336 self.push('334 {0}'.format(sim_cram_md5_challenge))
337 return
R. David Murray7a458e82009-05-24 14:48:53 +0000338 mech, auth = arg.split()
R. David Murray4ba681e2009-05-28 19:20:58 +0000339 mech = mech.lower()
340 if mech not in sim_auth_credentials:
R. David Murray7a458e82009-05-24 14:48:53 +0000341 self.push('504 auth type unimplemented')
R. David Murray4ba681e2009-05-28 19:20:58 +0000342 return
343 if mech == 'plain' and auth==sim_auth_credentials['plain']:
344 self.push('235 plain auth ok')
345 elif mech=='login' and auth==sim_auth_credentials['login']:
346 self.push('334 Password:')
347 else:
348 self.push('550 No access for you!')
R. David Murray7a458e82009-05-24 14:48:53 +0000349
Facundo Batista1bc8d632007-08-21 16:57:18 +0000350
351class SimSMTPServer(smtpd.SMTPServer):
R. David Murray4ba681e2009-05-28 19:20:58 +0000352
R. David Murray7e9d75a2009-05-29 17:38:16 +0000353 def __init__(self, *args, **kw):
354 self._extra_features = []
355 smtpd.SMTPServer.__init__(self, *args, **kw)
356
Facundo Batista1bc8d632007-08-21 16:57:18 +0000357 def handle_accept(self):
358 conn, addr = self.accept()
R. David Murray7e9d75a2009-05-29 17:38:16 +0000359 self._SMTPchannel = SimSMTPChannel(self._extra_features,
360 self, conn, addr)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000361
362 def process_message(self, peer, mailfrom, rcpttos, data):
363 pass
364
R. David Murray4ba681e2009-05-28 19:20:58 +0000365 def add_feature(self, feature):
R. David Murray7e9d75a2009-05-29 17:38:16 +0000366 self._extra_features.append(feature)
R. David Murray4ba681e2009-05-28 19:20:58 +0000367
Facundo Batista1bc8d632007-08-21 16:57:18 +0000368
369# Test various SMTP & ESMTP commands/behaviors that require a simulated server
370# (i.e., something with more features than DebuggingServer)
371class SMTPSimTests(TestCase):
372
373 def setUp(self):
374 self.serv_evt = threading.Event()
375 self.client_evt = threading.Event()
Antoine Pitrou838c1ee2010-04-30 23:10:44 +0000376 # Pick a random unused port by passing 0 for the port number
377 self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1))
378 # Keep a note of what port was assigned
379 self.port = self.serv.socket.getsockname()[1]
Trent Nelsone41b0062008-04-08 23:47:30 +0000380 serv_args = (self.serv, self.serv_evt, self.client_evt)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000381 threading.Thread(target=debugging_server, args=serv_args).start()
382
383 # wait until server thread has assigned a port number
Neal Norwitz75992ed2008-02-26 08:04:59 +0000384 self.serv_evt.wait()
385 self.serv_evt.clear()
Facundo Batista1bc8d632007-08-21 16:57:18 +0000386
387 def tearDown(self):
388 # indicate that the client is finished
389 self.client_evt.set()
390 # wait for the server thread to terminate
391 self.serv_evt.wait()
392
393 def testBasic(self):
394 # smoke test
Trent Nelsone41b0062008-04-08 23:47:30 +0000395 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000396 smtp.quit()
397
398 def testEHLO(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000399 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000400
401 # no features should be present before the EHLO
402 self.assertEqual(smtp.esmtp_features, {})
403
404 # features expected from the test server
405 expected_features = {'expn':'',
406 'size': '20000000',
407 'starttls': '',
408 'deliverby': '',
409 'help': '',
410 }
411
412 smtp.ehlo()
413 self.assertEqual(smtp.esmtp_features, expected_features)
414 for k in expected_features:
415 self.assertTrue(smtp.has_extn(k))
416 self.assertFalse(smtp.has_extn('unsupported-feature'))
417 smtp.quit()
418
419 def testVRFY(self):
Trent Nelsone41b0062008-04-08 23:47:30 +0000420 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000421
422 for email, name in sim_users.items():
423 expected_known = (250, '%s %s' % (name, smtplib.quoteaddr(email)))
424 self.assertEqual(smtp.vrfy(email), expected_known)
425
426 u = 'nobody@nowhere.com'
427 expected_unknown = (550, 'No such user: %s' % smtplib.quoteaddr(u))
428 self.assertEqual(smtp.vrfy(u), expected_unknown)
429 smtp.quit()
430
431 def testEXPN(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 listname, members in sim_lists.items():
435 users = []
436 for m in members:
437 users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
438 expected_known = (250, '\n'.join(users))
439 self.assertEqual(smtp.expn(listname), expected_known)
440
441 u = 'PSU-Members-List'
442 expected_unknown = (550, 'No access for you!')
443 self.assertEqual(smtp.expn(u), expected_unknown)
444 smtp.quit()
445
R. David Murray4ba681e2009-05-28 19:20:58 +0000446 def testAUTH_PLAIN(self):
R. David Murray4ba681e2009-05-28 19:20:58 +0000447 self.serv.add_feature("AUTH PLAIN")
R. David Murray7e9d75a2009-05-29 17:38:16 +0000448 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murray7a458e82009-05-24 14:48:53 +0000449
R. David Murray4ba681e2009-05-28 19:20:58 +0000450 expected_auth_ok = (235, b'plain auth ok')
R. David Murray7a458e82009-05-24 14:48:53 +0000451 self.assertEqual(smtp.login(sim_auth[0], sim_auth[1]), expected_auth_ok)
Facundo Batista1bc8d632007-08-21 16:57:18 +0000452
R. David Murray4ba681e2009-05-28 19:20:58 +0000453 # SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they
454 # require a synchronous read to obtain the credentials...so instead smtpd
455 # sees the credential sent by smtplib's login method as an unknown command,
456 # which results in smtplib raising an auth error. Fortunately the error
457 # message contains the encoded credential, so we can partially check that it
458 # was generated correctly (partially, because the 'word' is uppercased in
459 # the error message).
460
461 def testAUTH_LOGIN(self):
R. David Murray4ba681e2009-05-28 19:20:58 +0000462 self.serv.add_feature("AUTH LOGIN")
R. David Murray7e9d75a2009-05-29 17:38:16 +0000463 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murray4ba681e2009-05-28 19:20:58 +0000464 try: smtp.login(sim_auth[0], sim_auth[1])
465 except smtplib.SMTPAuthenticationError as err:
466 if sim_auth_login_password not in str(err):
467 raise "expected encoded password not found in error message"
468
469 def testAUTH_CRAM_MD5(self):
R. David Murray4ba681e2009-05-28 19:20:58 +0000470 self.serv.add_feature("AUTH CRAM-MD5")
R. David Murray7e9d75a2009-05-29 17:38:16 +0000471 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murray4ba681e2009-05-28 19:20:58 +0000472
473 try: smtp.login(sim_auth[0], sim_auth[1])
474 except smtplib.SMTPAuthenticationError as err:
475 if sim_auth_credentials['cram-md5'] not in str(err):
476 raise "expected encoded credentials not found in error message"
477
478 #TODO: add tests for correct AUTH method fallback now that the
479 #test infrastructure can support it.
480
Facundo Batista1bc8d632007-08-21 16:57:18 +0000481
Facundo Batista366d6262007-03-28 18:25:54 +0000482def test_main(verbose=None):
Facundo Batista412b8b62007-08-01 23:18:36 +0000483 test_support.run_unittest(GeneralTests, DebuggingServerTests,
Neal Norwitz75992ed2008-02-26 08:04:59 +0000484 NonConnectingTests,
Facundo Batista1bc8d632007-08-21 16:57:18 +0000485 BadHELOServerTests, SMTPSimTests)
Facundo Batista366d6262007-03-28 18:25:54 +0000486
487if __name__ == '__main__':
488 test_main()