forked from mtusi2026/mtusi2026lab7
Prepare verified laboratory 7 implementation and evidence
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
export type LoginObservation = {
|
||||
status: number;
|
||||
alias: string;
|
||||
actorCorrect: boolean;
|
||||
retryAfter: string | null;
|
||||
errorCode: string | null;
|
||||
};
|
||||
export type Replica = "api1" | "api2";
|
||||
export function createSharedLoginCase(ports: {
|
||||
request: (
|
||||
replica: Replica,
|
||||
kind: "main" | "control",
|
||||
) => Promise<LoginObservation>;
|
||||
now: () => number;
|
||||
}) {
|
||||
if (typeof ports?.request !== "function" || typeof ports?.now !== "function")
|
||||
throw Error("LOGIN_CASE_PORTS_REQUIRED");
|
||||
return async (expectedAliases: Record<Replica, string>) => {
|
||||
if (
|
||||
expectedAliases.api1 === expectedAliases.api2 ||
|
||||
!Object.values(expectedAliases).every((s) => /^[a-f0-9-]{36}$/.test(s))
|
||||
)
|
||||
throw Error("LOGIN_BASELINE_INVALID");
|
||||
const started = ports.now(),
|
||||
rows: { replica: Replica; result: LoginObservation }[] = [];
|
||||
for (let i = 0; i < 10; i++)
|
||||
rows.push(
|
||||
...(await Promise.all(
|
||||
(["api1", "api2"] as const).map(async (replica) => ({
|
||||
replica,
|
||||
result: await ports.request(replica, "main"),
|
||||
})),
|
||||
)),
|
||||
);
|
||||
const accepted = rows.filter((r) => r.result.status === 201).length,
|
||||
denied = rows.filter((r) => r.result.status === 429).length;
|
||||
const elapsedMilliseconds = ports.now() - started;
|
||||
if (
|
||||
!Number.isFinite(started) ||
|
||||
!Number.isFinite(elapsedMilliseconds) ||
|
||||
elapsedMilliseconds < 0 ||
|
||||
elapsedMilliseconds >= 60000
|
||||
)
|
||||
throw Error("LOGIN_CASE_WINDOW_EXPIRED");
|
||||
for (const { replica, result } of rows) {
|
||||
if (result.alias !== expectedAliases[replica])
|
||||
throw Error("LOGIN_INSTANCE_MISMATCH");
|
||||
if (result.status !== 201 && result.status !== 429)
|
||||
throw Error("LOGIN_STATUS_UNEXPECTED");
|
||||
if (result.status === 201 && !result.actorCorrect)
|
||||
throw Error("LOGIN_SUCCESS_ACTOR_MISMATCH");
|
||||
if (result.status === 429) {
|
||||
if (
|
||||
!/^[1-9][0-9]*$/.test(result.retryAfter ?? "") ||
|
||||
Number(result.retryAfter) > 60
|
||||
)
|
||||
throw Error("LOGIN_RETRY_AFTER_INVALID");
|
||||
if (result.errorCode !== "rate_limited")
|
||||
throw Error("LOGIN_REJECTION_BODY_INVALID");
|
||||
}
|
||||
}
|
||||
if (
|
||||
accepted !== 10 ||
|
||||
denied !== 10 ||
|
||||
!(["api1", "api2"] as const).every((replica) =>
|
||||
rows.some((r) => r.replica === replica && r.result.status === 429),
|
||||
)
|
||||
)
|
||||
throw Error("SHARED_LIMIT_NOT_PROVED");
|
||||
const controls = await Promise.all(
|
||||
(["api1", "api2"] as const).map((replica) =>
|
||||
ports.request(replica, "control"),
|
||||
),
|
||||
);
|
||||
if (
|
||||
!controls.every(
|
||||
(r, i) =>
|
||||
r.status === 201 &&
|
||||
r.actorCorrect &&
|
||||
r.alias === expectedAliases[i === 0 ? "api1" : "api2"],
|
||||
)
|
||||
)
|
||||
throw Error("LOGIN_CONTROL_FAILED");
|
||||
return {
|
||||
requests: rows.length,
|
||||
accepted,
|
||||
denied,
|
||||
controls: controls.length,
|
||||
elapsedMilliseconds,
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user