#!/usr/bin/env python3 import base64 import json import os import socket import subprocess import sys import threading import time import urllib.error import urllib.request import uuid from pathlib import Path from urllib.parse import quote ROOT = Path(__file__).resolve().parents[1] EVIDENCE = ROOT / "evidence" SECRET = "SECRET_MESSAGE_DO_NOT_LOG_7f3a" CLIENTS = 10 PER_CLIENT = 10 class Failure(AssertionError): pass def check(condition, message): if not condition: raise Failure(message) def free_port(): with socket.socket() as sock: sock.bind(("127.0.0.1", 0)) return sock.getsockname()[1] class Server: def __init__(self, port): self.base = "http://127.0.0.1:%d" % port env = os.environ.copy() env["PYTHONPATH"] = str(ROOT / "src") env["PYTHONUNBUFFERED"] = "1" env["APP_HOST"] = "127.0.0.1" env["APP_PORT"] = str(port) env["APP_ENV"] = "lab" env["DATABASE_URL"] = host_secret("host_database_url") env["REDIS_URL"] = host_secret("host_cache_url") env["SESSION_TTL_SECONDS"] = "3600" env.pop("DATABASE_URL_FILE", None) env.pop("CACHE_URL_FILE", None) self.logs = [] self.proc = subprocess.Popen( [sys.executable, "-m", "app"], cwd=str(ROOT), env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace", ) self._reader = threading.Thread(target=self._read, daemon=True) self._reader.start() def _read(self): assert self.proc.stdout is not None for line in self.proc.stdout: self.logs.append(line) def wait_ready(self): deadline = time.monotonic() + 40 last = "" while time.monotonic() < deadline: if self.proc.poll() is not None: raise Failure("server exited early:\n" + "".join(self.logs[-40:])) try: call(self.base, "GET", "/health/ready", 200) return except Failure as exc: last = str(exc) except (urllib.error.URLError, TimeoutError, OSError) as exc: last = type(exc).__name__ time.sleep(0.1) raise Failure("server did not become ready: " + last + "\n" + "".join(self.logs[-40:])) def stop(self): if self.proc.poll() is None: self.proc.terminate() try: self.proc.wait(timeout=8) except subprocess.TimeoutExpired: self.proc.kill() self.proc.wait(timeout=5) self._reader.join(timeout=2) def output(self): return "".join(self.logs) def host_secret(name): path = ROOT / "secrets" / name if not path.is_file(): raise Failure("missing secrets/%s; run scripts/bootstrap.ps1 or scripts/bootstrap.sh" % name) value = path.read_text(encoding="utf-8").strip() if not value: raise Failure("empty secrets/%s" % name) return value def call( base, method, path, expected, body=None, token=None, user_id=None, authorization=None, raw=None, content_type="application/json", timeout=20, headers=None, ): extra_headers = headers or {} headers = {"Accept": "application/json"} headers.update(extra_headers) data = None if raw is not None: data = raw headers["Content-Type"] = content_type elif body is not None: data = json.dumps(body, ensure_ascii=False).encode("utf-8") headers["Content-Type"] = content_type if authorization: headers["Authorization"] = authorization elif token: headers["Authorization"] = "Bearer " + token if user_id: headers["X-User-Id"] = user_id request = urllib.request.Request(base + path, data=data, headers=headers, method=method) try: response = urllib.request.urlopen(request, timeout=timeout) except urllib.error.HTTPError as error: response = error except (urllib.error.URLError, TimeoutError, OSError) as exc: raise Failure(method + " " + path.split("?")[0] + " failed: " + type(exc).__name__) from None with response: status = response.status payload = response.read() header_id = response.headers.get("X-Request-Id") instance_id = response.headers.get("X-Instance-Id") content_type_value = response.headers.get_content_type() allowed = [expected] if isinstance(expected, int) else list(expected) check(status in allowed, "%s %s: expected %s, got %s" % (method, path.split("?")[0], allowed, status)) check(header_id and header_id.strip() and "," not in header_id, "missing or duplicated X-Request-Id") check(instance_id and instance_id.strip() and "," not in instance_id, "missing or duplicated X-Instance-Id") parsed = None if payload: check(content_type_value == "application/json", "expected application/json") try: parsed = json.loads(payload) except json.JSONDecodeError as exc: raise Failure("response is not JSON") from exc if status >= 400: check(isinstance(parsed, dict) and set(parsed) == {"error"}, "error envelope") error = parsed["error"] check(set(error) == {"code", "message", "request_id"}, "error fields") check(error["request_id"] == header_id, "request_id differs from header") check(error["message"] and "Traceback" not in error["message"], "error message leaked internals") return {"status": status, "json": parsed, "request_id": header_id, "instance_id": instance_id} def send_message(base, chat_id, token, text, expected=201, key=None): headers = {"Idempotency-Key": key or uuid.uuid4().hex} return call( base, "POST", "/api/v1/chats/%s/messages" % chat_id, expected, token=token, body={"text": text}, headers=headers, ) def create_user(base, label): username = ("u_%s_%s" % (label, uuid.uuid4().hex[:10])).lower() password = "T3st-" + uuid.uuid4().hex body = {"username": username, "display_name": "Студент " + label, "password": password} created = call(base, "POST", "/api/v1/users", 201, body=body)["json"] check(created["username"] == username, "username changed") check("password" not in created, "password leaked in user response") raw = base64.b64encode(("%s:%s" % (username, password)).encode("utf-8")).decode("ascii") session = call(base, "POST", "/api/v1/auth/sessions", 201, authorization="Basic " + raw)["json"] check(session["token_type"] == "Bearer", "token type") check(session["user"]["id"] == created["id"], "session user") check("password" not in session["user"], "password leaked in session") created["token"] = session["token"] created["instance_id"] = session.get("instance_id") return created def replace_cursor(token, fields): padding = "=" * (-len(token) % 4) data = json.loads(base64.urlsafe_b64decode(token + padding)) data.update(fields) raw = json.dumps(data, separators=(",", ":"), sort_keys=True).encode("ascii") return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=") def test_invalid_json(base): broken = call(base, "POST", "/api/v1/users", 400, raw=b"{") check(broken["json"]["error"]["code"] == "invalid_request", "malformed JSON code") extra = call( base, "POST", "/api/v1/users", 400, body={"username": "abc_user", "display_name": "Имя", "extra": True}, ) check(extra["json"]["error"]["code"] == "invalid_request", "extra field code") bad_utf = call(base, "POST", "/api/v1/users", 400, raw=b"\xff") check(bad_utf["json"]["error"]["code"] == "invalid_request", "invalid utf-8 code") alice = create_user(base, "utf") chat = call( base, "POST", "/api/v1/chats", 201, token=alice["token"], body={"title": "UTF", "member_ids": [alice["id"]]}, )["json"] bad_message = call( base, "POST", "/api/v1/chats/%s/messages" % chat["id"], 400, token=alice["token"], raw=b"\xff", headers={"Idempotency-Key": uuid.uuid4().hex}, ) check(bad_message["json"]["error"]["code"] == "invalid_request", "invalid utf-8 message code") print("PASS: invalid JSON") def test_duplicate_username(base): first = create_user(base, "dup") again = call( base, "POST", "/api/v1/users", 409, body={ "username": first["username"], "display_name": "Другой", "password": "T3st-" + uuid.uuid4().hex, }, ) check(again["json"]["error"]["code"] == "conflict", "duplicate username code") me = call(base, "GET", "/api/v1/users/me", 200, token=first["token"])["json"] check(me["id"] == first["id"], "first user disappeared after duplicate") print("PASS: duplicate username") def test_empty_and_foreign(base): alice = create_user(base, "alice") bob = create_user(base, "bob") chat = call( base, "POST", "/api/v1/chats", 201, token=alice["token"], body={"title": "Только Алиса", "member_ids": [alice["id"]]}, )["json"] path = "/api/v1/chats/" + chat["id"] for text in ("", " ", "\n\t"): denied = call( base, "POST", path + "/messages", 400, token=alice["token"], body={"text": text}, headers={"Idempotency-Key": uuid.uuid4().hex}, ) check(denied["json"]["error"]["code"] == "invalid_request", "empty message code") for method, target, payload in ( ("GET", path, None), ("GET", path + "/messages", None), ("POST", path + "/messages", {"text": "чужое"}), ): extra = {"Idempotency-Key": uuid.uuid4().hex} if method == "POST" else None hidden = call(base, method, target, 404, token=bob["token"], body=payload, headers=extra) check(hidden["json"]["error"]["code"] == "not_found", "foreign chat code") check(chat["title"] not in hidden["json"]["error"]["message"], "foreign chat leaked title") listed = call(base, "GET", "/api/v1/chats?limit=100", 200, token=bob["token"])["json"] check(all(item["id"] != chat["id"] for item in listed["items"]), "foreign chat listed") missing = call(base, "GET", "/api/v1/chats/" + str(uuid.uuid4()), 404, token=bob["token"]) check(missing["json"]["error"]["code"] == "not_found", "missing chat code") bad_limit = call(base, "GET", path + "/messages?limit=0", 400, token=alice["token"]) check(bad_limit["json"]["error"]["code"] == "invalid_request", "limit=0 code") print("PASS: empty message and foreign chat") def test_pagination(base): alice = create_user(base, "page") other = create_user(base, "other") chat = call( base, "POST", "/api/v1/chats", 201, token=alice["token"], body={"title": "Страницы", "member_ids": [alice["id"]]}, )["json"] other_chat = call( base, "POST", "/api/v1/chats", 201, token=other["token"], body={"title": "Другой", "member_ids": [other["id"]]}, )["json"] texts = ["p0", "Привет, мир 👋", "p2", "p3", "p4"] created = [] for text in texts: created.append(send_message(base, chat["id"], alice["token"], text)["json"]) send_message(base, other_chat["id"], other["token"], "x") send_message(base, other_chat["id"], other["token"], "y") collected = [] cursor = None pages = 0 while True: target = "/api/v1/chats/%s/messages?limit=2" % chat["id"] if cursor: target += "&cursor=" + quote(cursor, safe="") page = call(base, "GET", target, 200, token=alice["token"])["json"] pages += 1 collected.extend(page["items"]) cursor = page["next_cursor"] if cursor is None: break check(pages < 10, "pagination did not finish") check(pages == 3, "expected 3 pages, got %d" % pages) check(collected == created, "pagination lost, duplicated or reordered messages") stolen = call( base, "GET", "/api/v1/chats/%s/messages?limit=1" % other_chat["id"], 200, token=other["token"], )["json"] check(stolen["next_cursor"], "foreign page should expose a cursor") rejected = call( base, "GET", "/api/v1/chats/%s/messages?limit=2&cursor=%s" % (chat["id"], quote(stolen["next_cursor"], safe="")), 400, token=alice["token"], ) check(rejected["json"]["error"]["code"] == "invalid_request", "foreign cursor code") first_page = call( base, "GET", "/api/v1/chats/%s/messages?limit=1" % chat["id"], 200, token=alice["token"], )["json"] for fields in ({"t": None}, {"t": 123}, {"i": 123}): broken = call( base, "GET", "/api/v1/chats/%s/messages?limit=1&cursor=%s" % (chat["id"], quote(replace_cursor(first_page["next_cursor"], fields), safe="")), 400, token=alice["token"], ) check(broken["json"]["error"]["code"] == "invalid_request", "tampered cursor code") print("PASS: pagination") def test_concurrency(base): users = [create_user(base, "c%d" % index) for index in range(CLIENTS)] chat = call( base, "POST", "/api/v1/chats", 201, token=users[0]["token"], body={"title": "Гонка", "member_ids": [user["id"] for user in users]}, )["json"] barrier = threading.Barrier(CLIENTS) found = [] errors = [] lock = threading.Lock() def worker(user): try: barrier.wait(10) local = [] for index in range(PER_CLIENT): message = send_message( base, chat["id"], user["token"], "m-%s-%d" % (user["id"][:8], index), )["json"] local.append(message["id"]) with lock: found.extend(local) except Exception as exc: with lock: errors.append("%s: %s" % (type(exc).__name__, exc)) threads = [threading.Thread(target=worker, args=(user,)) for user in users] for thread in threads: thread.start() for thread in threads: thread.join() check(not errors, "concurrent sends failed: " + "; ".join(errors[:5])) check(len(found) == CLIENTS * PER_CLIENT, "submitted count") check(len(set(found)) == CLIENTS * PER_CLIENT, "duplicate message ids") listed = [] cursor = None while True: target = "/api/v1/chats/%s/messages?limit=40" % chat["id"] if cursor: target += "&cursor=" + quote(cursor, safe="") page = call(base, "GET", target, 200, token=users[0]["token"])["json"] listed.extend(item["id"] for item in page["items"]) cursor = page["next_cursor"] if cursor is None: break check(len(listed) == 100 and len(set(listed)) == 100, "history does not contain 100 unique messages") check(set(listed) == set(found), "history ids differ from accepted sends") EVIDENCE.mkdir(exist_ok=True) report = { "date": "2026-10-25", "command": "python student-tests/run.py", "clients": CLIENTS, "messages_per_client": PER_CLIENT, "submitted": len(found), "unique_ids": len(set(found)), "listed": len(listed), "failed_requests": len(errors), "pass": True, } (EVIDENCE / "concurrency.json").write_text( json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) print("PASS: concurrency 100 unique messages from 10 clients") def test_logs(server): alice = create_user(server.base, "logs") chat = call( server.base, "POST", "/api/v1/chats", 201, token=alice["token"], body={"title": "Логи", "member_ids": [alice["id"]]}, )["json"] send_message(server.base, chat["id"], alice["token"], SECRET) time.sleep(0.2) output = server.output() check("duration_ms=" in output and "method=POST" in output, "request log line missing") check(SECRET not in output, "message text leaked into logs") check(alice["token"] not in output, "session token leaked into logs") check("lab-only-password" not in output and "postgresql+asyncpg://" not in output, "database url leaked into logs") print("PASS: logs omit message text") def main(): port = free_port() server = Server(port) try: server.wait_ready() call(server.base, "GET", "/health/live", 200) call(server.base, "GET", "/api/v1/users/me", 401) header_only = create_user(server.base, "header") rejected = call(server.base, "GET", "/api/v1/users/me", 401, user_id=header_only["id"]) check(rejected["json"]["error"]["code"] == "unauthorized", "X-User-Id was accepted") test_invalid_json(server.base) test_duplicate_username(server.base) test_empty_and_foreign(server.base) test_pagination(server.base) test_concurrency(server.base) test_logs(server) finally: server.stop() print("PASS: student tests") if __name__ == "__main__": try: main() except Failure as exc: print("FAIL:", exc) sys.exit(1)