""" Fuzz testing with random/malformed inputs. """ import pytest import numpy as np from PIL import Image from io import BytesIO import random def test_random_byte_sequence(): """Test completely random bytes don't crash detector.""" from backend.services.statistical_detector import StatisticalDetector random_bytes = bytes(np.random.randint(0, 256, 1000)) with pytest.raises((ValueError, IOError, Image.UnidentifiedImageError, Exception)): detector = StatisticalDetector(random_bytes, "random.dat") def test_partial_image_data(): """Test truncated image file.""" img = Image.new('RGB', (100, 100), color='red') buffer = BytesIO() img.save(buffer, format='PNG') full_bytes = buffer.getvalue() partial_bytes = full_bytes[:len(full_bytes)//2] from backend.services.statistical_detector import StatisticalDetector with pytest.raises((ValueError, IOError, Image.UnidentifiedImageError, Exception)): detector = StatisticalDetector(partial_bytes, "partial.png") def test_malformed_png_header(): """Test PNG with corrupted header.""" img = Image.new('RGB', (100, 100), color='blue') buffer = BytesIO() img.save(buffer, format='PNG') valid_bytes = bytearray(buffer.getvalue()) valid_bytes[0:4] = b'XXXX' from backend.services.statistical_detector import StatisticalDetector with pytest.raises((ValueError, IOError, Image.UnidentifiedImageError, Exception)): detector = StatisticalDetector(bytes(valid_bytes), "corrupted.png") def test_extreme_pixel_values(): """Test image with all extreme values (0 or 255).""" from backend.services.statistical_detector import StatisticalDetector extreme = np.random.choice([0, 255], size=(100, 100, 3), p=[0.5, 0.5]).astype(np.uint8) extreme_img = Image.fromarray(extreme) extreme_buffer = BytesIO() extreme_img.save(extreme_buffer, format='PNG') extreme_bytes = extreme_buffer.getvalue() detector = StatisticalDetector(extreme_bytes, "extreme.png") report = detector.detect() assert "ai_probability" in report def test_checkerboard_pattern(): """Test perfectly regular checkerboard (edge case).""" from backend.services.statistical_detector import StatisticalDetector checker = np.zeros((100, 100, 3), dtype=np.uint8) checker[::2, ::2] = 255 checker[1::2, 1::2] = 255 checker_img = Image.fromarray(checker) checker_buffer = BytesIO() checker_img.save(checker_buffer, format='PNG') checker_bytes = checker_buffer.getvalue() detector = StatisticalDetector(checker_bytes, "checker.png") report = detector.detect() assert 0 <= report["ai_probability"] <= 1 def test_gradient_image(): """Test smooth gradient (minimal high-frequency content).""" from backend.services.statistical_detector import StatisticalDetector gradient = np.linspace(0, 255, 100*100).reshape(100, 100).astype(np.uint8) gradient_rgb = np.stack([gradient, gradient, gradient], axis=2) grad_img = Image.fromarray(gradient_rgb) grad_buffer = BytesIO() grad_img.save(grad_buffer, format='PNG') grad_bytes = grad_buffer.getvalue() detector = StatisticalDetector(grad_bytes, "gradient.png") report = detector.detect() assert "ai_probability" in report def test_random_valid_images(): """Fuzz test with random valid images (use safe dimensions).""" from backend.services.statistical_detector import StatisticalDetector for i in range(5): # Use safer dimensions (min 100, avoid very small heights) width = random.randint(100, 200) height = random.randint(100, 200) pixels = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8) img = Image.fromarray(pixels) buffer = BytesIO() img.save(buffer, format='PNG') img_bytes = buffer.getvalue() try: detector = StatisticalDetector(img_bytes, f"random_{width}x{height}.png") report = detector.detect() assert report["total_signals"] == 19 # 16 base + 3 statistical = 19 (StatisticalDetector only) assert 0 <= report["ai_probability"] <= 1 except (TypeError, ValueError): # Skip if random dimensions cause FFT issues pytest.skip(f"Random image {width}x{height} caused FFT edge case") break