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 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 111 | |
| 112 | class DummyPOP3Server(asyncore.dispatcher, threading.Thread): |
| 113 | |
| 114 | handler = DummyPOP3Handler |
| 115 | |
| 116 | def __init__(self, address, af=socket.AF_INET): |
| 117 | threading.Thread.__init__(self) |
| 118 | asyncore.dispatcher.__init__(self) |
| 119 | self.create_socket(af, socket.SOCK_STREAM) |
| 120 | self.bind(address) |
| 121 | self.listen(5) |
| 122 | self.active = False |
| 123 | self.active_lock = threading.Lock() |
| 124 | self.host, self.port = self.socket.getsockname()[:2] |
| 125 | |
| 126 | def start(self): |
| 127 | assert not self.active |
| 128 | self.__flag = threading.Event() |
| 129 | threading.Thread.start(self) |
| 130 | self.__flag.wait() |
| 131 | |
| 132 | def run(self): |
| 133 | self.active = True |
| 134 | self.__flag.set() |
| 135 | while self.active and asyncore.socket_map: |
| 136 | self.active_lock.acquire() |
| 137 | asyncore.loop(timeout=0.1, count=1) |
| 138 | self.active_lock.release() |
| 139 | asyncore.close_all(ignore_all=True) |
| 140 | |
| 141 | def stop(self): |
| 142 | assert self.active |
| 143 | self.active = False |
| 144 | self.join() |
| 145 | |
| 146 | def handle_accept(self): |
| 147 | conn, addr = self.accept() |
| 148 | self.handler = self.handler(conn) |
| 149 | self.close() |
| 150 | |
| 151 | def handle_connect(self): |
| 152 | self.close() |
| 153 | handle_read = handle_connect |
| 154 | |
| 155 | def writable(self): |
| 156 | return 0 |
| 157 | |
| 158 | def handle_error(self): |
| 159 | raise |
| 160 | |
| 161 | |
| 162 | class TestPOP3Class(TestCase): |
| 163 | def assertOK(self, resp): |
| 164 | self.assertTrue(resp.startswith(b"+OK")) |
| 165 | |
| 166 | def setUp(self): |
| 167 | self.server = DummyPOP3Server((HOST, PORT)) |
| 168 | self.server.start() |
| 169 | self.client = poplib.POP3(self.server.host, self.server.port) |
| 170 | |
| 171 | def tearDown(self): |
| 172 | self.client.quit() |
| 173 | self.server.stop() |
| 174 | |
| 175 | def test_getwelcome(self): |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 176 | self.assertEqual(self.client.getwelcome(), |
| 177 | b'+OK dummy pop3 server ready. <timestamp>') |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 178 | |
| 179 | def test_exceptions(self): |
| 180 | self.assertRaises(poplib.error_proto, self.client._shortcmd, 'echo -err') |
| 181 | |
| 182 | def test_user(self): |
| 183 | self.assertOK(self.client.user('guido')) |
| 184 | self.assertRaises(poplib.error_proto, self.client.user, 'invalid') |
| 185 | |
| 186 | def test_pass_(self): |
| 187 | self.assertOK(self.client.pass_('python')) |
| 188 | self.assertRaises(poplib.error_proto, self.client.user, 'invalid') |
| 189 | |
| 190 | def test_stat(self): |
| 191 | self.assertEqual(self.client.stat(), (10, 100)) |
| 192 | |
| 193 | def test_list(self): |
| 194 | self.assertEqual(self.client.list()[1:], |
| 195 | ([b'1 1', b'2 2', b'3 3', b'4 4', b'5 5'], |
| 196 | 25)) |
| 197 | self.assertTrue(self.client.list('1').endswith(b"OK 1 1")) |
| 198 | |
| 199 | def test_retr(self): |
| 200 | expected = (b'+OK 116 bytes', |
| 201 | [b'From: postmaster@python.org', b'Content-Type: text/plain', |
| 202 | b'MIME-Version: 1.0', b'Subject: Dummy', |
| 203 | b'', b'line1', b'line2', b'line3'], |
| 204 | 113) |
| 205 | foo = self.client.retr('foo') |
| 206 | self.assertEqual(foo, expected) |
| 207 | |
| 208 | def test_dele(self): |
| 209 | self.assertOK(self.client.dele('foo')) |
| 210 | |
| 211 | def test_noop(self): |
| 212 | self.assertOK(self.client.noop()) |
| 213 | |
| 214 | def test_rpop(self): |
| 215 | self.assertOK(self.client.rpop('foo')) |
| 216 | |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 217 | def test_apop(self): |
| 218 | self.assertOK(self.client.apop('foo', 'dummypassword')) |
| 219 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 220 | def test_top(self): |
| 221 | expected = (b'+OK 116 bytes', |
| 222 | [b'From: postmaster@python.org', b'Content-Type: text/plain', |
| 223 | b'MIME-Version: 1.0', b'Subject: Dummy', b'', |
| 224 | b'line1', b'line2', b'line3'], |
| 225 | 113) |
| 226 | self.assertEqual(self.client.top(1, 1), expected) |
| 227 | |
| 228 | def test_uidl(self): |
| 229 | self.client.uidl() |
| 230 | self.client.uidl('foo') |
| 231 | |
| 232 | |
| 233 | SUPPORTS_SSL = False |
| 234 | if hasattr(poplib, 'POP3_SSL'): |
| 235 | import ssl |
| 236 | |
| 237 | SUPPORTS_SSL = True |
| 238 | CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem") |
| 239 | |
| 240 | class DummyPOP3_SSLHandler(DummyPOP3Handler): |
| 241 | |
| 242 | def __init__(self, conn): |
| 243 | asynchat.async_chat.__init__(self, conn) |
| 244 | ssl_socket = ssl.wrap_socket(self.socket, certfile=CERTFILE, |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 245 | server_side=True, |
| 246 | do_handshake_on_connect=False) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 247 | self.del_channel() |
| 248 | self.set_socket(ssl_socket) |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 249 | # Must try handshake before calling push() |
| 250 | self._ssl_accepting = True |
| 251 | self._do_ssl_handshake() |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 252 | self.set_terminator(b"\r\n") |
| 253 | self.in_buffer = [] |
Mark Dickinson | ea1158f | 2009-08-06 16:06:25 +0000 | [diff] [blame] | 254 | self.push('+OK dummy pop3 server ready. <timestamp>') |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | d3f8ab8 | 2010-04-24 21:26:44 +0000 | [diff] [blame] | 256 | def _do_ssl_handshake(self): |
| 257 | try: |
| 258 | self.socket.do_handshake() |
| 259 | except ssl.SSLError as err: |
| 260 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 261 | ssl.SSL_ERROR_WANT_WRITE): |
| 262 | return |
| 263 | elif err.args[0] == ssl.SSL_ERROR_EOF: |
| 264 | return self.handle_close() |
| 265 | raise |
| 266 | except socket.error as err: |
| 267 | if err.args[0] == errno.ECONNABORTED: |
| 268 | return self.handle_close() |
| 269 | else: |
| 270 | self._ssl_accepting = False |
| 271 | |
| 272 | def handle_read(self): |
| 273 | if self._ssl_accepting: |
| 274 | self._do_ssl_handshake() |
| 275 | else: |
| 276 | DummyPOP3Handler.handle_read(self) |
| 277 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 278 | class TestPOP3_SSLClass(TestPOP3Class): |
| 279 | # repeat previous tests by using poplib.POP3_SSL |
| 280 | |
| 281 | def setUp(self): |
| 282 | self.server = DummyPOP3Server((HOST, PORT)) |
| 283 | self.server.handler = DummyPOP3_SSLHandler |
| 284 | self.server.start() |
| 285 | self.client = poplib.POP3_SSL(self.server.host, self.server.port) |
| 286 | |
| 287 | def test__all__(self): |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 288 | self.assertIn('POP3_SSL', poplib.__all__) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 289 | |
| 290 | |
| 291 | class TestTimeouts(TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 292 | |
| 293 | def setUp(self): |
| 294 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 295 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 296 | self.sock.settimeout(3) |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 297 | self.port = test_support.bind_port(self.sock) |
| 298 | threading.Thread(target=self.server, args=(self.evt,self.sock)).start() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 299 | time.sleep(.1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 300 | |
| 301 | def tearDown(self): |
| 302 | self.evt.wait() |
| 303 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 304 | def server(self, evt, serv): |
| 305 | serv.listen(5) |
| 306 | try: |
| 307 | conn, addr = serv.accept() |
| 308 | except socket.timeout: |
| 309 | pass |
| 310 | else: |
| 311 | conn.send(b"+ Hola mundo\n") |
| 312 | conn.close() |
| 313 | finally: |
| 314 | serv.close() |
| 315 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 316 | |
| 317 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 318 | self.assertTrue(socket.getdefaulttimeout() is None) |
| 319 | socket.setdefaulttimeout(30) |
| 320 | try: |
| 321 | pop = poplib.POP3("localhost", self.port) |
| 322 | finally: |
| 323 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 324 | self.assertEqual(pop.sock.gettimeout(), 30) |
| 325 | pop.sock.close() |
| 326 | |
| 327 | def testTimeoutNone(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 328 | self.assertTrue(socket.getdefaulttimeout() is None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 329 | socket.setdefaulttimeout(30) |
| 330 | try: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 331 | pop = poplib.POP3(HOST, self.port, timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 332 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 333 | socket.setdefaulttimeout(None) |
| 334 | self.assertTrue(pop.sock.gettimeout() is None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 335 | pop.sock.close() |
| 336 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 337 | def testTimeoutValue(self): |
| 338 | pop = poplib.POP3("localhost", self.port, timeout=30) |
| 339 | self.assertEqual(pop.sock.gettimeout(), 30) |
| 340 | pop.sock.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 341 | |
| 342 | |
Christian Heimes | d395629 | 2008-11-05 19:48:27 +0000 | [diff] [blame] | 343 | def test_main(): |
| 344 | tests = [TestPOP3Class, TestTimeouts] |
| 345 | if SUPPORTS_SSL: |
| 346 | tests.append(TestPOP3_SSLClass) |
| 347 | thread_info = test_support.threading_setup() |
| 348 | try: |
| 349 | test_support.run_unittest(*tests) |
| 350 | finally: |
| 351 | test_support.threading_cleanup(*thread_info) |
| 352 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 353 | |
| 354 | if __name__ == '__main__': |
| 355 | test_main() |