Silence a py3k warning claiming to affect Lib/calendar.py
[python.git] / Lib / test / test_poplib.py
blob506d1838b70b495a6199fe1a6acfbd1e526e339f
1 """Test script for poplib module."""
3 # Modified by Giampaolo Rodola' to give poplib.POP3 and poplib.POP3_SSL
4 # a real test suite
6 import poplib
7 import threading
8 import asyncore
9 import asynchat
10 import socket
11 import os
12 import time
14 from unittest import TestCase
15 from test import test_support
16 from test.test_support import HOST
19 # the dummy data returned by server when LIST and RETR commands are issued
20 LIST_RESP = '1 1\r\n2 2\r\n3 3\r\n4 4\r\n5 5\r\n.\r\n'
21 RETR_RESP = """From: postmaster@python.org\
22 \r\nContent-Type: text/plain\r\n\
23 MIME-Version: 1.0\r\n\
24 Subject: Dummy\r\n\
25 \r\n\
26 line1\r\n\
27 line2\r\n\
28 line3\r\n\
29 .\r\n"""
32 class DummyPOP3Handler(asynchat.async_chat):
34 def __init__(self, conn):
35 asynchat.async_chat.__init__(self, conn)
36 self.set_terminator("\r\n")
37 self.in_buffer = []
38 self.push('+OK dummy pop3 server ready.')
40 def collect_incoming_data(self, data):
41 self.in_buffer.append(data)
43 def found_terminator(self):
44 line = ''.join(self.in_buffer)
45 self.in_buffer = []
46 cmd = line.split(' ')[0].lower()
47 space = line.find(' ')
48 if space != -1:
49 arg = line[space + 1:]
50 else:
51 arg = ""
52 if hasattr(self, 'cmd_' + cmd):
53 method = getattr(self, 'cmd_' + cmd)
54 method(arg)
55 else:
56 self.push('-ERR unrecognized POP3 command "%s".' %cmd)
58 def handle_error(self):
59 raise
61 def push(self, data):
62 asynchat.async_chat.push(self, data + '\r\n')
64 def cmd_echo(self, arg):
65 # sends back the received string (used by the test suite)
66 self.push(arg)
68 def cmd_user(self, arg):
69 if arg != "guido":
70 self.push("-ERR no such user")
71 self.push('+OK password required')
73 def cmd_pass(self, arg):
74 if arg != "python":
75 self.push("-ERR wrong password")
76 self.push('+OK 10 messages')
78 def cmd_stat(self, arg):
79 self.push('+OK 10 100')
81 def cmd_list(self, arg):
82 if arg:
83 self.push('+OK %s %s' %(arg, arg))
84 else:
85 self.push('+OK')
86 asynchat.async_chat.push(self, LIST_RESP)
88 cmd_uidl = cmd_list
90 def cmd_retr(self, arg):
91 self.push('+OK %s bytes' %len(RETR_RESP))
92 asynchat.async_chat.push(self, RETR_RESP)
94 cmd_top = cmd_retr
96 def cmd_dele(self, arg):
97 self.push('+OK message marked for deletion.')
99 def cmd_noop(self, arg):
100 self.push('+OK done nothing.')
102 def cmd_rpop(self, arg):
103 self.push('+OK done nothing.')
106 class DummyPOP3Server(asyncore.dispatcher, threading.Thread):
108 handler = DummyPOP3Handler
110 def __init__(self, address, af=socket.AF_INET):
111 threading.Thread.__init__(self)
112 asyncore.dispatcher.__init__(self)
113 self.create_socket(af, socket.SOCK_STREAM)
114 self.bind(address)
115 self.listen(5)
116 self.active = False
117 self.active_lock = threading.Lock()
118 self.host, self.port = self.socket.getsockname()[:2]
120 def start(self):
121 assert not self.active
122 self.__flag = threading.Event()
123 threading.Thread.start(self)
124 self.__flag.wait()
126 def run(self):
127 self.active = True
128 self.__flag.set()
129 while self.active and asyncore.socket_map:
130 self.active_lock.acquire()
131 asyncore.loop(timeout=0.1, count=1)
132 self.active_lock.release()
133 asyncore.close_all(ignore_all=True)
135 def stop(self):
136 assert self.active
137 self.active = False
138 self.join()
140 def handle_accept(self):
141 conn, addr = self.accept()
142 self.handler = self.handler(conn)
143 self.close()
145 def handle_connect(self):
146 self.close()
147 handle_read = handle_connect
149 def writable(self):
150 return 0
152 def handle_error(self):
153 raise
156 class TestPOP3Class(TestCase):
158 def assertOK(self, resp):
159 self.assertTrue(resp.startswith("+OK"))
161 def setUp(self):
162 self.server = DummyPOP3Server((HOST, 0))
163 self.server.start()
164 self.client = poplib.POP3(self.server.host, self.server.port)
166 def tearDown(self):
167 self.client.quit()
168 self.server.stop()
170 def test_getwelcome(self):
171 self.assertEqual(self.client.getwelcome(), '+OK dummy pop3 server ready.')
173 def test_exceptions(self):
174 self.assertRaises(poplib.error_proto, self.client._shortcmd, 'echo -err')
176 def test_user(self):
177 self.assertOK(self.client.user('guido'))
178 self.assertRaises(poplib.error_proto, self.client.user, 'invalid')
180 def test_pass_(self):
181 self.assertOK(self.client.pass_('python'))
182 self.assertRaises(poplib.error_proto, self.client.user, 'invalid')
184 def test_stat(self):
185 self.assertEqual(self.client.stat(), (10, 100))
187 def test_list(self):
188 self.assertEqual(self.client.list()[1:],
189 (['1 1', '2 2', '3 3', '4 4', '5 5'], 25))
190 self.assertTrue(self.client.list('1').endswith("OK 1 1"))
192 def test_retr(self):
193 expected = ('+OK 116 bytes',
194 ['From: postmaster@python.org', 'Content-Type: text/plain',
195 'MIME-Version: 1.0', 'Subject: Dummy',
196 '', 'line1', 'line2', 'line3'],
197 113)
198 self.assertEqual(self.client.retr('foo'), expected)
200 def test_dele(self):
201 self.assertOK(self.client.dele('foo'))
203 def test_noop(self):
204 self.assertOK(self.client.noop())
206 def test_rpop(self):
207 self.assertOK(self.client.rpop('foo'))
209 def test_top(self):
210 expected = ('+OK 116 bytes',
211 ['From: postmaster@python.org', 'Content-Type: text/plain',
212 'MIME-Version: 1.0', 'Subject: Dummy', '',
213 'line1', 'line2', 'line3'],
214 113)
215 self.assertEqual(self.client.top(1, 1), expected)
217 def test_uidl(self):
218 self.client.uidl()
219 self.client.uidl('foo')
222 SUPPORTS_SSL = False
223 if hasattr(poplib, 'POP3_SSL'):
224 import ssl
226 SUPPORTS_SSL = True
227 CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem")
229 class DummyPOP3_SSLHandler(DummyPOP3Handler):
231 def __init__(self, conn):
232 asynchat.async_chat.__init__(self, conn)
233 self.socket = ssl.wrap_socket(self.socket, certfile=CERTFILE,
234 server_side=True)
235 self.set_terminator("\r\n")
236 self.in_buffer = []
237 self.push('+OK dummy pop3 server ready.')
239 class TestPOP3_SSLClass(TestPOP3Class):
240 # repeat previous tests by using poplib.POP3_SSL
242 def setUp(self):
243 self.server = DummyPOP3Server((HOST, 0))
244 self.server.handler = DummyPOP3_SSLHandler
245 self.server.start()
246 self.client = poplib.POP3_SSL(self.server.host, self.server.port)
248 def test__all__(self):
249 self.assertTrue('POP3_SSL' in poplib.__all__)
252 class TestTimeouts(TestCase):
254 def setUp(self):
255 self.evt = threading.Event()
256 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
257 self.sock.settimeout(3)
258 self.port = test_support.bind_port(self.sock)
259 threading.Thread(target=self.server, args=(self.evt,self.sock)).start()
260 time.sleep(.1)
262 def tearDown(self):
263 self.evt.wait()
265 def server(self, evt, serv):
266 serv.listen(5)
267 try:
268 conn, addr = serv.accept()
269 except socket.timeout:
270 pass
271 else:
272 conn.send("+ Hola mundo\n")
273 conn.close()
274 finally:
275 serv.close()
276 evt.set()
278 def testTimeoutDefault(self):
279 self.assertTrue(socket.getdefaulttimeout() is None)
280 socket.setdefaulttimeout(30)
281 try:
282 pop = poplib.POP3("localhost", self.port)
283 finally:
284 socket.setdefaulttimeout(None)
285 self.assertEqual(pop.sock.gettimeout(), 30)
286 pop.sock.close()
288 def testTimeoutNone(self):
289 self.assertTrue(socket.getdefaulttimeout() is None)
290 socket.setdefaulttimeout(30)
291 try:
292 pop = poplib.POP3(HOST, self.port, timeout=None)
293 finally:
294 socket.setdefaulttimeout(None)
295 self.assertTrue(pop.sock.gettimeout() is None)
296 pop.sock.close()
298 def testTimeoutValue(self):
299 pop = poplib.POP3("localhost", self.port, timeout=30)
300 self.assertEqual(pop.sock.gettimeout(), 30)
301 pop.sock.close()
304 def test_main():
305 tests = [TestPOP3Class, TestTimeouts]
306 if SUPPORTS_SSL:
307 tests.append(TestPOP3_SSLClass)
308 thread_info = test_support.threading_setup()
309 try:
310 test_support.run_unittest(*tests)
311 finally:
312 test_support.threading_cleanup(*thread_info)
315 if __name__ == '__main__':
316 test_main()