blob: d0c9862edea93ffad364cd3648872fe094410cb8 [file] [log] [blame]
Guido van Rossum806c2462007-08-06 23:33:07 +00001import asyncore
R David Murrayb0deeb42015-11-08 01:03:52 -05002import base64
R. David Murray7dff9e02010-11-08 17:15:13 +00003import email.mime.text
R David Murray83084442015-05-17 19:27:22 -04004from email.message import EmailMessage
Barry Warsawc5ea7542015-07-09 10:39:55 -04005from email.base64mime import body_encode as encode_base64
Guido van Rossum04110fb2007-08-24 16:32:05 +00006import email.utils
Miss Islington (bot)66cd0412019-09-25 08:50:31 -07007import hashlib
R David Murrayb0deeb42015-11-08 01:03:52 -05008import hmac
Guido van Rossumd8faa362007-04-27 19:54:29 +00009import socket
Guido van Rossum806c2462007-08-06 23:33:07 +000010import smtpd
Guido van Rossumd8faa362007-04-27 19:54:29 +000011import smtplib
Guido van Rossum806c2462007-08-06 23:33:07 +000012import io
R. David Murray7dff9e02010-11-08 17:15:13 +000013import re
Guido van Rossum806c2462007-08-06 23:33:07 +000014import sys
Guido van Rossumd8faa362007-04-27 19:54:29 +000015import time
Guido van Rossum806c2462007-08-06 23:33:07 +000016import select
Ross Lagerwall86407432012-03-29 18:08:48 +020017import errno
R David Murray83084442015-05-17 19:27:22 -040018import textwrap
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020019import threading
Guido van Rossumd8faa362007-04-27 19:54:29 +000020
Victor Stinner45df8202010-04-28 22:31:17 +000021import unittest
Richard Jones64b02de2010-08-03 06:39:33 +000022from test import support, mock_socket
Victor Stinner466e18e2019-07-01 19:01:52 +020023from test.support import HOST
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +010024from test.support import threading_setup, threading_cleanup, join_thread
Miss Islington (bot)66cd0412019-09-25 08:50:31 -070025from test.support import requires_hashdigest
Pablo Aguiard5fbe9b2018-09-08 00:04:48 +020026from unittest.mock import Mock
Guido van Rossumd8faa362007-04-27 19:54:29 +000027
Victor Stinner45df8202010-04-28 22:31:17 +000028
Josiah Carlsond74900e2008-07-07 04:15:08 +000029if sys.platform == 'darwin':
30 # select.poll returns a select.POLLHUP at the end of the tests
31 # on darwin, so just ignore it
32 def handle_expt(self):
33 pass
34 smtpd.SMTPChannel.handle_expt = handle_expt
35
36
Christian Heimes5e696852008-04-09 08:37:03 +000037def server(evt, buf, serv):
Charles-François Natali6e204602014-07-23 19:28:13 +010038 serv.listen()
Christian Heimes380f7f22008-02-28 11:19:05 +000039 evt.set()
Guido van Rossumd8faa362007-04-27 19:54:29 +000040 try:
41 conn, addr = serv.accept()
42 except socket.timeout:
43 pass
44 else:
Guido van Rossum806c2462007-08-06 23:33:07 +000045 n = 500
46 while buf and n > 0:
47 r, w, e = select.select([], [conn], [])
48 if w:
49 sent = conn.send(buf)
50 buf = buf[sent:]
51
52 n -= 1
Guido van Rossum806c2462007-08-06 23:33:07 +000053
Guido van Rossumd8faa362007-04-27 19:54:29 +000054 conn.close()
55 finally:
56 serv.close()
57 evt.set()
58
Victor Stinner45df8202010-04-28 22:31:17 +000059class GeneralTests(unittest.TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +000060
61 def setUp(self):
Richard Jones64b02de2010-08-03 06:39:33 +000062 smtplib.socket = mock_socket
63 self.port = 25
Guido van Rossumd8faa362007-04-27 19:54:29 +000064
65 def tearDown(self):
Richard Jones64b02de2010-08-03 06:39:33 +000066 smtplib.socket = socket
Guido van Rossumd8faa362007-04-27 19:54:29 +000067
R. David Murray7dff9e02010-11-08 17:15:13 +000068 # This method is no longer used but is retained for backward compatibility,
69 # so test to make sure it still works.
70 def testQuoteData(self):
71 teststr = "abc\n.jkl\rfoo\r\n..blue"
72 expected = "abc\r\n..jkl\r\nfoo\r\n...blue"
73 self.assertEqual(expected, smtplib.quotedata(teststr))
74
Guido van Rossum806c2462007-08-06 23:33:07 +000075 def testBasic1(self):
Richard Jones64b02de2010-08-03 06:39:33 +000076 mock_socket.reply_with(b"220 Hola mundo")
Guido van Rossumd8faa362007-04-27 19:54:29 +000077 # connects
Christian Heimes5e696852008-04-09 08:37:03 +000078 smtp = smtplib.SMTP(HOST, self.port)
Georg Brandlf78e02b2008-06-10 17:40:04 +000079 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +000080
Senthil Kumaran3d23fd62011-07-30 10:56:50 +080081 def testSourceAddress(self):
82 mock_socket.reply_with(b"220 Hola mundo")
83 # connects
84 smtp = smtplib.SMTP(HOST, self.port,
85 source_address=('127.0.0.1',19876))
86 self.assertEqual(smtp.source_address, ('127.0.0.1', 19876))
87 smtp.close()
88
Guido van Rossum806c2462007-08-06 23:33:07 +000089 def testBasic2(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +000090 mock_socket.reply_with(b"220 Hola mundo")
Guido van Rossum806c2462007-08-06 23:33:07 +000091 # connects, include port in host name
Christian Heimes5e696852008-04-09 08:37:03 +000092 smtp = smtplib.SMTP("%s:%s" % (HOST, self.port))
Georg Brandlf78e02b2008-06-10 17:40:04 +000093 smtp.close()
Guido van Rossum806c2462007-08-06 23:33:07 +000094
95 def testLocalHostName(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +000096 mock_socket.reply_with(b"220 Hola mundo")
Guido van Rossum806c2462007-08-06 23:33:07 +000097 # check that supplied local_hostname is used
Christian Heimes5e696852008-04-09 08:37:03 +000098 smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost")
Guido van Rossum806c2462007-08-06 23:33:07 +000099 self.assertEqual(smtp.local_hostname, "testhost")
Georg Brandlf78e02b2008-06-10 17:40:04 +0000100 smtp.close()
Guido van Rossum806c2462007-08-06 23:33:07 +0000101
Guido van Rossumd8faa362007-04-27 19:54:29 +0000102 def testTimeoutDefault(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000103 mock_socket.reply_with(b"220 Hola mundo")
Serhiy Storchaka578c6772014-02-08 15:06:08 +0200104 self.assertIsNone(mock_socket.getdefaulttimeout())
Richard Jones64b02de2010-08-03 06:39:33 +0000105 mock_socket.setdefaulttimeout(30)
106 self.assertEqual(mock_socket.getdefaulttimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000107 try:
108 smtp = smtplib.SMTP(HOST, self.port)
109 finally:
Richard Jones64b02de2010-08-03 06:39:33 +0000110 mock_socket.setdefaulttimeout(None)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000111 self.assertEqual(smtp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000112 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000113
114 def testTimeoutNone(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000115 mock_socket.reply_with(b"220 Hola mundo")
Serhiy Storchaka578c6772014-02-08 15:06:08 +0200116 self.assertIsNone(socket.getdefaulttimeout())
Guido van Rossumd8faa362007-04-27 19:54:29 +0000117 socket.setdefaulttimeout(30)
118 try:
Christian Heimes5e696852008-04-09 08:37:03 +0000119 smtp = smtplib.SMTP(HOST, self.port, timeout=None)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000120 finally:
Georg Brandlf78e02b2008-06-10 17:40:04 +0000121 socket.setdefaulttimeout(None)
Serhiy Storchaka578c6772014-02-08 15:06:08 +0200122 self.assertIsNone(smtp.sock.gettimeout())
Georg Brandlf78e02b2008-06-10 17:40:04 +0000123 smtp.close()
124
125 def testTimeoutValue(self):
Richard Jones6a9e6bb2010-08-04 12:27:36 +0000126 mock_socket.reply_with(b"220 Hola mundo")
Georg Brandlf78e02b2008-06-10 17:40:04 +0000127 smtp = smtplib.SMTP(HOST, self.port, timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000128 self.assertEqual(smtp.sock.gettimeout(), 30)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000129 smtp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000130
R David Murray0c49b892015-04-16 17:14:42 -0400131 def test_debuglevel(self):
132 mock_socket.reply_with(b"220 Hello world")
133 smtp = smtplib.SMTP()
134 smtp.set_debuglevel(1)
135 with support.captured_stderr() as stderr:
136 smtp.connect(HOST, self.port)
137 smtp.close()
138 expected = re.compile(r"^connect:", re.MULTILINE)
139 self.assertRegex(stderr.getvalue(), expected)
140
141 def test_debuglevel_2(self):
142 mock_socket.reply_with(b"220 Hello world")
143 smtp = smtplib.SMTP()
144 smtp.set_debuglevel(2)
145 with support.captured_stderr() as stderr:
146 smtp.connect(HOST, self.port)
147 smtp.close()
148 expected = re.compile(r"^\d{2}:\d{2}:\d{2}\.\d{6} connect: ",
149 re.MULTILINE)
150 self.assertRegex(stderr.getvalue(), expected)
151
Guido van Rossumd8faa362007-04-27 19:54:29 +0000152
Guido van Rossum04110fb2007-08-24 16:32:05 +0000153# Test server thread using the specified SMTP server class
Christian Heimes5e696852008-04-09 08:37:03 +0000154def debugging_server(serv, serv_evt, client_evt):
Christian Heimes380f7f22008-02-28 11:19:05 +0000155 serv_evt.set()
Guido van Rossum806c2462007-08-06 23:33:07 +0000156
157 try:
158 if hasattr(select, 'poll'):
159 poll_fun = asyncore.poll2
160 else:
161 poll_fun = asyncore.poll
162
163 n = 1000
164 while asyncore.socket_map and n > 0:
165 poll_fun(0.01, asyncore.socket_map)
166
167 # when the client conversation is finished, it will
168 # set client_evt, and it's then ok to kill the server
Benjamin Peterson672b8032008-06-11 19:14:14 +0000169 if client_evt.is_set():
Guido van Rossum806c2462007-08-06 23:33:07 +0000170 serv.close()
171 break
172
173 n -= 1
174
175 except socket.timeout:
176 pass
177 finally:
Benjamin Peterson672b8032008-06-11 19:14:14 +0000178 if not client_evt.is_set():
Christian Heimes380f7f22008-02-28 11:19:05 +0000179 # allow some time for the client to read the result
180 time.sleep(0.5)
181 serv.close()
Guido van Rossum806c2462007-08-06 23:33:07 +0000182 asyncore.close_all()
Guido van Rossum806c2462007-08-06 23:33:07 +0000183 serv_evt.set()
184
185MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
186MSG_END = '------------ END MESSAGE ------------\n'
187
Guido van Rossum04110fb2007-08-24 16:32:05 +0000188# NOTE: Some SMTP objects in the tests below are created with a non-default
189# local_hostname argument to the constructor, since (on some systems) the FQDN
190# lookup caused by the default local_hostname sometimes takes so long that the
Guido van Rossum806c2462007-08-06 23:33:07 +0000191# test server times out, causing the test to fail.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000192
193# Test behavior of smtpd.DebuggingServer
Victor Stinner45df8202010-04-28 22:31:17 +0000194class DebuggingServerTests(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +0000195
R. David Murray7dff9e02010-11-08 17:15:13 +0000196 maxDiff = None
197
Guido van Rossum806c2462007-08-06 23:33:07 +0000198 def setUp(self):
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100199 self.thread_key = threading_setup()
Richard Jones64b02de2010-08-03 06:39:33 +0000200 self.real_getfqdn = socket.getfqdn
201 socket.getfqdn = mock_socket.getfqdn
Guido van Rossum806c2462007-08-06 23:33:07 +0000202 # temporarily replace sys.stdout to capture DebuggingServer output
203 self.old_stdout = sys.stdout
204 self.output = io.StringIO()
205 sys.stdout = self.output
206
207 self.serv_evt = threading.Event()
208 self.client_evt = threading.Event()
R. David Murray7dff9e02010-11-08 17:15:13 +0000209 # Capture SMTPChannel debug output
210 self.old_DEBUGSTREAM = smtpd.DEBUGSTREAM
211 smtpd.DEBUGSTREAM = io.StringIO()
Antoine Pitrou043bad02010-04-30 23:20:15 +0000212 # Pick a random unused port by passing 0 for the port number
R David Murray1144da52014-06-11 12:27:40 -0400213 self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1),
214 decode_data=True)
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700215 # Keep a note of what server host and port were assigned
216 self.host, self.port = self.serv.socket.getsockname()[:2]
Christian Heimes5e696852008-04-09 08:37:03 +0000217 serv_args = (self.serv, self.serv_evt, self.client_evt)
Antoine Pitrouc3d47722009-10-27 19:49:45 +0000218 self.thread = threading.Thread(target=debugging_server, args=serv_args)
219 self.thread.start()
Guido van Rossum806c2462007-08-06 23:33:07 +0000220
221 # wait until server thread has assigned a port number
Christian Heimes380f7f22008-02-28 11:19:05 +0000222 self.serv_evt.wait()
223 self.serv_evt.clear()
Guido van Rossum806c2462007-08-06 23:33:07 +0000224
225 def tearDown(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000226 socket.getfqdn = self.real_getfqdn
Guido van Rossum806c2462007-08-06 23:33:07 +0000227 # indicate that the client is finished
228 self.client_evt.set()
229 # wait for the server thread to terminate
230 self.serv_evt.wait()
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100231 join_thread(self.thread)
Guido van Rossum806c2462007-08-06 23:33:07 +0000232 # restore sys.stdout
233 sys.stdout = self.old_stdout
R. David Murray7dff9e02010-11-08 17:15:13 +0000234 # restore DEBUGSTREAM
235 smtpd.DEBUGSTREAM.close()
236 smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100237 del self.thread
238 self.doCleanups()
239 threading_cleanup(*self.thread_key)
Guido van Rossum806c2462007-08-06 23:33:07 +0000240
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700241 def get_output_without_xpeer(self):
242 test_output = self.output.getvalue()
243 return re.sub(r'(.*?)^X-Peer:\s*\S+\n(.*)', r'\1\2',
244 test_output, flags=re.MULTILINE|re.DOTALL)
245
Guido van Rossum806c2462007-08-06 23:33:07 +0000246 def testBasic(self):
247 # connect
Christian Heimes5e696852008-04-09 08:37:03 +0000248 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Guido van Rossum806c2462007-08-06 23:33:07 +0000249 smtp.quit()
250
Senthil Kumaran3d23fd62011-07-30 10:56:50 +0800251 def testSourceAddress(self):
252 # connect
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700253 src_port = support.find_unused_port()
Senthil Kumaranb351a482011-07-31 09:14:17 +0800254 try:
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700255 smtp = smtplib.SMTP(self.host, self.port, local_hostname='localhost',
256 timeout=3, source_address=(self.host, src_port))
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100257 self.addCleanup(smtp.close)
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700258 self.assertEqual(smtp.source_address, (self.host, src_port))
Senthil Kumaranb351a482011-07-31 09:14:17 +0800259 self.assertEqual(smtp.local_hostname, 'localhost')
260 smtp.quit()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200261 except OSError as e:
Senthil Kumaranb351a482011-07-31 09:14:17 +0800262 if e.errno == errno.EADDRINUSE:
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700263 self.skipTest("couldn't bind to source port %d" % src_port)
Senthil Kumaranb351a482011-07-31 09:14:17 +0800264 raise
Senthil Kumaran3d23fd62011-07-30 10:56:50 +0800265
Guido van Rossum04110fb2007-08-24 16:32:05 +0000266 def testNOOP(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000267 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100268 self.addCleanup(smtp.close)
R David Murrayd1a30c92012-05-26 14:33:59 -0400269 expected = (250, b'OK')
Guido van Rossum04110fb2007-08-24 16:32:05 +0000270 self.assertEqual(smtp.noop(), expected)
271 smtp.quit()
272
273 def testRSET(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000274 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100275 self.addCleanup(smtp.close)
R David Murrayd1a30c92012-05-26 14:33:59 -0400276 expected = (250, b'OK')
Guido van Rossum04110fb2007-08-24 16:32:05 +0000277 self.assertEqual(smtp.rset(), expected)
278 smtp.quit()
279
Benjamin Peterson1eca0622013-09-29 10:46:31 -0400280 def testELHO(self):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000281 # EHLO isn't implemented in DebuggingServer
Christian Heimes5e696852008-04-09 08:37:03 +0000282 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100283 self.addCleanup(smtp.close)
Benjamin Peterson1eca0622013-09-29 10:46:31 -0400284 expected = (250, b'\nSIZE 33554432\nHELP')
Guido van Rossum806c2462007-08-06 23:33:07 +0000285 self.assertEqual(smtp.ehlo(), expected)
286 smtp.quit()
287
Benjamin Peterson1eca0622013-09-29 10:46:31 -0400288 def testEXPNNotImplemented(self):
R David Murrayd1a30c92012-05-26 14:33:59 -0400289 # EXPN isn't implemented in DebuggingServer
Christian Heimes5e696852008-04-09 08:37:03 +0000290 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100291 self.addCleanup(smtp.close)
R David Murrayd1a30c92012-05-26 14:33:59 -0400292 expected = (502, b'EXPN not implemented')
293 smtp.putcmd('EXPN')
294 self.assertEqual(smtp.getreply(), expected)
295 smtp.quit()
296
297 def testVRFY(self):
298 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100299 self.addCleanup(smtp.close)
R David Murrayd1a30c92012-05-26 14:33:59 -0400300 expected = (252, b'Cannot VRFY user, but will accept message ' + \
301 b'and attempt delivery')
Guido van Rossum04110fb2007-08-24 16:32:05 +0000302 self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
303 self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
304 smtp.quit()
305
306 def testSecondHELO(self):
307 # check that a second HELO returns a message that it's a duplicate
308 # (this behavior is specific to smtpd.SMTPChannel)
Christian Heimes5e696852008-04-09 08:37:03 +0000309 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100310 self.addCleanup(smtp.close)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000311 smtp.helo()
312 expected = (503, b'Duplicate HELO/EHLO')
313 self.assertEqual(smtp.helo(), expected)
314 smtp.quit()
315
Guido van Rossum806c2462007-08-06 23:33:07 +0000316 def testHELP(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000317 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100318 self.addCleanup(smtp.close)
R David Murrayd1a30c92012-05-26 14:33:59 -0400319 self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \
320 b'RCPT DATA RSET NOOP QUIT VRFY')
Guido van Rossum806c2462007-08-06 23:33:07 +0000321 smtp.quit()
322
323 def testSend(self):
324 # connect and send mail
325 m = 'A test message'
Christian Heimes5e696852008-04-09 08:37:03 +0000326 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100327 self.addCleanup(smtp.close)
Guido van Rossum806c2462007-08-06 23:33:07 +0000328 smtp.sendmail('John', 'Sally', m)
Neal Norwitz25329672008-08-25 03:55:03 +0000329 # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
330 # in asyncore. This sleep might help, but should really be fixed
331 # properly by using an Event variable.
332 time.sleep(0.01)
Guido van Rossum806c2462007-08-06 23:33:07 +0000333 smtp.quit()
334
335 self.client_evt.set()
336 self.serv_evt.wait()
337 self.output.flush()
338 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
339 self.assertEqual(self.output.getvalue(), mexpect)
340
R. David Murray7dff9e02010-11-08 17:15:13 +0000341 def testSendBinary(self):
342 m = b'A test message'
343 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100344 self.addCleanup(smtp.close)
R. David Murray7dff9e02010-11-08 17:15:13 +0000345 smtp.sendmail('John', 'Sally', m)
346 # XXX (see comment in testSend)
347 time.sleep(0.01)
348 smtp.quit()
349
350 self.client_evt.set()
351 self.serv_evt.wait()
352 self.output.flush()
353 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.decode('ascii'), MSG_END)
354 self.assertEqual(self.output.getvalue(), mexpect)
355
R David Murray0f663d02011-06-09 15:05:57 -0400356 def testSendNeedingDotQuote(self):
357 # Issue 12283
358 m = '.A test\n.mes.sage.'
359 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100360 self.addCleanup(smtp.close)
R David Murray0f663d02011-06-09 15:05:57 -0400361 smtp.sendmail('John', 'Sally', m)
362 # XXX (see comment in testSend)
363 time.sleep(0.01)
364 smtp.quit()
365
366 self.client_evt.set()
367 self.serv_evt.wait()
368 self.output.flush()
369 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
370 self.assertEqual(self.output.getvalue(), mexpect)
371
R David Murray46346762011-07-18 21:38:54 -0400372 def testSendNullSender(self):
373 m = 'A test message'
374 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100375 self.addCleanup(smtp.close)
R David Murray46346762011-07-18 21:38:54 -0400376 smtp.sendmail('<>', 'Sally', m)
377 # XXX (see comment in testSend)
378 time.sleep(0.01)
379 smtp.quit()
380
381 self.client_evt.set()
382 self.serv_evt.wait()
383 self.output.flush()
384 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
385 self.assertEqual(self.output.getvalue(), mexpect)
386 debugout = smtpd.DEBUGSTREAM.getvalue()
387 sender = re.compile("^sender: <>$", re.MULTILINE)
388 self.assertRegex(debugout, sender)
389
R. David Murray7dff9e02010-11-08 17:15:13 +0000390 def testSendMessage(self):
391 m = email.mime.text.MIMEText('A test message')
392 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100393 self.addCleanup(smtp.close)
R. David Murray7dff9e02010-11-08 17:15:13 +0000394 smtp.send_message(m, from_addr='John', to_addrs='Sally')
395 # XXX (see comment in testSend)
396 time.sleep(0.01)
397 smtp.quit()
398
399 self.client_evt.set()
400 self.serv_evt.wait()
401 self.output.flush()
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700402 # Remove the X-Peer header that DebuggingServer adds as figuring out
403 # exactly what IP address format is put there is not easy (and
404 # irrelevant to our test). Typically 127.0.0.1 or ::1, but it is
405 # not always the same as socket.gethostbyname(HOST). :(
406 test_output = self.get_output_without_xpeer()
407 del m['X-Peer']
R. David Murray7dff9e02010-11-08 17:15:13 +0000408 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700409 self.assertEqual(test_output, mexpect)
R. David Murray7dff9e02010-11-08 17:15:13 +0000410
411 def testSendMessageWithAddresses(self):
412 m = email.mime.text.MIMEText('A test message')
413 m['From'] = 'foo@bar.com'
414 m['To'] = 'John'
415 m['CC'] = 'Sally, Fred'
416 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
417 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100418 self.addCleanup(smtp.close)
R. David Murray7dff9e02010-11-08 17:15:13 +0000419 smtp.send_message(m)
420 # XXX (see comment in testSend)
421 time.sleep(0.01)
422 smtp.quit()
R David Murrayac4e5ab2011-07-02 21:03:19 -0400423 # make sure the Bcc header is still in the message.
424 self.assertEqual(m['Bcc'], 'John Root <root@localhost>, "Dinsdale" '
425 '<warped@silly.walks.com>')
R. David Murray7dff9e02010-11-08 17:15:13 +0000426
427 self.client_evt.set()
428 self.serv_evt.wait()
429 self.output.flush()
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700430 # Remove the X-Peer header that DebuggingServer adds.
431 test_output = self.get_output_without_xpeer()
432 del m['X-Peer']
R David Murrayac4e5ab2011-07-02 21:03:19 -0400433 # The Bcc header should not be transmitted.
R. David Murray7dff9e02010-11-08 17:15:13 +0000434 del m['Bcc']
435 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700436 self.assertEqual(test_output, mexpect)
R. David Murray7dff9e02010-11-08 17:15:13 +0000437 debugout = smtpd.DEBUGSTREAM.getvalue()
438 sender = re.compile("^sender: foo@bar.com$", re.MULTILINE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000439 self.assertRegex(debugout, sender)
R. David Murray7dff9e02010-11-08 17:15:13 +0000440 for addr in ('John', 'Sally', 'Fred', 'root@localhost',
441 'warped@silly.walks.com'):
442 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
443 re.MULTILINE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000444 self.assertRegex(debugout, to_addr)
R. David Murray7dff9e02010-11-08 17:15:13 +0000445
446 def testSendMessageWithSomeAddresses(self):
447 # Make sure nothing breaks if not all of the three 'to' headers exist
448 m = email.mime.text.MIMEText('A test message')
449 m['From'] = 'foo@bar.com'
450 m['To'] = 'John, Dinsdale'
451 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100452 self.addCleanup(smtp.close)
R. David Murray7dff9e02010-11-08 17:15:13 +0000453 smtp.send_message(m)
454 # XXX (see comment in testSend)
455 time.sleep(0.01)
456 smtp.quit()
457
458 self.client_evt.set()
459 self.serv_evt.wait()
460 self.output.flush()
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700461 # Remove the X-Peer header that DebuggingServer adds.
462 test_output = self.get_output_without_xpeer()
463 del m['X-Peer']
R. David Murray7dff9e02010-11-08 17:15:13 +0000464 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700465 self.assertEqual(test_output, mexpect)
R. David Murray7dff9e02010-11-08 17:15:13 +0000466 debugout = smtpd.DEBUGSTREAM.getvalue()
467 sender = re.compile("^sender: foo@bar.com$", re.MULTILINE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000468 self.assertRegex(debugout, sender)
R. David Murray7dff9e02010-11-08 17:15:13 +0000469 for addr in ('John', 'Dinsdale'):
470 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
471 re.MULTILINE)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000472 self.assertRegex(debugout, to_addr)
R. David Murray7dff9e02010-11-08 17:15:13 +0000473
R David Murrayac4e5ab2011-07-02 21:03:19 -0400474 def testSendMessageWithSpecifiedAddresses(self):
475 # Make sure addresses specified in call override those in message.
476 m = email.mime.text.MIMEText('A test message')
477 m['From'] = 'foo@bar.com'
478 m['To'] = 'John, Dinsdale'
479 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100480 self.addCleanup(smtp.close)
R David Murrayac4e5ab2011-07-02 21:03:19 -0400481 smtp.send_message(m, from_addr='joe@example.com', to_addrs='foo@example.net')
482 # XXX (see comment in testSend)
483 time.sleep(0.01)
484 smtp.quit()
485
486 self.client_evt.set()
487 self.serv_evt.wait()
488 self.output.flush()
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700489 # Remove the X-Peer header that DebuggingServer adds.
490 test_output = self.get_output_without_xpeer()
491 del m['X-Peer']
R David Murrayac4e5ab2011-07-02 21:03:19 -0400492 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700493 self.assertEqual(test_output, mexpect)
R David Murrayac4e5ab2011-07-02 21:03:19 -0400494 debugout = smtpd.DEBUGSTREAM.getvalue()
495 sender = re.compile("^sender: joe@example.com$", re.MULTILINE)
496 self.assertRegex(debugout, sender)
497 for addr in ('John', 'Dinsdale'):
498 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
499 re.MULTILINE)
500 self.assertNotRegex(debugout, to_addr)
501 recip = re.compile(r"^recips: .*'foo@example.net'.*$", re.MULTILINE)
502 self.assertRegex(debugout, recip)
503
504 def testSendMessageWithMultipleFrom(self):
505 # Sender overrides To
506 m = email.mime.text.MIMEText('A test message')
507 m['From'] = 'Bernard, Bianca'
508 m['Sender'] = 'the_rescuers@Rescue-Aid-Society.com'
509 m['To'] = 'John, Dinsdale'
510 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100511 self.addCleanup(smtp.close)
R David Murrayac4e5ab2011-07-02 21:03:19 -0400512 smtp.send_message(m)
513 # XXX (see comment in testSend)
514 time.sleep(0.01)
515 smtp.quit()
516
517 self.client_evt.set()
518 self.serv_evt.wait()
519 self.output.flush()
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700520 # Remove the X-Peer header that DebuggingServer adds.
521 test_output = self.get_output_without_xpeer()
522 del m['X-Peer']
R David Murrayac4e5ab2011-07-02 21:03:19 -0400523 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700524 self.assertEqual(test_output, mexpect)
R David Murrayac4e5ab2011-07-02 21:03:19 -0400525 debugout = smtpd.DEBUGSTREAM.getvalue()
526 sender = re.compile("^sender: the_rescuers@Rescue-Aid-Society.com$", re.MULTILINE)
527 self.assertRegex(debugout, sender)
528 for addr in ('John', 'Dinsdale'):
529 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
530 re.MULTILINE)
531 self.assertRegex(debugout, to_addr)
532
533 def testSendMessageResent(self):
534 m = email.mime.text.MIMEText('A test message')
535 m['From'] = 'foo@bar.com'
536 m['To'] = 'John'
537 m['CC'] = 'Sally, Fred'
538 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
539 m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
540 m['Resent-From'] = 'holy@grail.net'
541 m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff'
542 m['Resent-Bcc'] = 'doe@losthope.net'
543 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100544 self.addCleanup(smtp.close)
R David Murrayac4e5ab2011-07-02 21:03:19 -0400545 smtp.send_message(m)
546 # XXX (see comment in testSend)
547 time.sleep(0.01)
548 smtp.quit()
549
550 self.client_evt.set()
551 self.serv_evt.wait()
552 self.output.flush()
553 # The Resent-Bcc headers are deleted before serialization.
554 del m['Bcc']
555 del m['Resent-Bcc']
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700556 # Remove the X-Peer header that DebuggingServer adds.
557 test_output = self.get_output_without_xpeer()
558 del m['X-Peer']
R David Murrayac4e5ab2011-07-02 21:03:19 -0400559 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
Gregory P. Smithefb1d0a2017-09-09 00:30:15 -0700560 self.assertEqual(test_output, mexpect)
R David Murrayac4e5ab2011-07-02 21:03:19 -0400561 debugout = smtpd.DEBUGSTREAM.getvalue()
562 sender = re.compile("^sender: holy@grail.net$", re.MULTILINE)
563 self.assertRegex(debugout, sender)
564 for addr in ('my_mom@great.cooker.com', 'Jeff', 'doe@losthope.net'):
565 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
566 re.MULTILINE)
567 self.assertRegex(debugout, to_addr)
568
569 def testSendMessageMultipleResentRaises(self):
570 m = email.mime.text.MIMEText('A test message')
571 m['From'] = 'foo@bar.com'
572 m['To'] = 'John'
573 m['CC'] = 'Sally, Fred'
574 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>'
575 m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
576 m['Resent-From'] = 'holy@grail.net'
577 m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff'
578 m['Resent-Bcc'] = 'doe@losthope.net'
579 m['Resent-Date'] = 'Thu, 2 Jan 1970 17:42:00 +0000'
580 m['Resent-To'] = 'holy@grail.net'
581 m['Resent-From'] = 'Martha <my_mom@great.cooker.com>, Jeff'
582 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100583 self.addCleanup(smtp.close)
R David Murrayac4e5ab2011-07-02 21:03:19 -0400584 with self.assertRaises(ValueError):
585 smtp.send_message(m)
586 smtp.close()
Guido van Rossum806c2462007-08-06 23:33:07 +0000587
Victor Stinner45df8202010-04-28 22:31:17 +0000588class NonConnectingTests(unittest.TestCase):
Christian Heimes380f7f22008-02-28 11:19:05 +0000589
590 def testNotConnected(self):
591 # Test various operations on an unconnected SMTP object that
592 # should raise exceptions (at present the attempt in SMTP.send
593 # to reference the nonexistent 'sock' attribute of the SMTP object
594 # causes an AttributeError)
595 smtp = smtplib.SMTP()
596 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
597 self.assertRaises(smtplib.SMTPServerDisconnected,
598 smtp.send, 'test msg')
599
600 def testNonnumericPort(self):
Andrew Svetlov0832af62012-12-18 23:10:48 +0200601 # check that non-numeric port raises OSError
Andrew Svetlov2ade6f22012-12-17 18:57:16 +0200602 self.assertRaises(OSError, smtplib.SMTP,
Christian Heimes380f7f22008-02-28 11:19:05 +0000603 "localhost", "bogus")
Andrew Svetlov2ade6f22012-12-17 18:57:16 +0200604 self.assertRaises(OSError, smtplib.SMTP,
Christian Heimes380f7f22008-02-28 11:19:05 +0000605 "localhost:bogus")
606
Romuald Brunet7b313972018-10-09 16:31:55 +0200607 def testSockAttributeExists(self):
608 # check that sock attribute is present outside of a connect() call
609 # (regression test, the previous behavior raised an
610 # AttributeError: 'SMTP' object has no attribute 'sock')
611 with smtplib.SMTP() as smtp:
612 self.assertIsNone(smtp.sock)
613
Christian Heimes380f7f22008-02-28 11:19:05 +0000614
Pablo Aguiard5fbe9b2018-09-08 00:04:48 +0200615class DefaultArgumentsTests(unittest.TestCase):
616
617 def setUp(self):
618 self.msg = EmailMessage()
619 self.msg['From'] = 'Páolo <főo@bar.com>'
620 self.smtp = smtplib.SMTP()
621 self.smtp.ehlo = Mock(return_value=(200, 'OK'))
622 self.smtp.has_extn, self.smtp.sendmail = Mock(), Mock()
623
624 def testSendMessage(self):
625 expected_mail_options = ('SMTPUTF8', 'BODY=8BITMIME')
626 self.smtp.send_message(self.msg)
627 self.smtp.send_message(self.msg)
628 self.assertEqual(self.smtp.sendmail.call_args_list[0][0][3],
629 expected_mail_options)
630 self.assertEqual(self.smtp.sendmail.call_args_list[1][0][3],
631 expected_mail_options)
632
633 def testSendMessageWithMailOptions(self):
634 mail_options = ['STARTTLS']
635 expected_mail_options = ('STARTTLS', 'SMTPUTF8', 'BODY=8BITMIME')
636 self.smtp.send_message(self.msg, None, None, mail_options)
637 self.assertEqual(mail_options, ['STARTTLS'])
638 self.assertEqual(self.smtp.sendmail.call_args_list[0][0][3],
639 expected_mail_options)
640
641
Guido van Rossum04110fb2007-08-24 16:32:05 +0000642# test response of client to a non-successful HELO message
Victor Stinner45df8202010-04-28 22:31:17 +0000643class BadHELOServerTests(unittest.TestCase):
Guido van Rossum806c2462007-08-06 23:33:07 +0000644
645 def setUp(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000646 smtplib.socket = mock_socket
647 mock_socket.reply_with(b"199 no hello for you!")
Guido van Rossum806c2462007-08-06 23:33:07 +0000648 self.old_stdout = sys.stdout
649 self.output = io.StringIO()
650 sys.stdout = self.output
Richard Jones64b02de2010-08-03 06:39:33 +0000651 self.port = 25
Guido van Rossum806c2462007-08-06 23:33:07 +0000652
653 def tearDown(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000654 smtplib.socket = socket
Guido van Rossum806c2462007-08-06 23:33:07 +0000655 sys.stdout = self.old_stdout
656
657 def testFailingHELO(self):
658 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
Christian Heimes5e696852008-04-09 08:37:03 +0000659 HOST, self.port, 'localhost', 3)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000660
Guido van Rossum04110fb2007-08-24 16:32:05 +0000661
Georg Brandlb38b5c42014-02-10 22:11:21 +0100662class TooLongLineTests(unittest.TestCase):
663 respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n'
664
665 def setUp(self):
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100666 self.thread_key = threading_setup()
Georg Brandlb38b5c42014-02-10 22:11:21 +0100667 self.old_stdout = sys.stdout
668 self.output = io.StringIO()
669 sys.stdout = self.output
670
671 self.evt = threading.Event()
672 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
673 self.sock.settimeout(15)
674 self.port = support.bind_port(self.sock)
675 servargs = (self.evt, self.respdata, self.sock)
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100676 self.thread = threading.Thread(target=server, args=servargs)
677 self.thread.start()
Georg Brandlb38b5c42014-02-10 22:11:21 +0100678 self.evt.wait()
679 self.evt.clear()
680
681 def tearDown(self):
682 self.evt.wait()
683 sys.stdout = self.old_stdout
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100684 join_thread(self.thread)
685 del self.thread
686 self.doCleanups()
687 threading_cleanup(*self.thread_key)
Georg Brandlb38b5c42014-02-10 22:11:21 +0100688
689 def testLineTooLong(self):
690 self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
691 HOST, self.port, 'localhost', 3)
692
693
Guido van Rossum04110fb2007-08-24 16:32:05 +0000694sim_users = {'Mr.A@somewhere.com':'John A',
R David Murray46346762011-07-18 21:38:54 -0400695 'Ms.B@xn--fo-fka.com':'Sally B',
Guido van Rossum04110fb2007-08-24 16:32:05 +0000696 'Mrs.C@somewhereesle.com':'Ruth C',
697 }
698
R. David Murraycaa27b72009-05-23 18:49:56 +0000699sim_auth = ('Mr.A@somewhere.com', 'somepassword')
R. David Murrayfb123912009-05-28 18:19:00 +0000700sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
701 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
Guido van Rossum04110fb2007-08-24 16:32:05 +0000702sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'],
R David Murray46346762011-07-18 21:38:54 -0400703 'list-2':['Ms.B@xn--fo-fka.com',],
Guido van Rossum04110fb2007-08-24 16:32:05 +0000704 }
705
706# Simulated SMTP channel & server
R David Murrayb0deeb42015-11-08 01:03:52 -0500707class ResponseException(Exception): pass
Guido van Rossum04110fb2007-08-24 16:32:05 +0000708class SimSMTPChannel(smtpd.SMTPChannel):
R. David Murrayfb123912009-05-28 18:19:00 +0000709
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400710 quit_response = None
R David Murrayd312c742013-03-20 20:36:14 -0400711 mail_response = None
712 rcpt_response = None
713 data_response = None
714 rcpt_count = 0
715 rset_count = 0
R David Murrayafb151a2014-04-14 18:21:38 -0400716 disconnect = 0
R David Murrayb0deeb42015-11-08 01:03:52 -0500717 AUTH = 99 # Add protocol state to enable auth testing.
718 authenticated_user = None
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400719
R. David Murray23ddc0e2009-05-29 18:03:16 +0000720 def __init__(self, extra_features, *args, **kw):
721 self._extrafeatures = ''.join(
722 [ "250-{0}\r\n".format(x) for x in extra_features ])
R. David Murrayfb123912009-05-28 18:19:00 +0000723 super(SimSMTPChannel, self).__init__(*args, **kw)
724
R David Murrayb0deeb42015-11-08 01:03:52 -0500725 # AUTH related stuff. It would be nice if support for this were in smtpd.
726 def found_terminator(self):
727 if self.smtp_state == self.AUTH:
728 line = self._emptystring.join(self.received_lines)
729 print('Data:', repr(line), file=smtpd.DEBUGSTREAM)
730 self.received_lines = []
731 try:
732 self.auth_object(line)
733 except ResponseException as e:
734 self.smtp_state = self.COMMAND
735 self.push('%s %s' % (e.smtp_code, e.smtp_error))
736 return
737 super().found_terminator()
738
739
740 def smtp_AUTH(self, arg):
741 if not self.seen_greeting:
742 self.push('503 Error: send EHLO first')
743 return
744 if not self.extended_smtp or 'AUTH' not in self._extrafeatures:
745 self.push('500 Error: command "AUTH" not recognized')
746 return
747 if self.authenticated_user is not None:
748 self.push(
749 '503 Bad sequence of commands: already authenticated')
750 return
751 args = arg.split()
752 if len(args) not in [1, 2]:
753 self.push('501 Syntax: AUTH <mechanism> [initial-response]')
754 return
755 auth_object_name = '_auth_%s' % args[0].lower().replace('-', '_')
756 try:
757 self.auth_object = getattr(self, auth_object_name)
758 except AttributeError:
759 self.push('504 Command parameter not implemented: unsupported '
760 ' authentication mechanism {!r}'.format(auth_object_name))
761 return
762 self.smtp_state = self.AUTH
763 self.auth_object(args[1] if len(args) == 2 else None)
764
765 def _authenticated(self, user, valid):
766 if valid:
767 self.authenticated_user = user
768 self.push('235 Authentication Succeeded')
769 else:
770 self.push('535 Authentication credentials invalid')
771 self.smtp_state = self.COMMAND
772
773 def _decode_base64(self, string):
774 return base64.decodebytes(string.encode('ascii')).decode('utf-8')
775
776 def _auth_plain(self, arg=None):
777 if arg is None:
778 self.push('334 ')
779 else:
780 logpass = self._decode_base64(arg)
781 try:
782 *_, user, password = logpass.split('\0')
783 except ValueError as e:
784 self.push('535 Splitting response {!r} into user and password'
785 ' failed: {}'.format(logpass, e))
786 return
787 self._authenticated(user, password == sim_auth[1])
788
789 def _auth_login(self, arg=None):
790 if arg is None:
791 # base64 encoded 'Username:'
792 self.push('334 VXNlcm5hbWU6')
793 elif not hasattr(self, '_auth_login_user'):
794 self._auth_login_user = self._decode_base64(arg)
795 # base64 encoded 'Password:'
796 self.push('334 UGFzc3dvcmQ6')
797 else:
798 password = self._decode_base64(arg)
799 self._authenticated(self._auth_login_user, password == sim_auth[1])
800 del self._auth_login_user
801
802 def _auth_cram_md5(self, arg=None):
803 if arg is None:
804 self.push('334 {}'.format(sim_cram_md5_challenge))
805 else:
806 logpass = self._decode_base64(arg)
807 try:
808 user, hashed_pass = logpass.split()
809 except ValueError as e:
Serhiy Storchaka34fd4c22018-11-05 16:20:25 +0200810 self.push('535 Splitting response {!r} into user and password '
R David Murrayb0deeb42015-11-08 01:03:52 -0500811 'failed: {}'.format(logpass, e))
812 return False
813 valid_hashed_pass = hmac.HMAC(
814 sim_auth[1].encode('ascii'),
815 self._decode_base64(sim_cram_md5_challenge).encode('ascii'),
816 'md5').hexdigest()
817 self._authenticated(user, hashed_pass == valid_hashed_pass)
818 # end AUTH related stuff.
819
Guido van Rossum04110fb2007-08-24 16:32:05 +0000820 def smtp_EHLO(self, arg):
R. David Murrayfb123912009-05-28 18:19:00 +0000821 resp = ('250-testhost\r\n'
822 '250-EXPN\r\n'
823 '250-SIZE 20000000\r\n'
824 '250-STARTTLS\r\n'
825 '250-DELIVERBY\r\n')
826 resp = resp + self._extrafeatures + '250 HELP'
Guido van Rossum04110fb2007-08-24 16:32:05 +0000827 self.push(resp)
R David Murrayf1a40b42013-03-20 21:12:17 -0400828 self.seen_greeting = arg
829 self.extended_smtp = True
Guido van Rossum04110fb2007-08-24 16:32:05 +0000830
831 def smtp_VRFY(self, arg):
R David Murray46346762011-07-18 21:38:54 -0400832 # For max compatibility smtplib should be sending the raw address.
833 if arg in sim_users:
834 self.push('250 %s %s' % (sim_users[arg], smtplib.quoteaddr(arg)))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000835 else:
836 self.push('550 No such user: %s' % arg)
837
838 def smtp_EXPN(self, arg):
R David Murray46346762011-07-18 21:38:54 -0400839 list_name = arg.lower()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000840 if list_name in sim_lists:
841 user_list = sim_lists[list_name]
842 for n, user_email in enumerate(user_list):
843 quoted_addr = smtplib.quoteaddr(user_email)
844 if n < len(user_list) - 1:
845 self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
846 else:
847 self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
848 else:
849 self.push('550 No access for you!')
850
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400851 def smtp_QUIT(self, arg):
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400852 if self.quit_response is None:
853 super(SimSMTPChannel, self).smtp_QUIT(arg)
854 else:
855 self.push(self.quit_response)
856 self.close_when_done()
857
R David Murrayd312c742013-03-20 20:36:14 -0400858 def smtp_MAIL(self, arg):
859 if self.mail_response is None:
860 super().smtp_MAIL(arg)
861 else:
862 self.push(self.mail_response)
R David Murrayafb151a2014-04-14 18:21:38 -0400863 if self.disconnect:
864 self.close_when_done()
R David Murrayd312c742013-03-20 20:36:14 -0400865
866 def smtp_RCPT(self, arg):
867 if self.rcpt_response is None:
868 super().smtp_RCPT(arg)
869 return
R David Murrayd312c742013-03-20 20:36:14 -0400870 self.rcpt_count += 1
R David Murray03b01162013-03-20 22:11:40 -0400871 self.push(self.rcpt_response[self.rcpt_count-1])
R David Murrayd312c742013-03-20 20:36:14 -0400872
873 def smtp_RSET(self, arg):
R David Murrayd312c742013-03-20 20:36:14 -0400874 self.rset_count += 1
R David Murray03b01162013-03-20 22:11:40 -0400875 super().smtp_RSET(arg)
R David Murrayd312c742013-03-20 20:36:14 -0400876
877 def smtp_DATA(self, arg):
878 if self.data_response is None:
879 super().smtp_DATA(arg)
880 else:
881 self.push(self.data_response)
882
Giampaolo Rodolàd930b632010-05-06 20:21:57 +0000883 def handle_error(self):
884 raise
885
Guido van Rossum04110fb2007-08-24 16:32:05 +0000886
887class SimSMTPServer(smtpd.SMTPServer):
R. David Murrayfb123912009-05-28 18:19:00 +0000888
R David Murrayd312c742013-03-20 20:36:14 -0400889 channel_class = SimSMTPChannel
Barry Warsaw1f5c9582011-03-15 15:04:44 -0400890
R. David Murray23ddc0e2009-05-29 18:03:16 +0000891 def __init__(self, *args, **kw):
892 self._extra_features = []
Stéphane Wirtel8d83e4b2018-01-31 01:02:51 +0100893 self._addresses = {}
R. David Murray23ddc0e2009-05-29 18:03:16 +0000894 smtpd.SMTPServer.__init__(self, *args, **kw)
895
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000896 def handle_accepted(self, conn, addr):
R David Murrayf1a40b42013-03-20 21:12:17 -0400897 self._SMTPchannel = self.channel_class(
R David Murray1144da52014-06-11 12:27:40 -0400898 self._extra_features, self, conn, addr,
899 decode_data=self._decode_data)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000900
901 def process_message(self, peer, mailfrom, rcpttos, data):
Stéphane Wirtel8d83e4b2018-01-31 01:02:51 +0100902 self._addresses['from'] = mailfrom
903 self._addresses['tos'] = rcpttos
Guido van Rossum04110fb2007-08-24 16:32:05 +0000904
R. David Murrayfb123912009-05-28 18:19:00 +0000905 def add_feature(self, feature):
R. David Murray23ddc0e2009-05-29 18:03:16 +0000906 self._extra_features.append(feature)
R. David Murrayfb123912009-05-28 18:19:00 +0000907
Giampaolo Rodolàd930b632010-05-06 20:21:57 +0000908 def handle_error(self):
909 raise
910
Guido van Rossum04110fb2007-08-24 16:32:05 +0000911
912# Test various SMTP & ESMTP commands/behaviors that require a simulated server
913# (i.e., something with more features than DebuggingServer)
Victor Stinner45df8202010-04-28 22:31:17 +0000914class SMTPSimTests(unittest.TestCase):
Guido van Rossum04110fb2007-08-24 16:32:05 +0000915
916 def setUp(self):
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100917 self.thread_key = threading_setup()
Richard Jones64b02de2010-08-03 06:39:33 +0000918 self.real_getfqdn = socket.getfqdn
919 socket.getfqdn = mock_socket.getfqdn
Guido van Rossum04110fb2007-08-24 16:32:05 +0000920 self.serv_evt = threading.Event()
921 self.client_evt = threading.Event()
Antoine Pitrou043bad02010-04-30 23:20:15 +0000922 # Pick a random unused port by passing 0 for the port number
R David Murray1144da52014-06-11 12:27:40 -0400923 self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1), decode_data=True)
Antoine Pitrou043bad02010-04-30 23:20:15 +0000924 # Keep a note of what port was assigned
925 self.port = self.serv.socket.getsockname()[1]
Christian Heimes5e696852008-04-09 08:37:03 +0000926 serv_args = (self.serv, self.serv_evt, self.client_evt)
Antoine Pitrouc3d47722009-10-27 19:49:45 +0000927 self.thread = threading.Thread(target=debugging_server, args=serv_args)
928 self.thread.start()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000929
930 # wait until server thread has assigned a port number
Christian Heimes380f7f22008-02-28 11:19:05 +0000931 self.serv_evt.wait()
932 self.serv_evt.clear()
Guido van Rossum04110fb2007-08-24 16:32:05 +0000933
934 def tearDown(self):
Richard Jones64b02de2010-08-03 06:39:33 +0000935 socket.getfqdn = self.real_getfqdn
Guido van Rossum04110fb2007-08-24 16:32:05 +0000936 # indicate that the client is finished
937 self.client_evt.set()
938 # wait for the server thread to terminate
939 self.serv_evt.wait()
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +0100940 join_thread(self.thread)
941 del self.thread
942 self.doCleanups()
943 threading_cleanup(*self.thread_key)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000944
945 def testBasic(self):
946 # smoke test
Christian Heimes5e696852008-04-09 08:37:03 +0000947 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000948 smtp.quit()
949
950 def testEHLO(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000951 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000952
953 # no features should be present before the EHLO
954 self.assertEqual(smtp.esmtp_features, {})
955
956 # features expected from the test server
957 expected_features = {'expn':'',
958 'size': '20000000',
959 'starttls': '',
960 'deliverby': '',
961 'help': '',
962 }
963
964 smtp.ehlo()
965 self.assertEqual(smtp.esmtp_features, expected_features)
966 for k in expected_features:
967 self.assertTrue(smtp.has_extn(k))
968 self.assertFalse(smtp.has_extn('unsupported-feature'))
969 smtp.quit()
970
971 def testVRFY(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000972 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000973
Barry Warsawc5ea7542015-07-09 10:39:55 -0400974 for addr_spec, name in sim_users.items():
Guido van Rossum04110fb2007-08-24 16:32:05 +0000975 expected_known = (250, bytes('%s %s' %
Barry Warsawc5ea7542015-07-09 10:39:55 -0400976 (name, smtplib.quoteaddr(addr_spec)),
Guido van Rossum5a23cc52007-08-30 14:02:43 +0000977 "ascii"))
Barry Warsawc5ea7542015-07-09 10:39:55 -0400978 self.assertEqual(smtp.vrfy(addr_spec), expected_known)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000979
980 u = 'nobody@nowhere.com'
R David Murray46346762011-07-18 21:38:54 -0400981 expected_unknown = (550, ('No such user: %s' % u).encode('ascii'))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000982 self.assertEqual(smtp.vrfy(u), expected_unknown)
983 smtp.quit()
984
985 def testEXPN(self):
Christian Heimes5e696852008-04-09 08:37:03 +0000986 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
Guido van Rossum04110fb2007-08-24 16:32:05 +0000987
988 for listname, members in sim_lists.items():
989 users = []
990 for m in members:
991 users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
Guido van Rossum5a23cc52007-08-30 14:02:43 +0000992 expected_known = (250, bytes('\n'.join(users), "ascii"))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000993 self.assertEqual(smtp.expn(listname), expected_known)
994
995 u = 'PSU-Members-List'
996 expected_unknown = (550, b'No access for you!')
997 self.assertEqual(smtp.expn(u), expected_unknown)
998 smtp.quit()
999
R David Murray76e13c12014-07-03 14:47:46 -04001000 def testAUTH_PLAIN(self):
1001 self.serv.add_feature("AUTH PLAIN")
1002 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murrayb0deeb42015-11-08 01:03:52 -05001003 resp = smtp.login(sim_auth[0], sim_auth[1])
1004 self.assertEqual(resp, (235, b'Authentication Succeeded'))
R David Murray76e13c12014-07-03 14:47:46 -04001005 smtp.close()
1006
R. David Murrayfb123912009-05-28 18:19:00 +00001007 def testAUTH_LOGIN(self):
R. David Murrayfb123912009-05-28 18:19:00 +00001008 self.serv.add_feature("AUTH LOGIN")
R. David Murray23ddc0e2009-05-29 18:03:16 +00001009 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murrayb0deeb42015-11-08 01:03:52 -05001010 resp = smtp.login(sim_auth[0], sim_auth[1])
1011 self.assertEqual(resp, (235, b'Authentication Succeeded'))
Benjamin Petersond094efd2010-10-31 17:15:42 +00001012 smtp.close()
R. David Murrayfb123912009-05-28 18:19:00 +00001013
Miss Islington (bot)66cd0412019-09-25 08:50:31 -07001014 @requires_hashdigest('md5')
R. David Murrayfb123912009-05-28 18:19:00 +00001015 def testAUTH_CRAM_MD5(self):
R. David Murrayfb123912009-05-28 18:19:00 +00001016 self.serv.add_feature("AUTH CRAM-MD5")
R. David Murray23ddc0e2009-05-29 18:03:16 +00001017 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murrayb0deeb42015-11-08 01:03:52 -05001018 resp = smtp.login(sim_auth[0], sim_auth[1])
1019 self.assertEqual(resp, (235, b'Authentication Succeeded'))
Benjamin Petersond094efd2010-10-31 17:15:42 +00001020 smtp.close()
R. David Murrayfb123912009-05-28 18:19:00 +00001021
Andrew Kuchling78591822013-11-11 14:03:23 -05001022 def testAUTH_multiple(self):
1023 # Test that multiple authentication methods are tried.
1024 self.serv.add_feature("AUTH BOGUS PLAIN LOGIN CRAM-MD5")
1025 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murrayb0deeb42015-11-08 01:03:52 -05001026 resp = smtp.login(sim_auth[0], sim_auth[1])
1027 self.assertEqual(resp, (235, b'Authentication Succeeded'))
R David Murray76e13c12014-07-03 14:47:46 -04001028 smtp.close()
1029
1030 def test_auth_function(self):
Miss Islington (bot)66cd0412019-09-25 08:50:31 -07001031 supported = {'PLAIN', 'LOGIN'}
1032 try:
1033 hashlib.md5()
1034 except ValueError:
1035 pass
1036 else:
1037 supported.add('CRAM-MD5')
R David Murrayb0deeb42015-11-08 01:03:52 -05001038 for mechanism in supported:
1039 self.serv.add_feature("AUTH {}".format(mechanism))
1040 for mechanism in supported:
1041 with self.subTest(mechanism=mechanism):
1042 smtp = smtplib.SMTP(HOST, self.port,
1043 local_hostname='localhost', timeout=15)
1044 smtp.ehlo('foo')
1045 smtp.user, smtp.password = sim_auth[0], sim_auth[1]
1046 method = 'auth_' + mechanism.lower().replace('-', '_')
1047 resp = smtp.auth(mechanism, getattr(smtp, method))
1048 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1049 smtp.close()
Andrew Kuchling78591822013-11-11 14:03:23 -05001050
R David Murray0cff49f2014-08-30 16:51:59 -04001051 def test_quit_resets_greeting(self):
1052 smtp = smtplib.SMTP(HOST, self.port,
1053 local_hostname='localhost',
1054 timeout=15)
1055 code, message = smtp.ehlo()
1056 self.assertEqual(code, 250)
1057 self.assertIn('size', smtp.esmtp_features)
1058 smtp.quit()
1059 self.assertNotIn('size', smtp.esmtp_features)
1060 smtp.connect(HOST, self.port)
1061 self.assertNotIn('size', smtp.esmtp_features)
1062 smtp.ehlo_or_helo_if_needed()
1063 self.assertIn('size', smtp.esmtp_features)
1064 smtp.quit()
1065
Barry Warsaw1f5c9582011-03-15 15:04:44 -04001066 def test_with_statement(self):
1067 with smtplib.SMTP(HOST, self.port) as smtp:
1068 code, message = smtp.noop()
1069 self.assertEqual(code, 250)
1070 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo')
1071 with smtplib.SMTP(HOST, self.port) as smtp:
1072 smtp.close()
1073 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo')
1074
1075 def test_with_statement_QUIT_failure(self):
Barry Warsaw1f5c9582011-03-15 15:04:44 -04001076 with self.assertRaises(smtplib.SMTPResponseException) as error:
1077 with smtplib.SMTP(HOST, self.port) as smtp:
1078 smtp.noop()
R David Murray6bd52022013-03-21 00:32:31 -04001079 self.serv._SMTPchannel.quit_response = '421 QUIT FAILED'
Barry Warsaw1f5c9582011-03-15 15:04:44 -04001080 self.assertEqual(error.exception.smtp_code, 421)
1081 self.assertEqual(error.exception.smtp_error, b'QUIT FAILED')
Barry Warsaw1f5c9582011-03-15 15:04:44 -04001082
R. David Murrayfb123912009-05-28 18:19:00 +00001083 #TODO: add tests for correct AUTH method fallback now that the
1084 #test infrastructure can support it.
1085
R David Murrayafb151a2014-04-14 18:21:38 -04001086 # Issue 17498: make sure _rset does not raise SMTPServerDisconnected exception
1087 def test__rest_from_mail_cmd(self):
1088 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
1089 smtp.noop()
1090 self.serv._SMTPchannel.mail_response = '451 Requested action aborted'
1091 self.serv._SMTPchannel.disconnect = True
1092 with self.assertRaises(smtplib.SMTPSenderRefused):
1093 smtp.sendmail('John', 'Sally', 'test message')
1094 self.assertIsNone(smtp.sock)
1095
R David Murrayd312c742013-03-20 20:36:14 -04001096 # Issue 5713: make sure close, not rset, is called if we get a 421 error
1097 def test_421_from_mail_cmd(self):
1098 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murray853c0f92013-03-20 21:54:05 -04001099 smtp.noop()
R David Murrayd312c742013-03-20 20:36:14 -04001100 self.serv._SMTPchannel.mail_response = '421 closing connection'
1101 with self.assertRaises(smtplib.SMTPSenderRefused):
1102 smtp.sendmail('John', 'Sally', 'test message')
1103 self.assertIsNone(smtp.sock)
R David Murray03b01162013-03-20 22:11:40 -04001104 self.assertEqual(self.serv._SMTPchannel.rset_count, 0)
R David Murrayd312c742013-03-20 20:36:14 -04001105
1106 def test_421_from_rcpt_cmd(self):
1107 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murray853c0f92013-03-20 21:54:05 -04001108 smtp.noop()
R David Murrayd312c742013-03-20 20:36:14 -04001109 self.serv._SMTPchannel.rcpt_response = ['250 accepted', '421 closing']
1110 with self.assertRaises(smtplib.SMTPRecipientsRefused) as r:
1111 smtp.sendmail('John', ['Sally', 'Frank', 'George'], 'test message')
1112 self.assertIsNone(smtp.sock)
1113 self.assertEqual(self.serv._SMTPchannel.rset_count, 0)
1114 self.assertDictEqual(r.exception.args[0], {'Frank': (421, b'closing')})
1115
1116 def test_421_from_data_cmd(self):
1117 class MySimSMTPChannel(SimSMTPChannel):
1118 def found_terminator(self):
1119 if self.smtp_state == self.DATA:
1120 self.push('421 closing')
1121 else:
1122 super().found_terminator()
1123 self.serv.channel_class = MySimSMTPChannel
1124 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15)
R David Murray853c0f92013-03-20 21:54:05 -04001125 smtp.noop()
R David Murrayd312c742013-03-20 20:36:14 -04001126 with self.assertRaises(smtplib.SMTPDataError):
1127 smtp.sendmail('John@foo.org', ['Sally@foo.org'], 'test message')
1128 self.assertIsNone(smtp.sock)
1129 self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0)
1130
R David Murraycee7cf62015-05-16 13:58:14 -04001131 def test_smtputf8_NotSupportedError_if_no_server_support(self):
1132 smtp = smtplib.SMTP(
1133 HOST, self.port, local_hostname='localhost', timeout=3)
1134 self.addCleanup(smtp.close)
1135 smtp.ehlo()
1136 self.assertTrue(smtp.does_esmtp)
1137 self.assertFalse(smtp.has_extn('smtputf8'))
1138 self.assertRaises(
1139 smtplib.SMTPNotSupportedError,
1140 smtp.sendmail,
1141 'John', 'Sally', '', mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
1142 self.assertRaises(
1143 smtplib.SMTPNotSupportedError,
1144 smtp.mail, 'John', options=['BODY=8BITMIME', 'SMTPUTF8'])
1145
1146 def test_send_unicode_without_SMTPUTF8(self):
1147 smtp = smtplib.SMTP(
1148 HOST, self.port, local_hostname='localhost', timeout=3)
1149 self.addCleanup(smtp.close)
1150 self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '')
1151 self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice')
1152
chason48ed88a2018-07-26 04:01:28 +09001153 def test_send_message_error_on_non_ascii_addrs_if_no_smtputf8(self):
1154 # This test is located here and not in the SMTPUTF8SimTests
1155 # class because it needs a "regular" SMTP server to work
1156 msg = EmailMessage()
1157 msg['From'] = "Páolo <főo@bar.com>"
1158 msg['To'] = 'Dinsdale'
1159 msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
1160 smtp = smtplib.SMTP(
1161 HOST, self.port, local_hostname='localhost', timeout=3)
1162 self.addCleanup(smtp.close)
1163 with self.assertRaises(smtplib.SMTPNotSupportedError):
1164 smtp.send_message(msg)
1165
Stéphane Wirtel8d83e4b2018-01-31 01:02:51 +01001166 def test_name_field_not_included_in_envelop_addresses(self):
1167 smtp = smtplib.SMTP(
1168 HOST, self.port, local_hostname='localhost', timeout=3
1169 )
1170 self.addCleanup(smtp.close)
1171
1172 message = EmailMessage()
1173 message['From'] = email.utils.formataddr(('Michaël', 'michael@example.com'))
1174 message['To'] = email.utils.formataddr(('René', 'rene@example.com'))
1175
1176 self.assertDictEqual(smtp.send_message(message), {})
1177
1178 self.assertEqual(self.serv._addresses['from'], 'michael@example.com')
1179 self.assertEqual(self.serv._addresses['tos'], ['rene@example.com'])
1180
R David Murraycee7cf62015-05-16 13:58:14 -04001181
1182class SimSMTPUTF8Server(SimSMTPServer):
1183
1184 def __init__(self, *args, **kw):
1185 # The base SMTP server turns these on automatically, but our test
1186 # server is set up to munge the EHLO response, so we need to provide
1187 # them as well. And yes, the call is to SMTPServer not SimSMTPServer.
1188 self._extra_features = ['SMTPUTF8', '8BITMIME']
1189 smtpd.SMTPServer.__init__(self, *args, **kw)
1190
1191 def handle_accepted(self, conn, addr):
1192 self._SMTPchannel = self.channel_class(
1193 self._extra_features, self, conn, addr,
1194 decode_data=self._decode_data,
1195 enable_SMTPUTF8=self.enable_SMTPUTF8,
1196 )
1197
1198 def process_message(self, peer, mailfrom, rcpttos, data, mail_options=None,
1199 rcpt_options=None):
1200 self.last_peer = peer
1201 self.last_mailfrom = mailfrom
1202 self.last_rcpttos = rcpttos
1203 self.last_message = data
1204 self.last_mail_options = mail_options
1205 self.last_rcpt_options = rcpt_options
1206
1207
R David Murraycee7cf62015-05-16 13:58:14 -04001208class SMTPUTF8SimTests(unittest.TestCase):
1209
R David Murray83084442015-05-17 19:27:22 -04001210 maxDiff = None
1211
R David Murraycee7cf62015-05-16 13:58:14 -04001212 def setUp(self):
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +01001213 self.thread_key = threading_setup()
R David Murraycee7cf62015-05-16 13:58:14 -04001214 self.real_getfqdn = socket.getfqdn
1215 socket.getfqdn = mock_socket.getfqdn
1216 self.serv_evt = threading.Event()
1217 self.client_evt = threading.Event()
1218 # Pick a random unused port by passing 0 for the port number
1219 self.serv = SimSMTPUTF8Server((HOST, 0), ('nowhere', -1),
1220 decode_data=False,
1221 enable_SMTPUTF8=True)
1222 # Keep a note of what port was assigned
1223 self.port = self.serv.socket.getsockname()[1]
1224 serv_args = (self.serv, self.serv_evt, self.client_evt)
1225 self.thread = threading.Thread(target=debugging_server, args=serv_args)
1226 self.thread.start()
1227
1228 # wait until server thread has assigned a port number
1229 self.serv_evt.wait()
1230 self.serv_evt.clear()
1231
1232 def tearDown(self):
1233 socket.getfqdn = self.real_getfqdn
1234 # indicate that the client is finished
1235 self.client_evt.set()
1236 # wait for the server thread to terminate
1237 self.serv_evt.wait()
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +01001238 join_thread(self.thread)
1239 del self.thread
1240 self.doCleanups()
1241 threading_cleanup(*self.thread_key)
R David Murraycee7cf62015-05-16 13:58:14 -04001242
1243 def test_test_server_supports_extensions(self):
1244 smtp = smtplib.SMTP(
1245 HOST, self.port, local_hostname='localhost', timeout=3)
1246 self.addCleanup(smtp.close)
1247 smtp.ehlo()
1248 self.assertTrue(smtp.does_esmtp)
1249 self.assertTrue(smtp.has_extn('smtputf8'))
1250
1251 def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
1252 m = '¡a test message containing unicode!'.encode('utf-8')
1253 smtp = smtplib.SMTP(
1254 HOST, self.port, local_hostname='localhost', timeout=3)
1255 self.addCleanup(smtp.close)
1256 smtp.sendmail('Jőhn', 'Sálly', m,
1257 mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
1258 self.assertEqual(self.serv.last_mailfrom, 'Jőhn')
1259 self.assertEqual(self.serv.last_rcpttos, ['Sálly'])
1260 self.assertEqual(self.serv.last_message, m)
1261 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1262 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1263 self.assertEqual(self.serv.last_rcpt_options, [])
1264
1265 def test_send_unicode_with_SMTPUTF8_via_low_level_API(self):
1266 m = '¡a test message containing unicode!'.encode('utf-8')
1267 smtp = smtplib.SMTP(
1268 HOST, self.port, local_hostname='localhost', timeout=3)
1269 self.addCleanup(smtp.close)
1270 smtp.ehlo()
1271 self.assertEqual(
1272 smtp.mail('Jő', options=['BODY=8BITMIME', 'SMTPUTF8']),
1273 (250, b'OK'))
1274 self.assertEqual(smtp.rcpt('János'), (250, b'OK'))
1275 self.assertEqual(smtp.data(m), (250, b'OK'))
1276 self.assertEqual(self.serv.last_mailfrom, 'Jő')
1277 self.assertEqual(self.serv.last_rcpttos, ['János'])
1278 self.assertEqual(self.serv.last_message, m)
1279 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1280 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1281 self.assertEqual(self.serv.last_rcpt_options, [])
1282
R David Murray83084442015-05-17 19:27:22 -04001283 def test_send_message_uses_smtputf8_if_addrs_non_ascii(self):
1284 msg = EmailMessage()
1285 msg['From'] = "Páolo <főo@bar.com>"
1286 msg['To'] = 'Dinsdale'
1287 msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
1288 # XXX I don't know why I need two \n's here, but this is an existing
1289 # bug (if it is one) and not a problem with the new functionality.
1290 msg.set_content("oh là là, know what I mean, know what I mean?\n\n")
1291 # XXX smtpd converts received /r/n to /n, so we can't easily test that
1292 # we are successfully sending /r/n :(.
1293 expected = textwrap.dedent("""\
1294 From: Páolo <főo@bar.com>
1295 To: Dinsdale
1296 Subject: Nudge nudge, wink, wink \u1F609
1297 Content-Type: text/plain; charset="utf-8"
1298 Content-Transfer-Encoding: 8bit
1299 MIME-Version: 1.0
1300
1301 oh là là, know what I mean, know what I mean?
1302 """)
1303 smtp = smtplib.SMTP(
1304 HOST, self.port, local_hostname='localhost', timeout=3)
1305 self.addCleanup(smtp.close)
1306 self.assertEqual(smtp.send_message(msg), {})
1307 self.assertEqual(self.serv.last_mailfrom, 'főo@bar.com')
1308 self.assertEqual(self.serv.last_rcpttos, ['Dinsdale'])
1309 self.assertEqual(self.serv.last_message.decode(), expected)
1310 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1311 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1312 self.assertEqual(self.serv.last_rcpt_options, [])
1313
Guido van Rossum04110fb2007-08-24 16:32:05 +00001314
Barry Warsawc5ea7542015-07-09 10:39:55 -04001315EXPECTED_RESPONSE = encode_base64(b'\0psu\0doesnotexist', eol='')
1316
1317class SimSMTPAUTHInitialResponseChannel(SimSMTPChannel):
1318 def smtp_AUTH(self, arg):
1319 # RFC 4954's AUTH command allows for an optional initial-response.
1320 # Not all AUTH methods support this; some require a challenge. AUTH
1321 # PLAIN does those, so test that here. See issue #15014.
1322 args = arg.split()
1323 if args[0].lower() == 'plain':
1324 if len(args) == 2:
1325 # AUTH PLAIN <initial-response> with the response base 64
1326 # encoded. Hard code the expected response for the test.
1327 if args[1] == EXPECTED_RESPONSE:
1328 self.push('235 Ok')
1329 return
1330 self.push('571 Bad authentication')
1331
1332class SimSMTPAUTHInitialResponseServer(SimSMTPServer):
1333 channel_class = SimSMTPAUTHInitialResponseChannel
1334
1335
Barry Warsawc5ea7542015-07-09 10:39:55 -04001336class SMTPAUTHInitialResponseSimTests(unittest.TestCase):
1337 def setUp(self):
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +01001338 self.thread_key = threading_setup()
Barry Warsawc5ea7542015-07-09 10:39:55 -04001339 self.real_getfqdn = socket.getfqdn
1340 socket.getfqdn = mock_socket.getfqdn
1341 self.serv_evt = threading.Event()
1342 self.client_evt = threading.Event()
1343 # Pick a random unused port by passing 0 for the port number
1344 self.serv = SimSMTPAUTHInitialResponseServer(
1345 (HOST, 0), ('nowhere', -1), decode_data=True)
1346 # Keep a note of what port was assigned
1347 self.port = self.serv.socket.getsockname()[1]
1348 serv_args = (self.serv, self.serv_evt, self.client_evt)
1349 self.thread = threading.Thread(target=debugging_server, args=serv_args)
1350 self.thread.start()
1351
1352 # wait until server thread has assigned a port number
1353 self.serv_evt.wait()
1354 self.serv_evt.clear()
1355
1356 def tearDown(self):
1357 socket.getfqdn = self.real_getfqdn
1358 # indicate that the client is finished
1359 self.client_evt.set()
1360 # wait for the server thread to terminate
1361 self.serv_evt.wait()
Pablo Galindo5b7a2cb2018-09-08 00:15:22 +01001362 join_thread(self.thread)
1363 del self.thread
1364 self.doCleanups()
1365 threading_cleanup(*self.thread_key)
Barry Warsawc5ea7542015-07-09 10:39:55 -04001366
1367 def testAUTH_PLAIN_initial_response_login(self):
1368 self.serv.add_feature('AUTH PLAIN')
1369 smtp = smtplib.SMTP(HOST, self.port,
1370 local_hostname='localhost', timeout=15)
1371 smtp.login('psu', 'doesnotexist')
1372 smtp.close()
1373
1374 def testAUTH_PLAIN_initial_response_auth(self):
1375 self.serv.add_feature('AUTH PLAIN')
1376 smtp = smtplib.SMTP(HOST, self.port,
1377 local_hostname='localhost', timeout=15)
1378 smtp.user = 'psu'
1379 smtp.password = 'doesnotexist'
1380 code, response = smtp.auth('plain', smtp.auth_plain)
1381 smtp.close()
1382 self.assertEqual(code, 235)
1383
1384
Guido van Rossumd8faa362007-04-27 19:54:29 +00001385if __name__ == '__main__':
chason48ed88a2018-07-26 04:01:28 +09001386 unittest.main()