|
| 1 | +import hashlib |
| 2 | +import os |
| 3 | +import unittest |
| 4 | + |
| 5 | +from autils.file import crypto |
| 6 | +from tests.utils import TestCaseTmpDir, setup_autils_loggers |
| 7 | + |
| 8 | +setup_autils_loggers() |
| 9 | + |
| 10 | + |
| 11 | +class HashFileTest(TestCaseTmpDir): |
| 12 | + """Test cases for crypto.hash_file function.""" |
| 13 | + |
| 14 | + def _create_test_file(self, content, filename="testfile"): |
| 15 | + """Helper to create a test file with given content.""" |
| 16 | + filepath = os.path.join(self.tmpdir.name, filename) |
| 17 | + with open(filepath, "wb") as f: |
| 18 | + f.write(content) |
| 19 | + return filepath |
| 20 | + |
| 21 | + # Core algorithm tests - testing the algorithm parameter code path |
| 22 | + def test_hash_file_md5_default(self): |
| 23 | + """Test MD5 hash calculation with default algorithm.""" |
| 24 | + content = b"Hello, World!" |
| 25 | + filepath = self._create_test_file(content) |
| 26 | + expected = hashlib.md5(content).hexdigest() |
| 27 | + result = crypto.hash_file(filepath) |
| 28 | + self.assertEqual(result, expected) |
| 29 | + |
| 30 | + def test_hash_file_sha256(self): |
| 31 | + """Test SHA256 hash calculation.""" |
| 32 | + content = b"Test content for SHA256" |
| 33 | + filepath = self._create_test_file(content) |
| 34 | + expected = hashlib.sha256(content).hexdigest() |
| 35 | + result = crypto.hash_file(filepath, algorithm="sha256") |
| 36 | + self.assertEqual(result, expected) |
| 37 | + |
| 38 | + # Size parameter tests - each tests a distinct code path |
| 39 | + def test_hash_file_with_size_limit(self): |
| 40 | + """Test hashing only the first N bytes of a file.""" |
| 41 | + content = b"ABCDEFGHIJ" # 10 bytes |
| 42 | + filepath = self._create_test_file(content) |
| 43 | + # Hash only first 5 bytes - tests size < file_size path |
| 44 | + expected = hashlib.md5(b"ABCDE").hexdigest() |
| 45 | + result = crypto.hash_file(filepath, size=5) |
| 46 | + self.assertEqual(result, expected) |
| 47 | + |
| 48 | + def test_hash_file_size_larger_than_file(self): |
| 49 | + """Test that size larger than file hashes the whole file.""" |
| 50 | + content = b"Small file" |
| 51 | + filepath = self._create_test_file(content) |
| 52 | + expected = hashlib.md5(content).hexdigest() |
| 53 | + # Request more bytes than file contains - tests size > file_size branch |
| 54 | + result = crypto.hash_file(filepath, size=1000000) |
| 55 | + self.assertEqual(result, expected) |
| 56 | + |
| 57 | + def test_hash_file_size_falsy_hashes_whole_file(self): |
| 58 | + """Test that falsy size values (None, 0) hash the entire file.""" |
| 59 | + content = b"Complete file content" |
| 60 | + filepath = self._create_test_file(content) |
| 61 | + expected = hashlib.md5(content).hexdigest() |
| 62 | + # Both None and 0 are falsy - tests 'not size' branch |
| 63 | + self.assertEqual(crypto.hash_file(filepath, size=None), expected) |
| 64 | + self.assertEqual(crypto.hash_file(filepath, size=0), expected) |
| 65 | + |
| 66 | + # Edge case tests - each tests unique behavior |
| 67 | + def test_hash_file_empty_file(self): |
| 68 | + """Test hashing an empty file.""" |
| 69 | + filepath = self._create_test_file(b"") |
| 70 | + expected = hashlib.md5(b"").hexdigest() |
| 71 | + result = crypto.hash_file(filepath) |
| 72 | + self.assertEqual(result, expected) |
| 73 | + |
| 74 | + def test_hash_file_binary_content(self): |
| 75 | + """Test hashing a file with all possible byte values.""" |
| 76 | + content = bytes(range(256)) # All byte values 0-255 |
| 77 | + filepath = self._create_test_file(content) |
| 78 | + expected = hashlib.md5(content).hexdigest() |
| 79 | + result = crypto.hash_file(filepath) |
| 80 | + self.assertEqual(result, expected) |
| 81 | + |
| 82 | + def test_hash_file_larger_than_chunk_size(self): |
| 83 | + """Test hashing a file that requires multiple read iterations.""" |
| 84 | + # Create content larger than io.DEFAULT_BUFFER_SIZE (typically 8192) |
| 85 | + content = b"x" * 100000 |
| 86 | + filepath = self._create_test_file(content) |
| 87 | + expected = hashlib.md5(content).hexdigest() |
| 88 | + result = crypto.hash_file(filepath) |
| 89 | + self.assertEqual(result, expected) |
| 90 | + |
| 91 | + # Error handling tests |
| 92 | + def test_hash_file_invalid_algorithm_returns_none(self): |
| 93 | + """Test that invalid algorithm returns None without raising.""" |
| 94 | + content = b"Test content" |
| 95 | + filepath = self._create_test_file(content) |
| 96 | + result = crypto.hash_file(filepath, algorithm="invalid_algo") |
| 97 | + self.assertIsNone(result) |
| 98 | + |
| 99 | + def test_hash_file_nonexistent_file_raises(self): |
| 100 | + """Test that non-existent file raises FileNotFoundError.""" |
| 101 | + nonexistent = os.path.join(self.tmpdir.name, "nonexistent_file.txt") |
| 102 | + with self.assertRaises(FileNotFoundError): |
| 103 | + crypto.hash_file(nonexistent) |
| 104 | + |
| 105 | + # Hash uniqueness test - verifies hash function works correctly |
| 106 | + def test_hash_file_different_content_produces_different_hash(self): |
| 107 | + """Test that different content produces different hash values.""" |
| 108 | + filepath1 = self._create_test_file(b"Content A", filename="file1.txt") |
| 109 | + filepath2 = self._create_test_file(b"Content B", filename="file2.txt") |
| 110 | + hash1 = crypto.hash_file(filepath1) |
| 111 | + hash2 = crypto.hash_file(filepath2) |
| 112 | + self.assertNotEqual(hash1, hash2) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + unittest.main() |
0 commit comments