Skip to content

Commit 62ae485

Browse files
committed
Begin adding more test coverage and clean up some copy/pasted pickle unit test
leftovers.
1 parent 4a7f352 commit 62ae485

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

tests/test_mig_shared_pwcrypto.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,62 @@
2525
# --- END_HEADER ---
2626
#
2727

28-
"""Unit test pwcrypto functions"""
28+
"""Unit tests for the migrid module pointed to in the filename"""
2929

3030
import os
3131
import sys
3232

33-
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
34-
35-
from support import MigTestCase, temppath, testmain
33+
from tests.support import MigTestCase, FakeConfiguration, \
34+
cleanpath, temppath, testmain
3635

36+
from mig.shared.defaults import POLICY_NONE, POLICY_WEAK, POLICY_MEDIUM, \
37+
POLICY_HIGH, POLICY_MODERN, POLICY_CUSTOM, PASSWORD_POLICIES
3738
from mig.shared.pwcrypto import *
3839

39-
class MigSharedPwcrypto_make_hash(MigTestCase):
40-
def test_pickle_string(self):
41-
expected = "PBKDF2$sha256$10000$MDAwMDAwMDAwMDAw$epib2rEg/HYTQZFnCp7hmIGZ6rzHnViy"
4240

43-
actual = make_hash('foobar', _urandom=lambda vlen: b'0' * vlen)
41+
DUMMY_USER = "dummy-user"
42+
DUMMY_ID = "dummy-id"
43+
DUMMY_PW = 'foobar'
44+
DUMMY_PW_HASH = \
45+
"PBKDF2$sha256$10000$MDAwMDAwMDAwMDAw$epib2rEg/HYTQZFnCp7hmIGZ6rzHnViy"
46+
DUMMY_HOME_DIR = 'dummy_user_home'
47+
DUMMY_SETTINGS_DIR = 'dummy_user_settings'
48+
DUMMY_SERVICE = 'svc'
49+
50+
51+
class MigSharedPwCrypto(MigTestCase):
52+
"""Wrap unit tests for the corresponding module"""
53+
54+
def before_each(self):
55+
test_user_home = temppath(DUMMY_HOME_DIR, self, ensure_dir=True)
56+
test_user_settings = cleanpath(
57+
DUMMY_SETTINGS_DIR, self, ensure_dir=True)
58+
# make two requisite root folders for the dummy user
59+
os.mkdir(os.path.join(test_user_home, DUMMY_USER))
60+
os.mkdir(os.path.join(test_user_settings, DUMMY_USER))
61+
# now create a configuration
62+
self.dummy_conf = FakeConfiguration(
63+
user_home=test_user_home, user_settings=test_user_settings,
64+
site_password_policy=POLICY_HIGH,
65+
site_password_legacy_policy=POLICY_MEDIUM,
66+
site_password_cracklib=False)
67+
68+
def test_make_hash_constant_string(self):
69+
"""Test basic hashing of a fixed string to be constant for a fixed
70+
random seed.
71+
"""
72+
actual = make_hash(DUMMY_PW, _urandom=lambda vlen: b'0' * vlen)
73+
self.assertEqual(actual, DUMMY_PW_HASH, "mismatch hashing string")
74+
75+
def test_check_hash(self):
76+
"""Test basic hash checking of a fixed string"""
77+
78+
random_pw = generate_random_password(self.dummy_conf)
79+
expected = make_hash(random_pw)
80+
result = check_hash(self.dummy_conf, DUMMY_SERVICE, DUMMY_USER,
81+
random_pw, expected)
4482

45-
self.assertEqual(actual, expected, "mismatch pickling string")
83+
self.assertTrue(result, "mismatch in hash check")
4684

4785

4886
if __name__ == '__main__':

0 commit comments

Comments
 (0)