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