Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 1 | import asyncore |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 2 | import email.utils |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3 | import socket |
| 4 | import threading |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 5 | import smtpd |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import smtplib |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 7 | import io |
| 8 | import sys |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 9 | import time |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 10 | import select |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | |
| 12 | from unittest import TestCase |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 13 | from test import support |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 14 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 15 | HOST = support.HOST |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 16 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 17 | def server(evt, buf, serv): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 18 | serv.listen(5) |
| 19 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 20 | try: |
| 21 | conn, addr = serv.accept() |
| 22 | except socket.timeout: |
| 23 | pass |
| 24 | else: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 25 | n = 500 |
| 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 |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 34 | conn.close() |
| 35 | finally: |
| 36 | serv.close() |
| 37 | evt.set() |
| 38 | |
| 39 | class GeneralTests(TestCase): |
| 40 | |
| 41 | def setUp(self): |
| 42 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 43 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 44 | self.sock.settimeout(15) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 45 | self.port = support.bind_port(self.sock) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 46 | servargs = (self.evt, b"220 Hola mundo\n", self.sock) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 47 | threading.Thread(target=server, args=servargs).start() |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 48 | self.evt.wait() |
| 49 | self.evt.clear() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 50 | |
| 51 | def tearDown(self): |
| 52 | self.evt.wait() |
| 53 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 54 | def testBasic1(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 55 | # connects |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 56 | smtp = smtplib.SMTP(HOST, self.port) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 57 | smtp.sock.close() |
| 58 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 59 | def testBasic2(self): |
| 60 | # connects, include port in host name |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 61 | smtp = smtplib.SMTP("%s:%s" % (HOST, self.port)) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 62 | smtp.sock.close() |
| 63 | |
| 64 | def testLocalHostName(self): |
| 65 | # check that supplied local_hostname is used |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 66 | smtp = smtplib.SMTP(HOST, self.port, local_hostname="testhost") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 67 | self.assertEqual(smtp.local_hostname, "testhost") |
| 68 | smtp.sock.close() |
| 69 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 70 | def testTimeoutDefault(self): |
| 71 | # default |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 72 | smtp = smtplib.SMTP(HOST, self.port) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 73 | self.assertTrue(smtp.sock.gettimeout() is None) |
| 74 | smtp.sock.close() |
| 75 | |
| 76 | def testTimeoutValue(self): |
| 77 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 78 | smtp = smtplib.SMTP(HOST, self.port, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +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: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 87 | smtp = smtplib.SMTP(HOST, self.port, timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 88 | finally: |
| 89 | socket.setdefaulttimeout(previous) |
| 90 | self.assertEqual(smtp.sock.gettimeout(), 30) |
| 91 | smtp.sock.close() |
| 92 | |
| 93 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 94 | # Test server thread using the specified SMTP server class |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 95 | def debugging_server(serv, serv_evt, client_evt): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 96 | serv_evt.set() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 97 | |
| 98 | try: |
| 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 | |
| 116 | except socket.timeout: |
| 117 | pass |
| 118 | finally: |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +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() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 123 | asyncore.close_all() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 124 | serv_evt.set() |
| 125 | |
| 126 | MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n' |
| 127 | MSG_END = '------------ END MESSAGE ------------\n' |
| 128 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +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 |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 132 | # test server times out, causing the test to fail. |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 133 | |
| 134 | # Test behavior of smtpd.DebuggingServer |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 135 | class DebuggingServerTests(TestCase): |
| 136 | |
| 137 | def setUp(self): |
| 138 | # temporarily replace sys.stdout to capture DebuggingServer output |
| 139 | self.old_stdout = sys.stdout |
| 140 | self.output = io.StringIO() |
| 141 | sys.stdout = self.output |
| 142 | |
| 143 | self.serv_evt = threading.Event() |
| 144 | self.client_evt = threading.Event() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 145 | self.port = support.find_unused_port() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 146 | self.serv = smtpd.DebuggingServer((HOST, self.port), ('nowhere', -1)) |
| 147 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 148 | threading.Thread(target=debugging_server, args=serv_args).start() |
| 149 | |
| 150 | # wait until server thread has assigned a port number |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 151 | self.serv_evt.wait() |
| 152 | self.serv_evt.clear() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 153 | |
| 154 | def tearDown(self): |
| 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 |
| 160 | sys.stdout = self.old_stdout |
| 161 | |
| 162 | def testBasic(self): |
| 163 | # connect |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 164 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 165 | smtp.quit() |
| 166 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 167 | def testNOOP(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 168 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 169 | expected = (250, b'Ok') |
| 170 | self.assertEqual(smtp.noop(), expected) |
| 171 | smtp.quit() |
| 172 | |
| 173 | def testRSET(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 174 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 175 | expected = (250, b'Ok') |
| 176 | self.assertEqual(smtp.rset(), expected) |
| 177 | smtp.quit() |
| 178 | |
| 179 | def testNotImplemented(self): |
| 180 | # EHLO isn't implemented in DebuggingServer |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 181 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 182 | expected = (502, b'Error: command "EHLO" not implemented') |
| 183 | self.assertEqual(smtp.ehlo(), expected) |
| 184 | smtp.quit() |
| 185 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 186 | def testVRFY(self): |
| 187 | # VRFY isn't implemented in DebuggingServer |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 188 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 189 | expected = (502, b'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) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 197 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 198 | smtp.helo() |
| 199 | expected = (503, b'Duplicate HELO/EHLO') |
| 200 | self.assertEqual(smtp.helo(), expected) |
| 201 | smtp.quit() |
| 202 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 203 | def testHELP(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 204 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 205 | self.assertEqual(smtp.help(), b'Error: command "HELP" not implemented') |
| 206 | smtp.quit() |
| 207 | |
| 208 | def testSend(self): |
| 209 | # connect and send mail |
| 210 | m = 'A test message' |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 211 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 212 | smtp.sendmail('John', 'Sally', m) |
| 213 | smtp.quit() |
| 214 | |
| 215 | self.client_evt.set() |
| 216 | self.serv_evt.wait() |
| 217 | self.output.flush() |
| 218 | mexpect = '%s%s\n%s' % (MSG_BEGIN, m, MSG_END) |
| 219 | self.assertEqual(self.output.getvalue(), mexpect) |
| 220 | |
| 221 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +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 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 242 | # test response of client to a non-successful HELO message |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 243 | class BadHELOServerTests(TestCase): |
| 244 | |
| 245 | def setUp(self): |
| 246 | self.old_stdout = sys.stdout |
| 247 | self.output = io.StringIO() |
| 248 | sys.stdout = self.output |
| 249 | |
| 250 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 251 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 252 | self.sock.settimeout(15) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 253 | self.port = support.bind_port(self.sock) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 254 | servargs = (self.evt, b"199 no hello for you!\n", self.sock) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 255 | threading.Thread(target=server, args=servargs).start() |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 256 | self.evt.wait() |
| 257 | self.evt.clear() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 258 | |
| 259 | def tearDown(self): |
| 260 | self.evt.wait() |
| 261 | sys.stdout = self.old_stdout |
| 262 | |
| 263 | def testFailingHELO(self): |
| 264 | self.assertRaises(smtplib.SMTPConnectError, smtplib.SMTP, |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 265 | HOST, self.port, 'localhost', 3) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +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() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 330 | self.port = support.find_unused_port() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 331 | self.serv = SimSMTPServer((HOST, self.port), ('nowhere', -1)) |
| 332 | serv_args = (self.serv, self.serv_evt, self.client_evt) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 333 | threading.Thread(target=debugging_server, args=serv_args).start() |
| 334 | |
| 335 | # wait until server thread has assigned a port number |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 336 | self.serv_evt.wait() |
| 337 | self.serv_evt.clear() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +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 |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 347 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 348 | smtp.quit() |
| 349 | |
| 350 | def testEHLO(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 351 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +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): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 372 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 373 | |
| 374 | for email, name in sim_users.items(): |
| 375 | expected_known = (250, bytes('%s %s' % |
Guido van Rossum | 5a23cc5 | 2007-08-30 14:02:43 +0000 | [diff] [blame] | 376 | (name, smtplib.quoteaddr(email)), |
| 377 | "ascii")) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 378 | self.assertEqual(smtp.vrfy(email), expected_known) |
| 379 | |
| 380 | u = 'nobody@nowhere.com' |
Thomas Wouters | 74e68c7 | 2007-08-31 00:20:14 +0000 | [diff] [blame] | 381 | expected_unknown = (550, ('No such user: %s' |
| 382 | % smtplib.quoteaddr(u)).encode('ascii')) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 383 | self.assertEqual(smtp.vrfy(u), expected_unknown) |
| 384 | smtp.quit() |
| 385 | |
| 386 | def testEXPN(self): |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 387 | smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=15) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 388 | |
| 389 | for listname, members in sim_lists.items(): |
| 390 | users = [] |
| 391 | for m in members: |
| 392 | users.append('%s %s' % (sim_users[m], smtplib.quoteaddr(m))) |
Guido van Rossum | 5a23cc5 | 2007-08-30 14:02:43 +0000 | [diff] [blame] | 393 | expected_known = (250, bytes('\n'.join(users), "ascii")) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 394 | self.assertEqual(smtp.expn(listname), expected_known) |
| 395 | |
| 396 | u = 'PSU-Members-List' |
| 397 | expected_unknown = (550, b'No access for you!') |
| 398 | self.assertEqual(smtp.expn(u), expected_unknown) |
| 399 | smtp.quit() |
| 400 | |
| 401 | |
| 402 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 403 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 404 | support.run_unittest(GeneralTests, DebuggingServerTests, |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 405 | NonConnectingTests, |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 406 | BadHELOServerTests, SMTPSimTests) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 407 | |
| 408 | if __name__ == '__main__': |
| 409 | test_main() |