@@ -208,7 +208,20 @@ def cmd_UNSELECT(self, tag, args):
208
208
self ._send_tagged (tag , 'BAD' , 'No mailbox selected' )
209
209
210
210
211
- class NewIMAPTestsMixin ():
211
+ class AuthHandler_CRAM_MD5 (SimpleIMAPHandler ):
212
+ capabilities = 'LOGINDISABLED AUTH=CRAM-MD5'
213
+ def cmd_AUTHENTICATE (self , tag , args ):
214
+ self ._send_textline ('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm'
215
+ 'VzdG9uLm1jaS5uZXQ=' )
216
+ r = yield
217
+ if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT'
218
+ b'FjY2E2ZmZkNmNmMmQ5ZjMy\r \n ' ):
219
+ self ._send_tagged (tag , 'OK' , 'CRAM-MD5 successful' )
220
+ else :
221
+ self ._send_tagged (tag , 'NO' , 'No access' )
222
+
223
+
224
+ class NewIMAPTestsMixin :
212
225
client = None
213
226
214
227
def _setup (self , imap_handler , connect = True ):
@@ -391,40 +404,31 @@ def cmd_AUTHENTICATE(self, tag, args):
391
404
392
405
@hashlib_helper .requires_hashdigest ('md5' , openssl = True )
393
406
def test_login_cram_md5_bytes (self ):
394
- class AuthHandler (SimpleIMAPHandler ):
395
- capabilities = 'LOGINDISABLED AUTH=CRAM-MD5'
396
- def cmd_AUTHENTICATE (self , tag , args ):
397
- self ._send_textline ('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm'
398
- 'VzdG9uLm1jaS5uZXQ=' )
399
- r = yield
400
- if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT'
401
- b'FjY2E2ZmZkNmNmMmQ5ZjMy\r \n ' ):
402
- self ._send_tagged (tag , 'OK' , 'CRAM-MD5 successful' )
403
- else :
404
- self ._send_tagged (tag , 'NO' , 'No access' )
405
- client , _ = self ._setup (AuthHandler )
406
- self .assertTrue ('AUTH=CRAM-MD5' in client .capabilities )
407
+ client , _ = self ._setup (AuthHandler_CRAM_MD5 )
408
+ self .assertIn ('AUTH=CRAM-MD5' , client .capabilities )
407
409
ret , _ = client .login_cram_md5 ("tim" , b"tanstaaftanstaaf" )
408
410
self .assertEqual (ret , "OK" )
409
411
410
412
@hashlib_helper .requires_hashdigest ('md5' , openssl = True )
411
413
def test_login_cram_md5_plain_text (self ):
412
- class AuthHandler (SimpleIMAPHandler ):
413
- capabilities = 'LOGINDISABLED AUTH=CRAM-MD5'
414
- def cmd_AUTHENTICATE (self , tag , args ):
415
- self ._send_textline ('+ PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2Uucm'
416
- 'VzdG9uLm1jaS5uZXQ=' )
417
- r = yield
418
- if (r == b'dGltIGYxY2E2YmU0NjRiOWVmYT'
419
- b'FjY2E2ZmZkNmNmMmQ5ZjMy\r \n ' ):
420
- self ._send_tagged (tag , 'OK' , 'CRAM-MD5 successful' )
421
- else :
422
- self ._send_tagged (tag , 'NO' , 'No access' )
423
- client , _ = self ._setup (AuthHandler )
424
- self .assertTrue ('AUTH=CRAM-MD5' in client .capabilities )
414
+ client , _ = self ._setup (AuthHandler_CRAM_MD5 )
415
+ self .assertIn ('AUTH=CRAM-MD5' , client .capabilities )
425
416
ret , _ = client .login_cram_md5 ("tim" , "tanstaaftanstaaf" )
426
417
self .assertEqual (ret , "OK" )
427
418
419
+ def test_login_cram_md5_blocked (self ):
420
+ def side_effect (* a , ** kw ):
421
+ raise ValueError
422
+
423
+ client , _ = self ._setup (AuthHandler_CRAM_MD5 )
424
+ self .assertIn ('AUTH=CRAM-MD5' , client .capabilities )
425
+ msg = re .escape ("CRAM-MD5 authentication is not supported" )
426
+ with (
427
+ mock .patch ("hmac.HMAC" , side_effect = side_effect ),
428
+ self .assertRaisesRegex (imaplib .IMAP4 .error , msg )
429
+ ):
430
+ client .login_cram_md5 ("tim" , b"tanstaaftanstaaf" )
431
+
428
432
def test_aborted_authentication (self ):
429
433
class MyServer (SimpleIMAPHandler ):
430
434
def cmd_AUTHENTICATE (self , tag , args ):
0 commit comments