From e105c1a2b40c66ce7a1d31f11344dc07693da924 Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Tue, 1 Jul 2025 00:13:08 -0400 Subject: [PATCH] Add unit tests for utilities --- src/tests/test_checksum_utils.py | 38 +++++++++++++++++++++ src/tests/test_fingerprint_manager_utils.py | 20 +++++++++++ src/tests/test_password_prompt.py | 34 ++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/tests/test_checksum_utils.py create mode 100644 src/tests/test_fingerprint_manager_utils.py create mode 100644 src/tests/test_password_prompt.py diff --git a/src/tests/test_checksum_utils.py b/src/tests/test_checksum_utils.py new file mode 100644 index 0000000..e30643d --- /dev/null +++ b/src/tests/test_checksum_utils.py @@ -0,0 +1,38 @@ +import hashlib +from pathlib import Path + +from utils import checksum + + +def test_calculate_checksum(tmp_path): + file = tmp_path / "data.txt" + content = "hello world" + file.write_text(content) + expected = hashlib.sha256(content.encode()).hexdigest() + result = checksum.calculate_checksum(str(file)) + assert result == expected + + +def test_calculate_checksum_missing(tmp_path): + missing = tmp_path / "missing.txt" + assert checksum.calculate_checksum(str(missing)) is None + + +def test_verify_and_update(tmp_path): + chk_file = tmp_path / "chk.txt" + chk_file.write_text("abc") + assert checksum.verify_checksum("abc", str(chk_file)) + assert not checksum.verify_checksum("def", str(chk_file)) + + assert checksum.update_checksum("payload", str(chk_file)) + expected = hashlib.sha256("payload".encode()).hexdigest() + assert chk_file.read_text() == expected + + +def test_initialize_checksum(tmp_path): + data = tmp_path / "file.bin" + data.write_text("payload") + chk_file = tmp_path / "chk2.txt" + assert checksum.initialize_checksum(str(data), str(chk_file)) + expected = hashlib.sha256("payload".encode()).hexdigest() + assert chk_file.read_text() == expected diff --git a/src/tests/test_fingerprint_manager_utils.py b/src/tests/test_fingerprint_manager_utils.py new file mode 100644 index 0000000..ca44175 --- /dev/null +++ b/src/tests/test_fingerprint_manager_utils.py @@ -0,0 +1,20 @@ +from utils.fingerprint_manager import FingerprintManager + + +def test_add_and_remove_fingerprint(tmp_path): + mgr = FingerprintManager(tmp_path) + phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" + fp = mgr.add_fingerprint(phrase) + assert fp in mgr.list_fingerprints() + dir_path = mgr.get_fingerprint_directory(fp) + assert dir_path and dir_path.exists() + assert mgr.select_fingerprint(fp) + assert mgr.get_current_fingerprint_dir() == dir_path + assert mgr.remove_fingerprint(fp) + assert fp not in mgr.list_fingerprints() + assert not dir_path.exists() + + +def test_remove_nonexistent_fingerprint(tmp_path): + mgr = FingerprintManager(tmp_path) + assert not mgr.remove_fingerprint("UNKNOWN") diff --git a/src/tests/test_password_prompt.py b/src/tests/test_password_prompt.py new file mode 100644 index 0000000..32c7a8f --- /dev/null +++ b/src/tests/test_password_prompt.py @@ -0,0 +1,34 @@ +import builtins +from itertools import cycle + +import pytest + +from utils import password_prompt + + +def test_prompt_new_password(monkeypatch): + responses = cycle(["goodpass", "goodpass"]) + monkeypatch.setattr( + password_prompt.getpass, "getpass", lambda prompt: next(responses) + ) + result = password_prompt.prompt_new_password() + assert result == "goodpass" + + +def test_prompt_new_password_retry(monkeypatch): + seq = iter(["pass1", "pass2", "passgood", "passgood"]) + monkeypatch.setattr(password_prompt.getpass, "getpass", lambda prompt: next(seq)) + result = password_prompt.prompt_new_password() + assert result == "passgood" + + +def test_prompt_existing_password(monkeypatch): + monkeypatch.setattr(password_prompt.getpass, "getpass", lambda prompt: "mypassword") + assert password_prompt.prompt_existing_password() == "mypassword" + + +def test_confirm_action_yes_no(monkeypatch): + monkeypatch.setattr(builtins, "input", lambda _: "Y") + assert password_prompt.confirm_action() + monkeypatch.setattr(builtins, "input", lambda _: "n") + assert not password_prompt.confirm_action()