|
25 | 25 | # --- END_HEADER --- |
26 | 26 | # |
27 | 27 |
|
28 | | -"""Unit test pwcrypto functions""" |
| 28 | +"""Unit tests for the migrid module pointed to in the filename""" |
29 | 29 |
|
30 | 30 | import os |
31 | 31 | import sys |
32 | 32 |
|
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 |
36 | 35 |
|
| 36 | +from mig.shared.defaults import POLICY_NONE, POLICY_WEAK, POLICY_MEDIUM, \ |
| 37 | + POLICY_HIGH, POLICY_MODERN, POLICY_CUSTOM, PASSWORD_POLICIES |
37 | 38 | from mig.shared.pwcrypto import * |
38 | 39 |
|
39 | | -class MigSharedPwcrypto_make_hash(MigTestCase): |
40 | | - def test_pickle_string(self): |
41 | | - expected = "PBKDF2$sha256$10000$MDAwMDAwMDAwMDAw$epib2rEg/HYTQZFnCp7hmIGZ6rzHnViy" |
42 | 40 |
|
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) |
44 | 82 |
|
45 | | - self.assertEqual(actual, expected, "mismatch pickling string") |
| 83 | + self.assertTrue(result, "mismatch in hash check") |
46 | 84 |
|
47 | 85 |
|
48 | 86 | if __name__ == '__main__': |
|
0 commit comments