From c4aa5d5a57c6f2334a02572f0beadfcdab23521e Mon Sep 17 00:00:00 2001 From: Dilawar Singh <dilawar@subcom.tech> Date: Sun, 9 Oct 2022 14:22:56 +0530 Subject: [PATCH] test: adds test to verify checksum --- bitia/checksumdir.py | 5 +---- tests/test_methods.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/bitia/checksumdir.py b/bitia/checksumdir.py index 5ebdff2..2cb5bd5 100644 --- a/bitia/checksumdir.py +++ b/bitia/checksumdir.py @@ -59,10 +59,7 @@ def filehash(filepath: Path, hashfunc: str = "sha256"): """Compute checksum of a file""" hasher = HASH_FUNCS[hashfunc]() blocksize = 64 * 1024 - - if not filepath.is_file(): - return hasher.hexdigest() - + assert filepath.is_file(), f"{filepath} is not a file" with filepath.open("rb") as fp: while True: data = fp.read(blocksize) diff --git a/tests/test_methods.py b/tests/test_methods.py index fa0fce7..eb2645d 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -3,17 +3,30 @@ from pathlib import Path from bitia.common import sha256sum +_common_hashes = { + "1": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", + "123": "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", + "abcdxyz": "bc5c610196f15a7e0d21a3af77b0e1740a4415a44d8a7ad4905d877825574bf9", +} + def test_filehash(): tempdir = tempfile.gettempdir() - t1 = "ls -ltrh /" - t2 = "ls -ltrh" f1 = Path(tempdir) / "a.txt" f2 = Path(tempdir) / "b.txt" + + for _c, _h in _common_hashes.items(): + f1.write_text(_c) + assert f1.read_text() == _c + print(f"Computing hash of {f1}, content: {_c}") + assert sha256sum(f1) == _h + + t1 = "ls -ltrh /" f1.write_text(t1) f2.write_text(t1) assert sha256sum(f1) == sha256sum(f2) + t2 = "ls -ltrh" f2.write_text(t2) assert sha256sum(f1) != sha256sum(f2) -- GitLab