1 | n/a | from test import support |
---|
2 | n/a | # If we end up with a significant number of tests that don't require |
---|
3 | n/a | # threading, this test module should be split. Right now we skip |
---|
4 | n/a | # them all if we don't have threading. |
---|
5 | n/a | threading = support.import_module('threading') |
---|
6 | n/a | |
---|
7 | n/a | from contextlib import contextmanager |
---|
8 | n/a | import imaplib |
---|
9 | n/a | import os.path |
---|
10 | n/a | import socketserver |
---|
11 | n/a | import time |
---|
12 | n/a | import calendar |
---|
13 | n/a | import inspect |
---|
14 | n/a | |
---|
15 | n/a | from test.support import (reap_threads, verbose, transient_internet, |
---|
16 | n/a | run_with_tz, run_with_locale) |
---|
17 | n/a | import unittest |
---|
18 | n/a | from unittest import mock |
---|
19 | n/a | from datetime import datetime, timezone, timedelta |
---|
20 | n/a | try: |
---|
21 | n/a | import ssl |
---|
22 | n/a | except ImportError: |
---|
23 | n/a | ssl = None |
---|
24 | n/a | |
---|
25 | n/a | CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert3.pem") |
---|
26 | n/a | CAFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "pycacert.pem") |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | class TestImaplib(unittest.TestCase): |
---|
30 | n/a | |
---|
31 | n/a | def test_Internaldate2tuple(self): |
---|
32 | n/a | t0 = calendar.timegm((2000, 1, 1, 0, 0, 0, -1, -1, -1)) |
---|
33 | n/a | tt = imaplib.Internaldate2tuple( |
---|
34 | n/a | b'25 (INTERNALDATE "01-Jan-2000 00:00:00 +0000")') |
---|
35 | n/a | self.assertEqual(time.mktime(tt), t0) |
---|
36 | n/a | tt = imaplib.Internaldate2tuple( |
---|
37 | n/a | b'25 (INTERNALDATE "01-Jan-2000 11:30:00 +1130")') |
---|
38 | n/a | self.assertEqual(time.mktime(tt), t0) |
---|
39 | n/a | tt = imaplib.Internaldate2tuple( |
---|
40 | n/a | b'25 (INTERNALDATE "31-Dec-1999 12:30:00 -1130")') |
---|
41 | n/a | self.assertEqual(time.mktime(tt), t0) |
---|
42 | n/a | |
---|
43 | n/a | @run_with_tz('MST+07MDT,M4.1.0,M10.5.0') |
---|
44 | n/a | def test_Internaldate2tuple_issue10941(self): |
---|
45 | n/a | self.assertNotEqual(imaplib.Internaldate2tuple( |
---|
46 | n/a | b'25 (INTERNALDATE "02-Apr-2000 02:30:00 +0000")'), |
---|
47 | n/a | imaplib.Internaldate2tuple( |
---|
48 | n/a | b'25 (INTERNALDATE "02-Apr-2000 03:30:00 +0000")')) |
---|
49 | n/a | |
---|
50 | n/a | def timevalues(self): |
---|
51 | n/a | return [2000000000, 2000000000.0, time.localtime(2000000000), |
---|
52 | n/a | (2033, 5, 18, 5, 33, 20, -1, -1, -1), |
---|
53 | n/a | (2033, 5, 18, 5, 33, 20, -1, -1, 1), |
---|
54 | n/a | datetime.fromtimestamp(2000000000, |
---|
55 | n/a | timezone(timedelta(0, 2 * 60 * 60))), |
---|
56 | n/a | '"18-May-2033 05:33:20 +0200"'] |
---|
57 | n/a | |
---|
58 | n/a | @run_with_locale('LC_ALL', 'de_DE', 'fr_FR') |
---|
59 | n/a | # DST rules included to work around quirk where the Gnu C library may not |
---|
60 | n/a | # otherwise restore the previous time zone |
---|
61 | n/a | @run_with_tz('STD-1DST,M3.2.0,M11.1.0') |
---|
62 | n/a | def test_Time2Internaldate(self): |
---|
63 | n/a | expected = '"18-May-2033 05:33:20 +0200"' |
---|
64 | n/a | |
---|
65 | n/a | for t in self.timevalues(): |
---|
66 | n/a | internal = imaplib.Time2Internaldate(t) |
---|
67 | n/a | self.assertEqual(internal, expected) |
---|
68 | n/a | |
---|
69 | n/a | def test_that_Time2Internaldate_returns_a_result(self): |
---|
70 | n/a | # Without tzset, we can check only that it successfully |
---|
71 | n/a | # produces a result, not the correctness of the result itself, |
---|
72 | n/a | # since the result depends on the timezone the machine is in. |
---|
73 | n/a | for t in self.timevalues(): |
---|
74 | n/a | imaplib.Time2Internaldate(t) |
---|
75 | n/a | |
---|
76 | n/a | |
---|
77 | n/a | if ssl: |
---|
78 | n/a | class SecureTCPServer(socketserver.TCPServer): |
---|
79 | n/a | |
---|
80 | n/a | def get_request(self): |
---|
81 | n/a | newsocket, fromaddr = self.socket.accept() |
---|
82 | n/a | context = ssl.SSLContext() |
---|
83 | n/a | context.load_cert_chain(CERTFILE) |
---|
84 | n/a | connstream = context.wrap_socket(newsocket, server_side=True) |
---|
85 | n/a | return connstream, fromaddr |
---|
86 | n/a | |
---|
87 | n/a | IMAP4_SSL = imaplib.IMAP4_SSL |
---|
88 | n/a | |
---|
89 | n/a | else: |
---|
90 | n/a | |
---|
91 | n/a | class SecureTCPServer: |
---|
92 | n/a | pass |
---|
93 | n/a | |
---|
94 | n/a | IMAP4_SSL = None |
---|
95 | n/a | |
---|
96 | n/a | |
---|
97 | n/a | class SimpleIMAPHandler(socketserver.StreamRequestHandler): |
---|
98 | n/a | timeout = 1 |
---|
99 | n/a | continuation = None |
---|
100 | n/a | capabilities = '' |
---|
101 | n/a | |
---|
102 | n/a | def setup(self): |
---|
103 | n/a | super().setup() |
---|
104 | n/a | self.server.logged = None |
---|
105 | n/a | |
---|
106 | n/a | def _send(self, message): |
---|
107 | n/a | if verbose: |
---|
108 | n/a | print("SENT: %r" % message.strip()) |
---|
109 | n/a | self.wfile.write(message) |
---|
110 | n/a | |
---|
111 | n/a | def _send_line(self, message): |
---|
112 | n/a | self._send(message + b'\r\n') |
---|
113 | n/a | |
---|
114 | n/a | def _send_textline(self, message): |
---|
115 | n/a | self._send_line(message.encode('ASCII')) |
---|
116 | n/a | |
---|
117 | n/a | def _send_tagged(self, tag, code, message): |
---|
118 | n/a | self._send_textline(' '.join((tag, code, message))) |
---|
119 | n/a | |
---|
120 | n/a | def handle(self): |
---|
121 | n/a | # Send a welcome message. |
---|
122 | n/a | self._send_textline('* OK IMAP4rev1') |
---|
123 | n/a | while 1: |
---|
124 | n/a | # Gather up input until we receive a line terminator or we timeout. |
---|
125 | n/a | # Accumulate read(1) because it's simpler to handle the differences |
---|
126 | n/a | # between naked sockets and SSL sockets. |
---|
127 | n/a | line = b'' |
---|
128 | n/a | while 1: |
---|
129 | n/a | try: |
---|
130 | n/a | part = self.rfile.read(1) |
---|
131 | n/a | if part == b'': |
---|
132 | n/a | # Naked sockets return empty strings.. |
---|
133 | n/a | return |
---|
134 | n/a | line += part |
---|
135 | n/a | except OSError: |
---|
136 | n/a | # ..but SSLSockets raise exceptions. |
---|
137 | n/a | return |
---|
138 | n/a | if line.endswith(b'\r\n'): |
---|
139 | n/a | break |
---|
140 | n/a | |
---|
141 | n/a | if verbose: |
---|
142 | n/a | print('GOT: %r' % line.strip()) |
---|
143 | n/a | if self.continuation: |
---|
144 | n/a | try: |
---|
145 | n/a | self.continuation.send(line) |
---|
146 | n/a | except StopIteration: |
---|
147 | n/a | self.continuation = None |
---|
148 | n/a | continue |
---|
149 | n/a | splitline = line.decode('ASCII').split() |
---|
150 | n/a | tag = splitline[0] |
---|
151 | n/a | cmd = splitline[1] |
---|
152 | n/a | args = splitline[2:] |
---|
153 | n/a | |
---|
154 | n/a | if hasattr(self, 'cmd_' + cmd): |
---|
155 | n/a | continuation = getattr(self, 'cmd_' + cmd)(tag, args) |
---|
156 | n/a | if continuation: |
---|
157 | n/a | self.continuation = continuation |
---|
158 | n/a | next(continuation) |
---|
159 | n/a | else: |
---|
160 | n/a | self._send_tagged(tag, 'BAD', cmd + ' unknown') |
---|
161 | n/a | |
---|
162 | n/a | def cmd_CAPABILITY(self, tag, args): |
---|
163 | n/a | caps = ('IMAP4rev1 ' + self.capabilities |
---|
164 | n/a | if self.capabilities |
---|
165 | n/a | else 'IMAP4rev1') |
---|
166 | n/a | self._send_textline('* CAPABILITY ' + caps) |
---|
167 | n/a | self._send_tagged(tag, 'OK', 'CAPABILITY completed') |
---|
168 | n/a | |
---|
169 | n/a | def cmd_LOGOUT(self, tag, args): |
---|
170 | n/a | self.server.logged = None |
---|
171 | n/a | self._send_textline('* BYE IMAP4ref1 Server logging out') |
---|
172 | n/a | self._send_tagged(tag, 'OK', 'LOGOUT completed') |
---|
173 | n/a | |
---|
174 | n/a | def cmd_LOGIN(self, tag, args): |
---|
175 | n/a | self.server.logged = args[0] |
---|
176 | n/a | self._send_tagged(tag, 'OK', 'LOGIN completed') |
---|
177 | n/a | |
---|
178 | n/a | |
---|
179 | n/a | class NewIMAPTestsMixin(): |
---|
180 | n/a | client = None |
---|
181 | n/a | |
---|
182 | n/a | def _setup(self, imap_handler, connect=True): |
---|
183 | n/a | """ |
---|
184 | n/a | Sets up imap_handler for tests. imap_handler should inherit from either: |
---|
185 | n/a | - SimpleIMAPHandler - for testing IMAP commands, |
---|
186 | n/a | - socketserver.StreamRequestHandler - if raw access to stream is needed. |
---|
187 | n/a | Returns (client, server). |
---|
188 | n/a | """ |
---|
189 | n/a | class TestTCPServer(self.server_class): |
---|
190 | n/a | def handle_error(self, request, client_address): |
---|
191 | n/a | """ |
---|
192 | n/a | End request and raise the error if one occurs. |
---|
193 | n/a | """ |
---|
194 | n/a | self.close_request(request) |
---|
195 | n/a | self.server_close() |
---|
196 | n/a | raise |
---|
197 | n/a | |
---|
198 | n/a | self.addCleanup(self._cleanup) |
---|
199 | n/a | self.server = self.server_class((support.HOST, 0), imap_handler) |
---|
200 | n/a | self.thread = threading.Thread( |
---|
201 | n/a | name=self._testMethodName+'-server', |
---|
202 | n/a | target=self.server.serve_forever, |
---|
203 | n/a | # Short poll interval to make the test finish quickly. |
---|
204 | n/a | # Time between requests is short enough that we won't wake |
---|
205 | n/a | # up spuriously too many times. |
---|
206 | n/a | kwargs={'poll_interval': 0.01}) |
---|
207 | n/a | self.thread.daemon = True # In case this function raises. |
---|
208 | n/a | self.thread.start() |
---|
209 | n/a | |
---|
210 | n/a | if connect: |
---|
211 | n/a | self.client = self.imap_class(*self.server.server_address) |
---|
212 | n/a | |
---|
213 | n/a | return self.client, self.server |
---|
214 | n/a | |
---|
215 | n/a | def _cleanup(self): |
---|
216 | n/a | """ |
---|
217 | n/a | Cleans up the test server. This method should not be called manually, |
---|
218 | n/a | it is added to the cleanup queue in the _setup method already. |
---|
219 | n/a | """ |
---|
220 | n/a | # if logout was called already we'd raise an exception trying to |
---|
221 | n/a | # shutdown the client once again |
---|
222 | n/a | if self.client is not None and self.client.state != 'LOGOUT': |
---|
223 | n/a | self.client.shutdown() |
---|
224 | n/a | # cleanup the server |
---|
225 | n/a | self.server.shutdown() |
---|
226 | n/a | self.server.server_close() |
---|
227 | n/a | self.thread.join(3.0) |
---|
228 | n/a | |
---|
229 | n/a | def test_EOF_without_complete_welcome_message(self): |
---|
230 | n/a | # http://bugs.python.org/issue5949 |
---|
231 | n/a | class EOFHandler(socketserver.StreamRequestHandler): |
---|
232 | n/a | def handle(self): |
---|
233 | n/a | self.wfile.write(b'* OK') |
---|
234 | n/a | _, server = self._setup(EOFHandler, connect=False) |
---|
235 | n/a | self.assertRaises(imaplib.IMAP4.abort, self.imap_class, |
---|
236 | n/a | *server.server_address) |
---|
237 | n/a | |
---|
238 | n/a | def test_line_termination(self): |
---|
239 | n/a | class BadNewlineHandler(SimpleIMAPHandler): |
---|
240 | n/a | def cmd_CAPABILITY(self, tag, args): |
---|
241 | n/a | self._send(b'* CAPABILITY IMAP4rev1 AUTH\n') |
---|
242 | n/a | self._send_tagged(tag, 'OK', 'CAPABILITY completed') |
---|
243 | n/a | _, server = self._setup(BadNewlineHandler, connect=False) |
---|
244 | n/a | self.assertRaises(imaplib.IMAP4.abort, self.imap_class, |
---|
245 | n/a | *server.server_address) |
---|
246 | n/a | |
---|
247 | n/a | def test_enable_raises_error_if_not_AUTH(self): |
---|
248 | n/a | class EnableHandler(SimpleIMAPHandler): |
---|
249 | n/a | capabilities = 'AUTH ENABLE UTF8=ACCEPT' |
---|
250 | n/a | client, _ = self._setup(EnableHandler) |
---|
251 | n/a | self.assertFalse(client.utf8_enabled) |
---|
252 | n/a | with self.assertRaisesRegex(imaplib.IMAP4.error, 'ENABLE.*NONAUTH'): |
---|
253 | n/a | client.enable('foo') |
---|
254 | n/a | self.assertFalse(client.utf8_enabled) |
---|
255 | n/a | |
---|
256 | n/a | def test_enable_raises_error_if_no_capability(self): |
---|
257 | n/a | client, _ = self._setup(SimpleIMAPHandler) |
---|
258 | n/a | with self.assertRaisesRegex(imaplib.IMAP4.error, |
---|
259 | n/a | 'does not support ENABLE'): |
---|
260 | n/a | client.enable('foo') |
---|
261 | n/a | |
---|
262 | n/a | def test_enable_UTF8_raises_error_if_not_supported(self): |
---|
263 | n/a | client, _ = self._setup(SimpleIMAPHandler) |
---|
264 | n/a | typ, data = client.login('user', 'pass') |
---|
265 | n/a | self.assertEqual(typ, 'OK') |
---|
266 | n/a | with self.assertRaisesRegex(imaplib.IMAP4.error, |
---|
267 | n/a | 'does not support ENABLE'): |
---|
268 | n/a | client.enable('UTF8=ACCEPT') |
---|
269 | n/a | |
---|
270 | n/a | def test_enable_UTF8_True_append(self): |
---|
271 | n/a | class UTF8AppendServer(SimpleIMAPHandler): |
---|
272 | n/a | capabilities = 'ENABLE UTF8=ACCEPT' |
---|
273 | n/a | def cmd_ENABLE(self, tag, args): |
---|
274 | n/a | self._send_tagged(tag, 'OK', 'ENABLE successful') |
---|
275 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
276 | n/a | self._send_textline('+') |
---|
277 | n/a | self.server.response = yield |
---|
278 | n/a | self._send_tagged(tag, 'OK', 'FAKEAUTH successful') |
---|
279 | n/a | def cmd_APPEND(self, tag, args): |
---|
280 | n/a | self._send_textline('+') |
---|
281 | n/a | self.server.response = yield |
---|
282 | n/a | self._send_tagged(tag, 'OK', 'okay') |
---|
283 | n/a | client, server = self._setup(UTF8AppendServer) |
---|
284 | n/a | self.assertEqual(client._encoding, 'ascii') |
---|
285 | n/a | code, _ = client.authenticate('MYAUTH', lambda x: b'fake') |
---|
286 | n/a | self.assertEqual(code, 'OK') |
---|
287 | n/a | self.assertEqual(server.response, b'ZmFrZQ==\r\n') # b64 encoded 'fake' |
---|
288 | n/a | code, _ = client.enable('UTF8=ACCEPT') |
---|
289 | n/a | self.assertEqual(code, 'OK') |
---|
290 | n/a | self.assertEqual(client._encoding, 'utf-8') |
---|
291 | n/a | msg_string = 'Subject: üñéöðé' |
---|
292 | n/a | typ, data = client.append(None, None, None, msg_string.encode('utf-8')) |
---|
293 | n/a | self.assertEqual(typ, 'OK') |
---|
294 | n/a | self.assertEqual(server.response, |
---|
295 | n/a | ('UTF8 (%s)\r\n' % msg_string).encode('utf-8')) |
---|
296 | n/a | |
---|
297 | n/a | def test_search_disallows_charset_in_utf8_mode(self): |
---|
298 | n/a | class UTF8Server(SimpleIMAPHandler): |
---|
299 | n/a | capabilities = 'AUTH ENABLE UTF8=ACCEPT' |
---|
300 | n/a | def cmd_ENABLE(self, tag, args): |
---|
301 | n/a | self._send_tagged(tag, 'OK', 'ENABLE successful') |
---|
302 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
303 | n/a | self._send_textline('+') |
---|
304 | n/a | self.server.response = yield |
---|
305 | n/a | self._send_tagged(tag, 'OK', 'FAKEAUTH successful') |
---|
306 | n/a | client, _ = self._setup(UTF8Server) |
---|
307 | n/a | typ, _ = client.authenticate('MYAUTH', lambda x: b'fake') |
---|
308 | n/a | self.assertEqual(typ, 'OK') |
---|
309 | n/a | typ, _ = client.enable('UTF8=ACCEPT') |
---|
310 | n/a | self.assertEqual(typ, 'OK') |
---|
311 | n/a | self.assertTrue(client.utf8_enabled) |
---|
312 | n/a | with self.assertRaisesRegex(imaplib.IMAP4.error, 'charset.*UTF8'): |
---|
313 | n/a | client.search('foo', 'bar') |
---|
314 | n/a | |
---|
315 | n/a | def test_bad_auth_name(self): |
---|
316 | n/a | class MyServer(SimpleIMAPHandler): |
---|
317 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
318 | n/a | self._send_tagged(tag, 'NO', |
---|
319 | n/a | 'unrecognized authentication type {}'.format(args[0])) |
---|
320 | n/a | client, _ = self._setup(MyServer) |
---|
321 | n/a | with self.assertRaisesRegex(imaplib.IMAP4.error, |
---|
322 | n/a | 'unrecognized authentication type METHOD'): |
---|
323 | n/a | client.authenticate('METHOD', lambda: 1) |
---|
324 | n/a | |
---|
325 | n/a | def test_invalid_authentication(self): |
---|
326 | n/a | class MyServer(SimpleIMAPHandler): |
---|
327 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
328 | n/a | self._send_textline('+') |
---|
329 | n/a | self.response = yield |
---|
330 | n/a | self._send_tagged(tag, 'NO', '[AUTHENTICATIONFAILED] invalid') |
---|
331 | n/a | client, _ = self._setup(MyServer) |
---|
332 | n/a | with self.assertRaisesRegex(imaplib.IMAP4.error, |
---|
333 | n/a | r'\[AUTHENTICATIONFAILED\] invalid'): |
---|
334 | n/a | client.authenticate('MYAUTH', lambda x: b'fake') |
---|
335 | n/a | |
---|
336 | n/a | def test_valid_authentication_bytes(self): |
---|
337 | n/a | class MyServer(SimpleIMAPHandler): |
---|
338 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
339 | n/a | self._send_textline('+') |
---|
340 | n/a | self.server.response = yield |
---|
341 | n/a | self._send_tagged(tag, 'OK', 'FAKEAUTH successful') |
---|
342 | n/a | client, server = self._setup(MyServer) |
---|
343 | n/a | code, _ = client.authenticate('MYAUTH', lambda x: b'fake') |
---|
344 | n/a | self.assertEqual(code, 'OK') |
---|
345 | n/a | self.assertEqual(server.response, b'ZmFrZQ==\r\n') # b64 encoded 'fake' |
---|
346 | n/a | |
---|
347 | n/a | def test_valid_authentication_plain_text(self): |
---|
348 | n/a | class MyServer(SimpleIMAPHandler): |
---|
349 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
350 | n/a | self._send_textline('+') |
---|
351 | n/a | self.server.response = yield |
---|
352 | n/a | self._send_tagged(tag, 'OK', 'FAKEAUTH successful') |
---|
353 | n/a | client, server = self._setup(MyServer) |
---|
354 | n/a | code, _ = client.authenticate('MYAUTH', lambda x: 'fake') |
---|
355 | n/a | self.assertEqual(code, 'OK') |
---|
356 | n/a | self.assertEqual(server.response, b'ZmFrZQ==\r\n') # b64 encoded 'fake' |
---|
357 | n/a | |
---|
358 | n/a | def test_login_cram_md5_bytes(self): |
---|
359 | n/a | class AuthHandler(SimpleIMAPHandler): |
---|
360 | n/a | capabilities = 'LOGINDISABLED AUTH=CRAM-MD5' |
---|
361 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
362 | n/a | self._send_textline('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm' |
---|
363 | n/a | 'VzdG9uLm1jaS5uZXQ=') |
---|
364 | n/a | r = yield |
---|
365 | n/a | if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT' |
---|
366 | n/a | b'FjY2E2ZmZkNmNmMmQ5ZjMy\r\n'): |
---|
367 | n/a | self._send_tagged(tag, 'OK', 'CRAM-MD5 successful') |
---|
368 | n/a | else: |
---|
369 | n/a | self._send_tagged(tag, 'NO', 'No access') |
---|
370 | n/a | client, _ = self._setup(AuthHandler) |
---|
371 | n/a | self.assertTrue('AUTH=CRAM-MD5' in client.capabilities) |
---|
372 | n/a | ret, _ = client.login_cram_md5("tim", b"tanstaaftanstaaf") |
---|
373 | n/a | self.assertEqual(ret, "OK") |
---|
374 | n/a | |
---|
375 | n/a | def test_login_cram_md5_plain_text(self): |
---|
376 | n/a | class AuthHandler(SimpleIMAPHandler): |
---|
377 | n/a | capabilities = 'LOGINDISABLED AUTH=CRAM-MD5' |
---|
378 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
379 | n/a | self._send_textline('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm' |
---|
380 | n/a | 'VzdG9uLm1jaS5uZXQ=') |
---|
381 | n/a | r = yield |
---|
382 | n/a | if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT' |
---|
383 | n/a | b'FjY2E2ZmZkNmNmMmQ5ZjMy\r\n'): |
---|
384 | n/a | self._send_tagged(tag, 'OK', 'CRAM-MD5 successful') |
---|
385 | n/a | else: |
---|
386 | n/a | self._send_tagged(tag, 'NO', 'No access') |
---|
387 | n/a | client, _ = self._setup(AuthHandler) |
---|
388 | n/a | self.assertTrue('AUTH=CRAM-MD5' in client.capabilities) |
---|
389 | n/a | ret, _ = client.login_cram_md5("tim", "tanstaaftanstaaf") |
---|
390 | n/a | self.assertEqual(ret, "OK") |
---|
391 | n/a | |
---|
392 | n/a | def test_aborted_authentication(self): |
---|
393 | n/a | class MyServer(SimpleIMAPHandler): |
---|
394 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
395 | n/a | self._send_textline('+') |
---|
396 | n/a | self.response = yield |
---|
397 | n/a | if self.response == b'*\r\n': |
---|
398 | n/a | self._send_tagged( |
---|
399 | n/a | tag, |
---|
400 | n/a | 'NO', |
---|
401 | n/a | '[AUTHENTICATIONFAILED] aborted') |
---|
402 | n/a | else: |
---|
403 | n/a | self._send_tagged(tag, 'OK', 'MYAUTH successful') |
---|
404 | n/a | client, _ = self._setup(MyServer) |
---|
405 | n/a | with self.assertRaisesRegex(imaplib.IMAP4.error, |
---|
406 | n/a | r'\[AUTHENTICATIONFAILED\] aborted'): |
---|
407 | n/a | client.authenticate('MYAUTH', lambda x: None) |
---|
408 | n/a | |
---|
409 | n/a | @mock.patch('imaplib._MAXLINE', 10) |
---|
410 | n/a | def test_linetoolong(self): |
---|
411 | n/a | class TooLongHandler(SimpleIMAPHandler): |
---|
412 | n/a | def handle(self): |
---|
413 | n/a | # send response line longer than the limit set in the next line |
---|
414 | n/a | self.wfile.write(b'* OK ' + 11 * b'x' + b'\r\n') |
---|
415 | n/a | _, server = self._setup(TooLongHandler, connect=False) |
---|
416 | n/a | with self.assertRaisesRegex(imaplib.IMAP4.error, |
---|
417 | n/a | 'got more than 10 bytes'): |
---|
418 | n/a | self.imap_class(*server.server_address) |
---|
419 | n/a | |
---|
420 | n/a | def test_simple_with_statement(self): |
---|
421 | n/a | _, server = self._setup(SimpleIMAPHandler, connect=False) |
---|
422 | n/a | with self.imap_class(*server.server_address): |
---|
423 | n/a | pass |
---|
424 | n/a | |
---|
425 | n/a | def test_with_statement(self): |
---|
426 | n/a | _, server = self._setup(SimpleIMAPHandler, connect=False) |
---|
427 | n/a | with self.imap_class(*server.server_address) as imap: |
---|
428 | n/a | imap.login('user', 'pass') |
---|
429 | n/a | self.assertEqual(server.logged, 'user') |
---|
430 | n/a | self.assertIsNone(server.logged) |
---|
431 | n/a | |
---|
432 | n/a | def test_with_statement_logout(self): |
---|
433 | n/a | # It is legal to log out explicitly inside the with block |
---|
434 | n/a | _, server = self._setup(SimpleIMAPHandler, connect=False) |
---|
435 | n/a | with self.imap_class(*server.server_address) as imap: |
---|
436 | n/a | imap.login('user', 'pass') |
---|
437 | n/a | self.assertEqual(server.logged, 'user') |
---|
438 | n/a | imap.logout() |
---|
439 | n/a | self.assertIsNone(server.logged) |
---|
440 | n/a | self.assertIsNone(server.logged) |
---|
441 | n/a | |
---|
442 | n/a | # command tests |
---|
443 | n/a | |
---|
444 | n/a | def test_login(self): |
---|
445 | n/a | client, _ = self._setup(SimpleIMAPHandler) |
---|
446 | n/a | typ, data = client.login('user', 'pass') |
---|
447 | n/a | self.assertEqual(typ, 'OK') |
---|
448 | n/a | self.assertEqual(data[0], b'LOGIN completed') |
---|
449 | n/a | self.assertEqual(client.state, 'AUTH') |
---|
450 | n/a | |
---|
451 | n/a | def test_logout(self): |
---|
452 | n/a | client, _ = self._setup(SimpleIMAPHandler) |
---|
453 | n/a | typ, data = client.login('user', 'pass') |
---|
454 | n/a | self.assertEqual(typ, 'OK') |
---|
455 | n/a | self.assertEqual(data[0], b'LOGIN completed') |
---|
456 | n/a | typ, data = client.logout() |
---|
457 | n/a | self.assertEqual(typ, 'BYE') |
---|
458 | n/a | self.assertEqual(data[0], b'IMAP4ref1 Server logging out') |
---|
459 | n/a | self.assertEqual(client.state, 'LOGOUT') |
---|
460 | n/a | |
---|
461 | n/a | def test_lsub(self): |
---|
462 | n/a | class LsubCmd(SimpleIMAPHandler): |
---|
463 | n/a | def cmd_LSUB(self, tag, args): |
---|
464 | n/a | self._send_textline('* LSUB () "." directoryA') |
---|
465 | n/a | return self._send_tagged(tag, 'OK', 'LSUB completed') |
---|
466 | n/a | client, _ = self._setup(LsubCmd) |
---|
467 | n/a | client.login('user', 'pass') |
---|
468 | n/a | typ, data = client.lsub() |
---|
469 | n/a | self.assertEqual(typ, 'OK') |
---|
470 | n/a | self.assertEqual(data[0], b'() "." directoryA') |
---|
471 | n/a | |
---|
472 | n/a | |
---|
473 | n/a | class NewIMAPTests(NewIMAPTestsMixin, unittest.TestCase): |
---|
474 | n/a | imap_class = imaplib.IMAP4 |
---|
475 | n/a | server_class = socketserver.TCPServer |
---|
476 | n/a | |
---|
477 | n/a | |
---|
478 | n/a | @unittest.skipUnless(ssl, "SSL not available") |
---|
479 | n/a | class NewIMAPSSLTests(NewIMAPTestsMixin, unittest.TestCase): |
---|
480 | n/a | imap_class = IMAP4_SSL |
---|
481 | n/a | server_class = SecureTCPServer |
---|
482 | n/a | |
---|
483 | n/a | def test_ssl_raises(self): |
---|
484 | n/a | ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
---|
485 | n/a | ssl_context.verify_mode = ssl.CERT_REQUIRED |
---|
486 | n/a | ssl_context.check_hostname = True |
---|
487 | n/a | ssl_context.load_verify_locations(CAFILE) |
---|
488 | n/a | |
---|
489 | n/a | with self.assertRaisesRegex(ssl.CertificateError, |
---|
490 | n/a | "hostname '127.0.0.1' doesn't match 'localhost'"): |
---|
491 | n/a | _, server = self._setup(SimpleIMAPHandler) |
---|
492 | n/a | client = self.imap_class(*server.server_address, |
---|
493 | n/a | ssl_context=ssl_context) |
---|
494 | n/a | client.shutdown() |
---|
495 | n/a | |
---|
496 | n/a | def test_ssl_verified(self): |
---|
497 | n/a | ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
---|
498 | n/a | ssl_context.verify_mode = ssl.CERT_REQUIRED |
---|
499 | n/a | ssl_context.check_hostname = True |
---|
500 | n/a | ssl_context.load_verify_locations(CAFILE) |
---|
501 | n/a | |
---|
502 | n/a | _, server = self._setup(SimpleIMAPHandler) |
---|
503 | n/a | client = self.imap_class("localhost", server.server_address[1], |
---|
504 | n/a | ssl_context=ssl_context) |
---|
505 | n/a | client.shutdown() |
---|
506 | n/a | |
---|
507 | n/a | class ThreadedNetworkedTests(unittest.TestCase): |
---|
508 | n/a | server_class = socketserver.TCPServer |
---|
509 | n/a | imap_class = imaplib.IMAP4 |
---|
510 | n/a | |
---|
511 | n/a | def make_server(self, addr, hdlr): |
---|
512 | n/a | |
---|
513 | n/a | class MyServer(self.server_class): |
---|
514 | n/a | def handle_error(self, request, client_address): |
---|
515 | n/a | self.close_request(request) |
---|
516 | n/a | self.server_close() |
---|
517 | n/a | raise |
---|
518 | n/a | |
---|
519 | n/a | if verbose: |
---|
520 | n/a | print("creating server") |
---|
521 | n/a | server = MyServer(addr, hdlr) |
---|
522 | n/a | self.assertEqual(server.server_address, server.socket.getsockname()) |
---|
523 | n/a | |
---|
524 | n/a | if verbose: |
---|
525 | n/a | print("server created") |
---|
526 | n/a | print("ADDR =", addr) |
---|
527 | n/a | print("CLASS =", self.server_class) |
---|
528 | n/a | print("HDLR =", server.RequestHandlerClass) |
---|
529 | n/a | |
---|
530 | n/a | t = threading.Thread( |
---|
531 | n/a | name='%s serving' % self.server_class, |
---|
532 | n/a | target=server.serve_forever, |
---|
533 | n/a | # Short poll interval to make the test finish quickly. |
---|
534 | n/a | # Time between requests is short enough that we won't wake |
---|
535 | n/a | # up spuriously too many times. |
---|
536 | n/a | kwargs={'poll_interval': 0.01}) |
---|
537 | n/a | t.daemon = True # In case this function raises. |
---|
538 | n/a | t.start() |
---|
539 | n/a | if verbose: |
---|
540 | n/a | print("server running") |
---|
541 | n/a | return server, t |
---|
542 | n/a | |
---|
543 | n/a | def reap_server(self, server, thread): |
---|
544 | n/a | if verbose: |
---|
545 | n/a | print("waiting for server") |
---|
546 | n/a | server.shutdown() |
---|
547 | n/a | server.server_close() |
---|
548 | n/a | thread.join() |
---|
549 | n/a | if verbose: |
---|
550 | n/a | print("done") |
---|
551 | n/a | |
---|
552 | n/a | @contextmanager |
---|
553 | n/a | def reaped_server(self, hdlr): |
---|
554 | n/a | server, thread = self.make_server((support.HOST, 0), hdlr) |
---|
555 | n/a | try: |
---|
556 | n/a | yield server |
---|
557 | n/a | finally: |
---|
558 | n/a | self.reap_server(server, thread) |
---|
559 | n/a | |
---|
560 | n/a | @contextmanager |
---|
561 | n/a | def reaped_pair(self, hdlr): |
---|
562 | n/a | with self.reaped_server(hdlr) as server: |
---|
563 | n/a | client = self.imap_class(*server.server_address) |
---|
564 | n/a | try: |
---|
565 | n/a | yield server, client |
---|
566 | n/a | finally: |
---|
567 | n/a | client.logout() |
---|
568 | n/a | |
---|
569 | n/a | @reap_threads |
---|
570 | n/a | def test_connect(self): |
---|
571 | n/a | with self.reaped_server(SimpleIMAPHandler) as server: |
---|
572 | n/a | client = self.imap_class(*server.server_address) |
---|
573 | n/a | client.shutdown() |
---|
574 | n/a | |
---|
575 | n/a | @reap_threads |
---|
576 | n/a | def test_bracket_flags(self): |
---|
577 | n/a | |
---|
578 | n/a | # This violates RFC 3501, which disallows ']' characters in tag names, |
---|
579 | n/a | # but imaplib has allowed producing such tags forever, other programs |
---|
580 | n/a | # also produce them (eg: OtherInbox's Organizer app as of 20140716), |
---|
581 | n/a | # and Gmail, for example, accepts them and produces them. So we |
---|
582 | n/a | # support them. See issue #21815. |
---|
583 | n/a | |
---|
584 | n/a | class BracketFlagHandler(SimpleIMAPHandler): |
---|
585 | n/a | |
---|
586 | n/a | def handle(self): |
---|
587 | n/a | self.flags = ['Answered', 'Flagged', 'Deleted', 'Seen', 'Draft'] |
---|
588 | n/a | super().handle() |
---|
589 | n/a | |
---|
590 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
591 | n/a | self._send_textline('+') |
---|
592 | n/a | self.server.response = yield |
---|
593 | n/a | self._send_tagged(tag, 'OK', 'FAKEAUTH successful') |
---|
594 | n/a | |
---|
595 | n/a | def cmd_SELECT(self, tag, args): |
---|
596 | n/a | flag_msg = ' \\'.join(self.flags) |
---|
597 | n/a | self._send_line(('* FLAGS (%s)' % flag_msg).encode('ascii')) |
---|
598 | n/a | self._send_line(b'* 2 EXISTS') |
---|
599 | n/a | self._send_line(b'* 0 RECENT') |
---|
600 | n/a | msg = ('* OK [PERMANENTFLAGS %s \\*)] Flags permitted.' |
---|
601 | n/a | % flag_msg) |
---|
602 | n/a | self._send_line(msg.encode('ascii')) |
---|
603 | n/a | self._send_tagged(tag, 'OK', '[READ-WRITE] SELECT completed.') |
---|
604 | n/a | |
---|
605 | n/a | def cmd_STORE(self, tag, args): |
---|
606 | n/a | new_flags = args[2].strip('(').strip(')').split() |
---|
607 | n/a | self.flags.extend(new_flags) |
---|
608 | n/a | flags_msg = '(FLAGS (%s))' % ' \\'.join(self.flags) |
---|
609 | n/a | msg = '* %s FETCH %s' % (args[0], flags_msg) |
---|
610 | n/a | self._send_line(msg.encode('ascii')) |
---|
611 | n/a | self._send_tagged(tag, 'OK', 'STORE completed.') |
---|
612 | n/a | |
---|
613 | n/a | with self.reaped_pair(BracketFlagHandler) as (server, client): |
---|
614 | n/a | code, data = client.authenticate('MYAUTH', lambda x: b'fake') |
---|
615 | n/a | self.assertEqual(code, 'OK') |
---|
616 | n/a | self.assertEqual(server.response, b'ZmFrZQ==\r\n') |
---|
617 | n/a | client.select('test') |
---|
618 | n/a | typ, [data] = client.store(b'1', "+FLAGS", "[test]") |
---|
619 | n/a | self.assertIn(b'[test]', data) |
---|
620 | n/a | client.select('test') |
---|
621 | n/a | typ, [data] = client.response('PERMANENTFLAGS') |
---|
622 | n/a | self.assertIn(b'[test]', data) |
---|
623 | n/a | |
---|
624 | n/a | @reap_threads |
---|
625 | n/a | def test_issue5949(self): |
---|
626 | n/a | |
---|
627 | n/a | class EOFHandler(socketserver.StreamRequestHandler): |
---|
628 | n/a | def handle(self): |
---|
629 | n/a | # EOF without sending a complete welcome message. |
---|
630 | n/a | self.wfile.write(b'* OK') |
---|
631 | n/a | |
---|
632 | n/a | with self.reaped_server(EOFHandler) as server: |
---|
633 | n/a | self.assertRaises(imaplib.IMAP4.abort, |
---|
634 | n/a | self.imap_class, *server.server_address) |
---|
635 | n/a | |
---|
636 | n/a | @reap_threads |
---|
637 | n/a | def test_line_termination(self): |
---|
638 | n/a | |
---|
639 | n/a | class BadNewlineHandler(SimpleIMAPHandler): |
---|
640 | n/a | |
---|
641 | n/a | def cmd_CAPABILITY(self, tag, args): |
---|
642 | n/a | self._send(b'* CAPABILITY IMAP4rev1 AUTH\n') |
---|
643 | n/a | self._send_tagged(tag, 'OK', 'CAPABILITY completed') |
---|
644 | n/a | |
---|
645 | n/a | with self.reaped_server(BadNewlineHandler) as server: |
---|
646 | n/a | self.assertRaises(imaplib.IMAP4.abort, |
---|
647 | n/a | self.imap_class, *server.server_address) |
---|
648 | n/a | |
---|
649 | n/a | class UTF8Server(SimpleIMAPHandler): |
---|
650 | n/a | capabilities = 'AUTH ENABLE UTF8=ACCEPT' |
---|
651 | n/a | |
---|
652 | n/a | def cmd_ENABLE(self, tag, args): |
---|
653 | n/a | self._send_tagged(tag, 'OK', 'ENABLE successful') |
---|
654 | n/a | |
---|
655 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
656 | n/a | self._send_textline('+') |
---|
657 | n/a | self.server.response = yield |
---|
658 | n/a | self._send_tagged(tag, 'OK', 'FAKEAUTH successful') |
---|
659 | n/a | |
---|
660 | n/a | @reap_threads |
---|
661 | n/a | def test_enable_raises_error_if_not_AUTH(self): |
---|
662 | n/a | with self.reaped_pair(self.UTF8Server) as (server, client): |
---|
663 | n/a | self.assertFalse(client.utf8_enabled) |
---|
664 | n/a | self.assertRaises(imaplib.IMAP4.error, client.enable, 'foo') |
---|
665 | n/a | self.assertFalse(client.utf8_enabled) |
---|
666 | n/a | |
---|
667 | n/a | # XXX Also need a test that enable after SELECT raises an error. |
---|
668 | n/a | |
---|
669 | n/a | @reap_threads |
---|
670 | n/a | def test_enable_raises_error_if_no_capability(self): |
---|
671 | n/a | class NoEnableServer(self.UTF8Server): |
---|
672 | n/a | capabilities = 'AUTH' |
---|
673 | n/a | with self.reaped_pair(NoEnableServer) as (server, client): |
---|
674 | n/a | self.assertRaises(imaplib.IMAP4.error, client.enable, 'foo') |
---|
675 | n/a | |
---|
676 | n/a | @reap_threads |
---|
677 | n/a | def test_enable_UTF8_raises_error_if_not_supported(self): |
---|
678 | n/a | class NonUTF8Server(SimpleIMAPHandler): |
---|
679 | n/a | pass |
---|
680 | n/a | with self.assertRaises(imaplib.IMAP4.error): |
---|
681 | n/a | with self.reaped_pair(NonUTF8Server) as (server, client): |
---|
682 | n/a | typ, data = client.login('user', 'pass') |
---|
683 | n/a | self.assertEqual(typ, 'OK') |
---|
684 | n/a | client.enable('UTF8=ACCEPT') |
---|
685 | n/a | pass |
---|
686 | n/a | |
---|
687 | n/a | @reap_threads |
---|
688 | n/a | def test_enable_UTF8_True_append(self): |
---|
689 | n/a | |
---|
690 | n/a | class UTF8AppendServer(self.UTF8Server): |
---|
691 | n/a | def cmd_APPEND(self, tag, args): |
---|
692 | n/a | self._send_textline('+') |
---|
693 | n/a | self.server.response = yield |
---|
694 | n/a | self._send_tagged(tag, 'OK', 'okay') |
---|
695 | n/a | |
---|
696 | n/a | with self.reaped_pair(UTF8AppendServer) as (server, client): |
---|
697 | n/a | self.assertEqual(client._encoding, 'ascii') |
---|
698 | n/a | code, _ = client.authenticate('MYAUTH', lambda x: b'fake') |
---|
699 | n/a | self.assertEqual(code, 'OK') |
---|
700 | n/a | self.assertEqual(server.response, |
---|
701 | n/a | b'ZmFrZQ==\r\n') # b64 encoded 'fake' |
---|
702 | n/a | code, _ = client.enable('UTF8=ACCEPT') |
---|
703 | n/a | self.assertEqual(code, 'OK') |
---|
704 | n/a | self.assertEqual(client._encoding, 'utf-8') |
---|
705 | n/a | msg_string = 'Subject: üñéöðé' |
---|
706 | n/a | typ, data = client.append( |
---|
707 | n/a | None, None, None, msg_string.encode('utf-8')) |
---|
708 | n/a | self.assertEqual(typ, 'OK') |
---|
709 | n/a | self.assertEqual( |
---|
710 | n/a | server.response, |
---|
711 | n/a | ('UTF8 (%s)\r\n' % msg_string).encode('utf-8') |
---|
712 | n/a | ) |
---|
713 | n/a | |
---|
714 | n/a | # XXX also need a test that makes sure that the Literal and Untagged_status |
---|
715 | n/a | # regexes uses unicode in UTF8 mode instead of the default ASCII. |
---|
716 | n/a | |
---|
717 | n/a | @reap_threads |
---|
718 | n/a | def test_search_disallows_charset_in_utf8_mode(self): |
---|
719 | n/a | with self.reaped_pair(self.UTF8Server) as (server, client): |
---|
720 | n/a | typ, _ = client.authenticate('MYAUTH', lambda x: b'fake') |
---|
721 | n/a | self.assertEqual(typ, 'OK') |
---|
722 | n/a | typ, _ = client.enable('UTF8=ACCEPT') |
---|
723 | n/a | self.assertEqual(typ, 'OK') |
---|
724 | n/a | self.assertTrue(client.utf8_enabled) |
---|
725 | n/a | self.assertRaises(imaplib.IMAP4.error, client.search, 'foo', 'bar') |
---|
726 | n/a | |
---|
727 | n/a | @reap_threads |
---|
728 | n/a | def test_bad_auth_name(self): |
---|
729 | n/a | |
---|
730 | n/a | class MyServer(SimpleIMAPHandler): |
---|
731 | n/a | |
---|
732 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
733 | n/a | self._send_tagged(tag, 'NO', 'unrecognized authentication ' |
---|
734 | n/a | 'type {}'.format(args[0])) |
---|
735 | n/a | |
---|
736 | n/a | with self.reaped_pair(MyServer) as (server, client): |
---|
737 | n/a | with self.assertRaises(imaplib.IMAP4.error): |
---|
738 | n/a | client.authenticate('METHOD', lambda: 1) |
---|
739 | n/a | |
---|
740 | n/a | @reap_threads |
---|
741 | n/a | def test_invalid_authentication(self): |
---|
742 | n/a | |
---|
743 | n/a | class MyServer(SimpleIMAPHandler): |
---|
744 | n/a | |
---|
745 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
746 | n/a | self._send_textline('+') |
---|
747 | n/a | self.response = yield |
---|
748 | n/a | self._send_tagged(tag, 'NO', '[AUTHENTICATIONFAILED] invalid') |
---|
749 | n/a | |
---|
750 | n/a | with self.reaped_pair(MyServer) as (server, client): |
---|
751 | n/a | with self.assertRaises(imaplib.IMAP4.error): |
---|
752 | n/a | code, data = client.authenticate('MYAUTH', lambda x: b'fake') |
---|
753 | n/a | |
---|
754 | n/a | @reap_threads |
---|
755 | n/a | def test_valid_authentication(self): |
---|
756 | n/a | |
---|
757 | n/a | class MyServer(SimpleIMAPHandler): |
---|
758 | n/a | |
---|
759 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
760 | n/a | self._send_textline('+') |
---|
761 | n/a | self.server.response = yield |
---|
762 | n/a | self._send_tagged(tag, 'OK', 'FAKEAUTH successful') |
---|
763 | n/a | |
---|
764 | n/a | with self.reaped_pair(MyServer) as (server, client): |
---|
765 | n/a | code, data = client.authenticate('MYAUTH', lambda x: b'fake') |
---|
766 | n/a | self.assertEqual(code, 'OK') |
---|
767 | n/a | self.assertEqual(server.response, |
---|
768 | n/a | b'ZmFrZQ==\r\n') # b64 encoded 'fake' |
---|
769 | n/a | |
---|
770 | n/a | with self.reaped_pair(MyServer) as (server, client): |
---|
771 | n/a | code, data = client.authenticate('MYAUTH', lambda x: 'fake') |
---|
772 | n/a | self.assertEqual(code, 'OK') |
---|
773 | n/a | self.assertEqual(server.response, |
---|
774 | n/a | b'ZmFrZQ==\r\n') # b64 encoded 'fake' |
---|
775 | n/a | |
---|
776 | n/a | @reap_threads |
---|
777 | n/a | def test_login_cram_md5(self): |
---|
778 | n/a | |
---|
779 | n/a | class AuthHandler(SimpleIMAPHandler): |
---|
780 | n/a | |
---|
781 | n/a | capabilities = 'LOGINDISABLED AUTH=CRAM-MD5' |
---|
782 | n/a | |
---|
783 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
784 | n/a | self._send_textline('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm' |
---|
785 | n/a | 'VzdG9uLm1jaS5uZXQ=') |
---|
786 | n/a | r = yield |
---|
787 | n/a | if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT' |
---|
788 | n/a | b'FjY2E2ZmZkNmNmMmQ5ZjMy\r\n'): |
---|
789 | n/a | self._send_tagged(tag, 'OK', 'CRAM-MD5 successful') |
---|
790 | n/a | else: |
---|
791 | n/a | self._send_tagged(tag, 'NO', 'No access') |
---|
792 | n/a | |
---|
793 | n/a | with self.reaped_pair(AuthHandler) as (server, client): |
---|
794 | n/a | self.assertTrue('AUTH=CRAM-MD5' in client.capabilities) |
---|
795 | n/a | ret, data = client.login_cram_md5("tim", "tanstaaftanstaaf") |
---|
796 | n/a | self.assertEqual(ret, "OK") |
---|
797 | n/a | |
---|
798 | n/a | with self.reaped_pair(AuthHandler) as (server, client): |
---|
799 | n/a | self.assertTrue('AUTH=CRAM-MD5' in client.capabilities) |
---|
800 | n/a | ret, data = client.login_cram_md5("tim", b"tanstaaftanstaaf") |
---|
801 | n/a | self.assertEqual(ret, "OK") |
---|
802 | n/a | |
---|
803 | n/a | |
---|
804 | n/a | @reap_threads |
---|
805 | n/a | def test_aborted_authentication(self): |
---|
806 | n/a | |
---|
807 | n/a | class MyServer(SimpleIMAPHandler): |
---|
808 | n/a | |
---|
809 | n/a | def cmd_AUTHENTICATE(self, tag, args): |
---|
810 | n/a | self._send_textline('+') |
---|
811 | n/a | self.response = yield |
---|
812 | n/a | |
---|
813 | n/a | if self.response == b'*\r\n': |
---|
814 | n/a | self._send_tagged(tag, 'NO', '[AUTHENTICATIONFAILED] aborted') |
---|
815 | n/a | else: |
---|
816 | n/a | self._send_tagged(tag, 'OK', 'MYAUTH successful') |
---|
817 | n/a | |
---|
818 | n/a | with self.reaped_pair(MyServer) as (server, client): |
---|
819 | n/a | with self.assertRaises(imaplib.IMAP4.error): |
---|
820 | n/a | code, data = client.authenticate('MYAUTH', lambda x: None) |
---|
821 | n/a | |
---|
822 | n/a | |
---|
823 | n/a | def test_linetoolong(self): |
---|
824 | n/a | class TooLongHandler(SimpleIMAPHandler): |
---|
825 | n/a | def handle(self): |
---|
826 | n/a | # Send a very long response line |
---|
827 | n/a | self.wfile.write(b'* OK ' + imaplib._MAXLINE * b'x' + b'\r\n') |
---|
828 | n/a | |
---|
829 | n/a | with self.reaped_server(TooLongHandler) as server: |
---|
830 | n/a | self.assertRaises(imaplib.IMAP4.error, |
---|
831 | n/a | self.imap_class, *server.server_address) |
---|
832 | n/a | |
---|
833 | n/a | @reap_threads |
---|
834 | n/a | def test_simple_with_statement(self): |
---|
835 | n/a | # simplest call |
---|
836 | n/a | with self.reaped_server(SimpleIMAPHandler) as server: |
---|
837 | n/a | with self.imap_class(*server.server_address): |
---|
838 | n/a | pass |
---|
839 | n/a | |
---|
840 | n/a | @reap_threads |
---|
841 | n/a | def test_with_statement(self): |
---|
842 | n/a | with self.reaped_server(SimpleIMAPHandler) as server: |
---|
843 | n/a | with self.imap_class(*server.server_address) as imap: |
---|
844 | n/a | imap.login('user', 'pass') |
---|
845 | n/a | self.assertEqual(server.logged, 'user') |
---|
846 | n/a | self.assertIsNone(server.logged) |
---|
847 | n/a | |
---|
848 | n/a | @reap_threads |
---|
849 | n/a | def test_with_statement_logout(self): |
---|
850 | n/a | # what happens if already logout in the block? |
---|
851 | n/a | with self.reaped_server(SimpleIMAPHandler) as server: |
---|
852 | n/a | with self.imap_class(*server.server_address) as imap: |
---|
853 | n/a | imap.login('user', 'pass') |
---|
854 | n/a | self.assertEqual(server.logged, 'user') |
---|
855 | n/a | imap.logout() |
---|
856 | n/a | self.assertIsNone(server.logged) |
---|
857 | n/a | self.assertIsNone(server.logged) |
---|
858 | n/a | |
---|
859 | n/a | |
---|
860 | n/a | @unittest.skipUnless(ssl, "SSL not available") |
---|
861 | n/a | class ThreadedNetworkedTestsSSL(ThreadedNetworkedTests): |
---|
862 | n/a | server_class = SecureTCPServer |
---|
863 | n/a | imap_class = IMAP4_SSL |
---|
864 | n/a | |
---|
865 | n/a | @reap_threads |
---|
866 | n/a | def test_ssl_verified(self): |
---|
867 | n/a | ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
---|
868 | n/a | ssl_context.verify_mode = ssl.CERT_REQUIRED |
---|
869 | n/a | ssl_context.check_hostname = True |
---|
870 | n/a | ssl_context.load_verify_locations(CAFILE) |
---|
871 | n/a | |
---|
872 | n/a | with self.assertRaisesRegex( |
---|
873 | n/a | ssl.CertificateError, |
---|
874 | n/a | "hostname '127.0.0.1' doesn't match 'localhost'"): |
---|
875 | n/a | with self.reaped_server(SimpleIMAPHandler) as server: |
---|
876 | n/a | client = self.imap_class(*server.server_address, |
---|
877 | n/a | ssl_context=ssl_context) |
---|
878 | n/a | client.shutdown() |
---|
879 | n/a | |
---|
880 | n/a | with self.reaped_server(SimpleIMAPHandler) as server: |
---|
881 | n/a | client = self.imap_class("localhost", server.server_address[1], |
---|
882 | n/a | ssl_context=ssl_context) |
---|
883 | n/a | client.shutdown() |
---|
884 | n/a | |
---|
885 | n/a | |
---|
886 | n/a | @unittest.skipUnless( |
---|
887 | n/a | support.is_resource_enabled('network'), 'network resource disabled') |
---|
888 | n/a | class RemoteIMAPTest(unittest.TestCase): |
---|
889 | n/a | host = 'cyrus.andrew.cmu.edu' |
---|
890 | n/a | port = 143 |
---|
891 | n/a | username = 'anonymous' |
---|
892 | n/a | password = 'pass' |
---|
893 | n/a | imap_class = imaplib.IMAP4 |
---|
894 | n/a | |
---|
895 | n/a | def setUp(self): |
---|
896 | n/a | with transient_internet(self.host): |
---|
897 | n/a | self.server = self.imap_class(self.host, self.port) |
---|
898 | n/a | |
---|
899 | n/a | def tearDown(self): |
---|
900 | n/a | if self.server is not None: |
---|
901 | n/a | with transient_internet(self.host): |
---|
902 | n/a | self.server.logout() |
---|
903 | n/a | |
---|
904 | n/a | def test_logincapa(self): |
---|
905 | n/a | with transient_internet(self.host): |
---|
906 | n/a | for cap in self.server.capabilities: |
---|
907 | n/a | self.assertIsInstance(cap, str) |
---|
908 | n/a | self.assertIn('LOGINDISABLED', self.server.capabilities) |
---|
909 | n/a | self.assertIn('AUTH=ANONYMOUS', self.server.capabilities) |
---|
910 | n/a | rs = self.server.login(self.username, self.password) |
---|
911 | n/a | self.assertEqual(rs[0], 'OK') |
---|
912 | n/a | |
---|
913 | n/a | def test_logout(self): |
---|
914 | n/a | with transient_internet(self.host): |
---|
915 | n/a | rs = self.server.logout() |
---|
916 | n/a | self.server = None |
---|
917 | n/a | self.assertEqual(rs[0], 'BYE') |
---|
918 | n/a | |
---|
919 | n/a | |
---|
920 | n/a | @unittest.skipUnless(ssl, "SSL not available") |
---|
921 | n/a | @unittest.skipUnless( |
---|
922 | n/a | support.is_resource_enabled('network'), 'network resource disabled') |
---|
923 | n/a | class RemoteIMAP_STARTTLSTest(RemoteIMAPTest): |
---|
924 | n/a | |
---|
925 | n/a | def setUp(self): |
---|
926 | n/a | super().setUp() |
---|
927 | n/a | with transient_internet(self.host): |
---|
928 | n/a | rs = self.server.starttls() |
---|
929 | n/a | self.assertEqual(rs[0], 'OK') |
---|
930 | n/a | |
---|
931 | n/a | def test_logincapa(self): |
---|
932 | n/a | for cap in self.server.capabilities: |
---|
933 | n/a | self.assertIsInstance(cap, str) |
---|
934 | n/a | self.assertNotIn('LOGINDISABLED', self.server.capabilities) |
---|
935 | n/a | |
---|
936 | n/a | |
---|
937 | n/a | @unittest.skipUnless(ssl, "SSL not available") |
---|
938 | n/a | class RemoteIMAP_SSLTest(RemoteIMAPTest): |
---|
939 | n/a | port = 993 |
---|
940 | n/a | imap_class = IMAP4_SSL |
---|
941 | n/a | |
---|
942 | n/a | def setUp(self): |
---|
943 | n/a | pass |
---|
944 | n/a | |
---|
945 | n/a | def tearDown(self): |
---|
946 | n/a | pass |
---|
947 | n/a | |
---|
948 | n/a | def create_ssl_context(self): |
---|
949 | n/a | ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) |
---|
950 | n/a | ssl_context.load_cert_chain(CERTFILE) |
---|
951 | n/a | return ssl_context |
---|
952 | n/a | |
---|
953 | n/a | def check_logincapa(self, server): |
---|
954 | n/a | try: |
---|
955 | n/a | for cap in server.capabilities: |
---|
956 | n/a | self.assertIsInstance(cap, str) |
---|
957 | n/a | self.assertNotIn('LOGINDISABLED', server.capabilities) |
---|
958 | n/a | self.assertIn('AUTH=PLAIN', server.capabilities) |
---|
959 | n/a | rs = server.login(self.username, self.password) |
---|
960 | n/a | self.assertEqual(rs[0], 'OK') |
---|
961 | n/a | finally: |
---|
962 | n/a | server.logout() |
---|
963 | n/a | |
---|
964 | n/a | def test_logincapa(self): |
---|
965 | n/a | with transient_internet(self.host): |
---|
966 | n/a | _server = self.imap_class(self.host, self.port) |
---|
967 | n/a | self.check_logincapa(_server) |
---|
968 | n/a | |
---|
969 | n/a | def test_logincapa_with_client_certfile(self): |
---|
970 | n/a | with transient_internet(self.host): |
---|
971 | n/a | with support.check_warnings(('', DeprecationWarning)): |
---|
972 | n/a | _server = self.imap_class(self.host, self.port, |
---|
973 | n/a | certfile=CERTFILE) |
---|
974 | n/a | self.check_logincapa(_server) |
---|
975 | n/a | |
---|
976 | n/a | def test_logincapa_with_client_ssl_context(self): |
---|
977 | n/a | with transient_internet(self.host): |
---|
978 | n/a | _server = self.imap_class( |
---|
979 | n/a | self.host, self.port, ssl_context=self.create_ssl_context()) |
---|
980 | n/a | self.check_logincapa(_server) |
---|
981 | n/a | |
---|
982 | n/a | def test_logout(self): |
---|
983 | n/a | with transient_internet(self.host): |
---|
984 | n/a | _server = self.imap_class(self.host, self.port) |
---|
985 | n/a | rs = _server.logout() |
---|
986 | n/a | self.assertEqual(rs[0], 'BYE') |
---|
987 | n/a | |
---|
988 | n/a | def test_ssl_context_certfile_exclusive(self): |
---|
989 | n/a | with transient_internet(self.host): |
---|
990 | n/a | self.assertRaises( |
---|
991 | n/a | ValueError, self.imap_class, self.host, self.port, |
---|
992 | n/a | certfile=CERTFILE, ssl_context=self.create_ssl_context()) |
---|
993 | n/a | |
---|
994 | n/a | def test_ssl_context_keyfile_exclusive(self): |
---|
995 | n/a | with transient_internet(self.host): |
---|
996 | n/a | self.assertRaises( |
---|
997 | n/a | ValueError, self.imap_class, self.host, self.port, |
---|
998 | n/a | keyfile=CERTFILE, ssl_context=self.create_ssl_context()) |
---|
999 | n/a | |
---|
1000 | n/a | |
---|
1001 | n/a | if __name__ == "__main__": |
---|
1002 | n/a | unittest.main() |
---|