diff options
author | mjfernez <mjfernez@gmail.com> | 2021-03-29 20:19:25 -0400 |
---|---|---|
committer | mjfernez <mjfernez@gmail.com> | 2021-03-29 20:19:25 -0400 |
commit | 242c3b75cffbffae114dd4a2b7dc453399cfa428 (patch) | |
tree | 9110b317ee65c688916628a5831dd430e27a7e56 /fhash/tests/run_tests.py | |
parent | b45b3948e5082a1b20419286a5059bd93834d167 (diff) | |
download | scripts-n-tools-242c3b75cffbffae114dd4a2b7dc453399cfa428.tar.gz |
Fhash clean up and unit tests
Added saner unit testing in the tests folder, which can be added to by
expanding the data in the tests/hashes.json file. Also removed some
redundant comments and inconsistencies
Diffstat (limited to 'fhash/tests/run_tests.py')
-rw-r--r-- | fhash/tests/run_tests.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/fhash/tests/run_tests.py b/fhash/tests/run_tests.py new file mode 100644 index 0000000..de78c06 --- /dev/null +++ b/fhash/tests/run_tests.py @@ -0,0 +1,62 @@ +import sys +import json +import unittest +import subprocess +sys.path.append('..') +import fhash + +class TestFhash(unittest.TestCase): + # these files were teseted with openssl + HASHES = json.load(open('hashes.json')) + FHASH = "../fhash.py" + DIR = "files/" + + + def test_shattered(self): + """Just for fun, two files that have the same hash, breaking sha1""" + SHATTERED = '38762cf7f55934b34d179ae6a4c80cadccbb7f0a' + cmd = [self.FHASH, 'sha1', '-i', self.DIR + 'shattered-1.pdf'] + shat1 = subprocess.check_output(cmd).strip().decode() + cmd[3] = self.DIR + 'shattered-2.pdf' + shat2 = subprocess.check_output(cmd).strip().decode() + self.assertTrue(shat1 == shat2 == SHATTERED) + + + def test_file_types(self): + """Tests that hashes for different file types are correct. + Uses sha1""" + for k in self.HASHES.keys(): + cmd = [self.FHASH, 'sha1', '-i', self.DIR + k] + fh = subprocess.check_output(cmd).strip().decode() + ssl = self.HASHES[k]['sha1'] + self.assertFalse(ssl != fh, + f"{k}:{fh} did not match the correct hash: {ssl}" + ) + + def test_algos(self): + """Tests that hashes for different algorithms and sizes are correct. + """ + + for k in self.HASHES.keys(): + for alg in fhash.ALGOS: + if alg == 'md5' or alg == 'sha1': + + cmd = [self.FHASH, alg, '-i', self.DIR + k] + fh = subprocess.check_output(cmd).strip().decode() + ssl = self.HASHES[k][alg] + self.assertFalse(ssl != fh, + f"{k}:{fh} did not match the correct hash: {ssl}" + ) + else: + for s in fhash.SIZES: + cmd = [self.FHASH, alg, '-i', self.DIR + k, '-s', + str(s)] + fh = subprocess.check_output(cmd).strip().decode() + ssl = self.HASHES[k][alg + '-' + str(s)] + self.assertFalse(ssl != fh, + f"{k}:{fhash} did not match the correct hash: {ssl}" + ) + + +if __name__ == '__main__': + unittest.main() |