|
| 1 | +import sys |
1 | 2 | from test import support
|
2 | 3 | import unittest
|
3 | 4 |
|
4 | 5 | crypt = support.import_module('crypt')
|
5 | 6 |
|
| 7 | +if sys.platform.startswith('openbsd'): |
| 8 | + raise unittest.SkipTest('The only supported method on OpenBSD is Blowfish') |
| 9 | + |
6 | 10 | class CryptTestCase(unittest.TestCase):
|
7 | 11 |
|
8 | 12 | def test_crypt(self):
|
9 |
| - c = crypt.crypt('mypassword', 'ab') |
10 |
| - if support.verbose: |
11 |
| - print('Test encryption: ', c) |
| 13 | + cr = crypt.crypt('mypassword') |
| 14 | + cr2 = crypt.crypt('mypassword', cr) |
| 15 | + self.assertEqual(cr2, cr) |
| 16 | + cr = crypt.crypt('mypassword', 'ab') |
| 17 | + if cr is not None: |
| 18 | + cr2 = crypt.crypt('mypassword', cr) |
| 19 | + self.assertEqual(cr2, cr) |
12 | 20 |
|
13 | 21 | def test_salt(self):
|
14 | 22 | self.assertEqual(len(crypt._saltchars), 64)
|
15 | 23 | for method in crypt.methods:
|
16 | 24 | salt = crypt.mksalt(method)
|
17 |
| - self.assertEqual(len(salt), |
18 |
| - method.salt_chars + (3 if method.ident else 0)) |
| 25 | + self.assertIn(len(salt) - method.salt_chars, {0, 1, 3, 4, 6, 7}) |
| 26 | + if method.ident: |
| 27 | + self.assertIn(method.ident, salt[:len(salt)-method.salt_chars]) |
19 | 28 |
|
20 | 29 | def test_saltedcrypt(self):
|
21 | 30 | for method in crypt.methods:
|
22 |
| - pw = crypt.crypt('assword', method) |
23 |
| - self.assertEqual(len(pw), method.total_size) |
24 |
| - pw = crypt.crypt('assword', crypt.mksalt(method)) |
25 |
| - self.assertEqual(len(pw), method.total_size) |
| 31 | + cr = crypt.crypt('assword', method) |
| 32 | + self.assertEqual(len(cr), method.total_size) |
| 33 | + cr2 = crypt.crypt('assword', cr) |
| 34 | + self.assertEqual(cr2, cr) |
| 35 | + cr = crypt.crypt('assword', crypt.mksalt(method)) |
| 36 | + self.assertEqual(len(cr), method.total_size) |
26 | 37 |
|
27 | 38 | def test_methods(self):
|
28 | 39 | # Guarantee that METHOD_CRYPT is the last method in crypt.methods.
|
29 | 40 | self.assertTrue(len(crypt.methods) >= 1)
|
30 | 41 | self.assertEqual(crypt.METHOD_CRYPT, crypt.methods[-1])
|
31 | 42 |
|
| 43 | + |
32 | 44 | if __name__ == "__main__":
|
33 | 45 | unittest.main()
|
0 commit comments