@@ -39,31 +39,48 @@ def test_methods(self):
3939 else :
4040 self .assertEqual (crypt .methods [- 1 ], crypt .METHOD_CRYPT )
4141
42+ @unittest .skipUnless (crypt .METHOD_SHA256 in crypt .methods or
43+ crypt .METHOD_SHA512 in crypt .methods ,
44+ 'requires support of SHA-2' )
45+ def test_sha2_rounds (self ):
46+ for method in (crypt .METHOD_SHA256 , crypt .METHOD_SHA512 ):
47+ for rounds in 1000 , 10_000 , 100_000 :
48+ salt = crypt .mksalt (method , rounds = rounds )
49+ self .assertIn ('$rounds=%d$' % rounds , salt )
50+ self .assertEqual (len (salt ) - method .salt_chars ,
51+ 11 + len (str (rounds )))
52+ cr = crypt .crypt ('mypassword' , salt )
53+ self .assertTrue (cr )
54+ cr2 = crypt .crypt ('mypassword' , cr )
55+ self .assertEqual (cr2 , cr )
56+
4257 @unittest .skipUnless (crypt .METHOD_BLOWFISH in crypt .methods ,
4358 'requires support of Blowfish' )
44- def test_log_rounds (self ):
45- self .assertEqual (len (crypt ._saltchars ), 64 )
59+ def test_blowfish_rounds (self ):
4660 for log_rounds in range (4 , 11 ):
47- salt = crypt .mksalt (crypt .METHOD_BLOWFISH , log_rounds = log_rounds )
61+ salt = crypt .mksalt (crypt .METHOD_BLOWFISH , rounds = 1 << log_rounds )
4862 self .assertIn ('$%02d$' % log_rounds , salt )
4963 self .assertIn (len (salt ) - crypt .METHOD_BLOWFISH .salt_chars , {6 , 7 })
5064 cr = crypt .crypt ('mypassword' , salt )
5165 self .assertTrue (cr )
5266 cr2 = crypt .crypt ('mypassword' , cr )
5367 self .assertEqual (cr2 , cr )
5468
55- @unittest .skipUnless (crypt .METHOD_BLOWFISH in crypt .methods ,
56- 'requires support of Blowfish' )
57- def test_invalid_log_rounds (self ):
58- for log_rounds in (1 , - 1 , 999 ):
59- salt = crypt .mksalt (crypt .METHOD_BLOWFISH , log_rounds = log_rounds )
60- cr = crypt .crypt ('mypassword' , salt )
61- if cr is not None :
62- # On failure the openwall implementation returns a magic
63- # string that is shorter than 13 characters and is guaranteed
64- # to differ from a salt.
65- self .assertNotEqual (cr , salt )
66- self .assertLess (len (cr ), 13 )
69+ def test_invalid_rounds (self ):
70+ for method in (crypt .METHOD_SHA256 , crypt .METHOD_SHA512 ,
71+ crypt .METHOD_BLOWFISH ):
72+ with self .assertRaises (TypeError ):
73+ crypt .mksalt (method , rounds = '4096' )
74+ with self .assertRaises (TypeError ):
75+ crypt .mksalt (method , rounds = 4096.0 )
76+ for rounds in (0 , 1 , - 1 , 1 << 999 ):
77+ with self .assertRaises (ValueError ):
78+ crypt .mksalt (method , rounds = rounds )
79+ with self .assertRaises (ValueError ):
80+ crypt .mksalt (crypt .METHOD_BLOWFISH , rounds = 1000 )
81+ for method in (crypt .METHOD_CRYPT , crypt .METHOD_MD5 ):
82+ with self .assertRaisesRegex (ValueError , 'support' ):
83+ crypt .mksalt (method , rounds = 4096 )
6784
6885
6986if __name__ == "__main__" :
0 commit comments