-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
135 lines (114 loc) · 4.95 KB
/
test_main.py
File metadata and controls
135 lines (114 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import pytest
import hashlib
from PIL import Image
from main import (
compute_image_hash,
extract_metadata,
compare_metadata,
perform_pixel_comparison
)
# Set up test paths
TEST_IMAGES_DIR = os.path.join(os.path.dirname(__file__), 'test_images')
ORIGINAL_IMAGE = os.path.join(TEST_IMAGES_DIR, 'original.jpg')
TAMPERED_IMAGE = os.path.join(TEST_IMAGES_DIR, 'tampered.jpg')
DIFFERENT_IMAGE = os.path.join(TEST_IMAGES_DIR, 'different.jpg')
NON_EXISTENT_IMAGE = os.path.join(TEST_IMAGES_DIR, 'ghost.jpg')
@pytest.fixture(scope="module")
def setup_test_images():
"""Create test images if they don't exist"""
os.makedirs(TEST_IMAGES_DIR, exist_ok=True)
# Create original test image
if not os.path.exists(ORIGINAL_IMAGE):
img = Image.new('RGB', (100, 100), color='red')
img.save(ORIGINAL_IMAGE)
# Create tampered version (single pixel changed)
if not os.path.exists(TAMPERED_IMAGE):
img = Image.open(ORIGINAL_IMAGE)
pixels = img.load()
pixels[47, 48] = (0, 255, 0) # Change one pixel to green
img.save(TAMPERED_IMAGE)
# Create completely different image
if not os.path.exists(DIFFERENT_IMAGE):
img = Image.new('RGB', (100, 100), color='blue')
img.save(DIFFERENT_IMAGE)
def test_compute_image_hash(setup_test_images):
"""Test SHA-256 hash computation for images"""
# Hash should be consistent for same image
hash1 = compute_image_hash(ORIGINAL_IMAGE)
hash2 = compute_image_hash(ORIGINAL_IMAGE)
assert hash1 == hash2
# Hash should be different for different images
hash_original = compute_image_hash(ORIGINAL_IMAGE)
hash_tampered = compute_image_hash(TAMPERED_IMAGE)
hash_different = compute_image_hash(DIFFERENT_IMAGE)
assert hash_original != hash_tampered
assert hash_original != hash_different
assert hash_tampered != hash_different
def test_extract_metadata(setup_test_images):
"""Test metadata extraction from images"""
# Test basic metadata extraction
meta = extract_metadata(ORIGINAL_IMAGE)
assert 'Format' in meta
assert 'Dimensions' in meta
assert 'File Size' in meta
# Verify dimensions are correct
assert meta['Dimensions'] == (100, 100)
# Test with non-existent image (should raise SystemExit)
with pytest.raises(SystemExit):
extract_metadata(NON_EXISTENT_IMAGE)
def test_compare_metadata():
"""Test metadata comparison between images"""
meta1 = extract_metadata(ORIGINAL_IMAGE)
assert compare_metadata(meta1, meta1) is True
def test_perform_pixel_comparison(capsys):
"""Test pixel-level comparison between images"""
# Test with identical images
perform_pixel_comparison(ORIGINAL_IMAGE, ORIGINAL_IMAGE)
captured = capsys.readouterr()
assert "No visual differences detected" in captured.out
# Test with tampered image (single pixel changed)
perform_pixel_comparison(ORIGINAL_IMAGE, TAMPERED_IMAGE)
captured = capsys.readouterr()
assert "Modified Pixels" in captured.out
assert "(47, 48)" in captured.out # Should find our changed pixel
# Test with different sized images
small_img = os.path.join(TEST_IMAGES_DIR, 'small.jpg')
Image.new('RGB', (50, 50), color='red').save(small_img)
perform_pixel_comparison(ORIGINAL_IMAGE, small_img)
captured = capsys.readouterr()
assert "different dimensions" in captured.out
os.remove(small_img)
def test_hash_algorithm_consistency():
"""Verify the hash algorithm produces expected results"""
test_file = os.path.join(TEST_IMAGES_DIR, 'hash_test.txt')
with open(test_file, 'w') as f:
f.write("PixelGuard test string")
# Manually compute expected hash
hasher = hashlib.sha256()
with open(test_file, 'rb') as f:
while chunk := f.read(4096):
hasher.update(chunk)
expected_hash = hasher.hexdigest()
# Compare with function output
assert compute_image_hash(test_file) == expected_hash
os.remove(test_file)
def test_metadata_fields_completeness(setup_test_images):
"""Verify all expected metadata fields are present"""
meta = extract_metadata(ORIGINAL_IMAGE)
required_fields = ['Format', 'Dimensions', 'File Size']
for field in required_fields:
assert field in meta
assert isinstance(meta['Dimensions'], tuple)
assert isinstance(meta['File Size'], int)
def test_pixel_comparison_limits(setup_test_images, capsys):
"""Test the max_pixels and display_limit parameters"""
# Should only check up to max_pixels (default 100)
perform_pixel_comparison(ORIGINAL_IMAGE, DIFFERENT_IMAGE, max_pixels=5)
captured = capsys.readouterr()
assert "checked up to 5" in captured.out
# Should only display up to display_limit (default 20)
perform_pixel_comparison(ORIGINAL_IMAGE, DIFFERENT_IMAGE, display_limit=3)
captured = capsys.readouterr()
assert "Sample modified pixels" in captured.out
assert "..." in captured.out # Indicates truncation