def test_file_to_file_failure_expected(self): hc = HashChecker() hc.filepath_one = self.testfile1_path hc.filepath_two = self.testfile2_path # a slightly modified version of above file (should change hash) ret_val = hc.file_to_file() self.assertEqual(ret_val['type'], "SHA256") self.assertEqual(ret_val['filehash1'], self.hashtest_sha256_hash) self.assertFalse(ret_val['is_equal'])
def test_file_to_file_success_expected(self): hc = HashChecker() hc.filepath_one = self.testfile1_path hc.filepath_two = self.testfile3_path # a duplicate of the above file ret_val = hc.file_to_file() self.assertEqual(ret_val['type'], "SHA256") self.assertEqual(ret_val['filehash1'], self.hashtest_sha256_hash) self.assertEqual(ret_val['filehash2'], self.hashtest_sha256_hash) self.assertTrue(ret_val['is_equal'])
def test_hash_to_file_md5_success_expected(self): hc = HashChecker() hc.hash_type = "md5" hc.filepath_one = self.testfile1_path hc.hash_one = self.hashtest_md5_hash ret_val = hc.file_to_hash() self.assertEqual(ret_val['type'], "MD5") self.assertEqual(ret_val['filehash'], self.hashtest_md5_hash) self.assertTrue(ret_val['is_equal'])
def test_hash_to_file_sha512_failure_expected(self): hc = HashChecker() hc.hash_type = "sha512" hc.filepath_one = self.testfile1_path hc.hash_one = self.hashtest_sha512_hash_bad ret_val = hc.file_to_hash() self.assertEqual(ret_val['type'], "SHA512") self.assertEqual(ret_val['filehash'], self.hashtest_sha512_hash) self.assertFalse(ret_val['is_equal'])
def test_hash_to_hash_fail_expected(self): hc = HashChecker() hc.hash_one = self.hashtest_md5_hash hc.hash_two = self.hashtest_sha1_hash self.assertFalse(hc.hash_to_hash())
def test_hash_to_hash_success_expected(self): hc = HashChecker() hc.hash_one = self.hashtest_md5_hash hc.hash_two = self.hashtest_md5_hash self.assertTrue(hc.hash_to_hash())
def test_is_not_hash_bogusstring(self): hc = HashChecker() self.assertFalse(hc.is_hash(self.fake_hash)) self.assertEqual(hc.hash_type, "")
def test_is_hash_sha512(self): hc = HashChecker() self.assertTrue(hc.is_hash(self.hashtest_sha512_hash)) self.assertEqual(hc.hash_type, "sha512")
def test_is_hash_md5(self): hc = HashChecker() self.assertTrue(hc.is_hash(self.hashtest_md5_hash)) self.assertEqual(hc.hash_type, "md5")
def main(): c = Command() if c.does_not_validate_missing_args(): print(hsh_usage) sys.exit(1) if c.is_help_request(): # User requested hsh help information print(hsh_help) sys.exit(0) elif c.is_usage_request(): # User requested hsh usage information print(hsh_usage) sys.exit(0) elif c.is_version_request(): # User requested hsh version information version_display_string = app_name + ' ' + major_version + '.' + minor_version + '.' + patch_version print(version_display_string) sys.exit(0) primary_command = c.subcmd.lower() # make the subcommand case-independent if primary_command == "sha1": if c.argc > 1: file_list = c.argv[1:] for file in file_list: if file_exists(file): hasher = Hasher() sha_hash = hasher.sha1(file) print("SHA1 (" + file + ") :") print(sha_hash) else: sys.stderr.write( file + " does not appear to be an existing file path.\n") else: sys.stderr.write( "You did not include a file in your command. Please try again.\n" ) sys.exit(1) elif primary_command == "sha224": if c.argc > 1: file_list = c.argv[1:] for file in file_list: if file_exists(file): hasher = Hasher() sha_hash = hasher.sha224(file) print("SHA224 (" + file + ") :") print(sha_hash) else: sys.stderr.write( file + " does not appear to be an existing file path.\n") else: sys.stderr.write( "You did not include a file in your command. Please try again.\n" ) sys.exit(1) elif primary_command == "sha256": if c.argc > 1: file_list = c.argv[1:] for file in file_list: if file_exists(file): hasher = Hasher() sha_hash = hasher.sha256(file) print("SHA256 (" + file + ") :") print(sha_hash) else: sys.stderr.write( file + " does not appear to be an existing file path.\n") else: sys.stderr.write( "You did not include a file in your command. Please try again.\n" ) sys.exit(1) elif primary_command == "sha384": if c.argc > 1: file_list = c.argv[1:] for file in file_list: if file_exists(file): hasher = Hasher() sha_hash = hasher.sha384(file) print("SHA384 (" + file + ") :") print(sha_hash) else: sys.stderr.write( file + " does not appear to be an existing file path.\n") else: sys.stderr.write( "You did not include a file in your command. Please try again.\n" ) sys.exit(1) elif primary_command == "sha512": if c.argc > 1: file_list = c.argv[1:] for file in file_list: if file_exists(file): hasher = Hasher() sha_hash = hasher.sha512(file) print("SHA512 (" + file + ") :") print(sha_hash) else: sys.stderr.write( file + " does not appear to be an existing file path.\n") else: sys.stderr.write( "You did not include a file in your command. Please try again.\n" ) sys.exit(1) elif primary_command == "md5": if c.argc > 1: file_list = c.argv[1:] for file in file_list: if file_exists(file): hasher = Hasher() sha_hash = hasher.md5(file) print("MD5 (" + file + ") :") print(sha_hash) else: sys.stderr.write( file + " does not appear to be an existing file path.\n") else: sys.stderr.write( "You did not include a file in your command. Please try again.\n" ) sys.exit(1) elif primary_command == "check": if c.argc == 3: # primary command + 2 arguments hc = HashChecker() hc.compare( c.argv[1:] ) # pass the argument list excluding the primary command elif c.argc < 3: sys.stderr.write( "You did not include a file or hash digest for comparison. Please try again.\n" ) sys.exit(1) elif c.argc > 3: sys.stderr.write( "Too many arguments. Please include two arguments for comparison.\n" ) sys.exit(1) elif c.argc == 1: # single file hash digest request with default SHA256 settings file = c.arg0 if file_exists(file): hasher = Hasher() sha_hash = hasher.sha256(file) print("SHA256 (" + file + ") :") print(sha_hash) else: sys.stderr.write( c.arg0 + " does not appear to be an existing file path. Please try again.\n" ) sys.exit(1) elif c.argc == 2: # exactly two arguments, perform comparison between them by default hc = HashChecker() hc.compare( c.argv ) # pass the entire argument list because there is no primary command else: print( "Could not complete the command that you entered. Please try again." ) sys.exit(1)