Skip to content
Snippets Groups Projects
Commit c4aa5d5a authored by dilawar's avatar dilawar :ant:
Browse files

test: adds test to verify checksum

parent 13d06d53
No related branches found
No related tags found
1 merge request!2version 0.2.0
Pipeline #3758 failed with stages
in 2 minutes and 4 seconds
......@@ -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)
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment