Co-authored-by: Югай Артур Рустамович <f1ekserr@yandex.ru>
57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
import io
|
|
import os
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
from urllib.parse import urlsplit
|
|
import zlib
|
|
|
|
from PIL import Image
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client as PublicClient
|
|
from scripts import lab
|
|
|
|
|
|
def project_name():
|
|
name = os.environ.get("LAB4_PROJECT", lab.settings().get("COMPOSE_PROJECT_NAME", "kuznetsov-lab4"))
|
|
if name != "kuznetsov-lab4" and not name.startswith("kuznetsov-lab4-"):
|
|
raise RuntimeError("Tests only operate on Kuznetsov lab4 projects")
|
|
return name
|
|
|
|
|
|
class Client(PublicClient):
|
|
def __init__(self, base_url=None, ca_file=None, timeout=10):
|
|
project_name()
|
|
port = int(os.environ.get("LAB4_PORT", lab.settings().get("HTTP_PORT", "18104")))
|
|
target = base_url or os.environ.get("BASE_URL") or f"http://127.0.0.1:{port}"
|
|
parsed = urlsplit(target)
|
|
if parsed.scheme != "http" or parsed.hostname not in ("127.0.0.1", "localhost") or parsed.port != port:
|
|
raise RuntimeError("BASE_URL must match this lab's local port")
|
|
super().__init__(target, ca_file=ca_file, timeout=timeout)
|
|
|
|
|
|
def image_bytes(kind="PNG", size=(32, 24), mode="RGB"):
|
|
with Image.new(mode, size, (35, 120, 200, 255) if mode == "RGBA" else (35, 120, 200)) as image:
|
|
output = io.BytesIO()
|
|
image.save(output, format=kind)
|
|
return output.getvalue()
|
|
|
|
|
|
def chunk(kind, data):
|
|
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xffffffff)
|
|
|
|
|
|
def padded_png(size):
|
|
base = image_bytes()
|
|
padding = b"p" * (size - len(base) - 12)
|
|
return base[:-12] + chunk(b"raNd", padding) + base[-12:]
|
|
|
|
|
|
def oversized_pixels():
|
|
header = struct.pack(">IIBBBBB", 8000, 5001, 8, 2, 0, 0, 0)
|
|
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IEND", b"")
|