Files
mtusi2026lab5/scripts/check_edge.py
T
2026-11-12 17:28:28 +03:00

51 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import base64, json, sys, time, urllib.request, urllib.error, uuid
BASE = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:8080"
PWD = "correct-horse-battery-staple"
def call(method, path, body=None, token=None, basic=None, key=None):
data = json.dumps(body).encode() if body is not None else None
r = urllib.request.Request(BASE + path, data=data, method=method)
if data is not None: r.add_header("Content-Type", "application/json")
if token: r.add_header("Authorization", "Bearer " + token)
if basic: r.add_header("Authorization", "Basic " + base64.b64encode(basic.encode()).decode())
if key: r.add_header("Idempotency-Key", key)
try:
with urllib.request.urlopen(r, timeout=10) as resp:
return resp.status, json.loads(resp.read() or b"null")
except urllib.error.HTTPError as e:
return e.code, None
name = "edge_" + uuid.uuid4().hex[:8]
_, u = call("POST", "/api/v1/users", {"username": name, "display_name": "e", "password": PWD})
_, s = call("POST", "/api/v1/auth/sessions", basic=f"{name}:{PWD}")
t = s["token"]
_, c = call("POST", "/api/v1/chats", {"title": "t", "member_ids": [u["id"]]}, token=t)
m = f"/api/v1/chats/{c['id']}/messages"
k = lambda: uuid.uuid4().hex
cases = [
("поле Text (другой регистр)", call("POST", m, {"Text": "x"}, token=t, key=k())[0], {400}),
("текст из NBSP/U+3000", call("POST", m, {"text": "\u00a0\u3000"}, token=t, key=k())[0], {400}),
("текст с NUL", call("POST", m, {"text": "a\u0000b"}, token=t, key=k())[0], {400}),
("limit= пустой", call("GET", "/api/v1/chats?limit=", token=t)[0], {400}),
("cursor= пустой", call("GET", m + "?cursor=", token=t)[0], {400}),
("chat_id не UUID", call("GET", "/api/v1/chats/not-a-uuid/messages", token=t)[0], {400}),
("member_ids не UUID", call("POST", "/api/v1/chats", {"title": "t", "member_ids": [u["id"], "not-a-uuid"]}, token=t)[0], {400}),
("название из пробелов", call("POST", "/api/v1/chats", {"title": " ", "member_ids": [u["id"]]}, token=t)[0], {400}),
("название 101 символ", call("POST", "/api/v1/chats", {"title": "a" * 101, "member_ids": [u["id"]]}, token=t)[0], {400}),
]
for label, got, ok in cases:
print("PASS" if got in ok else "FAIL", label, "->", got)
def login_ms(basic):
t0 = time.time(); call("POST", "/api/v1/auth/sessions", basic=basic)
return (time.time() - t0) * 1000
ms_unknown = sorted(login_ms("nobody_" + uuid.uuid4().hex[:6] + ":" + PWD) for _ in range(5))[2]
ms_wrong = sorted(login_ms(name + ":wrong-password-123") for _ in range(5))[2]
print("PASS" if abs(ms_unknown - ms_wrong) < 0.5 * ms_wrong else "FAIL",
f"тайминг входа: неизвестный {ms_unknown:.1f} мс, неверный пароль {ms_wrong:.1f} мс")