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