Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
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 | |
Ezio Melotti | 90bbbd1 | 2013-01-11 05:18:45 +0200 | [diff] [blame] | 4 | def setUpModule(): |
| 5 | # this import will raise unittest.SkipTest if _crypt doesn't exist, |
| 6 | # so it has to be done in setUpModule for test discovery to work |
| 7 | global crypt |
| 8 | crypt = support.import_module('crypt') |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 9 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 10 | class CryptTestCase(unittest.TestCase): |
| 11 | |
| 12 | def test_crypt(self): |
| 13 | c = crypt.crypt('mypassword', 'ab') |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 14 | if support.verbose: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 15 | print('Test encryption: ', c) |
| 16 | |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 17 | def test_salt(self): |
Brett Cannon | daa5799 | 2011-02-22 21:48:06 +0000 | [diff] [blame] | 18 | self.assertEqual(len(crypt._saltchars), 64) |
| 19 | for method in crypt.methods: |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 20 | salt = crypt.mksalt(method) |
| 21 | self.assertEqual(len(salt), |
| 22 | method.salt_chars + (3 if method.ident else 0)) |
| 23 | |
| 24 | def test_saltedcrypt(self): |
Brett Cannon | daa5799 | 2011-02-22 21:48:06 +0000 | [diff] [blame] | 25 | for method in crypt.methods: |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 26 | pw = crypt.crypt('assword', method) |
| 27 | self.assertEqual(len(pw), method.total_size) |
| 28 | pw = crypt.crypt('assword', crypt.mksalt(method)) |
| 29 | self.assertEqual(len(pw), method.total_size) |
| 30 | |
| 31 | def test_methods(self): |
Brett Cannon | daa5799 | 2011-02-22 21:48:06 +0000 | [diff] [blame] | 32 | # Gurantee that METHOD_CRYPT is the last method in crypt.methods. |
| 33 | self.assertTrue(len(crypt.methods) >= 1) |
| 34 | self.assertEqual(crypt.METHOD_CRYPT, crypt.methods[-1]) |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 35 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 36 | if __name__ == "__main__": |
Ezio Melotti | 90bbbd1 | 2013-01-11 05:18:45 +0200 | [diff] [blame] | 37 | unittest.main() |