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