Debian Codegraph @

tests.py

raw
1 import sys
2 import unittest
3 import legacycrypt as crypt
4
5 class CryptTestCase(unittest.TestCase):
6
7 def test_types(self):
8 self.assertIs(crypt.crypt('', ''), None)
9 with self.assertRaises(TypeError):
10 crypt.crypt(b'', '')
11 with self.assertRaises(TypeError):
12 crypt.crypt('', b'')
13 self.assertIsInstance(crypt.crypt('mypassword'), str)
14
15 def test_crypt(self):
16 cr = crypt.crypt('mypassword')
17 cr2 = crypt.crypt('mypassword', cr)
18 self.assertEqual(cr2, cr)
19 cr = crypt.crypt('mypassword', 'ab')
20 if cr is not None:
21 cr2 = crypt.crypt('mypassword', cr)
22 self.assertEqual(cr2, cr)
23
24 def test_salt(self):
25 self.assertEqual(len(crypt._saltchars), 64)
26 for method in crypt.methods:
27 salt = crypt.mksalt(method)
28 self.assertIn(len(salt) - method.salt_chars, {0, 1, 3, 4, 6, 7})
29 if method.ident:
30 self.assertIn(method.ident, salt[:len(salt)-method.salt_chars])
31
32 def test_saltedcrypt(self):
33 for method in crypt.methods:
34 cr = crypt.crypt('assword', method)
35 self.assertEqual(len(cr), method.total_size)
36 cr2 = crypt.crypt('assword', cr)
37 self.assertEqual(cr2, cr)
38 cr = crypt.crypt('assword', crypt.mksalt(method))
39 self.assertEqual(len(cr), method.total_size)
40
41 def test_methods(self):
42 self.assertTrue(len(crypt.methods) >= 1)
43 if sys.platform.startswith('openbsd'):
44 self.assertEqual(crypt.methods, [crypt.METHOD_BLOWFISH])
45 else:
46 self.assertEqual(crypt.methods[-1], crypt.METHOD_CRYPT)
47
48 @unittest.skipUnless(crypt.METHOD_SHA256 in crypt.methods or
49 crypt.METHOD_SHA512 in crypt.methods,
50 'requires support of SHA-2')
51 def test_sha2_rounds(self):
52 for method in (crypt.METHOD_SHA256, crypt.METHOD_SHA512):
53 for rounds in 1000, 10000, 100000:
54 salt = crypt.mksalt(method, rounds=rounds)
55 self.assertIn('$rounds=%d$' % rounds, salt)
56 self.assertEqual(len(salt) - method.salt_chars,
57 11 + len(str(rounds)))
58 cr = crypt.crypt('mypassword', salt)
59 self.assertTrue(cr)
60 cr2 = crypt.crypt('mypassword', cr)
61 self.assertEqual(cr2, cr)
62
63 @unittest.skipUnless(crypt.METHOD_BLOWFISH in crypt.methods,
64 'requires support of Blowfish')
65 def test_blowfish_rounds(self):
66 for log_rounds in range(4, 11):
67 salt = crypt.mksalt(crypt.METHOD_BLOWFISH, rounds=1 << log_rounds)
68 self.assertIn('$%02d$' % log_rounds, salt)
69 self.assertIn(len(salt) - crypt.METHOD_BLOWFISH.salt_chars, {6, 7})
70 cr = crypt.crypt('mypassword', salt)
71 self.assertTrue(cr)
72 cr2 = crypt.crypt('mypassword', cr)
73 self.assertEqual(cr2, cr)
74
75 def test_invalid_rounds(self):
76 for method in (crypt.METHOD_SHA256, crypt.METHOD_SHA512,
77 crypt.METHOD_BLOWFISH):
78 with self.assertRaises(TypeError):
79 crypt.mksalt(method, rounds='4096')
80 with self.assertRaises(TypeError):
81 crypt.mksalt(method, rounds=4096.0)
82 for rounds in (0, 1, -1, 1<<999):
83 with self.assertRaises(ValueError):
84 crypt.mksalt(method, rounds=rounds)
85 with self.assertRaises(ValueError):
86 crypt.mksalt(crypt.METHOD_BLOWFISH, rounds=1000)
87 for method in (crypt.METHOD_CRYPT, crypt.METHOD_MD5):
88 with self.assertRaisesRegex(ValueError, 'support'):
89 crypt.mksalt(method, rounds=4096)
90
91
92 if __name__ == "__main__":
93 unittest.main()