Files
mtusi2026lab4/tests/attachment_boundaries.py
T

100 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Black-box attachment validation and exact contract-boundary checks."""
import os
import struct
import zlib
from attachment_memory import cleanup, noisy_png, png_chunk
from client import Client, check, run
MAX_BYTES = 10 * 1024 * 1024
def exact_size_png(sample, target_size=MAX_BYTES):
check(sample.endswith(png_chunk(b"IEND", b"")), "sample PNG has no final IEND")
prefix, iend = sample[:-12], sample[-12:]
filler_size = target_size - len(prefix) - len(iend) - 12
check(filler_size >= 0, "sample PNG is larger than boundary")
result = prefix + png_chunk(b"ruSt", bytes(filler_size)) + iend
check(len(result) == target_size, "exact-size PNG construction failed")
return result
def exact_pixel_png(width=10_000, height=4_000):
compressor = zlib.compressobj(level=9)
row = bytes(1 + width * 3)
parts = []
for _ in range(height):
chunk = compressor.compress(row)
if chunk:
parts.append(chunk)
parts.append(compressor.flush())
return (
b"\x89PNG\r\n\x1a\n"
+ png_chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
+ png_chunk(b"IDAT", b"".join(parts))
+ png_chunk(b"IEND", b"")
)
def config_only_png(width, height):
return (
b"\x89PNG\r\n\x1a\n"
+ png_chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
)
def upload(client, actor, chat, filename, payload, expected, schema=None):
return client.call(
"POST",
"/api/v1/chats/%s/attachments?filename=%s" % (chat["id"], filename),
expected,
actor=actor,
raw=payload,
headers={"Content-Type": "image/png"},
schema=schema,
)
def main():
client = Client(os.environ.get("BASE_URL"), None, timeout=30)
alice, bob, eve, chat = client.bootstrap()
users = [alice["id"], bob["id"], eve["id"]]
sample = open(os.path.join(os.path.dirname(__file__), "fixtures", "sample.png"), "rb").read()
try:
boundary = exact_size_png(sample)
response = upload(client, alice, chat, "exact-10mib.png", boundary, 201, "Attachment")
attachment = response["json"]
check(attachment["size_bytes"] == MAX_BYTES, "10 MiB boundary size changed")
downloaded = client.call(
"GET", "/api/v1/attachments/%s/content" % attachment["id"],
200, actor=alice,
)["bytes"]
check(downloaded == boundary, "10 MiB original changed after round trip")
pixels = exact_pixel_png()
upload(client, alice, chat, "exact-40m-pixels.png", pixels, 201, "Attachment")
upload(client, alice, chat, "over-pixels.png", config_only_png(20_000, 10_000), 413)
upload(client, alice, chat, "truncated.png", sample[:33], 415)
corrupted = noisy_png()
check(len(corrupted) > 70_000, "corruption fixture is too small")
corrupted = bytearray(corrupted)
corrupted[70_000] ^= 0xff
upload(client, alice, chat, "corrupt-after-prefix.png", bytes(corrupted), 415)
upload(client, alice, chat, "%20%20%20", sample, 400)
upload(client, alice, chat, "c1%C2%85.png", sample, 400)
upload(client, alice, chat, "invalid-%FF.png", sample, 400)
print("PASS: exact byte/pixel boundaries, complete decode, and filename validation are correct.")
finally:
cleanup(chat["id"], users)
if __name__ == "__main__":
run(main)