blob: e4f58979c13567276a4f7da23237ab1b8e5e42ce [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test import support
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002import unittest
R. David Murraya21e4ca2009-03-31 23:16:50 +00003
Ezio Melottifbd26862013-03-01 14:53:45 +02004crypt = support.import_module('crypt')
Roger E. Massefab8ab81996-12-20 22:36:52 +00005
Christian Heimesdd15f6c2008-03-16 00:07:10 +00006class CryptTestCase(unittest.TestCase):
7
8 def test_crypt(self):
9 c = crypt.crypt('mypassword', 'ab')
Benjamin Petersonee8712c2008-05-20 21:35:26 +000010 if support.verbose:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000011 print('Test encryption: ', c)
12
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000013 def test_salt(self):
Brett Cannondaa57992011-02-22 21:48:06 +000014 self.assertEqual(len(crypt._saltchars), 64)
15 for method in crypt.methods:
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000016 salt = crypt.mksalt(method)
17 self.assertEqual(len(salt),
18 method.salt_chars + (3 if method.ident else 0))
19
20 def test_saltedcrypt(self):
Brett Cannondaa57992011-02-22 21:48:06 +000021 for method in crypt.methods:
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000022 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)
26
27 def test_methods(self):
Martin Panter46f50722016-05-26 05:35:26 +000028 # Guarantee that METHOD_CRYPT is the last method in crypt.methods.
Brett Cannondaa57992011-02-22 21:48:06 +000029 self.assertTrue(len(crypt.methods) >= 1)
30 self.assertEqual(crypt.METHOD_CRYPT, crypt.methods[-1])
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000031
Christian Heimesdd15f6c2008-03-16 00:07:10 +000032if __name__ == "__main__":
Ezio Melotti90bbbd12013-01-11 05:18:45 +020033 unittest.main()