Files

68 lines
2.8 KiB
JavaScript

const { createClient } = require(process.argv[2]);
const { addMovement } = require("./client-movement.cjs");
const [port, version] = process.argv.slice(3);
const peers = [];
const wait = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
async function join(username) {
const client = createClient({
host: "127.0.0.1", port: Number(port), version, username, auth: "offline", hideErrors: true,
});
addMovement(client, version);
const peer = { client, ended: false, reason: "", messages: [] };
peers.push(peer);
client.on("end", reason => { peer.ended = true; if (!peer.reason) peer.reason = String(reason); });
client.on("kick_disconnect", ({ reason }) => { peer.reason = JSON.stringify(reason); });
client.on("error", error => { if (!peer.reason) peer.reason = error.message; });
for (const event of ["systemChat", "playerChat"]) {
client.on(event, data => peer.messages.push(JSON.stringify(data)));
}
await new Promise((resolve, reject) => {
client.once("login", resolve);
client.once("end", () => reject(new Error("Login ended: " + peer.reason)));
});
return peer;
}
async function expectDisconnect(peer, label) {
const deadline = Date.now() + 6500;
while (!peer.ended && Date.now() < deadline) await wait(65);
if (!peer.ended) throw new Error(label + " packet did not trigger a server disconnect");
console.log(label + "=PASS " + peer.reason);
}
async function main() {
const witness = await join("ICGBoundaryW");
const oversizedChat = await join("ICGBoundary01");
await wait(1000);
oversizedChat.client.chat("ICG-chat-limit " + "x".repeat(300));
await expectDisconnect(oversizedChat, "OVERSIZED_CHAT");
if (witness.messages.some(message => message.includes("ICG-chat-limit"))) {
throw new Error("Oversized chat reached another player");
}
const oversizedBook = await join("ICGBoundary02");
const rapid = await join("ICGBoundary03");
await wait(5200);
oversizedBook.client.write("edit_book", { hand: 0, pages: ["ICG-boundary invalid title"], title: "a".repeat(33) });
await expectDisconnect(oversizedBook, "OVERSIZED_TITLE");
rapid.client.write("edit_book", { hand: 0, pages: ["ICG-boundary first edit"] });
await wait(100);
rapid.client.write("edit_book", { hand: 0, pages: ["ICG-boundary rapid duplicate"] });
await expectDisconnect(rapid, "RAPID_BOOK_EDIT");
if (witness.ended) throw new Error("An unrelated player was disconnected");
}
const timer = setTimeout(() => {
console.error("Boundary client timed out");
process.exit(1);
}, 32_000);
main().catch(error => {
console.error(error.message);
process.exitCode = 1;
}).finally(() => {
clearTimeout(timer);
for (const peer of peers) if (!peer.ended) peer.client.end("Boundary checks finished");
});