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