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