@@ -282,13 +282,15 @@ def test_auth_response(self):
282
282
283
283
# verify an error is shown if server response is not well formated.
284
284
with self .assertRaises (InterfaceError ) as context :
285
- auth_plugin .auth_continue ("r=/ZT33fXoR/BZT,s=IApa7ZwqQ/ZT,w54" )
285
+ auth_plugin .auth_continue (
286
+ bytearray ("r=/ZT33fXoR/BZT,s=IApa7ZwqQ/ZT,w54" .encode ()))
286
287
self .assertIn ("Incomplete reponse" , context .exception .msg ,
287
288
"not the expected error {}" .format (context .exception .msg ))
288
289
289
290
# verify an error is shown if server does not authenticate response.
290
291
with self .assertRaises (InterfaceError ) as context :
291
- auth_plugin .auth_continue ("r=/ZT33fXoR/BZT,s=IApa7ZwqQ/ZT,i=40" )
292
+ auth_plugin .auth_continue (
293
+ bytearray ("r=/ZT33fXoR/BZT,s=IApa7ZwqQ/ZT,i=40" .encode ()))
292
294
self .assertIn ("Unable to authenticate resp" , context .exception .msg ,
293
295
"not the expected error {}" .format (context .exception .msg ))
294
296
@@ -306,3 +308,99 @@ def test_auth_response(self):
306
308
bytearray (b"v=5H6b+IApa7ZwqQ/ZT33fXoR/BTM=" ))
307
309
self .assertIn ("Unable to proof server identity" , context .exception .msg ,
308
310
"not the expected error {}" .format (context .exception .msg ))
311
+
312
+ def test_auth_response256 (self ):
313
+ # Test unsupported mechanism error message
314
+ auth_data = b'UNKOWN-METHOD'
315
+ auth_plugin = self .plugin_class (auth_data , username = "user" ,
316
+ password = "spam" )
317
+ with self .assertRaises (InterfaceError ) as context :
318
+ auth_plugin .auth_response ()
319
+ self .assertIn ('sasl authentication method "UNKOWN-METHOD"' ,
320
+ context .exception .msg , "not the expected error {}"
321
+ "" .format (context .exception .msg ))
322
+ self .assertIn ("is not supported" , context .exception .msg ,
323
+ "not the expected error {}" .format (context .exception .msg ))
324
+ with self .assertRaises (NotImplementedError ) as context :
325
+ auth_plugin .prepare_password ()
326
+
327
+ # Test SCRAM-SHA-256 mechanism is accepted
328
+ auth_data = b'SCRAM-SHA-256'
329
+
330
+ auth_plugin = self .plugin_class (auth_data , username = "" , password = "" )
331
+
332
+ # Verify the format of the first message from client.
333
+ exp = b'n,a=,n=,r='
334
+ client_first_nsg = auth_plugin .auth_response ()
335
+ self .assertTrue (client_first_nsg .startswith (exp ),
336
+ "got header: {}" .format (auth_plugin .auth_response ()))
337
+
338
+ auth_plugin = self .plugin_class (auth_data , username = "user" ,
339
+ password = "spam" )
340
+
341
+ # Verify the length of the client's nonce in r=
342
+ cnonce = client_first_nsg [(len (b'n,a=,n=,r=' )):]
343
+ r_len = len (cnonce )
344
+ self .assertEqual (32 , r_len , "Unexpected legth {}" .format (len (cnonce )))
345
+
346
+ # Verify the format of the first message from client.
347
+ exp = b'n,a=user,n=user,r='
348
+ client_first_nsg = auth_plugin .auth_response ()
349
+ self .assertTrue (client_first_nsg .startswith (exp ),
350
+ "got header: {}" .format (auth_plugin .auth_response ()))
351
+
352
+ # Verify the length of the client's nonce in r=
353
+ cnonce = client_first_nsg [(len (exp )):]
354
+ r_len = len (cnonce )
355
+ self .assertEqual (32 , r_len , "Unexpected cnonce legth {}, response {}"
356
+ "" .format (len (cnonce ), client_first_nsg ))
357
+
358
+ # Verify that a user name that requires character mapping is mapped
359
+ auth_plugin = self .plugin_class (auth_data , username = u"u\u1680 ser" ,
360
+ password = "spam" )
361
+ exp = b'n,a=u ser,n=u ser,r='
362
+ client_first_nsg = auth_plugin .auth_response ()
363
+ self .assertTrue (client_first_nsg .startswith (exp ),
364
+ "got header: {}" .format (auth_plugin .auth_response ()))
365
+
366
+ # Verify the length of the client's nonce in r=
367
+ cnonce = client_first_nsg [(len (exp )):]
368
+ r_len = len (cnonce )
369
+ self .assertEqual (32 , r_len , "Unexpected legth {}" .format (len (cnonce )))
370
+
371
+ bad_responses = [None , "" , "v=5H6b+IApa7ZwqQ/ZT33fXoR/BTM=" , b"" , 123 ]
372
+ for bad_res in bad_responses :
373
+ # verify an error is shown if server response is not as expected.
374
+ with self .assertRaises (InterfaceError ) as context :
375
+ auth_plugin .auth_continue (bad_res )
376
+ self .assertIn ("Unexpected server message" , context .exception .msg ,
377
+ "not the expected: {}" .format (context .exception .msg ))
378
+
379
+ # verify an error is shown if server response is not well formated.
380
+ with self .assertRaises (InterfaceError ) as context :
381
+ auth_plugin .auth_continue (
382
+ bytearray (b"r=/ZT33fXoR/BZT,s=IApa7ZwqQ/ZT,w54" ))
383
+ self .assertIn ("Incomplete reponse" , context .exception .msg ,
384
+ "not the expected error {}" .format (context .exception .msg ))
385
+
386
+ # verify an error is shown if server does not authenticate response.
387
+ with self .assertRaises (InterfaceError ) as context :
388
+ auth_plugin .auth_continue (
389
+ bytearray (b"r=/ZT33fXoR/BZT,s=IApa7ZwqQ/ZT,i=40" ))
390
+ self .assertIn ("Unable to authenticate resp" , context .exception .msg ,
391
+ "not the expected error {}" .format (context .exception .msg ))
392
+
393
+ bad_proofs = [None , "" , b"5H6b+IApa7ZwqQ/ZT33fXoR/BTM=" , b"" , 123 ]
394
+ for bad_proof in bad_proofs :
395
+ # verify an error is shown if server proof is not well formated.
396
+ with self .assertRaises (InterfaceError ) as context :
397
+ auth_plugin .auth_finalize (bad_proof )
398
+ self .assertIn ("proof is not well formated." , context .exception .msg ,
399
+ "not the expected: {}" .format (context .exception .msg ))
400
+
401
+ # verify an error is shown it the server can not prove it self.
402
+ with self .assertRaises (InterfaceError ) as context :
403
+ auth_plugin .auth_finalize (
404
+ bytearray (b"v=5H6b+IApa7ZwqQ/ZT33fXoR/BTM=" ))
405
+ self .assertIn ("Unable to proof server identity" , context .exception .msg ,
406
+ "not the expected error {}" .format (context .exception .msg ))
0 commit comments