Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 1 | """Test script for poplib module.""" |
| 2 | |
| 3 | # Modified by Giampaolo Rodola' to give poplib.POP3 and poplib.POP3_SSL |
| 4 | # a real test suite |
| 5 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import poplib |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 7 | import asyncore |
| 8 | import asynchat |
| 9 | import socket |
| 10 | import os |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | import time |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 12 | import errno |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 13 | |
| 14 | from unittest import TestCase |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 15 | from test import support as test_support |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 16 | threading = test_support.import_module('threading') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 17 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 18 | HOST = test_support.HOST |
| 19 | PORT = 0 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 20 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 21 | # the dummy data returned by server when LIST and RETR commands are issued |
| 22 | LIST_RESP = b'1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n.\r\n' |
| 23 | RETR_RESP = b"""From: postmaster@python.org\ |
| 24 | \r\nContent-Type: text/plain\r\n\ |
| 25 | MIME-Version: 1.0\r\n\ |
| 26 | Subject: Dummy\r\n\ |
| 27 | \r\n\ |
| 28 | line1\r\n\ |
| 29 | line2\r\n\ |
| 30 | line3\r\n\ |
| 31 | .\r\n""" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 33 | |
| 34 | class DummyPOP3Handler(asynchat.async_chat): |
| 35 | |
| 36 | def __init__(self, conn): |
| 37 | asynchat.async_chat.__init__(self, conn) |
| 38 | self.set_terminator(b"\r\n") |
| 39 | self.in_buffer = [] |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 40 | self.push('+OK dummy pop3 server ready. <timestamp>') |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 41 | |
| 42 | def collect_incoming_data(self, data): |
| 43 | self.in_buffer.append(data) |
| 44 | |
| 45 | def found_terminator(self): |
| 46 | line = b''.join(self.in_buffer) |
| 47 | line = str(line, 'ISO-8859-1') |
| 48 | self.in_buffer = [] |
| 49 | cmd = line.split(' ')[0].lower() |
| 50 | space = line.find(' ') |
| 51 | if space != -1: |
| 52 | arg = line[space + 1:] |
| 53 | else: |
| 54 | arg = "" |
| 55 | if hasattr(self, 'cmd_' + cmd): |
| 56 | method = getattr(self, 'cmd_' + cmd) |
| 57 | method(arg) |
| 58 | else: |
| 59 | self.push('-ERR unrecognized POP3 command "%s".' %cmd) |
| 60 | |
| 61 | def handle_error(self): |
| 62 | raise |
| 63 | |
| 64 | def push(self, data): |
| 65 | asynchat.async_chat.push(self, data.encode("ISO-8859-1") + b'\r\n') |
| 66 | |
| 67 | def cmd_echo(self, arg): |
| 68 | # sends back the received string (used by the test suite) |
| 69 | self.push(arg) |
| 70 | |
| 71 | def cmd_user(self, arg): |
| 72 | if arg != "guido": |
| 73 | self.push("-ERR no such user") |
| 74 | self.push('+OK password required') |
| 75 | |
| 76 | def cmd_pass(self, arg): |
| 77 | if arg != "python": |
| 78 | self.push("-ERR wrong password") |
| 79 | self.push('+OK 10 messages') |
| 80 | |
| 81 | def cmd_stat(self, arg): |
| 82 | self.push('+OK 10 100') |
| 83 | |
| 84 | def cmd_list(self, arg): |
| 85 | if arg: |
| 86 | self.push('+OK %s %s' %(arg, arg)) |
| 87 | else: |
| 88 | self.push('+OK') |
| 89 | asynchat.async_chat.push(self, LIST_RESP) |
| 90 | |
| 91 | cmd_uidl = cmd_list |
| 92 | |
| 93 | def cmd_retr(self, arg): |
| 94 | self.push('+OK %s bytes' %len(RETR_RESP)) |
| 95 | asynchat.async_chat.push(self, RETR_RESP) |
| 96 | |
| 97 | cmd_top = cmd_retr |
| 98 | |
| 99 | def cmd_dele(self, arg): |
| 100 | self.push('+OK message marked for deletion.') |
| 101 | |
| 102 | def cmd_noop(self, arg): |
| 103 | self.push('+OK done nothing.') |
| 104 | |
| 105 | def cmd_rpop(self, arg): |
| 106 | self.push('+OK done nothing.') |
| 107 | |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 108 | def cmd_apop(self, arg): |
| 109 | self.push('+OK done nothing.') |
| 110 | |
Giampaolo Rodolà | 95bcb93 | 2011-02-25 22:28:24 +0000 | [diff] [blame] | 111 | def cmd_quit(self, arg): |
| 112 | self.push('+OK closing.') |
| 113 | self.close_when_done() |
| 114 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 115 | |
| 116 | class DummyPOP3Server(asyncore.dispatcher, threading.Thread): |
| 117 | |
| 118 | handler = DummyPOP3Handler |
| 119 | |
| 120 | def __init__(self, address, af=socket.AF_INET): |
| 121 | threading.Thread.__init__(self) |
| 122 | asyncore.dispatcher.__init__(self) |
| 123 | self.create_socket(af, socket.SOCK_STREAM) |
| 124 | self.bind(address) |
| 125 | self.listen(5) |
| 126 | self.active = False |
| 127 | self.active_lock = threading.Lock() |
| 128 | self.host, self.port = self.socket.getsockname()[:2] |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 129 | self.handler_instance = None |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 130 | |
| 131 | def start(self): |
| 132 | assert not self.active |
| 133 | self.__flag = threading.Event() |
| 134 | threading.Thread.start(self) |
| 135 | self.__flag.wait() |
| 136 | |
| 137 | def run(self): |
| 138 | self.active = True |
| 139 | self.__flag.set() |
| 140 | while self.active and asyncore.socket_map: |
| 141 | self.active_lock.acquire() |
| 142 | asyncore.loop(timeout=0.1, count=1) |
| 143 | self.active_lock.release() |
| 144 | asyncore.close_all(ignore_all=True) |
| 145 | |
| 146 | def stop(self): |
| 147 | assert self.active |
| 148 | self.active = False |
| 149 | self.join() |
| 150 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 151 | def handle_accepted(self, conn, addr): |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 152 | self.handler_instance = self.handler(conn) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 153 | |
| 154 | def handle_connect(self): |
| 155 | self.close() |
| 156 | handle_read = handle_connect |
| 157 | |
| 158 | def writable(self): |
| 159 | return 0 |
| 160 | |
| 161 | def handle_error(self): |
| 162 | raise |
| 163 | |
| 164 | |
| 165 | class TestPOP3Class(TestCase): |
| 166 | def assertOK(self, resp): |
| 167 | self.assertTrue(resp.startswith(b"+OK")) |
| 168 | |
| 169 | def setUp(self): |
| 170 | self.server = DummyPOP3Server((HOST, PORT)) |
| 171 | self.server.start() |
Giampaolo Rodolà | 95bcb93 | 2011-02-25 22:28:24 +0000 | [diff] [blame] | 172 | self.client = poplib.POP3(self.server.host, self.server.port, timeout=3) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 173 | |
| 174 | def tearDown(self): |
Giampaolo Rodolà | 95bcb93 | 2011-02-25 22:28:24 +0000 | [diff] [blame] | 175 | self.client.close() |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 176 | self.server.stop() |
| 177 | |
| 178 | def test_getwelcome(self): |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 179 | self.assertEqual(self.client.getwelcome(), |
| 180 | b'+OK dummy pop3 server ready. <timestamp>') |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 181 | |
| 182 | def test_exceptions(self): |
| 183 | self.assertRaises(poplib.error_proto, self.client._shortcmd, 'echo -err') |
| 184 | |
| 185 | def test_user(self): |
| 186 | self.assertOK(self.client.user('guido')) |
| 187 | self.assertRaises(poplib.error_proto, self.client.user, 'invalid') |
| 188 | |
| 189 | def test_pass_(self): |
| 190 | self.assertOK(self.client.pass_('python')) |
| 191 | self.assertRaises(poplib.error_proto, self.client.user, 'invalid') |
| 192 | |
| 193 | def test_stat(self): |
| 194 | self.assertEqual(self.client.stat(), (10, 100)) |
| 195 | |
| 196 | def test_list(self): |
| 197 | self.assertEqual(self.client.list()[1:], |
| 198 | ([b'1 1', b'2 2', b'3 3', b'4 4', b'5 5'], |
| 199 | 25)) |
| 200 | self.assertTrue(self.client.list('1').endswith(b"OK 1 1")) |
| 201 | |
| 202 | def test_retr(self): |
| 203 | expected = (b'+OK 116 bytes', |
| 204 | [b'From: postmaster@python.org', b'Content-Type: text/plain', |
| 205 | b'MIME-Version: 1.0', b'Subject: Dummy', |
| 206 | b'', b'line1', b'line2', b'line3'], |
| 207 | 113) |
| 208 | foo = self.client.retr('foo') |
| 209 | self.assertEqual(foo, expected) |
| 210 | |
| 211 | def test_dele(self): |
| 212 | self.assertOK(self.client.dele('foo')) |
| 213 | |
| 214 | def test_noop(self): |
| 215 | self.assertOK(self.client.noop()) |
| 216 | |
| 217 | def test_rpop(self): |
| 218 | self.assertOK(self.client.rpop('foo')) |
| 219 | |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 220 | def test_apop(self): |
| 221 | self.assertOK(self.client.apop('foo', 'dummypassword')) |
| 222 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 223 | def test_top(self): |
| 224 | expected = (b'+OK 116 bytes', |
| 225 | [b'From: postmaster@python.org', b'Content-Type: text/plain', |
| 226 | b'MIME-Version: 1.0', b'Subject: Dummy', b'', |
| 227 | b'line1', b'line2', b'line3'], |
| 228 | 113) |
| 229 | self.assertEqual(self.client.top(1, 1), expected) |
| 230 | |
| 231 | def test_uidl(self): |
| 232 | self.client.uidl() |
| 233 | self.client.uidl('foo') |
| 234 | |
Giampaolo Rodolà | 95bcb93 | 2011-02-25 22:28:24 +0000 | [diff] [blame] | 235 | def test_quit(self): |
| 236 | resp = self.client.quit() |
| 237 | self.assertTrue(resp) |
| 238 | self.assertIsNone(self.client.sock) |
| 239 | self.assertIsNone(self.client.file) |
| 240 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 241 | |
| 242 | SUPPORTS_SSL = False |
| 243 | if hasattr(poplib, 'POP3_SSL'): |
| 244 | import ssl |
| 245 | |
| 246 | SUPPORTS_SSL = True |
| 247 | CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem") |
| 248 | |
| 249 | class DummyPOP3_SSLHandler(DummyPOP3Handler): |
| 250 | |
| 251 | def __init__(self, conn): |
| 252 | asynchat.async_chat.__init__(self, conn) |
| 253 | ssl_socket = ssl.wrap_socket(self.socket, certfile=CERTFILE, |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 254 | server_side=True, |
| 255 | do_handshake_on_connect=False) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 256 | self.del_channel() |
| 257 | self.set_socket(ssl_socket) |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 258 | # Must try handshake before calling push() |
| 259 | self._ssl_accepting = True |
| 260 | self._do_ssl_handshake() |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 261 | self.set_terminator(b"\r\n") |
| 262 | self.in_buffer = [] |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 263 | self.push('+OK dummy pop3 server ready. <timestamp>') |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 265 | def _do_ssl_handshake(self): |
| 266 | try: |
| 267 | self.socket.do_handshake() |
| 268 | except ssl.SSLError as err: |
| 269 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 270 | ssl.SSL_ERROR_WANT_WRITE): |
| 271 | return |
| 272 | elif err.args[0] == ssl.SSL_ERROR_EOF: |
| 273 | return self.handle_close() |
| 274 | raise |
| 275 | except socket.error as err: |
| 276 | if err.args[0] == errno.ECONNABORTED: |
| 277 | return self.handle_close() |
| 278 | else: |
| 279 | self._ssl_accepting = False |
| 280 | |
| 281 | def handle_read(self): |
| 282 | if self._ssl_accepting: |
| 283 | self._do_ssl_handshake() |
| 284 | else: |
| 285 | DummyPOP3Handler.handle_read(self) |
| 286 | |
Giampaolo Rodolà | 95bcb93 | 2011-02-25 22:28:24 +0000 | [diff] [blame] | 287 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 288 | class TestPOP3_SSLClass(TestPOP3Class): |
| 289 | # repeat previous tests by using poplib.POP3_SSL |
| 290 | |
| 291 | def setUp(self): |
| 292 | self.server = DummyPOP3Server((HOST, PORT)) |
| 293 | self.server.handler = DummyPOP3_SSLHandler |
| 294 | self.server.start() |
| 295 | self.client = poplib.POP3_SSL(self.server.host, self.server.port) |
| 296 | |
| 297 | def test__all__(self): |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 298 | self.assertIn('POP3_SSL', poplib.__all__) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 299 | |
Giampaolo Rodolà | 42382fe | 2010-08-17 16:09:53 +0000 | [diff] [blame] | 300 | def test_context(self): |
| 301 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 302 | self.assertRaises(ValueError, poplib.POP3_SSL, self.server.host, |
| 303 | self.server.port, keyfile=CERTFILE, context=ctx) |
| 304 | self.assertRaises(ValueError, poplib.POP3_SSL, self.server.host, |
| 305 | self.server.port, certfile=CERTFILE, context=ctx) |
| 306 | self.assertRaises(ValueError, poplib.POP3_SSL, self.server.host, |
| 307 | self.server.port, keyfile=CERTFILE, |
| 308 | certfile=CERTFILE, context=ctx) |
| 309 | |
| 310 | self.client.quit() |
| 311 | self.client = poplib.POP3_SSL(self.server.host, self.server.port, |
| 312 | context=ctx) |
| 313 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
| 314 | self.assertIs(self.client.sock.context, ctx) |
| 315 | self.assertTrue(self.client.noop().startswith(b'+OK')) |
| 316 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 317 | |
| 318 | class TestTimeouts(TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 319 | |
| 320 | def setUp(self): |
| 321 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 322 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Charles-François Natali | 83ef254 | 2011-12-14 19:28:56 +0100 | [diff] [blame] | 323 | self.sock.settimeout(60) # Safety net. Look issue 11812 |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 324 | self.port = test_support.bind_port(self.sock) |
Charles-François Natali | 83ef254 | 2011-12-14 19:28:56 +0100 | [diff] [blame] | 325 | self.thread = threading.Thread(target=self.server, args=(self.evt,self.sock)) |
| 326 | self.thread.setDaemon(True) |
| 327 | self.thread.start() |
| 328 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 329 | |
| 330 | def tearDown(self): |
Charles-François Natali | 83ef254 | 2011-12-14 19:28:56 +0100 | [diff] [blame] | 331 | self.thread.join() |
| 332 | del self.thread # Clear out any dangling Thread objects. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 333 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 334 | def server(self, evt, serv): |
| 335 | serv.listen(5) |
Charles-François Natali | 83ef254 | 2011-12-14 19:28:56 +0100 | [diff] [blame] | 336 | evt.set() |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 337 | try: |
| 338 | conn, addr = serv.accept() |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 339 | conn.send(b"+ Hola mundo\n") |
| 340 | conn.close() |
Charles-François Natali | 83ef254 | 2011-12-14 19:28:56 +0100 | [diff] [blame] | 341 | except socket.timeout: |
| 342 | pass |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 343 | finally: |
| 344 | serv.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 345 | |
| 346 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 347 | self.assertTrue(socket.getdefaulttimeout() is None) |
| 348 | socket.setdefaulttimeout(30) |
| 349 | try: |
Charles-François Natali | 83ef254 | 2011-12-14 19:28:56 +0100 | [diff] [blame] | 350 | pop = poplib.POP3(HOST, self.port) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 351 | finally: |
| 352 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 353 | self.assertEqual(pop.sock.gettimeout(), 30) |
| 354 | pop.sock.close() |
| 355 | |
| 356 | def testTimeoutNone(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 357 | self.assertTrue(socket.getdefaulttimeout() is None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 358 | socket.setdefaulttimeout(30) |
| 359 | try: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 360 | pop = poplib.POP3(HOST, self.port, timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 361 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 362 | socket.setdefaulttimeout(None) |
| 363 | self.assertTrue(pop.sock.gettimeout() is None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 364 | pop.sock.close() |
| 365 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 366 | def testTimeoutValue(self): |
Charles-François Natali | 83ef254 | 2011-12-14 19:28:56 +0100 | [diff] [blame] | 367 | pop = poplib.POP3(HOST, self.port, timeout=30) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 368 | self.assertEqual(pop.sock.gettimeout(), 30) |
| 369 | pop.sock.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 370 | |
| 371 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 372 | def test_main(): |
| 373 | tests = [TestPOP3Class, TestTimeouts] |
| 374 | if SUPPORTS_SSL: |
| 375 | tests.append(TestPOP3_SSLClass) |
| 376 | thread_info = test_support.threading_setup() |
| 377 | try: |
| 378 | test_support.run_unittest(*tests) |
| 379 | finally: |
| 380 | test_support.threading_cleanup(*thread_info) |
| 381 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 382 | |
| 383 | if __name__ == '__main__': |
| 384 | test_main() |