blob: d2143759ba6652ef1938bce53d4c649e2d421997 [file] [log] [blame]
Benjamin Peterson76f71a52008-10-11 17:25:36 +00001"""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
Facundo Batistab20c5002007-03-27 18:50:29 +00006import poplib
Benjamin Peterson76f71a52008-10-11 17:25:36 +00007import asyncore
8import asynchat
9import socket
10import os
Facundo Batistab20c5002007-03-27 18:50:29 +000011import time
Antoine Pitroufc69af12010-04-24 20:04:58 +000012import errno
Facundo Batistab20c5002007-03-27 18:50:29 +000013
Serhiy Storchaka32e23e72013-11-03 23:15:46 +020014from unittest import TestCase, skipUnless
Facundo Batistab20c5002007-03-27 18:50:29 +000015from test import test_support
Benjamin Peterson76f71a52008-10-11 17:25:36 +000016from test.test_support import HOST
Victor Stinner6a102812010-04-27 23:55:59 +000017threading = test_support.import_module('threading')
Facundo Batistab20c5002007-03-27 18:50:29 +000018
19
Benjamin Peterson76f71a52008-10-11 17:25:36 +000020# the dummy data returned by server when LIST and RETR commands are issued
21LIST_RESP = '1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n.\r\n'
22RETR_RESP = """From: postmaster@python.org\
23\r\nContent-Type: text/plain\r\n\
24MIME-Version: 1.0\r\n\
25Subject: Dummy\r\n\
26\r\n\
27line1\r\n\
28line2\r\n\
29line3\r\n\
30.\r\n"""
Facundo Batistab20c5002007-03-27 18:50:29 +000031
Benjamin Peterson76f71a52008-10-11 17:25:36 +000032
33class DummyPOP3Handler(asynchat.async_chat):
34
35 def __init__(self, conn):
36 asynchat.async_chat.__init__(self, conn)
37 self.set_terminator("\r\n")
38 self.in_buffer = []
39 self.push('+OK dummy pop3 server ready.')
40
41 def collect_incoming_data(self, data):
42 self.in_buffer.append(data)
43
44 def found_terminator(self):
45 line = ''.join(self.in_buffer)
46 self.in_buffer = []
47 cmd = line.split(' ')[0].lower()
48 space = line.find(' ')
49 if space != -1:
50 arg = line[space + 1:]
51 else:
52 arg = ""
53 if hasattr(self, 'cmd_' + cmd):
54 method = getattr(self, 'cmd_' + cmd)
55 method(arg)
56 else:
57 self.push('-ERR unrecognized POP3 command "%s".' %cmd)
58
59 def handle_error(self):
60 raise
61
62 def push(self, data):
63 asynchat.async_chat.push(self, data + '\r\n')
64
65 def cmd_echo(self, arg):
66 # sends back the received string (used by the test suite)
67 self.push(arg)
68
69 def cmd_user(self, arg):
70 if arg != "guido":
71 self.push("-ERR no such user")
72 self.push('+OK password required')
73
74 def cmd_pass(self, arg):
75 if arg != "python":
76 self.push("-ERR wrong password")
77 self.push('+OK 10 messages')
78
79 def cmd_stat(self, arg):
80 self.push('+OK 10 100')
81
82 def cmd_list(self, arg):
83 if arg:
84 self.push('+OK %s %s' %(arg, arg))
85 else:
86 self.push('+OK')
87 asynchat.async_chat.push(self, LIST_RESP)
88
89 cmd_uidl = cmd_list
90
91 def cmd_retr(self, arg):
92 self.push('+OK %s bytes' %len(RETR_RESP))
93 asynchat.async_chat.push(self, RETR_RESP)
94
95 cmd_top = cmd_retr
96
97 def cmd_dele(self, arg):
98 self.push('+OK message marked for deletion.')
99
100 def cmd_noop(self, arg):
101 self.push('+OK done nothing.')
102
103 def cmd_rpop(self, arg):
104 self.push('+OK done nothing.')
105
106
107class DummyPOP3Server(asyncore.dispatcher, threading.Thread):
108
109 handler = DummyPOP3Handler
110
111 def __init__(self, address, af=socket.AF_INET):
112 threading.Thread.__init__(self)
113 asyncore.dispatcher.__init__(self)
114 self.create_socket(af, socket.SOCK_STREAM)
115 self.bind(address)
116 self.listen(5)
117 self.active = False
118 self.active_lock = threading.Lock()
119 self.host, self.port = self.socket.getsockname()[:2]
120
121 def start(self):
122 assert not self.active
123 self.__flag = threading.Event()
124 threading.Thread.start(self)
125 self.__flag.wait()
126
127 def run(self):
128 self.active = True
129 self.__flag.set()
130 while self.active and asyncore.socket_map:
131 self.active_lock.acquire()
132 asyncore.loop(timeout=0.1, count=1)
133 self.active_lock.release()
134 asyncore.close_all(ignore_all=True)
135
136 def stop(self):
137 assert self.active
138 self.active = False
139 self.join()
140
141 def handle_accept(self):
142 conn, addr = self.accept()
143 self.handler = self.handler(conn)
144 self.close()
145
146 def handle_connect(self):
147 self.close()
148 handle_read = handle_connect
149
150 def writable(self):
151 return 0
152
153 def handle_error(self):
154 raise
155
156
157class TestPOP3Class(TestCase):
158
159 def assertOK(self, resp):
160 self.assertTrue(resp.startswith("+OK"))
161
162 def setUp(self):
163 self.server = DummyPOP3Server((HOST, 0))
164 self.server.start()
165 self.client = poplib.POP3(self.server.host, self.server.port)
166
167 def tearDown(self):
168 self.client.quit()
169 self.server.stop()
170
171 def test_getwelcome(self):
172 self.assertEqual(self.client.getwelcome(), '+OK dummy pop3 server ready.')
173
174 def test_exceptions(self):
175 self.assertRaises(poplib.error_proto, self.client._shortcmd, 'echo -err')
176
177 def test_user(self):
178 self.assertOK(self.client.user('guido'))
179 self.assertRaises(poplib.error_proto, self.client.user, 'invalid')
180
181 def test_pass_(self):
182 self.assertOK(self.client.pass_('python'))
183 self.assertRaises(poplib.error_proto, self.client.user, 'invalid')
184
185 def test_stat(self):
186 self.assertEqual(self.client.stat(), (10, 100))
187
188 def test_list(self):
189 self.assertEqual(self.client.list()[1:],
190 (['1 1', '2 2', '3 3', '4 4', '5 5'], 25))
191 self.assertTrue(self.client.list('1').endswith("OK 1 1"))
192
193 def test_retr(self):
194 expected = ('+OK 116 bytes',
195 ['From: postmaster@python.org', 'Content-Type: text/plain',
196 'MIME-Version: 1.0', 'Subject: Dummy',
197 '', 'line1', 'line2', 'line3'],
198 113)
199 self.assertEqual(self.client.retr('foo'), expected)
200
Benjamin Petersonfaad6bb2014-12-05 20:02:38 -0500201 def test_too_long_lines(self):
202 self.assertRaises(poplib.error_proto, self.client._shortcmd,
203 'echo +%s' % ((poplib._MAXLINE + 10) * 'a'))
204
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000205 def test_dele(self):
206 self.assertOK(self.client.dele('foo'))
207
208 def test_noop(self):
209 self.assertOK(self.client.noop())
210
211 def test_rpop(self):
212 self.assertOK(self.client.rpop('foo'))
213
Benjamin Petersone052d402018-03-03 22:18:17 -0800214 def test_apop_REDOS(self):
215 # Replace welcome with very long evil welcome.
216 # NB The upper bound on welcome length is currently 2048.
217 # At this length, evil input makes each apop call take
218 # on the order of milliseconds instead of microseconds.
219 evil_welcome = b'+OK' + (b'<' * 1000000)
220 with test_support.swap_attr(self.client, 'welcome', evil_welcome):
221 # The evil welcome is invalid, so apop should throw.
222 self.assertRaises(poplib.error_proto, self.client.apop, 'a', 'kb')
223
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000224 def test_top(self):
225 expected = ('+OK 116 bytes',
226 ['From: postmaster@python.org', 'Content-Type: text/plain',
227 'MIME-Version: 1.0', 'Subject: Dummy', '',
228 'line1', 'line2', 'line3'],
229 113)
230 self.assertEqual(self.client.top(1, 1), expected)
231
232 def test_uidl(self):
233 self.client.uidl()
234 self.client.uidl('foo')
235
236
237SUPPORTS_SSL = False
238if hasattr(poplib, 'POP3_SSL'):
239 import ssl
240
241 SUPPORTS_SSL = True
242 CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem")
243
244 class DummyPOP3_SSLHandler(DummyPOP3Handler):
245
246 def __init__(self, conn):
247 asynchat.async_chat.__init__(self, conn)
248 self.socket = ssl.wrap_socket(self.socket, certfile=CERTFILE,
Antoine Pitroufc69af12010-04-24 20:04:58 +0000249 server_side=True,
250 do_handshake_on_connect=False)
251 # Must try handshake before calling push()
252 self._ssl_accepting = True
253 self._do_ssl_handshake()
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000254 self.set_terminator("\r\n")
255 self.in_buffer = []
256 self.push('+OK dummy pop3 server ready.')
257
Antoine Pitroufc69af12010-04-24 20:04:58 +0000258 def _do_ssl_handshake(self):
259 try:
260 self.socket.do_handshake()
261 except ssl.SSLError, err:
262 if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
263 ssl.SSL_ERROR_WANT_WRITE):
264 return
265 elif err.args[0] == ssl.SSL_ERROR_EOF:
266 return self.handle_close()
267 raise
268 except socket.error, err:
269 if err.args[0] == errno.ECONNABORTED:
270 return self.handle_close()
271 else:
272 self._ssl_accepting = False
273
274 def handle_read(self):
275 if self._ssl_accepting:
276 self._do_ssl_handshake()
277 else:
278 DummyPOP3Handler.handle_read(self)
279
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200280requires_ssl = skipUnless(SUPPORTS_SSL, 'SSL not supported')
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000281
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200282@requires_ssl
283class TestPOP3_SSLClass(TestPOP3Class):
284 # repeat previous tests by using poplib.POP3_SSL
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000285
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200286 def setUp(self):
287 self.server = DummyPOP3Server((HOST, 0))
288 self.server.handler = DummyPOP3_SSLHandler
289 self.server.start()
290 self.client = poplib.POP3_SSL(self.server.host, self.server.port)
291
292 def test__all__(self):
293 self.assertIn('POP3_SSL', poplib.__all__)
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000294
295
296class TestTimeouts(TestCase):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000297
Facundo Batistab20c5002007-03-27 18:50:29 +0000298 def setUp(self):
299 self.evt = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +0000300 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Charles-François Natali27b154e2011-12-14 19:28:08 +0100301 self.sock.settimeout(60) # Safety net. Look issue 11812
Trent Nelsone41b0062008-04-08 23:47:30 +0000302 self.port = test_support.bind_port(self.sock)
Charles-François Natali27b154e2011-12-14 19:28:08 +0100303 self.thread = threading.Thread(target=self.server, args=(self.evt,self.sock))
304 self.thread.setDaemon(True)
305 self.thread.start()
306 self.evt.wait()
Facundo Batistab20c5002007-03-27 18:50:29 +0000307
308 def tearDown(self):
Charles-François Natali27b154e2011-12-14 19:28:08 +0100309 self.thread.join()
310 del self.thread # Clear out any dangling Thread objects.
Facundo Batistab20c5002007-03-27 18:50:29 +0000311
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000312 def server(self, evt, serv):
313 serv.listen(5)
Charles-François Natali27b154e2011-12-14 19:28:08 +0100314 evt.set()
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000315 try:
316 conn, addr = serv.accept()
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000317 conn.send("+ Hola mundo\n")
318 conn.close()
Charles-François Natali27b154e2011-12-14 19:28:08 +0100319 except socket.timeout:
320 pass
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000321 finally:
322 serv.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +0000323
Facundo Batistab20c5002007-03-27 18:50:29 +0000324 def testTimeoutDefault(self):
Serhiy Storchaka79b6f172014-02-08 15:05:53 +0200325 self.assertIsNone(socket.getdefaulttimeout())
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000326 socket.setdefaulttimeout(30)
327 try:
Charles-François Natali27b154e2011-12-14 19:28:08 +0100328 pop = poplib.POP3(HOST, self.port)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000329 finally:
330 socket.setdefaulttimeout(None)
Facundo Batistab20c5002007-03-27 18:50:29 +0000331 self.assertEqual(pop.sock.gettimeout(), 30)
332 pop.sock.close()
333
334 def testTimeoutNone(self):
Serhiy Storchaka79b6f172014-02-08 15:05:53 +0200335 self.assertIsNone(socket.getdefaulttimeout())
Facundo Batistab20c5002007-03-27 18:50:29 +0000336 socket.setdefaulttimeout(30)
337 try:
Trent Nelsone41b0062008-04-08 23:47:30 +0000338 pop = poplib.POP3(HOST, self.port, timeout=None)
Facundo Batistab20c5002007-03-27 18:50:29 +0000339 finally:
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000340 socket.setdefaulttimeout(None)
Serhiy Storchaka79b6f172014-02-08 15:05:53 +0200341 self.assertIsNone(pop.sock.gettimeout())
Facundo Batistab20c5002007-03-27 18:50:29 +0000342 pop.sock.close()
343
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000344 def testTimeoutValue(self):
Charles-François Natali27b154e2011-12-14 19:28:08 +0100345 pop = poplib.POP3(HOST, self.port, timeout=30)
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000346 self.assertEqual(pop.sock.gettimeout(), 30)
347 pop.sock.close()
Facundo Batistab20c5002007-03-27 18:50:29 +0000348
349
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000350def test_main():
Serhiy Storchaka32e23e72013-11-03 23:15:46 +0200351 tests = [TestPOP3Class, TestTimeouts,
352 TestPOP3_SSLClass]
Benjamin Peterson76f71a52008-10-11 17:25:36 +0000353 thread_info = test_support.threading_setup()
354 try:
355 test_support.run_unittest(*tests)
356 finally:
357 test_support.threading_cleanup(*thread_info)
358
Facundo Batistab20c5002007-03-27 18:50:29 +0000359
360if __name__ == '__main__':
361 test_main()