@@ -256,7 +256,20 @@ def cmd_IDLE(self, tag, args):
256
256
self ._send_tagged (tag , 'BAD' , 'Expected DONE' )
257
257
258
258
259
- class NewIMAPTestsMixin ():
259
+ class AuthHandler_CRAM_MD5 (SimpleIMAPHandler ):
260
+ capabilities = 'LOGINDISABLED AUTH=CRAM-MD5'
261
+ def cmd_AUTHENTICATE (self , tag , args ):
262
+ self ._send_textline ('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm'
263
+ 'VzdG9uLm1jaS5uZXQ=' )
264
+ r = yield
265
+ if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT'
266
+ b'FjY2E2ZmZkNmNmMmQ5ZjMy\r \n ' ):
267
+ self ._send_tagged (tag , 'OK' , 'CRAM-MD5 successful' )
268
+ else :
269
+ self ._send_tagged (tag , 'NO' , 'No access' )
270
+
271
+
272
+ class NewIMAPTestsMixin :
260
273
client = None
261
274
262
275
def _setup (self , imap_handler , connect = True ):
@@ -439,40 +452,31 @@ def cmd_AUTHENTICATE(self, tag, args):
439
452
440
453
@hashlib_helper .requires_hashdigest ('md5' , openssl = True )
441
454
def test_login_cram_md5_bytes (self ):
442
- class AuthHandler (SimpleIMAPHandler ):
443
- capabilities = 'LOGINDISABLED AUTH=CRAM-MD5'
444
- def cmd_AUTHENTICATE (self , tag , args ):
445
- self ._send_textline ('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm'
446
- 'VzdG9uLm1jaS5uZXQ=' )
447
- r = yield
448
- if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT'
449
- b'FjY2E2ZmZkNmNmMmQ5ZjMy\r \n ' ):
450
- self ._send_tagged (tag , 'OK' , 'CRAM-MD5 successful' )
451
- else :
452
- self ._send_tagged (tag , 'NO' , 'No access' )
453
- client , _ = self ._setup (AuthHandler )
454
- self .assertTrue ('AUTH=CRAM-MD5' in client .capabilities )
455
+ client , _ = self ._setup (AuthHandler_CRAM_MD5 )
456
+ self .assertIn ('AUTH=CRAM-MD5' , client .capabilities )
455
457
ret , _ = client .login_cram_md5 ("tim" , b"tanstaaftanstaaf" )
456
458
self .assertEqual (ret , "OK" )
457
459
458
460
@hashlib_helper .requires_hashdigest ('md5' , openssl = True )
459
461
def test_login_cram_md5_plain_text (self ):
460
- class AuthHandler (SimpleIMAPHandler ):
461
- capabilities = 'LOGINDISABLED AUTH=CRAM-MD5'
462
- def cmd_AUTHENTICATE (self , tag , args ):
463
- self ._send_textline ('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm'
464
- 'VzdG9uLm1jaS5uZXQ=' )
465
- r = yield
466
- if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT'
467
- b'FjY2E2ZmZkNmNmMmQ5ZjMy\r \n ' ):
468
- self ._send_tagged (tag , 'OK' , 'CRAM-MD5 successful' )
469
- else :
470
- self ._send_tagged (tag , 'NO' , 'No access' )
471
- client , _ = self ._setup (AuthHandler )
472
- self .assertTrue ('AUTH=CRAM-MD5' in client .capabilities )
462
+ client , _ = self ._setup (AuthHandler_CRAM_MD5 )
463
+ self .assertIn ('AUTH=CRAM-MD5' , client .capabilities )
473
464
ret , _ = client .login_cram_md5 ("tim" , "tanstaaftanstaaf" )
474
465
self .assertEqual (ret , "OK" )
475
466
467
+ def test_login_cram_md5_blocked (self ):
468
+ def side_effect (* a , ** kw ):
469
+ raise ValueError
470
+
471
+ client , _ = self ._setup (AuthHandler_CRAM_MD5 )
472
+ self .assertIn ('AUTH=CRAM-MD5' , client .capabilities )
473
+ msg = re .escape ("CRAM-MD5 authentication is not supported" )
474
+ with (
475
+ mock .patch ("hmac.HMAC" , side_effect = side_effect ),
476
+ self .assertRaisesRegex (imaplib .IMAP4 .error , msg )
477
+ ):
478
+ client .login_cram_md5 ("tim" , b"tanstaaftanstaaf" )
479
+
476
480
def test_aborted_authentication (self ):
477
481
class MyServer (SimpleIMAPHandler ):
478
482
def cmd_AUTHENTICATE (self , tag , args ):
0 commit comments