Skip to content

Commit f52dff6

Browse files
bpo-25287: Backport new tests for crypt and skip test_crypt on OpenBSD. (#4111)
1 parent d5d7954 commit f52dff6

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

Lib/test/test_crypt.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
1+
import sys
12
from test import support
23
import unittest
34

45
crypt = support.import_module('crypt')
56

7+
if sys.platform.startswith('openbsd'):
8+
raise unittest.SkipTest('The only supported method on OpenBSD is Blowfish')
9+
610
class CryptTestCase(unittest.TestCase):
711

812
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)
1220

1321
def test_salt(self):
1422
self.assertEqual(len(crypt._saltchars), 64)
1523
for method in crypt.methods:
1624
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])
1928

2029
def test_saltedcrypt(self):
2130
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)
2637

2738
def test_methods(self):
2839
# Guarantee that METHOD_CRYPT is the last method in crypt.methods.
2940
self.assertTrue(len(crypt.methods) >= 1)
3041
self.assertEqual(crypt.METHOD_CRYPT, crypt.methods[-1])
3142

43+
3244
if __name__ == "__main__":
3345
unittest.main()

0 commit comments

Comments
 (0)