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