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