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