1111import time
1212import calendar
1313
14- from test .support import reap_threads , verbose , transient_internet , run_with_tz , run_with_locale
14+ from test .support import (reap_threads , verbose , transient_internet ,
15+ run_with_tz , run_with_locale )
1516import unittest
1617from datetime import datetime , timezone , timedelta
1718try :
2223else :
2324 from ssl import HAS_SNI
2425
25- CERTFILE = None
26- CAFILE = None
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" )
2728
2829
2930class TestImaplib (unittest .TestCase ):
@@ -44,17 +45,15 @@ def test_Internaldate2tuple(self):
4445 def test_Internaldate2tuple_issue10941 (self ):
4546 self .assertNotEqual (imaplib .Internaldate2tuple (
4647 b'25 (INTERNALDATE "02-Apr-2000 02:30:00 +0000")' ),
47- imaplib .Internaldate2tuple (
48- b'25 (INTERNALDATE "02-Apr-2000 03:30:00 +0000")' ))
49-
50-
48+ imaplib .Internaldate2tuple (
49+ b'25 (INTERNALDATE "02-Apr-2000 03:30:00 +0000")' ))
5150
5251 def timevalues (self ):
5352 return [2000000000 , 2000000000.0 , time .localtime (2000000000 ),
5453 (2033 , 5 , 18 , 5 , 33 , 20 , - 1 , - 1 , - 1 ),
5554 (2033 , 5 , 18 , 5 , 33 , 20 , - 1 , - 1 , 1 ),
5655 datetime .fromtimestamp (2000000000 ,
57- timezone (timedelta (0 , 2 * 60 * 60 ))),
56+ timezone (timedelta (0 , 2 * 60 * 60 ))),
5857 '"18-May-2033 05:33:20 +0200"' ]
5958
6059 @run_with_locale ('LC_ALL' , 'de_DE' , 'fr_FR' )
@@ -75,7 +74,6 @@ def test_that_Time2Internaldate_returns_a_result(self):
7574
7675
7776if ssl :
78-
7977 class SecureTCPServer (socketserver .TCPServer ):
8078
8179 def get_request (self ):
@@ -96,13 +94,13 @@ class SecureTCPServer:
9694
9795
9896class SimpleIMAPHandler (socketserver .StreamRequestHandler ):
99-
10097 timeout = 1
10198 continuation = None
10299 capabilities = ''
103100
104101 def _send (self , message ):
105- if verbose : print ("SENT: %r" % message .strip ())
102+ if verbose :
103+ print ("SENT: %r" % message .strip ())
106104 self .wfile .write (message )
107105
108106 def _send_line (self , message ):
@@ -135,7 +133,8 @@ def handle(self):
135133 if line .endswith (b'\r \n ' ):
136134 break
137135
138- if verbose : print ('GOT: %r' % line .strip ())
136+ if verbose :
137+ print ('GOT: %r' % line .strip ())
139138 if self .continuation :
140139 try :
141140 self .continuation .send (line )
@@ -147,16 +146,18 @@ def handle(self):
147146 cmd = splitline [1 ]
148147 args = splitline [2 :]
149148
150- if hasattr (self , 'cmd_' + cmd ):
151- continuation = getattr (self , 'cmd_' + cmd )(tag , args )
149+ if hasattr (self , 'cmd_' + cmd ):
150+ continuation = getattr (self , 'cmd_' + cmd )(tag , args )
152151 if continuation :
153152 self .continuation = continuation
154153 next (continuation )
155154 else :
156155 self ._send_tagged (tag , 'BAD' , cmd + ' unknown' )
157156
158157 def cmd_CAPABILITY (self , tag , args ):
159- caps = 'IMAP4rev1 ' + self .capabilities if self .capabilities else 'IMAP4rev1'
158+ caps = ('IMAP4rev1 ' + self .capabilities
159+ if self .capabilities
160+ else 'IMAP4rev1' )
160161 self ._send_textline ('* CAPABILITY ' + caps )
161162 self ._send_tagged (tag , 'OK' , 'CAPABILITY completed' )
162163
@@ -165,7 +166,9 @@ def cmd_LOGOUT(self, tag, args):
165166 self ._send_tagged (tag , 'OK' , 'LOGOUT completed' )
166167
167168
168- class BaseThreadedNetworkedTests (unittest .TestCase ):
169+ class ThreadedNetworkedTests (unittest .TestCase ):
170+ server_class = socketserver .TCPServer
171+ imap_class = imaplib .IMAP4
169172
170173 def make_server (self , addr , hdlr ):
171174
@@ -175,7 +178,8 @@ def handle_error(self, request, client_address):
175178 self .server_close ()
176179 raise
177180
178- if verbose : print ("creating server" )
181+ if verbose :
182+ print ("creating server" )
179183 server = MyServer (addr , hdlr )
180184 self .assertEqual (server .server_address , server .socket .getsockname ())
181185
@@ -191,18 +195,21 @@ def handle_error(self, request, client_address):
191195 # Short poll interval to make the test finish quickly.
192196 # Time between requests is short enough that we won't wake
193197 # up spuriously too many times.
194- kwargs = {'poll_interval' :0.01 })
198+ kwargs = {'poll_interval' : 0.01 })
195199 t .daemon = True # In case this function raises.
196200 t .start ()
197- if verbose : print ("server running" )
201+ if verbose :
202+ print ("server running" )
198203 return server , t
199204
200205 def reap_server (self , server , thread ):
201- if verbose : print ("waiting for server" )
206+ if verbose :
207+ print ("waiting for server" )
202208 server .shutdown ()
203209 server .server_close ()
204210 thread .join ()
205- if verbose : print ("done" )
211+ if verbose :
212+ print ("done" )
206213
207214 @contextmanager
208215 def reaped_server (self , hdlr ):
@@ -259,7 +266,7 @@ class MyServer(SimpleIMAPHandler):
259266
260267 def cmd_AUTHENTICATE (self , tag , args ):
261268 self ._send_tagged (tag , 'NO' , 'unrecognized authentication '
262- 'type {}' .format (args [0 ]))
269+ 'type {}' .format (args [0 ]))
263270
264271 with self .reaped_pair (MyServer ) as (server , client ):
265272 with self .assertRaises (imaplib .IMAP4 .error ):
@@ -293,13 +300,13 @@ def cmd_AUTHENTICATE(self, tag, args):
293300 code , data = client .authenticate ('MYAUTH' , lambda x : b'fake' )
294301 self .assertEqual (code , 'OK' )
295302 self .assertEqual (server .response ,
296- b'ZmFrZQ==\r \n ' ) # b64 encoded 'fake'
303+ b'ZmFrZQ==\r \n ' ) # b64 encoded 'fake'
297304
298305 with self .reaped_pair (MyServer ) as (server , client ):
299306 code , data = client .authenticate ('MYAUTH' , lambda x : 'fake' )
300307 self .assertEqual (code , 'OK' )
301308 self .assertEqual (server .response ,
302- b'ZmFrZQ==\r \n ' ) # b64 encoded 'fake'
309+ b'ZmFrZQ==\r \n ' ) # b64 encoded 'fake'
303310
304311 @reap_threads
305312 def test_login_cram_md5 (self ):
@@ -310,9 +317,10 @@ class AuthHandler(SimpleIMAPHandler):
310317
311318 def cmd_AUTHENTICATE (self , tag , args ):
312319 self ._send_textline ('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm'
313- 'VzdG9uLm1jaS5uZXQ=' )
320+ 'VzdG9uLm1jaS5uZXQ=' )
314321 r = yield
315- if r == b'dGltIGYxY2E2YmU0NjRiOWVmYTFjY2E2ZmZkNmNmMmQ5ZjMy\r \n ' :
322+ if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT'
323+ b'FjY2E2ZmZkNmNmMmQ5ZjMy\r \n ' ):
316324 self ._send_tagged (tag , 'OK' , 'CRAM-MD5 successful' )
317325 else :
318326 self ._send_tagged (tag , 'NO' , 'No access' )
@@ -327,27 +335,19 @@ def cmd_AUTHENTICATE(self, tag, args):
327335 ret , data = client .login_cram_md5 ("tim" , b"tanstaaftanstaaf" )
328336 self .assertEqual (ret , "OK" )
329337
330-
331338 def test_linetoolong (self ):
332339 class TooLongHandler (SimpleIMAPHandler ):
333340 def handle (self ):
334341 # Send a very long response line
335- self .wfile .write (b'* OK ' + imaplib ._MAXLINE * b'x' + b'\r \n ' )
342+ self .wfile .write (b'* OK ' + imaplib ._MAXLINE * b'x' + b'\r \n ' )
336343
337344 with self .reaped_server (TooLongHandler ) as server :
338345 self .assertRaises (imaplib .IMAP4 .error ,
339346 self .imap_class , * server .server_address )
340347
341348
342- class ThreadedNetworkedTests (BaseThreadedNetworkedTests ):
343-
344- server_class = socketserver .TCPServer
345- imap_class = imaplib .IMAP4
346-
347-
348349@unittest .skipUnless (ssl , "SSL not available" )
349- class ThreadedNetworkedTestsSSL (BaseThreadedNetworkedTests ):
350-
350+ class ThreadedNetworkedTestsSSL (ThreadedNetworkedTests ):
351351 server_class = SecureTCPServer
352352 imap_class = IMAP4_SSL
353353
@@ -359,8 +359,9 @@ def test_ssl_verified(self):
359359 ssl_context .check_hostname = True
360360 ssl_context .load_verify_locations (CAFILE )
361361
362- with self .assertRaisesRegex (ssl .CertificateError ,
363- "hostname '127.0.0.1' doesn't match 'localhost'" ):
362+ with self .assertRaisesRegex (
363+ ssl .CertificateError ,
364+ "hostname '127.0.0.1' doesn't match 'localhost'" ):
364365 with self .reaped_server (SimpleIMAPHandler ) as server :
365366 client = self .imap_class (* server .server_address ,
366367 ssl_context = ssl_context )
@@ -372,6 +373,8 @@ def test_ssl_verified(self):
372373 client .shutdown ()
373374
374375
376+ @unittest .skipUnless (
377+ support .is_resource_enabled ('network' ), 'network resource disabled' )
375378class RemoteIMAPTest (unittest .TestCase ):
376379 host = 'cyrus.andrew.cmu.edu'
377380 port = 143
@@ -405,6 +408,8 @@ def test_logout(self):
405408
406409
407410@unittest .skipUnless (ssl , "SSL not available" )
411+ @unittest .skipUnless (
412+ support .is_resource_enabled ('network' ), 'network resource disabled' )
408413class RemoteIMAP_STARTTLSTest (RemoteIMAPTest ):
409414
410415 def setUp (self ):
@@ -458,7 +463,8 @@ def test_logincapa_with_client_certfile(self):
458463
459464 def test_logincapa_with_client_ssl_context (self ):
460465 with transient_internet (self .host ):
461- _server = self .imap_class (self .host , self .port , ssl_context = self .create_ssl_context ())
466+ _server = self .imap_class (
467+ self .host , self .port , ssl_context = self .create_ssl_context ())
462468 self .check_logincapa (_server )
463469
464470 def test_logout (self ):
@@ -469,35 +475,15 @@ def test_logout(self):
469475
470476 def test_ssl_context_certfile_exclusive (self ):
471477 with transient_internet (self .host ):
472- self .assertRaises (ValueError , self .imap_class , self .host , self .port ,
473- certfile = CERTFILE , ssl_context = self .create_ssl_context ())
478+ self .assertRaises (
479+ ValueError , self .imap_class , self .host , self .port ,
480+ certfile = CERTFILE , ssl_context = self .create_ssl_context ())
474481
475482 def test_ssl_context_keyfile_exclusive (self ):
476483 with transient_internet (self .host ):
477- self .assertRaises (ValueError , self .imap_class , self .host , self .port ,
478- keyfile = CERTFILE , ssl_context = self .create_ssl_context ())
479-
480-
481- def load_tests (* args ):
482- tests = [TestImaplib ]
483-
484- if support .is_resource_enabled ('network' ):
485- if ssl :
486- global CERTFILE , CAFILE
487- CERTFILE = os .path .join (os .path .dirname (__file__ ) or os .curdir ,
488- "keycert3.pem" )
489- if not os .path .exists (CERTFILE ):
490- raise support .TestFailed ("Can't read certificate files!" )
491- CAFILE = os .path .join (os .path .dirname (__file__ ) or os .curdir ,
492- "pycacert.pem" )
493- if not os .path .exists (CAFILE ):
494- raise support .TestFailed ("Can't read CA file!" )
495- tests .extend ([
496- ThreadedNetworkedTests , ThreadedNetworkedTestsSSL ,
497- RemoteIMAPTest , RemoteIMAP_SSLTest , RemoteIMAP_STARTTLSTest ,
498- ])
499-
500- return unittest .TestSuite ([unittest .makeSuite (test ) for test in tests ])
484+ self .assertRaises (
485+ ValueError , self .imap_class , self .host , self .port ,
486+ keyfile = CERTFILE , ssl_context = self .create_ssl_context ())
501487
502488
503489if __name__ == "__main__" :
0 commit comments