blob: 8e362414288463b85d4a6b454350fbd1cb8555f7 [file] [log] [blame]
Guido van Rossum806c2462007-08-06 23:33:07 +00001import asyncore
R. David Murray7dff9e02010-11-08 17:15:13 +00002import email.mime.text
R David Murray83084442015-05-17 19:27:22 -04003from email.message import EmailMessage
Barry Warsawc5ea7542015-07-09 10:39:55 -04004from email.base64mime import body_encode as encode_base64
Guido van Rossum04110fb2007-08-24 16:32:05 +00005import email.utils
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import socket
Guido van Rossum806c2462007-08-06 23:33:07 +00007import smtpd
Guido van Rossumd8faa362007-04-27 19:54:29 +00008import smtplib
Guido van Rossum806c2462007-08-06 23:33:07 +00009import io
R. David Murray7dff9e02010-11-08 17:15:13 +000010import re
Guido van Rossum806c2462007-08-06 23:33:07 +000011import sys
Guido van Rossumd8faa362007-04-27 19:54:29 +000012import time
Guido van Rossum806c2462007-08-06 23:33:07 +000013import select
Ross Lagerwall86407432012-03-29 18:08:48 +020014import errno
R David Murray83084442015-05-17 19:27:22 -040015import textwrap
Guido van Rossumd8faa362007-04-27 19:54:29 +000016
Victor Stinner45df8202010-04-28 22:31:17 +000017import unittest
Richard Jones64b02de2010-08-03 06:39:33 +000018from test import support, mock_socket
Guido van Rossumd8faa362007-04-27 19:54:29 +000019
Victor Stinner45df8202010-04-28 22:31:17 +000020try:
21 import threading
22except ImportError:
23 threading = None
24
Benjamin Petersonee8712c2008-05-20 21:35:26 +000025HOST = support.HOST
Guido van Rossumd8faa362007-04-27 19:54:29 +000026
Josiah Carlsond74900e2008-07-07 04:15:08 +000027if sys.platform == 'darwin':
28 # select.poll returns a select.POLLHUP at the end of the tests
29 # on darwin, so just ignore it
30 def handle_expt(self):
31 pass
32 smtpd.SMTPChannel.handle_expt = handle_expt
33
34
Christian Heimes5e696852008-04-09 08:37:03 +000035def server(evt, buf, serv):
Charles-François Natali6e204602014-07-23 19:28:13 +010036 serv.listen()
Christian Heimes380f7f22008-02-28 11:19:05 +000037 evt.set()
Guido van Rossumd8faa362007-04-27 19:54:29 +000038 try:
39 conn, addr = serv.accept()
40 except socket.timeout:
41 pass
42 else:
Guido van Rossum806c2462007-08-06 23:33:07 +000043 n = 500
44 while buf and n > 0:
45 r, w, e = select.select([], [conn], [])
46 if w:
47 sent = conn.send(buf)
48 buf = buf[sent:]
49
50 n -= 1
Guido van Rossum806c2462007-08-06 23:33:07 +000051
Guido van Rossumd8faa362007-04-27 19:54:29 +000052 conn.close()
53 finally:
54 serv.close()
55 evt.set()
56
Victor Stinner45df8202010-04-28 22:31:17 +000057class GeneralTests(unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +000058
59 def setUp(self):
Richard Jones64b02de2010-08-03 06:39:33 +000060 smtplib.socket = mock_socket
61 self.port = 25
Guido van Rossumd8faa362007-04-27 19:54:29 +000062
63 def tearDown(self):
Richard Jones64b02de2010-08-03 06:39:33 +000064 smtplib.socket = socket
Guido van Rossumd8faa362007-04-27 19:54:29 +000065
R. David Murray7dff9e02010-11-08 17:15:13 +000066 # This method is no longer used but is retained for backward compatibility,
67 # so test to make sure it still works.
68 def testQuoteData(self):
69 teststr = "abc\n.jkl\rfoo\r\n..blue"
70 expected = "abc\r\n..jkl\r\nfoo\r\n...blue"
71 self.assertEqual(expected, smtplib.quotedata(teststr))
72
Guido van Rossum806c2462007-08-06 23:33:07 +000073 def testBasic1(self):
Richard Jones64b02de2010-08-03 06:39:33 +000074 mock_socket.reply_with(b"220 Hola mundo")
Guido van Rossumd8faa362007-04-27 19:54:29 +000075 # connects
Christian Heimes5e696852008-04-09 08:37:03 +000076 smtp = smtplib.SMTP(HOST, self.port)
Georg Brandlf78e02b2008-06-10 17:40:04 +000077 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +000078
Senthil Kumaran3d23fd62011-07-30 10:56:50 +080079 def testSourceAddress(self):
80 mock_socket.reply_with(b"220 Hola mundo")
81 # connects
82 smtp = smtplib.SMTP(HOST, self.port,
83 source_address=('127.0.0.1',19876))
84 self.assertEqual(smtp.source_address, ('127.0.0.1', 19876))
85 smtp.close()
86
Guido van Rossum806c2462007-08-06 23:33:07 +000087 def testBasic2(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +000088 mock_socket.reply_with(b"220 Hola mundo")
Guido van Rossum806c2462007-08-06 23:33:07 +000089 # connects, include port in host name
Christian Heimes5e696852008-04-09 08:37:03 +000090 smtp = smtplib.SMTP("%s:%s" % (HOST, self.port))
Georg Brandlf78e02b2008-06-10 17:40:04 +000091 smtp.close()
Guido van Rossum806c2462007-08-06 23:33:07 +000092
93 def testLocalHostName(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +000094 mock_socket.reply_with(b"220 Hola mundo")
Guido van Rossum806c2462007-08-06 23:33:07 +000095 # check that supplied local_hostname is used
Christian Heimes5e696852008-04-09 08:37:03 +000096 smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost")
Guido van Rossum806c2462007-08-06 23:33:07 +000097 self.assertEqual(smtp.local_hostname, "testhost")
Georg Brandlf78e02b2008-06-10 17:40:04 +000098 smtp.close()
Guido van Rossum806c2462007-08-06 23:33:07 +000099
Guido van Rossumd8faa362007-04-27 19:54:29 +0000100 def testTimeoutDefault(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000101 mock_socket.reply_with(b"220 Hola mundo")
Serhiy Storchaka578c6772014-02-08 15:06:08 +0200102 self.assertIsNone(mock_socket.getdefaulttimeout())
Richard Jones64b02de2010-08-03 06:39:33 +0000103 mock_socket.setdefaulttimeout(30)
104 self.assertEqual(mock_socket.getdefaulttimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000105 try:
106 smtp = smtplib.SMTP(HOST, self.port)
107 finally:
Richard Jones64b02de2010-08-03 06:39:33 +0000108 mock_socket.setdefaulttimeout(None)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000109 self.assertEqual(smtp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000110 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000111
112 def testTimeoutNone(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000113 mock_socket.reply_with(b"220 Hola mundo")
Serhiy Storchaka578c6772014-02-08 15:06:08 +0200114 self.assertIsNone(socket.getdefaulttimeout())
Guido van Rossumd8faa362007-04-27 19:54:29 +0000115 socket.setdefaulttimeout(30)
116 try:
Christian Heimes5e696852008-04-09 08:37:03 +0000117 smtp = smtplib.SMTP(HOST, self.port, timeout=None)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000118 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000119 socket.setdefaulttimeout(None)
Serhiy Storchaka578c6772014-02-08 15:06:08 +0200120 self.assertIsNone(smtp.sock.gettimeout())
Georg Brandlf78e02b2008-06-10 17:40:04 +0000121 smtp.close()
122
123 def testTimeoutValue(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000124 mock_socket.reply_with(b"220 Hola mundo")
Georg Brandlf78e02b2008-06-10 17:40:04 +0000125 smtp = smtplib.SMTP(HOST, self.port, timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000126 self.assertEqual(smtp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000127 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000128
R David Murray0c49b892015-04-16 17:14:42 -0400129 def test_debuglevel(self):
130 mock_socket.reply_with(b"220 Hello world")
131 smtp = smtplib.SMTP()
132 smtp.set_debuglevel(1)
133 with support.captured_stderr() as stderr:
134 smtp.connect(HOST, self.port)
135 smtp.close()
136 expected = re.compile(r"^connect:", re.MULTILINE)
137 self.assertRegex(stderr.getvalue(), expected)
138
139 def test_debuglevel_2(self):
140 mock_socket.reply_with(b"220 Hello world")
141 smtp = smtplib.SMTP()
142 smtp.set_debuglevel(2)
143 with support.captured_stderr() as stderr:
144 smtp.connect(HOST, self.port)
145 smtp.close()
146 expected = re.compile(r"^\d{2}:\d{2}:\d{2}\.\d{6} connect: ",
147 re.MULTILINE)
148 self.assertRegex(stderr.getvalue(), expected)
149
Guido van Rossumd8faa362007-04-27 19:54:29 +0000150
Guido van Rossum04110fb2007-08-24 16:32:05 +0000151# Test server thread using the specified SMTP server class
Christian Heimes5e696852008-04-09 08:37:03 +0000152def debugging_server(serv, serv_evt, client_evt):
Christian Heimes380f7f22008-02-28 11:19:05 +0000153 serv_evt.set()
Guido van Rossum806c2462007-08-06 23:33:07 +0000154
155 try:
156 if hasattr(select, 'poll'):
157 poll_fun = asyncore.poll2
158 else:
159 poll_fun = asyncore.poll
160
161 n = 1000
162 while asyncore.socket_map and n > 0:
163 poll_fun(0.01, asyncore.socket_map)
164
165 # when the client conversation is finished, it will
166 # set client_evt, and it's then ok to kill the server
Benjamin Peterson672b8032008-06-11 19:14:14 +0000167 if client_evt.is_set():
Guido van Rossum806c2462007-08-06 23:33:07 +0000168 serv.close()
169 break
170
171 n -= 1
172
173 except socket.timeout:
174 pass
175 finally:
Benjamin Peterson672b8032008-06-11 19:14:14 +0000176 if not client_evt.is_set():
Christian Heimes380f7f22008-02-28 11:19:05 +0000177 # allow some time for the client to read the result
178 time.sleep(0.5)
179 serv.close()
Guido van Rossum806c2462007-08-06 23:33:07 +0000180 asyncore.close_all()
Guido van Rossum806c2462007-08-06 23:33:07 +0000181 serv_evt.set()
182
183MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
184MSG_END = '------------ END MESSAGE ------------\n'
185
Guido van Rossum04110fb2007-08-24 16:32:05 +0000186# NOTE: Some SMTP objects in the tests below are created with a non-default
187# local_hostname argument to the constructor, since (on some systems) the FQDN
188# lookup caused by the default local_hostname sometimes takes so long that the
Guido van Rossum806c2462007-08-06 23:33:07 +0000189# test server times out, causing the test to fail.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000190
191# Test behavior of smtpd.DebuggingServer
Victor Stinner45df8202010-04-28 22:31:17 +0000192@unittest.skipUnless(threading, 'Threading required for this test.')
193class DebuggingServerTests(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +0000194
R. David Murray7dff9e02010-11-08 17:15:13 +0000195 maxDiff = None
196
Guido van Rossum806c2462007-08-06 23:33:07 +0000197 def setUp(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000198 self.real_getfqdn = socket.getfqdn
199 socket.getfqdn = mock_socket.getfqdn
Guido van Rossum806c2462007-08-06 23:33:07 +0000200 # temporarily replace sys.stdout to capture DebuggingServer output
201 self.old_stdout = sys.stdout
202 self.output = io.StringIO()
203 sys.stdout = self.output
204
205 self.serv_evt = threading.Event()
206 self.client_evt = threading.Event()
R. David Murray7dff9e02010-11-08 17:15:13 +0000207 # Capture SMTPChannel debug output
208 self.old_DEBUGSTREAM = smtpd.DEBUGSTREAM
209 smtpd.DEBUGSTREAM = io.StringIO()
Antoine Pitrou043bad02010-04-30 23:20:15 +0000210 # Pick a random unused port by passing 0 for the port number
R David Murray1144da52014-06-11 12:27:40 -0400211 self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1),
212 decode_data=True)
Antoine Pitrou043bad02010-04-30 23:20:15 +0000213 # Keep a note of what port was assigned
214 self.port = self.serv.socket.getsockname()[1]
Christian Heimes5e696852008-04-09 08:37:03 +0000215 serv_args = (self.serv, self.serv_evt, self.client_evt)
Antoine Pitrouc3d47722009-10-27 19:49:45 +0000216 self.thread = threading.Thread(target=debugging_server, args=serv_args)
217 self.thread.start()
Guido van Rossum806c2462007-08-06 23:33:07 +0000218
219 # wait until server thread has assigned a port number
Christian Heimes380f7f22008-02-28 11:19:05 +0000220 self.serv_evt.wait()
221 self.serv_evt.clear()
Guido van Rossum806c2462007-08-06 23:33:07 +0000222
223 def tearDown(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000224 socket.getfqdn = self.real_getfqdn
Guido van Rossum806c2462007-08-06 23:33:07 +0000225 # indicate that the client is finished
226 self.client_evt.set()
227 # wait for the server thread to terminate
228 self.serv_evt.wait()
Antoine Pitrouc3d47722009-10-27 19:49:45 +0000229 self.thread.join()
Guido van Rossum806c2462007-08-06 23:33:07 +0000230 # restore sys.stdout
231 sys.stdout = self.old_stdout
R. David Murray7dff9e02010-11-08 17:15:13 +0000232 # restore DEBUGSTREAM
233 smtpd.DEBUGSTREAM.close()
234 smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM
Guido van Rossum806c2462007-08-06 23:33:07 +0000235
236 def testBasic(self):
237 # connect
Christian Heimes5e696852008-04-09 08:37:03 +0000238 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000239 smtp.quit()
240
Senthil Kumaran3d23fd62011-07-30 10:56:50 +0800241 def testSourceAddress(self):
242 # connect
Senthil Kumaranb351a482011-07-31 09:14:17 +0800243 port = support.find_unused_port()
244 try:
245 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
246 timeout=3, source_address=('127.0.0.1', port))
247 self.assertEqual(smtp.source_address, ('127.0.0.1', port))
248 self.assertEqual(smtp.local_hostname, 'localhost')
249 smtp.quit()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200250 except OSError as e:
Senthil Kumaranb351a482011-07-31 09:14:17 +0800251 if e.errno == errno.EADDRINUSE:
252 self.skipTest("couldn't bind to port %d" % port)
253 raise
Senthil Kumaran3d23fd62011-07-30 10:56:50 +0800254
Guido van Rossum04110fb2007-08-24 16:32:05 +0000255 def testNOOP(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000256 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
R David Murrayd1a30c92012-05-26 14:33:59 -0400257 expected = (250, b'OK')
Guido van Rossum04110fb2007-08-24 16:32:05 +0000258 self.assertEqual(smtp.noop(), expected)
259 smtp.quit()
260
261 def testRSET(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000262 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
R David Murrayd1a30c92012-05-26 14:33:59 -0400263 expected = (250, b'OK')
Guido van Rossum04110fb2007-08-24 16:32:05 +0000264 self.assertEqual(smtp.rset(), expected)
265 smtp.quit()
266
Benjamin Peterson1eca0622013-09-29 10:46:31 -0400267 def testELHO(self):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000268 # EHLO isn't implemented in DebuggingServer
Christian Heimes5e696852008-04-09 08:37:03 +0000269 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Benjamin Peterson1eca0622013-09-29 10:46:31 -0400270 expected = (250, b'\nSIZE 33554432\nHELP')
Guido van Rossum806c2462007-08-06 23:33:07 +0000271 self.assertEqual(smtp.ehlo(), expected)
272 smtp.quit()
273
Benjamin Peterson1eca0622013-09-29 10:46:31 -0400274 def testEXPNNotImplemented(self):
R David Murrayd1a30c92012-05-26 14:33:59 -0400275 # EXPN isn't implemented in DebuggingServer
Christian Heimes5e696852008-04-09 08:37:03 +0000276 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
R David Murrayd1a30c92012-05-26 14:33:59 -0400277 expected = (502, b'EXPN not implemented')
278 smtp.putcmd('EXPN')
279 self.assertEqual(smtp.getreply(), expected)
280 smtp.quit()
281
282 def testVRFY(self):
283 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
284 expected = (252, b'Cannot VRFY user, but will accept message ' + \
285 b'and attempt delivery')
Guido van Rossum04110fb2007-08-24 16:32:05 +0000286 self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
287 self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
288 smtp.quit()
289
290 def testSecondHELO(self):
291 # check that a second HELO returns a message that it's a duplicate
292 # (this behavior is specific to smtpd.SMTPChannel)
Christian Heimes5e696852008-04-09 08:37:03 +0000293 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000294 smtp.helo()
295 expected = (503, b'Duplicate HELO/EHLO')
296 self.assertEqual(smtp.helo(), expected)
297 smtp.quit()
298
Guido van Rossum806c2462007-08-06 23:33:07 +0000299 def testHELP(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000300 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
R David Murrayd1a30c92012-05-26 14:33:59 -0400301 self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \
302 b'RCPT DATA RSET NOOP QUIT VRFY')
Guido van Rossum806c2462007-08-06 23:33:07 +0000303 smtp.quit()
304
305 def testSend(self):
306 # connect and send mail
307 m = 'A test message'
Christian Heimes5e696852008-04-09 08:37:03 +0000308 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000309 smtp.sendmail('John', 'Sally', m)
Neal Norwitz25329672008-08-25 03:55:03 +0000310 # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
311 # in asyncore. This sleep might help, but should really be fixed
312 # properly by using an Event variable.
313 time.sleep(0.01)
Guido van Rossum806c2462007-08-06 23:33:07 +0000314 smtp.quit()
315
316 self.client_evt.set()
317 self.serv_evt.wait()
318 self.output.flush()
319 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
320 self.assertEqual(self.output.getvalue(), mexpect)
321
R. David Murray7dff9e02010-11-08 17:15:13 +0000322 def testSendBinary(self):
323 m = b'A test message'
324 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
325 smtp.sendmail('John', 'Sally', m)
326 # XXX (see comment in testSend)
327 time.sleep(0.01)
328 smtp.quit()
329
330 self.client_evt.set()
331 self.serv_evt.wait()
332 self.output.flush()
333 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.decode('ascii'), MSG_END)
334 self.assertEqual(self.output.getvalue(), mexpect)
335
R David Murray0f663d02011-06-09 15:05:57 -0400336 def testSendNeedingDotQuote(self):
337 # Issue 12283
338 m = '.A test\n.mes.sage.'
339 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
340 smtp.sendmail('John', 'Sally', m)
341 # XXX (see comment in testSend)
342 time.sleep(0.01)
343 smtp.quit()
344
345 self.client_evt.set()
346 self.serv_evt.wait()
347 self.output.flush()
348 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
349 self.assertEqual(self.output.getvalue(), mexpect)
350
R David Murray46346762011-07-18 21:38:54 -0400351 def testSendNullSender(self):
352 m = 'A test message'
353 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
354 smtp.sendmail('<>', 'Sally', m)
355 # XXX (see comment in testSend)
356 time.sleep(0.01)
357 smtp.quit()
358
359 self.client_evt.set()
360 self.serv_evt.wait()
361 self.output.flush()
362 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
363 self.assertEqual(self.output.getvalue(), mexpect)
364 debugout = smtpd.DEBUGSTREAM.getvalue()
365 sender = re.compile("^sender: <>$", re.MULTILINE)
366 self.assertRegex(debugout, sender)
367
R. David Murray7dff9e02010-11-08 17:15:13 +0000368 def testSendMessage(self):
369 m = email.mime.text.MIMEText('A test message')
370 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
371 smtp.send_message(m, from_addr='John', to_addrs='Sally')
372 # XXX (see comment in testSend)
373 time.sleep(0.01)
374 smtp.quit()
375
376 self.client_evt.set()
377 self.serv_evt.wait()
378 self.output.flush()
379 # Add the X-Peer header that DebuggingServer adds
R David Murrayb912c5a2011-05-02 08:47:24 -0400380 m['X-Peer'] = socket.gethostbyname('localhost')
R. David Murray7dff9e02010-11-08 17:15:13 +0000381 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
382 self.assertEqual(self.output.getvalue(), mexpect)
383
384 def testSendMessageWithAddresses(self):
385 m = email.mime.text.MIMEText('A test message')
386 m['From'] = 'foo@bar.com'
387 m['To'] = 'John'
388 m['CC'] = 'Sally, Fred'
389 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
390 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
391 smtp.send_message(m)
392 # XXX (see comment in testSend)
393 time.sleep(0.01)
394 smtp.quit()
R David Murrayac4e5ab2011-07-02 21:03:19 -0400395 # make sure the Bcc header is still in the message.
396 self.assertEqual(m['Bcc'], 'John Root <root@localhost>, "Dinsdale" '
397 '<warped@silly.walks.com>')
R. David Murray7dff9e02010-11-08 17:15:13 +0000398
399 self.client_evt.set()
400 self.serv_evt.wait()
401 self.output.flush()
402 # Add the X-Peer header that DebuggingServer adds
R David Murrayb912c5a2011-05-02 08:47:24 -0400403 m['X-Peer'] = socket.gethostbyname('localhost')
R David Murrayac4e5ab2011-07-02 21:03:19 -0400404 # The Bcc header should not be transmitted.
R. David Murray7dff9e02010-11-08 17:15:13 +0000405 del m['Bcc']
406 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
407 self.assertEqual(self.output.getvalue(), mexpect)
408 debugout = smtpd.DEBUGSTREAM.getvalue()
409 sender = re.compile("^sender: foo@bar.com$", re.MULTILINE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000410 self.assertRegex(debugout, sender)
R. David Murray7dff9e02010-11-08 17:15:13 +0000411 for addr in ('John', 'Sally', 'Fred', 'root@localhost',
412 'warped@silly.walks.com'):
413 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
414 re.MULTILINE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000415 self.assertRegex(debugout, to_addr)
R. David Murray7dff9e02010-11-08 17:15:13 +0000416
417 def testSendMessageWithSomeAddresses(self):
418 # Make sure nothing breaks if not all of the three 'to' headers exist
419 m = email.mime.text.MIMEText('A test message')
420 m['From'] = 'foo@bar.com'
421 m['To'] = 'John, Dinsdale'
422 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
423 smtp.send_message(m)
424 # XXX (see comment in testSend)
425 time.sleep(0.01)
426 smtp.quit()
427
428 self.client_evt.set()
429 self.serv_evt.wait()
430 self.output.flush()
431 # Add the X-Peer header that DebuggingServer adds
R David Murrayb912c5a2011-05-02 08:47:24 -0400432 m['X-Peer'] = socket.gethostbyname('localhost')
R. David Murray7dff9e02010-11-08 17:15:13 +0000433 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
434 self.assertEqual(self.output.getvalue(), mexpect)
435 debugout = smtpd.DEBUGSTREAM.getvalue()
436 sender = re.compile("^sender: foo@bar.com$", re.MULTILINE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000437 self.assertRegex(debugout, sender)
R. David Murray7dff9e02010-11-08 17:15:13 +0000438 for addr in ('John', 'Dinsdale'):
439 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
440 re.MULTILINE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000441 self.assertRegex(debugout, to_addr)
R. David Murray7dff9e02010-11-08 17:15:13 +0000442
R David Murrayac4e5ab2011-07-02 21:03:19 -0400443 def testSendMessageWithSpecifiedAddresses(self):
444 # Make sure addresses specified in call override those in message.
445 m = email.mime.text.MIMEText('A test message')
446 m['From'] = 'foo@bar.com'
447 m['To'] = 'John, Dinsdale'
448 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
449 smtp.send_message(m, from_addr='joe@example.com', to_addrs='foo@example.net')
450 # XXX (see comment in testSend)
451 time.sleep(0.01)
452 smtp.quit()
453
454 self.client_evt.set()
455 self.serv_evt.wait()
456 self.output.flush()
457 # Add the X-Peer header that DebuggingServer adds
458 m['X-Peer'] = socket.gethostbyname('localhost')
459 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
460 self.assertEqual(self.output.getvalue(), mexpect)
461 debugout = smtpd.DEBUGSTREAM.getvalue()
462 sender = re.compile("^sender: joe@example.com$", re.MULTILINE)
463 self.assertRegex(debugout, sender)
464 for addr in ('John', 'Dinsdale'):
465 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
466 re.MULTILINE)
467 self.assertNotRegex(debugout, to_addr)
468 recip = re.compile(r"^recips: .*'foo@example.net'.*$", re.MULTILINE)
469 self.assertRegex(debugout, recip)
470
471 def testSendMessageWithMultipleFrom(self):
472 # Sender overrides To
473 m = email.mime.text.MIMEText('A test message')
474 m['From'] = 'Bernard, Bianca'
475 m['Sender'] = 'the_rescuers@Rescue-Aid-Society.com'
476 m['To'] = 'John, Dinsdale'
477 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
478 smtp.send_message(m)
479 # XXX (see comment in testSend)
480 time.sleep(0.01)
481 smtp.quit()
482
483 self.client_evt.set()
484 self.serv_evt.wait()
485 self.output.flush()
486 # Add the X-Peer header that DebuggingServer adds
487 m['X-Peer'] = socket.gethostbyname('localhost')
488 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
489 self.assertEqual(self.output.getvalue(), mexpect)
490 debugout = smtpd.DEBUGSTREAM.getvalue()
491 sender = re.compile("^sender: the_rescuers@Rescue-Aid-Society.com$", re.MULTILINE)
492 self.assertRegex(debugout, sender)
493 for addr in ('John', 'Dinsdale'):
494 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
495 re.MULTILINE)
496 self.assertRegex(debugout, to_addr)
497
498 def testSendMessageResent(self):
499 m = email.mime.text.MIMEText('A test message')
500 m['From'] = 'foo@bar.com'
501 m['To'] = 'John'
502 m['CC'] = 'Sally, Fred'
503 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
504 m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
505 m['Resent-From'] = 'holy@grail.net'
506 m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff'
507 m['Resent-Bcc'] = 'doe@losthope.net'
508 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
509 smtp.send_message(m)
510 # XXX (see comment in testSend)
511 time.sleep(0.01)
512 smtp.quit()
513
514 self.client_evt.set()
515 self.serv_evt.wait()
516 self.output.flush()
517 # The Resent-Bcc headers are deleted before serialization.
518 del m['Bcc']
519 del m['Resent-Bcc']
520 # Add the X-Peer header that DebuggingServer adds
521 m['X-Peer'] = socket.gethostbyname('localhost')
522 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
523 self.assertEqual(self.output.getvalue(), mexpect)
524 debugout = smtpd.DEBUGSTREAM.getvalue()
525 sender = re.compile("^sender: holy@grail.net$", re.MULTILINE)
526 self.assertRegex(debugout, sender)
527 for addr in ('my_mom@great.cooker.com', 'Jeff', 'doe@losthope.net'):
528 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
529 re.MULTILINE)
530 self.assertRegex(debugout, to_addr)
531
532 def testSendMessageMultipleResentRaises(self):
533 m = email.mime.text.MIMEText('A test message')
534 m['From'] = 'foo@bar.com'
535 m['To'] = 'John'
536 m['CC'] = 'Sally, Fred'
537 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
538 m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
539 m['Resent-From'] = 'holy@grail.net'
540 m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff'
541 m['Resent-Bcc'] = 'doe@losthope.net'
542 m['Resent-Date'] = 'Thu, 2 Jan 1970 17:42:00 +0000'
543 m['Resent-To'] = 'holy@grail.net'
544 m['Resent-From'] = 'Martha <my_mom@great.cooker.com>, Jeff'
545 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
546 with self.assertRaises(ValueError):
547 smtp.send_message(m)
548 smtp.close()
Guido van Rossum806c2462007-08-06 23:33:07 +0000549
Victor Stinner45df8202010-04-28 22:31:17 +0000550class NonConnectingTests(unittest.TestCase):
Christian Heimes380f7f22008-02-28 11:19:05 +0000551
552 def testNotConnected(self):
553 # Test various operations on an unconnected SMTP object that
554 # should raise exceptions (at present the attempt in SMTP.send
555 # to reference the nonexistent 'sock' attribute of the SMTP object
556 # causes an AttributeError)
557 smtp = smtplib.SMTP()
558 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
559 self.assertRaises(smtplib.SMTPServerDisconnected,
560 smtp.send, 'test msg')
561
562 def testNonnumericPort(self):
Andrew Svetlov0832af62012-12-18 23:10:48 +0200563 # check that non-numeric port raises OSError
Andrew Svetlov2ade6f22012-12-17 18:57:16 +0200564 self.assertRaises(OSError, smtplib.SMTP,
Christian Heimes380f7f22008-02-28 11:19:05 +0000565 "localhost", "bogus")
Andrew Svetlov2ade6f22012-12-17 18:57:16 +0200566 self.assertRaises(OSError, smtplib.SMTP,
Christian Heimes380f7f22008-02-28 11:19:05 +0000567 "localhost:bogus")
568
569
Guido van Rossum04110fb2007-08-24 16:32:05 +0000570# test response of client to a non-successful HELO message
Victor Stinner45df8202010-04-28 22:31:17 +0000571@unittest.skipUnless(threading, 'Threading required for this test.')
572class BadHELOServerTests(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +0000573
574 def setUp(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000575 smtplib.socket = mock_socket
576 mock_socket.reply_with(b"199 no hello for you!")
Guido van Rossum806c2462007-08-06 23:33:07 +0000577 self.old_stdout = sys.stdout
578 self.output = io.StringIO()
579 sys.stdout = self.output
Richard Jones64b02de2010-08-03 06:39:33 +0000580 self.port = 25
Guido van Rossum806c2462007-08-06 23:33:07 +0000581
582 def tearDown(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000583 smtplib.socket = socket
Guido van Rossum806c2462007-08-06 23:33:07 +0000584 sys.stdout = self.old_stdout
585
586 def testFailingHELO(self):
587 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
Christian Heimes5e696852008-04-09 08:37:03 +0000588 HOST, self.port, 'localhost', 3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000589
Guido van Rossum04110fb2007-08-24 16:32:05 +0000590
Georg Brandlb38b5c42014-02-10 22:11:21 +0100591@unittest.skipUnless(threading, 'Threading required for this test.')
592class TooLongLineTests(unittest.TestCase):
593 respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n'
594
595 def setUp(self):
596 self.old_stdout = sys.stdout
597 self.output = io.StringIO()
598 sys.stdout = self.output
599
600 self.evt = threading.Event()
601 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
602 self.sock.settimeout(15)
603 self.port = support.bind_port(self.sock)
604 servargs = (self.evt, self.respdata, self.sock)
605 threading.Thread(target=server, args=servargs).start()
606 self.evt.wait()
607 self.evt.clear()
608
609 def tearDown(self):
610 self.evt.wait()
611 sys.stdout = self.old_stdout
612
613 def testLineTooLong(self):
614 self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
615 HOST, self.port, 'localhost', 3)
616
617
Guido van Rossum04110fb2007-08-24 16:32:05 +0000618sim_users = {'Mr.A@somewhere.com':'John A',
R David Murray46346762011-07-18 21:38:54 -0400619 'Ms.B@xn--fo-fka.com':'Sally B',
Guido van Rossum04110fb2007-08-24 16:32:05 +0000620 'Mrs.C@somewhereesle.com':'Ruth C',
621 }
622
R. David Murraycaa27b72009-05-23 18:49:56 +0000623sim_auth = ('Mr.A@somewhere.com', 'somepassword')
R. David Murrayfb123912009-05-28 18:19:00 +0000624sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
625 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
626sim_auth_credentials = {
627 'login': 'TXIuQUBzb21ld2hlcmUuY29t',
628 'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=',
629 'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ'
630 'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'),
631 }
R David Murray76e13c12014-07-03 14:47:46 -0400632sim_auth_login_user = 'TXIUQUBZB21LD2HLCMUUY29T'
633sim_auth_plain = 'AE1YLKFAC29TZXDOZXJLLMNVBQBZB21LCGFZC3DVCMQ='
R. David Murraycaa27b72009-05-23 18:49:56 +0000634
Guido van Rossum04110fb2007-08-24 16:32:05 +0000635sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
R David Murray46346762011-07-18 21:38:54 -0400636 'list-2':['Ms.B@xn--fo-fka.com',],
Guido van Rossum04110fb2007-08-24 16:32:05 +0000637 }
638
639# Simulated SMTP channel & server
640class SimSMTPChannel(smtpd.SMTPChannel):
R. David Murrayfb123912009-05-28 18:19:00 +0000641
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400642 quit_response = None
R David Murrayd312c742013-03-20 20:36:14 -0400643 mail_response = None
644 rcpt_response = None
645 data_response = None
646 rcpt_count = 0
647 rset_count = 0
R David Murrayafb151a2014-04-14 18:21:38 -0400648 disconnect = 0
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400649
R. David Murray23ddc0e2009-05-29 18:03:16 +0000650 def __init__(self, extra_features, *args, **kw):
651 self._extrafeatures = ''.join(
652 [ "250-{0}\r\n".format(x) for x in extra_features ])
R. David Murrayfb123912009-05-28 18:19:00 +0000653 super(SimSMTPChannel, self).__init__(*args, **kw)
654
Guido van Rossum04110fb2007-08-24 16:32:05 +0000655 def smtp_EHLO(self, arg):
R. David Murrayfb123912009-05-28 18:19:00 +0000656 resp = ('250-testhost\r\n'
657 '250-EXPN\r\n'
658 '250-SIZE 20000000\r\n'
659 '250-STARTTLS\r\n'
660 '250-DELIVERBY\r\n')
661 resp = resp + self._extrafeatures + '250 HELP'
Guido van Rossum04110fb2007-08-24 16:32:05 +0000662 self.push(resp)
R David Murrayf1a40b42013-03-20 21:12:17 -0400663 self.seen_greeting = arg
664 self.extended_smtp = True
Guido van Rossum04110fb2007-08-24 16:32:05 +0000665
666 def smtp_VRFY(self, arg):
R David Murray46346762011-07-18 21:38:54 -0400667 # For max compatibility smtplib should be sending the raw address.
668 if arg in sim_users:
669 self.push('250 %s %s' % (sim_users[arg], smtplib.quoteaddr(arg)))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000670 else:
671 self.push('550 No such user: %s' % arg)
672
673 def smtp_EXPN(self, arg):
R David Murray46346762011-07-18 21:38:54 -0400674 list_name = arg.lower()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000675 if list_name in sim_lists:
676 user_list = sim_lists[list_name]
677 for n, user_email in enumerate(user_list):
678 quoted_addr = smtplib.quoteaddr(user_email)
679 if n < len(user_list) - 1:
680 self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
681 else:
682 self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
683 else:
684 self.push('550 No access for you!')
685
R. David Murraycaa27b72009-05-23 18:49:56 +0000686 def smtp_AUTH(self, arg):
R David Murray76e13c12014-07-03 14:47:46 -0400687 mech = arg.strip().lower()
688 if mech=='cram-md5':
R. David Murrayfb123912009-05-28 18:19:00 +0000689 self.push('334 {}'.format(sim_cram_md5_challenge))
R David Murray76e13c12014-07-03 14:47:46 -0400690 elif mech not in sim_auth_credentials:
R. David Murraycaa27b72009-05-23 18:49:56 +0000691 self.push('504 auth type unimplemented')
R. David Murrayfb123912009-05-28 18:19:00 +0000692 return
R David Murray76e13c12014-07-03 14:47:46 -0400693 elif mech=='plain':
694 self.push('334 ')
695 elif mech=='login':
696 self.push('334 ')
R. David Murrayfb123912009-05-28 18:19:00 +0000697 else:
698 self.push('550 No access for you!')
R. David Murraycaa27b72009-05-23 18:49:56 +0000699
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400700 def smtp_QUIT(self, arg):
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400701 if self.quit_response is None:
702 super(SimSMTPChannel, self).smtp_QUIT(arg)
703 else:
704 self.push(self.quit_response)
705 self.close_when_done()
706
R David Murrayd312c742013-03-20 20:36:14 -0400707 def smtp_MAIL(self, arg):
708 if self.mail_response is None:
709 super().smtp_MAIL(arg)
710 else:
711 self.push(self.mail_response)
R David Murrayafb151a2014-04-14 18:21:38 -0400712 if self.disconnect:
713 self.close_when_done()
R David Murrayd312c742013-03-20 20:36:14 -0400714
715 def smtp_RCPT(self, arg):
716 if self.rcpt_response is None:
717 super().smtp_RCPT(arg)
718 return
R David Murrayd312c742013-03-20 20:36:14 -0400719 self.rcpt_count += 1
R David Murray03b01162013-03-20 22:11:40 -0400720 self.push(self.rcpt_response[self.rcpt_count-1])
R David Murrayd312c742013-03-20 20:36:14 -0400721
722 def smtp_RSET(self, arg):
R David Murrayd312c742013-03-20 20:36:14 -0400723 self.rset_count += 1
R David Murray03b01162013-03-20 22:11:40 -0400724 super().smtp_RSET(arg)
R David Murrayd312c742013-03-20 20:36:14 -0400725
726 def smtp_DATA(self, arg):
727 if self.data_response is None:
728 super().smtp_DATA(arg)
729 else:
730 self.push(self.data_response)
731
Giampaolo Rodolàd930b632010-05-06 20:21:57 +0000732 def handle_error(self):
733 raise
734
Guido van Rossum04110fb2007-08-24 16:32:05 +0000735
736class SimSMTPServer(smtpd.SMTPServer):
R. David Murrayfb123912009-05-28 18:19:00 +0000737
R David Murrayd312c742013-03-20 20:36:14 -0400738 channel_class = SimSMTPChannel
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400739
R. David Murray23ddc0e2009-05-29 18:03:16 +0000740 def __init__(self, *args, **kw):
741 self._extra_features = []
742 smtpd.SMTPServer.__init__(self, *args, **kw)
743
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000744 def handle_accepted(self, conn, addr):
R David Murrayf1a40b42013-03-20 21:12:17 -0400745 self._SMTPchannel = self.channel_class(
R David Murray1144da52014-06-11 12:27:40 -0400746 self._extra_features, self, conn, addr,
747 decode_data=self._decode_data)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000748
749 def process_message(self, peer, mailfrom, rcpttos, data):
750 pass
751
R. David Murrayfb123912009-05-28 18:19:00 +0000752 def add_feature(self, feature):
R. David Murray23ddc0e2009-05-29 18:03:16 +0000753 self._extra_features.append(feature)
R. David Murrayfb123912009-05-28 18:19:00 +0000754
Giampaolo Rodolàd930b632010-05-06 20:21:57 +0000755 def handle_error(self):
756 raise
757
Guido van Rossum04110fb2007-08-24 16:32:05 +0000758
759# Test various SMTP & ESMTP commands/behaviors that require a simulated server
760# (i.e., something with more features than DebuggingServer)
Victor Stinner45df8202010-04-28 22:31:17 +0000761@unittest.skipUnless(threading, 'Threading required for this test.')
762class SMTPSimTests(unittest.TestCase):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000763
764 def setUp(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000765 self.real_getfqdn = socket.getfqdn
766 socket.getfqdn = mock_socket.getfqdn
Guido van Rossum04110fb2007-08-24 16:32:05 +0000767 self.serv_evt = threading.Event()
768 self.client_evt = threading.Event()
Antoine Pitrou043bad02010-04-30 23:20:15 +0000769 # Pick a random unused port by passing 0 for the port number
R David Murray1144da52014-06-11 12:27:40 -0400770 self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1), decode_data=True)
Antoine Pitrou043bad02010-04-30 23:20:15 +0000771 # Keep a note of what port was assigned
772 self.port = self.serv.socket.getsockname()[1]
Christian Heimes5e696852008-04-09 08:37:03 +0000773 serv_args = (self.serv, self.serv_evt, self.client_evt)
Antoine Pitrouc3d47722009-10-27 19:49:45 +0000774 self.thread = threading.Thread(target=debugging_server, args=serv_args)
775 self.thread.start()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000776
777 # wait until server thread has assigned a port number
Christian Heimes380f7f22008-02-28 11:19:05 +0000778 self.serv_evt.wait()
779 self.serv_evt.clear()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000780
781 def tearDown(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000782 socket.getfqdn = self.real_getfqdn
Guido van Rossum04110fb2007-08-24 16:32:05 +0000783 # indicate that the client is finished
784 self.client_evt.set()
785 # wait for the server thread to terminate
786 self.serv_evt.wait()
Antoine Pitrouc3d47722009-10-27 19:49:45 +0000787 self.thread.join()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000788
789 def testBasic(self):
790 # smoke test
Christian Heimes5e696852008-04-09 08:37:03 +0000791 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000792 smtp.quit()
793
794 def testEHLO(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000795 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000796
797 # no features should be present before the EHLO
798 self.assertEqual(smtp.esmtp_features, {})
799
800 # features expected from the test server
801 expected_features = {'expn':'',
802 'size': '20000000',
803 'starttls': '',
804 'deliverby': '',
805 'help': '',
806 }
807
808 smtp.ehlo()
809 self.assertEqual(smtp.esmtp_features, expected_features)
810 for k in expected_features:
811 self.assertTrue(smtp.has_extn(k))
812 self.assertFalse(smtp.has_extn('unsupported-feature'))
813 smtp.quit()
814
815 def testVRFY(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000816 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000817
Barry Warsawc5ea7542015-07-09 10:39:55 -0400818 for addr_spec, name in sim_users.items():
Guido van Rossum04110fb2007-08-24 16:32:05 +0000819 expected_known = (250, bytes('%s %s' %
Barry Warsawc5ea7542015-07-09 10:39:55 -0400820 (name, smtplib.quoteaddr(addr_spec)),
Guido van Rossum5a23cc52007-08-30 14:02:43 +0000821 "ascii"))
Barry Warsawc5ea7542015-07-09 10:39:55 -0400822 self.assertEqual(smtp.vrfy(addr_spec), expected_known)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000823
824 u = 'nobody@nowhere.com'
R David Murray46346762011-07-18 21:38:54 -0400825 expected_unknown = (550, ('No such user: %s' % u).encode('ascii'))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000826 self.assertEqual(smtp.vrfy(u), expected_unknown)
827 smtp.quit()
828
829 def testEXPN(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000830 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000831
832 for listname, members in sim_lists.items():
833 users = []
834 for m in members:
835 users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
Guido van Rossum5a23cc52007-08-30 14:02:43 +0000836 expected_known = (250, bytes('\n'.join(users), "ascii"))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000837 self.assertEqual(smtp.expn(listname), expected_known)
838
839 u = 'PSU-Members-List'
840 expected_unknown = (550, b'No access for you!')
841 self.assertEqual(smtp.expn(u), expected_unknown)
842 smtp.quit()
843
R David Murray76e13c12014-07-03 14:47:46 -0400844 # SimSMTPChannel doesn't fully support AUTH because it requires a
845 # synchronous read to obtain the credentials...so instead smtpd
R. David Murrayfb123912009-05-28 18:19:00 +0000846 # sees the credential sent by smtplib's login method as an unknown command,
847 # which results in smtplib raising an auth error. Fortunately the error
848 # message contains the encoded credential, so we can partially check that it
849 # was generated correctly (partially, because the 'word' is uppercased in
850 # the error message).
851
R David Murray76e13c12014-07-03 14:47:46 -0400852 def testAUTH_PLAIN(self):
853 self.serv.add_feature("AUTH PLAIN")
854 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Barry Warsawc5ea7542015-07-09 10:39:55 -0400855 try: smtp.login(sim_auth[0], sim_auth[1], initial_response_ok=False)
R David Murray76e13c12014-07-03 14:47:46 -0400856 except smtplib.SMTPAuthenticationError as err:
857 self.assertIn(sim_auth_plain, str(err))
858 smtp.close()
859
R. David Murrayfb123912009-05-28 18:19:00 +0000860 def testAUTH_LOGIN(self):
R. David Murrayfb123912009-05-28 18:19:00 +0000861 self.serv.add_feature("AUTH LOGIN")
R. David Murray23ddc0e2009-05-29 18:03:16 +0000862 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murrayfb123912009-05-28 18:19:00 +0000863 try: smtp.login(sim_auth[0], sim_auth[1])
864 except smtplib.SMTPAuthenticationError as err:
R David Murray76e13c12014-07-03 14:47:46 -0400865 self.assertIn(sim_auth_login_user, str(err))
Benjamin Petersond094efd2010-10-31 17:15:42 +0000866 smtp.close()
R. David Murrayfb123912009-05-28 18:19:00 +0000867
868 def testAUTH_CRAM_MD5(self):
R. David Murrayfb123912009-05-28 18:19:00 +0000869 self.serv.add_feature("AUTH CRAM-MD5")
R. David Murray23ddc0e2009-05-29 18:03:16 +0000870 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R. David Murrayfb123912009-05-28 18:19:00 +0000871
872 try: smtp.login(sim_auth[0], sim_auth[1])
873 except smtplib.SMTPAuthenticationError as err:
Benjamin Peterson95951662010-10-31 17:59:20 +0000874 self.assertIn(sim_auth_credentials['cram-md5'], str(err))
Benjamin Petersond094efd2010-10-31 17:15:42 +0000875 smtp.close()
R. David Murrayfb123912009-05-28 18:19:00 +0000876
Andrew Kuchling78591822013-11-11 14:03:23 -0500877 def testAUTH_multiple(self):
878 # Test that multiple authentication methods are tried.
879 self.serv.add_feature("AUTH BOGUS PLAIN LOGIN CRAM-MD5")
880 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
881 try: smtp.login(sim_auth[0], sim_auth[1])
882 except smtplib.SMTPAuthenticationError as err:
R David Murray76e13c12014-07-03 14:47:46 -0400883 self.assertIn(sim_auth_login_user, str(err))
884 smtp.close()
885
886 def test_auth_function(self):
887 smtp = smtplib.SMTP(HOST, self.port,
888 local_hostname='localhost', timeout=15)
889 self.serv.add_feature("AUTH CRAM-MD5")
890 smtp.user, smtp.password = sim_auth[0], sim_auth[1]
891 supported = {'CRAM-MD5': smtp.auth_cram_md5,
892 'PLAIN': smtp.auth_plain,
893 'LOGIN': smtp.auth_login,
894 }
895 for mechanism, method in supported.items():
Barry Warsawc5ea7542015-07-09 10:39:55 -0400896 try: smtp.auth(mechanism, method, initial_response_ok=False)
R David Murray76e13c12014-07-03 14:47:46 -0400897 except smtplib.SMTPAuthenticationError as err:
898 self.assertIn(sim_auth_credentials[mechanism.lower()].upper(),
899 str(err))
Andrew Kuchling78591822013-11-11 14:03:23 -0500900 smtp.close()
901
R David Murray0cff49f2014-08-30 16:51:59 -0400902 def test_quit_resets_greeting(self):
903 smtp = smtplib.SMTP(HOST, self.port,
904 local_hostname='localhost',
905 timeout=15)
906 code, message = smtp.ehlo()
907 self.assertEqual(code, 250)
908 self.assertIn('size', smtp.esmtp_features)
909 smtp.quit()
910 self.assertNotIn('size', smtp.esmtp_features)
911 smtp.connect(HOST, self.port)
912 self.assertNotIn('size', smtp.esmtp_features)
913 smtp.ehlo_or_helo_if_needed()
914 self.assertIn('size', smtp.esmtp_features)
915 smtp.quit()
916
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400917 def test_with_statement(self):
918 with smtplib.SMTP(HOST, self.port) as smtp:
919 code, message = smtp.noop()
920 self.assertEqual(code, 250)
921 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo')
922 with smtplib.SMTP(HOST, self.port) as smtp:
923 smtp.close()
924 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo')
925
926 def test_with_statement_QUIT_failure(self):
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400927 with self.assertRaises(smtplib.SMTPResponseException) as error:
928 with smtplib.SMTP(HOST, self.port) as smtp:
929 smtp.noop()
R David Murray6bd52022013-03-21 00:32:31 -0400930 self.serv._SMTPchannel.quit_response = '421 QUIT FAILED'
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400931 self.assertEqual(error.exception.smtp_code, 421)
932 self.assertEqual(error.exception.smtp_error, b'QUIT FAILED')
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400933
R. David Murrayfb123912009-05-28 18:19:00 +0000934 #TODO: add tests for correct AUTH method fallback now that the
935 #test infrastructure can support it.
936
R David Murrayafb151a2014-04-14 18:21:38 -0400937 # Issue 17498: make sure _rset does not raise SMTPServerDisconnected exception
938 def test__rest_from_mail_cmd(self):
939 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
940 smtp.noop()
941 self.serv._SMTPchannel.mail_response = '451 Requested action aborted'
942 self.serv._SMTPchannel.disconnect = True
943 with self.assertRaises(smtplib.SMTPSenderRefused):
944 smtp.sendmail('John', 'Sally', 'test message')
945 self.assertIsNone(smtp.sock)
946
R David Murrayd312c742013-03-20 20:36:14 -0400947 # Issue 5713: make sure close, not rset, is called if we get a 421 error
948 def test_421_from_mail_cmd(self):
949 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murray853c0f92013-03-20 21:54:05 -0400950 smtp.noop()
R David Murrayd312c742013-03-20 20:36:14 -0400951 self.serv._SMTPchannel.mail_response = '421 closing connection'
952 with self.assertRaises(smtplib.SMTPSenderRefused):
953 smtp.sendmail('John', 'Sally', 'test message')
954 self.assertIsNone(smtp.sock)
R David Murray03b01162013-03-20 22:11:40 -0400955 self.assertEqual(self.serv._SMTPchannel.rset_count, 0)
R David Murrayd312c742013-03-20 20:36:14 -0400956
957 def test_421_from_rcpt_cmd(self):
958 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murray853c0f92013-03-20 21:54:05 -0400959 smtp.noop()
R David Murrayd312c742013-03-20 20:36:14 -0400960 self.serv._SMTPchannel.rcpt_response = ['250 accepted', '421 closing']
961 with self.assertRaises(smtplib.SMTPRecipientsRefused) as r:
962 smtp.sendmail('John', ['Sally', 'Frank', 'George'], 'test message')
963 self.assertIsNone(smtp.sock)
964 self.assertEqual(self.serv._SMTPchannel.rset_count, 0)
965 self.assertDictEqual(r.exception.args[0], {'Frank': (421, b'closing')})
966
967 def test_421_from_data_cmd(self):
968 class MySimSMTPChannel(SimSMTPChannel):
969 def found_terminator(self):
970 if self.smtp_state == self.DATA:
971 self.push('421 closing')
972 else:
973 super().found_terminator()
974 self.serv.channel_class = MySimSMTPChannel
975 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murray853c0f92013-03-20 21:54:05 -0400976 smtp.noop()
R David Murrayd312c742013-03-20 20:36:14 -0400977 with self.assertRaises(smtplib.SMTPDataError):
978 smtp.sendmail('John@foo.org', ['Sally@foo.org'], 'test message')
979 self.assertIsNone(smtp.sock)
980 self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0)
981
R David Murraycee7cf62015-05-16 13:58:14 -0400982 def test_smtputf8_NotSupportedError_if_no_server_support(self):
983 smtp = smtplib.SMTP(
984 HOST, self.port, local_hostname='localhost', timeout=3)
985 self.addCleanup(smtp.close)
986 smtp.ehlo()
987 self.assertTrue(smtp.does_esmtp)
988 self.assertFalse(smtp.has_extn('smtputf8'))
989 self.assertRaises(
990 smtplib.SMTPNotSupportedError,
991 smtp.sendmail,
992 'John', 'Sally', '', mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
993 self.assertRaises(
994 smtplib.SMTPNotSupportedError,
995 smtp.mail, 'John', options=['BODY=8BITMIME', 'SMTPUTF8'])
996
997 def test_send_unicode_without_SMTPUTF8(self):
998 smtp = smtplib.SMTP(
999 HOST, self.port, local_hostname='localhost', timeout=3)
1000 self.addCleanup(smtp.close)
1001 self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '')
1002 self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice')
1003
1004
1005class SimSMTPUTF8Server(SimSMTPServer):
1006
1007 def __init__(self, *args, **kw):
1008 # The base SMTP server turns these on automatically, but our test
1009 # server is set up to munge the EHLO response, so we need to provide
1010 # them as well. And yes, the call is to SMTPServer not SimSMTPServer.
1011 self._extra_features = ['SMTPUTF8', '8BITMIME']
1012 smtpd.SMTPServer.__init__(self, *args, **kw)
1013
1014 def handle_accepted(self, conn, addr):
1015 self._SMTPchannel = self.channel_class(
1016 self._extra_features, self, conn, addr,
1017 decode_data=self._decode_data,
1018 enable_SMTPUTF8=self.enable_SMTPUTF8,
1019 )
1020
1021 def process_message(self, peer, mailfrom, rcpttos, data, mail_options=None,
1022 rcpt_options=None):
1023 self.last_peer = peer
1024 self.last_mailfrom = mailfrom
1025 self.last_rcpttos = rcpttos
1026 self.last_message = data
1027 self.last_mail_options = mail_options
1028 self.last_rcpt_options = rcpt_options
1029
1030
1031@unittest.skipUnless(threading, 'Threading required for this test.')
1032class SMTPUTF8SimTests(unittest.TestCase):
1033
R David Murray83084442015-05-17 19:27:22 -04001034 maxDiff = None
1035
R David Murraycee7cf62015-05-16 13:58:14 -04001036 def setUp(self):
1037 self.real_getfqdn = socket.getfqdn
1038 socket.getfqdn = mock_socket.getfqdn
1039 self.serv_evt = threading.Event()
1040 self.client_evt = threading.Event()
1041 # Pick a random unused port by passing 0 for the port number
1042 self.serv = SimSMTPUTF8Server((HOST, 0), ('nowhere', -1),
1043 decode_data=False,
1044 enable_SMTPUTF8=True)
1045 # Keep a note of what port was assigned
1046 self.port = self.serv.socket.getsockname()[1]
1047 serv_args = (self.serv, self.serv_evt, self.client_evt)
1048 self.thread = threading.Thread(target=debugging_server, args=serv_args)
1049 self.thread.start()
1050
1051 # wait until server thread has assigned a port number
1052 self.serv_evt.wait()
1053 self.serv_evt.clear()
1054
1055 def tearDown(self):
1056 socket.getfqdn = self.real_getfqdn
1057 # indicate that the client is finished
1058 self.client_evt.set()
1059 # wait for the server thread to terminate
1060 self.serv_evt.wait()
1061 self.thread.join()
1062
1063 def test_test_server_supports_extensions(self):
1064 smtp = smtplib.SMTP(
1065 HOST, self.port, local_hostname='localhost', timeout=3)
1066 self.addCleanup(smtp.close)
1067 smtp.ehlo()
1068 self.assertTrue(smtp.does_esmtp)
1069 self.assertTrue(smtp.has_extn('smtputf8'))
1070
1071 def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
1072 m = '¡a test message containing unicode!'.encode('utf-8')
1073 smtp = smtplib.SMTP(
1074 HOST, self.port, local_hostname='localhost', timeout=3)
1075 self.addCleanup(smtp.close)
1076 smtp.sendmail('Jőhn', 'Sálly', m,
1077 mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
1078 self.assertEqual(self.serv.last_mailfrom, 'Jőhn')
1079 self.assertEqual(self.serv.last_rcpttos, ['Sálly'])
1080 self.assertEqual(self.serv.last_message, m)
1081 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1082 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1083 self.assertEqual(self.serv.last_rcpt_options, [])
1084
1085 def test_send_unicode_with_SMTPUTF8_via_low_level_API(self):
1086 m = '¡a test message containing unicode!'.encode('utf-8')
1087 smtp = smtplib.SMTP(
1088 HOST, self.port, local_hostname='localhost', timeout=3)
1089 self.addCleanup(smtp.close)
1090 smtp.ehlo()
1091 self.assertEqual(
1092 smtp.mail('Jő', options=['BODY=8BITMIME', 'SMTPUTF8']),
1093 (250, b'OK'))
1094 self.assertEqual(smtp.rcpt('János'), (250, b'OK'))
1095 self.assertEqual(smtp.data(m), (250, b'OK'))
1096 self.assertEqual(self.serv.last_mailfrom, 'Jő')
1097 self.assertEqual(self.serv.last_rcpttos, ['János'])
1098 self.assertEqual(self.serv.last_message, m)
1099 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1100 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1101 self.assertEqual(self.serv.last_rcpt_options, [])
1102
R David Murray83084442015-05-17 19:27:22 -04001103 def test_send_message_uses_smtputf8_if_addrs_non_ascii(self):
1104 msg = EmailMessage()
1105 msg['From'] = "Páolo <főo@bar.com>"
1106 msg['To'] = 'Dinsdale'
1107 msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
1108 # XXX I don't know why I need two \n's here, but this is an existing
1109 # bug (if it is one) and not a problem with the new functionality.
1110 msg.set_content("oh là là, know what I mean, know what I mean?\n\n")
1111 # XXX smtpd converts received /r/n to /n, so we can't easily test that
1112 # we are successfully sending /r/n :(.
1113 expected = textwrap.dedent("""\
1114 From: Páolo <főo@bar.com>
1115 To: Dinsdale
1116 Subject: Nudge nudge, wink, wink \u1F609
1117 Content-Type: text/plain; charset="utf-8"
1118 Content-Transfer-Encoding: 8bit
1119 MIME-Version: 1.0
1120
1121 oh là là, know what I mean, know what I mean?
1122 """)
1123 smtp = smtplib.SMTP(
1124 HOST, self.port, local_hostname='localhost', timeout=3)
1125 self.addCleanup(smtp.close)
1126 self.assertEqual(smtp.send_message(msg), {})
1127 self.assertEqual(self.serv.last_mailfrom, 'főo@bar.com')
1128 self.assertEqual(self.serv.last_rcpttos, ['Dinsdale'])
1129 self.assertEqual(self.serv.last_message.decode(), expected)
1130 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1131 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1132 self.assertEqual(self.serv.last_rcpt_options, [])
1133
1134 def test_send_message_error_on_non_ascii_addrs_if_no_smtputf8(self):
1135 msg = EmailMessage()
1136 msg['From'] = "Páolo <főo@bar.com>"
1137 msg['To'] = 'Dinsdale'
1138 msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
1139 smtp = smtplib.SMTP(
1140 HOST, self.port, local_hostname='localhost', timeout=3)
1141 self.addCleanup(smtp.close)
1142 self.assertRaises(smtplib.SMTPNotSupportedError,
1143 smtp.send_message(msg))
1144
Guido van Rossum04110fb2007-08-24 16:32:05 +00001145
Barry Warsawc5ea7542015-07-09 10:39:55 -04001146EXPECTED_RESPONSE = encode_base64(b'\0psu\0doesnotexist', eol='')
1147
1148class SimSMTPAUTHInitialResponseChannel(SimSMTPChannel):
1149 def smtp_AUTH(self, arg):
1150 # RFC 4954's AUTH command allows for an optional initial-response.
1151 # Not all AUTH methods support this; some require a challenge. AUTH
1152 # PLAIN does those, so test that here. See issue #15014.
1153 args = arg.split()
1154 if args[0].lower() == 'plain':
1155 if len(args) == 2:
1156 # AUTH PLAIN <initial-response> with the response base 64
1157 # encoded. Hard code the expected response for the test.
1158 if args[1] == EXPECTED_RESPONSE:
1159 self.push('235 Ok')
1160 return
1161 self.push('571 Bad authentication')
1162
1163class SimSMTPAUTHInitialResponseServer(SimSMTPServer):
1164 channel_class = SimSMTPAUTHInitialResponseChannel
1165
1166
1167@unittest.skipUnless(threading, 'Threading required for this test.')
1168class SMTPAUTHInitialResponseSimTests(unittest.TestCase):
1169 def setUp(self):
1170 self.real_getfqdn = socket.getfqdn
1171 socket.getfqdn = mock_socket.getfqdn
1172 self.serv_evt = threading.Event()
1173 self.client_evt = threading.Event()
1174 # Pick a random unused port by passing 0 for the port number
1175 self.serv = SimSMTPAUTHInitialResponseServer(
1176 (HOST, 0), ('nowhere', -1), decode_data=True)
1177 # Keep a note of what port was assigned
1178 self.port = self.serv.socket.getsockname()[1]
1179 serv_args = (self.serv, self.serv_evt, self.client_evt)
1180 self.thread = threading.Thread(target=debugging_server, args=serv_args)
1181 self.thread.start()
1182
1183 # wait until server thread has assigned a port number
1184 self.serv_evt.wait()
1185 self.serv_evt.clear()
1186
1187 def tearDown(self):
1188 socket.getfqdn = self.real_getfqdn
1189 # indicate that the client is finished
1190 self.client_evt.set()
1191 # wait for the server thread to terminate
1192 self.serv_evt.wait()
1193 self.thread.join()
1194
1195 def testAUTH_PLAIN_initial_response_login(self):
1196 self.serv.add_feature('AUTH PLAIN')
1197 smtp = smtplib.SMTP(HOST, self.port,
1198 local_hostname='localhost', timeout=15)
1199 smtp.login('psu', 'doesnotexist')
1200 smtp.close()
1201
1202 def testAUTH_PLAIN_initial_response_auth(self):
1203 self.serv.add_feature('AUTH PLAIN')
1204 smtp = smtplib.SMTP(HOST, self.port,
1205 local_hostname='localhost', timeout=15)
1206 smtp.user = 'psu'
1207 smtp.password = 'doesnotexist'
1208 code, response = smtp.auth('plain', smtp.auth_plain)
1209 smtp.close()
1210 self.assertEqual(code, 235)
1211
1212
Antoine Pitroud54fa552011-08-28 01:23:52 +02001213@support.reap_threads
Guido van Rossumd8faa362007-04-27 19:54:29 +00001214def test_main(verbose=None):
Barry Warsawc5ea7542015-07-09 10:39:55 -04001215 support.run_unittest(
1216 BadHELOServerTests,
1217 DebuggingServerTests,
1218 GeneralTests,
1219 NonConnectingTests,
1220 SMTPAUTHInitialResponseSimTests,
1221 SMTPSimTests,
1222 TooLongLineTests,
1223 )
1224
Guido van Rossumd8faa362007-04-27 19:54:29 +00001225
1226if __name__ == '__main__':
1227 test_main()