1 """Test script for poplib module."""
3 # Modified by Giampaolo Rodola' to give poplib.POP3 and poplib.POP3_SSL
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\
32 class DummyPOP3Handler(asynchat
.async_chat
):
34 def __init__(self
, conn
):
35 asynchat
.async_chat
.__init
__(self
, conn
)
36 self
.set_terminator("\r\n")
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
)
46 cmd
= line
.split(' ')[0].lower()
47 space
= line
.find(' ')
49 arg
= line
[space
+ 1:]
52 if hasattr(self
, 'cmd_' + cmd
):
53 method
= getattr(self
, 'cmd_' + cmd
)
56 self
.push('-ERR unrecognized POP3 command "%s".' %cmd
)
58 def handle_error(self
):
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)
68 def cmd_user(self
, arg
):
70 self
.push("-ERR no such user")
71 self
.push('+OK password required')
73 def cmd_pass(self
, arg
):
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
):
83 self
.push('+OK %s %s' %(arg
, arg
))
86 asynchat
.async_chat
.push(self
, LIST_RESP
)
90 def cmd_retr(self
, arg
):
91 self
.push('+OK %s bytes' %len(RETR_RESP
))
92 asynchat
.async_chat
.push(self
, RETR_RESP
)
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
)
117 self
.active_lock
= threading
.Lock()
118 self
.host
, self
.port
= self
.socket
.getsockname()[:2]
121 assert not self
.active
122 self
.__flag
= threading
.Event()
123 threading
.Thread
.start(self
)
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)
140 def handle_accept(self
):
141 conn
, addr
= self
.accept()
142 self
.handler
= self
.handler(conn
)
145 def handle_connect(self
):
147 handle_read
= handle_connect
152 def handle_error(self
):
156 class TestPOP3Class(TestCase
):
158 def assertOK(self
, resp
):
159 self
.assertTrue(resp
.startswith("+OK"))
162 self
.server
= DummyPOP3Server((HOST
, 0))
164 self
.client
= poplib
.POP3(self
.server
.host
, self
.server
.port
)
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')
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')
185 self
.assertEqual(self
.client
.stat(), (10, 100))
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"))
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'],
198 self
.assertEqual(self
.client
.retr('foo'), expected
)
201 self
.assertOK(self
.client
.dele('foo'))
204 self
.assertOK(self
.client
.noop())
207 self
.assertOK(self
.client
.rpop('foo'))
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'],
215 self
.assertEqual(self
.client
.top(1, 1), expected
)
219 self
.client
.uidl('foo')
223 if hasattr(poplib
, 'POP3_SSL'):
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
,
235 self
.set_terminator("\r\n")
237 self
.push('+OK dummy pop3 server ready.')
239 class TestPOP3_SSLClass(TestPOP3Class
):
240 # repeat previous tests by using poplib.POP3_SSL
243 self
.server
= DummyPOP3Server((HOST
, 0))
244 self
.server
.handler
= DummyPOP3_SSLHandler
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
):
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()
265 def server(self
, evt
, serv
):
268 conn
, addr
= serv
.accept()
269 except socket
.timeout
:
272 conn
.send("+ Hola mundo\n")
278 def testTimeoutDefault(self
):
279 self
.assertTrue(socket
.getdefaulttimeout() is None)
280 socket
.setdefaulttimeout(30)
282 pop
= poplib
.POP3("localhost", self
.port
)
284 socket
.setdefaulttimeout(None)
285 self
.assertEqual(pop
.sock
.gettimeout(), 30)
288 def testTimeoutNone(self
):
289 self
.assertTrue(socket
.getdefaulttimeout() is None)
290 socket
.setdefaulttimeout(30)
292 pop
= poplib
.POP3(HOST
, self
.port
, timeout
=None)
294 socket
.setdefaulttimeout(None)
295 self
.assertTrue(pop
.sock
.gettimeout() is None)
298 def testTimeoutValue(self
):
299 pop
= poplib
.POP3("localhost", self
.port
, timeout
=30)
300 self
.assertEqual(pop
.sock
.gettimeout(), 30)
305 tests
= [TestPOP3Class
, TestTimeouts
]
307 tests
.append(TestPOP3_SSLClass
)
308 thread_info
= test_support
.threading_setup()
310 test_support
.run_unittest(*tests
)
312 test_support
.threading_cleanup(*thread_info
)
315 if __name__
== '__main__':