Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 1 | import asyncore |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 2 | import email.utils |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 3 | import socket |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 4 | import smtpd |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 5 | import smtplib |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 6 | import StringIO |
| 7 | import sys |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 8 | import time |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 9 | import select |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 10 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame^] | 11 | import unittest |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 12 | from test import test_support |
| 13 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame^] | 14 | try: |
| 15 | import threading |
| 16 | except ImportError: |
| 17 | threading = None |
| 18 | |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 19 | HOST = test_support.HOST |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 20 | |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 21 | def server(evt, buf, serv): |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 22 | serv.listen(5) |
| 23 | evt.set() |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 24 | try: |
| 25 | conn, addr = serv.accept() |
| 26 | except socket.timeout: |
| 27 | pass |
| 28 | else: |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 29 | n = 500 |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 30 | while buf and n > 0: |
| 31 | r, w, e = select.select([], [conn], []) |
| 32 | if w: |
| 33 | sent = conn.send(buf) |
| 34 | buf = buf[sent:] |
| 35 | |
| 36 | n -= 1 |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 37 | |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 38 | conn.close() |
| 39 | finally: |
| 40 | serv.close() |
| 41 | evt.set() |
| 42 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame^] | 43 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 44 | class GeneralTests(unittest.TestCase): |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 45 | |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 46 | def setUp(self): |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 47 | self._threads = test_support.threading_setup() |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 48 | self.evt = threading.Event() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 49 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 50 | self.sock.settimeout(15) |
| 51 | self.port = test_support.bind_port(self.sock) |
| 52 | servargs = (self.evt, "220 Hola mundo\n", self.sock) |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 53 | self.thread = threading.Thread(target=server, args=servargs) |
| 54 | self.thread.start() |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 55 | self.evt.wait() |
| 56 | self.evt.clear() |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 57 | |
| 58 | def tearDown(self): |
| 59 | self.evt.wait() |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 60 | self.thread.join() |
| 61 | test_support.threading_cleanup(*self._threads) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 62 | |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 63 | def testBasic1(self): |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 64 | # connects |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 65 | smtp = smtplib.SMTP(HOST, self.port) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 66 | smtp.close() |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 67 | |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 68 | def testBasic2(self): |
| 69 | # connects, include port in host name |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 70 | smtp = smtplib.SMTP("%s:%s" % (HOST, self.port)) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 71 | smtp.close() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 72 | |
| 73 | def testLocalHostName(self): |
| 74 | # check that supplied local_hostname is used |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 75 | smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost") |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 76 | self.assertEqual(smtp.local_hostname, "testhost") |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 77 | smtp.close() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 78 | |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 79 | def testTimeoutDefault(self): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 80 | self.assertTrue(socket.getdefaulttimeout() is None) |
| 81 | socket.setdefaulttimeout(30) |
| 82 | try: |
| 83 | smtp = smtplib.SMTP(HOST, self.port) |
| 84 | finally: |
| 85 | socket.setdefaulttimeout(None) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 86 | self.assertEqual(smtp.sock.gettimeout(), 30) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 87 | smtp.close() |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 88 | |
| 89 | def testTimeoutNone(self): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 90 | self.assertTrue(socket.getdefaulttimeout() is None) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 91 | socket.setdefaulttimeout(30) |
| 92 | try: |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 93 | smtp = smtplib.SMTP(HOST, self.port, timeout=None) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 94 | finally: |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 95 | socket.setdefaulttimeout(None) |
| 96 | self.assertTrue(smtp.sock.gettimeout() is None) |
| 97 | smtp.close() |
| 98 | |
| 99 | def testTimeoutValue(self): |
| 100 | smtp = smtplib.SMTP(HOST, self.port, timeout=30) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 101 | self.assertEqual(smtp.sock.gettimeout(), 30) |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 102 | smtp.close() |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 103 | |
| 104 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 105 | # Test server thread using the specified SMTP server class |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 106 | def debugging_server(serv, serv_evt, client_evt): |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 107 | serv_evt.set() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 108 | |
| 109 | try: |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 110 | if hasattr(select, 'poll'): |
| 111 | poll_fun = asyncore.poll2 |
| 112 | else: |
| 113 | poll_fun = asyncore.poll |
| 114 | |
| 115 | n = 1000 |
| 116 | while asyncore.socket_map and n > 0: |
| 117 | poll_fun(0.01, asyncore.socket_map) |
| 118 | |
| 119 | # when the client conversation is finished, it will |
| 120 | # set client_evt, and it's then ok to kill the server |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 121 | if client_evt.is_set(): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 122 | serv.close() |
| 123 | break |
| 124 | |
| 125 | n -= 1 |
| 126 | |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 127 | except socket.timeout: |
| 128 | pass |
| 129 | finally: |
Benjamin Peterson | 0fbcf69 | 2008-06-11 17:27:50 +0000 | [diff] [blame] | 130 | if not client_evt.is_set(): |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 131 | # allow some time for the client to read the result |
| 132 | time.sleep(0.5) |
| 133 | serv.close() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 134 | asyncore.close_all() |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 135 | serv_evt.set() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 136 | |
| 137 | MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n' |
| 138 | MSG_END = '------------ END MESSAGE ------------\n' |
| 139 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 140 | # NOTE: Some SMTP objects in the tests below are created with a non-default |
| 141 | # local_hostname argument to the constructor, since (on some systems) the FQDN |
| 142 | # lookup caused by the default local_hostname sometimes takes so long that the |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 143 | # test server times out, causing the test to fail. |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 144 | |
| 145 | # Test behavior of smtpd.DebuggingServer |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame^] | 146 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 147 | class DebuggingServerTests(unittest.TestCase): |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 148 | |
| 149 | def setUp(self): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 150 | # temporarily replace sys.stdout to capture DebuggingServer output |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 151 | self.old_stdout = sys.stdout |
| 152 | self.output = StringIO.StringIO() |
| 153 | sys.stdout = self.output |
| 154 | |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 155 | self._threads = test_support.threading_setup() |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 156 | self.serv_evt = threading.Event() |
| 157 | self.client_evt = threading.Event() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 158 | self.port = test_support.find_unused_port() |
| 159 | self.serv = smtpd.DebuggingServer((HOST, self.port), ('nowhere', -1)) |
| 160 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 161 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 162 | self.thread.start() |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 163 | |
| 164 | # wait until server thread has assigned a port number |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 165 | self.serv_evt.wait() |
| 166 | self.serv_evt.clear() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 167 | |
| 168 | def tearDown(self): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 169 | # indicate that the client is finished |
| 170 | self.client_evt.set() |
| 171 | # wait for the server thread to terminate |
| 172 | self.serv_evt.wait() |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 173 | self.thread.join() |
| 174 | test_support.threading_cleanup(*self._threads) |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 175 | # restore sys.stdout |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 176 | sys.stdout = self.old_stdout |
| 177 | |
| 178 | def testBasic(self): |
| 179 | # connect |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 180 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 181 | smtp.quit() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 182 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 183 | def testNOOP(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 184 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 185 | expected = (250, 'Ok') |
| 186 | self.assertEqual(smtp.noop(), expected) |
| 187 | smtp.quit() |
| 188 | |
| 189 | def testRSET(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 190 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 191 | expected = (250, 'Ok') |
| 192 | self.assertEqual(smtp.rset(), expected) |
| 193 | smtp.quit() |
| 194 | |
| 195 | def testNotImplemented(self): |
| 196 | # EHLO isn't implemented in DebuggingServer |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 197 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 198 | expected = (502, 'Error: command "EHLO" not implemented') |
| 199 | self.assertEqual(smtp.ehlo(), expected) |
| 200 | smtp.quit() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 201 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 202 | def testVRFY(self): |
| 203 | # VRFY isn't implemented in DebuggingServer |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 204 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 205 | expected = (502, 'Error: command "VRFY" not implemented') |
| 206 | self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected) |
| 207 | self.assertEqual(smtp.verify('nobody@nowhere.com'), expected) |
| 208 | smtp.quit() |
| 209 | |
| 210 | def testSecondHELO(self): |
| 211 | # check that a second HELO returns a message that it's a duplicate |
| 212 | # (this behavior is specific to smtpd.SMTPChannel) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 213 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 214 | smtp.helo() |
| 215 | expected = (503, 'Duplicate HELO/EHLO') |
| 216 | self.assertEqual(smtp.helo(), expected) |
| 217 | smtp.quit() |
| 218 | |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 219 | def testHELP(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 220 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 221 | self.assertEqual(smtp.help(), 'Error: command "HELP" not implemented') |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 222 | smtp.quit() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 223 | |
| 224 | def testSend(self): |
| 225 | # connect and send mail |
| 226 | m = 'A test message' |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 227 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 228 | smtp.sendmail('John', 'Sally', m) |
Neal Norwitz | e39be53 | 2008-08-25 03:52:40 +0000 | [diff] [blame] | 229 | # XXX(nnorwitz): this test is flaky and dies with a bad file descriptor |
| 230 | # in asyncore. This sleep might help, but should really be fixed |
| 231 | # properly by using an Event variable. |
| 232 | time.sleep(0.01) |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 233 | smtp.quit() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 234 | |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 235 | self.client_evt.set() |
| 236 | self.serv_evt.wait() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 237 | self.output.flush() |
| 238 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END) |
| 239 | self.assertEqual(self.output.getvalue(), mexpect) |
| 240 | |
| 241 | |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame^] | 242 | class NonConnectingTests(unittest.TestCase): |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 243 | |
| 244 | def testNotConnected(self): |
| 245 | # Test various operations on an unconnected SMTP object that |
| 246 | # should raise exceptions (at present the attempt in SMTP.send |
| 247 | # to reference the nonexistent 'sock' attribute of the SMTP object |
| 248 | # causes an AttributeError) |
| 249 | smtp = smtplib.SMTP() |
| 250 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo) |
| 251 | self.assertRaises(smtplib.SMTPServerDisconnected, |
| 252 | smtp.send, 'test msg') |
| 253 | |
| 254 | def testNonnumericPort(self): |
| 255 | # check that non-numeric port raises socket.error |
| 256 | self.assertRaises(socket.error, smtplib.SMTP, |
| 257 | "localhost", "bogus") |
| 258 | self.assertRaises(socket.error, smtplib.SMTP, |
| 259 | "localhost:bogus") |
| 260 | |
| 261 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 262 | # test response of client to a non-successful HELO message |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame^] | 263 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 264 | class BadHELOServerTests(unittest.TestCase): |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 265 | |
| 266 | def setUp(self): |
| 267 | self.old_stdout = sys.stdout |
| 268 | self.output = StringIO.StringIO() |
| 269 | sys.stdout = self.output |
| 270 | |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 271 | self._threads = test_support.threading_setup() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 272 | self.evt = threading.Event() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 273 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 274 | self.sock.settimeout(15) |
| 275 | self.port = test_support.bind_port(self.sock) |
| 276 | servargs = (self.evt, "199 no hello for you!\n", self.sock) |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 277 | self.thread = threading.Thread(target=server, args=servargs) |
| 278 | self.thread.start() |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 279 | self.evt.wait() |
| 280 | self.evt.clear() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 281 | |
| 282 | def tearDown(self): |
| 283 | self.evt.wait() |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 284 | self.thread.join() |
| 285 | test_support.threading_cleanup(*self._threads) |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 286 | sys.stdout = self.old_stdout |
| 287 | |
| 288 | def testFailingHELO(self): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 289 | self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP, |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 290 | HOST, self.port, 'localhost', 3) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 291 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 292 | |
| 293 | sim_users = {'Mr.A@somewhere.com':'John A', |
| 294 | 'Ms.B@somewhere.com':'Sally B', |
| 295 | 'Mrs.C@somewhereesle.com':'Ruth C', |
| 296 | } |
| 297 | |
R. David Murray | 3724d6c | 2009-05-23 21:48:06 +0000 | [diff] [blame] | 298 | sim_auth = ('Mr.A@somewhere.com', 'somepassword') |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 299 | sim_cram_md5_challenge = ('PENCeUxFREJoU0NnbmhNWitOMjNGNn' |
| 300 | 'dAZWx3b29kLmlubm9zb2Z0LmNvbT4=') |
| 301 | sim_auth_credentials = { |
| 302 | 'login': 'TXIuQUBzb21ld2hlcmUuY29t', |
| 303 | 'plain': 'AE1yLkFAc29tZXdoZXJlLmNvbQBzb21lcGFzc3dvcmQ=', |
| 304 | 'cram-md5': ('TXIUQUBZB21LD2HLCMUUY29TIDG4OWQ0MJ' |
| 305 | 'KWZGQ4ODNMNDA4NTGXMDRLZWMYZJDMODG1'), |
| 306 | } |
| 307 | sim_auth_login_password = 'C29TZXBHC3N3B3JK' |
R. David Murray | 3724d6c | 2009-05-23 21:48:06 +0000 | [diff] [blame] | 308 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 309 | sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'], |
| 310 | 'list-2':['Ms.B@somewhere.com',], |
| 311 | } |
| 312 | |
| 313 | # Simulated SMTP channel & server |
| 314 | class SimSMTPChannel(smtpd.SMTPChannel): |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 315 | |
R. David Murray | 6b98544 | 2009-05-29 17:31:05 +0000 | [diff] [blame] | 316 | def __init__(self, extra_features, *args, **kw): |
| 317 | self._extrafeatures = ''.join( |
| 318 | [ "250-{0}\r\n".format(x) for x in extra_features ]) |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 319 | smtpd.SMTPChannel.__init__(self, *args, **kw) |
| 320 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 321 | def smtp_EHLO(self, arg): |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 322 | resp = ('250-testhost\r\n' |
| 323 | '250-EXPN\r\n' |
| 324 | '250-SIZE 20000000\r\n' |
| 325 | '250-STARTTLS\r\n' |
| 326 | '250-DELIVERBY\r\n') |
| 327 | resp = resp + self._extrafeatures + '250 HELP' |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 328 | self.push(resp) |
| 329 | |
| 330 | def smtp_VRFY(self, arg): |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 331 | raw_addr = email.utils.parseaddr(arg)[1] |
| 332 | quoted_addr = smtplib.quoteaddr(arg) |
| 333 | if raw_addr in sim_users: |
| 334 | self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr)) |
| 335 | else: |
| 336 | self.push('550 No such user: %s' % arg) |
| 337 | |
| 338 | def smtp_EXPN(self, arg): |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 339 | list_name = email.utils.parseaddr(arg)[1].lower() |
| 340 | if list_name in sim_lists: |
| 341 | user_list = sim_lists[list_name] |
| 342 | for n, user_email in enumerate(user_list): |
| 343 | quoted_addr = smtplib.quoteaddr(user_email) |
| 344 | if n < len(user_list) - 1: |
| 345 | self.push('250-%s %s' % (sim_users[user_email], quoted_addr)) |
| 346 | else: |
| 347 | self.push('250 %s %s' % (sim_users[user_email], quoted_addr)) |
| 348 | else: |
| 349 | self.push('550 No access for you!') |
| 350 | |
R. David Murray | 3724d6c | 2009-05-23 21:48:06 +0000 | [diff] [blame] | 351 | def smtp_AUTH(self, arg): |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 352 | if arg.strip().lower()=='cram-md5': |
| 353 | self.push('334 {0}'.format(sim_cram_md5_challenge)) |
| 354 | return |
R. David Murray | 3724d6c | 2009-05-23 21:48:06 +0000 | [diff] [blame] | 355 | mech, auth = arg.split() |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 356 | mech = mech.lower() |
| 357 | if mech not in sim_auth_credentials: |
R. David Murray | 3724d6c | 2009-05-23 21:48:06 +0000 | [diff] [blame] | 358 | self.push('504 auth type unimplemented') |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 359 | return |
| 360 | if mech == 'plain' and auth==sim_auth_credentials['plain']: |
| 361 | self.push('235 plain auth ok') |
| 362 | elif mech=='login' and auth==sim_auth_credentials['login']: |
| 363 | self.push('334 Password:') |
| 364 | else: |
| 365 | self.push('550 No access for you!') |
R. David Murray | 3724d6c | 2009-05-23 21:48:06 +0000 | [diff] [blame] | 366 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 367 | |
| 368 | class SimSMTPServer(smtpd.SMTPServer): |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 369 | |
R. David Murray | 6b98544 | 2009-05-29 17:31:05 +0000 | [diff] [blame] | 370 | def __init__(self, *args, **kw): |
| 371 | self._extra_features = [] |
| 372 | smtpd.SMTPServer.__init__(self, *args, **kw) |
| 373 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 374 | def handle_accept(self): |
| 375 | conn, addr = self.accept() |
R. David Murray | 6b98544 | 2009-05-29 17:31:05 +0000 | [diff] [blame] | 376 | self._SMTPchannel = SimSMTPChannel(self._extra_features, |
| 377 | self, conn, addr) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 378 | |
| 379 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 380 | pass |
| 381 | |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 382 | def add_feature(self, feature): |
R. David Murray | 6b98544 | 2009-05-29 17:31:05 +0000 | [diff] [blame] | 383 | self._extra_features.append(feature) |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 384 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 385 | |
| 386 | # Test various SMTP & ESMTP commands/behaviors that require a simulated server |
| 387 | # (i.e., something with more features than DebuggingServer) |
Victor Stinner | 6a10281 | 2010-04-27 23:55:59 +0000 | [diff] [blame^] | 388 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 389 | class SMTPSimTests(unittest.TestCase): |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 390 | |
| 391 | def setUp(self): |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 392 | self._threads = test_support.threading_setup() |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 393 | self.serv_evt = threading.Event() |
| 394 | self.client_evt = threading.Event() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 395 | self.port = test_support.find_unused_port() |
| 396 | self.serv = SimSMTPServer((HOST, self.port), ('nowhere', -1)) |
| 397 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 398 | self.thread = threading.Thread(target=debugging_server, args=serv_args) |
| 399 | self.thread.start() |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 400 | |
| 401 | # wait until server thread has assigned a port number |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 402 | self.serv_evt.wait() |
| 403 | self.serv_evt.clear() |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 404 | |
| 405 | def tearDown(self): |
| 406 | # indicate that the client is finished |
| 407 | self.client_evt.set() |
| 408 | # wait for the server thread to terminate |
| 409 | self.serv_evt.wait() |
Antoine Pitrou | a763c06 | 2009-10-27 19:47:30 +0000 | [diff] [blame] | 410 | self.thread.join() |
| 411 | test_support.threading_cleanup(*self._threads) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 412 | |
| 413 | def testBasic(self): |
| 414 | # smoke test |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 415 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 416 | smtp.quit() |
| 417 | |
| 418 | def testEHLO(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 419 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 420 | |
| 421 | # no features should be present before the EHLO |
| 422 | self.assertEqual(smtp.esmtp_features, {}) |
| 423 | |
| 424 | # features expected from the test server |
| 425 | expected_features = {'expn':'', |
| 426 | 'size': '20000000', |
| 427 | 'starttls': '', |
| 428 | 'deliverby': '', |
| 429 | 'help': '', |
| 430 | } |
| 431 | |
| 432 | smtp.ehlo() |
| 433 | self.assertEqual(smtp.esmtp_features, expected_features) |
| 434 | for k in expected_features: |
| 435 | self.assertTrue(smtp.has_extn(k)) |
| 436 | self.assertFalse(smtp.has_extn('unsupported-feature')) |
| 437 | smtp.quit() |
| 438 | |
| 439 | def testVRFY(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 440 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 441 | |
| 442 | for email, name in sim_users.items(): |
| 443 | expected_known = (250, '%s %s' % (name, smtplib.quoteaddr(email))) |
| 444 | self.assertEqual(smtp.vrfy(email), expected_known) |
| 445 | |
| 446 | u = 'nobody@nowhere.com' |
| 447 | expected_unknown = (550, 'No such user: %s' % smtplib.quoteaddr(u)) |
| 448 | self.assertEqual(smtp.vrfy(u), expected_unknown) |
| 449 | smtp.quit() |
| 450 | |
| 451 | def testEXPN(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 452 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 453 | |
| 454 | for listname, members in sim_lists.items(): |
| 455 | users = [] |
| 456 | for m in members: |
| 457 | users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m))) |
| 458 | expected_known = (250, '\n'.join(users)) |
| 459 | self.assertEqual(smtp.expn(listname), expected_known) |
| 460 | |
| 461 | u = 'PSU-Members-List' |
| 462 | expected_unknown = (550, 'No access for you!') |
| 463 | self.assertEqual(smtp.expn(u), expected_unknown) |
| 464 | smtp.quit() |
| 465 | |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 466 | def testAUTH_PLAIN(self): |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 467 | self.serv.add_feature("AUTH PLAIN") |
R. David Murray | 6b98544 | 2009-05-29 17:31:05 +0000 | [diff] [blame] | 468 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | 3724d6c | 2009-05-23 21:48:06 +0000 | [diff] [blame] | 469 | |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 470 | expected_auth_ok = (235, b'plain auth ok') |
R. David Murray | 3724d6c | 2009-05-23 21:48:06 +0000 | [diff] [blame] | 471 | self.assertEqual(smtp.login(sim_auth[0], sim_auth[1]), expected_auth_ok) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 472 | |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 473 | # SimSMTPChannel doesn't fully support LOGIN or CRAM-MD5 auth because they |
| 474 | # require a synchronous read to obtain the credentials...so instead smtpd |
| 475 | # sees the credential sent by smtplib's login method as an unknown command, |
| 476 | # which results in smtplib raising an auth error. Fortunately the error |
| 477 | # message contains the encoded credential, so we can partially check that it |
| 478 | # was generated correctly (partially, because the 'word' is uppercased in |
| 479 | # the error message). |
| 480 | |
| 481 | def testAUTH_LOGIN(self): |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 482 | self.serv.add_feature("AUTH LOGIN") |
R. David Murray | 6b98544 | 2009-05-29 17:31:05 +0000 | [diff] [blame] | 483 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 484 | try: smtp.login(sim_auth[0], sim_auth[1]) |
| 485 | except smtplib.SMTPAuthenticationError as err: |
| 486 | if sim_auth_login_password not in str(err): |
| 487 | raise "expected encoded password not found in error message" |
| 488 | |
| 489 | def testAUTH_CRAM_MD5(self): |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 490 | self.serv.add_feature("AUTH CRAM-MD5") |
R. David Murray | 6b98544 | 2009-05-29 17:31:05 +0000 | [diff] [blame] | 491 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
R. David Murray | 8de212b | 2009-05-28 18:49:23 +0000 | [diff] [blame] | 492 | |
| 493 | try: smtp.login(sim_auth[0], sim_auth[1]) |
| 494 | except smtplib.SMTPAuthenticationError as err: |
| 495 | if sim_auth_credentials['cram-md5'] not in str(err): |
| 496 | raise "expected encoded credentials not found in error message" |
| 497 | |
| 498 | #TODO: add tests for correct AUTH method fallback now that the |
| 499 | #test infrastructure can support it. |
| 500 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 501 | |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 502 | def test_main(verbose=None): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 503 | test_support.run_unittest(GeneralTests, DebuggingServerTests, |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 504 | NonConnectingTests, |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 505 | BadHELOServerTests, SMTPSimTests) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 506 | |
| 507 | if __name__ == '__main__': |
| 508 | test_main() |