"""Contract boundaries for decoded pixels using two small synthetic PNG files.""" import hashlib from pathlib import Path import struct import sys import zlib ROOT = Path(__file__).resolve().parents[1] sys.path[:0] = [str(ROOT), str(ROOT / "tests")] from client import Client from scripts.operator import settings def chunk(tag, data): return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data)) def rgba_png(width, height): stream = zlib.compressobj(1) row = b"\0" + b"\x80\x70\x60\x60" * width data = b"".join(stream.compress(row) for _ in range(height)) + stream.flush() header = struct.pack(">IIBBBBB", width, height, 8, 6, 0, 0, 0) return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", data) + chunk(b"IEND", b"") def main(): port = settings().get("HTTPS_PORT", "8443") client = Client(f"https://localhost:{port}", str(ROOT / "secrets/trust/edge.crt"), timeout=15) alice, _, _, chat = client.bootstrap() upload = f"/api/v1/chats/{chat['id']}/attachments?filename=large.png" headers = {"Content-Type": "image/png"} client.call("POST", upload, 413, actor=alice, raw=rgba_png(8000, 5001), headers=headers) raw = rgba_png(8000, 5000) received = client.call("POST", upload, 202, actor=alice, raw=raw, headers=headers, schema="Attachment") ready = client.wait_ready(alice, received["json"]) assert ready["sha256"] == hashlib.sha256(raw).hexdigest() assert client.call("GET", f"/api/v1/attachments/{ready['id']}/content", actor=alice)["bytes"] == raw print("PASS 40,008,000 decoded pixels rejected; 40,000,000 decoded pixels processed, bytes preserved") if __name__ == "__main__": main()