Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 1 | import asyncore |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 2 | import email.mime.text |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 3 | import email.utils |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 4 | import socket |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 5 | import smtpd |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import smtplib |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7 | import io |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 8 | import re |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 9 | import sys |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 10 | import time |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 11 | import select |
Ross Lagerwall | 8640743 | 2012-03-29 18:08:48 +0200 | [diff] [blame] | 12 | import errno |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 13 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 14 | import unittest |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 15 | from test import support, mock_socket |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 16 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 17 | try: |
| 18 | import threading |
| 19 | except ImportError: |
| 20 | threading = None |
| 21 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 22 | HOST = support.HOST |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 23 | |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 24 | if sys.platform == 'darwin': |
| 25 | # select.poll returns a select.POLLHUP at the end of the tests |
| 26 | # on darwin, so just ignore it |
| 27 | def handle_expt(self): |
| 28 | pass |
| 29 | smtpd.SMTPChannel.handle_expt = handle_expt |
| 30 | |
| 31 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 32 | def server(evt, buf, serv): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 33 | serv.listen(5) |
| 34 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 35 | try: |
| 36 | conn, addr = serv.accept() |
| 37 | except socket.timeout: |
| 38 | pass |
| 39 | else: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 40 | n = 500 |
| 41 | while buf and n > 0: |
| 42 | r, w, e = select.select([], [conn], []) |
| 43 | if w: |
| 44 | sent = conn.send(buf) |
| 45 | buf = buf[sent:] |
| 46 | |
| 47 | n -= 1 |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 49 | conn.close() |
| 50 | finally: |
| 51 | serv.close() |
| 52 | evt.set() |
| 53 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 54 | class GeneralTests(unittest.TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 55 | |
| 56 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 57 | smtplib.socket = mock_socket |
| 58 | self.port = 25 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 59 | |
| 60 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 61 | smtplib.socket = socket |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 62 | |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 63 | # This method is no longer used but is retained for backward compatibility, |
| 64 | # so test to make sure it still works. |
| 65 | def testQuoteData(self): |
| 66 | teststr = "abc\n.jkl\rfoo\r\n..blue" |
| 67 | expected = "abc\r\n..jkl\r\nfoo\r\n...blue" |
| 68 | self.assertEqual(expected, smtplib.quotedata(teststr)) |
| 69 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 70 | def testBasic1(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 71 | mock_socket.reply_with(b"220 Hola mundo") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 72 | # connects |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 73 | smtp = smtplib.SMTP(HOST, self.port) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 74 | smtp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 75 | |
Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 76 | def testSourceAddress(self): |
| 77 | mock_socket.reply_with(b"220 Hola mundo") |
| 78 | # connects |
| 79 | smtp = smtplib.SMTP(HOST, self.port, |
| 80 | source_address=('127.0.0.1',19876)) |
| 81 | self.assertEqual(smtp.source_address, ('127.0.0.1', 19876)) |
| 82 | smtp.close() |
| 83 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 84 | def testBasic2(self): |
Richard Jones | 6a9e6bb | 2010-08-04 12:27:36 +0000 | [diff] [blame] | 85 | mock_socket.reply_with(b"220 Hola mundo") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 86 | # connects, include port in host name |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 87 | smtp = smtplib.SMTP("%s:%s" % (HOST, self.port)) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 88 | smtp.close() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 89 | |
| 90 | def testLocalHostName(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 | # check that supplied local_hostname is used |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 93 | smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 94 | self.assertEqual(smtp.local_hostname, "testhost") |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 95 | smtp.close() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 97 | def testTimeoutDefault(self): |
Richard Jones | 6a9e6bb | 2010-08-04 12:27:36 +0000 | [diff] [blame] | 98 | mock_socket.reply_with(b"220 Hola mundo") |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 99 | self.assertIsNone(mock_socket.getdefaulttimeout()) |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 100 | mock_socket.setdefaulttimeout(30) |
| 101 | self.assertEqual(mock_socket.getdefaulttimeout(), 30) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 102 | try: |
| 103 | smtp = smtplib.SMTP(HOST, self.port) |
| 104 | finally: |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 105 | mock_socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 106 | self.assertEqual(smtp.sock.gettimeout(), 30) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 107 | smtp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 108 | |
| 109 | def testTimeoutNone(self): |
Richard Jones | 6a9e6bb | 2010-08-04 12:27:36 +0000 | [diff] [blame] | 110 | mock_socket.reply_with(b"220 Hola mundo") |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 111 | self.assertIsNone(socket.getdefaulttimeout()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 112 | socket.setdefaulttimeout(30) |
| 113 | try: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 114 | smtp = smtplib.SMTP(HOST, self.port, timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 115 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 116 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 117 | self.assertIsNone(smtp.sock.gettimeout()) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 118 | smtp.close() |
| 119 | |
| 120 | def testTimeoutValue(self): |
Richard Jones | 6a9e6bb | 2010-08-04 12:27:36 +0000 | [diff] [blame] | 121 | mock_socket.reply_with(b"220 Hola mundo") |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 122 | smtp = smtplib.SMTP(HOST, self.port, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 123 | self.assertEqual(smtp.sock.gettimeout(), 30) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 124 | smtp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 125 | |
| 126 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 127 | # Test server thread using the specified SMTP server class |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 128 | def debugging_server(serv, serv_evt, client_evt): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 129 | serv_evt.set() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 130 | |
| 131 | try: |
| 132 | if hasattr(select, 'poll'): |
| 133 | poll_fun = asyncore.poll2 |
| 134 | else: |
| 135 | poll_fun = asyncore.poll |
| 136 | |
| 137 | n = 1000 |
| 138 | while asyncore.socket_map and n > 0: |
| 139 | poll_fun(0.01, asyncore.socket_map) |
| 140 | |
| 141 | # when the client conversation is finished, it will |
| 142 | # set client_evt, and it's then ok to kill the server |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 143 | if client_evt.is_set(): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 144 | serv.close() |
| 145 | break |
| 146 | |
| 147 | n -= 1 |
| 148 | |
| 149 | except socket.timeout: |
| 150 | pass |
| 151 | finally: |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 152 | if not client_evt.is_set(): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 153 | # allow some time for the client to read the result |
| 154 | time.sleep(0.5) |
| 155 | serv.close() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 156 | asyncore.close_all() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 157 | serv_evt.set() |
| 158 | |
| 159 | MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n' |
| 160 | MSG_END = '------------ END MESSAGE ------------\n' |
| 161 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 162 | # NOTE: Some SMTP objects in the tests below are created with a non-default |
| 163 | # local_hostname argument to the constructor, since (on some systems) the FQDN |
| 164 | # 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] | 165 | # test server times out, causing the test to fail. |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 166 | |
| 167 | # Test behavior of smtpd.DebuggingServer |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 168 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 169 | class DebuggingServerTests(unittest.TestCase): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 170 | |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 171 | maxDiff = None |
| 172 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 173 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 174 | self.real_getfqdn = socket.getfqdn |
| 175 | socket.getfqdn = mock_socket.getfqdn |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 176 | # temporarily replace sys.stdout to capture DebuggingServer output |
| 177 | self.old_stdout = sys.stdout |
| 178 | self.output = io.StringIO() |
| 179 | sys.stdout = self.output |
| 180 | |
| 181 | self.serv_evt = threading.Event() |
| 182 | self.client_evt = threading.Event() |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 183 | # Capture SMTPChannel debug output |
| 184 | self.old_DEBUGSTREAM = smtpd.DEBUGSTREAM |
| 185 | smtpd.DEBUGSTREAM = io.StringIO() |
Antoine Pitrou | 043bad0 | 2010-04-30 23:20:15 +0000 | [diff] [blame] | 186 | # 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^] | 187 | self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1), |
| 188 | decode_data=True) |
Antoine Pitrou | 043bad0 | 2010-04-30 23:20:15 +0000 | [diff] [blame] | 189 | # Keep a note of what port was assigned |
| 190 | self.port = self.serv.socket.getsockname()[1] |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 191 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 192 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 193 | self.thread.start() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 194 | |
| 195 | # wait until server thread has assigned a port number |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 196 | self.serv_evt.wait() |
| 197 | self.serv_evt.clear() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 198 | |
| 199 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 200 | socket.getfqdn = self.real_getfqdn |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 201 | # indicate that the client is finished |
| 202 | self.client_evt.set() |
| 203 | # wait for the server thread to terminate |
| 204 | self.serv_evt.wait() |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 205 | self.thread.join() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 206 | # restore sys.stdout |
| 207 | sys.stdout = self.old_stdout |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 208 | # restore DEBUGSTREAM |
| 209 | smtpd.DEBUGSTREAM.close() |
| 210 | smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 211 | |
| 212 | def testBasic(self): |
| 213 | # connect |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 214 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 215 | smtp.quit() |
| 216 | |
Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 217 | def testSourceAddress(self): |
| 218 | # connect |
Senthil Kumaran | b351a48 | 2011-07-31 09:14:17 +0800 | [diff] [blame] | 219 | port = support.find_unused_port() |
| 220 | try: |
| 221 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', |
| 222 | timeout=3, source_address=('127.0.0.1', port)) |
| 223 | self.assertEqual(smtp.source_address, ('127.0.0.1', port)) |
| 224 | self.assertEqual(smtp.local_hostname, 'localhost') |
| 225 | smtp.quit() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 226 | except OSError as e: |
Senthil Kumaran | b351a48 | 2011-07-31 09:14:17 +0800 | [diff] [blame] | 227 | if e.errno == errno.EADDRINUSE: |
| 228 | self.skipTest("couldn't bind to port %d" % port) |
| 229 | raise |
Senthil Kumaran | 3d23fd6 | 2011-07-30 10:56:50 +0800 | [diff] [blame] | 230 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 231 | def testNOOP(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 232 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 233 | expected = (250, b'OK') |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 234 | self.assertEqual(smtp.noop(), expected) |
| 235 | smtp.quit() |
| 236 | |
| 237 | def testRSET(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 238 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 239 | expected = (250, b'OK') |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 240 | self.assertEqual(smtp.rset(), expected) |
| 241 | smtp.quit() |
| 242 | |
Benjamin Peterson | 1eca062 | 2013-09-29 10:46:31 -0400 | [diff] [blame] | 243 | def testELHO(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 244 | # EHLO isn't implemented in DebuggingServer |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 245 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Benjamin Peterson | 1eca062 | 2013-09-29 10:46:31 -0400 | [diff] [blame] | 246 | expected = (250, b'\nSIZE 33554432\nHELP') |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 247 | self.assertEqual(smtp.ehlo(), expected) |
| 248 | smtp.quit() |
| 249 | |
Benjamin Peterson | 1eca062 | 2013-09-29 10:46:31 -0400 | [diff] [blame] | 250 | def testEXPNNotImplemented(self): |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 251 | # EXPN isn't implemented in DebuggingServer |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 252 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 253 | expected = (502, b'EXPN not implemented') |
| 254 | smtp.putcmd('EXPN') |
| 255 | self.assertEqual(smtp.getreply(), expected) |
| 256 | smtp.quit() |
| 257 | |
| 258 | def testVRFY(self): |
| 259 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 260 | expected = (252, b'Cannot VRFY user, but will accept message ' + \ |
| 261 | b'and attempt delivery') |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 262 | self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected) |
| 263 | self.assertEqual(smtp.verify('nobody@nowhere.com'), expected) |
| 264 | smtp.quit() |
| 265 | |
| 266 | def testSecondHELO(self): |
| 267 | # check that a second HELO returns a message that it's a duplicate |
| 268 | # (this behavior is specific to smtpd.SMTPChannel) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 269 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 270 | smtp.helo() |
| 271 | expected = (503, b'Duplicate HELO/EHLO') |
| 272 | self.assertEqual(smtp.helo(), expected) |
| 273 | smtp.quit() |
| 274 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 275 | def testHELP(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 276 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 277 | self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \ |
| 278 | b'RCPT DATA RSET NOOP QUIT VRFY') |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 279 | smtp.quit() |
| 280 | |
| 281 | def testSend(self): |
| 282 | # connect and send mail |
| 283 | m = 'A test message' |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 284 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 285 | smtp.sendmail('John', 'Sally', m) |
Neal Norwitz | 2532967 | 2008-08-25 03:55:03 +0000 | [diff] [blame] | 286 | # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor |
| 287 | # in asyncore. This sleep might help, but should really be fixed |
| 288 | # properly by using an Event variable. |
| 289 | time.sleep(0.01) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 290 | smtp.quit() |
| 291 | |
| 292 | self.client_evt.set() |
| 293 | self.serv_evt.wait() |
| 294 | self.output.flush() |
| 295 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END) |
| 296 | self.assertEqual(self.output.getvalue(), mexpect) |
| 297 | |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 298 | def testSendBinary(self): |
| 299 | m = b'A test message' |
| 300 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 301 | smtp.sendmail('John', 'Sally', m) |
| 302 | # XXX (see comment in testSend) |
| 303 | time.sleep(0.01) |
| 304 | smtp.quit() |
| 305 | |
| 306 | self.client_evt.set() |
| 307 | self.serv_evt.wait() |
| 308 | self.output.flush() |
| 309 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.decode('ascii'), MSG_END) |
| 310 | self.assertEqual(self.output.getvalue(), mexpect) |
| 311 | |
R David Murray | 0f663d0 | 2011-06-09 15:05:57 -0400 | [diff] [blame] | 312 | def testSendNeedingDotQuote(self): |
| 313 | # Issue 12283 |
| 314 | m = '.A test\n.mes.sage.' |
| 315 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 316 | smtp.sendmail('John', 'Sally', m) |
| 317 | # XXX (see comment in testSend) |
| 318 | time.sleep(0.01) |
| 319 | smtp.quit() |
| 320 | |
| 321 | self.client_evt.set() |
| 322 | self.serv_evt.wait() |
| 323 | self.output.flush() |
| 324 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END) |
| 325 | self.assertEqual(self.output.getvalue(), mexpect) |
| 326 | |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 327 | def testSendNullSender(self): |
| 328 | m = 'A test message' |
| 329 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 330 | smtp.sendmail('<>', 'Sally', m) |
| 331 | # XXX (see comment in testSend) |
| 332 | time.sleep(0.01) |
| 333 | smtp.quit() |
| 334 | |
| 335 | self.client_evt.set() |
| 336 | self.serv_evt.wait() |
| 337 | self.output.flush() |
| 338 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END) |
| 339 | self.assertEqual(self.output.getvalue(), mexpect) |
| 340 | debugout = smtpd.DEBUGSTREAM.getvalue() |
| 341 | sender = re.compile("^sender: <>$", re.MULTILINE) |
| 342 | self.assertRegex(debugout, sender) |
| 343 | |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 344 | def testSendMessage(self): |
| 345 | m = email.mime.text.MIMEText('A test message') |
| 346 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 347 | smtp.send_message(m, from_addr='John', to_addrs='Sally') |
| 348 | # XXX (see comment in testSend) |
| 349 | time.sleep(0.01) |
| 350 | smtp.quit() |
| 351 | |
| 352 | self.client_evt.set() |
| 353 | self.serv_evt.wait() |
| 354 | self.output.flush() |
| 355 | # Add the X-Peer header that DebuggingServer adds |
R David Murray | b912c5a | 2011-05-02 08:47:24 -0400 | [diff] [blame] | 356 | m['X-Peer'] = socket.gethostbyname('localhost') |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 357 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 358 | self.assertEqual(self.output.getvalue(), mexpect) |
| 359 | |
| 360 | def testSendMessageWithAddresses(self): |
| 361 | m = email.mime.text.MIMEText('A test message') |
| 362 | m['From'] = 'foo@bar.com' |
| 363 | m['To'] = 'John' |
| 364 | m['CC'] = 'Sally, Fred' |
| 365 | m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>' |
| 366 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 367 | smtp.send_message(m) |
| 368 | # XXX (see comment in testSend) |
| 369 | time.sleep(0.01) |
| 370 | smtp.quit() |
R David Murray | ac4e5ab | 2011-07-02 21:03:19 -0400 | [diff] [blame] | 371 | # make sure the Bcc header is still in the message. |
| 372 | self.assertEqual(m['Bcc'], 'John Root <root@localhost>, "Dinsdale" ' |
| 373 | '<warped@silly.walks.com>') |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 374 | |
| 375 | self.client_evt.set() |
| 376 | self.serv_evt.wait() |
| 377 | self.output.flush() |
| 378 | # Add the X-Peer header that DebuggingServer adds |
R David Murray | b912c5a | 2011-05-02 08:47:24 -0400 | [diff] [blame] | 379 | m['X-Peer'] = socket.gethostbyname('localhost') |
R David Murray | ac4e5ab | 2011-07-02 21:03:19 -0400 | [diff] [blame] | 380 | # The Bcc header should not be transmitted. |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 381 | del m['Bcc'] |
| 382 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 383 | self.assertEqual(self.output.getvalue(), mexpect) |
| 384 | debugout = smtpd.DEBUGSTREAM.getvalue() |
| 385 | sender = re.compile("^sender: foo@bar.com$", re.MULTILINE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 386 | self.assertRegex(debugout, sender) |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 387 | for addr in ('John', 'Sally', 'Fred', 'root@localhost', |
| 388 | 'warped@silly.walks.com'): |
| 389 | to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr), |
| 390 | re.MULTILINE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 391 | self.assertRegex(debugout, to_addr) |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 392 | |
| 393 | def testSendMessageWithSomeAddresses(self): |
| 394 | # Make sure nothing breaks if not all of the three 'to' headers exist |
| 395 | m = email.mime.text.MIMEText('A test message') |
| 396 | m['From'] = 'foo@bar.com' |
| 397 | m['To'] = 'John, Dinsdale' |
| 398 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 399 | smtp.send_message(m) |
| 400 | # XXX (see comment in testSend) |
| 401 | time.sleep(0.01) |
| 402 | smtp.quit() |
| 403 | |
| 404 | self.client_evt.set() |
| 405 | self.serv_evt.wait() |
| 406 | self.output.flush() |
| 407 | # Add the X-Peer header that DebuggingServer adds |
R David Murray | b912c5a | 2011-05-02 08:47:24 -0400 | [diff] [blame] | 408 | m['X-Peer'] = socket.gethostbyname('localhost') |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 409 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 410 | self.assertEqual(self.output.getvalue(), mexpect) |
| 411 | debugout = smtpd.DEBUGSTREAM.getvalue() |
| 412 | sender = re.compile("^sender: foo@bar.com$", re.MULTILINE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 413 | self.assertRegex(debugout, sender) |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 414 | for addr in ('John', 'Dinsdale'): |
| 415 | to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr), |
| 416 | re.MULTILINE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 417 | self.assertRegex(debugout, to_addr) |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 418 | |
R David Murray | ac4e5ab | 2011-07-02 21:03:19 -0400 | [diff] [blame] | 419 | def testSendMessageWithSpecifiedAddresses(self): |
| 420 | # Make sure addresses specified in call override those in message. |
| 421 | m = email.mime.text.MIMEText('A test message') |
| 422 | m['From'] = 'foo@bar.com' |
| 423 | m['To'] = 'John, Dinsdale' |
| 424 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 425 | smtp.send_message(m, from_addr='joe@example.com', to_addrs='foo@example.net') |
| 426 | # XXX (see comment in testSend) |
| 427 | time.sleep(0.01) |
| 428 | smtp.quit() |
| 429 | |
| 430 | self.client_evt.set() |
| 431 | self.serv_evt.wait() |
| 432 | self.output.flush() |
| 433 | # Add the X-Peer header that DebuggingServer adds |
| 434 | m['X-Peer'] = socket.gethostbyname('localhost') |
| 435 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 436 | self.assertEqual(self.output.getvalue(), mexpect) |
| 437 | debugout = smtpd.DEBUGSTREAM.getvalue() |
| 438 | sender = re.compile("^sender: joe@example.com$", re.MULTILINE) |
| 439 | self.assertRegex(debugout, sender) |
| 440 | for addr in ('John', 'Dinsdale'): |
| 441 | to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr), |
| 442 | re.MULTILINE) |
| 443 | self.assertNotRegex(debugout, to_addr) |
| 444 | recip = re.compile(r"^recips: .*'foo@example.net'.*$", re.MULTILINE) |
| 445 | self.assertRegex(debugout, recip) |
| 446 | |
| 447 | def testSendMessageWithMultipleFrom(self): |
| 448 | # Sender overrides To |
| 449 | m = email.mime.text.MIMEText('A test message') |
| 450 | m['From'] = 'Bernard, Bianca' |
| 451 | m['Sender'] = 'the_rescuers@Rescue-Aid-Society.com' |
| 452 | m['To'] = 'John, Dinsdale' |
| 453 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 454 | smtp.send_message(m) |
| 455 | # XXX (see comment in testSend) |
| 456 | time.sleep(0.01) |
| 457 | smtp.quit() |
| 458 | |
| 459 | self.client_evt.set() |
| 460 | self.serv_evt.wait() |
| 461 | self.output.flush() |
| 462 | # Add the X-Peer header that DebuggingServer adds |
| 463 | m['X-Peer'] = socket.gethostbyname('localhost') |
| 464 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 465 | self.assertEqual(self.output.getvalue(), mexpect) |
| 466 | debugout = smtpd.DEBUGSTREAM.getvalue() |
| 467 | sender = re.compile("^sender: the_rescuers@Rescue-Aid-Society.com$", re.MULTILINE) |
| 468 | self.assertRegex(debugout, sender) |
| 469 | for addr in ('John', 'Dinsdale'): |
| 470 | to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr), |
| 471 | re.MULTILINE) |
| 472 | self.assertRegex(debugout, to_addr) |
| 473 | |
| 474 | def testSendMessageResent(self): |
| 475 | m = email.mime.text.MIMEText('A test message') |
| 476 | m['From'] = 'foo@bar.com' |
| 477 | m['To'] = 'John' |
| 478 | m['CC'] = 'Sally, Fred' |
| 479 | m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>' |
| 480 | m['Resent-Date'] = 'Thu, 1 Jan 1970 17:42:00 +0000' |
| 481 | m['Resent-From'] = 'holy@grail.net' |
| 482 | m['Resent-To'] = 'Martha <my_mom@great.cooker.com>, Jeff' |
| 483 | m['Resent-Bcc'] = 'doe@losthope.net' |
| 484 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 485 | smtp.send_message(m) |
| 486 | # XXX (see comment in testSend) |
| 487 | time.sleep(0.01) |
| 488 | smtp.quit() |
| 489 | |
| 490 | self.client_evt.set() |
| 491 | self.serv_evt.wait() |
| 492 | self.output.flush() |
| 493 | # The Resent-Bcc headers are deleted before serialization. |
| 494 | del m['Bcc'] |
| 495 | del m['Resent-Bcc'] |
| 496 | # Add the X-Peer header that DebuggingServer adds |
| 497 | m['X-Peer'] = socket.gethostbyname('localhost') |
| 498 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 499 | self.assertEqual(self.output.getvalue(), mexpect) |
| 500 | debugout = smtpd.DEBUGSTREAM.getvalue() |
| 501 | sender = re.compile("^sender: holy@grail.net$", re.MULTILINE) |
| 502 | self.assertRegex(debugout, sender) |
| 503 | for addr in ('my_mom@great.cooker.com', 'Jeff', 'doe@losthope.net'): |
| 504 | to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr), |
| 505 | re.MULTILINE) |
| 506 | self.assertRegex(debugout, to_addr) |
| 507 | |
| 508 | def testSendMessageMultipleResentRaises(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 | m['Resent-Date'] = 'Thu, 2 Jan 1970 17:42:00 +0000' |
| 519 | m['Resent-To'] = 'holy@grail.net' |
| 520 | m['Resent-From'] = 'Martha <my_mom@great.cooker.com>, Jeff' |
| 521 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 522 | with self.assertRaises(ValueError): |
| 523 | smtp.send_message(m) |
| 524 | smtp.close() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 525 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 526 | class NonConnectingTests(unittest.TestCase): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 527 | |
| 528 | def testNotConnected(self): |
| 529 | # Test various operations on an unconnected SMTP object that |
| 530 | # should raise exceptions (at present the attempt in SMTP.send |
| 531 | # to reference the nonexistent 'sock' attribute of the SMTP object |
| 532 | # causes an AttributeError) |
| 533 | smtp = smtplib.SMTP() |
| 534 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo) |
| 535 | self.assertRaises(smtplib.SMTPServerDisconnected, |
| 536 | smtp.send, 'test msg') |
| 537 | |
| 538 | def testNonnumericPort(self): |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 539 | # check that non-numeric port raises OSError |
Andrew Svetlov | 2ade6f2 | 2012-12-17 18:57:16 +0200 | [diff] [blame] | 540 | self.assertRaises(OSError, smtplib.SMTP, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 541 | "localhost", "bogus") |
Andrew Svetlov | 2ade6f2 | 2012-12-17 18:57:16 +0200 | [diff] [blame] | 542 | self.assertRaises(OSError, smtplib.SMTP, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 543 | "localhost:bogus") |
| 544 | |
| 545 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 546 | # test response of client to a non-successful HELO message |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 547 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 548 | class BadHELOServerTests(unittest.TestCase): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 549 | |
| 550 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 551 | smtplib.socket = mock_socket |
| 552 | mock_socket.reply_with(b"199 no hello for you!") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 553 | self.old_stdout = sys.stdout |
| 554 | self.output = io.StringIO() |
| 555 | sys.stdout = self.output |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 556 | self.port = 25 |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 557 | |
| 558 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 559 | smtplib.socket = socket |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 560 | sys.stdout = self.old_stdout |
| 561 | |
| 562 | def testFailingHELO(self): |
| 563 | self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 564 | HOST, self.port, 'localhost', 3) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 565 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 566 | |
Georg Brandl | b38b5c4 | 2014-02-10 22:11:21 +0100 | [diff] [blame] | 567 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 568 | class TooLongLineTests(unittest.TestCase): |
| 569 | respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n' |
| 570 | |
| 571 | def setUp(self): |
| 572 | self.old_stdout = sys.stdout |
| 573 | self.output = io.StringIO() |
| 574 | sys.stdout = self.output |
| 575 | |
| 576 | self.evt = threading.Event() |
| 577 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 578 | self.sock.settimeout(15) |
| 579 | self.port = support.bind_port(self.sock) |
| 580 | servargs = (self.evt, self.respdata, self.sock) |
| 581 | threading.Thread(target=server, args=servargs).start() |
| 582 | self.evt.wait() |
| 583 | self.evt.clear() |
| 584 | |
| 585 | def tearDown(self): |
| 586 | self.evt.wait() |
| 587 | sys.stdout = self.old_stdout |
| 588 | |
| 589 | def testLineTooLong(self): |
| 590 | self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP, |
| 591 | HOST, self.port, 'localhost', 3) |
| 592 | |
| 593 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 594 | sim_users = {'Mr.A@somewhere.com':'John A', |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 595 | 'Ms.B@xn--fo-fka.com':'Sally B', |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 596 | 'Mrs.C@somewhereesle.com':'Ruth C', |
| 597 | } |
| 598 | |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 599 | sim_auth = ('Mr.A@somewhere.com', 'somepassword') |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 600 | sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn' |
| 601 | 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=') |
| 602 | sim_auth_credentials = { |
| 603 | 'login': 'TXIuQUBzb21ld2hlcmUuY29t', |
| 604 | 'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=', |
| 605 | 'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ' |
| 606 | 'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'), |
| 607 | } |
| 608 | sim_auth_login_password = 'C29TZXBHC3N3B3JK' |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 609 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 610 | 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] | 611 | 'list-2':['Ms.B@xn--fo-fka.com',], |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | # Simulated SMTP channel & server |
| 615 | class SimSMTPChannel(smtpd.SMTPChannel): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 616 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 617 | quit_response = None |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 618 | mail_response = None |
| 619 | rcpt_response = None |
| 620 | data_response = None |
| 621 | rcpt_count = 0 |
| 622 | rset_count = 0 |
R David Murray | afb151a | 2014-04-14 18:21:38 -0400 | [diff] [blame] | 623 | disconnect = 0 |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 624 | |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 625 | def __init__(self, extra_features, *args, **kw): |
| 626 | self._extrafeatures = ''.join( |
| 627 | [ "250-{0}\r\n".format(x) for x in extra_features ]) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 628 | super(SimSMTPChannel, self).__init__(*args, **kw) |
| 629 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 630 | def smtp_EHLO(self, arg): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 631 | resp = ('250-testhost\r\n' |
| 632 | '250-EXPN\r\n' |
| 633 | '250-SIZE 20000000\r\n' |
| 634 | '250-STARTTLS\r\n' |
| 635 | '250-DELIVERBY\r\n') |
| 636 | resp = resp + self._extrafeatures + '250 HELP' |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 637 | self.push(resp) |
R David Murray | f1a40b4 | 2013-03-20 21:12:17 -0400 | [diff] [blame] | 638 | self.seen_greeting = arg |
| 639 | self.extended_smtp = True |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 640 | |
| 641 | def smtp_VRFY(self, arg): |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 642 | # For max compatibility smtplib should be sending the raw address. |
| 643 | if arg in sim_users: |
| 644 | self.push('250 %s %s' % (sim_users[arg], smtplib.quoteaddr(arg))) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 645 | else: |
| 646 | self.push('550 No such user: %s' % arg) |
| 647 | |
| 648 | def smtp_EXPN(self, arg): |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 649 | list_name = arg.lower() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 650 | if list_name in sim_lists: |
| 651 | user_list = sim_lists[list_name] |
| 652 | for n, user_email in enumerate(user_list): |
| 653 | quoted_addr = smtplib.quoteaddr(user_email) |
| 654 | if n < len(user_list) - 1: |
| 655 | self.push('250-%s %s' % (sim_users[user_email], quoted_addr)) |
| 656 | else: |
| 657 | self.push('250 %s %s' % (sim_users[user_email], quoted_addr)) |
| 658 | else: |
| 659 | self.push('550 No access for you!') |
| 660 | |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 661 | def smtp_AUTH(self, arg): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 662 | if arg.strip().lower()=='cram-md5': |
| 663 | self.push('334 {}'.format(sim_cram_md5_challenge)) |
| 664 | return |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 665 | mech, auth = arg.split() |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 666 | mech = mech.lower() |
| 667 | if mech not in sim_auth_credentials: |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 668 | self.push('504 auth type unimplemented') |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 669 | return |
| 670 | if mech == 'plain' and auth==sim_auth_credentials['plain']: |
| 671 | self.push('235 plain auth ok') |
| 672 | elif mech=='login' and auth==sim_auth_credentials['login']: |
| 673 | self.push('334 Password:') |
| 674 | else: |
| 675 | self.push('550 No access for you!') |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 676 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 677 | def smtp_QUIT(self, arg): |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 678 | if self.quit_response is None: |
| 679 | super(SimSMTPChannel, self).smtp_QUIT(arg) |
| 680 | else: |
| 681 | self.push(self.quit_response) |
| 682 | self.close_when_done() |
| 683 | |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 684 | def smtp_MAIL(self, arg): |
| 685 | if self.mail_response is None: |
| 686 | super().smtp_MAIL(arg) |
| 687 | else: |
| 688 | self.push(self.mail_response) |
R David Murray | afb151a | 2014-04-14 18:21:38 -0400 | [diff] [blame] | 689 | if self.disconnect: |
| 690 | self.close_when_done() |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 691 | |
| 692 | def smtp_RCPT(self, arg): |
| 693 | if self.rcpt_response is None: |
| 694 | super().smtp_RCPT(arg) |
| 695 | return |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 696 | self.rcpt_count += 1 |
R David Murray | 03b0116 | 2013-03-20 22:11:40 -0400 | [diff] [blame] | 697 | self.push(self.rcpt_response[self.rcpt_count-1]) |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 698 | |
| 699 | def smtp_RSET(self, arg): |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 700 | self.rset_count += 1 |
R David Murray | 03b0116 | 2013-03-20 22:11:40 -0400 | [diff] [blame] | 701 | super().smtp_RSET(arg) |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 702 | |
| 703 | def smtp_DATA(self, arg): |
| 704 | if self.data_response is None: |
| 705 | super().smtp_DATA(arg) |
| 706 | else: |
| 707 | self.push(self.data_response) |
| 708 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 709 | def handle_error(self): |
| 710 | raise |
| 711 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 712 | |
| 713 | class SimSMTPServer(smtpd.SMTPServer): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 714 | |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 715 | channel_class = SimSMTPChannel |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 716 | |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 717 | def __init__(self, *args, **kw): |
| 718 | self._extra_features = [] |
| 719 | smtpd.SMTPServer.__init__(self, *args, **kw) |
| 720 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 721 | def handle_accepted(self, conn, addr): |
R David Murray | f1a40b4 | 2013-03-20 21:12:17 -0400 | [diff] [blame] | 722 | self._SMTPchannel = self.channel_class( |
R David Murray | 1144da5 | 2014-06-11 12:27:40 -0400 | [diff] [blame^] | 723 | self._extra_features, self, conn, addr, |
| 724 | decode_data=self._decode_data) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 725 | |
| 726 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 727 | pass |
| 728 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 729 | def add_feature(self, feature): |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 730 | self._extra_features.append(feature) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 731 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 732 | def handle_error(self): |
| 733 | raise |
| 734 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 735 | |
| 736 | # Test various SMTP & ESMTP commands/behaviors that require a simulated server |
| 737 | # (i.e., something with more features than DebuggingServer) |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 738 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 739 | class SMTPSimTests(unittest.TestCase): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 740 | |
| 741 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 742 | self.real_getfqdn = socket.getfqdn |
| 743 | socket.getfqdn = mock_socket.getfqdn |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 744 | self.serv_evt = threading.Event() |
| 745 | self.client_evt = threading.Event() |
Antoine Pitrou | 043bad0 | 2010-04-30 23:20:15 +0000 | [diff] [blame] | 746 | # 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^] | 747 | self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1), decode_data=True) |
Antoine Pitrou | 043bad0 | 2010-04-30 23:20:15 +0000 | [diff] [blame] | 748 | # Keep a note of what port was assigned |
| 749 | self.port = self.serv.socket.getsockname()[1] |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 750 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 751 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 752 | self.thread.start() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 753 | |
| 754 | # wait until server thread has assigned a port number |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 755 | self.serv_evt.wait() |
| 756 | self.serv_evt.clear() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 757 | |
| 758 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 759 | socket.getfqdn = self.real_getfqdn |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 760 | # indicate that the client is finished |
| 761 | self.client_evt.set() |
| 762 | # wait for the server thread to terminate |
| 763 | self.serv_evt.wait() |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 764 | self.thread.join() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 765 | |
| 766 | def testBasic(self): |
| 767 | # smoke test |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 768 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 769 | smtp.quit() |
| 770 | |
| 771 | def testEHLO(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 772 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 773 | |
| 774 | # no features should be present before the EHLO |
| 775 | self.assertEqual(smtp.esmtp_features, {}) |
| 776 | |
| 777 | # features expected from the test server |
| 778 | expected_features = {'expn':'', |
| 779 | 'size': '20000000', |
| 780 | 'starttls': '', |
| 781 | 'deliverby': '', |
| 782 | 'help': '', |
| 783 | } |
| 784 | |
| 785 | smtp.ehlo() |
| 786 | self.assertEqual(smtp.esmtp_features, expected_features) |
| 787 | for k in expected_features: |
| 788 | self.assertTrue(smtp.has_extn(k)) |
| 789 | self.assertFalse(smtp.has_extn('unsupported-feature')) |
| 790 | smtp.quit() |
| 791 | |
| 792 | def testVRFY(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 793 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 794 | |
| 795 | for email, name in sim_users.items(): |
| 796 | expected_known = (250, bytes('%s %s' % |
Guido van Rossum | 5a23cc5 | 2007-08-30 14:02:43 +0000 | [diff] [blame] | 797 | (name, smtplib.quoteaddr(email)), |
| 798 | "ascii")) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 799 | self.assertEqual(smtp.vrfy(email), expected_known) |
| 800 | |
| 801 | u = 'nobody@nowhere.com' |
R David Murray | 4634676 | 2011-07-18 21:38:54 -0400 | [diff] [blame] | 802 | expected_unknown = (550, ('No such user: %s' % u).encode('ascii')) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 803 | self.assertEqual(smtp.vrfy(u), expected_unknown) |
| 804 | smtp.quit() |
| 805 | |
| 806 | def testEXPN(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 807 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 808 | |
| 809 | for listname, members in sim_lists.items(): |
| 810 | users = [] |
| 811 | for m in members: |
| 812 | users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m))) |
Guido van Rossum | 5a23cc5 | 2007-08-30 14:02:43 +0000 | [diff] [blame] | 813 | expected_known = (250, bytes('\n'.join(users), "ascii")) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 814 | self.assertEqual(smtp.expn(listname), expected_known) |
| 815 | |
| 816 | u = 'PSU-Members-List' |
| 817 | expected_unknown = (550, b'No access for you!') |
| 818 | self.assertEqual(smtp.expn(u), expected_unknown) |
| 819 | smtp.quit() |
| 820 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 821 | def testAUTH_PLAIN(self): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 822 | self.serv.add_feature("AUTH PLAIN") |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 823 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 824 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 825 | expected_auth_ok = (235, b'plain auth ok') |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 826 | self.assertEqual(smtp.login(sim_auth[0], sim_auth[1]), expected_auth_ok) |
Benjamin Peterson | d094efd | 2010-10-31 17:15:42 +0000 | [diff] [blame] | 827 | smtp.close() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 828 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 829 | # SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they |
| 830 | # require a synchronous read to obtain the credentials...so instead smtpd |
| 831 | # sees the credential sent by smtplib's login method as an unknown command, |
| 832 | # which results in smtplib raising an auth error. Fortunately the error |
| 833 | # message contains the encoded credential, so we can partially check that it |
| 834 | # was generated correctly (partially, because the 'word' is uppercased in |
| 835 | # the error message). |
| 836 | |
| 837 | def testAUTH_LOGIN(self): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 838 | self.serv.add_feature("AUTH LOGIN") |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 839 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 840 | try: smtp.login(sim_auth[0], sim_auth[1]) |
| 841 | except smtplib.SMTPAuthenticationError as err: |
Benjamin Peterson | 9595166 | 2010-10-31 17:59:20 +0000 | [diff] [blame] | 842 | self.assertIn(sim_auth_login_password, str(err)) |
Benjamin Peterson | d094efd | 2010-10-31 17:15:42 +0000 | [diff] [blame] | 843 | smtp.close() |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 844 | |
| 845 | def testAUTH_CRAM_MD5(self): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 846 | self.serv.add_feature("AUTH CRAM-MD5") |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 847 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 848 | |
| 849 | try: smtp.login(sim_auth[0], sim_auth[1]) |
| 850 | except smtplib.SMTPAuthenticationError as err: |
Benjamin Peterson | 9595166 | 2010-10-31 17:59:20 +0000 | [diff] [blame] | 851 | self.assertIn(sim_auth_credentials['cram-md5'], str(err)) |
Benjamin Peterson | d094efd | 2010-10-31 17:15:42 +0000 | [diff] [blame] | 852 | smtp.close() |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 853 | |
Andrew Kuchling | 7859182 | 2013-11-11 14:03:23 -0500 | [diff] [blame] | 854 | def testAUTH_multiple(self): |
| 855 | # Test that multiple authentication methods are tried. |
| 856 | self.serv.add_feature("AUTH BOGUS PLAIN LOGIN CRAM-MD5") |
| 857 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
| 858 | try: smtp.login(sim_auth[0], sim_auth[1]) |
| 859 | except smtplib.SMTPAuthenticationError as err: |
| 860 | self.assertIn(sim_auth_login_password, str(err)) |
| 861 | smtp.close() |
| 862 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 863 | def test_with_statement(self): |
| 864 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 865 | code, message = smtp.noop() |
| 866 | self.assertEqual(code, 250) |
| 867 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo') |
| 868 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 869 | smtp.close() |
| 870 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo') |
| 871 | |
| 872 | def test_with_statement_QUIT_failure(self): |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 873 | with self.assertRaises(smtplib.SMTPResponseException) as error: |
| 874 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 875 | smtp.noop() |
R David Murray | 6bd5202 | 2013-03-21 00:32:31 -0400 | [diff] [blame] | 876 | self.serv._SMTPchannel.quit_response = '421 QUIT FAILED' |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 877 | self.assertEqual(error.exception.smtp_code, 421) |
| 878 | self.assertEqual(error.exception.smtp_error, b'QUIT FAILED') |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 879 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 880 | #TODO: add tests for correct AUTH method fallback now that the |
| 881 | #test infrastructure can support it. |
| 882 | |
R David Murray | afb151a | 2014-04-14 18:21:38 -0400 | [diff] [blame] | 883 | # Issue 17498: make sure _rset does not raise SMTPServerDisconnected exception |
| 884 | def test__rest_from_mail_cmd(self): |
| 885 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
| 886 | smtp.noop() |
| 887 | self.serv._SMTPchannel.mail_response = '451 Requested action aborted' |
| 888 | self.serv._SMTPchannel.disconnect = True |
| 889 | with self.assertRaises(smtplib.SMTPSenderRefused): |
| 890 | smtp.sendmail('John', 'Sally', 'test message') |
| 891 | self.assertIsNone(smtp.sock) |
| 892 | |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 893 | # Issue 5713: make sure close, not rset, is called if we get a 421 error |
| 894 | def test_421_from_mail_cmd(self): |
| 895 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R David Murray | 853c0f9 | 2013-03-20 21:54:05 -0400 | [diff] [blame] | 896 | smtp.noop() |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 897 | self.serv._SMTPchannel.mail_response = '421 closing connection' |
| 898 | with self.assertRaises(smtplib.SMTPSenderRefused): |
| 899 | smtp.sendmail('John', 'Sally', 'test message') |
| 900 | self.assertIsNone(smtp.sock) |
R David Murray | 03b0116 | 2013-03-20 22:11:40 -0400 | [diff] [blame] | 901 | self.assertEqual(self.serv._SMTPchannel.rset_count, 0) |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 902 | |
| 903 | def test_421_from_rcpt_cmd(self): |
| 904 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R David Murray | 853c0f9 | 2013-03-20 21:54:05 -0400 | [diff] [blame] | 905 | smtp.noop() |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 906 | self.serv._SMTPchannel.rcpt_response = ['250 accepted', '421 closing'] |
| 907 | with self.assertRaises(smtplib.SMTPRecipientsRefused) as r: |
| 908 | smtp.sendmail('John', ['Sally', 'Frank', 'George'], 'test message') |
| 909 | self.assertIsNone(smtp.sock) |
| 910 | self.assertEqual(self.serv._SMTPchannel.rset_count, 0) |
| 911 | self.assertDictEqual(r.exception.args[0], {'Frank': (421, b'closing')}) |
| 912 | |
| 913 | def test_421_from_data_cmd(self): |
| 914 | class MySimSMTPChannel(SimSMTPChannel): |
| 915 | def found_terminator(self): |
| 916 | if self.smtp_state == self.DATA: |
| 917 | self.push('421 closing') |
| 918 | else: |
| 919 | super().found_terminator() |
| 920 | self.serv.channel_class = MySimSMTPChannel |
| 921 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R David Murray | 853c0f9 | 2013-03-20 21:54:05 -0400 | [diff] [blame] | 922 | smtp.noop() |
R David Murray | d312c74 | 2013-03-20 20:36:14 -0400 | [diff] [blame] | 923 | with self.assertRaises(smtplib.SMTPDataError): |
| 924 | smtp.sendmail('John@foo.org', ['Sally@foo.org'], 'test message') |
| 925 | self.assertIsNone(smtp.sock) |
| 926 | self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0) |
| 927 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | d54fa55 | 2011-08-28 01:23:52 +0200 | [diff] [blame] | 929 | @support.reap_threads |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 930 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 931 | support.run_unittest(GeneralTests, DebuggingServerTests, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 932 | NonConnectingTests, |
Georg Brandl | b38b5c4 | 2014-02-10 22:11:21 +0100 | [diff] [blame] | 933 | BadHELOServerTests, SMTPSimTests, |
| 934 | TooLongLineTests) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 935 | |
| 936 | if __name__ == '__main__': |
| 937 | test_main() |