forked from mtusi2026/mtusi2026lab4
lab4: s3 attachments, private bucket, healthchecks, tests and report
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import threading
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("Lab3: concurrent idempotency and validation")
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
c.call("POST", "/api/v1/users", 400,
|
||||
body={"username": "valid\n", "display_name": "Test", "password": "LongPassword123"})
|
||||
registration = {"username": "race_" + uuid.uuid4().hex[:12],
|
||||
"display_name": "Race", "password": "LongPassword123"}
|
||||
registration_barrier = threading.Barrier(2)
|
||||
|
||||
def register(_):
|
||||
local = Client(args.base_url, args.ca_file)
|
||||
registration_barrier.wait()
|
||||
return local.call("POST", "/api/v1/users", [201, 409],
|
||||
body=registration, schema="User")["status"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
check(sorted(pool.map(register, range(2))) == [201, 409], "registration race")
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
path = "/api/v1/chats/" + chat["id"] + "/messages"
|
||||
key = uuid.uuid4().hex
|
||||
barrier = threading.Barrier(12)
|
||||
|
||||
def send(_):
|
||||
local = Client(args.base_url, args.ca_file)
|
||||
barrier.wait()
|
||||
return local.message(alice, chat, "Одна запись", key=key)
|
||||
|
||||
with ThreadPoolExecutor(max_workers=12) as pool:
|
||||
responses = list(pool.map(send, range(12)))
|
||||
first = responses[0]["json"]
|
||||
check(all(r["json"] == first for r in responses), "concurrent replay differs")
|
||||
instances = {r["headers"]["X-Instance-Id"] for r in responses}
|
||||
check(len(instances) >= 2, "same key must reach at least two replicas")
|
||||
page = c.call("GET", path, actor=bob, schema="MessagePage")["json"]
|
||||
check(page["items"] == [first], "concurrent retry created duplicates")
|
||||
c.call("POST", path, 409, actor=alice, body={"text": "Изменённый текст"},
|
||||
headers={"Idempotency-Key": key})
|
||||
c.call("POST", path, 404, actor=eve, body={"text": "Одна запись"},
|
||||
headers={"Idempotency-Key": key})
|
||||
# The same key belongs to a different scope for Bob and for another chat.
|
||||
other = c.message(bob, chat, "Другая область", key=key)["json"]
|
||||
check(other["id"] != first["id"], "different users share an idempotency scope")
|
||||
second_chat = c.call("POST", "/api/v1/chats", 201, actor=alice,
|
||||
body={"title": "Второй", "member_ids": [alice["id"]]},
|
||||
schema="Chat")["json"]
|
||||
other = c.message(alice, second_chat, "Одна запись", key=key)["json"]
|
||||
check(other["id"] != first["id"], "different chats share an idempotency scope")
|
||||
for bad_key in ("", "bad key", "x" * 129):
|
||||
c.call("POST", path, 400, actor=alice, body={"text": "Текст"},
|
||||
headers={"Idempotency-Key": bad_key})
|
||||
for body in ({"text": " "}, {"text": 123}, {"text": "x", "sender_id": alice["id"]}):
|
||||
c.call("POST", path, 400, actor=alice, body=body,
|
||||
headers={"Idempotency-Key": uuid.uuid4().hex})
|
||||
c.call("POST", path, 400, actor=alice, raw=b'{"text":',
|
||||
headers={"Content-Type": "application/json", "Idempotency-Key": uuid.uuid4().hex})
|
||||
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice)
|
||||
c.call("POST", path, 401, actor=alice, body={"text": "Одна запись"},
|
||||
headers={"Idempotency-Key": key})
|
||||
print("PASS: 12 concurrent requests across %d replicas, one message" % len(instances))
|
||||
print("PASS: conflict, key scope, validation, access and revoked session")
|
||||
print("PASS: concurrent registration, exactly one 201 and one 409")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Additional grade-4 negative and access tests. Requires running Compose stack."""
|
||||
import hashlib
|
||||
import zlib
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
||||
from client import Client, check, arguments, run
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
def main():
|
||||
args = arguments("Lab4: file validation, permissions, idempotency and isolation")
|
||||
c = Client(args.base_url, args.ca_file, timeout=35)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
file = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
||||
path = "/api/v1/chats/" + chat["id"]
|
||||
upload = path + "/attachments?filename=photo.png"
|
||||
result = c.call("POST",upload,201,actor=alice,raw=file,
|
||||
headers={"Content-Type":"image/png"},schema="Attachment")["json"]
|
||||
obj = "/api/v1/attachments/" + result["id"]
|
||||
check(result["sha256"] == hashlib.sha256(file).hexdigest(),"checksum mismatch")
|
||||
check(c.call("GET",obj+"/content",actor=bob)["bytes"] == file,"original differs")
|
||||
c.call("GET",obj,404,actor=eve)
|
||||
c.call("GET",obj+"/content",404,actor=eve)
|
||||
c.call("GET","/api/v1/attachments/"+str(uuid.uuid4()),404,actor=bob)
|
||||
c.call("POST",upload,415,actor=alice,raw=b"not an image",
|
||||
headers={"Content-Type":"image/png"})
|
||||
c.call("POST",upload,415,actor=alice,raw=file,
|
||||
headers={"Content-Type":"image/jpeg"})
|
||||
c.call("POST",upload,413,actor=alice,raw=b"\0"*10485761,
|
||||
headers={"Content-Type":"image/png"})
|
||||
huge = bytearray(file)
|
||||
huge[16:20] = (10000).to_bytes(4, "big")
|
||||
huge[20:24] = (5000).to_bytes(4, "big")
|
||||
huge[29:33] = (zlib.crc32(huge[12:29]) & 0xffffffff).to_bytes(4, "big")
|
||||
c.call("POST",upload,413,actor=alice,raw=bytes(huge),
|
||||
headers={"Content-Type":"image/png"})
|
||||
c.call("POST",path+"/attachments?filename=..%2Fhidden.png",400,actor=alice,
|
||||
raw=file,headers={"Content-Type":"image/png"})
|
||||
c.call("POST",path+"/attachments?filename=sample.txt",415,actor=alice,
|
||||
raw=file,headers={"Content-Type":"text/plain"})
|
||||
second = c.call("POST","/api/v1/chats",201,actor=alice,
|
||||
body={"title":"Other Chat","member_ids":[alice["id"]]},schema="Chat")["json"]
|
||||
c.call("POST", "/api/v1/chats/"+second["id"]+"/messages",404,
|
||||
actor=alice,body={"text":"","attachment_ids":[result["id"]]},
|
||||
headers={"Idempotency-Key":uuid.uuid4().hex})
|
||||
attached = c.message(alice,chat,"",attachments=[result["id"]])["json"]
|
||||
check(attached["attachment_ids"] == [result["id"]],"message lost attachment")
|
||||
key = uuid.uuid4().hex
|
||||
one = c.message(alice,chat,"Фото",key=key,attachments=[result["id"]])["json"]
|
||||
check(c.message(alice,chat,"Фото",key=key,attachments=[result["id"]])["json"] == one,
|
||||
"repeated idempotency response mismatch")
|
||||
c.call("POST",path+"/messages",409,actor=alice,
|
||||
body={"text":"Фото", "attachment_ids":[]},headers={"Idempotency-Key":key})
|
||||
print("PASS: private downloads, MIME spoof, corrupt image, oversize, dangerous filename, foreign chat, idempotency")
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user