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 | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 57 | smtp.sock.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 | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 62 | smtp.sock.close() |
| 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") |
| 68 | smtp.sock.close() |
| 69 | |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 70 | def testTimeoutDefault(self): |
| 71 | # default |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 72 | smtp = smtplib.SMTP(HOST, self.port) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 73 | self.assertTrue(smtp.sock.gettimeout() is None) |
| 74 | smtp.sock.close() |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 75 | |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 76 | def testTimeoutValue(self): |
| 77 | # a value |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 78 | smtp = smtplib.SMTP(HOST, self.port, timeout=30) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 79 | self.assertEqual(smtp.sock.gettimeout(), 30) |
| 80 | smtp.sock.close() |
| 81 | |
| 82 | def testTimeoutNone(self): |
| 83 | # None, having other default |
| 84 | previous = socket.getdefaulttimeout() |
| 85 | socket.setdefaulttimeout(30) |
| 86 | try: |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 87 | smtp = smtplib.SMTP(HOST, self.port, timeout=None) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 88 | finally: |
| 89 | socket.setdefaulttimeout(previous) |
| 90 | self.assertEqual(smtp.sock.gettimeout(), 30) |
| 91 | smtp.sock.close() |
| 92 | |
| 93 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 94 | # Test server thread using the specified SMTP server class |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 95 | def debugging_server(serv, serv_evt, client_evt): |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 96 | serv_evt.set() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 97 | |
| 98 | try: |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 99 | if hasattr(select, 'poll'): |
| 100 | poll_fun = asyncore.poll2 |
| 101 | else: |
| 102 | poll_fun = asyncore.poll |
| 103 | |
| 104 | n = 1000 |
| 105 | while asyncore.socket_map and n > 0: |
| 106 | poll_fun(0.01, asyncore.socket_map) |
| 107 | |
| 108 | # when the client conversation is finished, it will |
| 109 | # set client_evt, and it's then ok to kill the server |
| 110 | if client_evt.isSet(): |
| 111 | serv.close() |
| 112 | break |
| 113 | |
| 114 | n -= 1 |
| 115 | |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 116 | except socket.timeout: |
| 117 | pass |
| 118 | finally: |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 119 | if not client_evt.isSet(): |
| 120 | # allow some time for the client to read the result |
| 121 | time.sleep(0.5) |
| 122 | serv.close() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 123 | asyncore.close_all() |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 124 | serv_evt.set() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 125 | |
| 126 | MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n' |
| 127 | MSG_END = '------------ END MESSAGE ------------\n' |
| 128 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 129 | # NOTE: Some SMTP objects in the tests below are created with a non-default |
| 130 | # local_hostname argument to the constructor, since (on some systems) the FQDN |
| 131 | # 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] | 132 | # test server times out, causing the test to fail. |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 133 | |
| 134 | # Test behavior of smtpd.DebuggingServer |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 135 | class DebuggingServerTests(TestCase): |
| 136 | |
| 137 | def setUp(self): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 138 | # temporarily replace sys.stdout to capture DebuggingServer output |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 139 | self.old_stdout = sys.stdout |
| 140 | self.output = StringIO.StringIO() |
| 141 | sys.stdout = self.output |
| 142 | |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 143 | self.serv_evt = threading.Event() |
| 144 | self.client_evt = threading.Event() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 145 | self.port = test_support.find_unused_port() |
| 146 | self.serv = smtpd.DebuggingServer((HOST, self.port), ('nowhere', -1)) |
| 147 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 148 | threading.Thread(target=debugging_server, args=serv_args).start() |
| 149 | |
| 150 | # wait until server thread has assigned a port number |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 151 | self.serv_evt.wait() |
| 152 | self.serv_evt.clear() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 153 | |
| 154 | def tearDown(self): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 155 | # indicate that the client is finished |
| 156 | self.client_evt.set() |
| 157 | # wait for the server thread to terminate |
| 158 | self.serv_evt.wait() |
| 159 | # restore sys.stdout |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 160 | sys.stdout = self.old_stdout |
| 161 | |
| 162 | def testBasic(self): |
| 163 | # connect |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 164 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 165 | smtp.quit() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 166 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 167 | def testNOOP(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 168 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 169 | expected = (250, 'Ok') |
| 170 | self.assertEqual(smtp.noop(), expected) |
| 171 | smtp.quit() |
| 172 | |
| 173 | def testRSET(self): |
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 | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 175 | expected = (250, 'Ok') |
| 176 | self.assertEqual(smtp.rset(), expected) |
| 177 | smtp.quit() |
| 178 | |
| 179 | def testNotImplemented(self): |
| 180 | # EHLO isn't implemented in DebuggingServer |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 181 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 182 | expected = (502, 'Error: command "EHLO" not implemented') |
| 183 | self.assertEqual(smtp.ehlo(), expected) |
| 184 | smtp.quit() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 185 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 186 | def testVRFY(self): |
| 187 | # VRFY isn't implemented in DebuggingServer |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 188 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 189 | expected = (502, 'Error: command "VRFY" not implemented') |
| 190 | self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected) |
| 191 | self.assertEqual(smtp.verify('nobody@nowhere.com'), expected) |
| 192 | smtp.quit() |
| 193 | |
| 194 | def testSecondHELO(self): |
| 195 | # check that a second HELO returns a message that it's a duplicate |
| 196 | # (this behavior is specific to smtpd.SMTPChannel) |
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 | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 198 | smtp.helo() |
| 199 | expected = (503, 'Duplicate HELO/EHLO') |
| 200 | self.assertEqual(smtp.helo(), expected) |
| 201 | smtp.quit() |
| 202 | |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 203 | def testHELP(self): |
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 | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 205 | self.assertEqual(smtp.help(), 'Error: command "HELP" not implemented') |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 206 | smtp.quit() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 207 | |
| 208 | def testSend(self): |
| 209 | # connect and send mail |
| 210 | m = 'A test message' |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 211 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 212 | smtp.sendmail('John', 'Sally', m) |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 213 | smtp.quit() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 214 | |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 215 | self.client_evt.set() |
| 216 | self.serv_evt.wait() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 217 | self.output.flush() |
| 218 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END) |
| 219 | self.assertEqual(self.output.getvalue(), mexpect) |
| 220 | |
| 221 | |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 222 | class NonConnectingTests(TestCase): |
| 223 | |
| 224 | def testNotConnected(self): |
| 225 | # Test various operations on an unconnected SMTP object that |
| 226 | # should raise exceptions (at present the attempt in SMTP.send |
| 227 | # to reference the nonexistent 'sock' attribute of the SMTP object |
| 228 | # causes an AttributeError) |
| 229 | smtp = smtplib.SMTP() |
| 230 | self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo) |
| 231 | self.assertRaises(smtplib.SMTPServerDisconnected, |
| 232 | smtp.send, 'test msg') |
| 233 | |
| 234 | def testNonnumericPort(self): |
| 235 | # check that non-numeric port raises socket.error |
| 236 | self.assertRaises(socket.error, smtplib.SMTP, |
| 237 | "localhost", "bogus") |
| 238 | self.assertRaises(socket.error, smtplib.SMTP, |
| 239 | "localhost:bogus") |
| 240 | |
| 241 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 242 | # test response of client to a non-successful HELO message |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 243 | class BadHELOServerTests(TestCase): |
| 244 | |
| 245 | def setUp(self): |
| 246 | self.old_stdout = sys.stdout |
| 247 | self.output = StringIO.StringIO() |
| 248 | sys.stdout = self.output |
| 249 | |
| 250 | self.evt = threading.Event() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 251 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 252 | self.sock.settimeout(15) |
| 253 | self.port = test_support.bind_port(self.sock) |
| 254 | servargs = (self.evt, "199 no hello for you!\n", self.sock) |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 255 | threading.Thread(target=server, args=servargs).start() |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 256 | self.evt.wait() |
| 257 | self.evt.clear() |
Facundo Batista | 16ed5b4 | 2007-07-24 21:20:42 +0000 | [diff] [blame] | 258 | |
| 259 | def tearDown(self): |
| 260 | self.evt.wait() |
| 261 | sys.stdout = self.old_stdout |
| 262 | |
| 263 | def testFailingHELO(self): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 264 | self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP, |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 265 | HOST, self.port, 'localhost', 3) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 266 | |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 267 | |
| 268 | sim_users = {'Mr.A@somewhere.com':'John A', |
| 269 | 'Ms.B@somewhere.com':'Sally B', |
| 270 | 'Mrs.C@somewhereesle.com':'Ruth C', |
| 271 | } |
| 272 | |
| 273 | sim_lists = {'list-1':['Mr.A@somewhere.com','Mrs.C@somewhereesle.com'], |
| 274 | 'list-2':['Ms.B@somewhere.com',], |
| 275 | } |
| 276 | |
| 277 | # Simulated SMTP channel & server |
| 278 | class SimSMTPChannel(smtpd.SMTPChannel): |
| 279 | def smtp_EHLO(self, arg): |
| 280 | resp = '250-testhost\r\n' \ |
| 281 | '250-EXPN\r\n' \ |
| 282 | '250-SIZE 20000000\r\n' \ |
| 283 | '250-STARTTLS\r\n' \ |
| 284 | '250-DELIVERBY\r\n' \ |
| 285 | '250 HELP' |
| 286 | self.push(resp) |
| 287 | |
| 288 | def smtp_VRFY(self, arg): |
| 289 | # print '\nsmtp_VRFY(%r)\n' % arg |
| 290 | |
| 291 | raw_addr = email.utils.parseaddr(arg)[1] |
| 292 | quoted_addr = smtplib.quoteaddr(arg) |
| 293 | if raw_addr in sim_users: |
| 294 | self.push('250 %s %s' % (sim_users[raw_addr], quoted_addr)) |
| 295 | else: |
| 296 | self.push('550 No such user: %s' % arg) |
| 297 | |
| 298 | def smtp_EXPN(self, arg): |
| 299 | # print '\nsmtp_EXPN(%r)\n' % arg |
| 300 | |
| 301 | list_name = email.utils.parseaddr(arg)[1].lower() |
| 302 | if list_name in sim_lists: |
| 303 | user_list = sim_lists[list_name] |
| 304 | for n, user_email in enumerate(user_list): |
| 305 | quoted_addr = smtplib.quoteaddr(user_email) |
| 306 | if n < len(user_list) - 1: |
| 307 | self.push('250-%s %s' % (sim_users[user_email], quoted_addr)) |
| 308 | else: |
| 309 | self.push('250 %s %s' % (sim_users[user_email], quoted_addr)) |
| 310 | else: |
| 311 | self.push('550 No access for you!') |
| 312 | |
| 313 | |
| 314 | class SimSMTPServer(smtpd.SMTPServer): |
| 315 | def handle_accept(self): |
| 316 | conn, addr = self.accept() |
| 317 | channel = SimSMTPChannel(self, conn, addr) |
| 318 | |
| 319 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 320 | pass |
| 321 | |
| 322 | |
| 323 | # Test various SMTP & ESMTP commands/behaviors that require a simulated server |
| 324 | # (i.e., something with more features than DebuggingServer) |
| 325 | class SMTPSimTests(TestCase): |
| 326 | |
| 327 | def setUp(self): |
| 328 | self.serv_evt = threading.Event() |
| 329 | self.client_evt = threading.Event() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 330 | self.port = test_support.find_unused_port() |
| 331 | self.serv = SimSMTPServer((HOST, self.port), ('nowhere', -1)) |
| 332 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 333 | threading.Thread(target=debugging_server, args=serv_args).start() |
| 334 | |
| 335 | # wait until server thread has assigned a port number |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 336 | self.serv_evt.wait() |
| 337 | self.serv_evt.clear() |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 338 | |
| 339 | def tearDown(self): |
| 340 | # indicate that the client is finished |
| 341 | self.client_evt.set() |
| 342 | # wait for the server thread to terminate |
| 343 | self.serv_evt.wait() |
| 344 | |
| 345 | def testBasic(self): |
| 346 | # smoke test |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 347 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 348 | smtp.quit() |
| 349 | |
| 350 | def testEHLO(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 351 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 352 | |
| 353 | # no features should be present before the EHLO |
| 354 | self.assertEqual(smtp.esmtp_features, {}) |
| 355 | |
| 356 | # features expected from the test server |
| 357 | expected_features = {'expn':'', |
| 358 | 'size': '20000000', |
| 359 | 'starttls': '', |
| 360 | 'deliverby': '', |
| 361 | 'help': '', |
| 362 | } |
| 363 | |
| 364 | smtp.ehlo() |
| 365 | self.assertEqual(smtp.esmtp_features, expected_features) |
| 366 | for k in expected_features: |
| 367 | self.assertTrue(smtp.has_extn(k)) |
| 368 | self.assertFalse(smtp.has_extn('unsupported-feature')) |
| 369 | smtp.quit() |
| 370 | |
| 371 | def testVRFY(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 372 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 373 | |
| 374 | for email, name in sim_users.items(): |
| 375 | expected_known = (250, '%s %s' % (name, smtplib.quoteaddr(email))) |
| 376 | self.assertEqual(smtp.vrfy(email), expected_known) |
| 377 | |
| 378 | u = 'nobody@nowhere.com' |
| 379 | expected_unknown = (550, 'No such user: %s' % smtplib.quoteaddr(u)) |
| 380 | self.assertEqual(smtp.vrfy(u), expected_unknown) |
| 381 | smtp.quit() |
| 382 | |
| 383 | def testEXPN(self): |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 384 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 385 | |
| 386 | for listname, members in sim_lists.items(): |
| 387 | users = [] |
| 388 | for m in members: |
| 389 | users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m))) |
| 390 | expected_known = (250, '\n'.join(users)) |
| 391 | self.assertEqual(smtp.expn(listname), expected_known) |
| 392 | |
| 393 | u = 'PSU-Members-List' |
| 394 | expected_unknown = (550, 'No access for you!') |
| 395 | self.assertEqual(smtp.expn(u), expected_unknown) |
| 396 | smtp.quit() |
| 397 | |
| 398 | |
| 399 | |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 400 | def test_main(verbose=None): |
Facundo Batista | 412b8b6 | 2007-08-01 23:18:36 +0000 | [diff] [blame] | 401 | test_support.run_unittest(GeneralTests, DebuggingServerTests, |
Neal Norwitz | 75992ed | 2008-02-26 08:04:59 +0000 | [diff] [blame] | 402 | NonConnectingTests, |
Facundo Batista | 1bc8d63 | 2007-08-21 16:57:18 +0000 | [diff] [blame] | 403 | BadHELOServerTests, SMTPSimTests) |
Facundo Batista | 366d626 | 2007-03-28 18:25:54 +0000 | [diff] [blame] | 404 | |
| 405 | if __name__ == '__main__': |
| 406 | test_main() |