Files
mtusi2026lab4/student-tests/test_attachments.py
T

269 lines
15 KiB
Python

"""Вложения через балансировщик: корректные файлы, лимиты, битые файлы, подмена MIME, доступ.
Только HTTP, Docker не нужен. Каждый негативный случай проверяет точный код ответа:
пользовательская ошибка не должна становиться 500.
"""
import hashlib
import http.client
import socket
import urllib.parse
import uuid
import zlib
import images
from common import BASE_URL, call, check, new_chat, new_key, new_user, post_message, read_history, run_tests, upload
URL = urllib.parse.urlsplit(BASE_URL)
def setup():
alice, bob, eve = new_user("alice"), new_user("bob"), new_user("eve")
return alice, bob, eve, new_chat(alice, bob)
def sha(data):
return hashlib.sha256(data).hexdigest()
def check_error(reply, code):
check(reply.json["error"]["code"] == code, f"expected error code {code}, got {reply.json['error']['code']}")
def test_png_and_jpeg_roundtrip():
alice, bob, _, chat = setup()
for data, ctype, name in ((images.sample_png(), "image/png", "sample.png"),
(images.sample_jpeg(), "image/jpeg", "sample.jpg"),
(images.progressive_jpeg(), "image/jpeg", "progressive.jpg"),
(images.png(33, 17, interlace=1), "image/png", "interlaced.png"),
(images.png(1, 1, gray=True), "image/png", "1x1.png")):
att = upload(alice, chat["id"], data, ctype, name).json
check(att["status"] == "ready" and att["variants"] == [] and att["error_code"] is None, "not ready")
check(att["sha256"] == sha(data) and att["size_bytes"] == len(data), "checksum/size differ")
check(att["content_type"] == ctype and att["filename"] == name, "metadata differs")
meta = call("GET", f"/api/v1/attachments/{att['id']}", 200, token=bob["token"]).json
check(meta == att, "GET metadata differs from POST response")
got = call("GET", f"/api/v1/attachments/{att['id']}/content", 200, token=bob["token"])
check(got.data == data, "downloaded bytes differ")
check(got.headers["content-type"] == ctype, "wrong Content-Type on download")
check(got.headers.get("content-length") == str(len(data)), "wrong Content-Length")
check(got.headers.get("x-content-type-options") == "nosniff", "nosniff is missing")
print(f"{name}: {len(data)} bytes, sha256 {att['sha256'][:12]}..., downloaded by the other member, same bytes")
print("PASS: PNG, JPEG, progressive JPEG, interlaced PNG and 1x1 PNG are stored and returned byte for byte")
def test_size_limit_exact_boundary():
alice, _, _, chat = setup()
exact = images.png(64, 64, pad_to=images.MAX_UPLOAD_BYTES)
check(len(exact) == images.MAX_UPLOAD_BYTES, "fixture must be exactly 10 MiB")
att = upload(alice, chat["id"], exact).json
check(att["size_bytes"] == images.MAX_UPLOAD_BYTES and att["sha256"] == sha(exact), "10 MiB file changed")
print(f"exactly {len(exact)} bytes (10 MiB) -> 201")
over = images.png(64, 64, pad_to=images.MAX_UPLOAD_BYTES + 1)
reply = upload(alice, chat["id"], over, expected=413)
check_error(reply, "payload_too_large")
print(f"{len(over)} bytes (10 MiB + 1) with Content-Length -> 413 before reading the body")
print("PASS: the 10 MiB limit is exact")
def chunked_upload(user, chat_id, total, piece=64 * 1024):
"""Тело без Content-Length (Transfer-Encoding: chunked): сервер узнаёт размер только при чтении."""
conn = http.client.HTTPConnection(URL.hostname, URL.port or 80, timeout=60)
def body():
sent = 0
head = images.sample_png()[:64]
yield head
sent += len(head)
while sent < total:
part = bytes(min(piece, total - sent))
sent += len(part)
yield part
try:
conn.request("POST", f"/api/v1/chats/{chat_id}/attachments?filename=big.png", body=body(),
headers={"Content-Type": "image/png", "Authorization": "Bearer " + user["token"]},
encode_chunked=True)
reply = conn.getresponse()
return reply.status, reply.read()
finally:
conn.close()
def test_size_limit_streaming():
alice, _, _, chat = setup()
status, data = chunked_upload(alice, chat["id"], 12 * 1024 * 1024)
check(status == 413 and b"payload_too_large" in data, f"chunked 12 MiB: expected 413, got {status}")
print("12 MiB without Content-Length (chunked) -> 413 as soon as the 10 MiB limit is passed")
call("GET", "/health/ready", 200)
print("PASS: oversized streamed body is cut off, the API keeps working")
def test_pixel_limit():
alice, _, _, chat = setup()
at_limit = images.png(8000, 5000, gray=True) # ровно 40 000 000 пикселей
att = upload(alice, chat["id"], at_limit, filename="40mpx.png").json
print(f"8000x5000 = 40 000 000 pixels ({len(at_limit)} bytes compressed) -> 201 {att['id'][:8]}")
over = images.png(8001, 5000, gray=True)
check_error(upload(alice, chat["id"], over, filename="over.png", expected=413), "payload_too_large")
print(f"8001x5000 = 40 005 000 pixels ({len(over)} bytes) -> 413")
bomb = images.png(100_000, 100_000, idat=b"x\x9c\x03\x00\x00\x00\x00\x01")
check_error(upload(alice, chat["id"], bomb, filename="bomb.png", expected=413), "payload_too_large")
print(f"header claims 100000x100000 in {len(bomb)} bytes -> 413 from the header, nothing is decoded")
print("PASS: pixel limit is checked before decompression")
def test_damaged_images():
alice, _, _, chat = setup()
png, jpeg = images.sample_png(), images.sample_jpeg()
cases = [
("PNG cut in half", png[:len(png) // 2], "image/png"),
("PNG without IEND", png[:-12], "image/png"),
("PNG with a flipped byte in IDAT (CRC mismatch)", images.flip_byte(png, len(png) // 2), "image/png"),
("PNG with garbage after IEND", png + b"garbage", "image/png"),
("PNG signature only", png[:8], "image/png"),
("PNG with less pixel data than the header says", images.png(10, 10, idat=zlib.compress(b"\x00" * 5)), "image/png"),
("JPEG cut in half", jpeg[:len(jpeg) // 2], "image/jpeg"),
("JPEG header only", jpeg[:20], "image/jpeg"),
("random bytes", uuid.uuid4().bytes * 100, "image/png"),
("plain text", "это не картинка".encode(), "image/jpeg"),
]
for name, data, ctype in cases:
reply = upload(alice, chat["id"], data, ctype, "broken.bin", expected=415)
check_error(reply, "unsupported_media_type")
print(f"{name} ({len(data)} bytes, {ctype}) -> 415")
check_error(upload(alice, chat["id"], b"", expected=400), "invalid_request")
print("empty body -> 400")
print("PASS: damaged and foreign files are rejected with 415, never 500")
def test_mime_substitution():
alice, _, _, chat = setup()
cases = [
("PNG bytes sent as image/jpeg", images.sample_png(), "image/jpeg"),
("JPEG bytes sent as image/png", images.sample_jpeg(), "image/png"),
("GIF bytes sent as image/png", images.GIF, "image/png"),
("PNG sent as image/gif", images.sample_png(), "image/gif"),
("PNG sent as text/html", images.sample_png(), "text/html"),
("PNG sent as application/octet-stream", images.sample_png(), "application/octet-stream"),
("PNG sent as multipart/form-data", images.sample_png(), "multipart/form-data; boundary=x"),
]
for name, data, ctype in cases:
check_error(upload(alice, chat["id"], data, ctype, expected=415), "unsupported_media_type")
print(f"{name} -> 415")
att = upload(alice, chat["id"], images.sample_png(), "IMAGE/PNG; charset=binary").json
check(att["content_type"] == "image/png", "content type must be normalised")
print("PNG sent as 'IMAGE/PNG; charset=binary' -> 201, stored as image/png")
print("PASS: the type is taken from the content; a declared type that does not match is refused")
def test_filenames():
alice, _, _, chat = setup()
bad = ["../../etc/passwd", "a/b.png", "a\\b.png", "a\x00b.png", "line\nbreak.png", "tab\t.png",
"bell\x07.png", "del\x7f.png", "", " ", ".", "..", "x" * 256]
for name in bad:
check_error(upload(alice, chat["id"], images.sample_png(), filename=name, expected=400), "invalid_request")
print(f"{len(bad)} dangerous or invalid names (path, slashes, NUL, control characters, empty, 256 chars) -> 400")
check_error(upload(alice, chat["id"], images.sample_png(), filename=None, expected=400), "invalid_request")
print("no filename -> 400")
for name in ("фото отпуска 😀.png", "x" * 255, "..hidden.png", "a b%20c?.png"):
att = upload(alice, chat["id"], images.sample_png(), filename=name).json
check(att["filename"] == name, "display name changed")
got = call("GET", f"/api/v1/attachments/{att['id']}/content", 200, token=alice["token"])
check("filename*=UTF-8''" in got.headers.get("content-disposition", ""), "Content-Disposition is not encoded")
print("Unicode, 255 chars, leading dots, %20 and ? are kept as display names only")
print("PASS: filename never becomes a path; dangerous names are refused")
def test_access_control():
alice, bob, eve, chat = setup()
att = upload(alice, chat["id"], images.sample_png()).json
for suffix in ("", "/content"):
reply = call("GET", f"/api/v1/attachments/{att['id']}{suffix}", 404, token=eve["token"])
check_error(reply, "not_found")
call("GET", f"/api/v1/attachments/{att['id']}{suffix}", 401)
call("GET", f"/api/v1/attachments/{uuid.uuid4()}{suffix}", 404, token=alice["token"])
call("GET", f"/api/v1/attachments/not-a-uuid{suffix}", 400, token=alice["token"])
print("outsider -> 404 for metadata and bytes (same as a non-existent id); no token -> 401; bad UUID -> 400")
check_error(upload(eve, chat["id"], images.sample_png(), expected=404), "not_found")
check_error(upload(alice, str(uuid.uuid4()), images.sample_png(), expected=404), "not_found")
upload(None, chat["id"], images.sample_png(), expected=401)
print("upload into someone else's / unknown chat -> 404, without token -> 401")
call("GET", f"/api/v1/attachments/{att['id']}/content?variant=thumbnail", 400, token=bob["token"])
call("GET", f"/api/v1/attachments/{att['id']}/content?variant=original", 200, token=bob["token"])
print("variant=thumbnail -> 400 (not in the lab4 contract), variant=original -> 200")
print("PASS: only chat members can see an attachment")
def test_message_attachments():
alice, bob, eve, chat = setup()
first = upload(alice, chat["id"], images.sample_png()).json
second = upload(bob, chat["id"], images.sample_jpeg(), "image/jpeg", "b.jpg").json
msg = post_message(alice, chat["id"], "", [second["id"], first["id"]]).json
check(msg["attachment_ids"] == [second["id"], first["id"]] and msg["text"] == "", "order or text changed")
print("text='' with 2 attachments -> 201, order kept; any member may attach a file uploaded by another member")
plain = post_message(bob, chat["id"], "без вложений").json
check(plain["attachment_ids"] == [], "default attachment_ids must be []")
reused = post_message(bob, chat["id"], "то же вложение ещё раз", [first["id"]]).json
history, _ = read_history(bob, chat["id"])
check(history == [msg, plain, reused], "history differs from the confirmed messages")
print("history returns attachment_ids for every message, [] for a message without files; reuse is allowed")
other_chat = new_chat(alice, eve)
foreign = upload(alice, other_chat["id"], images.sample_png()).json
for ids, what in (([foreign["id"]], "attachment of another chat (author is a member of both)"),
([first["id"], foreign["id"]], "own + foreign attachment"),
([str(uuid.uuid4())], "unknown attachment_id")):
check_error(post_message(alice, chat["id"], "x", ids, expected=404), "not_found")
print(f"{what} -> 404")
check_error(post_message(eve, chat["id"], "x", [first["id"]], expected=404), "not_found")
print("outsider referencing an attachment of the chat -> 404")
for ids, text, what in (([first["id"], first["id"]], "x", "duplicate ids"),
([str(uuid.uuid4()) for _ in range(11)], "x", "11 ids"),
(["not-a-uuid"], "x", "bad UUID"),
([], "", "empty text without attachments"),
([], " ", "whitespace text without attachments")):
check_error(post_message(alice, chat["id"], text, ids, expected=400), "invalid_request")
print(f"{what} -> 400")
history_after, _ = read_history(bob, chat["id"])
check(history_after == history, "a rejected message was stored")
print("PASS: only attachments of the same chat can be linked; rejected requests change nothing")
def test_idempotency_with_attachments():
alice, bob, _, chat = setup()
att = upload(alice, chat["id"], images.sample_png()).json
other = upload(alice, chat["id"], images.sample_jpeg(), "image/jpeg").json
key = new_key()
first = post_message(alice, chat["id"], "фото", [att["id"]], key=key).json
again = post_message(alice, chat["id"], "фото", [att["id"]], key=key).json
check(again == first, "replay returned a different message")
check_error(post_message(alice, chat["id"], "фото", [other["id"]], key=key, expected=409), "conflict")
check_error(post_message(alice, chat["id"], "фото", [att["id"], other["id"]], key=key, expected=409), "conflict")
key2 = new_key()
plain = post_message(alice, chat["id"], "текст", None, key=key2).json
check(post_message(alice, chat["id"], "текст", [], key=key2).json == plain, "[] must equal a missing field")
history, _ = read_history(bob, chat["id"])
check(len(history) == 2, "replays created extra messages")
print("same key + same attachments -> same 201; other attachments -> 409; missing field == []")
print("PASS: Idempotency-Key covers attachment_ids")
def test_aborted_upload():
alice, _, _, chat = setup()
sock = socket.create_connection((URL.hostname, URL.port or 80), timeout=10)
head = (f"POST /api/v1/chats/{chat['id']}/attachments?filename=cut.png HTTP/1.1\r\nHost: {URL.hostname}\r\n"
f"Authorization: Bearer {alice['token']}\r\nContent-Type: image/png\r\nContent-Length: 5000000\r\n\r\n")
sock.sendall(head.encode() + images.sample_png() + bytes(1_000_000))
sock.close() # клиент пропал на середине тела
print("sent 1.1 MB of a declared 5 MB body and closed the connection")
att = upload(alice, chat["id"], images.sample_png()).json
call("GET", f"/api/v1/attachments/{att['id']}/content", 200, token=alice["token"])
print("the API keeps serving uploads; nothing of the aborted request is visible "
"(DB/S3 side is checked by test_consistency.py)")
print("PASS: an interrupted upload is dropped")
if __name__ == "__main__":
run_tests([test_png_and_jpeg_roundtrip, test_size_limit_exact_boundary, test_size_limit_streaming,
test_pixel_limit, test_damaged_images, test_mime_substitution, test_filenames, test_access_control,
test_message_attachments, test_idempotency_with_attachments, test_aborted_upload])