Debian Codegraph @

Lib/test/test_poplib.py

raw
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
6 import poplib
7 import socket
8 import os
9 import errno
10 import threading
11
12 import unittest
13 from unittest import TestCase, skipUnless
14 from test import support as test_support
15 from test.support import hashlib_helper
16 from test.support import socket_helper
17 from test.support import threading_helper
18 from test.support import asynchat
19 from test.support import asyncore
20
21
22 test_support.requires_working_socket(module=True)
23
24 HOST = socket_helper.HOST
25 PORT = 0
26
27 SUPPORTS_SSL = False
28 if hasattr(poplib, 'POP3_SSL'):
29 import ssl
30
31 SUPPORTS_SSL = True
32 CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "certdata", "keycert3.pem")
33 CAFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "certdata", "pycacert.pem")
34
35 requires_ssl = skipUnless(SUPPORTS_SSL, 'SSL not supported')
36
37 # the dummy data returned by server when LIST and RETR commands are issued
38 LIST_RESP = b'1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n.\r\n'
39 RETR_RESP = b"""From: [email protected]\
40 \r\nContent-Type: text/plain\r\n\
41 MIME-Version: 1.0\r\n\
42 Subject: Dummy\r\n\
43 \r\n\
44 line1\r\n\
45 line2\r\n\
46 line3\r\n\
47 .\r\n"""
48
49
50 class DummyPOP3Handler(asynchat.async_chat):
51
52 CAPAS = {'UIDL': [], 'IMPLEMENTATION': ['python-testlib-pop-server']}
53 enable_UTF8 = False
54
55 def __init__(self, conn):
56 asynchat.async_chat.__init__(self, conn)
57 self.set_terminator(b"\r\n")
58 self.in_buffer = []
59 self.push('+OK dummy pop3 server ready. <timestamp>')
60 self.tls_active = False
61 self.tls_starting = False
62
63 def collect_incoming_data(self, data):
64 self.in_buffer.append(data)
65
66 def found_terminator(self):
67 line = b''.join(self.in_buffer)
68 line = str(line, 'ISO-8859-1')
69 self.in_buffer = []
70 cmd = line.split(' ')[0].lower()
71 space = line.find(' ')
72 if space != -1:
73 arg = line[space + 1:]
74 else:
75 arg = ""
76 if hasattr(self, 'cmd_' + cmd):
77 method = getattr(self, 'cmd_' + cmd)
78 method(arg)
79 else:
80 self.push('-ERR unrecognized POP3 command "%s".' %cmd)
81
82 def handle_error(self):
83 raise
84
85 def push(self, data):
86 asynchat.async_chat.push(self, data.encode("ISO-8859-1") + b'\r\n')
87
88 def cmd_echo(self, arg):
89 # sends back the received string (used by the test suite)
90 self.push(arg)
91
92 def cmd_user(self, arg):
93 if arg != "guido":
94 self.push("-ERR no such user")
95 self.push('+OK password required')
96
97 def cmd_pass(self, arg):
98 if arg != "python":
99 self.push("-ERR wrong password")
100 self.push('+OK 10 messages')
101
102 def cmd_stat(self, arg):
103 self.push('+OK 10 100')
104
105 def cmd_list(self, arg):
106 if arg:
107 self.push('+OK %s %s' % (arg, arg))
108 else:
109 self.push('+OK')
110 asynchat.async_chat.push(self, LIST_RESP)
111
112 cmd_uidl = cmd_list
113
114 def cmd_retr(self, arg):
115 self.push('+OK %s bytes' %len(RETR_RESP))
116 asynchat.async_chat.push(self, RETR_RESP)
117
118 cmd_top = cmd_retr
119
120 def cmd_dele(self, arg):
121 self.push('+OK message marked for deletion.')
122
123 def cmd_noop(self, arg):
124 self.push('+OK done nothing.')
125
126 def cmd_rpop(self, arg):
127 self.push('+OK done nothing.')
128
129 def cmd_apop(self, arg):
130 self.push('+OK done nothing.')
131
132 def cmd_quit(self, arg):
133 self.push('+OK closing.')
134 self.close_when_done()
135
136 def _get_capas(self):
137 _capas = dict(self.CAPAS)
138 if not self.tls_active and SUPPORTS_SSL:
139 _capas['STLS'] = []
140 return _capas
141
142 def cmd_capa(self, arg):
143 self.push('+OK Capability list follows')
144 if self._get_capas():
145 for cap, params in self._get_capas().items():
146 _ln = [cap]
147 if params:
148 _ln.extend(params)
149 self.push(' '.join(_ln))
150 self.push('.')
151
152 def cmd_utf8(self, arg):
153 self.push('+OK I know RFC6856'
154 if self.enable_UTF8
155 else '-ERR What is UTF8?!')
156
157 if SUPPORTS_SSL:
158
159 def cmd_stls(self, arg):
160 if self.tls_active is False:
161 self.push('+OK Begin TLS negotiation')
162 context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
163 context.load_cert_chain(CERTFILE)
164 tls_sock = context.wrap_socket(self.socket,
165 server_side=True,
166 do_handshake_on_connect=False,
167 suppress_ragged_eofs=False)
168 self.del_channel()
169 self.set_socket(tls_sock)
170 self.tls_active = True
171 self.tls_starting = True
172 self.in_buffer = []
173 self._do_tls_handshake()
174 else:
175 self.push('-ERR Command not permitted when TLS active')
176
177 def _do_tls_handshake(self):
178 try:
179 self.socket.do_handshake()
180 except ssl.SSLError as err:
181 if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
182 ssl.SSL_ERROR_WANT_WRITE):
183 return
184 elif err.args[0] == ssl.SSL_ERROR_EOF:
185 return self.handle_close()
186 # TODO: SSLError does not expose alert information
187 elif ("SSLV3_ALERT_BAD_CERTIFICATE" in err.args[1] or
188 "SSLV3_ALERT_CERTIFICATE_UNKNOWN" in err.args[1]):
189 return self.handle_close()
190 raise
191 except OSError as err:
192 if err.args[0] == errno.ECONNABORTED:
193 return self.handle_close()
194 else:
195 self.tls_active = True
196 self.tls_starting = False
197
198 def handle_read(self):
199 if self.tls_starting:
200 self._do_tls_handshake()
201 else:
202 try:
203 asynchat.async_chat.handle_read(self)
204 except ssl.SSLEOFError:
205 self.handle_close()
206
207 class DummyPOP3Server(asyncore.dispatcher, threading.Thread):
208
209 handler = DummyPOP3Handler
210
211 def __init__(self, address, af=socket.AF_INET):
212 threading.Thread.__init__(self)
213 asyncore.dispatcher.__init__(self)
214 self.daemon = True
215 self.create_socket(af, socket.SOCK_STREAM)
216 self.bind(address)
217 self.listen(5)
218 self.active = False
219 self.active_lock = threading.Lock()
220 self.host, self.port = self.socket.getsockname()[:2]
221 self.handler_instance = None
222
223 def start(self):
224 assert not self.active
225 self.__flag = threading.Event()
226 threading.Thread.start(self)
227 self.__flag.wait()
228
229 def run(self):
230 self.active = True
231 self.__flag.set()
232 try:
233 while self.active and asyncore.socket_map:
234 with self.active_lock:
235 asyncore.loop(timeout=0.1, count=1)
236 finally:
237 asyncore.close_all(ignore_all=True)
238
239 def stop(self):
240 assert self.active
241 self.active = False
242 self.join()
243
244 def handle_accepted(self, conn, addr):
245 self.handler_instance = self.handler(conn)
246
247 def handle_connect(self):
248 self.close()
249 handle_read = handle_connect
250
251 def writable(self):
252 return 0
253
254 def handle_error(self):
255 raise
256
257
258 class TestPOP3Class(TestCase):
259 def assertOK(self, resp):
260 self.assertStartsWith(resp, b"+OK")
261
262 def setUp(self):
263 self.server = DummyPOP3Server((HOST, PORT))
264 self.server.start()
265 self.client = poplib.POP3(self.server.host, self.server.port,
266 timeout=test_support.LOOPBACK_TIMEOUT)
267
268 def tearDown(self):
269 self.client.close()
270 self.server.stop()
271 # Explicitly clear the attribute to prevent dangling thread
272 self.server = None
273
274 def test_getwelcome(self):
275 self.assertEqual(self.client.getwelcome(),
276 b'+OK dummy pop3 server ready. <timestamp>')
277
278 def test_exceptions(self):
279 self.assertRaises(poplib.error_proto, self.client._shortcmd, 'echo -err')
280
281 def test_user(self):
282 self.assertOK(self.client.user('guido'))
283 self.assertRaises(poplib.error_proto, self.client.user, 'invalid')
284
285 def test_pass_(self):
286 self.assertOK(self.client.pass_('python'))
287 self.assertRaises(poplib.error_proto, self.client.user, 'invalid')
288
289 def test_stat(self):
290 self.assertEqual(self.client.stat(), (10, 100))
291
292 original_shortcmd = self.client._shortcmd
293 def mock_shortcmd_invalid_format(cmd):
294 if cmd == 'STAT':
295 return b'+OK'
296 return original_shortcmd(cmd)
297
298 self.client._shortcmd = mock_shortcmd_invalid_format
299 with self.assertRaises(poplib.error_proto):
300 self.client.stat()
301
302 def mock_shortcmd_invalid_data(cmd):
303 if cmd == 'STAT':
304 return b'+OK abc def'
305 return original_shortcmd(cmd)
306
307 self.client._shortcmd = mock_shortcmd_invalid_data
308 with self.assertRaises(poplib.error_proto):
309 self.client.stat()
310
311 def mock_shortcmd_extra_fields(cmd):
312 if cmd == 'STAT':
313 return b'+OK 1 2 3 4 5'
314 return original_shortcmd(cmd)
315
316 self.client._shortcmd = mock_shortcmd_extra_fields
317
318 result = self.client.stat()
319 self.assertEqual(result, (1, 2))
320
321 self.client._shortcmd = original_shortcmd
322
323 def test_list(self):
324 self.assertEqual(self.client.list()[1:],
325 ([b'1 1', b'2 2', b'3 3', b'4 4', b'5 5'],
326 25))
327 self.assertEndsWith(self.client.list('1'), b"OK 1 1")
328
329 def test_retr(self):
330 expected = (b'+OK 116 bytes',
331 [b'From: [email protected]', b'Content-Type: text/plain',
332 b'MIME-Version: 1.0', b'Subject: Dummy',
333 b'', b'line1', b'line2', b'line3'],
334 113)
335 foo = self.client.retr('foo')
336 self.assertEqual(foo, expected)
337
338 def test_too_long_lines(self):
339 self.assertRaises(poplib.error_proto, self.client._shortcmd,
340 'echo +%s' % ((poplib._MAXLINE + 10) * 'a'))
341
342 def test_dele(self):
343 self.assertOK(self.client.dele('foo'))
344
345 def test_noop(self):
346 self.assertOK(self.client.noop())
347
348 def test_rpop(self):
349 self.assertOK(self.client.rpop('foo'))
350
351 @hashlib_helper.requires_hashdigest('md5', openssl=True)
352 def test_apop_normal(self):
353 self.assertOK(self.client.apop('foo', 'dummypassword'))
354
355 @hashlib_helper.requires_hashdigest('md5', openssl=True)
356 def test_apop_REDOS(self):
357 # Replace welcome with very long evil welcome.
358 # NB The upper bound on welcome length is currently 2048.
359 # At this length, evil input makes each apop call take
360 # on the order of milliseconds instead of microseconds.
361 evil_welcome = b'+OK' + (b'<' * 1000000)
362 with test_support.swap_attr(self.client, 'welcome', evil_welcome):
363 # The evil welcome is invalid, so apop should throw.
364 self.assertRaises(poplib.error_proto, self.client.apop, 'a', 'kb')
365
366 def test_top(self):
367 expected = (b'+OK 116 bytes',
368 [b'From: [email protected]', b'Content-Type: text/plain',
369 b'MIME-Version: 1.0', b'Subject: Dummy', b'',
370 b'line1', b'line2', b'line3'],
371 113)
372 self.assertEqual(self.client.top(1, 1), expected)
373
374 def test_uidl(self):
375 self.client.uidl()
376 self.client.uidl('foo')
377
378 def test_utf8_raises_if_unsupported(self):
379 self.server.handler.enable_UTF8 = False
380 self.assertRaises(poplib.error_proto, self.client.utf8)
381
382 def test_utf8(self):
383 self.server.handler.enable_UTF8 = True
384 expected = b'+OK I know RFC6856'
385 result = self.client.utf8()
386 self.assertEqual(result, expected)
387
388 def test_capa(self):
389 capa = self.client.capa()
390 self.assertTrue('IMPLEMENTATION' in capa.keys())
391
392 def test_quit(self):
393 resp = self.client.quit()
394 self.assertTrue(resp)
395 self.assertIsNone(self.client.sock)
396 self.assertIsNone(self.client.file)
397
398 @requires_ssl
399 def test_stls_capa(self):
400 capa = self.client.capa()
401 self.assertTrue('STLS' in capa.keys())
402
403 @requires_ssl
404 def test_stls(self):
405 expected = b'+OK Begin TLS negotiation'
406 resp = self.client.stls()
407 self.assertEqual(resp, expected)
408
409 @requires_ssl
410 def test_stls_context(self):
411 expected = b'+OK Begin TLS negotiation'
412 ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
413 ctx.load_verify_locations(CAFILE)
414 self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
415 self.assertEqual(ctx.check_hostname, True)
416 with self.assertRaises(ssl.CertificateError):
417 resp = self.client.stls(context=ctx)
418 self.client = poplib.POP3("localhost", self.server.port,
419 timeout=test_support.LOOPBACK_TIMEOUT)
420 resp = self.client.stls(context=ctx)
421 self.assertEqual(resp, expected)
422
423
424 if SUPPORTS_SSL:
425 from test.test_ftplib import SSLConnection
426
427 class DummyPOP3_SSLHandler(SSLConnection, DummyPOP3Handler):
428
429 def __init__(self, conn):
430 asynchat.async_chat.__init__(self, conn)
431 self.secure_connection()
432 self.set_terminator(b"\r\n")
433 self.in_buffer = []
434 self.push('+OK dummy pop3 server ready. <timestamp>')
435 self.tls_active = True
436 self.tls_starting = False
437
438
439 @requires_ssl
440 class TestPOP3_SSLClass(TestPOP3Class):
441 # repeat previous tests by using poplib.POP3_SSL
442
443 def setUp(self):
444 self.server = DummyPOP3Server((HOST, PORT))
445 self.server.handler = DummyPOP3_SSLHandler
446 self.server.start()
447 self.client = poplib.POP3_SSL(self.server.host, self.server.port)
448
449 def test__all__(self):
450 self.assertIn('POP3_SSL', poplib.__all__)
451
452 def test_context(self):
453 ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
454 ctx.check_hostname = False
455 ctx.verify_mode = ssl.CERT_NONE
456
457 self.client.quit()
458 self.client = poplib.POP3_SSL(self.server.host, self.server.port,
459 context=ctx)
460 self.assertIsInstance(self.client.sock, ssl.SSLSocket)
461 self.assertIs(self.client.sock.context, ctx)
462 self.assertStartsWith(self.client.noop(), b'+OK')
463
464 def test_stls(self):
465 self.assertRaises(poplib.error_proto, self.client.stls)
466
467 test_stls_context = test_stls
468
469 def test_stls_capa(self):
470 capa = self.client.capa()
471 self.assertFalse('STLS' in capa.keys())
472
473
474 @requires_ssl
475 class TestPOP3_TLSClass(TestPOP3Class):
476 # repeat previous tests by using poplib.POP3.stls()
477
478 def setUp(self):
479 self.server = DummyPOP3Server((HOST, PORT))
480 self.server.start()
481 self.client = poplib.POP3(self.server.host, self.server.port,
482 timeout=test_support.LOOPBACK_TIMEOUT)
483 self.client.stls()
484
485 def tearDown(self):
486 if self.client.file is not None and self.client.sock is not None:
487 try:
488 self.client.quit()
489 except poplib.error_proto:
490 # happens in the test_too_long_lines case; the overlong
491 # response will be treated as response to QUIT and raise
492 # this exception
493 self.client.close()
494 self.server.stop()
495 # Explicitly clear the attribute to prevent dangling thread
496 self.server = None
497
498 def test_stls(self):
499 self.assertRaises(poplib.error_proto, self.client.stls)
500
501 test_stls_context = test_stls
502
503 def test_stls_capa(self):
504 capa = self.client.capa()
505 self.assertFalse(b'STLS' in capa.keys())
506
507
508 class TestTimeouts(TestCase):
509
510 def setUp(self):
511 self.evt = threading.Event()
512 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
513 self.sock.settimeout(60) # Safety net. Look issue 11812
514 self.port = socket_helper.bind_port(self.sock)
515 self.thread = threading.Thread(target=self.server, args=(self.evt, self.sock))
516 self.thread.daemon = True
517 self.thread.start()
518 self.evt.wait()
519
520 def tearDown(self):
521 self.thread.join()
522 # Explicitly clear the attribute to prevent dangling thread
523 self.thread = None
524
525 def server(self, evt, serv):
526 serv.listen()
527 evt.set()
528 try:
529 conn, addr = serv.accept()
530 conn.send(b"+ Hola mundo\n")
531 conn.close()
532 except TimeoutError:
533 pass
534 finally:
535 serv.close()
536
537 def testTimeoutDefault(self):
538 self.assertIsNone(socket.getdefaulttimeout())
539 socket.setdefaulttimeout(test_support.LOOPBACK_TIMEOUT)
540 try:
541 pop = poplib.POP3(HOST, self.port)
542 finally:
543 socket.setdefaulttimeout(None)
544 self.assertEqual(pop.sock.gettimeout(), test_support.LOOPBACK_TIMEOUT)
545 pop.close()
546
547 def testTimeoutNone(self):
548 self.assertIsNone(socket.getdefaulttimeout())
549 socket.setdefaulttimeout(30)
550 try:
551 pop = poplib.POP3(HOST, self.port, timeout=None)
552 finally:
553 socket.setdefaulttimeout(None)
554 self.assertIsNone(pop.sock.gettimeout())
555 pop.close()
556
557 def testTimeoutValue(self):
558 pop = poplib.POP3(HOST, self.port, timeout=test_support.LOOPBACK_TIMEOUT)
559 self.assertEqual(pop.sock.gettimeout(), test_support.LOOPBACK_TIMEOUT)
560 pop.close()
561 with self.assertRaises(ValueError):
562 poplib.POP3(HOST, self.port, timeout=0)
563
564
565 def setUpModule():
566 thread_info = threading_helper.threading_setup()
567 unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)
568
569
570 if __name__ == '__main__':
571 unittest.main()