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 | |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 172 | self._threads = support.threading_setup() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 173 | self.serv_evt = threading.Event() |
| 174 | self.client_evt = threading.Event() |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 175 | # Capture SMTPChannel debug output |
| 176 | self.old_DEBUGSTREAM = smtpd.DEBUGSTREAM |
| 177 | smtpd.DEBUGSTREAM = io.StringIO() |
Antoine Pitrou | 043bad0 | 2010-04-30 23:20:15 +0000 | [diff] [blame] | 178 | # Pick a random unused port by passing 0 for the port number |
| 179 | self.serv = smtpd.DebuggingServer((HOST, 0), ('nowhere', -1)) |
| 180 | # Keep a note of what port was assigned |
| 181 | self.port = self.serv.socket.getsockname()[1] |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 182 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 183 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 184 | self.thread.start() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 185 | |
| 186 | # wait until server thread has assigned a port number |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 187 | self.serv_evt.wait() |
| 188 | self.serv_evt.clear() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 189 | |
| 190 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 191 | socket.getfqdn = self.real_getfqdn |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 192 | # indicate that the client is finished |
| 193 | self.client_evt.set() |
| 194 | # wait for the server thread to terminate |
| 195 | self.serv_evt.wait() |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 196 | self.thread.join() |
| 197 | support.threading_cleanup(*self._threads) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 198 | # restore sys.stdout |
| 199 | sys.stdout = self.old_stdout |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 200 | # restore DEBUGSTREAM |
| 201 | smtpd.DEBUGSTREAM.close() |
| 202 | smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 203 | |
| 204 | def testBasic(self): |
| 205 | # connect |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 206 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 207 | smtp.quit() |
| 208 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 209 | def testNOOP(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 210 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 211 | expected = (250, b'Ok') |
| 212 | self.assertEqual(smtp.noop(), expected) |
| 213 | smtp.quit() |
| 214 | |
| 215 | def testRSET(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 216 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 217 | expected = (250, b'Ok') |
| 218 | self.assertEqual(smtp.rset(), expected) |
| 219 | smtp.quit() |
| 220 | |
| 221 | def testNotImplemented(self): |
| 222 | # EHLO isn't implemented in DebuggingServer |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 223 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 224 | expected = (502, b'Error: command "EHLO" not implemented') |
| 225 | self.assertEqual(smtp.ehlo(), expected) |
| 226 | smtp.quit() |
| 227 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 228 | def testVRFY(self): |
| 229 | # VRFY isn't implemented in DebuggingServer |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 230 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 231 | expected = (502, b'Error: command "VRFY" not implemented') |
| 232 | self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected) |
| 233 | self.assertEqual(smtp.verify('nobody@nowhere.com'), expected) |
| 234 | smtp.quit() |
| 235 | |
| 236 | def testSecondHELO(self): |
| 237 | # check that a second HELO returns a message that it's a duplicate |
| 238 | # (this behavior is specific to smtpd.SMTPChannel) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 239 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 240 | smtp.helo() |
| 241 | expected = (503, b'Duplicate HELO/EHLO') |
| 242 | self.assertEqual(smtp.helo(), expected) |
| 243 | smtp.quit() |
| 244 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 245 | def testHELP(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 246 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 247 | self.assertEqual(smtp.help(), b'Error: command "HELP" not implemented') |
| 248 | smtp.quit() |
| 249 | |
| 250 | def testSend(self): |
| 251 | # connect and send mail |
| 252 | m = 'A test message' |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 253 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 254 | smtp.sendmail('John', 'Sally', m) |
Neal Norwitz | 2532967 | 2008-08-25 03:55:03 +0000 | [diff] [blame] | 255 | # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor |
| 256 | # in asyncore. This sleep might help, but should really be fixed |
| 257 | # properly by using an Event variable. |
| 258 | time.sleep(0.01) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 259 | smtp.quit() |
| 260 | |
| 261 | self.client_evt.set() |
| 262 | self.serv_evt.wait() |
| 263 | self.output.flush() |
| 264 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END) |
| 265 | self.assertEqual(self.output.getvalue(), mexpect) |
| 266 | |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 267 | def testSendBinary(self): |
| 268 | m = b'A test message' |
| 269 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 270 | smtp.sendmail('John', 'Sally', m) |
| 271 | # XXX (see comment in testSend) |
| 272 | time.sleep(0.01) |
| 273 | smtp.quit() |
| 274 | |
| 275 | self.client_evt.set() |
| 276 | self.serv_evt.wait() |
| 277 | self.output.flush() |
| 278 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.decode('ascii'), MSG_END) |
| 279 | self.assertEqual(self.output.getvalue(), mexpect) |
| 280 | |
R David Murray | 0f663d0 | 2011-06-09 15:05:57 -0400 | [diff] [blame] | 281 | def testSendNeedingDotQuote(self): |
| 282 | # Issue 12283 |
| 283 | m = '.A test\n.mes.sage.' |
| 284 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 285 | smtp.sendmail('John', 'Sally', m) |
| 286 | # XXX (see comment in testSend) |
| 287 | time.sleep(0.01) |
| 288 | smtp.quit() |
| 289 | |
| 290 | self.client_evt.set() |
| 291 | self.serv_evt.wait() |
| 292 | self.output.flush() |
| 293 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END) |
| 294 | self.assertEqual(self.output.getvalue(), mexpect) |
| 295 | |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 296 | def testSendMessage(self): |
| 297 | m = email.mime.text.MIMEText('A test message') |
| 298 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 299 | smtp.send_message(m, from_addr='John', to_addrs='Sally') |
| 300 | # XXX (see comment in testSend) |
| 301 | time.sleep(0.01) |
| 302 | smtp.quit() |
| 303 | |
| 304 | self.client_evt.set() |
| 305 | self.serv_evt.wait() |
| 306 | self.output.flush() |
| 307 | # Add the X-Peer header that DebuggingServer adds |
R David Murray | b912c5a | 2011-05-02 08:47:24 -0400 | [diff] [blame] | 308 | m['X-Peer'] = socket.gethostbyname('localhost') |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 309 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 310 | self.assertEqual(self.output.getvalue(), mexpect) |
| 311 | |
| 312 | def testSendMessageWithAddresses(self): |
| 313 | m = email.mime.text.MIMEText('A test message') |
| 314 | m['From'] = 'foo@bar.com' |
| 315 | m['To'] = 'John' |
| 316 | m['CC'] = 'Sally, Fred' |
| 317 | m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" <warped@silly.walks.com>' |
| 318 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 319 | smtp.send_message(m) |
| 320 | # XXX (see comment in testSend) |
| 321 | time.sleep(0.01) |
| 322 | smtp.quit() |
| 323 | |
| 324 | self.client_evt.set() |
| 325 | self.serv_evt.wait() |
| 326 | self.output.flush() |
| 327 | # Add the X-Peer header that DebuggingServer adds |
R David Murray | b912c5a | 2011-05-02 08:47:24 -0400 | [diff] [blame] | 328 | m['X-Peer'] = socket.gethostbyname('localhost') |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 329 | # The Bcc header is deleted before serialization. |
| 330 | del m['Bcc'] |
| 331 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 332 | self.assertEqual(self.output.getvalue(), mexpect) |
| 333 | debugout = smtpd.DEBUGSTREAM.getvalue() |
| 334 | sender = re.compile("^sender: foo@bar.com$", re.MULTILINE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 335 | self.assertRegex(debugout, sender) |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 336 | for addr in ('John', 'Sally', 'Fred', 'root@localhost', |
| 337 | 'warped@silly.walks.com'): |
| 338 | to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr), |
| 339 | re.MULTILINE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 340 | self.assertRegex(debugout, to_addr) |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 341 | |
| 342 | def testSendMessageWithSomeAddresses(self): |
| 343 | # Make sure nothing breaks if not all of the three 'to' headers exist |
| 344 | m = email.mime.text.MIMEText('A test message') |
| 345 | m['From'] = 'foo@bar.com' |
| 346 | m['To'] = 'John, Dinsdale' |
| 347 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
| 348 | smtp.send_message(m) |
| 349 | # XXX (see comment in testSend) |
| 350 | time.sleep(0.01) |
| 351 | smtp.quit() |
| 352 | |
| 353 | self.client_evt.set() |
| 354 | self.serv_evt.wait() |
| 355 | self.output.flush() |
| 356 | # Add the X-Peer header that DebuggingServer adds |
R David Murray | b912c5a | 2011-05-02 08:47:24 -0400 | [diff] [blame] | 357 | m['X-Peer'] = socket.gethostbyname('localhost') |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 358 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m.as_string(), MSG_END) |
| 359 | self.assertEqual(self.output.getvalue(), mexpect) |
| 360 | debugout = smtpd.DEBUGSTREAM.getvalue() |
| 361 | sender = re.compile("^sender: foo@bar.com$", re.MULTILINE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 362 | self.assertRegex(debugout, sender) |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 363 | for addr in ('John', 'Dinsdale'): |
| 364 | to_addr = re.compile(r"^recips: .*'{}'.*$".format(addr), |
| 365 | re.MULTILINE) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 366 | self.assertRegex(debugout, to_addr) |
R. David Murray | 7dff9e0 | 2010-11-08 17:15:13 +0000 | [diff] [blame] | 367 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 368 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 369 | class NonConnectingTests(unittest.TestCase): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 370 | |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 371 | def setUp(self): |
| 372 | smtplib.socket = mock_socket |
| 373 | |
| 374 | def tearDown(self): |
| 375 | smtplib.socket = socket |
| 376 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 377 | def testNotConnected(self): |
| 378 | # Test various operations on an unconnected SMTP object that |
| 379 | # should raise exceptions (at present the attempt in SMTP.send |
| 380 | # to reference the nonexistent 'sock' attribute of the SMTP object |
| 381 | # causes an AttributeError) |
| 382 | smtp = smtplib.SMTP() |
| 383 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo) |
| 384 | self.assertRaises(smtplib.SMTPServerDisconnected, |
| 385 | smtp.send, 'test msg') |
| 386 | |
| 387 | def testNonnumericPort(self): |
| 388 | # check that non-numeric port raises socket.error |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 389 | self.assertRaises(mock_socket.error, smtplib.SMTP, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 390 | "localhost", "bogus") |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 391 | self.assertRaises(mock_socket.error, smtplib.SMTP, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 392 | "localhost:bogus") |
| 393 | |
| 394 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 395 | # test response of client to a non-successful HELO message |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 396 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 397 | class BadHELOServerTests(unittest.TestCase): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 398 | |
| 399 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 400 | smtplib.socket = mock_socket |
| 401 | mock_socket.reply_with(b"199 no hello for you!") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 402 | self.old_stdout = sys.stdout |
| 403 | self.output = io.StringIO() |
| 404 | sys.stdout = self.output |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 405 | self.port = 25 |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 406 | |
| 407 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 408 | smtplib.socket = socket |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 409 | sys.stdout = self.old_stdout |
| 410 | |
| 411 | def testFailingHELO(self): |
| 412 | self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 413 | HOST, self.port, 'localhost', 3) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 415 | |
| 416 | sim_users = {'Mr.A@somewhere.com':'John A', |
| 417 | 'Ms.B@somewhere.com':'Sally B', |
| 418 | 'Mrs.C@somewhereesle.com':'Ruth C', |
| 419 | } |
| 420 | |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 421 | sim_auth = ('Mr.A@somewhere.com', 'somepassword') |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 422 | sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn' |
| 423 | 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=') |
| 424 | sim_auth_credentials = { |
| 425 | 'login': 'TXIuQUBzb21ld2hlcmUuY29t', |
| 426 | 'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=', |
| 427 | 'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ' |
| 428 | 'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'), |
| 429 | } |
| 430 | sim_auth_login_password = 'C29TZXBHC3N3B3JK' |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 431 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 432 | sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'], |
| 433 | 'list-2':['Ms.B@somewhere.com',], |
| 434 | } |
| 435 | |
| 436 | # Simulated SMTP channel & server |
| 437 | class SimSMTPChannel(smtpd.SMTPChannel): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 438 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 439 | # For testing failures in QUIT when using the context manager API. |
| 440 | quit_response = None |
| 441 | |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 442 | def __init__(self, extra_features, *args, **kw): |
| 443 | self._extrafeatures = ''.join( |
| 444 | [ "250-{0}\r\n".format(x) for x in extra_features ]) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 445 | super(SimSMTPChannel, self).__init__(*args, **kw) |
| 446 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 447 | def smtp_EHLO(self, arg): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 448 | resp = ('250-testhost\r\n' |
| 449 | '250-EXPN\r\n' |
| 450 | '250-SIZE 20000000\r\n' |
| 451 | '250-STARTTLS\r\n' |
| 452 | '250-DELIVERBY\r\n') |
| 453 | resp = resp + self._extrafeatures + '250 HELP' |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 454 | self.push(resp) |
| 455 | |
| 456 | def smtp_VRFY(self, arg): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 457 | raw_addr = email.utils.parseaddr(arg)[1] |
| 458 | quoted_addr = smtplib.quoteaddr(arg) |
| 459 | if raw_addr in sim_users: |
| 460 | self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr)) |
| 461 | else: |
| 462 | self.push('550 No such user: %s' % arg) |
| 463 | |
| 464 | def smtp_EXPN(self, arg): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 465 | list_name = email.utils.parseaddr(arg)[1].lower() |
| 466 | if list_name in sim_lists: |
| 467 | user_list = sim_lists[list_name] |
| 468 | for n, user_email in enumerate(user_list): |
| 469 | quoted_addr = smtplib.quoteaddr(user_email) |
| 470 | if n < len(user_list) - 1: |
| 471 | self.push('250-%s %s' % (sim_users[user_email], quoted_addr)) |
| 472 | else: |
| 473 | self.push('250 %s %s' % (sim_users[user_email], quoted_addr)) |
| 474 | else: |
| 475 | self.push('550 No access for you!') |
| 476 | |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 477 | def smtp_AUTH(self, arg): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 478 | if arg.strip().lower()=='cram-md5': |
| 479 | self.push('334 {}'.format(sim_cram_md5_challenge)) |
| 480 | return |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 481 | mech, auth = arg.split() |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 482 | mech = mech.lower() |
| 483 | if mech not in sim_auth_credentials: |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 484 | self.push('504 auth type unimplemented') |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 485 | return |
| 486 | if mech == 'plain' and auth==sim_auth_credentials['plain']: |
| 487 | self.push('235 plain auth ok') |
| 488 | elif mech=='login' and auth==sim_auth_credentials['login']: |
| 489 | self.push('334 Password:') |
| 490 | else: |
| 491 | self.push('550 No access for you!') |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 492 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 493 | def smtp_QUIT(self, arg): |
| 494 | # args is ignored |
| 495 | if self.quit_response is None: |
| 496 | super(SimSMTPChannel, self).smtp_QUIT(arg) |
| 497 | else: |
| 498 | self.push(self.quit_response) |
| 499 | self.close_when_done() |
| 500 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 501 | def handle_error(self): |
| 502 | raise |
| 503 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 504 | |
| 505 | class SimSMTPServer(smtpd.SMTPServer): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 506 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 507 | # For testing failures in QUIT when using the context manager API. |
| 508 | quit_response = None |
| 509 | |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 510 | def __init__(self, *args, **kw): |
| 511 | self._extra_features = [] |
| 512 | smtpd.SMTPServer.__init__(self, *args, **kw) |
| 513 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 514 | def handle_accepted(self, conn, addr): |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 515 | self._SMTPchannel = SimSMTPChannel( |
| 516 | self._extra_features, self, conn, addr) |
| 517 | self._SMTPchannel.quit_response = self.quit_response |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 518 | |
| 519 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 520 | pass |
| 521 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 522 | def add_feature(self, feature): |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 523 | self._extra_features.append(feature) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 524 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 525 | def handle_error(self): |
| 526 | raise |
| 527 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 528 | |
| 529 | # Test various SMTP & ESMTP commands/behaviors that require a simulated server |
| 530 | # (i.e., something with more features than DebuggingServer) |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 531 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 532 | class SMTPSimTests(unittest.TestCase): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 533 | |
| 534 | def setUp(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 535 | self.real_getfqdn = socket.getfqdn |
| 536 | socket.getfqdn = mock_socket.getfqdn |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 537 | self._threads = support.threading_setup() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 538 | self.serv_evt = threading.Event() |
| 539 | self.client_evt = threading.Event() |
Antoine Pitrou | 043bad0 | 2010-04-30 23:20:15 +0000 | [diff] [blame] | 540 | # Pick a random unused port by passing 0 for the port number |
| 541 | self.serv = SimSMTPServer((HOST, 0), ('nowhere', -1)) |
| 542 | # Keep a note of what port was assigned |
| 543 | self.port = self.serv.socket.getsockname()[1] |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 544 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 545 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 546 | self.thread.start() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 547 | |
| 548 | # wait until server thread has assigned a port number |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 549 | self.serv_evt.wait() |
| 550 | self.serv_evt.clear() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 551 | |
| 552 | def tearDown(self): |
Richard Jones | 64b02de | 2010-08-03 06:39:33 +0000 | [diff] [blame] | 553 | socket.getfqdn = self.real_getfqdn |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 554 | # indicate that the client is finished |
| 555 | self.client_evt.set() |
| 556 | # wait for the server thread to terminate |
| 557 | self.serv_evt.wait() |
Antoine Pitrou | c3d4772 | 2009-10-27 19:49:45 +0000 | [diff] [blame] | 558 | self.thread.join() |
| 559 | support.threading_cleanup(*self._threads) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 560 | |
| 561 | def testBasic(self): |
| 562 | # smoke test |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 563 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 564 | smtp.quit() |
| 565 | |
| 566 | def testEHLO(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 567 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 568 | |
| 569 | # no features should be present before the EHLO |
| 570 | self.assertEqual(smtp.esmtp_features, {}) |
| 571 | |
| 572 | # features expected from the test server |
| 573 | expected_features = {'expn':'', |
| 574 | 'size': '20000000', |
| 575 | 'starttls': '', |
| 576 | 'deliverby': '', |
| 577 | 'help': '', |
| 578 | } |
| 579 | |
| 580 | smtp.ehlo() |
| 581 | self.assertEqual(smtp.esmtp_features, expected_features) |
| 582 | for k in expected_features: |
| 583 | self.assertTrue(smtp.has_extn(k)) |
| 584 | self.assertFalse(smtp.has_extn('unsupported-feature')) |
| 585 | smtp.quit() |
| 586 | |
| 587 | def testVRFY(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 588 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 589 | |
| 590 | for email, name in sim_users.items(): |
| 591 | expected_known = (250, bytes('%s %s' % |
Guido van Rossum | 5a23cc5 | 2007-08-30 14:02:43 +0000 | [diff] [blame] | 592 | (name, smtplib.quoteaddr(email)), |
| 593 | "ascii")) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 594 | self.assertEqual(smtp.vrfy(email), expected_known) |
| 595 | |
| 596 | u = 'nobody@nowhere.com' |
Thomas Wouters | 74e68c7 | 2007-08-31 00:20:14 +0000 | [diff] [blame] | 597 | expected_unknown = (550, ('No such user: %s' |
| 598 | % smtplib.quoteaddr(u)).encode('ascii')) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 599 | self.assertEqual(smtp.vrfy(u), expected_unknown) |
| 600 | smtp.quit() |
| 601 | |
| 602 | def testEXPN(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 603 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 604 | |
| 605 | for listname, members in sim_lists.items(): |
| 606 | users = [] |
| 607 | for m in members: |
| 608 | users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m))) |
Guido van Rossum | 5a23cc5 | 2007-08-30 14:02:43 +0000 | [diff] [blame] | 609 | expected_known = (250, bytes('\n'.join(users), "ascii")) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 610 | self.assertEqual(smtp.expn(listname), expected_known) |
| 611 | |
| 612 | u = 'PSU-Members-List' |
| 613 | expected_unknown = (550, b'No access for you!') |
| 614 | self.assertEqual(smtp.expn(u), expected_unknown) |
| 615 | smtp.quit() |
| 616 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 617 | def testAUTH_PLAIN(self): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 618 | self.serv.add_feature("AUTH PLAIN") |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 619 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 620 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 621 | expected_auth_ok = (235, b'plain auth ok') |
R. David Murray | caa27b7 | 2009-05-23 18:49:56 +0000 | [diff] [blame] | 622 | 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] | 623 | smtp.close() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 624 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 625 | # SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they |
| 626 | # require a synchronous read to obtain the credentials...so instead smtpd |
| 627 | # sees the credential sent by smtplib's login method as an unknown command, |
| 628 | # which results in smtplib raising an auth error. Fortunately the error |
| 629 | # message contains the encoded credential, so we can partially check that it |
| 630 | # was generated correctly (partially, because the 'word' is uppercased in |
| 631 | # the error message). |
| 632 | |
| 633 | def testAUTH_LOGIN(self): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 634 | self.serv.add_feature("AUTH LOGIN") |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 635 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 636 | try: smtp.login(sim_auth[0], sim_auth[1]) |
| 637 | except smtplib.SMTPAuthenticationError as err: |
Benjamin Peterson | 9595166 | 2010-10-31 17:59:20 +0000 | [diff] [blame] | 638 | self.assertIn(sim_auth_login_password, str(err)) |
Benjamin Peterson | d094efd | 2010-10-31 17:15:42 +0000 | [diff] [blame] | 639 | smtp.close() |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 640 | |
| 641 | def testAUTH_CRAM_MD5(self): |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 642 | self.serv.add_feature("AUTH CRAM-MD5") |
R. David Murray | 23ddc0e | 2009-05-29 18:03:16 +0000 | [diff] [blame] | 643 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 644 | |
| 645 | try: smtp.login(sim_auth[0], sim_auth[1]) |
| 646 | except smtplib.SMTPAuthenticationError as err: |
Benjamin Peterson | 9595166 | 2010-10-31 17:59:20 +0000 | [diff] [blame] | 647 | self.assertIn(sim_auth_credentials['cram-md5'], str(err)) |
Benjamin Peterson | d094efd | 2010-10-31 17:15:42 +0000 | [diff] [blame] | 648 | smtp.close() |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 649 | |
Barry Warsaw | 1f5c958 | 2011-03-15 15:04:44 -0400 | [diff] [blame] | 650 | def test_with_statement(self): |
| 651 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 652 | code, message = smtp.noop() |
| 653 | self.assertEqual(code, 250) |
| 654 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo') |
| 655 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 656 | smtp.close() |
| 657 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.send, b'foo') |
| 658 | |
| 659 | def test_with_statement_QUIT_failure(self): |
| 660 | self.serv.quit_response = '421 QUIT FAILED' |
| 661 | with self.assertRaises(smtplib.SMTPResponseException) as error: |
| 662 | with smtplib.SMTP(HOST, self.port) as smtp: |
| 663 | smtp.noop() |
| 664 | self.assertEqual(error.exception.smtp_code, 421) |
| 665 | self.assertEqual(error.exception.smtp_error, b'QUIT FAILED') |
| 666 | # We don't need to clean up self.serv.quit_response because a new |
| 667 | # server is always instantiated in the setUp(). |
| 668 | |
R. David Murray | fb12391 | 2009-05-28 18:19:00 +0000 | [diff] [blame] | 669 | #TODO: add tests for correct AUTH method fallback now that the |
| 670 | #test infrastructure can support it. |
| 671 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 672 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 673 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 674 | support.run_unittest(GeneralTests, DebuggingServerTests, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 675 | NonConnectingTests, |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 676 | BadHELOServerTests, SMTPSimTests) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 677 | |
| 678 | if __name__ == '__main__': |
| 679 | test_main() |