caellion-python-commons: tests/test_hashing_file_hashing.py Source File

caellion-python-commons

test_hashing_file_hashing.py
Go to the documentation of this file.
1 # test framework
2 import unittest
3 import pytest
4 import binascii
5
6 # package
7 from caellion.pycommons.hashing.file_hashing import FileHasher, NotDoneYetException, AlreadyDoneException, UnsupportedAlgorithmException # noqa: F401
8
9 TEST_DATA_PATH = "tests/testdata/hashing/file_hashing/"
10
11 TEST_PARAMETERIZED_LIST = [
12 # Empty file in 01.bin
13 ("01.bin", "", "da39a3ee5e6b4b0d3255bfef95601890afd80709"),
14 ("01.bin", None, "da39a3ee5e6b4b0d3255bfef95601890afd80709"),
15 ("01.bin", "sha1", "da39a3ee5e6b4b0d3255bfef95601890afd80709"),
16 ("01.bin", "sha256", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
17 ("01.bin", "sha384", "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"),
18 ("01.bin", "sha512", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"),
19 ("01.bin", "md5", "d41d8cd98f00b204e9800998ecf8427e"),
20 # UTF16-LE Strings in 02.bin
21 ("02.bin", "sha1", "752f931aa0c4b7a942ceff6f35ecf73bf84723f7"),
22 ("02.bin", "sha256", "2379d57ca5836d99c3b19fa06611fdbf3240d25597bb813768cacc10064a1773"),
23 ("02.bin", "sha384", "f8bd72222ecc74c456fb96ba1dd049a37a2b643af64a5e811a08a9a2c507b1efecb0033ab7fb9560e1e3a17d3f6dc607"),
24 ("02.bin", "sha512", "d40223f1c63c75b457be72ed6959b2a3bda8ba36f12f2bd6db882b04892cfac37874aa496a23f1d63e77a7afef6fb04c52533fe0fc67a6e178357e4f38324450"),
25 ("02.bin", "md5", "9756321f9c028d259e79fb074e17149e"),
26 # Random binary in 03.bin
27 ("03.bin", "sha1", "a3a14d1f4115e8280e87e1eeb91fa89dcccb1a37"),
28 ("03.bin", "sha256", "1c527e0612df82e21032f39774507541813db0809fa5809ed1707231684219e4"),
29 ("03.bin", "sha384", "a9f9364ea82117a6edf0769d51e8da4dce89b0114b75d143583a1309094b4d9b40a279927b152335c84adfa391550760"),
30 ("03.bin", "sha512", "e9652220c97b6b8c511be78c4924f89e84f86f0d78fbfa8a9a46f5c559b26140f45f70c44cc61e5b570cc3ad64a327e25f0de13180352caa4eb4d1f53cd556cb"),
31 ("03.bin", "md5", "db4a90ecad9e87970d6fc6e30ccbf8ee"),
32 # Random binary in 04.bin (10MB file)
33 ("04.bin", "sha1", "66edb5278033be74dbfeb06693701fce60b8236c"),
34 ("04.bin", "sha256", "5f44e1370f6cb59f0fa47df86ede5a55226580f6471b44159fe8372a105392da"),
35 ("04.bin", "sha384", "9fa04d1f69d44492c3a4c8fb48be4146dbc1c3b739e3f45e2abaf64051a55f592ce4ec87bdfc74db53ef5bdfbe1e071f"),
36 ("04.bin", "sha512", "73f18b99cc8bbd5da23e961d5edfd4ceeab3f7a328785f9c0a46f66aaaf89e541690cebef3a450f23dcc327122c08a791221454500e87ee6e4b9d39dbb39413c"),
37 ("04.bin", "md5", "bba058fa7e56035c7435da960140155d"),
38 ]
39
40
41 # test cases
42 class TestHashingFileHasher():
43
44 maxDiff = None
45
46 # exceptions
47 def test_unsupported_algorithm(self):
48 with pytest.raises(UnsupportedAlgorithmException):
49 FH = FileHasher("md4") # noqa
50
51 def test_get_hexdigest_not_done_hash_bytes(self):
52 with pytest.raises(NotDoneYetException):
53 FH = FileHasher("md5") # noqa
54 FH.get_hashbytes()
55
56 def test_get_hexdigest_not_done_hash_hex(self):
57 with pytest.raises(NotDoneYetException):
58 FH = FileHasher("md5") # noqa
59 FH.get_hexdigest()
60
61 def test_cannot_hash_twice_raw(self):
62 with pytest.raises(AlreadyDoneException):
63 FH = FileHasher("md5") # noqa
64 with open(TEST_DATA_PATH + "01.bin", "rb") as f:
65 FH.hash_file(f)
66 FH.hash_file(f)
67
68 def test_cannot_hash_twice_path(self):
69 with pytest.raises(AlreadyDoneException):
70 FH = FileHasher("md5") # noqa
71 FH.hash_path(TEST_DATA_PATH + "01.bin")
72 FH.hash_path(TEST_DATA_PATH + "01.bin")
73
74 @pytest.mark.parametrize("path,algo,expected",TEST_PARAMETERIZED_LIST)
75 def test_hashes_file_ok_raw(self, path, algo, expected):
76 FH = FileHasher(algo) # noqa
77 with open(TEST_DATA_PATH + path, "rb") as f:
78 FH.hash_file(f)
79 result = FH.get_hashbytes()
80 assert result == binascii.unhexlify(expected)
81
82 @pytest.mark.parametrize("path,algo,expected",TEST_PARAMETERIZED_LIST)
83 def test_hashes_file_ok_hexdigest(self, path, algo, expected):
84 FH = FileHasher(algo) # noqa
85 with open(TEST_DATA_PATH + path, "rb") as f:
86 FH.hash_file(f)
87 result = FH.get_hexdigest()
88 assert result == expected
89
90 @pytest.mark.parametrize("path,algo,expected",TEST_PARAMETERIZED_LIST)
91 def test_hashes_path_ok_raw(self, path, algo, expected):
92 FH = FileHasher(algo) # noqa
93 FH.hash_path(TEST_DATA_PATH + path)
94 result = FH.get_hashbytes()
95 assert result == binascii.unhexlify(expected)
96
97 @pytest.mark.parametrize("path,algo,expected",TEST_PARAMETERIZED_LIST)
98 def test_hashes_path_ok_hexdigest(self, path, algo, expected):
99 FH = FileHasher(algo) # noqa
100 FH.hash_path(TEST_DATA_PATH + path)
101 result = FH.get_hexdigest()
102 assert result == expected
103
104 @pytest.mark.parametrize("path,algo,expected",TEST_PARAMETERIZED_LIST)
105 def test_hashes_file_ok_raw_combo(self, path, algo, expected):
106 FH = FileHasher(algo) # noqa
107 with open(TEST_DATA_PATH + path, "rb") as f:
108 result = FH.hash_file_and_get_hashbytes(f)
109 assert result == binascii.unhexlify(expected)
110
111 @pytest.mark.parametrize("path,algo,expected",TEST_PARAMETERIZED_LIST)
112 def test_hashes_file_ok_hexdigest_combo(self, path, algo, expected):
113 FH = FileHasher(algo) # noqa
114 with open(TEST_DATA_PATH + path, "rb") as f:
115 result = FH.hash_file_and_get_hexdigest(f)
116 assert result == expected
117
118 @pytest.mark.parametrize("path,algo,expected",TEST_PARAMETERIZED_LIST)
119 def test_hashes_path_ok_raw_combo(self, path, algo, expected):
120 FH = FileHasher(algo) # noqa
121 result = FH.hash_path_and_get_hashbytes(TEST_DATA_PATH + path)
122 assert result == binascii.unhexlify(expected)
123
124 @pytest.mark.parametrize("path,algo,expected",TEST_PARAMETERIZED_LIST)
125 def test_hashes_path_ok_hexdigest_combo(self, path, algo, expected):
126 FH = FileHasher(algo) # noqa
127 result = FH.hash_path_and_get_hexdigest(TEST_DATA_PATH + path)
128 assert result == expected
129
130
131 if __name__ == "__main__":
132 unittest.main()
caellion.pycommons.hashing.file_hashing.FileHasher
This class provides hashlib-based file hasher.
Definition: file_hashing.py:32
tests.test_hashing_file_hashing.TestHashingFileHasher
Definition: test_hashing_file_hashing.py:42
tests.test_hashing_file_hashing.TestHashingFileHasher.test_hashes_file_ok_hexdigest
def test_hashes_file_ok_hexdigest(self, path, algo, expected)
Definition: test_hashing_file_hashing.py:83
tests.test_hashing_file_hashing.TestHashingFileHasher.test_hashes_path_ok_raw
def test_hashes_path_ok_raw(self, path, algo, expected)
Definition: test_hashing_file_hashing.py:91
tests.test_hashing_file_hashing.TestHashingFileHasher.test_unsupported_algorithm
def test_unsupported_algorithm(self)
Definition: test_hashing_file_hashing.py:47
tests.test_hashing_file_hashing.TestHashingFileHasher.test_get_hexdigest_not_done_hash_bytes
def test_get_hexdigest_not_done_hash_bytes(self)
Definition: test_hashing_file_hashing.py:51
tests.test_hashing_file_hashing.TestHashingFileHasher.test_hashes_file_ok_hexdigest_combo
def test_hashes_file_ok_hexdigest_combo(self, path, algo, expected)
Definition: test_hashing_file_hashing.py:112
tests.test_hashing_file_hashing.TestHashingFileHasher.test_hashes_path_ok_raw_combo
def test_hashes_path_ok_raw_combo(self, path, algo, expected)
Definition: test_hashing_file_hashing.py:119
tests.test_hashing_file_hashing.TestHashingFileHasher.test_hashes_file_ok_raw
def test_hashes_file_ok_raw(self, path, algo, expected)
Definition: test_hashing_file_hashing.py:75
tests.test_hashing_file_hashing.TestHashingFileHasher.test_hashes_path_ok_hexdigest_combo
def test_hashes_path_ok_hexdigest_combo(self, path, algo, expected)
Definition: test_hashing_file_hashing.py:125
tests.test_hashing_file_hashing.TestHashingFileHasher.test_cannot_hash_twice_path
def test_cannot_hash_twice_path(self)
Definition: test_hashing_file_hashing.py:68
tests.test_hashing_file_hashing.TestHashingFileHasher.test_hashes_file_ok_raw_combo
def test_hashes_file_ok_raw_combo(self, path, algo, expected)
Definition: test_hashing_file_hashing.py:105
tests.test_hashing_file_hashing.TestHashingFileHasher.test_hashes_path_ok_hexdigest
def test_hashes_path_ok_hexdigest(self, path, algo, expected)
Definition: test_hashing_file_hashing.py:98
tests.test_hashing_file_hashing.TestHashingFileHasher.test_cannot_hash_twice_raw
def test_cannot_hash_twice_raw(self)
Definition: test_hashing_file_hashing.py:61
tests.test_hashing_file_hashing.TestHashingFileHasher.test_get_hexdigest_not_done_hash_hex
def test_get_hexdigest_not_done_hash_hex(self)
Definition: test_hashing_file_hashing.py:56
caellion.pycommons.hashing.file_hashing
This module provides utilities related to hashing files.
Definition: file_hashing.py:1