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