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 | |
| 605 | |
Pablo Aguiar | d5fbe9b | 2018-09-08 00:04:48 +0200 | [diff] [blame] | 606 | class DefaultArgumentsTests(unittest.TestCase): |
| 607 | |
| 608 | def setUp(self): |
| 609 | self.msg = EmailMessage() |
| 610 | self.msg['From'] = 'Páolo <főo@bar.com>' |
| 611 | self.smtp = smtplib.SMTP() |
| 612 | self.smtp.ehlo = Mock(return_value=(200, 'OK')) |
| 613 | self.smtp.has_extn, self.smtp.sendmail = Mock(), Mock() |
| 614 | |
| 615 | def testSendMessage(self): |
| 616 | expected_mail_options = ('SMTPUTF8', 'BODY=8BITMIME') |
| 617 | self.smtp.send_message(self.msg) |
| 618 | self.smtp.send_message(self.msg) |
| 619 | self.assertEqual(self.smtp.sendmail.call_args_list[0][0][3], |
| 620 | expected_mail_options) |
| 621 | self.assertEqual(self.smtp.sendmail.call_args_list[1][0][3], |
| 622 | expected_mail_options) |
| 623 | |
| 624 | def testSendMessageWithMailOptions(self): |
| 625 | mail_options = ['STARTTLS'] |
| 626 | expected_mail_options = ('STARTTLS', 'SMTPUTF8', 'BODY=8BITMIME') |
| 627 | self.smtp.send_message(self.msg, None, None, mail_options) |
| 628 | self.assertEqual(mail_options, ['STARTTLS']) |
| 629 | self.assertEqual(self.smtp.sendmail.call_args_list[0][0][3], |
| 630 | expected_mail_options) |
| 631 | |
| 632 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 633 | # test response of client to a non-successful HELO message |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 634 | class BadHELOServerTests(unittest.TestCase): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 635 | |
| 636 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 637 | smtplib.socket = mock_socket |
| 638 | mock_socket.reply_with(b"199 no hello for you!") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 639 | self.old_stdout = sys.stdout |
| 640 | self.output = io.StringIO() |
| 641 | sys.stdout = self.output |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 642 | self.port = 25 |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 643 | |
| 644 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 645 | smtplib.socket = socket |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 646 | sys.stdout = self.old_stdout |
| 647 | |
| 648 | def testFailingHELO(self): |
| 649 | self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 650 | HOST, self.port, 'localhost', 3) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 651 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 652 | |
Georg Brandl | b38b5c4 | 2014-02-10 22:11:21 +0100 | [diff] [blame] | 653 | class TooLongLineTests(unittest.TestCase): |
| 654 | respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n' |
| 655 | |
| 656 | def setUp(self): |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 657 | self.thread_key = threading_setup() |
Georg Brandl | b38b5c4 | 2014-02-10 22:11:21 +0100 | [diff] [blame] | 658 | self.old_stdout = sys.stdout |
| 659 | self.output = io.StringIO() |
| 660 | sys.stdout = self.output |
| 661 | |
| 662 | self.evt = threading.Event() |
| 663 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 664 | self.sock.settimeout(15) |
| 665 | self.port = support.bind_port(self.sock) |
| 666 | servargs = (self.evt, self.respdata, self.sock) |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 667 | self.thread = threading.Thread(target=server, args=servargs) |
| 668 | self.thread.start() |
Georg Brandl | b38b5c4 | 2014-02-10 22:11:21 +0100 | [diff] [blame] | 669 | self.evt.wait() |
| 670 | self.evt.clear() |
| 671 | |
| 672 | def tearDown(self): |
| 673 | self.evt.wait() |
| 674 | sys.stdout = self.old_stdout |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 675 | join_thread(self.thread) |
| 676 | del self.thread |
| 677 | self.doCleanups() |
| 678 | threading_cleanup(*self.thread_key) |
Georg Brandl | b38b5c4 | 2014-02-10 22:11:21 +0100 | [diff] [blame] | 679 | |
| 680 | def testLineTooLong(self): |
| 681 | self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP, |
| 682 | HOST, self.port, 'localhost', 3) |
| 683 | |
| 684 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 685 | sim_users = {'Mr.A@somewhere.com':'John A', |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 686 | 'Ms.B@xn--fo-fka.com':'Sally B', |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 687 | 'Mrs.C@somewhereesle.com':'Ruth C', |
| 688 | } |
| 689 | |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 690 | sim_auth = ('Mr.A@somewhere.com', 'somepassword') |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 691 | sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn' |
| 692 | 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=') |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 693 | 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] | 694 | 'list-2':['Ms.B@xn--fo-fka.com',], |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | # Simulated SMTP channel & server |
R David Murray | b0deeb4 | 2015-11-08 01:03:52 -0500 | [diff] [blame] | 698 | class ResponseException(Exception): pass |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 699 | class SimSMTPChannel(smtpd.SMTPChannel): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 700 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 701 | quit_response = None |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 702 | mail_response = None |
| 703 | rcpt_response = None |
| 704 | data_response = None |
| 705 | rcpt_count = 0 |
| 706 | rset_count = 0 |
R David Murray | afb151a | 2014-04-14 18:21:38 -0400 | [diff] [blame] | 707 | disconnect = 0 |
R David Murray | b0deeb4 | 2015-11-08 01:03:52 -0500 | [diff] [blame] | 708 | AUTH = 99 # Add protocol state to enable auth testing. |
| 709 | authenticated_user = None |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 710 | |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 711 | def __init__(self, extra_features, *args, **kw): |
| 712 | self._extrafeatures = ''.join( |
| 713 | [ "250-{0}\r\n".format(x) for x in extra_features ]) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 714 | super(SimSMTPChannel, self).__init__(*args, **kw) |
| 715 | |
R David Murray | b0deeb4 | 2015-11-08 01:03:52 -0500 | [diff] [blame] | 716 | # AUTH related stuff. It would be nice if support for this were in smtpd. |
| 717 | def found_terminator(self): |
| 718 | if self.smtp_state == self.AUTH: |
| 719 | line = self._emptystring.join(self.received_lines) |
| 720 | print('Data:', repr(line), file=smtpd.DEBUGSTREAM) |
| 721 | self.received_lines = [] |
| 722 | try: |
| 723 | self.auth_object(line) |
| 724 | except ResponseException as e: |
| 725 | self.smtp_state = self.COMMAND |
| 726 | self.push('%s %s' % (e.smtp_code, e.smtp_error)) |
| 727 | return |
| 728 | super().found_terminator() |
| 729 | |
| 730 | |
| 731 | def smtp_AUTH(self, arg): |
| 732 | if not self.seen_greeting: |
| 733 | self.push('503 Error: send EHLO first') |
| 734 | return |
| 735 | if not self.extended_smtp or 'AUTH' not in self._extrafeatures: |
| 736 | self.push('500 Error: command "AUTH" not recognized') |
| 737 | return |
| 738 | if self.authenticated_user is not None: |
| 739 | self.push( |
| 740 | '503 Bad sequence of commands: already authenticated') |
| 741 | return |
| 742 | args = arg.split() |
| 743 | if len(args) not in [1, 2]: |
| 744 | self.push('501 Syntax: AUTH <mechanism> [initial-response]') |
| 745 | return |
| 746 | auth_object_name = '_auth_%s' % args[0].lower().replace('-', '_') |
| 747 | try: |
| 748 | self.auth_object = getattr(self, auth_object_name) |
| 749 | except AttributeError: |
| 750 | self.push('504 Command parameter not implemented: unsupported ' |
| 751 | ' authentication mechanism {!r}'.format(auth_object_name)) |
| 752 | return |
| 753 | self.smtp_state = self.AUTH |
| 754 | self.auth_object(args[1] if len(args) == 2 else None) |
| 755 | |
| 756 | def _authenticated(self, user, valid): |
| 757 | if valid: |
| 758 | self.authenticated_user = user |
| 759 | self.push('235 Authentication Succeeded') |
| 760 | else: |
| 761 | self.push('535 Authentication credentials invalid') |
| 762 | self.smtp_state = self.COMMAND |
| 763 | |
| 764 | def _decode_base64(self, string): |
| 765 | return base64.decodebytes(string.encode('ascii')).decode('utf-8') |
| 766 | |
| 767 | def _auth_plain(self, arg=None): |
| 768 | if arg is None: |
| 769 | self.push('334 ') |
| 770 | else: |
| 771 | logpass = self._decode_base64(arg) |
| 772 | try: |
| 773 | *_, user, password = logpass.split('\0') |
| 774 | except ValueError as e: |
| 775 | self.push('535 Splitting response {!r} into user and password' |
| 776 | ' failed: {}'.format(logpass, e)) |
| 777 | return |
| 778 | self._authenticated(user, password == sim_auth[1]) |
| 779 | |
| 780 | def _auth_login(self, arg=None): |
| 781 | if arg is None: |
| 782 | # base64 encoded 'Username:' |
| 783 | self.push('334 VXNlcm5hbWU6') |
| 784 | elif not hasattr(self, '_auth_login_user'): |
| 785 | self._auth_login_user = self._decode_base64(arg) |
| 786 | # base64 encoded 'Password:' |
| 787 | self.push('334 UGFzc3dvcmQ6') |
| 788 | else: |
| 789 | password = self._decode_base64(arg) |
| 790 | self._authenticated(self._auth_login_user, password == sim_auth[1]) |
| 791 | del self._auth_login_user |
| 792 | |
| 793 | def _auth_cram_md5(self, arg=None): |
| 794 | if arg is None: |
| 795 | self.push('334 {}'.format(sim_cram_md5_challenge)) |
| 796 | else: |
| 797 | logpass = self._decode_base64(arg) |
| 798 | try: |
| 799 | user, hashed_pass = logpass.split() |
| 800 | except ValueError as e: |
| 801 | self.push('535 Splitting response {!r} into user and password' |
| 802 | 'failed: {}'.format(logpass, e)) |
| 803 | return False |
| 804 | valid_hashed_pass = hmac.HMAC( |
| 805 | sim_auth[1].encode('ascii'), |
| 806 | self._decode_base64(sim_cram_md5_challenge).encode('ascii'), |
| 807 | 'md5').hexdigest() |
| 808 | self._authenticated(user, hashed_pass == valid_hashed_pass) |
| 809 | # end AUTH related stuff. |
| 810 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 811 | def smtp_EHLO(self, arg): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 812 | resp = ('250-testhost\r\n' |
| 813 | '250-EXPN\r\n' |
| 814 | '250-SIZE 20000000\r\n' |
| 815 | '250-STARTTLS\r\n' |
| 816 | '250-DELIVERBY\r\n') |
| 817 | resp = resp + self._extrafeatures + '250 HELP' |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 818 | self.push(resp) |
R David Murray | f1a40b4 | 2013-03-20 21:12:17 -0400 | [diff] [blame] | 819 | self.seen_greeting = arg |
| 820 | self.extended_smtp = True |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 821 | |
| 822 | def smtp_VRFY(self, arg): |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 823 | # For max compatibility smtplib should be sending the raw address. |
| 824 | if arg in sim_users: |
| 825 | self.push('250 %s %s' % (sim_users[arg], smtplib.quoteaddr(arg))) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 826 | else: |
| 827 | self.push('550 No such user: %s' % arg) |
| 828 | |
| 829 | def smtp_EXPN(self, arg): |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 830 | list_name = arg.lower() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 831 | if list_name in sim_lists: |
| 832 | user_list = sim_lists[list_name] |
| 833 | for n, user_email in enumerate(user_list): |
| 834 | quoted_addr = smtplib.quoteaddr(user_email) |
| 835 | if n < len(user_list) - 1: |
| 836 | self.push('250-%s %s' % (sim_users[user_email], quoted_addr)) |
| 837 | else: |
| 838 | self.push('250 %s %s' % (sim_users[user_email], quoted_addr)) |
| 839 | else: |
| 840 | self.push('550 No access for you!') |
| 841 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 842 | def smtp_QUIT(self, arg): |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 843 | if self.quit_response is None: |
| 844 | super(SimSMTPChannel, self).smtp_QUIT(arg) |
| 845 | else: |
| 846 | self.push(self.quit_response) |
| 847 | self.close_when_done() |
| 848 | |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 849 | def smtp_MAIL(self, arg): |
| 850 | if self.mail_response is None: |
| 851 | super().smtp_MAIL(arg) |
| 852 | else: |
| 853 | self.push(self.mail_response) |
R David Murray | afb151a | 2014-04-14 18:21:38 -0400 | [diff] [blame] | 854 | if self.disconnect: |
| 855 | self.close_when_done() |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 856 | |
| 857 | def smtp_RCPT(self, arg): |
| 858 | if self.rcpt_response is None: |
| 859 | super().smtp_RCPT(arg) |
| 860 | return |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 861 | self.rcpt_count += 1 |
R David Murray | 03b0116 | 2013-03-20 22:11:40 -0400 | [diff] [blame] | 862 | self.push(self.rcpt_response[self.rcpt_count-1]) |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 863 | |
| 864 | def smtp_RSET(self, arg): |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 865 | self.rset_count += 1 |
R David Murray | 03b0116 | 2013-03-20 22:11:40 -0400 | [diff] [blame] | 866 | super().smtp_RSET(arg) |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 867 | |
| 868 | def smtp_DATA(self, arg): |
| 869 | if self.data_response is None: |
| 870 | super().smtp_DATA(arg) |
| 871 | else: |
| 872 | self.push(self.data_response) |
| 873 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 874 | def handle_error(self): |
| 875 | raise |
| 876 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 877 | |
| 878 | class SimSMTPServer(smtpd.SMTPServer): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 879 | |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 880 | channel_class = SimSMTPChannel |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 881 | |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 882 | def __init__(self, *args, **kw): |
| 883 | self._extra_features = [] |
Stéphane Wirtel | 8d83e4b | 2018-01-31 01:02:51 +0100 | [diff] [blame] | 884 | self._addresses = {} |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 885 | smtpd.SMTPServer.__init__(self, *args, **kw) |
| 886 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 887 | def handle_accepted(self, conn, addr): |
R David Murray | f1a40b4 | 2013-03-20 21:12:17 -0400 | [diff] [blame] | 888 | self._SMTPchannel = self.channel_class( |
R David Murray | 1144da5 | 2014-06-11 12:27:40 -0400 | [diff] [blame] | 889 | self._extra_features, self, conn, addr, |
| 890 | decode_data=self._decode_data) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 891 | |
| 892 | def process_message(self, peer, mailfrom, rcpttos, data): |
Stéphane Wirtel | 8d83e4b | 2018-01-31 01:02:51 +0100 | [diff] [blame] | 893 | self._addresses['from'] = mailfrom |
| 894 | self._addresses['tos'] = rcpttos |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 895 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 896 | def add_feature(self, feature): |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 897 | self._extra_features.append(feature) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 898 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 899 | def handle_error(self): |
| 900 | raise |
| 901 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 902 | |
| 903 | # Test various SMTP & ESMTP commands/behaviors that require a simulated server |
| 904 | # (i.e., something with more features than DebuggingServer) |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 905 | class SMTPSimTests(unittest.TestCase): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 906 | |
| 907 | def setUp(self): |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 908 | self.thread_key = threading_setup() |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 909 | self.real_getfqdn = socket.getfqdn |
| 910 | socket.getfqdn = mock_socket.getfqdn |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 911 | self.serv_evt = threading.Event() |
| 912 | self.client_evt = threading.Event() |
Antoine Pitrou | 043bad0 | 2010-04-30 23:20:15 +0000 | [diff] [blame] | 913 | # 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] | 914 | self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1), decode_data=True) |
Antoine Pitrou | 043bad0 | 2010-04-30 23:20:15 +0000 | [diff] [blame] | 915 | # Keep a note of what port was assigned |
| 916 | self.port = self.serv.socket.getsockname()[1] |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 917 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 918 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 919 | self.thread.start() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 920 | |
| 921 | # wait until server thread has assigned a port number |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 922 | self.serv_evt.wait() |
| 923 | self.serv_evt.clear() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 924 | |
| 925 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 926 | socket.getfqdn = self.real_getfqdn |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 927 | # indicate that the client is finished |
| 928 | self.client_evt.set() |
| 929 | # wait for the server thread to terminate |
| 930 | self.serv_evt.wait() |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 931 | join_thread(self.thread) |
| 932 | del self.thread |
| 933 | self.doCleanups() |
| 934 | threading_cleanup(*self.thread_key) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 935 | |
| 936 | def testBasic(self): |
| 937 | # smoke test |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 938 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 939 | smtp.quit() |
| 940 | |
| 941 | def testEHLO(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 942 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 943 | |
| 944 | # no features should be present before the EHLO |
| 945 | self.assertEqual(smtp.esmtp_features, {}) |
| 946 | |
| 947 | # features expected from the test server |
| 948 | expected_features = {'expn':'', |
| 949 | 'size': '20000000', |
| 950 | 'starttls': '', |
| 951 | 'deliverby': '', |
| 952 | 'help': '', |
| 953 | } |
| 954 | |
| 955 | smtp.ehlo() |
| 956 | self.assertEqual(smtp.esmtp_features, expected_features) |
| 957 | for k in expected_features: |
| 958 | self.assertTrue(smtp.has_extn(k)) |
| 959 | self.assertFalse(smtp.has_extn('unsupported-feature')) |
| 960 | smtp.quit() |
| 961 | |
| 962 | def testVRFY(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 963 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 964 | |
Barry Warsaw | c5ea754 | 2015-07-09 10:39:55 -0400 | [diff] [blame] | 965 | for addr_spec, name in sim_users.items(): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 966 | expected_known = (250, bytes('%s %s' % |
Barry Warsaw | c5ea754 | 2015-07-09 10:39:55 -0400 | [diff] [blame] | 967 | (name, smtplib.quoteaddr(addr_spec)), |
Guido van Rossum | 5a23cc5 | 2007-08-30 14:02:43 +0000 | [diff] [blame] | 968 | "ascii")) |
Barry Warsaw | c5ea754 | 2015-07-09 10:39:55 -0400 | [diff] [blame] | 969 | self.assertEqual(smtp.vrfy(addr_spec), expected_known) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 970 | |
| 971 | u = 'nobody@nowhere.com' |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 972 | expected_unknown = (550, ('No such user: %s' % u).encode('ascii')) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 973 | self.assertEqual(smtp.vrfy(u), expected_unknown) |
| 974 | smtp.quit() |
| 975 | |
| 976 | def testEXPN(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 977 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 978 | |
| 979 | for listname, members in sim_lists.items(): |
| 980 | users = [] |
| 981 | for m in members: |
| 982 | users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m))) |
Guido van Rossum | 5a23cc5 | 2007-08-30 14:02:43 +0000 | [diff] [blame] | 983 | expected_known = (250, bytes('\n'.join(users), "ascii")) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 984 | self.assertEqual(smtp.expn(listname), expected_known) |
| 985 | |
| 986 | u = 'PSU-Members-List' |
| 987 | expected_unknown = (550, b'No access for you!') |
| 988 | self.assertEqual(smtp.expn(u), expected_unknown) |
| 989 | smtp.quit() |
| 990 | |
R David Murray | 76e13c1 | 2014-07-03 14:47:46 -0400 | [diff] [blame] | 991 | def testAUTH_PLAIN(self): |
| 992 | self.serv.add_feature("AUTH PLAIN") |
| 993 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R David Murray | b0deeb4 | 2015-11-08 01:03:52 -0500 | [diff] [blame] | 994 | resp = smtp.login(sim_auth[0], sim_auth[1]) |
| 995 | self.assertEqual(resp, (235, b'Authentication Succeeded')) |
R David Murray | 76e13c1 | 2014-07-03 14:47:46 -0400 | [diff] [blame] | 996 | smtp.close() |
| 997 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 998 | def testAUTH_LOGIN(self): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 999 | self.serv.add_feature("AUTH LOGIN") |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 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')) |
Benjamin Peterson | d094efd | 2010-10-31 17:15:42 +0000 | [diff] [blame] | 1003 | smtp.close() |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 1004 | |
| 1005 | def testAUTH_CRAM_MD5(self): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 1006 | self.serv.add_feature("AUTH CRAM-MD5") |
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 | |
Andrew Kuchling | 7859182 | 2013-11-11 14:03:23 -0500 | [diff] [blame] | 1012 | def testAUTH_multiple(self): |
| 1013 | # Test that multiple authentication methods are tried. |
| 1014 | self.serv.add_feature("AUTH BOGUS PLAIN LOGIN CRAM-MD5") |
| 1015 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R David Murray | b0deeb4 | 2015-11-08 01:03:52 -0500 | [diff] [blame] | 1016 | resp = smtp.login(sim_auth[0], sim_auth[1]) |
| 1017 | self.assertEqual(resp, (235, b'Authentication Succeeded')) |
R David Murray | 76e13c1 | 2014-07-03 14:47:46 -0400 | [diff] [blame] | 1018 | smtp.close() |
| 1019 | |
| 1020 | def test_auth_function(self): |
R David Murray | b0deeb4 | 2015-11-08 01:03:52 -0500 | [diff] [blame] | 1021 | supported = {'CRAM-MD5', 'PLAIN', 'LOGIN'} |
| 1022 | for mechanism in supported: |
| 1023 | self.serv.add_feature("AUTH {}".format(mechanism)) |
| 1024 | for mechanism in supported: |
| 1025 | with self.subTest(mechanism=mechanism): |
| 1026 | smtp = smtplib.SMTP(HOST, self.port, |
| 1027 | local_hostname='localhost', timeout=15) |
| 1028 | smtp.ehlo('foo') |
| 1029 | smtp.user, smtp.password = sim_auth[0], sim_auth[1] |
| 1030 | method = 'auth_' + mechanism.lower().replace('-', '_') |
| 1031 | resp = smtp.auth(mechanism, getattr(smtp, method)) |
| 1032 | self.assertEqual(resp, (235, b'Authentication Succeeded')) |
| 1033 | smtp.close() |
Andrew Kuchling | 7859182 | 2013-11-11 14:03:23 -0500 | [diff] [blame] | 1034 | |
R David Murray | 0cff49f | 2014-08-30 16:51:59 -0400 | [diff] [blame] | 1035 | def test_quit_resets_greeting(self): |
| 1036 | smtp = smtplib.SMTP(HOST, self.port, |
| 1037 | local_hostname='localhost', |
| 1038 | timeout=15) |
| 1039 | code, message = smtp.ehlo() |
| 1040 | self.assertEqual(code, 250) |
| 1041 | self.assertIn('size', smtp.esmtp_features) |
| 1042 | smtp.quit() |
| 1043 | self.assertNotIn('size', smtp.esmtp_features) |
| 1044 | smtp.connect(HOST, self.port) |
| 1045 | self.assertNotIn('size', smtp.esmtp_features) |
| 1046 | smtp.ehlo_or_helo_if_needed() |
| 1047 | self.assertIn('size', smtp.esmtp_features) |
| 1048 | smtp.quit() |
| 1049 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 1050 | def test_with_statement(self): |
| 1051 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 1052 | code, message = smtp.noop() |
| 1053 | self.assertEqual(code, 250) |
| 1054 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo') |
| 1055 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 1056 | smtp.close() |
| 1057 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo') |
| 1058 | |
| 1059 | def test_with_statement_QUIT_failure(self): |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 1060 | with self.assertRaises(smtplib.SMTPResponseException) as error: |
| 1061 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 1062 | smtp.noop() |
R David Murray | 6bd5202 | 2013-03-21 00:32:31 -0400 | [diff] [blame] | 1063 | self.serv._SMTPchannel.quit_response = '421 QUIT FAILED' |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 1064 | self.assertEqual(error.exception.smtp_code, 421) |
| 1065 | self.assertEqual(error.exception.smtp_error, b'QUIT FAILED') |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 1066 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 1067 | #TODO: add tests for correct AUTH method fallback now that the |
| 1068 | #test infrastructure can support it. |
| 1069 | |
R David Murray | afb151a | 2014-04-14 18:21:38 -0400 | [diff] [blame] | 1070 | # Issue 17498: make sure _rset does not raise SMTPServerDisconnected exception |
| 1071 | def test__rest_from_mail_cmd(self): |
| 1072 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
| 1073 | smtp.noop() |
| 1074 | self.serv._SMTPchannel.mail_response = '451 Requested action aborted' |
| 1075 | self.serv._SMTPchannel.disconnect = True |
| 1076 | with self.assertRaises(smtplib.SMTPSenderRefused): |
| 1077 | smtp.sendmail('John', 'Sally', 'test message') |
| 1078 | self.assertIsNone(smtp.sock) |
| 1079 | |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 1080 | # Issue 5713: make sure close, not rset, is called if we get a 421 error |
| 1081 | def test_421_from_mail_cmd(self): |
| 1082 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R David Murray | 853c0f9 | 2013-03-20 21:54:05 -0400 | [diff] [blame] | 1083 | smtp.noop() |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 1084 | self.serv._SMTPchannel.mail_response = '421 closing connection' |
| 1085 | with self.assertRaises(smtplib.SMTPSenderRefused): |
| 1086 | smtp.sendmail('John', 'Sally', 'test message') |
| 1087 | self.assertIsNone(smtp.sock) |
R David Murray | 03b0116 | 2013-03-20 22:11:40 -0400 | [diff] [blame] | 1088 | self.assertEqual(self.serv._SMTPchannel.rset_count, 0) |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 1089 | |
| 1090 | def test_421_from_rcpt_cmd(self): |
| 1091 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R David Murray | 853c0f9 | 2013-03-20 21:54:05 -0400 | [diff] [blame] | 1092 | smtp.noop() |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 1093 | self.serv._SMTPchannel.rcpt_response = ['250 accepted', '421 closing'] |
| 1094 | with self.assertRaises(smtplib.SMTPRecipientsRefused) as r: |
| 1095 | smtp.sendmail('John', ['Sally', 'Frank', 'George'], 'test message') |
| 1096 | self.assertIsNone(smtp.sock) |
| 1097 | self.assertEqual(self.serv._SMTPchannel.rset_count, 0) |
| 1098 | self.assertDictEqual(r.exception.args[0], {'Frank': (421, b'closing')}) |
| 1099 | |
| 1100 | def test_421_from_data_cmd(self): |
| 1101 | class MySimSMTPChannel(SimSMTPChannel): |
| 1102 | def found_terminator(self): |
| 1103 | if self.smtp_state == self.DATA: |
| 1104 | self.push('421 closing') |
| 1105 | else: |
| 1106 | super().found_terminator() |
| 1107 | self.serv.channel_class = MySimSMTPChannel |
| 1108 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R David Murray | 853c0f9 | 2013-03-20 21:54:05 -0400 | [diff] [blame] | 1109 | smtp.noop() |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 1110 | with self.assertRaises(smtplib.SMTPDataError): |
| 1111 | smtp.sendmail('John@foo.org', ['Sally@foo.org'], 'test message') |
| 1112 | self.assertIsNone(smtp.sock) |
| 1113 | self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0) |
| 1114 | |
R David Murray | cee7cf6 | 2015-05-16 13:58:14 -0400 | [diff] [blame] | 1115 | def test_smtputf8_NotSupportedError_if_no_server_support(self): |
| 1116 | smtp = smtplib.SMTP( |
| 1117 | HOST, self.port, local_hostname='localhost', timeout=3) |
| 1118 | self.addCleanup(smtp.close) |
| 1119 | smtp.ehlo() |
| 1120 | self.assertTrue(smtp.does_esmtp) |
| 1121 | self.assertFalse(smtp.has_extn('smtputf8')) |
| 1122 | self.assertRaises( |
| 1123 | smtplib.SMTPNotSupportedError, |
| 1124 | smtp.sendmail, |
| 1125 | 'John', 'Sally', '', mail_options=['BODY=8BITMIME', 'SMTPUTF8']) |
| 1126 | self.assertRaises( |
| 1127 | smtplib.SMTPNotSupportedError, |
| 1128 | smtp.mail, 'John', options=['BODY=8BITMIME', 'SMTPUTF8']) |
| 1129 | |
| 1130 | def test_send_unicode_without_SMTPUTF8(self): |
| 1131 | smtp = smtplib.SMTP( |
| 1132 | HOST, self.port, local_hostname='localhost', timeout=3) |
| 1133 | self.addCleanup(smtp.close) |
| 1134 | self.assertRaises(UnicodeEncodeError, smtp.sendmail, 'Alice', 'Böb', '') |
| 1135 | self.assertRaises(UnicodeEncodeError, smtp.mail, 'Älice') |
| 1136 | |
chason | 48ed88a | 2018-07-26 04:01:28 +0900 | [diff] [blame] | 1137 | def test_send_message_error_on_non_ascii_addrs_if_no_smtputf8(self): |
| 1138 | # This test is located here and not in the SMTPUTF8SimTests |
| 1139 | # class because it needs a "regular" SMTP server to work |
| 1140 | msg = EmailMessage() |
| 1141 | msg['From'] = "Páolo <főo@bar.com>" |
| 1142 | msg['To'] = 'Dinsdale' |
| 1143 | msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' |
| 1144 | smtp = smtplib.SMTP( |
| 1145 | HOST, self.port, local_hostname='localhost', timeout=3) |
| 1146 | self.addCleanup(smtp.close) |
| 1147 | with self.assertRaises(smtplib.SMTPNotSupportedError): |
| 1148 | smtp.send_message(msg) |
| 1149 | |
Stéphane Wirtel | 8d83e4b | 2018-01-31 01:02:51 +0100 | [diff] [blame] | 1150 | def test_name_field_not_included_in_envelop_addresses(self): |
| 1151 | smtp = smtplib.SMTP( |
| 1152 | HOST, self.port, local_hostname='localhost', timeout=3 |
| 1153 | ) |
| 1154 | self.addCleanup(smtp.close) |
| 1155 | |
| 1156 | message = EmailMessage() |
| 1157 | message['From'] = email.utils.formataddr(('Michaël', 'michael@example.com')) |
| 1158 | message['To'] = email.utils.formataddr(('René', 'rene@example.com')) |
| 1159 | |
| 1160 | self.assertDictEqual(smtp.send_message(message), {}) |
| 1161 | |
| 1162 | self.assertEqual(self.serv._addresses['from'], 'michael@example.com') |
| 1163 | self.assertEqual(self.serv._addresses['tos'], ['rene@example.com']) |
| 1164 | |
R David Murray | cee7cf6 | 2015-05-16 13:58:14 -0400 | [diff] [blame] | 1165 | |
| 1166 | class SimSMTPUTF8Server(SimSMTPServer): |
| 1167 | |
| 1168 | def __init__(self, *args, **kw): |
| 1169 | # The base SMTP server turns these on automatically, but our test |
| 1170 | # server is set up to munge the EHLO response, so we need to provide |
| 1171 | # them as well. And yes, the call is to SMTPServer not SimSMTPServer. |
| 1172 | self._extra_features = ['SMTPUTF8', '8BITMIME'] |
| 1173 | smtpd.SMTPServer.__init__(self, *args, **kw) |
| 1174 | |
| 1175 | def handle_accepted(self, conn, addr): |
| 1176 | self._SMTPchannel = self.channel_class( |
| 1177 | self._extra_features, self, conn, addr, |
| 1178 | decode_data=self._decode_data, |
| 1179 | enable_SMTPUTF8=self.enable_SMTPUTF8, |
| 1180 | ) |
| 1181 | |
| 1182 | def process_message(self, peer, mailfrom, rcpttos, data, mail_options=None, |
| 1183 | rcpt_options=None): |
| 1184 | self.last_peer = peer |
| 1185 | self.last_mailfrom = mailfrom |
| 1186 | self.last_rcpttos = rcpttos |
| 1187 | self.last_message = data |
| 1188 | self.last_mail_options = mail_options |
| 1189 | self.last_rcpt_options = rcpt_options |
| 1190 | |
| 1191 | |
R David Murray | cee7cf6 | 2015-05-16 13:58:14 -0400 | [diff] [blame] | 1192 | class SMTPUTF8SimTests(unittest.TestCase): |
| 1193 | |
R David Murray | 8308444 | 2015-05-17 19:27:22 -0400 | [diff] [blame] | 1194 | maxDiff = None |
| 1195 | |
R David Murray | cee7cf6 | 2015-05-16 13:58:14 -0400 | [diff] [blame] | 1196 | def setUp(self): |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 1197 | self.thread_key = threading_setup() |
R David Murray | cee7cf6 | 2015-05-16 13:58:14 -0400 | [diff] [blame] | 1198 | self.real_getfqdn = socket.getfqdn |
| 1199 | socket.getfqdn = mock_socket.getfqdn |
| 1200 | self.serv_evt = threading.Event() |
| 1201 | self.client_evt = threading.Event() |
| 1202 | # Pick a random unused port by passing 0 for the port number |
| 1203 | self.serv = SimSMTPUTF8Server((HOST, 0), ('nowhere', -1), |
| 1204 | decode_data=False, |
| 1205 | enable_SMTPUTF8=True) |
| 1206 | # Keep a note of what port was assigned |
| 1207 | self.port = self.serv.socket.getsockname()[1] |
| 1208 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
| 1209 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 1210 | self.thread.start() |
| 1211 | |
| 1212 | # wait until server thread has assigned a port number |
| 1213 | self.serv_evt.wait() |
| 1214 | self.serv_evt.clear() |
| 1215 | |
| 1216 | def tearDown(self): |
| 1217 | socket.getfqdn = self.real_getfqdn |
| 1218 | # indicate that the client is finished |
| 1219 | self.client_evt.set() |
| 1220 | # wait for the server thread to terminate |
| 1221 | self.serv_evt.wait() |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 1222 | join_thread(self.thread) |
| 1223 | del self.thread |
| 1224 | self.doCleanups() |
| 1225 | threading_cleanup(*self.thread_key) |
R David Murray | cee7cf6 | 2015-05-16 13:58:14 -0400 | [diff] [blame] | 1226 | |
| 1227 | def test_test_server_supports_extensions(self): |
| 1228 | smtp = smtplib.SMTP( |
| 1229 | HOST, self.port, local_hostname='localhost', timeout=3) |
| 1230 | self.addCleanup(smtp.close) |
| 1231 | smtp.ehlo() |
| 1232 | self.assertTrue(smtp.does_esmtp) |
| 1233 | self.assertTrue(smtp.has_extn('smtputf8')) |
| 1234 | |
| 1235 | def test_send_unicode_with_SMTPUTF8_via_sendmail(self): |
| 1236 | m = '¡a test message containing unicode!'.encode('utf-8') |
| 1237 | smtp = smtplib.SMTP( |
| 1238 | HOST, self.port, local_hostname='localhost', timeout=3) |
| 1239 | self.addCleanup(smtp.close) |
| 1240 | smtp.sendmail('Jőhn', 'Sálly', m, |
| 1241 | mail_options=['BODY=8BITMIME', 'SMTPUTF8']) |
| 1242 | self.assertEqual(self.serv.last_mailfrom, 'Jőhn') |
| 1243 | self.assertEqual(self.serv.last_rcpttos, ['Sálly']) |
| 1244 | self.assertEqual(self.serv.last_message, m) |
| 1245 | self.assertIn('BODY=8BITMIME', self.serv.last_mail_options) |
| 1246 | self.assertIn('SMTPUTF8', self.serv.last_mail_options) |
| 1247 | self.assertEqual(self.serv.last_rcpt_options, []) |
| 1248 | |
| 1249 | def test_send_unicode_with_SMTPUTF8_via_low_level_API(self): |
| 1250 | m = '¡a test message containing unicode!'.encode('utf-8') |
| 1251 | smtp = smtplib.SMTP( |
| 1252 | HOST, self.port, local_hostname='localhost', timeout=3) |
| 1253 | self.addCleanup(smtp.close) |
| 1254 | smtp.ehlo() |
| 1255 | self.assertEqual( |
| 1256 | smtp.mail('Jő', options=['BODY=8BITMIME', 'SMTPUTF8']), |
| 1257 | (250, b'OK')) |
| 1258 | self.assertEqual(smtp.rcpt('János'), (250, b'OK')) |
| 1259 | self.assertEqual(smtp.data(m), (250, b'OK')) |
| 1260 | self.assertEqual(self.serv.last_mailfrom, 'Jő') |
| 1261 | self.assertEqual(self.serv.last_rcpttos, ['János']) |
| 1262 | self.assertEqual(self.serv.last_message, m) |
| 1263 | self.assertIn('BODY=8BITMIME', self.serv.last_mail_options) |
| 1264 | self.assertIn('SMTPUTF8', self.serv.last_mail_options) |
| 1265 | self.assertEqual(self.serv.last_rcpt_options, []) |
| 1266 | |
R David Murray | 8308444 | 2015-05-17 19:27:22 -0400 | [diff] [blame] | 1267 | def test_send_message_uses_smtputf8_if_addrs_non_ascii(self): |
| 1268 | msg = EmailMessage() |
| 1269 | msg['From'] = "Páolo <főo@bar.com>" |
| 1270 | msg['To'] = 'Dinsdale' |
| 1271 | msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' |
| 1272 | # XXX I don't know why I need two \n's here, but this is an existing |
| 1273 | # bug (if it is one) and not a problem with the new functionality. |
| 1274 | msg.set_content("oh là là, know what I mean, know what I mean?\n\n") |
| 1275 | # XXX smtpd converts received /r/n to /n, so we can't easily test that |
| 1276 | # we are successfully sending /r/n :(. |
| 1277 | expected = textwrap.dedent("""\ |
| 1278 | From: Páolo <főo@bar.com> |
| 1279 | To: Dinsdale |
| 1280 | Subject: Nudge nudge, wink, wink \u1F609 |
| 1281 | Content-Type: text/plain; charset="utf-8" |
| 1282 | Content-Transfer-Encoding: 8bit |
| 1283 | MIME-Version: 1.0 |
| 1284 | |
| 1285 | oh là là, know what I mean, know what I mean? |
| 1286 | """) |
| 1287 | smtp = smtplib.SMTP( |
| 1288 | HOST, self.port, local_hostname='localhost', timeout=3) |
| 1289 | self.addCleanup(smtp.close) |
| 1290 | self.assertEqual(smtp.send_message(msg), {}) |
| 1291 | self.assertEqual(self.serv.last_mailfrom, 'főo@bar.com') |
| 1292 | self.assertEqual(self.serv.last_rcpttos, ['Dinsdale']) |
| 1293 | self.assertEqual(self.serv.last_message.decode(), expected) |
| 1294 | self.assertIn('BODY=8BITMIME', self.serv.last_mail_options) |
| 1295 | self.assertIn('SMTPUTF8', self.serv.last_mail_options) |
| 1296 | self.assertEqual(self.serv.last_rcpt_options, []) |
| 1297 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 1298 | |
Barry Warsaw | c5ea754 | 2015-07-09 10:39:55 -0400 | [diff] [blame] | 1299 | EXPECTED_RESPONSE = encode_base64(b'\0psu\0doesnotexist', eol='') |
| 1300 | |
| 1301 | class SimSMTPAUTHInitialResponseChannel(SimSMTPChannel): |
| 1302 | def smtp_AUTH(self, arg): |
| 1303 | # RFC 4954's AUTH command allows for an optional initial-response. |
| 1304 | # Not all AUTH methods support this; some require a challenge. AUTH |
| 1305 | # PLAIN does those, so test that here. See issue #15014. |
| 1306 | args = arg.split() |
| 1307 | if args[0].lower() == 'plain': |
| 1308 | if len(args) == 2: |
| 1309 | # AUTH PLAIN <initial-response> with the response base 64 |
| 1310 | # encoded. Hard code the expected response for the test. |
| 1311 | if args[1] == EXPECTED_RESPONSE: |
| 1312 | self.push('235 Ok') |
| 1313 | return |
| 1314 | self.push('571 Bad authentication') |
| 1315 | |
| 1316 | class SimSMTPAUTHInitialResponseServer(SimSMTPServer): |
| 1317 | channel_class = SimSMTPAUTHInitialResponseChannel |
| 1318 | |
| 1319 | |
Barry Warsaw | c5ea754 | 2015-07-09 10:39:55 -0400 | [diff] [blame] | 1320 | class SMTPAUTHInitialResponseSimTests(unittest.TestCase): |
| 1321 | def setUp(self): |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 1322 | self.thread_key = threading_setup() |
Barry Warsaw | c5ea754 | 2015-07-09 10:39:55 -0400 | [diff] [blame] | 1323 | self.real_getfqdn = socket.getfqdn |
| 1324 | socket.getfqdn = mock_socket.getfqdn |
| 1325 | self.serv_evt = threading.Event() |
| 1326 | self.client_evt = threading.Event() |
| 1327 | # Pick a random unused port by passing 0 for the port number |
| 1328 | self.serv = SimSMTPAUTHInitialResponseServer( |
| 1329 | (HOST, 0), ('nowhere', -1), decode_data=True) |
| 1330 | # Keep a note of what port was assigned |
| 1331 | self.port = self.serv.socket.getsockname()[1] |
| 1332 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
| 1333 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 1334 | self.thread.start() |
| 1335 | |
| 1336 | # wait until server thread has assigned a port number |
| 1337 | self.serv_evt.wait() |
| 1338 | self.serv_evt.clear() |
| 1339 | |
| 1340 | def tearDown(self): |
| 1341 | socket.getfqdn = self.real_getfqdn |
| 1342 | # indicate that the client is finished |
| 1343 | self.client_evt.set() |
| 1344 | # wait for the server thread to terminate |
| 1345 | self.serv_evt.wait() |
Pablo Galindo | 5b7a2cb | 2018-09-08 00:15:22 +0100 | [diff] [blame] | 1346 | join_thread(self.thread) |
| 1347 | del self.thread |
| 1348 | self.doCleanups() |
| 1349 | threading_cleanup(*self.thread_key) |
Barry Warsaw | c5ea754 | 2015-07-09 10:39:55 -0400 | [diff] [blame] | 1350 | |
| 1351 | def testAUTH_PLAIN_initial_response_login(self): |
| 1352 | self.serv.add_feature('AUTH PLAIN') |
| 1353 | smtp = smtplib.SMTP(HOST, self.port, |
| 1354 | local_hostname='localhost', timeout=15) |
| 1355 | smtp.login('psu', 'doesnotexist') |
| 1356 | smtp.close() |
| 1357 | |
| 1358 | def testAUTH_PLAIN_initial_response_auth(self): |
| 1359 | self.serv.add_feature('AUTH PLAIN') |
| 1360 | smtp = smtplib.SMTP(HOST, self.port, |
| 1361 | local_hostname='localhost', timeout=15) |
| 1362 | smtp.user = 'psu' |
| 1363 | smtp.password = 'doesnotexist' |
| 1364 | code, response = smtp.auth('plain', smtp.auth_plain) |
| 1365 | smtp.close() |
| 1366 | self.assertEqual(code, 235) |
| 1367 | |
| 1368 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1369 | if __name__ == '__main__': |
chason | 48ed88a | 2018-07-26 04:01:28 +0900 | [diff] [blame] | 1370 | unittest.main() |