Skip to content

Commit a37ea11

Browse files
authored
Improved coverage and fixed string checks for basic functionality of several of the pwcrypto helpers (#362)
* Add fixed string checks for basic functionality of several of the pwcrypto helper functions as suggested by @albu-diku in the comments to PR343. * Adjust `pwcrypto` slightly to support external use of the existing self-tests, like it's the case e.g. for `userio`, and integrate running it in unit testing. Added a lot of unit tests to cover almost all functions in `pwcrypto` and to include more tests of fundamental functionality against fixed known values as suggested in PR343 discussion. A few tests will require API changes to pass a random seed to allow that and have been left as disabled for now with TODO comments added. * Workaround for pylint failing CI with bogus missing member on rocky9 and latest stable py3. I can't see why `site_password_legacy_policy` is any different from e.g. `site_password_policy`, which does NOT trigger the same pylint error :-s * Make sure the AAD date stamp helper keeps the date input static over time to prevent tests failing after a while.
1 parent d18dbc0 commit a37ea11

File tree

2 files changed

+399
-45
lines changed

2 files changed

+399
-45
lines changed

mig/shared/pwcrypto.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import datetime
4141
import hashlib
4242
import time
43+
import sys
4344

4445
from mig.shared.base import force_utf8, force_native_str, mask_creds, string_snippet
4546
from mig.shared.defaults import keyword_auto, RESET_TOKEN_TTL
@@ -1027,7 +1028,8 @@ def generate_random_password(configuration, tries=42):
10271028
raise ValueError("Failed to generate suitable password!")
10281029

10291030

1030-
if __name__ == "__main__":
1031+
def main(_exit=sys.exit, _print=print):
1032+
"""Run module self-tests"""
10311033
from mig.shared.conf import get_configuration_object
10321034
configuration = get_configuration_object()
10331035
dummy_user = {'distinguished_name': 'Test User', 'password_hash': ''}
@@ -1057,6 +1059,8 @@ def generate_random_password(configuration, tries=42):
10571059
hashed = make_hash(pw)
10581060
snippet = string_snippet(hashed)
10591061
dummy_user['password_hash'] = hashed
1062+
if 'migoid' not in configuration.site_login_methods:
1063+
configuration.site_login_methods.append('migoid')
10601064
token = generate_reset_token(configuration, dummy_user, 'migoid')
10611065
print("Password %r gives hash %r, snippet %r and reset token %r" %
10621066
(pw, hashed, snippet, token))
@@ -1112,3 +1116,7 @@ def generate_random_password(configuration, tries=42):
11121116
except Exception as exc:
11131117
print(
11141118
"Failed to handle aesgcm static encrypt/decrypt %s : %s" % (pw, exc))
1119+
1120+
1121+
if __name__ == "__main__":
1122+
main()

0 commit comments

Comments
 (0)