From 3912b85d08fb40753b04dac4207696d8be7893d7 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Fri, 16 Jul 2021 10:08:24 -0400 Subject: [PATCH] Common: Add get_file_sha256_hash() --- .gitattributes | 1 + monkey/common/utils/file_utils.py | 10 ++++++++++ monkey/tests/conftest.py | 10 ++++++++++ monkey/tests/data_for_tests/stable_file.txt | 1 + .../unit_tests/common/utils/test_common_file_utils.py | 6 +++++- 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 monkey/tests/data_for_tests/stable_file.txt diff --git a/.gitattributes b/.gitattributes index 1cc8cc472..8ae3cfdb8 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ monkey/tests/data_for_tests/ransomware_targets/** -text +monkey/tests/data_for_tests/stable_file.txt -text diff --git a/monkey/common/utils/file_utils.py b/monkey/common/utils/file_utils.py index a4cff2b48..fd2c85ec1 100644 --- a/monkey/common/utils/file_utils.py +++ b/monkey/common/utils/file_utils.py @@ -1,3 +1,4 @@ +import hashlib import os from pathlib import Path @@ -11,3 +12,12 @@ def expand_path(path: str) -> Path: raise InvalidPath("Empty path provided") return Path(os.path.expandvars(os.path.expanduser(path))) + + +def get_file_sha256_hash(filepath: Path): + sha256 = hashlib.sha256() + with open(filepath, "rb") as f: + for block in iter(lambda: f.read(65536), b""): + sha256.update(block) + + return sha256.hexdigest() diff --git a/monkey/tests/conftest.py b/monkey/tests/conftest.py index 23cc840a3..fc44af014 100644 --- a/monkey/tests/conftest.py +++ b/monkey/tests/conftest.py @@ -11,3 +11,13 @@ sys.path.insert(0, MONKEY_BASE_PATH) @pytest.fixture(scope="session") def data_for_tests_dir(pytestconfig): return Path(os.path.join(pytestconfig.rootdir, "monkey", "tests", "data_for_tests")) + + +@pytest.fixture(scope="session") +def stable_file(data_for_tests_dir) -> Path: + return data_for_tests_dir / "stable_file.txt" + + +@pytest.fixture(scope="session") +def stable_file_sha256_hash() -> str: + return "d9dcaadc91261692dafa86e7275b1bf39bb7e19d2efcfacd6fe2bfc9a1ae1062" diff --git a/monkey/tests/data_for_tests/stable_file.txt b/monkey/tests/data_for_tests/stable_file.txt new file mode 100644 index 000000000..ffe82625b --- /dev/null +++ b/monkey/tests/data_for_tests/stable_file.txt @@ -0,0 +1 @@ +Don't change me! diff --git a/monkey/tests/unit_tests/common/utils/test_common_file_utils.py b/monkey/tests/unit_tests/common/utils/test_common_file_utils.py index 3fe981a74..79d00d027 100644 --- a/monkey/tests/unit_tests/common/utils/test_common_file_utils.py +++ b/monkey/tests/unit_tests/common/utils/test_common_file_utils.py @@ -2,7 +2,7 @@ import os import pytest -from common.utils.file_utils import InvalidPath, expand_path +from common.utils.file_utils import InvalidPath, expand_path, get_file_sha256_hash def test_expand_user(patched_home_env): @@ -22,3 +22,7 @@ def test_expand_vars(patched_home_env): def test_expand_path__empty_path_provided(): with pytest.raises(InvalidPath): expand_path("") + + +def test_get_file_sha256_hash(stable_file, stable_file_sha256_hash): + assert get_file_sha256_hash(stable_file) == stable_file_sha256_hash