Serhiy Storchaka | eab3ff7 | 2017-10-24 19:36:17 +0300 | [diff] [blame] | 1 | import sys |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2 | import unittest |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 3 | |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 4 | |
Miss Islington (bot) | e7ec9e0 | 2019-08-13 14:52:20 -0700 | [diff] [blame] | 5 | try: |
| 6 | import crypt |
| 7 | IMPORT_ERROR = None |
| 8 | except ImportError as ex: |
| 9 | crypt = None |
| 10 | IMPORT_ERROR = str(ex) |
| 11 | |
| 12 | |
| 13 | @unittest.skipIf(crypt, 'This should only run on windows') |
| 14 | class TestWhyCryptDidNotImport(unittest.TestCase): |
| 15 | def test_failure_only_for_windows(self): |
| 16 | self.assertEqual(sys.platform, 'win32') |
| 17 | |
| 18 | def test_import_failure_message(self): |
| 19 | self.assertIn('not supported', IMPORT_ERROR) |
| 20 | |
| 21 | |
| 22 | @unittest.skipUnless(crypt, 'Not supported on Windows') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 23 | class CryptTestCase(unittest.TestCase): |
| 24 | |
| 25 | def test_crypt(self): |
Serhiy Storchaka | eab3ff7 | 2017-10-24 19:36:17 +0300 | [diff] [blame] | 26 | cr = crypt.crypt('mypassword') |
| 27 | cr2 = crypt.crypt('mypassword', cr) |
| 28 | self.assertEqual(cr2, cr) |
| 29 | cr = crypt.crypt('mypassword', 'ab') |
| 30 | if cr is not None: |
| 31 | cr2 = crypt.crypt('mypassword', cr) |
| 32 | self.assertEqual(cr2, cr) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 33 | |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 34 | def test_salt(self): |
Brett Cannon | daa5799 | 2011-02-22 21:48:06 +0000 | [diff] [blame] | 35 | self.assertEqual(len(crypt._saltchars), 64) |
| 36 | for method in crypt.methods: |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 37 | salt = crypt.mksalt(method) |
Serhiy Storchaka | eab3ff7 | 2017-10-24 19:36:17 +0300 | [diff] [blame] | 38 | self.assertIn(len(salt) - method.salt_chars, {0, 1, 3, 4, 6, 7}) |
| 39 | if method.ident: |
| 40 | self.assertIn(method.ident, salt[:len(salt)-method.salt_chars]) |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 41 | |
| 42 | def test_saltedcrypt(self): |
Brett Cannon | daa5799 | 2011-02-22 21:48:06 +0000 | [diff] [blame] | 43 | for method in crypt.methods: |
Serhiy Storchaka | eab3ff7 | 2017-10-24 19:36:17 +0300 | [diff] [blame] | 44 | cr = crypt.crypt('assword', method) |
| 45 | self.assertEqual(len(cr), method.total_size) |
| 46 | cr2 = crypt.crypt('assword', cr) |
| 47 | self.assertEqual(cr2, cr) |
| 48 | cr = crypt.crypt('assword', crypt.mksalt(method)) |
| 49 | self.assertEqual(len(cr), method.total_size) |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 50 | |
| 51 | def test_methods(self): |
Brett Cannon | daa5799 | 2011-02-22 21:48:06 +0000 | [diff] [blame] | 52 | self.assertTrue(len(crypt.methods) >= 1) |
Serhiy Storchaka | eab3ff7 | 2017-10-24 19:36:17 +0300 | [diff] [blame] | 53 | if sys.platform.startswith('openbsd'): |
| 54 | self.assertEqual(crypt.methods, [crypt.METHOD_BLOWFISH]) |
| 55 | else: |
| 56 | self.assertEqual(crypt.methods[-1], crypt.METHOD_CRYPT) |
| 57 | |
Miss Islington (bot) | e7ec9e0 | 2019-08-13 14:52:20 -0700 | [diff] [blame] | 58 | @unittest.skipUnless( |
| 59 | crypt |
| 60 | and ( |
| 61 | crypt.METHOD_SHA256 in crypt.methods or crypt.METHOD_SHA512 in crypt.methods |
| 62 | ), |
| 63 | 'requires support of SHA-2', |
| 64 | ) |
Serhiy Storchaka | cede8c9 | 2017-11-16 13:22:51 +0200 | [diff] [blame] | 65 | def test_sha2_rounds(self): |
| 66 | for method in (crypt.METHOD_SHA256, crypt.METHOD_SHA512): |
| 67 | for rounds in 1000, 10_000, 100_000: |
| 68 | salt = crypt.mksalt(method, rounds=rounds) |
| 69 | self.assertIn('$rounds=%d$' % rounds, salt) |
| 70 | self.assertEqual(len(salt) - method.salt_chars, |
| 71 | 11 + len(str(rounds))) |
| 72 | cr = crypt.crypt('mypassword', salt) |
| 73 | self.assertTrue(cr) |
| 74 | cr2 = crypt.crypt('mypassword', cr) |
| 75 | self.assertEqual(cr2, cr) |
| 76 | |
Miss Islington (bot) | e7ec9e0 | 2019-08-13 14:52:20 -0700 | [diff] [blame] | 77 | @unittest.skipUnless( |
| 78 | crypt and crypt.METHOD_BLOWFISH in crypt.methods, 'requires support of Blowfish' |
| 79 | ) |
Serhiy Storchaka | cede8c9 | 2017-11-16 13:22:51 +0200 | [diff] [blame] | 80 | def test_blowfish_rounds(self): |
Serhiy Storchaka | eab3ff7 | 2017-10-24 19:36:17 +0300 | [diff] [blame] | 81 | for log_rounds in range(4, 11): |
Serhiy Storchaka | cede8c9 | 2017-11-16 13:22:51 +0200 | [diff] [blame] | 82 | salt = crypt.mksalt(crypt.METHOD_BLOWFISH, rounds=1 << log_rounds) |
Serhiy Storchaka | eab3ff7 | 2017-10-24 19:36:17 +0300 | [diff] [blame] | 83 | self.assertIn('$%02d$' % log_rounds, salt) |
| 84 | self.assertIn(len(salt) - crypt.METHOD_BLOWFISH.salt_chars, {6, 7}) |
| 85 | cr = crypt.crypt('mypassword', salt) |
| 86 | self.assertTrue(cr) |
| 87 | cr2 = crypt.crypt('mypassword', cr) |
| 88 | self.assertEqual(cr2, cr) |
| 89 | |
Serhiy Storchaka | cede8c9 | 2017-11-16 13:22:51 +0200 | [diff] [blame] | 90 | def test_invalid_rounds(self): |
| 91 | for method in (crypt.METHOD_SHA256, crypt.METHOD_SHA512, |
| 92 | crypt.METHOD_BLOWFISH): |
| 93 | with self.assertRaises(TypeError): |
| 94 | crypt.mksalt(method, rounds='4096') |
| 95 | with self.assertRaises(TypeError): |
| 96 | crypt.mksalt(method, rounds=4096.0) |
| 97 | for rounds in (0, 1, -1, 1<<999): |
| 98 | with self.assertRaises(ValueError): |
| 99 | crypt.mksalt(method, rounds=rounds) |
| 100 | with self.assertRaises(ValueError): |
| 101 | crypt.mksalt(crypt.METHOD_BLOWFISH, rounds=1000) |
| 102 | for method in (crypt.METHOD_CRYPT, crypt.METHOD_MD5): |
| 103 | with self.assertRaisesRegex(ValueError, 'support'): |
| 104 | crypt.mksalt(method, rounds=4096) |
Serhiy Storchaka | eab3ff7 | 2017-10-24 19:36:17 +0300 | [diff] [blame] | 105 | |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 106 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 107 | if __name__ == "__main__": |
Ezio Melotti | 90bbbd1 | 2013-01-11 05:18:45 +0200 | [diff] [blame] | 108 | unittest.main() |