1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
"""tests for passlib.pwhash -- (c) Assurance Technologies 2003-2009"""
#=========================================================
#imports
#=========================================================
from __future__ import with_statement
#core
import hashlib
from logging import getLogger
#site
#pkg
import passlib as mod
from passlib.tests.utils import TestCase
#module
log = getLogger(__name__)
#=========================================================
#pull crypt tests
#=========================================================
def get_crypt_cases():
#this test suite uses info stored in the specific hash algs' test suites,
#so we have to import them here.
from passlib.tests.test_drivers import Sha256CryptTest, Sha512CryptTest, DesCryptTest, BCryptTest, Md5CryptTest
crypt_cases = [ DesCryptTest, Md5CryptTest, Sha256CryptTest]
if BCryptTest:
crypt_cases.append(BCryptTest)
crypt_cases.extend([ Sha512CryptTest ])
return crypt_cases
#=========================================================
#quick access functions
#=========================================================
class QuickAccessTest(TestCase):
"test quick access functions"
crypt_cases = get_crypt_cases()
def test_00_identify(self):
"test pwhash.identify()"
identify = mod.identify
for cc in self.crypt_cases:
name = cc.handler.name
for _, hash in cc.known_correct:
self.assertEqual(identify(hash), name)
for _, hash in cc.known_incorrect:
self.assertEqual(identify(hash), name)
for other, hash in cc.known_other:
if other == name:
self.assertEqual(identify(hash), name)
else:
self.assertNotEqual(identify(hash), name)
for hash in cc.known_invalid:
self.assertEqual(identify(hash), None)
def test_01_verify(self):
"test pwhash.verify()"
verify = mod.verify
for cc in self.crypt_cases:
name = cc.handler.name
for secret, hash in cc.known_correct[:3]:
self.assert_(verify(secret, hash))
self.assert_(verify(secret, hash, alg=name))
for secret, hash in cc.known_incorrect[:3]:
self.assert_(not verify(secret, hash))
self.assert_(not verify(secret, hash, alg=name))
for hash in cc.known_invalid[:3]:
#context should raise ValueError because can't be identified
self.assertRaises(ValueError, verify, secret, hash)
def test_02_encrypt(self):
"test pwhash.encrypt()"
identify = mod.identify
verify = mod.verify
encrypt = mod.encrypt
for cc in self.crypt_cases:
handler = cc.handler
name = handler.name
s = 'test'
h = encrypt(s, alg=name)
self.assertEqual(identify(h), name)
self.assertEqual(verify(s, h), True)
if hasattr(handler, "parse"):
info = handler.parse(h)
del info['checksum']
h2 = encrypt(s, alg=name, **info)
self.assertEqual(identify(h2), name, "failed to identify %r rehash %r of hash %r from secret %r:" % (name, h2, h, s))
self.assertEqual(verify(s, h2, alg=name), True)
def test_04_default_context(self):
"test pwhash.default_context contents"
dc = mod.default_context
for case in self.crypt_cases:
self.assert_(dc.lookup(case.handler.name) is case.handler)
last = 'sha512-crypt'
self.assertEqual(dc.lookup().name, last)
h = dc.encrypt("test")
self.assertEqual(dc.identify(h).name, last)
self.assertEqual(dc.verify('test', h, alg=last), True)
#=========================================================
#EOF
#=========================================================
|