Debian Codegraph @

Lib/test/test_smtplib.py

raw
1 import base64
2 import email.mime.text
3 from email.message import EmailMessage
4 from email.base64mime import body_encode as encode_base64
5 import email.utils
6 import hashlib
7 import hmac
8 import socket
9 import smtplib
10 import io
11 import re
12 import sys
13 import time
14 import select
15 import errno
16 import textwrap
17 import threading
18
19 import unittest
20 import unittest.mock as mock
21 from test import support, mock_socket
22 from test.support import hashlib_helper
23 from test.support import socket_helper
24 from test.support import threading_helper
25 from test.support import asyncore
26 from test.support import smtpd
27 from unittest.mock import Mock
28
29
30 support.requires_working_socket(module=True)
31
32 HOST = socket_helper.HOST
33
34 if sys.platform == 'darwin':
35 # select.poll returns a select.POLLHUP at the end of the tests
36 # on darwin, so just ignore it
37 def handle_expt(self):
38 pass
39 smtpd.SMTPChannel.handle_expt = handle_expt
40
41
42 def server(evt, buf, serv):
43 serv.listen()
44 evt.set()
45 try:
46 conn, addr = serv.accept()
47 except TimeoutError:
48 pass
49 else:
50 n = 500
51 while buf and n > 0:
52 r, w, e = select.select([], [conn], [])
53 if w:
54 sent = conn.send(buf)
55 buf = buf[sent:]
56
57 n -= 1
58
59 conn.close()
60 finally:
61 serv.close()
62 evt.set()
63
64 class GeneralTests:
65
66 def setUp(self):
67 smtplib.socket = mock_socket
68 self.port = 25
69
70 def tearDown(self):
71 smtplib.socket = socket
72
73 # This method is no longer used but is retained for backward compatibility,
74 # so test to make sure it still works.
75 def testQuoteData(self):
76 teststr = "abc\n.jkl\rfoo\r\n..blue"
77 expected = "abc\r\n..jkl\r\nfoo\r\n...blue"
78 self.assertEqual(expected, smtplib.quotedata(teststr))
79
80 def testBasic1(self):
81 mock_socket.reply_with(b"220 Hola mundo")
82 # connects
83 client = self.client(HOST, self.port)
84 client.close()
85
86 def testSourceAddress(self):
87 mock_socket.reply_with(b"220 Hola mundo")
88 # connects
89 client = self.client(HOST, self.port,
90 source_address=('127.0.0.1',19876))
91 self.assertEqual(client.source_address, ('127.0.0.1', 19876))
92 client.close()
93
94 def testBasic2(self):
95 mock_socket.reply_with(b"220 Hola mundo")
96 # connects, include port in host name
97 client = self.client("%s:%s" % (HOST, self.port))
98 client.close()
99
100 def testLocalHostName(self):
101 mock_socket.reply_with(b"220 Hola mundo")
102 # check that supplied local_hostname is used
103 client = self.client(HOST, self.port, local_hostname="testhost")
104 self.assertEqual(client.local_hostname, "testhost")
105 client.close()
106
107 def testTimeoutDefault(self):
108 mock_socket.reply_with(b"220 Hola mundo")
109 self.assertIsNone(mock_socket.getdefaulttimeout())
110 mock_socket.setdefaulttimeout(30)
111 self.assertEqual(mock_socket.getdefaulttimeout(), 30)
112 try:
113 client = self.client(HOST, self.port)
114 finally:
115 mock_socket.setdefaulttimeout(None)
116 self.assertEqual(client.sock.gettimeout(), 30)
117 client.close()
118
119 def testTimeoutNone(self):
120 mock_socket.reply_with(b"220 Hola mundo")
121 self.assertIsNone(socket.getdefaulttimeout())
122 socket.setdefaulttimeout(30)
123 try:
124 client = self.client(HOST, self.port, timeout=None)
125 finally:
126 socket.setdefaulttimeout(None)
127 self.assertIsNone(client.sock.gettimeout())
128 client.close()
129
130 def testTimeoutZero(self):
131 mock_socket.reply_with(b"220 Hola mundo")
132 with self.assertRaises(ValueError):
133 self.client(HOST, self.port, timeout=0)
134
135 def testTimeoutValue(self):
136 mock_socket.reply_with(b"220 Hola mundo")
137 client = self.client(HOST, self.port, timeout=30)
138 self.assertEqual(client.sock.gettimeout(), 30)
139 client.close()
140
141 def test_debuglevel(self):
142 mock_socket.reply_with(b"220 Hello world")
143 client = self.client()
144 client.set_debuglevel(1)
145 with support.captured_stderr() as stderr:
146 client.connect(HOST, self.port)
147 client.close()
148 expected = re.compile(r"^connect:", re.MULTILINE)
149 self.assertRegex(stderr.getvalue(), expected)
150
151 def test_debuglevel_2(self):
152 mock_socket.reply_with(b"220 Hello world")
153 client = self.client()
154 client.set_debuglevel(2)
155 with support.captured_stderr() as stderr:
156 client.connect(HOST, self.port)
157 client.close()
158 expected = re.compile(r"^\d{2}:\d{2}:\d{2}\.\d{6} connect: ",
159 re.MULTILINE)
160 self.assertRegex(stderr.getvalue(), expected)
161
162
163 class SMTPGeneralTests(GeneralTests, unittest.TestCase):
164
165 client = smtplib.SMTP
166
167
168 class LMTPGeneralTests(GeneralTests, unittest.TestCase):
169
170 client = smtplib.LMTP
171
172 @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), "test requires Unix domain socket")
173 def testUnixDomainSocketTimeoutDefault(self):
174 local_host = '/some/local/lmtp/delivery/program'
175 mock_socket.reply_with(b"220 Hello world")
176 try:
177 client = self.client(local_host, self.port)
178 finally:
179 mock_socket.setdefaulttimeout(None)
180 self.assertIsNone(client.sock.gettimeout())
181 client.close()
182
183 def testTimeoutZero(self):
184 super().testTimeoutZero()
185 local_host = '/some/local/lmtp/delivery/program'
186 with self.assertRaises(ValueError):
187 self.client(local_host, timeout=0)
188
189 # Test server thread using the specified SMTP server class
190 def debugging_server(serv, serv_evt, client_evt):
191 serv_evt.set()
192
193 try:
194 if hasattr(select, 'poll'):
195 poll_fun = asyncore.poll2
196 else:
197 poll_fun = asyncore.poll
198
199 n = 1000
200 while asyncore.socket_map and n > 0:
201 poll_fun(0.01, asyncore.socket_map)
202
203 # when the client conversation is finished, it will
204 # set client_evt, and it's then ok to kill the server
205 if client_evt.is_set():
206 serv.close()
207 break
208
209 n -= 1
210
211 except TimeoutError:
212 pass
213 finally:
214 if not client_evt.is_set():
215 # allow some time for the client to read the result
216 time.sleep(0.5)
217 serv.close()
218 asyncore.close_all()
219 serv_evt.set()
220
221 MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
222 MSG_END = '------------ END MESSAGE ------------\n'
223
224 # NOTE: Some SMTP objects in the tests below are created with a non-default
225 # local_hostname argument to the constructor, since (on some systems) the FQDN
226 # lookup caused by the default local_hostname sometimes takes so long that the
227 # test server times out, causing the test to fail.
228
229 # Test behavior of smtpd.DebuggingServer
230 class DebuggingServerTests(unittest.TestCase):
231
232 maxDiff = None
233
234 def setUp(self):
235 self.thread_key = threading_helper.threading_setup()
236 self.real_getfqdn = socket.getfqdn
237 socket.getfqdn = mock_socket.getfqdn
238 # temporarily replace sys.stdout to capture DebuggingServer output
239 self.old_stdout = sys.stdout
240 self.output = io.StringIO()
241 sys.stdout = self.output
242
243 self.serv_evt = threading.Event()
244 self.client_evt = threading.Event()
245 # Capture SMTPChannel debug output
246 self.old_DEBUGSTREAM = smtpd.DEBUGSTREAM
247 smtpd.DEBUGSTREAM = io.StringIO()
248 # Pick a random unused port by passing 0 for the port number
249 self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1),
250 decode_data=True)
251 # Keep a note of what server host and port were assigned
252 self.host, self.port = self.serv.socket.getsockname()[:2]
253 serv_args = (self.serv, self.serv_evt, self.client_evt)
254 self.thread = threading.Thread(target=debugging_server, args=serv_args)
255 self.thread.start()
256
257 # wait until server thread has assigned a port number
258 self.serv_evt.wait()
259 self.serv_evt.clear()
260
261 def tearDown(self):
262 socket.getfqdn = self.real_getfqdn
263 # indicate that the client is finished
264 self.client_evt.set()
265 # wait for the server thread to terminate
266 self.serv_evt.wait()
267 threading_helper.join_thread(self.thread)
268 # restore sys.stdout
269 sys.stdout = self.old_stdout
270 # restore DEBUGSTREAM
271 smtpd.DEBUGSTREAM.close()
272 smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM
273 del self.thread
274 self.doCleanups()
275 threading_helper.threading_cleanup(*self.thread_key)
276
277 def get_output_without_xpeer(self):
278 test_output = self.output.getvalue()
279 return re.sub(r'(.*?)^X-Peer:\s*\S+\n(.*)', r'\1\2',
280 test_output, flags=re.MULTILINE|re.DOTALL)
281
282 def testBasic(self):
283 # connect
284 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
285 timeout=support.LOOPBACK_TIMEOUT)
286 smtp.quit()
287
288 def testSourceAddress(self):
289 # connect
290 src_port = socket_helper.find_unused_port()
291 try:
292 smtp = smtplib.SMTP(self.host, self.port, local_hostname='localhost',
293 timeout=support.LOOPBACK_TIMEOUT,
294 source_address=(self.host, src_port))
295 self.addCleanup(smtp.close)
296 self.assertEqual(smtp.source_address, (self.host, src_port))
297 self.assertEqual(smtp.local_hostname, 'localhost')
298 smtp.quit()
299 except OSError as e:
300 if e.errno == errno.EADDRINUSE:
301 self.skipTest("couldn't bind to source port %d" % src_port)
302 raise
303
304 def testNOOP(self):
305 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
306 timeout=support.LOOPBACK_TIMEOUT)
307 self.addCleanup(smtp.close)
308 expected = (250, b'OK')
309 self.assertEqual(smtp.noop(), expected)
310 smtp.quit()
311
312 def testRSET(self):
313 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
314 timeout=support.LOOPBACK_TIMEOUT)
315 self.addCleanup(smtp.close)
316 expected = (250, b'OK')
317 self.assertEqual(smtp.rset(), expected)
318 smtp.quit()
319
320 def testELHO(self):
321 # EHLO isn't implemented in DebuggingServer
322 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
323 timeout=support.LOOPBACK_TIMEOUT)
324 self.addCleanup(smtp.close)
325 expected = (250, b'\nSIZE 33554432\nHELP')
326 self.assertEqual(smtp.ehlo(), expected)
327 smtp.quit()
328
329 def testEXPNNotImplemented(self):
330 # EXPN isn't implemented in DebuggingServer
331 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
332 timeout=support.LOOPBACK_TIMEOUT)
333 self.addCleanup(smtp.close)
334 expected = (502, b'EXPN not implemented')
335 smtp.putcmd('EXPN')
336 self.assertEqual(smtp.getreply(), expected)
337 smtp.quit()
338
339 def test_issue43124_putcmd_escapes_newline(self):
340 # see: https://bugs.python.org/issue43124
341 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
342 timeout=support.LOOPBACK_TIMEOUT)
343 self.addCleanup(smtp.close)
344 with self.assertRaises(ValueError) as exc:
345 smtp.putcmd('helo\nX-INJECTED')
346 self.assertIn("prohibited newline characters", str(exc.exception))
347 smtp.quit()
348
349 def testVRFY(self):
350 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
351 timeout=support.LOOPBACK_TIMEOUT)
352 self.addCleanup(smtp.close)
353 expected = (252, b'Cannot VRFY user, but will accept message ' + \
354 b'and attempt delivery')
355 self.assertEqual(smtp.vrfy('[email protected]'), expected)
356 self.assertEqual(smtp.verify('[email protected]'), expected)
357 smtp.quit()
358
359 def testSecondHELO(self):
360 # check that a second HELO returns a message that it's a duplicate
361 # (this behavior is specific to smtpd.SMTPChannel)
362 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
363 timeout=support.LOOPBACK_TIMEOUT)
364 self.addCleanup(smtp.close)
365 smtp.helo()
366 expected = (503, b'Duplicate HELO/EHLO')
367 self.assertEqual(smtp.helo(), expected)
368 smtp.quit()
369
370 def testHELP(self):
371 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
372 timeout=support.LOOPBACK_TIMEOUT)
373 self.addCleanup(smtp.close)
374 self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \
375 b'RCPT DATA RSET NOOP QUIT VRFY')
376 smtp.quit()
377
378 def testSend(self):
379 # connect and send mail
380 m = 'A test message'
381 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
382 timeout=support.LOOPBACK_TIMEOUT)
383 self.addCleanup(smtp.close)
384 smtp.sendmail('John', 'Sally', m)
385 # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
386 # in asyncore. This sleep might help, but should really be fixed
387 # properly by using an Event variable.
388 time.sleep(0.01)
389 smtp.quit()
390
391 self.client_evt.set()
392 self.serv_evt.wait()
393 self.output.flush()
394 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
395 self.assertEqual(self.output.getvalue(), mexpect)
396
397 def testSendBinary(self):
398 m = b'A test message'
399 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
400 timeout=support.LOOPBACK_TIMEOUT)
401 self.addCleanup(smtp.close)
402 smtp.sendmail('John', 'Sally', m)
403 # XXX (see comment in testSend)
404 time.sleep(0.01)
405 smtp.quit()
406
407 self.client_evt.set()
408 self.serv_evt.wait()
409 self.output.flush()
410 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.decode('ascii'), MSG_END)
411 self.assertEqual(self.output.getvalue(), mexpect)
412
413 def testSendNeedingDotQuote(self):
414 # Issue 12283
415 m = '.A test\n.mes.sage.'
416 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
417 timeout=support.LOOPBACK_TIMEOUT)
418 self.addCleanup(smtp.close)
419 smtp.sendmail('John', 'Sally', m)
420 # XXX (see comment in testSend)
421 time.sleep(0.01)
422 smtp.quit()
423
424 self.client_evt.set()
425 self.serv_evt.wait()
426 self.output.flush()
427 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
428 self.assertEqual(self.output.getvalue(), mexpect)
429
430 def test_issue43124_escape_localhostname(self):
431 # see: https://bugs.python.org/issue43124
432 # connect and send mail
433 m = 'wazzuuup\nlinetwo'
434 smtp = smtplib.SMTP(HOST, self.port, local_hostname='hi\nX-INJECTED',
435 timeout=support.LOOPBACK_TIMEOUT)
436 self.addCleanup(smtp.close)
437 with self.assertRaises(ValueError) as exc:
438 smtp.sendmail("[email protected]", "[email protected]", m)
439 self.assertIn(
440 "prohibited newline characters: ehlo hi\\nX-INJECTED",
441 str(exc.exception),
442 )
443 # XXX (see comment in testSend)
444 time.sleep(0.01)
445 smtp.quit()
446
447 debugout = smtpd.DEBUGSTREAM.getvalue()
448 self.assertNotIn("X-INJECTED", debugout)
449
450 def test_issue43124_escape_options(self):
451 # see: https://bugs.python.org/issue43124
452 # connect and send mail
453 m = 'wazzuuup\nlinetwo'
454 smtp = smtplib.SMTP(
455 HOST, self.port, local_hostname='localhost',
456 timeout=support.LOOPBACK_TIMEOUT)
457
458 self.addCleanup(smtp.close)
459 smtp.sendmail("[email protected]", "[email protected]", m)
460 with self.assertRaises(ValueError) as exc:
461 smtp.mail("[email protected]", ["X-OPTION\nX-INJECTED-1", "X-OPTION2\nX-INJECTED-2"])
462 msg = str(exc.exception)
463 self.assertIn("prohibited newline characters", msg)
464 self.assertIn("X-OPTION\\nX-INJECTED-1 X-OPTION2\\nX-INJECTED-2", msg)
465 # XXX (see comment in testSend)
466 time.sleep(0.01)
467 smtp.quit()
468
469 debugout = smtpd.DEBUGSTREAM.getvalue()
470 self.assertNotIn("X-OPTION", debugout)
471 self.assertNotIn("X-OPTION2", debugout)
472 self.assertNotIn("X-INJECTED-1", debugout)
473 self.assertNotIn("X-INJECTED-2", debugout)
474
475 def testSendNullSender(self):
476 m = 'A test message'
477 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
478 timeout=support.LOOPBACK_TIMEOUT)
479 self.addCleanup(smtp.close)
480 smtp.sendmail('<>', 'Sally', m)
481 # XXX (see comment in testSend)
482 time.sleep(0.01)
483 smtp.quit()
484
485 self.client_evt.set()
486 self.serv_evt.wait()
487 self.output.flush()
488 mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END)
489 self.assertEqual(self.output.getvalue(), mexpect)
490 debugout = smtpd.DEBUGSTREAM.getvalue()
491 sender = re.compile("^sender: <>$", re.MULTILINE)
492 self.assertRegex(debugout, sender)
493
494 def testSendMessage(self):
495 m = email.mime.text.MIMEText('A test message')
496 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
497 timeout=support.LOOPBACK_TIMEOUT)
498 self.addCleanup(smtp.close)
499 smtp.send_message(m, from_addr='John', to_addrs='Sally')
500 # XXX (see comment in testSend)
501 time.sleep(0.01)
502 smtp.quit()
503
504 self.client_evt.set()
505 self.serv_evt.wait()
506 self.output.flush()
507 # Remove the X-Peer header that DebuggingServer adds as figuring out
508 # exactly what IP address format is put there is not easy (and
509 # irrelevant to our test). Typically 127.0.0.1 or ::1, but it is
510 # not always the same as socket.gethostbyname(HOST). :(
511 test_output = self.get_output_without_xpeer()
512 del m['X-Peer']
513 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
514 self.assertEqual(test_output, mexpect)
515
516 def testSendMessageWithAddresses(self):
517 m = email.mime.text.MIMEText('A test message')
518 m['From'] = '[email protected]'
519 m['To'] = 'John'
520 m['CC'] = 'Sally, Fred'
521 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <[email protected]>'
522 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
523 timeout=support.LOOPBACK_TIMEOUT)
524 self.addCleanup(smtp.close)
525 smtp.send_message(m)
526 # XXX (see comment in testSend)
527 time.sleep(0.01)
528 smtp.quit()
529 # make sure the Bcc header is still in the message.
530 self.assertEqual(m['Bcc'], 'John Root <root@localhost>, "Dinsdale" '
531 '<[email protected]>')
532
533 self.client_evt.set()
534 self.serv_evt.wait()
535 self.output.flush()
536 # Remove the X-Peer header that DebuggingServer adds.
537 test_output = self.get_output_without_xpeer()
538 del m['X-Peer']
539 # The Bcc header should not be transmitted.
540 del m['Bcc']
541 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
542 self.assertEqual(test_output, mexpect)
543 debugout = smtpd.DEBUGSTREAM.getvalue()
544 sender = re.compile("^sender: [email protected]$", re.MULTILINE)
545 self.assertRegex(debugout, sender)
546 for addr in ('John', 'Sally', 'Fred', 'root@localhost',
547 '[email protected]'):
548 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
549 re.MULTILINE)
550 self.assertRegex(debugout, to_addr)
551
552 def testSendMessageWithSomeAddresses(self):
553 # Make sure nothing breaks if not all of the three 'to' headers exist
554 m = email.mime.text.MIMEText('A test message')
555 m['From'] = '[email protected]'
556 m['To'] = 'John, Dinsdale'
557 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
558 timeout=support.LOOPBACK_TIMEOUT)
559 self.addCleanup(smtp.close)
560 smtp.send_message(m)
561 # XXX (see comment in testSend)
562 time.sleep(0.01)
563 smtp.quit()
564
565 self.client_evt.set()
566 self.serv_evt.wait()
567 self.output.flush()
568 # Remove the X-Peer header that DebuggingServer adds.
569 test_output = self.get_output_without_xpeer()
570 del m['X-Peer']
571 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
572 self.assertEqual(test_output, mexpect)
573 debugout = smtpd.DEBUGSTREAM.getvalue()
574 sender = re.compile("^sender: [email protected]$", re.MULTILINE)
575 self.assertRegex(debugout, sender)
576 for addr in ('John', 'Dinsdale'):
577 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
578 re.MULTILINE)
579 self.assertRegex(debugout, to_addr)
580
581 def testSendMessageWithSpecifiedAddresses(self):
582 # Make sure addresses specified in call override those in message.
583 m = email.mime.text.MIMEText('A test message')
584 m['From'] = '[email protected]'
585 m['To'] = 'John, Dinsdale'
586 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
587 timeout=support.LOOPBACK_TIMEOUT)
588 self.addCleanup(smtp.close)
589 smtp.send_message(m, from_addr='[email protected]', to_addrs='[email protected]')
590 # XXX (see comment in testSend)
591 time.sleep(0.01)
592 smtp.quit()
593
594 self.client_evt.set()
595 self.serv_evt.wait()
596 self.output.flush()
597 # Remove the X-Peer header that DebuggingServer adds.
598 test_output = self.get_output_without_xpeer()
599 del m['X-Peer']
600 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
601 self.assertEqual(test_output, mexpect)
602 debugout = smtpd.DEBUGSTREAM.getvalue()
603 sender = re.compile("^sender: [email protected]$", re.MULTILINE)
604 self.assertRegex(debugout, sender)
605 for addr in ('John', 'Dinsdale'):
606 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
607 re.MULTILINE)
608 self.assertNotRegex(debugout, to_addr)
609 recip = re.compile(r"^recips: .*'[email protected]'.*$", re.MULTILINE)
610 self.assertRegex(debugout, recip)
611
612 def testSendMessageWithMultipleFrom(self):
613 # Sender overrides To
614 m = email.mime.text.MIMEText('A test message')
615 m['From'] = 'Bernard, Bianca'
616 m['Sender'] = '[email protected]'
617 m['To'] = 'John, Dinsdale'
618 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
619 timeout=support.LOOPBACK_TIMEOUT)
620 self.addCleanup(smtp.close)
621 smtp.send_message(m)
622 # XXX (see comment in testSend)
623 time.sleep(0.01)
624 smtp.quit()
625
626 self.client_evt.set()
627 self.serv_evt.wait()
628 self.output.flush()
629 # Remove the X-Peer header that DebuggingServer adds.
630 test_output = self.get_output_without_xpeer()
631 del m['X-Peer']
632 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
633 self.assertEqual(test_output, mexpect)
634 debugout = smtpd.DEBUGSTREAM.getvalue()
635 sender = re.compile("^sender: [email protected]$", re.MULTILINE)
636 self.assertRegex(debugout, sender)
637 for addr in ('John', 'Dinsdale'):
638 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
639 re.MULTILINE)
640 self.assertRegex(debugout, to_addr)
641
642 def testSendMessageResent(self):
643 m = email.mime.text.MIMEText('A test message')
644 m['From'] = '[email protected]'
645 m['To'] = 'John'
646 m['CC'] = 'Sally, Fred'
647 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <[email protected]>'
648 m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
649 m['Resent-From'] = '[email protected]'
650 m['Resent-To'] = 'Martha <[email protected]>, Jeff'
651 m['Resent-Bcc'] = '[email protected]'
652 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
653 timeout=support.LOOPBACK_TIMEOUT)
654 self.addCleanup(smtp.close)
655 smtp.send_message(m)
656 # XXX (see comment in testSend)
657 time.sleep(0.01)
658 smtp.quit()
659
660 self.client_evt.set()
661 self.serv_evt.wait()
662 self.output.flush()
663 # The Resent-Bcc headers are deleted before serialization.
664 del m['Bcc']
665 del m['Resent-Bcc']
666 # Remove the X-Peer header that DebuggingServer adds.
667 test_output = self.get_output_without_xpeer()
668 del m['X-Peer']
669 mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END)
670 self.assertEqual(test_output, mexpect)
671 debugout = smtpd.DEBUGSTREAM.getvalue()
672 sender = re.compile("^sender: [email protected]$", re.MULTILINE)
673 self.assertRegex(debugout, sender)
674 for addr in ('[email protected]', 'Jeff', '[email protected]'):
675 to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr),
676 re.MULTILINE)
677 self.assertRegex(debugout, to_addr)
678
679 def testSendMessageMultipleResentRaises(self):
680 m = email.mime.text.MIMEText('A test message')
681 m['From'] = '[email protected]'
682 m['To'] = 'John'
683 m['CC'] = 'Sally, Fred'
684 m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <[email protected]>'
685 m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000'
686 m['Resent-From'] = '[email protected]'
687 m['Resent-To'] = 'Martha <[email protected]>, Jeff'
688 m['Resent-Bcc'] = '[email protected]'
689 m['Resent-Date'] = 'Thu, 2 Jan 1970 17:42:00 +0000'
690 m['Resent-To'] = '[email protected]'
691 m['Resent-From'] = 'Martha <[email protected]>, Jeff'
692 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
693 timeout=support.LOOPBACK_TIMEOUT)
694 self.addCleanup(smtp.close)
695 with self.assertRaises(ValueError):
696 smtp.send_message(m)
697 smtp.close()
698
699 class NonConnectingTests(unittest.TestCase):
700
701 def testNotConnected(self):
702 # Test various operations on an unconnected SMTP object that
703 # should raise exceptions (at present the attempt in SMTP.send
704 # to reference the nonexistent 'sock' attribute of the SMTP object
705 # causes an AttributeError)
706 smtp = smtplib.SMTP()
707 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
708 self.assertRaises(smtplib.SMTPServerDisconnected,
709 smtp.send, 'test msg')
710
711 def testNonnumericPort(self):
712 # check that non-numeric port raises OSError
713 self.assertRaises(OSError, smtplib.SMTP,
714 "localhost", "bogus")
715 self.assertRaises(OSError, smtplib.SMTP,
716 "localhost:bogus")
717
718 def testSockAttributeExists(self):
719 # check that sock attribute is present outside of a connect() call
720 # (regression test, the previous behavior raised an
721 # AttributeError: 'SMTP' object has no attribute 'sock')
722 with smtplib.SMTP() as smtp:
723 self.assertIsNone(smtp.sock)
724
725
726 class DefaultArgumentsTests(unittest.TestCase):
727
728 def setUp(self):
729 self.msg = EmailMessage()
730 self.msg['From'] = 'Páolo <fő[email protected]>'
731 self.smtp = smtplib.SMTP()
732 self.smtp.ehlo = Mock(return_value=(200, 'OK'))
733 self.smtp.has_extn, self.smtp.sendmail = Mock(), Mock()
734
735 def testSendMessage(self):
736 expected_mail_options = ('SMTPUTF8', 'BODY=8BITMIME')
737 self.smtp.send_message(self.msg)
738 self.smtp.send_message(self.msg)
739 self.assertEqual(self.smtp.sendmail.call_args_list[0][0][3],
740 expected_mail_options)
741 self.assertEqual(self.smtp.sendmail.call_args_list[1][0][3],
742 expected_mail_options)
743
744 def testSendMessageWithMailOptions(self):
745 mail_options = ['STARTTLS']
746 expected_mail_options = ('STARTTLS', 'SMTPUTF8', 'BODY=8BITMIME')
747 self.smtp.send_message(self.msg, None, None, mail_options)
748 self.assertEqual(mail_options, ['STARTTLS'])
749 self.assertEqual(self.smtp.sendmail.call_args_list[0][0][3],
750 expected_mail_options)
751
752
753 # test response of client to a non-successful HELO message
754 class BadHELOServerTests(unittest.TestCase):
755
756 def setUp(self):
757 smtplib.socket = mock_socket
758 mock_socket.reply_with(b"199 no hello for you!")
759 self.old_stdout = sys.stdout
760 self.output = io.StringIO()
761 sys.stdout = self.output
762 self.port = 25
763
764 def tearDown(self):
765 smtplib.socket = socket
766 sys.stdout = self.old_stdout
767
768 def testFailingHELO(self):
769 self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP,
770 HOST, self.port, 'localhost', 3)
771
772
773 class TooLongLineTests(unittest.TestCase):
774 respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n'
775
776 def setUp(self):
777 self.thread_key = threading_helper.threading_setup()
778 self.old_stdout = sys.stdout
779 self.output = io.StringIO()
780 sys.stdout = self.output
781
782 self.evt = threading.Event()
783 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
784 self.sock.settimeout(15)
785 self.port = socket_helper.bind_port(self.sock)
786 servargs = (self.evt, self.respdata, self.sock)
787 self.thread = threading.Thread(target=server, args=servargs)
788 self.thread.start()
789 self.evt.wait()
790 self.evt.clear()
791
792 def tearDown(self):
793 self.evt.wait()
794 sys.stdout = self.old_stdout
795 threading_helper.join_thread(self.thread)
796 del self.thread
797 self.doCleanups()
798 threading_helper.threading_cleanup(*self.thread_key)
799
800 def testLineTooLong(self):
801 self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
802 HOST, self.port, 'localhost', 3)
803
804
805 sim_users = {'[email protected]':'John A',
806 '[email protected]':'Sally B',
807 '[email protected]':'Ruth C',
808 }
809
810 sim_auth = ('[email protected]', 'somepassword')
811 sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn'
812 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=')
813 sim_lists = {'list-1':['[email protected]','[email protected]'],
814 'list-2':['[email protected]',],
815 }
816
817 # Simulated SMTP channel & server
818 class ResponseException(Exception): pass
819 class SimSMTPChannel(smtpd.SMTPChannel):
820
821 quit_response = None
822 mail_response = None
823 rcpt_response = None
824 data_response = None
825 rcpt_count = 0
826 rset_count = 0
827 disconnect = 0
828 AUTH = 99 # Add protocol state to enable auth testing.
829 authenticated_user = None
830
831 def __init__(self, extra_features, *args, **kw):
832 self._extrafeatures = ''.join(
833 [ "250-{0}\r\n".format(x) for x in extra_features ])
834 self.all_received_lines = []
835 super(SimSMTPChannel, self).__init__(*args, **kw)
836
837 # AUTH related stuff. It would be nice if support for this were in smtpd.
838 def found_terminator(self):
839 if self.smtp_state == self.AUTH:
840 line = self._emptystring.join(self.received_lines)
841 print('Data:', repr(line), file=smtpd.DEBUGSTREAM)
842 self.received_lines = []
843 try:
844 self.auth_object(line)
845 except ResponseException as e:
846 self.smtp_state = self.COMMAND
847 self.push('%s %s' % (e.smtp_code, e.smtp_error))
848 return
849 self.all_received_lines.append(self.received_lines)
850 super().found_terminator()
851
852
853 def smtp_AUTH(self, arg):
854 if not self.seen_greeting:
855 self.push('503 Error: send EHLO first')
856 return
857 if not self.extended_smtp or 'AUTH' not in self._extrafeatures:
858 self.push('500 Error: command "AUTH" not recognized')
859 return
860 if self.authenticated_user is not None:
861 self.push(
862 '503 Bad sequence of commands: already authenticated')
863 return
864 args = arg.split()
865 if len(args) not in [1, 2]:
866 self.push('501 Syntax: AUTH <mechanism> [initial-response]')
867 return
868 auth_object_name = '_auth_%s' % args[0].lower().replace('-', '_')
869 try:
870 self.auth_object = getattr(self, auth_object_name)
871 except AttributeError:
872 self.push('504 Command parameter not implemented: unsupported '
873 ' authentication mechanism {!r}'.format(auth_object_name))
874 return
875 self.smtp_state = self.AUTH
876 self.auth_object(args[1] if len(args) == 2 else None)
877
878 def _authenticated(self, user, valid):
879 if valid:
880 self.authenticated_user = user
881 self.push('235 Authentication Succeeded')
882 else:
883 self.push('535 Authentication credentials invalid')
884 self.smtp_state = self.COMMAND
885
886 def _decode_base64(self, string):
887 return base64.decodebytes(string.encode('ascii')).decode('utf-8')
888
889 def _auth_plain(self, arg=None):
890 if arg is None:
891 self.push('334 ')
892 else:
893 logpass = self._decode_base64(arg)
894 try:
895 *_, user, password = logpass.split('\0')
896 except ValueError as e:
897 self.push('535 Splitting response {!r} into user and password'
898 ' failed: {}'.format(logpass, e))
899 return
900 self._authenticated(user, password == sim_auth[1])
901
902 def _auth_login(self, arg=None):
903 if arg is None:
904 # base64 encoded 'Username:'
905 self.push('334 VXNlcm5hbWU6')
906 elif not hasattr(self, '_auth_login_user'):
907 self._auth_login_user = self._decode_base64(arg)
908 # base64 encoded 'Password:'
909 self.push('334 UGFzc3dvcmQ6')
910 else:
911 password = self._decode_base64(arg)
912 self._authenticated(self._auth_login_user, password == sim_auth[1])
913 del self._auth_login_user
914
915 def _auth_buggy(self, arg=None):
916 # This AUTH mechanism will 'trap' client in a neverending 334
917 # base64 encoded 'BuGgYbUgGy'
918 self.push('334 QnVHZ1liVWdHeQ==')
919
920 def _auth_cram_md5(self, arg=None):
921 if arg is None:
922 self.push('334 {}'.format(sim_cram_md5_challenge))
923 else:
924 logpass = self._decode_base64(arg)
925 try:
926 user, hashed_pass = logpass.split()
927 except ValueError as e:
928 self.push('535 Splitting response {!r} into user and password '
929 'failed: {}'.format(logpass, e))
930 return
931 pwd = sim_auth[1].encode('ascii')
932 msg = self._decode_base64(sim_cram_md5_challenge).encode('ascii')
933 try:
934 valid_hashed_pass = hmac.HMAC(pwd, msg, 'md5').hexdigest()
935 except ValueError:
936 self.push('504 CRAM-MD5 is not supported')
937 return
938 self._authenticated(user, hashed_pass == valid_hashed_pass)
939 # end AUTH related stuff.
940
941 def smtp_EHLO(self, arg):
942 resp = ('250-testhost\r\n'
943 '250-EXPN\r\n'
944 '250-SIZE 20000000\r\n'
945 '250-STARTTLS\r\n'
946 '250-DELIVERBY\r\n')
947 resp = resp + self._extrafeatures + '250 HELP'
948 self.push(resp)
949 self.seen_greeting = arg
950 self.extended_smtp = True
951
952 def smtp_VRFY(self, arg):
953 # For max compatibility smtplib should be sending the raw address.
954 if arg in sim_users:
955 self.push('250 %s %s' % (sim_users[arg], smtplib.quoteaddr(arg)))
956 else:
957 self.push('550 No such user: %s' % arg)
958
959 def smtp_EXPN(self, arg):
960 list_name = arg.lower()
961 if list_name in sim_lists:
962 user_list = sim_lists[list_name]
963 for n, user_email in enumerate(user_list):
964 quoted_addr = smtplib.quoteaddr(user_email)
965 if n < len(user_list) - 1:
966 self.push('250-%s %s' % (sim_users[user_email], quoted_addr))
967 else:
968 self.push('250 %s %s' % (sim_users[user_email], quoted_addr))
969 else:
970 self.push('550 No access for you!')
971
972 def smtp_QUIT(self, arg):
973 if self.quit_response is None:
974 super(SimSMTPChannel, self).smtp_QUIT(arg)
975 else:
976 self.push(self.quit_response)
977 self.close_when_done()
978
979 def smtp_MAIL(self, arg):
980 if self.mail_response is None:
981 super().smtp_MAIL(arg)
982 else:
983 self.push(self.mail_response)
984 if self.disconnect:
985 self.close_when_done()
986
987 def smtp_RCPT(self, arg):
988 if self.rcpt_response is None:
989 super().smtp_RCPT(arg)
990 return
991 self.rcpt_count += 1
992 self.push(self.rcpt_response[self.rcpt_count-1])
993
994 def smtp_RSET(self, arg):
995 self.rset_count += 1
996 super().smtp_RSET(arg)
997
998 def smtp_DATA(self, arg):
999 if self.data_response is None:
1000 super().smtp_DATA(arg)
1001 else:
1002 self.push(self.data_response)
1003
1004 def handle_error(self):
1005 raise
1006
1007
1008 class SimSMTPServer(smtpd.SMTPServer):
1009
1010 channel_class = SimSMTPChannel
1011
1012 def __init__(self, *args, **kw):
1013 self._extra_features = []
1014 self._addresses = {}
1015 smtpd.SMTPServer.__init__(self, *args, **kw)
1016
1017 def handle_accepted(self, conn, addr):
1018 self._SMTPchannel = self.channel_class(
1019 self._extra_features, self, conn, addr,
1020 decode_data=self._decode_data)
1021
1022 def process_message(self, peer, mailfrom, rcpttos, data):
1023 self._addresses['from'] = mailfrom
1024 self._addresses['tos'] = rcpttos
1025
1026 def add_feature(self, feature):
1027 self._extra_features.append(feature)
1028
1029 def handle_error(self):
1030 raise
1031
1032
1033 # Test various SMTP & ESMTP commands/behaviors that require a simulated server
1034 # (i.e., something with more features than DebuggingServer)
1035 class SMTPSimTests(unittest.TestCase):
1036
1037 def setUp(self):
1038 self.thread_key = threading_helper.threading_setup()
1039 self.real_getfqdn = socket.getfqdn
1040 socket.getfqdn = mock_socket.getfqdn
1041 self.serv_evt = threading.Event()
1042 self.client_evt = threading.Event()
1043 # Pick a random unused port by passing 0 for the port number
1044 self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1), decode_data=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 threading_helper.join_thread(self.thread)
1062 del self.thread
1063 self.doCleanups()
1064 threading_helper.threading_cleanup(*self.thread_key)
1065
1066 def testBasic(self):
1067 # smoke test
1068 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1069 timeout=support.LOOPBACK_TIMEOUT)
1070 smtp.quit()
1071
1072 def testEHLO(self):
1073 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1074 timeout=support.LOOPBACK_TIMEOUT)
1075
1076 # no features should be present before the EHLO
1077 self.assertEqual(smtp.esmtp_features, {})
1078
1079 # features expected from the test server
1080 expected_features = {'expn':'',
1081 'size': '20000000',
1082 'starttls': '',
1083 'deliverby': '',
1084 'help': '',
1085 }
1086
1087 smtp.ehlo()
1088 self.assertEqual(smtp.esmtp_features, expected_features)
1089 for k in expected_features:
1090 self.assertTrue(smtp.has_extn(k))
1091 self.assertFalse(smtp.has_extn('unsupported-feature'))
1092 smtp.quit()
1093
1094 def testVRFY(self):
1095 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1096 timeout=support.LOOPBACK_TIMEOUT)
1097
1098 for addr_spec, name in sim_users.items():
1099 expected_known = (250, bytes('%s %s' %
1100 (name, smtplib.quoteaddr(addr_spec)),
1101 "ascii"))
1102 self.assertEqual(smtp.vrfy(addr_spec), expected_known)
1103
1104 u = '[email protected]'
1105 expected_unknown = (550, ('No such user: %s' % u).encode('ascii'))
1106 self.assertEqual(smtp.vrfy(u), expected_unknown)
1107 smtp.quit()
1108
1109 def testEXPN(self):
1110 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1111 timeout=support.LOOPBACK_TIMEOUT)
1112
1113 for listname, members in sim_lists.items():
1114 users = []
1115 for m in members:
1116 users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m)))
1117 expected_known = (250, bytes('\n'.join(users), "ascii"))
1118 self.assertEqual(smtp.expn(listname), expected_known)
1119
1120 u = 'PSU-Members-List'
1121 expected_unknown = (550, b'No access for you!')
1122 self.assertEqual(smtp.expn(u), expected_unknown)
1123 smtp.quit()
1124
1125 def testAUTH_PLAIN(self):
1126 self.serv.add_feature("AUTH PLAIN")
1127 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1128 timeout=support.LOOPBACK_TIMEOUT)
1129 resp = smtp.login(sim_auth[0], sim_auth[1])
1130 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1131 smtp.close()
1132
1133 def testAUTH_LOGIN(self):
1134 self.serv.add_feature("AUTH LOGIN")
1135 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1136 timeout=support.LOOPBACK_TIMEOUT)
1137 resp = smtp.login(sim_auth[0], sim_auth[1])
1138 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1139 smtp.close()
1140
1141 def testAUTH_LOGIN_initial_response_ok(self):
1142 self.serv.add_feature("AUTH LOGIN")
1143 with smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1144 timeout=support.LOOPBACK_TIMEOUT) as smtp:
1145 smtp.user, smtp.password = sim_auth
1146 smtp.ehlo("test_auth_login")
1147 resp = smtp.auth("LOGIN", smtp.auth_login, initial_response_ok=True)
1148 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1149
1150 def testAUTH_LOGIN_initial_response_notok(self):
1151 self.serv.add_feature("AUTH LOGIN")
1152 with smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1153 timeout=support.LOOPBACK_TIMEOUT) as smtp:
1154 smtp.user, smtp.password = sim_auth
1155 smtp.ehlo("test_auth_login")
1156 resp = smtp.auth("LOGIN", smtp.auth_login, initial_response_ok=False)
1157 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1158
1159 def testAUTH_BUGGY(self):
1160 self.serv.add_feature("AUTH BUGGY")
1161
1162 def auth_buggy(challenge=None):
1163 self.assertEqual(b"BuGgYbUgGy", challenge)
1164 return "\0"
1165
1166 smtp = smtplib.SMTP(
1167 HOST, self.port, local_hostname='localhost',
1168 timeout=support.LOOPBACK_TIMEOUT
1169 )
1170 try:
1171 smtp.user, smtp.password = sim_auth
1172 smtp.ehlo("test_auth_buggy")
1173 expect = r"^Server AUTH mechanism infinite loop.*"
1174 with self.assertRaisesRegex(smtplib.SMTPException, expect) as cm:
1175 smtp.auth("BUGGY", auth_buggy, initial_response_ok=False)
1176 finally:
1177 smtp.close()
1178
1179 @hashlib_helper.requires_hashdigest('md5', openssl=True)
1180 def testAUTH_CRAM_MD5(self):
1181 self.serv.add_feature("AUTH CRAM-MD5")
1182 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1183 timeout=support.LOOPBACK_TIMEOUT)
1184 resp = smtp.login(sim_auth[0], sim_auth[1])
1185 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1186 smtp.close()
1187
1188 @mock.patch("hmac.HMAC")
1189 @mock.patch("smtplib._have_cram_md5_support", False)
1190 def testAUTH_CRAM_MD5_blocked(self, hmac_constructor):
1191 # CRAM-MD5 is the only "known" method by the server,
1192 # but it is not supported by the client. In particular,
1193 # no challenge will ever be sent.
1194 self.serv.add_feature("AUTH CRAM-MD5")
1195 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1196 timeout=support.LOOPBACK_TIMEOUT)
1197 self.addCleanup(smtp.close)
1198 msg = re.escape("No suitable authentication method found.")
1199 with self.assertRaisesRegex(smtplib.SMTPException, msg):
1200 smtp.login(sim_auth[0], sim_auth[1])
1201 hmac_constructor.assert_not_called() # call has been bypassed
1202
1203 @mock.patch("smtplib._have_cram_md5_support", False)
1204 def testAUTH_CRAM_MD5_blocked_and_fallback(self):
1205 # Test that PLAIN is tried after CRAM-MD5 failed
1206 self.serv.add_feature("AUTH CRAM-MD5 PLAIN")
1207 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1208 timeout=support.LOOPBACK_TIMEOUT)
1209 self.addCleanup(smtp.close)
1210 with (
1211 mock.patch.object(smtp, "auth_cram_md5") as smtp_auth_cram_md5,
1212 mock.patch.object(
1213 smtp, "auth_plain", wraps=smtp.auth_plain
1214 ) as smtp_auth_plain
1215 ):
1216 resp = smtp.login(sim_auth[0], sim_auth[1])
1217 smtp_auth_plain.assert_called_once()
1218 smtp_auth_cram_md5.assert_not_called() # no call to HMAC constructor
1219 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1220
1221 @hashlib_helper.requires_hashdigest('md5', openssl=True)
1222 def testAUTH_multiple(self):
1223 # Test that multiple authentication methods are tried.
1224 self.serv.add_feature("AUTH BOGUS PLAIN LOGIN CRAM-MD5")
1225 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1226 timeout=support.LOOPBACK_TIMEOUT)
1227 resp = smtp.login(sim_auth[0], sim_auth[1])
1228 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1229 smtp.close()
1230
1231 def test_auth_function(self):
1232 supported = {'PLAIN', 'LOGIN'}
1233 try:
1234 hashlib.md5()
1235 except ValueError:
1236 pass
1237 else:
1238 supported.add('CRAM-MD5')
1239 for mechanism in supported:
1240 self.serv.add_feature("AUTH {}".format(mechanism))
1241 for mechanism in supported:
1242 with self.subTest(mechanism=mechanism):
1243 smtp = smtplib.SMTP(HOST, self.port,
1244 local_hostname='localhost',
1245 timeout=support.LOOPBACK_TIMEOUT)
1246 smtp.ehlo('foo')
1247 smtp.user, smtp.password = sim_auth[0], sim_auth[1]
1248 method = 'auth_' + mechanism.lower().replace('-', '_')
1249 resp = smtp.auth(mechanism, getattr(smtp, method))
1250 self.assertEqual(resp, (235, b'Authentication Succeeded'))
1251 smtp.close()
1252
1253 def test_quit_resets_greeting(self):
1254 smtp = smtplib.SMTP(HOST, self.port,
1255 local_hostname='localhost',
1256 timeout=support.LOOPBACK_TIMEOUT)
1257 code, message = smtp.ehlo()
1258 self.assertEqual(code, 250)
1259 self.assertIn('size', smtp.esmtp_features)
1260 smtp.quit()
1261 self.assertNotIn('size', smtp.esmtp_features)
1262 smtp.connect(HOST, self.port)
1263 self.assertNotIn('size', smtp.esmtp_features)
1264 smtp.ehlo_or_helo_if_needed()
1265 self.assertIn('size', smtp.esmtp_features)
1266 smtp.quit()
1267
1268 def test_with_statement(self):
1269 with smtplib.SMTP(HOST, self.port) as smtp:
1270 code, message = smtp.noop()
1271 self.assertEqual(code, 250)
1272 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo')
1273 with smtplib.SMTP(HOST, self.port) as smtp:
1274 smtp.close()
1275 self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo')
1276
1277 def test_with_statement_QUIT_failure(self):
1278 with self.assertRaises(smtplib.SMTPResponseException) as error:
1279 with smtplib.SMTP(HOST, self.port) as smtp:
1280 smtp.noop()
1281 self.serv._SMTPchannel.quit_response = '421 QUIT FAILED'
1282 self.assertEqual(error.exception.smtp_code, 421)
1283 self.assertEqual(error.exception.smtp_error, b'QUIT FAILED')
1284
1285 #TODO: add tests for correct AUTH method fallback now that the
1286 #test infrastructure can support it.
1287
1288 # Issue 17498: make sure _rset does not raise SMTPServerDisconnected exception
1289 def test__rest_from_mail_cmd(self):
1290 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1291 timeout=support.LOOPBACK_TIMEOUT)
1292 smtp.noop()
1293 self.serv._SMTPchannel.mail_response = '451 Requested action aborted'
1294 self.serv._SMTPchannel.disconnect = True
1295 with self.assertRaises(smtplib.SMTPSenderRefused):
1296 smtp.sendmail('John', 'Sally', 'test message')
1297 self.assertIsNone(smtp.sock)
1298
1299 # Issue 5713: make sure close, not rset, is called if we get a 421 error
1300 def test_421_from_mail_cmd(self):
1301 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1302 timeout=support.LOOPBACK_TIMEOUT)
1303 smtp.noop()
1304 self.serv._SMTPchannel.mail_response = '421 closing connection'
1305 with self.assertRaises(smtplib.SMTPSenderRefused):
1306 smtp.sendmail('John', 'Sally', 'test message')
1307 self.assertIsNone(smtp.sock)
1308 self.assertEqual(self.serv._SMTPchannel.rset_count, 0)
1309
1310 def test_421_from_rcpt_cmd(self):
1311 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1312 timeout=support.LOOPBACK_TIMEOUT)
1313 smtp.noop()
1314 self.serv._SMTPchannel.rcpt_response = ['250 accepted', '421 closing']
1315 with self.assertRaises(smtplib.SMTPRecipientsRefused) as r:
1316 smtp.sendmail('John', ['Sally', 'Frank', 'George'], 'test message')
1317 self.assertIsNone(smtp.sock)
1318 self.assertEqual(self.serv._SMTPchannel.rset_count, 0)
1319 self.assertDictEqual(r.exception.args[0], {'Frank': (421, b'closing')})
1320
1321 def test_421_from_data_cmd(self):
1322 class MySimSMTPChannel(SimSMTPChannel):
1323 def found_terminator(self):
1324 if self.smtp_state == self.DATA:
1325 self.push('421 closing')
1326 else:
1327 super().found_terminator()
1328 self.serv.channel_class = MySimSMTPChannel
1329 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1330 timeout=support.LOOPBACK_TIMEOUT)
1331 smtp.noop()
1332 with self.assertRaises(smtplib.SMTPDataError):
1333 smtp.sendmail('[email protected]', ['[email protected]'], 'test message')
1334 self.assertIsNone(smtp.sock)
1335 self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0)
1336
1337 def test_smtputf8_NotSupportedError_if_no_server_support(self):
1338 smtp = smtplib.SMTP(
1339 HOST, self.port, local_hostname='localhost',
1340 timeout=support.LOOPBACK_TIMEOUT)
1341 self.addCleanup(smtp.close)
1342 smtp.ehlo()
1343 self.assertTrue(smtp.does_esmtp)
1344 self.assertFalse(smtp.has_extn('smtputf8'))
1345 self.assertRaises(
1346 smtplib.SMTPNotSupportedError,
1347 smtp.sendmail,
1348 'John', 'Sally', '', mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
1349 self.assertRaises(
1350 smtplib.SMTPNotSupportedError,
1351 smtp.mail, 'John', options=['BODY=8BITMIME', 'SMTPUTF8'])
1352
1353 def test_send_unicode_without_SMTPUTF8(self):
1354 smtp = smtplib.SMTP(
1355 HOST, self.port, local_hostname='localhost',
1356 timeout=support.LOOPBACK_TIMEOUT)
1357 self.addCleanup(smtp.close)
1358 self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '')
1359 self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice')
1360
1361 def test_send_message_error_on_non_ascii_addrs_if_no_smtputf8(self):
1362 # This test is located here and not in the SMTPUTF8SimTests
1363 # class because it needs a "regular" SMTP server to work
1364 msg = EmailMessage()
1365 msg['From'] = "Páolo <fő[email protected]>"
1366 msg['To'] = 'Dinsdale'
1367 msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
1368 smtp = smtplib.SMTP(
1369 HOST, self.port, local_hostname='localhost',
1370 timeout=support.LOOPBACK_TIMEOUT)
1371 self.addCleanup(smtp.close)
1372 with self.assertRaises(smtplib.SMTPNotSupportedError):
1373 smtp.send_message(msg)
1374
1375 def test_name_field_not_included_in_envelop_addresses(self):
1376 smtp = smtplib.SMTP(
1377 HOST, self.port, local_hostname='localhost',
1378 timeout=support.LOOPBACK_TIMEOUT)
1379 self.addCleanup(smtp.close)
1380
1381 message = EmailMessage()
1382 message['From'] = email.utils.formataddr(('Michaël', '[email protected]'))
1383 message['To'] = email.utils.formataddr(('René', '[email protected]'))
1384
1385 self.assertDictEqual(smtp.send_message(message), {})
1386
1387 self.assertEqual(self.serv._addresses['from'], '[email protected]')
1388 self.assertEqual(self.serv._addresses['tos'], ['[email protected]'])
1389
1390 def test_lowercase_mail_from_rcpt_to(self):
1391 m = 'A test message'
1392 smtp = smtplib.SMTP(
1393 HOST, self.port, local_hostname='localhost',
1394 timeout=support.LOOPBACK_TIMEOUT)
1395 self.addCleanup(smtp.close)
1396
1397 smtp.sendmail('John', 'Sally', m)
1398
1399 self.assertIn(['mail from:<John> size=14'], self.serv._SMTPchannel.all_received_lines)
1400 self.assertIn(['rcpt to:<Sally>'], self.serv._SMTPchannel.all_received_lines)
1401
1402
1403 class SimSMTPUTF8Server(SimSMTPServer):
1404
1405 def __init__(self, *args, **kw):
1406 # The base SMTP server turns these on automatically, but our test
1407 # server is set up to munge the EHLO response, so we need to provide
1408 # them as well. And yes, the call is to SMTPServer not SimSMTPServer.
1409 self._extra_features = ['SMTPUTF8', '8BITMIME']
1410 smtpd.SMTPServer.__init__(self, *args, **kw)
1411
1412 def handle_accepted(self, conn, addr):
1413 self._SMTPchannel = self.channel_class(
1414 self._extra_features, self, conn, addr,
1415 decode_data=self._decode_data,
1416 enable_SMTPUTF8=self.enable_SMTPUTF8,
1417 )
1418
1419 def process_message(self, peer, mailfrom, rcpttos, data, mail_options=None,
1420 rcpt_options=None):
1421 self.last_peer = peer
1422 self.last_mailfrom = mailfrom
1423 self.last_rcpttos = rcpttos
1424 self.last_message = data
1425 self.last_mail_options = mail_options
1426 self.last_rcpt_options = rcpt_options
1427
1428
1429 class SMTPUTF8SimTests(unittest.TestCase):
1430
1431 maxDiff = None
1432
1433 def setUp(self):
1434 self.thread_key = threading_helper.threading_setup()
1435 self.real_getfqdn = socket.getfqdn
1436 socket.getfqdn = mock_socket.getfqdn
1437 self.serv_evt = threading.Event()
1438 self.client_evt = threading.Event()
1439 # Pick a random unused port by passing 0 for the port number
1440 self.serv = SimSMTPUTF8Server((HOST, 0), ('nowhere', -1),
1441 decode_data=False,
1442 enable_SMTPUTF8=True)
1443 # Keep a note of what port was assigned
1444 self.port = self.serv.socket.getsockname()[1]
1445 serv_args = (self.serv, self.serv_evt, self.client_evt)
1446 self.thread = threading.Thread(target=debugging_server, args=serv_args)
1447 self.thread.start()
1448
1449 # wait until server thread has assigned a port number
1450 self.serv_evt.wait()
1451 self.serv_evt.clear()
1452
1453 def tearDown(self):
1454 socket.getfqdn = self.real_getfqdn
1455 # indicate that the client is finished
1456 self.client_evt.set()
1457 # wait for the server thread to terminate
1458 self.serv_evt.wait()
1459 threading_helper.join_thread(self.thread)
1460 del self.thread
1461 self.doCleanups()
1462 threading_helper.threading_cleanup(*self.thread_key)
1463
1464 def test_test_server_supports_extensions(self):
1465 smtp = smtplib.SMTP(
1466 HOST, self.port, local_hostname='localhost',
1467 timeout=support.LOOPBACK_TIMEOUT)
1468 self.addCleanup(smtp.close)
1469 smtp.ehlo()
1470 self.assertTrue(smtp.does_esmtp)
1471 self.assertTrue(smtp.has_extn('smtputf8'))
1472
1473 def test_send_unicode_with_SMTPUTF8_via_sendmail(self):
1474 m = '¡a test message containing unicode!'.encode('utf-8')
1475 smtp = smtplib.SMTP(
1476 HOST, self.port, local_hostname='localhost',
1477 timeout=support.LOOPBACK_TIMEOUT)
1478 self.addCleanup(smtp.close)
1479 smtp.sendmail('Jőhn', 'Sálly', m,
1480 mail_options=['BODY=8BITMIME', 'SMTPUTF8'])
1481 self.assertEqual(self.serv.last_mailfrom, 'Jőhn')
1482 self.assertEqual(self.serv.last_rcpttos, ['Sálly'])
1483 self.assertEqual(self.serv.last_message, m)
1484 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1485 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1486 self.assertEqual(self.serv.last_rcpt_options, [])
1487
1488 def test_send_unicode_with_SMTPUTF8_via_low_level_API(self):
1489 m = '¡a test message containing unicode!'.encode('utf-8')
1490 smtp = smtplib.SMTP(
1491 HOST, self.port, local_hostname='localhost',
1492 timeout=support.LOOPBACK_TIMEOUT)
1493 self.addCleanup(smtp.close)
1494 smtp.ehlo()
1495 self.assertEqual(
1496 smtp.mail('Jő', options=['BODY=8BITMIME', 'SMTPUTF8']),
1497 (250, b'OK'))
1498 self.assertEqual(smtp.rcpt('János'), (250, b'OK'))
1499 self.assertEqual(smtp.data(m), (250, b'OK'))
1500 self.assertEqual(self.serv.last_mailfrom, 'Jő')
1501 self.assertEqual(self.serv.last_rcpttos, ['János'])
1502 self.assertEqual(self.serv.last_message, m)
1503 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1504 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1505 self.assertEqual(self.serv.last_rcpt_options, [])
1506
1507 def test_send_message_uses_smtputf8_if_addrs_non_ascii(self):
1508 msg = EmailMessage()
1509 msg['From'] = "Páolo <fő[email protected]>"
1510 msg['To'] = 'Dinsdale'
1511 msg['Subject'] = 'Nudge nudge, wink, wink \u1F609'
1512 # XXX I don't know why I need two \n's here, but this is an existing
1513 # bug (if it is one) and not a problem with the new functionality.
1514 msg.set_content("oh là là, know what I mean, know what I mean?\n\n")
1515 # XXX smtpd converts received /r/n to /n, so we can't easily test that
1516 # we are successfully sending /r/n :(.
1517 expected = textwrap.dedent("""\
1518 From: Páolo <fő[email protected]>
1519 To: Dinsdale
1520 Subject: Nudge nudge, wink, wink \u1F609
1521 Content-Type: text/plain; charset="utf-8"
1522 Content-Transfer-Encoding: 8bit
1523 MIME-Version: 1.0
1524
1525 oh là là, know what I mean, know what I mean?
1526 """)
1527 smtp = smtplib.SMTP(
1528 HOST, self.port, local_hostname='localhost',
1529 timeout=support.LOOPBACK_TIMEOUT)
1530 self.addCleanup(smtp.close)
1531 self.assertEqual(smtp.send_message(msg), {})
1532 self.assertEqual(self.serv.last_mailfrom, 'fő[email protected]')
1533 self.assertEqual(self.serv.last_rcpttos, ['Dinsdale'])
1534 self.assertEqual(self.serv.last_message.decode(), expected)
1535 self.assertIn('BODY=8BITMIME', self.serv.last_mail_options)
1536 self.assertIn('SMTPUTF8', self.serv.last_mail_options)
1537 self.assertEqual(self.serv.last_rcpt_options, [])
1538
1539
1540 EXPECTED_RESPONSE = encode_base64(b'\0psu\0doesnotexist', eol='')
1541
1542 class SimSMTPAUTHInitialResponseChannel(SimSMTPChannel):
1543 def smtp_AUTH(self, arg):
1544 # RFC 4954's AUTH command allows for an optional initial-response.
1545 # Not all AUTH methods support this; some require a challenge. AUTH
1546 # PLAIN does those, so test that here. See issue #15014.
1547 args = arg.split()
1548 if args[0].lower() == 'plain':
1549 if len(args) == 2:
1550 # AUTH PLAIN <initial-response> with the response base 64
1551 # encoded. Hard code the expected response for the test.
1552 if args[1] == EXPECTED_RESPONSE:
1553 self.push('235 Ok')
1554 return
1555 self.push('571 Bad authentication')
1556
1557 class SimSMTPAUTHInitialResponseServer(SimSMTPServer):
1558 channel_class = SimSMTPAUTHInitialResponseChannel
1559
1560
1561 class SMTPAUTHInitialResponseSimTests(unittest.TestCase):
1562 def setUp(self):
1563 self.thread_key = threading_helper.threading_setup()
1564 self.real_getfqdn = socket.getfqdn
1565 socket.getfqdn = mock_socket.getfqdn
1566 self.serv_evt = threading.Event()
1567 self.client_evt = threading.Event()
1568 # Pick a random unused port by passing 0 for the port number
1569 self.serv = SimSMTPAUTHInitialResponseServer(
1570 (HOST, 0), ('nowhere', -1), decode_data=True)
1571 # Keep a note of what port was assigned
1572 self.port = self.serv.socket.getsockname()[1]
1573 serv_args = (self.serv, self.serv_evt, self.client_evt)
1574 self.thread = threading.Thread(target=debugging_server, args=serv_args)
1575 self.thread.start()
1576
1577 # wait until server thread has assigned a port number
1578 self.serv_evt.wait()
1579 self.serv_evt.clear()
1580
1581 def tearDown(self):
1582 socket.getfqdn = self.real_getfqdn
1583 # indicate that the client is finished
1584 self.client_evt.set()
1585 # wait for the server thread to terminate
1586 self.serv_evt.wait()
1587 threading_helper.join_thread(self.thread)
1588 del self.thread
1589 self.doCleanups()
1590 threading_helper.threading_cleanup(*self.thread_key)
1591
1592 def testAUTH_PLAIN_initial_response_login(self):
1593 self.serv.add_feature('AUTH PLAIN')
1594 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1595 timeout=support.LOOPBACK_TIMEOUT)
1596 smtp.login('psu', 'doesnotexist')
1597 smtp.close()
1598
1599 def testAUTH_PLAIN_initial_response_auth(self):
1600 self.serv.add_feature('AUTH PLAIN')
1601 smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
1602 timeout=support.LOOPBACK_TIMEOUT)
1603 smtp.user = 'psu'
1604 smtp.password = 'doesnotexist'
1605 code, response = smtp.auth('plain', smtp.auth_plain)
1606 smtp.close()
1607 self.assertEqual(code, 235)
1608
1609
1610 if __name__ == '__main__':
1611 unittest.main()