blob: cfb7341e20a9b15644b617b23f80f893e1f707a0 [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 Melotti90bbbd12013-01-11 05:18:45 +02004def 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. Massefab8ab81996-12-20 22:36:52 +00009
Christian Heimesdd15f6c2008-03-16 00:07:10 +000010class CryptTestCase(unittest.TestCase):
11
12 def test_crypt(self):
13 c = crypt.crypt('mypassword', 'ab')
Benjamin Petersonee8712c2008-05-20 21:35:26 +000014 if support.verbose:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000015 print('Test encryption: ', c)
16
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000017 def test_salt(self):
Brett Cannondaa57992011-02-22 21:48:06 +000018 self.assertEqual(len(crypt._saltchars), 64)
19 for method in crypt.methods:
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000020 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 Cannondaa57992011-02-22 21:48:06 +000025 for method in crypt.methods:
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000026 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 Cannondaa57992011-02-22 21:48:06 +000032 # 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 Reifscheidere2dfefb2011-02-22 10:55:44 +000035
Christian Heimesdd15f6c2008-03-16 00:07:10 +000036if __name__ == "__main__":
Ezio Melotti90bbbd12013-01-11 05:18:45 +020037 unittest.main()