forked from mtusi2026/mtusi2026lab4
27 lines
1.1 KiB
Python
27 lines
1.1 KiB
Python
import argparse
|
|
import json
|
|
import struct
|
|
import sys
|
|
import zlib
|
|
from pathlib import Path
|
|
sys.path.insert(0,str(Path.cwd()/"tests"))
|
|
from client import Client
|
|
parser = argparse.ArgumentParser(description="Check the decoded pixel limit")
|
|
parser.add_argument("--base-url", required=True)
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
p = args.output
|
|
p.mkdir(parents=True, exist_ok=True)
|
|
def chunk(name,data):
|
|
return struct.pack(">I",len(data))+name+data+struct.pack(">I",zlib.crc32(name+data))
|
|
w,h=6500,6200
|
|
obj=zlib.compressobj()
|
|
row=bytes(w+1)
|
|
blob=b"".join(obj.compress(row) for _ in range(h))+obj.flush()
|
|
data=b"\x89PNG\r\n\x1a\n"+chunk(b"IHDR",struct.pack(">IIBBBBB",w,h,8,0,0,0,0))+chunk(b"IDAT",blob)+chunk(b"IEND",b"")
|
|
c=Client(args.base_url)
|
|
alice,_,_,chat=c.bootstrap()
|
|
response=c.call("POST","/api/v1/chats/"+chat["id"]+"/attachments?filename=large.png",413,actor=alice,raw=data,headers={"Content-Type":"image/png"})
|
|
assert response["json"]["error"]["code"]=="payload_too_large"
|
|
(p/"pixel-limit.json").write_text(json.dumps({"width":w,"height":h,"encoded_bytes":len(data),"upload_status":response["status"]},indent=2)+"\n")
|