music on atproto
plyr.fm
1"""tests for chunked hash calculation."""
2
3import hashlib
4import io
5
6from backend.utilities.hashing import hash_file_chunked
7
8
9def test_hash_file_chunked_correctness() -> None:
10 """chunked hashing must produce same result as standard approach."""
11 # create test data larger than chunk size to test chunking
12 test_data = b"relay audio streaming test data " * 500000 # ~16MB
13
14 # standard approach (what we used before)
15 expected_hash = hashlib.sha256(test_data).hexdigest()
16
17 # chunked approach (new implementation)
18 file_obj = io.BytesIO(test_data)
19 actual_hash = hash_file_chunked(file_obj)
20
21 assert actual_hash == expected_hash, (
22 f"chunked hash doesn't match standard hash\n"
23 f"expected: {expected_hash}\n"
24 f"actual: {actual_hash}"
25 )
26
27
28def test_hash_file_chunked_resets_pointer() -> None:
29 """file pointer must be reset after hashing for subsequent operations."""
30 test_data = b"test data for pointer reset verification"
31
32 file_obj = io.BytesIO(test_data)
33
34 # hash the file
35 hash_file_chunked(file_obj)
36
37 # pointer should be at start
38 assert file_obj.tell() == 0, "file pointer not reset after hashing"
39
40 # should be able to read full content
41 content = file_obj.read()
42 assert content == test_data, "can't read full content after hashing"
43
44
45def test_hash_file_chunked_empty_file() -> None:
46 """hashing empty file should work without error."""
47 file_obj = io.BytesIO(b"")
48
49 file_hash = hash_file_chunked(file_obj)
50
51 # empty data has known SHA256 hash
52 expected = hashlib.sha256(b"").hexdigest()
53 assert file_hash == expected