Implement lab 3 level 4 failover and idempotency
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Contract-based lab2 JSON edge cases; all data is synthetic.
|
||||
|
||||
Uses raw HTTP bodies for byte encoding and field-name checks. Expectations
|
||||
come from contracts/README.md and CreateUser/CreateChat/CreateMessage schemas.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, Failure, LAB, check, shape # noqa: E402
|
||||
|
||||
|
||||
def encoded(value):
|
||||
return json.dumps(value, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
||||
|
||||
|
||||
def source_fingerprint():
|
||||
digest = hashlib.sha256()
|
||||
for path in sorted([ROOT / name for name in ("go.mod", "go.sum", "Dockerfile", ".dockerignore")]
|
||||
+ list((ROOT / "cmd").rglob("*.go"))
|
||||
+ list((ROOT / "internal").rglob("*.go"))):
|
||||
digest.update(path.relative_to(ROOT).as_posix().encode() + b"\0")
|
||||
digest.update(path.read_bytes() + b"\0")
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def cases_for(client):
|
||||
owner = client.user("edge")
|
||||
chat = client.call("POST", "/api/v1/chats", 201, actor=owner,
|
||||
body={"title": "Validation audit", "member_ids": [owner["id"]]},
|
||||
schema="Chat")["json"]
|
||||
endpoints = (
|
||||
("users", "/api/v1/users", None, "User", ("username", "display_name", "password")),
|
||||
("chats", "/api/v1/chats", owner, "Chat", ("title", "member_ids")),
|
||||
("messages", "/api/v1/chats/" + chat["id"] + "/messages", owner, "Message", ("text",)),
|
||||
)
|
||||
|
||||
def body_for(endpoint):
|
||||
if endpoint == "users":
|
||||
return {"username": "edge_" + uuid.uuid4().hex[:16], "display_name": "Probe",
|
||||
"password": "Synthetic:validation-password"}
|
||||
if endpoint == "chats":
|
||||
return {"title": "Probe", "member_ids": [owner["id"]]}
|
||||
return {"text": "CASE_PROBE"}
|
||||
|
||||
cases = []
|
||||
|
||||
def add(endpoint, group, name, body=None, raw=None, expected=400, echo=None):
|
||||
label, path, actor, schema, _ = endpoint
|
||||
cases.append({"name": label + ": " + name, "group": group, "path": path,
|
||||
"actor": actor, "schema": schema, "raw": raw if raw is not None else encoded(body),
|
||||
"expected": expected, "echo": echo,
|
||||
"headers": {"Idempotency-Key": uuid.uuid4().hex} if label == "messages" and LAB >= 3 else {}})
|
||||
|
||||
invalid_bytes = (("ff", b"\xff"), ("continuation", b"\x80"), ("overlong", b"\xc0\xaf"),
|
||||
("truncated", b"\xe2\x82"), ("surrogate bytes", b"\xed\xa0\x80"),
|
||||
("above U+10FFFF", b"\xf4\x90\x80\x80"))
|
||||
for endpoint in endpoints:
|
||||
label, _, _, _, fields = endpoint
|
||||
for field in fields:
|
||||
aliases = [field.upper(), field[0].upper() + field[1:]]
|
||||
if "s" in field:
|
||||
aliases.append(field.replace("s", "ſ"))
|
||||
for alias in dict.fromkeys(aliases):
|
||||
for extra in (False, True):
|
||||
body = body_for(label)
|
||||
body[alias] = body[field] if extra else body.pop(field)
|
||||
add(endpoint, "key_case", field + " alias " + alias + (" extra" if extra else " only"), body)
|
||||
body = body_for(label)
|
||||
body.pop(field)
|
||||
add(endpoint, "shape", "missing " + field, body)
|
||||
wrong_values = (None, True, 1, {}, [], "not-an-array") if field == "member_ids" else (None, True, 1, {}, [])
|
||||
for wrong in wrong_values:
|
||||
body = body_for(label)
|
||||
body[field] = wrong
|
||||
add(endpoint, "shape", field + " type " + type(wrong).__name__, body)
|
||||
for root in (None, True, 1, "string", []):
|
||||
add(endpoint, "shape", "root " + type(root).__name__, root)
|
||||
body = body_for(label)
|
||||
body["extra"] = True
|
||||
add(endpoint, "shape", "unknown field", body)
|
||||
for suffix in (b"{}", b"null", b" garbage"):
|
||||
add(endpoint, "shape", "trailing " + suffix.decode(), raw=encoded(body_for(label)) + suffix)
|
||||
for raw in (b"", b'{"', b"{\"x\":NaN}", b'{"x":"\x00"}'):
|
||||
add(endpoint, "shape", "malformed " + raw.hex(), raw=raw)
|
||||
|
||||
value_field = {"users": "display_name", "chats": "title", "messages": "text"}[label]
|
||||
for name, bad in invalid_bytes:
|
||||
body = body_for(label)
|
||||
body[value_field] = "BYTE_PROBE"
|
||||
add(endpoint, "utf8", value_field + " invalid UTF-8 " + name,
|
||||
raw=encoded(body).replace(b"BYTE_PROBE", b"prefix" + bad + b"suffix"))
|
||||
for value in ("Привет 😀", "�", "e\u0301", "\U0010ffff"):
|
||||
body = body_for(label)
|
||||
body[value_field] = value
|
||||
add(endpoint, "utf8", value_field + " valid " + ascii(value), body, expected=201, echo=body)
|
||||
body = body_for(label)
|
||||
body[value_field] = "😀"
|
||||
add(endpoint, "utf8", "valid escaped surrogate pair",
|
||||
raw=json.dumps(body, ensure_ascii=True).encode(), expected=201, echo=body)
|
||||
body = body_for(label)
|
||||
raw = encoded(body).replace(('"' + value_field + '"').encode(),
|
||||
('"\\u%04x%s"' % (ord(value_field[0]), value_field[1:])).encode())
|
||||
add(endpoint, "key_case", "escaped canonical key", raw=raw, expected=201, echo=body)
|
||||
body = body_for(label)
|
||||
raw = encoded(body).replace(('"' + value_field + '"').encode(),
|
||||
('"\\u%04x%s"' % (ord(value_field[0].upper()), value_field[1:].upper())).encode())
|
||||
add(endpoint, "key_case", "escaped uppercase key", raw=raw)
|
||||
for value in ("", " \t\n ", "\u00a0\u2003", "😀" * (4001 if label == "messages" else 101)):
|
||||
body = body_for(label)
|
||||
body[value_field] = value
|
||||
add(endpoint, "boundaries", value_field + " invalid length/space " + str(len(value)), body)
|
||||
body = body_for(label)
|
||||
body[value_field] = "😀" * (4000 if label == "messages" else 100)
|
||||
add(endpoint, "boundaries", value_field + " exact Unicode maximum", body, expected=201, echo=body)
|
||||
|
||||
# The contract does not specify duplicate-key or lone-surrogate policy.
|
||||
# Keep these as observations, without asserting a required HTTP status.
|
||||
for escape in (b"\\ud800", b"\\udc00"):
|
||||
body = body_for(label)
|
||||
body[value_field] = "ESCAPE_PROBE"
|
||||
add(endpoint, "observations", "unpaired escape " + escape.decode(),
|
||||
raw=encoded(body).replace(b"ESCAPE_PROBE", escape), expected=None)
|
||||
raw = encoded(body_for(label))
|
||||
add(endpoint, "observations", "duplicate key then null",
|
||||
raw=raw[:-1] + b',"' + value_field.encode() + b'":null}', expected=None)
|
||||
|
||||
for value in ("ab", "Aaa", "a-a", "abc\n", "кириллица", "a" * 33):
|
||||
body = body_for("users")
|
||||
body["username"] = value
|
||||
add(endpoints[0], "boundaries", "invalid username " + ascii(value), body)
|
||||
for value in ([], [owner["id"], owner["id"]], [str(uuid.uuid4())],
|
||||
[owner["id"], str(uuid.uuid4())], [owner["id"], None], [owner["id"], "not-uuid"]):
|
||||
body = body_for("chats")
|
||||
body["member_ids"] = value
|
||||
add(endpoints[1], "boundaries", "invalid members " + repr(value), body)
|
||||
return cases
|
||||
|
||||
|
||||
def run(client, groups=None, verbose=True):
|
||||
results = []
|
||||
for case in cases_for(client):
|
||||
if case["group"] == "observations" and (groups is None or "observations" not in groups):
|
||||
continue
|
||||
if groups is not None and case["group"] not in groups:
|
||||
continue
|
||||
result = {key: case[key] for key in ("name", "group", "path", "expected")}
|
||||
# Request bodies may contain a registration password; do not put them
|
||||
# in evidence, even as hex. Tokens remain only in the teacher client.
|
||||
try:
|
||||
response = client.call("POST", case["path"], expected=list(range(200, 600)),
|
||||
actor=case["actor"], raw=case["raw"],
|
||||
headers={"Content-Type": "application/json", **case["headers"]})
|
||||
result["actual"] = response["status"]
|
||||
if case["expected"] is None:
|
||||
result["status"] = "OBSERVED"
|
||||
if response["status"] == 201:
|
||||
result["response"] = json.loads(response["bytes"])
|
||||
else:
|
||||
check(response["status"] == case["expected"],
|
||||
"expected %s, received %s" % (case["expected"], response["status"]))
|
||||
if response["status"] == 400:
|
||||
check(response["json"]["error"]["code"] == "invalid_request", "wrong error code")
|
||||
elif response["status"] == 201:
|
||||
payload = json.loads(response["bytes"])
|
||||
shape(payload, case["schema"])
|
||||
check(all(payload.get(k) == v for k, v in case["echo"].items()
|
||||
if k != "password"), "valid fields changed")
|
||||
check("password" not in payload and "password_hash" not in payload,
|
||||
"response exposed password material")
|
||||
result["status"] = "PASS"
|
||||
except (Failure, OSError, ValueError) as exc:
|
||||
result.update(status="FAIL", details=str(exc))
|
||||
results.append(result)
|
||||
if verbose:
|
||||
print(result["status"] + ": " + result["name"] +
|
||||
(": " + result["details"] if "details" in result else ""))
|
||||
return results
|
||||
|
||||
|
||||
def acceptance(client, group=None):
|
||||
results = run(client, {group} if group else None, verbose=False)
|
||||
check(bool(results), "validation group has no cases")
|
||||
failures = [x for x in results if x["status"] == "FAIL"]
|
||||
check(not failures, "; ".join(x["name"] + ": " + x["details"] for x in failures))
|
||||
return {"json_cases": len(results)}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--output")
|
||||
parser.add_argument("--include-observations", action="store_true")
|
||||
parser.add_argument("--tested-source-sha256", help="Fingerprint of the source used to build the tested server")
|
||||
args = parser.parse_args()
|
||||
if args.tested_source_sha256 and (len(args.tested_source_sha256) != 64 or
|
||||
any(c not in "0123456789abcdef" for c in args.tested_source_sha256)):
|
||||
parser.error("--tested-source-sha256 must be a lowercase SHA-256")
|
||||
groups = {"key_case", "utf8", "shape", "boundaries"}
|
||||
if args.include_observations:
|
||||
groups.add("observations")
|
||||
results = run(Client(args.base_url), groups)
|
||||
failures = sum(x["status"] == "FAIL" for x in results)
|
||||
observed = sum(x["status"] == "OBSERVED" for x in results)
|
||||
checked = len(results) - observed
|
||||
summary = {"status": "FAIL" if failures else "PASS", "passed": checked - failures,
|
||||
"failed": failures, "checked": checked, "observations": observed}
|
||||
print("SUMMARY:", json.dumps(summary))
|
||||
if args.output:
|
||||
report = {"date_utc": dt.datetime.now(dt.timezone.utc).isoformat(),
|
||||
"commit": subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT, text=True).strip(),
|
||||
"tested_source_sha256": args.tested_source_sha256,
|
||||
"working_tree_source_sha256": source_fingerprint(), "base_url": args.base_url,
|
||||
"scope": "HTTP JSON validation only; no Docker/runtime checks",
|
||||
"command": [sys.executable, *sys.argv], "summary": summary, "cases": results}
|
||||
target = Path(args.output)
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
|
||||
return 1 if failures else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user