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