mail4one/tests/test_pwhash.py

41 lines
1.3 KiB
Python
Raw Permalink Normal View History

2023-06-13 21:12:46 -04:00
from mail4one.pwhash import gen_pwhash, parse_hash, check_pass, SALT_LEN, KEY_LEN
import unittest
2023-05-15 22:41:21 -04:00
class TestPWHash(unittest.TestCase):
def test_expected_usage(self):
password = "Blah Blah ABCD"
pwhash = gen_pwhash(password)
2023-05-15 22:41:21 -04:00
pwinfo = parse_hash(pwhash)
self.assertEqual(len(pwinfo.salt), SALT_LEN)
self.assertEqual(len(pwinfo.scrypt_hash), KEY_LEN)
2024-04-01 11:14:20 -04:00
self.assertTrue(
check_pass(password, pwinfo), "check pass with correct password"
)
self.assertFalse(check_pass("foobar", pwinfo), "check pass with wrong password")
2023-05-15 22:41:21 -04:00
def test_hardcoded_hash(self):
2024-04-01 11:14:20 -04:00
test_hash = "".join(
c
for c in """
AFTY5EVN7AX47ZL7UMH3BETYWFBTAV3XHR73CEFAJBPN2NIHPWD
ZHV2UQSMSPHSQQ2A2BFQBNC77VL7F2UKATQNJZGYLCSU6C43UQD
AQXWXSWNGAEPGIMG2F3QDKBXL3MRHY6K2BPID64ZR6LABLPVSF
2024-04-01 11:14:20 -04:00
"""
if not c.isspace()
)
pwinfo = parse_hash(test_hash)
2024-04-01 11:14:20 -04:00
self.assertTrue(
check_pass("helloworld", pwinfo), "check pass with correct password"
)
self.assertFalse(check_pass("foobar", pwinfo), "check pass with wrong password")
2023-05-15 22:41:21 -04:00
def test_invalid_hash(self):
with self.assertRaises(Exception):
parse_hash("sdlfkjdsklfjdsk")
2023-05-15 22:41:21 -04:00
2024-04-01 11:14:20 -04:00
if __name__ == "__main__":
unittest.main()