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