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