Add content moderation and release validation tools
This commit is contained in:
1 parent
a9d572fff0
commit
7c2e82fc15
63 files changed
+5508
-38
No files matched your search
@@ -0,0 +1,67 @@
|
||||
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");
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
// Keeps a protocol-only client synchronized like a stationary game client.
|
||||
function addMovement(client, version) {
|
||||
let timer;
|
||||
const usesFlags = Number(version.split(".")[2] || 0) >= 2;
|
||||
client.on("position", packet => {
|
||||
if (Number.isInteger(packet.teleportId)) {
|
||||
client.write("teleport_confirm", { teleportId: packet.teleportId });
|
||||
}
|
||||
if (!timer) {
|
||||
const ground = usesFlags ? { flags: { onGround: true, hasHorizontalCollision: false } } : { onGround: true };
|
||||
timer = setInterval(() => client.write("flying", ground), 150);
|
||||
}
|
||||
});
|
||||
client.on("end", () => clearInterval(timer));
|
||||
}
|
||||
|
||||
module.exports = { addMovement };
|
||||
@@ -0,0 +1,198 @@
|
||||
"""Loopback Chat Completions and SMTP fixtures for dedicated server tests."""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import re
|
||||
import socketserver
|
||||
import socket
|
||||
import ssl
|
||||
import threading
|
||||
import time
|
||||
import zipfile
|
||||
from collections import deque
|
||||
from contextlib import ExitStack, contextmanager
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class AIHandler(BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
request = json.loads(self.rfile.read(int(self.headers["content-length"])))
|
||||
text = request["messages"][-1]["content"]
|
||||
with self.server.lock:
|
||||
self.server.requests.append({"received": time.time(), "size": len(text)})
|
||||
self.server.request_count += 1
|
||||
if "ICG-timeout" in text:
|
||||
time.sleep(2.5)
|
||||
if "ICG-offline-pending" in text:
|
||||
time.sleep(0.75)
|
||||
if "ICG-move-pending" in text:
|
||||
self.server.move_requested.set()
|
||||
deadline = time.monotonic() + 7
|
||||
def can_respond():
|
||||
return self.server.move_signal.exists() and (
|
||||
not self.server.require_reload or self.server.reload_signal.exists())
|
||||
|
||||
while not can_respond() and time.monotonic() < deadline:
|
||||
time.sleep(0.025)
|
||||
if not can_respond():
|
||||
self.send_error(503)
|
||||
return
|
||||
if "ICG-error" in text:
|
||||
self.send_error(503)
|
||||
return
|
||||
if "ICG-disconnect" in text:
|
||||
self.close_connection = True
|
||||
return
|
||||
blocked = "ICG-denied" in text
|
||||
review = {"safe": not blocked, "level": "HIGH" if blocked else "LOW", "category": "INSULT" if blocked else "SAFE",
|
||||
"reason": "loopback fixture", "suggestion": "BLOCK" if blocked else "ALLOW"}
|
||||
content = "invalid JSON" if "ICG-invalid" in text else json.dumps(review)
|
||||
response = json.dumps({"choices": [{"message": {"content": content}}]}).encode()
|
||||
try:
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(response)))
|
||||
self.end_headers()
|
||||
self.wfile.write(response)
|
||||
except ConnectionError:
|
||||
pass
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
pass
|
||||
|
||||
|
||||
class SmtpHandler(socketserver.StreamRequestHandler):
|
||||
def handle(self):
|
||||
self.connection.settimeout(8)
|
||||
if self.server.fail_connections:
|
||||
return
|
||||
self.wfile.write(b"220 localhost ICG test SMTP\r\n")
|
||||
encrypted = isinstance(self.connection, ssl.SSLSocket)
|
||||
if encrypted:
|
||||
with self.server.lock:
|
||||
self.server.negotiated.add(self.connection.version())
|
||||
data = None
|
||||
while command := self.rfile.readline(70_000):
|
||||
if data is not None:
|
||||
if command == b".\r\n":
|
||||
with self.server.lock:
|
||||
self.server.messages.append(b"".join(data))
|
||||
self.server.message_count += 1
|
||||
self.wfile.write(b"250 queued\r\n")
|
||||
data = None
|
||||
else:
|
||||
data.append(command)
|
||||
continue
|
||||
label = command.upper()
|
||||
if label.startswith((b"EHLO ", b"HELO ")):
|
||||
if self.server.tls_mode == "starttls" and not encrypted:
|
||||
self.wfile.write(b"250-localhost\r\n250 STARTTLS\r\n")
|
||||
elif self.server.authenticate:
|
||||
self.wfile.write(b"250-localhost\r\n250 AUTH PLAIN\r\n")
|
||||
else:
|
||||
self.wfile.write(b"250 localhost\r\n")
|
||||
elif label.startswith(b"STARTTLS") and self.server.tls_mode == "starttls" and not encrypted:
|
||||
self.wfile.write(b"220 start TLS\r\n")
|
||||
self.rfile.close()
|
||||
self.wfile.close()
|
||||
self.connection = self.server.ssl_context.wrap_socket(self.connection, server_side=True)
|
||||
self.rfile = self.connection.makefile("rb")
|
||||
self.wfile = self.connection.makefile("wb", buffering=0)
|
||||
encrypted = True
|
||||
with self.server.lock:
|
||||
self.server.negotiated.add(self.connection.version())
|
||||
elif label.startswith(b"AUTH PLAIN "):
|
||||
try:
|
||||
received = base64.b64decode(command.strip().split()[2], validate=True).split(b"\0")[-2:]
|
||||
except ValueError:
|
||||
received = []
|
||||
if received == [b"fixture-user", b"fixture-password"]:
|
||||
self.wfile.write(b"235 authenticated\r\n")
|
||||
else:
|
||||
self.wfile.write(b"535 authentication failed\r\n")
|
||||
elif label.startswith((b"MAIL FROM:", b"RCPT TO:", b"RSET")):
|
||||
self.wfile.write(b"250 accepted\r\n")
|
||||
elif label.startswith(b"DATA"):
|
||||
if self.server.tls_mode == "timeout":
|
||||
time.sleep(2)
|
||||
return
|
||||
self.wfile.write(b"354 send message\r\n")
|
||||
data = []
|
||||
elif label.startswith(b"QUIT"):
|
||||
self.wfile.write(b"221 bye\r\n")
|
||||
return
|
||||
else:
|
||||
self.wfile.write(b"502 unsupported\r\n")
|
||||
|
||||
|
||||
class SmtpServer(socketserver.ThreadingTCPServer):
|
||||
daemon_threads = True
|
||||
|
||||
def get_request(self):
|
||||
client, endpoint = super().get_request()
|
||||
if self.tls_mode == "ssl":
|
||||
client.settimeout(8)
|
||||
client = self.ssl_context.wrap_socket(client, server_side=True)
|
||||
return client, endpoint
|
||||
|
||||
|
||||
@contextmanager
|
||||
def fixtures(plugin: Path, directory: Path, mode="STRICT", smtp_mode="plain", smtp_tls=None,
|
||||
folia_special=False, require_reload=False):
|
||||
with ExitStack() as stack:
|
||||
ai = stack.enter_context(ThreadingHTTPServer(("127.0.0.1", 0), AIHandler))
|
||||
smtp = stack.enter_context(SmtpServer(("127.0.0.1", 0), SmtpHandler))
|
||||
ai.lock, ai.requests, ai.request_count = threading.Lock(), deque(maxlen=4096), 0
|
||||
ai.move_signal = directory / "plugins" / "ICGCompatibilityProbe" / "moved.signal"
|
||||
ai.require_reload = require_reload
|
||||
ai.reload_signal = directory / "configuration-reloaded.signal"
|
||||
ai.move_requested = threading.Event()
|
||||
smtp.lock, smtp.messages, smtp.message_count = threading.Lock(), deque(maxlen=200), 0
|
||||
smtp.fail_connections = False
|
||||
smtp.tls_mode = smtp_mode
|
||||
smtp.ssl_context = smtp_tls
|
||||
smtp.authenticate = smtp_mode in ("ssl", "starttls", "auth-error")
|
||||
smtp.negotiated = set()
|
||||
smtp_port = smtp.server_address[1]
|
||||
if smtp_mode == "port-closed":
|
||||
with socket.socket() as unserved:
|
||||
unserved.bind(("127.0.0.1", 0))
|
||||
smtp_port = unserved.getsockname()[1]
|
||||
# Keep the port reserved without a listening socket for this run.
|
||||
inherited = unserved.dup()
|
||||
stack.callback(inherited.close)
|
||||
for service in (ai, smtp):
|
||||
thread = threading.Thread(target=service.serve_forever, kwargs={"poll_interval": 0.1}, daemon=True)
|
||||
thread.start()
|
||||
stack.callback(thread.join)
|
||||
stack.callback(service.shutdown)
|
||||
data = directory / "plugins" / "ItxiaohuiContentGuard"
|
||||
data.mkdir()
|
||||
with zipfile.ZipFile(plugin) as archive:
|
||||
for name, items in (
|
||||
("ai.yml", {
|
||||
"mode": mode, "api-key": "fixture-token", "model": "fixture-model",
|
||||
"base-url": f"http://127.0.0.1:{ai.server_port}/v1", "timeout-ms": 900, "deadline-ms": 1500,
|
||||
"max-retries": 0, "requests-per-minute": 6000, "circuit-failures": 3, "circuit-cooldown-seconds": 2,
|
||||
}),
|
||||
("smtp.yml", {
|
||||
"enabled": True, "host": "127.0.0.1", "port": smtp_port,
|
||||
"starttls": smtp_mode == "starttls", "ssl": smtp_mode == "ssl",
|
||||
"from": "contentguard@example.test", "to": ["admin@example.test"],
|
||||
"send-interval-ms": 150, "merge-window-ms": 100,
|
||||
"send-timeout-ms": 600 if smtp_mode == "timeout" else 5000,
|
||||
"username": "fixture-user" if smtp.authenticate else "",
|
||||
"password": ("fixture-invalid" if smtp_mode == "auth-error" else "fixture-password")
|
||||
if smtp.authenticate else "",
|
||||
}),
|
||||
):
|
||||
if folia_special and name == "ai.yml":
|
||||
items.update({"timeout-ms": 8000, "deadline-ms": 9000})
|
||||
yaml = archive.read(name).decode("utf-8")
|
||||
for key, value in items.items():
|
||||
yaml, count = re.subn(rf"(?m)^{re.escape(key)}:.*$", lambda _: key + ": " + json.dumps(value), yaml)
|
||||
if count != 1:
|
||||
raise ValueError(f"Fixture key missing: {name} {key}")
|
||||
(data / name).write_text(yaml, encoding="utf-8")
|
||||
yield ai, smtp
|
||||
@@ -0,0 +1,163 @@
|
||||
const { createClient } = require(process.argv[2]);
|
||||
const { addMovement } = require("./client-movement.cjs");
|
||||
const [port, version, scenario = "normal", mode = "STRICT"] = process.argv.slice(3);
|
||||
const wait = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
|
||||
const clients = [];
|
||||
const expectedEnds = new Set();
|
||||
|
||||
async function connect(username) {
|
||||
const client = createClient({ host: "127.0.0.1", port: Number(port), version, username, auth: "offline", hideErrors: true });
|
||||
addMovement(client, version);
|
||||
clients.push(client);
|
||||
const messages = [];
|
||||
for (const event of ["systemChat", "playerChat"]) {
|
||||
client.on(event, data => messages.push(JSON.stringify(data)));
|
||||
}
|
||||
await new Promise((resolve, reject) => {
|
||||
client.once("login", resolve);
|
||||
client.on("error", reject);
|
||||
});
|
||||
return { client, messages };
|
||||
}
|
||||
|
||||
async function delivered(messages, text, timeout = 5000) {
|
||||
const expires = Date.now() + timeout;
|
||||
while (Date.now() < expires) {
|
||||
if (messages.some(line => line.includes(text))) return;
|
||||
await wait(70);
|
||||
}
|
||||
throw new Error("Missing delivery: " + text + " Received: " + JSON.stringify(messages.slice(-7)));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const receiver = await connect("ICGReceiver");
|
||||
const sender = await connect("ICGProbe");
|
||||
if (scenario === "db-fill") {
|
||||
await wait(5000);
|
||||
const pages = Array.from({ length: 50 }, (_, page) =>
|
||||
"ICG-disk " + page + " 违规内容 " + "test data ".repeat(45));
|
||||
sender.client.write("edit_book", { hand: 0, pages, title: "ICG storage limit" });
|
||||
await wait(1700);
|
||||
sender.client.chat("ICG-filter-after-disk 违规内容");
|
||||
await delivered(receiver.messages, "ICG-filter-after-disk ***");
|
||||
console.log("LOCAL_AUDIT_WHILE_DISK_FULL=PASS");
|
||||
return;
|
||||
}
|
||||
await wait(700);
|
||||
sender.client.chat("ICG-public 违规内容");
|
||||
await delivered(receiver.messages, "ICG-public ***");
|
||||
console.log("PUBLIC_FILTER=PASS");
|
||||
await wait(500);
|
||||
sender.client.chat("/msg ICGReceiver ICG-private 违规内容");
|
||||
await delivered(receiver.messages, "ICG-private ***");
|
||||
console.log("PRIVATE_FILTER=PASS");
|
||||
if (scenario === "commands") {
|
||||
for (const alias of ["tell", "w"]) {
|
||||
await wait(400);
|
||||
sender.client.chat("/" + alias + " ICGReceiver ICG-" + alias + " 违规内容");
|
||||
await delivered(receiver.messages, "ICG-" + alias + " ***");
|
||||
}
|
||||
console.log("PRIVATE_ALIASES=PASS");
|
||||
}
|
||||
await wait(500);
|
||||
sender.client.chat("ICG-denied marker");
|
||||
if (mode === "STRICT" || mode === "AUTO") {
|
||||
await delivered(sender.messages, "内容未通过审核");
|
||||
if (receiver.messages.some(line => line.includes("ICG-denied"))) throw new Error("Unsafe message reached recipient");
|
||||
console.log("AI_BLOCK=PASS");
|
||||
} else {
|
||||
await delivered(receiver.messages, "ICG-denied marker");
|
||||
console.log("AI_AUDIT_WITHOUT_BLOCK=PASS");
|
||||
}
|
||||
await wait(4200);
|
||||
|
||||
const pages = ["ICG-page 违 规内容", "Second page"];
|
||||
sender.client.write("edit_book", { hand: 0, pages });
|
||||
await wait(2200);
|
||||
sender.client.chat("/icgprobe book-edit");
|
||||
await wait(1600);
|
||||
sender.client.write("edit_book", { hand: 0, pages, title: "ICG 违规内容" });
|
||||
await wait(1400);
|
||||
sender.client.chat("/icgprobe book-sign");
|
||||
await wait(600);
|
||||
if (scenario === "fault") {
|
||||
for (let i = 0; i < 4; i++) {
|
||||
sender.client.chat("ICG-invalid " + i);
|
||||
await wait(500);
|
||||
}
|
||||
await delivered(sender.messages, "审核暂不可用");
|
||||
if (receiver.messages.some(line => line.includes("ICG-invalid"))) throw new Error("Invalid review reached recipient");
|
||||
console.log("AI_INVALID_FAIL_CLOSED=PASS");
|
||||
await wait(2200);
|
||||
}
|
||||
if (scenario === "folia") {
|
||||
sender.client.chat("ICG-move-pending");
|
||||
await wait(250);
|
||||
sender.client.chat("/icgprobe move");
|
||||
await delivered(receiver.messages, "ICG-move-pending", 11000);
|
||||
console.log("AI_DURING_MOVE=PASS");
|
||||
sender.client.chat("/icgprobe retire");
|
||||
await wait(400);
|
||||
sender.client.chat("/icgprobe regions");
|
||||
const offline = await connect("ICGOffline");
|
||||
await wait(1000);
|
||||
offline.client.chat("ICG-offline-pending");
|
||||
await wait(250);
|
||||
expectedEnds.add(offline.client);
|
||||
offline.client.end("Deliberate disconnect before audit completion");
|
||||
await wait(1500);
|
||||
if (receiver.messages.some(line => line.includes("ICG-offline-pending"))) {
|
||||
throw new Error("A disconnected player's pending chat was delivered");
|
||||
}
|
||||
console.log("OFFLINE_RETURN_DROPPED=PASS");
|
||||
}
|
||||
if (scenario === "permissions") {
|
||||
const admin = await connect("ICGAdmin");
|
||||
const bypass = await connect("ICGBypass");
|
||||
await wait(1300);
|
||||
receiver.client.chat("ICG-high-test 死全家");
|
||||
await delivered(admin.messages, "ICG-high-test");
|
||||
if (!admin.messages.some(line => line.includes("/icg logs "))) {
|
||||
throw new Error("Admin alert lacked a clickable record command");
|
||||
}
|
||||
receiver.client.chat("/icg logs");
|
||||
await delivered(receiver.messages, "你没有权限");
|
||||
if (receiver.messages.some(line => line.includes("ICG-high-test") || line.includes("/icg logs "))) {
|
||||
throw new Error("Admin-only moderation notice reached a regular player");
|
||||
}
|
||||
console.log("ADMIN_NOTIFICATION_ONLY=PASS");
|
||||
bypass.client.chat("ICG-bypass 违规内容");
|
||||
await delivered(receiver.messages, "ICG-bypass 违规内容");
|
||||
await wait(3800);
|
||||
bypass.client.write("edit_book", { hand: 0, pages, title: "ICG 违规内容" });
|
||||
await wait(2300);
|
||||
bypass.client.chat("/icgprobe book-sign");
|
||||
await wait(500);
|
||||
console.log("CHAT_BYPASS_GRANTED=PASS");
|
||||
}
|
||||
}
|
||||
|
||||
const deadline = setTimeout(() => {
|
||||
console.error("Feature client timed out");
|
||||
process.exit(1);
|
||||
}, ["folia", "permissions"].includes(scenario) ? 45_000 : 30_000);
|
||||
let closing = false;
|
||||
function game() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const monitor = setInterval(() => {
|
||||
if (!closing && clients.some(client => client.ended && !expectedEnds.has(client))) {
|
||||
clearInterval(monitor);
|
||||
reject(new Error("A Minecraft client disconnected during feature verification"));
|
||||
}
|
||||
}, 100);
|
||||
main().then(resolve, reject).finally(() => clearInterval(monitor));
|
||||
});
|
||||
}
|
||||
game().catch(error => {
|
||||
console.error(error.message);
|
||||
process.exitCode = 1;
|
||||
}).finally(() => {
|
||||
closing = true;
|
||||
clearTimeout(deadline);
|
||||
for (const client of clients) client.end("Test finished");
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
psutil==7.2.2
|
||||
@@ -0,0 +1,541 @@
|
||||
"""Run the compatibility-stage probe on genuine, isolated Minecraft servers."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import html
|
||||
import json
|
||||
import platform
|
||||
import re
|
||||
import shutil
|
||||
import socket
|
||||
import sqlite3
|
||||
import ssl
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextlib import ExitStack
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from compat_fixtures import fixtures
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
TZ = timezone(timedelta(hours=8))
|
||||
ADAPTERS = {
|
||||
"paper": "PaperSchedulerAdapter", "purpur": "PaperSchedulerAdapter",
|
||||
"leaves": "PaperSchedulerAdapter", "spigot": "BukkitSchedulerAdapter", "folia": "FoliaSchedulerAdapter",
|
||||
}
|
||||
PENDING = ["玩家聊天与私聊", "书本与签署", "AI", "SMTP", "SQLite", "配置重载", "压力与故障恢复", "长时间稳定性"]
|
||||
|
||||
|
||||
def timestamp():
|
||||
return datetime.now(TZ).isoformat(timespec="seconds")
|
||||
|
||||
|
||||
def digest(path):
|
||||
with path.open("rb") as stream:
|
||||
return hashlib.file_digest(stream, "sha256").hexdigest()
|
||||
|
||||
|
||||
def local_port():
|
||||
with socket.socket() as listener:
|
||||
listener.bind(("127.0.0.1", 0))
|
||||
return listener.getsockname()[1]
|
||||
|
||||
|
||||
class ServerProcess:
|
||||
def __init__(self, command, directory, log):
|
||||
self.lines = []
|
||||
self.updated = threading.Condition()
|
||||
self.process = subprocess.Popen(command, cwd=directory, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT, encoding="utf-8", errors="replace", bufsize=1)
|
||||
self.reader = threading.Thread(target=self.read, args=(log,))
|
||||
self.reader.start()
|
||||
|
||||
def read(self, log):
|
||||
with log.open("w", encoding="utf-8") as evidence:
|
||||
for line in self.process.stdout:
|
||||
evidence.write(line)
|
||||
evidence.flush()
|
||||
with self.updated:
|
||||
self.lines.append(line.rstrip())
|
||||
self.updated.notify_all()
|
||||
with self.updated:
|
||||
self.updated.notify_all()
|
||||
|
||||
def await_line(self, pattern, timeout, start=0):
|
||||
expression = re.compile(pattern)
|
||||
deadline = time.monotonic() + timeout
|
||||
with self.updated:
|
||||
while True:
|
||||
for line in self.lines[start:]:
|
||||
if expression.search(line):
|
||||
return line
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0 or self.process.poll() is not None:
|
||||
raise RuntimeError(f"Expected log entry missing: {pattern}")
|
||||
self.updated.wait(min(remaining, 1))
|
||||
|
||||
def send(self, command):
|
||||
self.process.stdin.write(command + "\n")
|
||||
self.process.stdin.flush()
|
||||
|
||||
def stop(self):
|
||||
try:
|
||||
if self.process.poll() is None:
|
||||
self.send("stop")
|
||||
return self.process.wait(timeout=65)
|
||||
except (subprocess.TimeoutExpired, BrokenPipeError):
|
||||
self.process.kill()
|
||||
self.process.wait()
|
||||
return -1
|
||||
finally:
|
||||
self.reader.join()
|
||||
self.process.stdin.close()
|
||||
self.process.stdout.close()
|
||||
|
||||
|
||||
def run_one(args, core, version):
|
||||
with ExitStack() as resources:
|
||||
return run_isolated(args, core, version, resources)
|
||||
|
||||
|
||||
def run_isolated(args, core, version, resources):
|
||||
test_dir = ROOT / ".test-servers" / core / version
|
||||
server_jar = test_dir / "server.jar"
|
||||
if not server_jar.is_file():
|
||||
return f"{core}/{version}: UNVERIFIED, server.jar missing"
|
||||
|
||||
started = timestamp()
|
||||
run_dir = test_dir / "runs" / datetime.now(TZ).strftime("%Y%m%d-%H%M%S-%f")
|
||||
(run_dir / "plugins").mkdir(parents=True)
|
||||
output = ROOT / "test-results" / core / version
|
||||
if args.profile:
|
||||
output /= args.profile
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
if (output / "environment.txt").exists():
|
||||
prior = output / "previous" / run_dir.name
|
||||
prior.mkdir(parents=True)
|
||||
for entry in output.iterdir():
|
||||
if entry.is_file():
|
||||
entry.replace(prior / entry.name)
|
||||
shutil.copy2(server_jar, run_dir / "server.jar")
|
||||
shutil.copy2(args.plugin, run_dir / "plugins" / args.plugin.name)
|
||||
shutil.copy2(args.probe, run_dir / "plugins" / args.probe.name)
|
||||
port = local_port()
|
||||
(run_dir / "server.properties").write_text(
|
||||
f"server-ip=127.0.0.1\nserver-port={port}\nonline-mode=false\nenforce-secure-profile=false\n"
|
||||
"max-players=100\nview-distance=3\nsimulation-distance=3\npause-when-empty-seconds=-1\n"
|
||||
"level-name=world\nlevel-type=minecraft:flat\nwhite-list=false\nenable-rcon=false\n", encoding="ascii")
|
||||
(run_dir / "eula.txt").write_text("eula=true\n", encoding="ascii")
|
||||
flags = ["-Dfile.encoding=UTF-8", "-Dterminal.jline=false", "-Dterminal.ansi=false",
|
||||
"-XX:ActiveProcessorCount=6", "-Xms512M", "-Xmx2G", "-jar", "server.jar", "nogui"]
|
||||
if args.features:
|
||||
flags.insert(0, "-Dicg.compat.features=true")
|
||||
smtp_tls = None
|
||||
if args.smtp_mode in ("ssl", "starttls"):
|
||||
smtp_tls, trust = make_test_tls(run_dir, args.java)
|
||||
flags[0:0] = [f"-Djavax.net.ssl.trustStore={trust}", "-Djavax.net.ssl.trustStorePassword=changeit"]
|
||||
ai, smtp = resources.enter_context(fixtures(args.plugin, run_dir, args.mode, args.smtp_mode, smtp_tls,
|
||||
args.scenario == "folia", args.profile == "folia-reload"))
|
||||
damaged_file = run_dir / "plugins" / "ItxiaohuiContentGuard" / "database" / "audit.sqlite"
|
||||
damage = b"ICG fixture corrupted SQLite\x00" * 100
|
||||
if args.profile == "db-corrupt":
|
||||
damaged_file.parent.mkdir()
|
||||
damaged_file.write_bytes(damage)
|
||||
command = [str(args.java), *flags]
|
||||
if platform.system() == "Windows":
|
||||
(run_dir / "start.cmd").write_text(
|
||||
'@echo off\ncd /d "%~dp0"\n' + subprocess.list2cmdline(command) + "\n", encoding="utf-8")
|
||||
else:
|
||||
from shlex import join
|
||||
(run_dir / "start.sh").write_text(
|
||||
'#!/bin/sh\ncd "$(dirname "$0")" || exit 1\nexec ' + join(command) + "\n", encoding="utf-8")
|
||||
|
||||
attempts = []
|
||||
results = []
|
||||
probe_names = ("async", "global", "region", "entity-cancel", "entity")
|
||||
expected = rf"core={core.capitalize()}\b.* mc={re.escape(version)}\b.* adapter={ADAPTERS[core]}\b"
|
||||
try:
|
||||
server = ServerProcess(command, run_dir, output / "startup.log")
|
||||
attempts.append(server)
|
||||
try:
|
||||
server.await_line(r"Done \([\d.]+s\)!", args.start_timeout)
|
||||
results.append(("真实服务端就绪", "PASS"))
|
||||
server.await_line(expected, 10)
|
||||
server.send("version")
|
||||
server.send("icg info")
|
||||
server.await_line(r"\]: " + expected, 12)
|
||||
results.append(("核心识别、适配器和管理命令", "PASS"))
|
||||
server.send("icgprobe")
|
||||
script = "feature-client.cjs" if args.features else "compat-client.cjs"
|
||||
config_failures = []
|
||||
if args.profile == "folia-reload":
|
||||
def reload_during_request():
|
||||
try:
|
||||
if not ai.move_requested.wait(40):
|
||||
raise RuntimeError("The held request never reached the fixture")
|
||||
server.send("icg reload")
|
||||
verify_database(run_dir, "ADMIN", "SUCCESS")
|
||||
ai.reload_signal.write_text("configuration applied during review", encoding="ascii")
|
||||
except (OSError, RuntimeError) as error:
|
||||
config_failures.append(str(error))
|
||||
|
||||
reloader = threading.Thread(target=reload_during_request, daemon=True)
|
||||
reloader.start()
|
||||
player = subprocess.run(["node", str(ROOT / "scripts" / script), str(args.client),
|
||||
str(port), version, args.scenario, args.mode], capture_output=True, encoding="utf-8",
|
||||
timeout=50 if args.scenario in ("folia", "permissions") else 35)
|
||||
(output / "client.log").write_text(player.stdout + player.stderr, encoding="utf-8")
|
||||
if player.returncode != 0:
|
||||
raise RuntimeError(f"Protocol client failed with code {player.returncode}")
|
||||
if args.profile == "folia-reload":
|
||||
reloader.join(timeout=1)
|
||||
if config_failures or not ai.reload_signal.exists():
|
||||
raise RuntimeError("Reload test did not complete: " + " ".join(config_failures))
|
||||
results.append(("已在 AI 请求挂起期间重载,原审核继续完成", "PASS"))
|
||||
results.append(("真实客户端登录", "PASS"))
|
||||
for name in probe_names:
|
||||
server.await_line(rf"ICG_PROBE {name}=true thread=", 12)
|
||||
results.append((f"调度 {name}", "PASS"))
|
||||
if any("ICG_PROBE" in line and "=false thread=" in line for line in server.lines):
|
||||
raise RuntimeError("Scheduler probe reported a failed assertion")
|
||||
if args.features:
|
||||
server.send("icg status")
|
||||
is_corrupt = args.profile == "db-corrupt"
|
||||
server.await_line(r"database=FAILED" if is_corrupt else r"database=READY", 10)
|
||||
for signal, description in (
|
||||
("PUBLIC_FILTER=PASS", "真实公共聊天替换"),
|
||||
("PRIVATE_FILTER=PASS", "真实私聊替换"),
|
||||
(("AI_BLOCK=PASS" if args.mode in ("AUTO", "STRICT") else "AI_AUDIT_WITHOUT_BLOCK=PASS"),
|
||||
args.mode + " 审核模式行为"),
|
||||
):
|
||||
if signal not in player.stdout:
|
||||
raise RuntimeError(f"Client assertion missing: {signal}")
|
||||
results.append((description, "PASS"))
|
||||
if args.scenario == "fault" and "AI_INVALID_FAIL_CLOSED=PASS" not in player.stdout:
|
||||
raise RuntimeError("AI invalid response did not fail closed")
|
||||
if args.scenario == "fault":
|
||||
results.append(("AI 无效 JSON 安全拦截", "PASS"))
|
||||
if args.scenario == "commands":
|
||||
if "PRIVATE_ALIASES=PASS" not in player.stdout:
|
||||
raise RuntimeError("Protocol client could not deliver both aliases")
|
||||
results.append(("/tell 与 /w 私聊过滤", "PASS"))
|
||||
verify_word_commands(server, run_dir)
|
||||
results.append(("/icg add / words / test / remove", "PASS"))
|
||||
if args.scenario == "permissions":
|
||||
for signal in ("ADMIN_NOTIFICATION_ONLY=PASS", "CHAT_BYPASS_GRANTED=PASS"):
|
||||
if signal not in player.stdout:
|
||||
raise RuntimeError(f"Permission assertion missing: {signal}")
|
||||
results.append((signal, "PASS"))
|
||||
server.await_line(r"ICG_PROBE bypass-book-sign=true thread=", 5)
|
||||
results.append(("绕过聊天的管理员仍受书本审核", "PASS"))
|
||||
if args.scenario == "folia":
|
||||
for signal in ("AI_DURING_MOVE=PASS", "OFFLINE_RETURN_DROPPED=PASS"):
|
||||
if signal not in player.stdout:
|
||||
raise RuntimeError(f"Folia client assertion missing: {signal}")
|
||||
results.append((signal, "PASS"))
|
||||
for action in ("player-cross-region", "expired-entity", "region-burst"):
|
||||
server.await_line(rf"ICG_PROBE {action}=true thread=", 5)
|
||||
results.append((action, "PASS"))
|
||||
verify_database(run_dir, "AI", "ALLOW", "ICGOffline", "ICG-offline-pending")
|
||||
results.append(("离线前提交的审核已入库", "PASS"))
|
||||
if args.profile == "db-full":
|
||||
exercise_page_limit(args, version, port, server, output)
|
||||
results.append(("实服 SQLite 页数耗尽时本地过滤继续", "PASS"))
|
||||
if args.profile == "reload-invalid":
|
||||
exercise_invalid_reload(server, run_dir)
|
||||
results.append(("无效 YAML 配置报告文件与配置项,旧词库继续生效", "PASS"))
|
||||
if args.profile == "boundary":
|
||||
exercise_protocol_boundaries(args, version, port, server, output)
|
||||
results.append(("真实协议的聊天长度、书本标题长度与重复书本提交", "PASS"))
|
||||
for action in ("book-edit", "book-sign"):
|
||||
server.await_line(rf"ICG_PROBE {action}=true thread=", 5)
|
||||
results.append((f"真实书本 {action}", "PASS"))
|
||||
server.send("icg ai test")
|
||||
server.await_line(r"UNAVAILABLE" if args.mode == "OFF" else r"safe=true category=SAFE", 8)
|
||||
if args.mode == "OFF" and ai.request_count:
|
||||
raise RuntimeError("AI HTTP requests occurred in OFF mode")
|
||||
results.append(("AI 测试命令", "PASS"))
|
||||
server.send("icg email test")
|
||||
mail_expected = smtp_result(args.smtp_mode)
|
||||
server.await_line(mail_expected + (r" attempts=3" if mail_expected == "SEND_FAILED" else r" attempts="), 8)
|
||||
if mail_expected == "SENT" and not smtp.messages:
|
||||
raise RuntimeError("SMTP fixture received no message")
|
||||
if mail_expected != "SENT" and smtp.messages:
|
||||
raise RuntimeError("The fixture unexpectedly accepted a faulted SMTP transaction")
|
||||
if args.smtp_mode in ("ssl", "starttls") and not smtp.negotiated:
|
||||
raise RuntimeError("Fixture did not negotiate TLS")
|
||||
results.append(("SMTP " + args.smtp_mode + " " + mail_expected, "PASS"))
|
||||
if is_corrupt:
|
||||
if damaged_file.read_bytes() != damage:
|
||||
raise RuntimeError("The plugin changed the corrupt file")
|
||||
results.append(("SQLite 损坏时保留原文件,聊天、书本、SMTP 仍工作", "PASS"))
|
||||
else:
|
||||
server.send("icg reload")
|
||||
verify_database(run_dir, "ADMIN", "SUCCESS")
|
||||
results.append(("配置重载与管理员操作入库", "PASS"))
|
||||
server.send("icg export")
|
||||
csv_dir = run_dir / "plugins" / "ItxiaohuiContentGuard" / "logs"
|
||||
wait_until(lambda: list(csv_dir.glob("audit-*.csv")), "CSV export", 8)
|
||||
if not any("ICG-public" in path.read_text(encoding="utf-8") for path in csv_dir.glob("audit-*.csv")):
|
||||
raise RuntimeError("CSV did not contain the chat audit")
|
||||
results.append(("SQLite 查询、CSV 导出", "PASS"))
|
||||
finally:
|
||||
exit_code = server.stop()
|
||||
if exit_code != 0:
|
||||
raise RuntimeError(f"First shutdown exited with code {exit_code}")
|
||||
server.await_line(r"ItxiaohuiContentGuard.*ICG_LIFECYCLE scheduler=closed", 0)
|
||||
results.append(("首次关闭", "PASS"))
|
||||
if args.profile == "db-corrupt":
|
||||
damaged_file.rename(damaged_file.with_name("audit.sqlite.fixture-quarantined"))
|
||||
|
||||
restart = ServerProcess(command, run_dir, output / "restart.log")
|
||||
attempts.append(restart)
|
||||
try:
|
||||
restart.await_line(r"Done \([\d.]+s\)!", args.start_timeout)
|
||||
restart.await_line(expected, 5)
|
||||
if args.features:
|
||||
restart.send("icg status")
|
||||
try:
|
||||
restart.await_line(r"database=READY", 3)
|
||||
except RuntimeError:
|
||||
restart.send("icg status")
|
||||
restart.await_line(r"database=READY", 8)
|
||||
finally:
|
||||
exit_code = restart.stop()
|
||||
if exit_code != 0:
|
||||
raise RuntimeError(f"Restart shutdown exited with code {exit_code}")
|
||||
restart.await_line(r"ItxiaohuiContentGuard.*ICG_LIFECYCLE scheduler=closed", 0)
|
||||
results.append(("原目录重启与再次关闭", "PASS"))
|
||||
if args.features and args.profile != "db-corrupt":
|
||||
verify_database(run_dir, "LOCAL", "REPLACE")
|
||||
if args.mode != "OFF":
|
||||
verify_database(run_dir, "AI", "RECORD" if args.mode == "RECORD" else "BLOCK")
|
||||
verify_database(run_dir, "MAIL", smtp_result(args.smtp_mode))
|
||||
results.append(("重启后 SQLite 审核、AI、邮件记录可读", "PASS"))
|
||||
if args.profile == "db-corrupt":
|
||||
if not damaged_file.is_file() or damaged_file.read_bytes() == damage:
|
||||
raise RuntimeError("SQLite did not recreate a database after the corrupt copy was quarantined")
|
||||
results.append(("隔离损坏文件后重启,SQLite 初始化新数据库", "PASS"))
|
||||
except (OSError, RuntimeError, subprocess.SubprocessError) as error:
|
||||
results.append((str(error), "FAIL"))
|
||||
|
||||
lines = [line for server in attempts for line in server.lines]
|
||||
relevant = [line for line in lines if any(token in line for token in
|
||||
("ItxiaohuiContentGuard", "ICGCompatibilityProbe", "ICG_PROBE", "core="))]
|
||||
compatibility = [line for line in lines if "core=" in line or "This server is running" in line]
|
||||
scheduler = [line for line in lines if "ICG_PROBE" in line or "adapter=" in line]
|
||||
errors = [line for line in lines if re.search(
|
||||
r"/ERROR\]|\bException\b|Exception:|NoClassDefFoundError|UnsupportedOperationException|\bSEVERE\b", line)]
|
||||
if any(re.search(r"IllegalStateException|NoClassDefFoundError|UnsupportedOperationException|ICG_PROBE.*error=", line)
|
||||
for line in errors):
|
||||
results.append(("日志含需排查的类加载、线程或能力异常", "FAIL"))
|
||||
if not any(state == "FAIL" for _, state in results):
|
||||
results.append(("探针日志检查", "PASS"))
|
||||
|
||||
for path, values in (("plugin.log", relevant), ("compatibility.txt", compatibility),
|
||||
("scheduler.txt", scheduler), ("errors.log", errors)):
|
||||
(output / path).write_text("\n".join(values) + ("\n" if values else ""), encoding="utf-8")
|
||||
if args.features:
|
||||
(output / "fixture-evidence.json").write_text(
|
||||
json.dumps({"request_count": ai.request_count, "requests": list(ai.requests),
|
||||
"smtp_messages": smtp.message_count, "smtp_mode": args.smtp_mode,
|
||||
"tls_versions": sorted(smtp.negotiated)}, indent=2), encoding="utf-8")
|
||||
|
||||
ended = timestamp()
|
||||
java = subprocess.run([str(args.java), "-version"], capture_output=True, text=True, check=True)
|
||||
source_file = test_dir / "source.json"
|
||||
source = source_file.read_text(encoding="utf-8-sig") if source_file.is_file() else "Build provenance not recorded"
|
||||
environment = (f"started={started}\nended={ended}\n"
|
||||
f"OS={platform.platform()}\nprocessor={platform.processor()}\n"
|
||||
f"isolated_directory={run_dir}\nport={port}\n"
|
||||
f"expected_core={core}\nexpected_minecraft={version}\n"
|
||||
f"plugin_sha256={digest(args.plugin)}\nprobe_sha256={digest(args.probe)}\n"
|
||||
f"server_sha256={digest(server_jar)}\n\n{java.stderr}\n{source}\n")
|
||||
(output / "environment.txt").write_text(environment, encoding="utf-8")
|
||||
rows = "".join(f"<tr><td>{html.escape(name)}</td><td>{state}</td></tr>" for name, state in results)
|
||||
pending = ["压力与故障恢复", "长时间稳定性"] if args.features else PENDING
|
||||
skips = "".join(f"<tr><td>{html.escape(name)}</td><td>SKIP — 未执行</td></tr>" for name in pending)
|
||||
(output / "integration-report.html").write_text(
|
||||
f'<!doctype html><html lang="zh-CN"><meta charset="utf-8"><title>{core} {version}</title>'
|
||||
f"<h1>{core} {version} 兼容适配测试</h1><p>{started} — {ended}</p>"
|
||||
f"<table><thead><tr><th>用例</th><th>结果</th></tr></thead><tbody>{rows}{skips}</tbody></table></html>",
|
||||
encoding="utf-8")
|
||||
stress_page = ('<!doctype html><html lang="zh-CN"><meta charset="utf-8"><title>压力测试</title>'
|
||||
'<h1>压力测试未执行</h1><p>本次服务器运行用于兼容适配验收。正式负载矩阵待执行。</p></html>')
|
||||
(output / "stress-report.html").write_text(stress_page, encoding="utf-8")
|
||||
success = sum(state == "PASS" for _, state in results)
|
||||
failed = sum(state == "FAIL" for _, state in results)
|
||||
final = "FAILED" if failed else "UNVERIFIED"
|
||||
summary = (f"# {core} {version}\n\n状态:{final}\n\n"
|
||||
f"用例通过 {success},失败 {failed},跳过 {len(pending)},合计 {len(results) + len(pending)}。\n\n"
|
||||
f"开始 {started},结束 {ended}。实际品牌和版本见 compatibility.txt。\n\n"
|
||||
f"异常匹配行 {len(errors)},详见 errors.log。\n\n"
|
||||
+ "\n".join(f"- {state} · {name}" for name, state in results) + "\n")
|
||||
(output / "summary.md").write_text(summary, encoding="utf-8")
|
||||
return f"{core}/{version}: {final} ({success} passed, {failed} failed, {len(pending)} skipped)"
|
||||
|
||||
|
||||
def wait_until(check, label, timeout):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
if check():
|
||||
return
|
||||
time.sleep(0.125)
|
||||
raise RuntimeError(f"Expected evidence missing: {label}")
|
||||
|
||||
|
||||
def smtp_result(mode):
|
||||
if mode == "auth-error":
|
||||
return "AUTH_FAILED"
|
||||
return "SEND_FAILED" if mode in ("timeout", "port-closed") else "SENT"
|
||||
|
||||
|
||||
def verify_database(run_dir, kind, action, actor=None, original=None, category=None):
|
||||
file = run_dir / "plugins" / "ItxiaohuiContentGuard" / "database" / "audit.sqlite"
|
||||
|
||||
def query():
|
||||
if not file.is_file():
|
||||
return False
|
||||
try:
|
||||
with sqlite3.connect(file.as_uri() + "?mode=ro", uri=True, timeout=1) as connection:
|
||||
sql = "SELECT 1 FROM audit WHERE kind=? AND action=?"
|
||||
params = [kind, action]
|
||||
if actor is not None:
|
||||
sql += " AND actor=?"
|
||||
params.append(actor)
|
||||
if original is not None:
|
||||
sql += " AND original=?"
|
||||
params.append(original)
|
||||
if category is not None:
|
||||
sql += " AND category=?"
|
||||
params.append(category)
|
||||
return (connection.execute("PRAGMA quick_check").fetchone()[0] == "ok"
|
||||
and connection.execute(sql + " LIMIT 1", params).fetchone() is not None)
|
||||
except sqlite3.Error:
|
||||
return False
|
||||
|
||||
wait_until(query, f"SQLite {kind}/{action}", 8)
|
||||
|
||||
|
||||
def verify_word_commands(server, run_dir):
|
||||
server.send("icg add ICGNewGuardWord")
|
||||
verify_database(run_dir, "ADMIN", "SUCCESS", category="add")
|
||||
start = len(server.lines)
|
||||
server.send("icg words")
|
||||
server.await_line(r"ICGNewGuardWord CONTAINS OTHER LOW", 5, start)
|
||||
start = len(server.lines)
|
||||
server.send("icg test ICGLOCAL ICGNewGuardWord")
|
||||
server.await_line(r"ICGLOCAL \*{3}", 5, start)
|
||||
server.send("icg remove ICGNewGuardWord")
|
||||
verify_database(run_dir, "ADMIN", "SUCCESS", category="remove")
|
||||
start = len(server.lines)
|
||||
server.send("icg test ICGLOCAL ICGNewGuardWord")
|
||||
server.await_line(r"ICGLOCAL ICGNewGuardWord", 5, start)
|
||||
|
||||
|
||||
def exercise_page_limit(args, version, port, server, output):
|
||||
start = len(server.lines)
|
||||
server.send("icgprobe db-tighten")
|
||||
server.await_line(r"ICG_DB_PROBE limit=tightened pages=[1-9]", 5, start)
|
||||
try:
|
||||
player = subprocess.run(["node", str(ROOT / "scripts" / "feature-client.cjs"), str(args.client),
|
||||
str(port), version, "db-fill", args.mode], capture_output=True, encoding="utf-8",
|
||||
timeout=30)
|
||||
(output / "db-fill-client.log").write_text(player.stdout + player.stderr, encoding="utf-8")
|
||||
if player.returncode != 0 or "LOCAL_AUDIT_WHILE_DISK_FULL=PASS" not in player.stdout:
|
||||
raise RuntimeError("Disk fault did not retain player moderation")
|
||||
server.await_line(r"SQLite operation failed: SQLITE_", 5, start)
|
||||
finally:
|
||||
start = len(server.lines)
|
||||
server.send("icgprobe db-restore")
|
||||
server.await_line(r"ICG_DB_PROBE limit=restored pages=[1-9]", 5, start)
|
||||
|
||||
|
||||
def exercise_invalid_reload(server, run_dir):
|
||||
path = run_dir / "plugins" / "ItxiaohuiContentGuard" / "words.yml"
|
||||
original = path.read_bytes()
|
||||
invalid, changed = re.subn(rb"(?m)^( max-matches:).*$", rb"\g<1> 'fixture-invalid'", original)
|
||||
if changed != 1:
|
||||
raise RuntimeError("Could not prepare invalid word fixture")
|
||||
try:
|
||||
path.write_bytes(invalid)
|
||||
start = len(server.lines)
|
||||
server.send("icg reload")
|
||||
server.await_line(r"words\.yml .*settings\.max-matches", 8, start)
|
||||
verify_database(run_dir, "ADMIN", "FAILED", category="reload")
|
||||
start = len(server.lines)
|
||||
server.send("icg test ICGLOCAL 违规内容")
|
||||
server.await_line(r"ICGLOCAL \*{3}", 5, start)
|
||||
finally:
|
||||
path.write_bytes(original)
|
||||
|
||||
|
||||
def exercise_protocol_boundaries(args, version, port, server, output):
|
||||
start = len(server.lines)
|
||||
client = subprocess.run(["node", str(ROOT / "scripts" / "boundary-client.cjs"), str(args.client),
|
||||
str(port), version], capture_output=True, encoding="utf-8", timeout=40)
|
||||
(output / "boundary-client.log").write_text(client.stdout + client.stderr, encoding="utf-8")
|
||||
if client.returncode != 0:
|
||||
raise RuntimeError(f"Protocol boundary client exited with {client.returncode}")
|
||||
for check in ("OVERSIZED_CHAT", "OVERSIZED_TITLE", "RAPID_BOOK_EDIT"):
|
||||
if check + "=PASS" not in client.stdout:
|
||||
raise RuntimeError(f"Missing boundary check: {check}")
|
||||
server.await_line(r"ICG_BOUNDARY book event ICGBoundary03", 5, start)
|
||||
observed = [line for line in server.lines[start:] if "ICG_BOUNDARY book event " in line]
|
||||
if len(observed) != 1:
|
||||
raise RuntimeError("Unexpected number of player book events before core rate/length limiting")
|
||||
|
||||
|
||||
def make_test_tls(run_dir, java):
|
||||
cert = run_dir / "fixture-cert.pem"
|
||||
key = run_dir / "fixture-key.pem"
|
||||
trust = run_dir / "fixture-trust.jks"
|
||||
request_config = run_dir / "openssl.cnf"
|
||||
request_config.write_text("[req]\ndistinguished_name=dn\nprompt=no\n[dn]\nCN=localhost\n", encoding="ascii")
|
||||
keytool = java.with_name("keytool.exe" if platform.system() == "Windows" else "keytool")
|
||||
subprocess.run(["openssl", "req", "-config", str(request_config),
|
||||
"-new", "-x509", "-newkey", "rsa:2048", "-nodes", "-days", "2",
|
||||
"-addext", "subjectAltName=DNS:localhost,IP:127.0.0.1",
|
||||
"-keyout", str(key), "-out", str(cert)], check=True, capture_output=True)
|
||||
shutil.copy2(java.parent.parent / "lib" / "security" / "cacerts", trust)
|
||||
subprocess.run([str(keytool), "-importcert", "-noprompt", "-storetype", "JKS",
|
||||
"-keystore", str(trust), "-storepass", "changeit", "-file", str(cert)],
|
||||
check=True, capture_output=True)
|
||||
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
context.load_cert_chain(cert, key)
|
||||
return context, trust
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
for argument in ("java", "plugin", "probe", "client"):
|
||||
parser.add_argument(f"--{argument}", type=lambda value: Path(value).resolve(), required=True)
|
||||
parser.add_argument("--target", action="append", required=True, help="core/version")
|
||||
parser.add_argument("--start-timeout", type=int, default=240)
|
||||
parser.add_argument("--jobs", type=int, default=4)
|
||||
parser.add_argument("--features", action="store_true")
|
||||
parser.add_argument("--scenario", choices=("normal", "fault", "folia", "commands", "permissions"), default="normal")
|
||||
parser.add_argument("--mode", choices=("OFF", "RECORD", "AUTO", "STRICT"), default="STRICT")
|
||||
parser.add_argument("--profile", choices=("mode-off", "mode-record", "mode-auto", "invalid-json", "folia-special",
|
||||
"smtp-ssl", "smtp-starttls", "smtp-auth-error", "db-corrupt",
|
||||
"folia-reload", "commands", "smtp-timeout", "smtp-port-closed",
|
||||
"permissions", "db-full", "core-pr", "reload-invalid", "boundary"))
|
||||
parser.add_argument("--smtp-mode", choices=("plain", "ssl", "starttls", "auth-error", "timeout", "port-closed"),
|
||||
default="plain")
|
||||
args = parser.parse_args()
|
||||
failed = False
|
||||
with ThreadPoolExecutor(max_workers=args.jobs) as pool:
|
||||
futures = [pool.submit(run_one, args, *target.split("/", 1)) for target in args.target]
|
||||
for future in futures:
|
||||
result = future.result()
|
||||
print(result, flush=True)
|
||||
failed |= ": FAILED" in result or "server.jar missing" in result
|
||||
if failed:
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,218 @@
|
||||
"""Run loopback Minecraft client load and save measured evidence for one core."""
|
||||
|
||||
import argparse
|
||||
import html
|
||||
import json
|
||||
import platform
|
||||
import re
|
||||
import shutil
|
||||
import sqlite3
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from contextlib import closing
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import psutil
|
||||
|
||||
from compat_fixtures import fixtures
|
||||
from importlib.util import module_from_spec, spec_from_file_location
|
||||
|
||||
spec = spec_from_file_location("compat_runner", Path(__file__).with_name("run-compat.py"))
|
||||
shared = module_from_spec(spec)
|
||||
spec.loader.exec_module(shared)
|
||||
ROOT = shared.ROOT
|
||||
|
||||
|
||||
def database_counts(directory):
|
||||
file = directory / "plugins" / "ItxiaohuiContentGuard" / "database" / "audit.sqlite"
|
||||
if not file.is_file():
|
||||
return {"integrity": "missing"}
|
||||
with closing(sqlite3.connect(file.as_uri() + "?mode=ro", uri=True, timeout=3)) as connection:
|
||||
return {"integrity": connection.execute("PRAGMA quick_check").fetchone()[0],
|
||||
"audit": dict(connection.execute("SELECT kind, count(*) FROM audit GROUP BY kind")),
|
||||
"ai_failures": dict(connection.execute(
|
||||
"SELECT reasoning, count(*) FROM audit WHERE kind='API_STATUS' GROUP BY reasoning")),
|
||||
"mail_status": dict(connection.execute(
|
||||
"SELECT action, count(*) FROM audit WHERE kind='MAIL' GROUP BY action")),
|
||||
"word_hits": connection.execute("SELECT count(*) FROM word_hits").fetchone()[0]}
|
||||
|
||||
|
||||
def sample(server, destination, period, run_finished):
|
||||
process = psutil.Process(server.process.pid)
|
||||
process.cpu_percent()
|
||||
try:
|
||||
with destination.open("w", encoding="utf-8") as evidence:
|
||||
while not run_finished.is_set() and server.process.poll() is None:
|
||||
memory = process.memory_info()
|
||||
evidence.write(json.dumps({"time": shared.timestamp(), "cpu_percent": process.cpu_percent(),
|
||||
"rss_bytes": memory.rss, "java_threads": process.num_threads()}) + "\n")
|
||||
evidence.flush()
|
||||
server.send("icgprobe metrics")
|
||||
server.send("icg status")
|
||||
if run_finished.wait(period):
|
||||
break
|
||||
except (OSError, psutil.NoSuchProcess):
|
||||
return
|
||||
|
||||
|
||||
def exercise_faults(server, smtp):
|
||||
def probe(command, pattern):
|
||||
start = len(server.lines)
|
||||
server.send(command)
|
||||
return server.await_line(pattern, 12, start)
|
||||
|
||||
time.sleep(2.5)
|
||||
cases = {
|
||||
"ai-http-503": probe("icg ai test ICG-error", r"HTTP_503"),
|
||||
"ai-timeout": probe("icg ai test ICG-timeout", r"(?:TIMEOUT|DEADLINE)"),
|
||||
"ai-disconnect": probe("icg ai test ICG-disconnect", r"NETWORK"),
|
||||
"ai-circuit-open": probe("icg ai test circuit should be open", r"CIRCUIT_OPEN"),
|
||||
}
|
||||
time.sleep(2.3)
|
||||
cases["ai-circuit-recovery"] = probe("icg ai test circuit should recover", r"safe=true category=SAFE")
|
||||
smtp.fail_connections = True
|
||||
try:
|
||||
cases["smtp-unreachable-retry"] = probe("icg email test", r"SEND_FAILED attempts=3")
|
||||
finally:
|
||||
smtp.fail_connections = False
|
||||
return cases
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
for path in ("java", "plugin", "probe", "client"):
|
||||
parser.add_argument(f"--{path}", type=lambda value: Path(value).resolve(), required=True)
|
||||
parser.add_argument("--target", required=True, help="core/version")
|
||||
parser.add_argument("--players", default="1,10,50,100")
|
||||
parser.add_argument("--seconds", type=int, default=3)
|
||||
parser.add_argument("--interval-ms", type=int, default=950)
|
||||
parser.add_argument("--sample-seconds", type=int, default=3)
|
||||
parser.add_argument("--name", default="stress")
|
||||
parser.add_argument("--spread", action="store_true", help="Teleport Folia stress clients into four distant areas")
|
||||
parser.add_argument("--faults", action="store_true")
|
||||
parser.add_argument("--forced-restart", action="store_true")
|
||||
parser.add_argument("--refill-books", action="store_true")
|
||||
parser.add_argument("--rich-books", action="store_true")
|
||||
parser.add_argument("--smtp-flood", action="store_true")
|
||||
parser.add_argument("--healthy", action="store_true", help="Keep STRICT enabled and pace successful review requests")
|
||||
args = parser.parse_args()
|
||||
|
||||
core, version = args.target.split("/", 1)
|
||||
asset = ROOT / ".test-servers" / core / version
|
||||
stamp = datetime.now(shared.TZ).strftime("%Y%m%d-%H%M%S")
|
||||
directory = asset / "runs" / f"{args.name}-{stamp}"
|
||||
output = ROOT / "test-results" / core / version / f"{args.name}-{stamp}"
|
||||
output.mkdir(parents=True)
|
||||
(directory / "plugins").mkdir(parents=True)
|
||||
shutil.copy2(asset / "server.jar", directory / "server.jar")
|
||||
shutil.copy2(args.plugin, directory / "plugins" / args.plugin.name)
|
||||
shutil.copy2(args.probe, directory / "plugins" / args.probe.name)
|
||||
port = shared.local_port()
|
||||
(directory / "eula.txt").write_text("eula=true\n", encoding="ascii")
|
||||
(directory / "server.properties").write_text(
|
||||
f"server-ip=127.0.0.1\nserver-port={port}\nonline-mode=false\nenforce-secure-profile=false\n"
|
||||
"max-players=120\nview-distance=3\nsimulation-distance=3\npause-when-empty-seconds=-1\n"
|
||||
"level-name=world\nlevel-type=minecraft:flat\nwhite-list=false\nenable-rcon=false\ndifficulty=peaceful\n",
|
||||
encoding="ascii")
|
||||
command = [str(args.java), "-Dicg.compat.features=true", f"-Dicg.compat.spread={'true' if args.spread else 'false'}",
|
||||
"-Dfile.encoding=UTF-8", "-Dterminal.jline=false", "-Dterminal.ansi=false",
|
||||
"-XX:ActiveProcessorCount=8", "-Xms1G", "-Xmx4G", "-Xlog:gc:file=gc.log:time,uptime,level,tags",
|
||||
"-jar", "server.jar", "nogui"]
|
||||
began = shared.timestamp()
|
||||
report = {"start": began, "core": core, "minecraft": version, "os": platform.platform(),
|
||||
"jar_sha256": shared.digest(args.plugin), "server_sha256": shared.digest(asset / "server.jar"),
|
||||
"local_directory": str(directory), "requested_players": args.players, "seconds_per_group": args.seconds,
|
||||
"rich_books": args.rich_books, "refill_books": args.refill_books, "smtp_flood": args.smtp_flood,
|
||||
"healthy_provider": args.healthy,
|
||||
"cases": [], "errors": []}
|
||||
manifest = output / "run.json"
|
||||
manifest.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
with fixtures(args.plugin, directory) as (ai, smtp):
|
||||
server = shared.ServerProcess(command, directory, output / "server.log")
|
||||
try:
|
||||
server.await_line(r"Done \([\d.]+s\)!", 240)
|
||||
report["runtime"] = server.await_line(r"core=.* adapter=", 8)
|
||||
completed = threading.Event()
|
||||
tracker = threading.Thread(target=sample, args=(server, output / "samples.jsonl", args.sample_seconds, completed))
|
||||
tracker.start()
|
||||
try:
|
||||
with (output / "client.log").open("w", encoding="utf-8") as client_log:
|
||||
session = subprocess.run(["node", str(ROOT / "scripts" / "stress-client.cjs"), str(args.client),
|
||||
str(port), version, args.players, str(args.seconds), str(args.interval_ms),
|
||||
"true" if args.refill_books else "false", "true" if args.rich_books else "false",
|
||||
"true" if args.smtp_flood else "false", "true" if args.healthy else "false"],
|
||||
stdout=client_log, stderr=subprocess.STDOUT,
|
||||
timeout=abs(args.seconds) * len(args.players.split(",")) + 400)
|
||||
if session.returncode != 0:
|
||||
report["errors"].append(f"Client exit: {session.returncode}")
|
||||
for line in (output / "client.log").read_text(encoding="utf-8").splitlines():
|
||||
if line.startswith("{"):
|
||||
report["cases"].append(json.loads(line))
|
||||
time.sleep(2)
|
||||
finally:
|
||||
completed.set()
|
||||
tracker.join()
|
||||
report["db_live"] = database_counts(directory)
|
||||
if args.faults:
|
||||
report["faults"] = exercise_faults(server, smtp)
|
||||
if args.forced_restart:
|
||||
server.process.kill()
|
||||
report["forced_exit"] = server.process.wait(timeout=15)
|
||||
except (RuntimeError, OSError, subprocess.SubprocessError, sqlite3.Error) as error:
|
||||
report["errors"].append(str(error))
|
||||
finally:
|
||||
report["exit"] = server.stop()
|
||||
if "forced_exit" in report:
|
||||
restarted = shared.ServerProcess(command, directory, output / "restart.log")
|
||||
try:
|
||||
restarted.await_line(r"Done \([\d.]+s\)!", 240)
|
||||
restarted.send("icg status")
|
||||
try:
|
||||
report["restarted"] = restarted.await_line(r"database=READY", 4)
|
||||
except RuntimeError:
|
||||
restarted.send("icg status")
|
||||
report["restarted"] = restarted.await_line(r"database=READY", 10)
|
||||
except RuntimeError as error:
|
||||
report["errors"].append(str(error))
|
||||
finally:
|
||||
report["exit"] = restarted.stop()
|
||||
report["ai_requests"] = ai.request_count
|
||||
report["smtp_messages"] = smtp.message_count
|
||||
|
||||
report["end"] = shared.timestamp()
|
||||
report["db_after_stop"] = database_counts(directory)
|
||||
report["telemetry"] = [line for line in server.lines if "ICG_METRICS " in line or "database=READY" in line]
|
||||
relevant = [line for line in server.lines if re.search(
|
||||
r"IllegalStateException|NoClassDefFoundError|UnsupportedOperationException|Thread failed|SQLite .*failed|ICG_PROBE teleport error", line)]
|
||||
report["exceptions"] = relevant
|
||||
if (directory / "gc.log").exists():
|
||||
shutil.copy2(directory / "gc.log", output / "gc.log")
|
||||
passed = (len(report["cases"]) == len(args.players.split(",")) and
|
||||
all(test["online"] == test["target"] and test["onlineAfter"] == test["target"] for test in report["cases"])
|
||||
and report["exit"] == 0 and report["db_after_stop"].get("integrity") == "ok"
|
||||
and not report["errors"] and not relevant)
|
||||
if args.smtp_flood and not report["db_after_stop"].get("mail_status", {}).get("QUEUE_FULL", 0):
|
||||
passed = False
|
||||
report["errors"].append("SMTP queue did not reach its configured limit")
|
||||
if args.healthy and any(case["firstClientObservedDeliveries"] != case["messagesSent"] for case in report["cases"]):
|
||||
passed = False
|
||||
report["errors"].append("Healthy provider run missed one or more expected public chat deliveries")
|
||||
report["online_and_integrity_check"] = "PASS" if passed else "FAIL"
|
||||
(output / "evidence.json").write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
markup = ('<!doctype html><html lang="zh-CN"><meta charset="utf-8"><title>实服并发</title>'
|
||||
f'<h1>{html.escape(args.target)} 实服并发</h1><pre>'
|
||||
+ html.escape(json.dumps(report, ensure_ascii=False, indent=2)) + "</pre></html>")
|
||||
(output / "stress-report.html").write_text(markup, encoding="utf-8")
|
||||
if args.name == "stress":
|
||||
(output.parent / "stress-report.html").write_text(
|
||||
'<!doctype html><meta charset="utf-8"><title>实服并发</title>'
|
||||
f'<a href="{output.name}/stress-report.html">实服并发证据</a>', encoding="utf-8")
|
||||
print(f"{args.target} {report['online_and_integrity_check']} {output}")
|
||||
if not passed:
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,92 @@
|
||||
const { createClient } = require(process.argv[2]);
|
||||
const { addMovement } = require("./client-movement.cjs");
|
||||
const [port, version, playerCounts, seconds, interval, supplyBooks, richBooks, smtpFlood, healthy] = process.argv.slice(3);
|
||||
const targets = playerCounts.split(",").map(Number);
|
||||
const clients = [];
|
||||
let observedDeliveries = 0;
|
||||
|
||||
const wait = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
|
||||
function join(index) {
|
||||
return new Promise(resolve => {
|
||||
const username = "ICGLoad" + String(index).padStart(3, "0");
|
||||
const client = createClient({ host: "127.0.0.1", port: Number(port), version, username, auth: "offline", hideErrors: true });
|
||||
addMovement(client, version);
|
||||
const entry = { client, online: false, username, reason: null };
|
||||
clients.push(entry);
|
||||
const timeout = setTimeout(() => { entry.reason = "login timeout"; client.end("login timeout"); resolve(); }, 25_000);
|
||||
function settled() { clearTimeout(timeout); resolve(); }
|
||||
client.once("login", () => { entry.online = true; settled(); });
|
||||
client.once("error", error => { entry.reason = error.message; entry.online = false; settled(); });
|
||||
client.on("end", reason => { entry.reason = reason; entry.online = false; settled(); });
|
||||
for (const event of ["systemChat", "playerChat"]) {
|
||||
client.on(event, data => {
|
||||
if (index === 0 && JSON.stringify(data).includes("ICG-load")) observedDeliveries++;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function main() {
|
||||
let previous = 0;
|
||||
for (const target of targets) {
|
||||
for (let start = previous; start < target; start += 8) {
|
||||
await Promise.all(Array.from({ length: Math.min(8, target - start) }, (_, i) => join(start + i)));
|
||||
await wait(100);
|
||||
}
|
||||
previous = target;
|
||||
await wait(4200);
|
||||
if (supplyBooks === "true") {
|
||||
for (const entry of clients.filter(entry => entry.online)) {
|
||||
entry.client.chat("/icgprobe refill");
|
||||
}
|
||||
await wait(1300);
|
||||
}
|
||||
let messagesSent = 0;
|
||||
let booksSent = 0;
|
||||
const online = clients.filter(entry => entry.online).length;
|
||||
const before = observedDeliveries;
|
||||
const started = Date.now();
|
||||
const stop = started + Number(seconds) * 1000;
|
||||
let cycle = 0;
|
||||
while (Date.now() < stop) {
|
||||
const active = clients.filter(entry => entry.online);
|
||||
const cycleStarted = Date.now();
|
||||
for (let i = 0; i < active.length; i++) {
|
||||
const text = "ICG-load " + target + " " + cycle + " " + i + " "
|
||||
+ (smtpFlood === "true" ? "死全家" : healthy === "true" || cycle % 3 === 0 ? "违 规-内容 English test" :
|
||||
cycle % 3 === 1 ? "ICG-timeout 违违规内容" : "违规内容 ".repeat(20) + "x".repeat(100));
|
||||
active[i].client.chat(text);
|
||||
messagesSent++;
|
||||
if (cycle === 0) {
|
||||
const pages = richBooks === "true"
|
||||
? Array.from({ length: 50 }, (_, page) => "ICG-load " + i + "/" + page + " 违规内容 " + "text ".repeat(90))
|
||||
: ["ICG-load 违 规内容", "Book stress test"];
|
||||
active[i].client.write("edit_book", {
|
||||
hand: 0, pages, ...(target >= 50 ? { title: "ICG stress" } : {}),
|
||||
});
|
||||
booksSent++;
|
||||
}
|
||||
if (healthy === "true") {
|
||||
const next = cycleStarted + (i + 1) * Number(interval) / active.length;
|
||||
await wait(Math.max(0, next - Date.now()));
|
||||
}
|
||||
}
|
||||
cycle++;
|
||||
await wait(healthy === "true" && active.length ? 0 : Number(interval));
|
||||
}
|
||||
await wait(2500);
|
||||
console.log(JSON.stringify({
|
||||
target, online, onlineAfter: clients.filter(entry => entry.online).length,
|
||||
durationMs: Date.now() - started, messagesSent, booksSent,
|
||||
firstClientObservedDeliveries: observedDeliveries - before,
|
||||
disconnects: clients.filter(entry => !entry.online).map(entry => ({ player: entry.username, reason: entry.reason })),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(error => {
|
||||
console.error(error.stack);
|
||||
process.exitCode = 1;
|
||||
}).finally(() => {
|
||||
for (const entry of clients) entry.client.end("Stress run complete");
|
||||
});
|
||||
@@ -0,0 +1,226 @@
|
||||
"""Create a Chinese compatibility matrix from locally recorded evidence."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import statistics
|
||||
import xml.etree.ElementTree as ET
|
||||
from collections import Counter
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
RESULTS = ROOT / "test-results"
|
||||
CORES = {"paper": "Paper", "purpur": "Purpur", "spigot": "Spigot", "folia": "Folia", "leaves": "Leaves"}
|
||||
VERSIONS = ("1.21.1", "1.21.4", "1.21.8", "1.21.11")
|
||||
JAR = ROOT / "build" / "libs" / "ItxiaohuiContentGuard-0.2.0-dev.jar"
|
||||
|
||||
|
||||
def jread(path):
|
||||
return json.loads(path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def evidence(directory, pattern, expected):
|
||||
for path in sorted(directory.glob(pattern), reverse=True):
|
||||
data = jread(path)
|
||||
if data.get("jar_sha256") == expected:
|
||||
return path, data
|
||||
return None, None
|
||||
|
||||
|
||||
def integration(core, version, sha):
|
||||
directory = RESULTS / core / version
|
||||
file = directory / "summary.md"
|
||||
environment = directory / "environment.txt"
|
||||
if not file.exists() or not environment.exists() or f"plugin_sha256={sha}" not in environment.read_text(encoding="utf-8"):
|
||||
reason = "未测"
|
||||
if core == "folia" and version == "1.21.1":
|
||||
reason = "Fill v3 发行接口返回 404"
|
||||
if core == "leaves" and version == "1.21.11":
|
||||
reason = "上游仅提供实验版"
|
||||
return (reason, False, False)
|
||||
text = file.read_text(encoding="utf-8")
|
||||
passed = re.search(r"通过 (\d+),失败 (\d+),跳过 (\d+)", text)
|
||||
stage = "ALPHA · " if core == "folia" and version == "1.21.4" else ""
|
||||
read = f'{stage}[{passed.group(1)} 通过 / {passed.group(2)} 失败 / {passed.group(3)} 跳过]({core}/{version}/summary.md)'
|
||||
return (read, int(passed.group(2)) == 0, int(passed.group(2)) > 0)
|
||||
|
||||
|
||||
def stress_cell(core, version, sha):
|
||||
directory = RESULTS / core / version
|
||||
path, result = evidence(directory, "stress-*/evidence.json", sha)
|
||||
if not result:
|
||||
return "压力未完成", False
|
||||
state = result["online_and_integrity_check"]
|
||||
clients = max((case["online"] for case in result["cases"]), default=0)
|
||||
link = path.parent.relative_to(RESULTS).as_posix() + "/stress-report.html"
|
||||
return f"[{clients} 在线 · {state}]({link})", state == "FAIL"
|
||||
|
||||
|
||||
def report():
|
||||
with JAR.open("rb") as binary:
|
||||
sha = hashlib.file_digest(binary, "sha256").hexdigest()
|
||||
paragraphs = [
|
||||
"# 真实服务端兼容与发布验收",
|
||||
"",
|
||||
"报告生成时间 " + datetime.now(timezone(timedelta(hours=8))).isoformat(timespec="seconds") + "。",
|
||||
"",
|
||||
"候选构建为 `0.2.0-dev`,SHA-256 为 `" + sha + "`。",
|
||||
"",
|
||||
"运行环境为 Windows 11 x64、Intel Core i9-12900KS(24 逻辑处理器)、Microsoft OpenJDK 21.0.13.0.2。"
|
||||
"测试核心使用各自的发行包或 BuildTools 生成的 Spigot JAR,各自的世界、配置、插件目录、启动命令和日志。"
|
||||
"协议客户端由本机程序驱动,连接到回环地址上的真实 Minecraft 服务端。",
|
||||
"",
|
||||
"## 正式兼容状态",
|
||||
"",
|
||||
"| 核心 | 真实启动与功能 | 1.21.8 并发抽样 | 1.21.8 故障抽样 | 最终状态 |",
|
||||
"|---|---|---|---|---|",
|
||||
]
|
||||
version_rows = []
|
||||
stress_rows = []
|
||||
for core, name in CORES.items():
|
||||
versions = [integration(core, version, sha) for version in VERSIONS]
|
||||
version_stress = [stress_cell(core, version, sha) for version in VERSIONS]
|
||||
version_faults = [evidence(RESULTS / core / version, "fault-*/evidence.json", sha) for version in VERSIONS]
|
||||
directory = RESULTS / core / "1.21.8"
|
||||
path, sample = evidence(directory, "stress-*/evidence.json", sha)
|
||||
fault_path, faults = evidence(directory, "fault-*/evidence.json", sha)
|
||||
failed = (any(item[2] for item in versions) or any(item[1] for item in version_stress)
|
||||
or any(data and data["online_and_integrity_check"] == "FAIL" for _, data in version_faults))
|
||||
stress = "未测"
|
||||
failed |= bool(sample and sample["online_and_integrity_check"] != "PASS")
|
||||
failed |= bool(faults and faults["online_and_integrity_check"] != "PASS")
|
||||
if sample:
|
||||
case = sample["cases"][-1] if sample["cases"] else {"online": 0, "messagesSent": 0, "booksSent": 0, "durationMs": 0}
|
||||
state = sample["online_and_integrity_check"]
|
||||
folder = path.parent.relative_to(RESULTS).as_posix()
|
||||
stress = f'[{case["online"]} 在线 · {state}]({folder}/stress-report.html)'
|
||||
host = [json.loads(line) for line in path.with_name("samples.jsonl").read_text(encoding="utf-8").splitlines()]
|
||||
db = sample["db_after_stop"]
|
||||
minimums = []
|
||||
for line in sample["telemetry"]:
|
||||
gauge = re.search(r"tps=\[([\d.]+)[^\]]*\] mspt=([\d.]+)", line)
|
||||
if gauge:
|
||||
minimums.append(tuple(map(float, gauge.groups())))
|
||||
cpu = f'{max(row["cpu_percent"] for row in host):.1f}%' if host else "缺失"
|
||||
memory = f'{max(row["rss_bytes"] for row in host) / (1024 ** 2):.0f}' if host else "缺失"
|
||||
tps = f'{min(tps for tps, _ in minimums):.2f}' if minimums else "不可用"
|
||||
mspt = f'{max(mspt for _, mspt in minimums):.2f}' if minimums else "不可用"
|
||||
stress_rows.append(f'| {name} | {case["online"]} | {case["messagesSent"]} | {case["booksSent"]} | '
|
||||
f'{case["durationMs"] / 1000:.2f} s | {sample["ai_requests"]} | {db.get("integrity", "缺失")} | '
|
||||
f'{cpu} | {memory} MiB | {tps} / {mspt} | {len(sample["exceptions"])} |')
|
||||
failure_view = (f'[{faults["online_and_integrity_check"]}]({fault_path.parent.relative_to(RESULTS).as_posix()}/stress-report.html)'
|
||||
if faults else "未测")
|
||||
count = sum(item[1] for item in versions)
|
||||
final = "FAILED" if failed else "UNVERIFIED"
|
||||
paragraphs.append(f"| {name} | {count} / {len(VERSIONS)} 目标组合实服通过 | {stress} | {failure_view} | **{final}** |")
|
||||
version_cells = []
|
||||
for version, load, (fault_file, fault) in zip(versions, version_stress, version_faults):
|
||||
cell = f"{version[0]}<br>{load[0]}"
|
||||
if fault:
|
||||
link = fault_file.with_name("stress-report.html").relative_to(RESULTS).as_posix()
|
||||
cell += f'<br>[故障 {fault["online_and_integrity_check"]}]({link})'
|
||||
version_cells.append(cell)
|
||||
version_rows.append("| " + name + " | " + " | ".join(version_cells) + " |")
|
||||
|
||||
paragraphs.extend(["", "## Minecraft 小版本矩阵", "",
|
||||
"| 核心 | 1.21.1 | 1.21.4 | 1.21.8 | 1.21.11 |",
|
||||
"|---|---|---|---|---|", *version_rows, "",
|
||||
"每个版本目录包含环境、JAR SHA-256、独立启动/重启日志、调度权属探针、客户端输出、集成 HTML、"
|
||||
"异常摘要与测试时间。Folia 1.21.4 测的是上游 ALPHA 构建。"])
|
||||
suites = [ET.parse(path).getroot() for path in (RESULTS / "unit" / "candidate").rglob("TEST-*.xml")]
|
||||
numbers = Counter()
|
||||
for suite in suites:
|
||||
for name in ("tests", "failures", "errors", "skipped"):
|
||||
numbers[name] += int(suite.get(name, "0"))
|
||||
paragraphs.extend(["", "## 自动化单元测试", "",
|
||||
f'JUnit 5 共 {numbers["tests"]} 例,失败 {numbers["failures"]},错误 {numbers["errors"]},'
|
||||
f'跳过 {numbers["skipped"]}。参见 [JUnit](unit/candidate/index.html)、'
|
||||
'[MockBukkit](unit/candidate/mockbukkit/index.html) 和同目录 XML。', "",
|
||||
"## 1.21.8 压力抽样", "",
|
||||
"| 核心 | 峰值阶段客户端 | 该阶段发送聊天 | 该阶段提交书本 | 该阶段耗时含等待 | 全程 AI 到达 | SQLite | Java 进程 CPU 峰值 | RSS 峰值 | 最低 1 分钟 TPS / 最大采样 MSPT | 检测到的关键异常 |",
|
||||
"|---|---:|---:|---:|---:|---:|---|---:|---:|---:|---:|", *stress_rows, "",
|
||||
"上表聊天和书本数量为真实客户端发送量。服务端触发的事件、AI 接收数、送达观察值以及服务器限流结果见原始报告。"
|
||||
"CPU 百分比按单逻辑处理器计,超过 100% 表示多个逻辑处理器。"
|
||||
"Folia 的 TPS/MSPT 来自全局接口。区域活动由分区事件计数证明。"
|
||||
"Spigot 缺少相同的 Bukkit 指标接口。每次运行另保存 RSS、线程、GC、AI/SMTP 队列和数据库操作耗时采样。"])
|
||||
paragraphs.extend(["", "## 长时间稳定性", ""])
|
||||
for tag, label in (("stability-30m", "30 分钟"), ("stability-2h", "2 小时"), ("stability-8h", "8 小时")):
|
||||
manifests = sorted(RESULTS.glob(f"*/*/{tag}-*/run.json"), reverse=True)
|
||||
manifest = manifests[0] if manifests else None
|
||||
if manifest:
|
||||
config = jread(manifest)
|
||||
final = manifest.with_name("evidence.json")
|
||||
if final.is_file():
|
||||
outcome = jread(final)
|
||||
status = f'已结束 · {outcome["online_and_integrity_check"]}'
|
||||
samples = [json.loads(line) for line in manifest.with_name("samples.jsonl")
|
||||
.read_text(encoding="utf-8").splitlines()]
|
||||
if len(samples) >= 10:
|
||||
window = max(3, len(samples) // 10)
|
||||
initial = statistics.median(row["rss_bytes"] for row in samples[window:window * 2])
|
||||
final_rss = statistics.median(row["rss_bytes"] for row in samples[-window:])
|
||||
status += f" · 热身后/尾段 RSS 中位数 {initial / 1024 ** 2:.0f}/{final_rss / 1024 ** 2:.0f} MiB"
|
||||
detail = final
|
||||
else:
|
||||
status = "已启动,等待完成证据"
|
||||
detail = manifest.with_name("samples.jsonl")
|
||||
if detail.is_file():
|
||||
rows = detail.read_text(encoding="utf-8").splitlines()
|
||||
if rows:
|
||||
try:
|
||||
status += ",最近采样 " + json.loads(rows[-1])["time"]
|
||||
except (KeyError, ValueError):
|
||||
pass
|
||||
link = detail.relative_to(RESULTS).as_posix()
|
||||
paragraphs.append(f'- {label} · {config["core"]} {config["minecraft"]} · {status} · [{detail.name}]({link})')
|
||||
else:
|
||||
paragraphs.append(f"- {label} · 未执行")
|
||||
paragraphs.extend(["", "## 专项实服证据", ""])
|
||||
for title, directory in (
|
||||
("Folia 跨区域、离线回调及实体失效 1.21.8", "folia/1.21.8/folia-special"),
|
||||
("Folia 跨区域、离线回调及实体失效 1.21.11", "folia/1.21.11/folia-special"),
|
||||
("Folia 审核进行中热重载", "folia/1.21.8/folia-reload"),
|
||||
("AI 关闭时本地审核", "spigot/1.21.8/mode-off"),
|
||||
("AI RECORD", "paper/1.21.8/mode-record"),
|
||||
("AI AUTO", "folia/1.21.8/mode-auto"),
|
||||
("AI 无效 JSON 降级", "folia/1.21.8/invalid-json"),
|
||||
("SMTP 直接 TLS", "paper/1.21.8/smtp-ssl"),
|
||||
("SMTP STARTTLS", "folia/1.21.8/smtp-starttls"),
|
||||
("SMTP 密码错误", "spigot/1.21.8/smtp-auth-error"),
|
||||
("SMTP 端口拒绝", "purpur/1.21.8/smtp-port-closed"),
|
||||
("SMTP 发送阶段超时", "leaves/1.21.8/smtp-timeout"),
|
||||
*((f"{name} 私聊别名与词库管理", f"{core}/1.21.8/commands") for core, name in CORES.items()),
|
||||
*((f"{name} 管理员通知与书本权限", f"{core}/1.21.8/permissions") for core, name in CORES.items()),
|
||||
*((f"{name} SQLite 文件损坏与隔离恢复", f"{core}/1.21.8/db-corrupt") for core, name in CORES.items()),
|
||||
*((f"{name} 实服 SQLite 页限故障", f"{core}/1.21.8/db-full") for core, name in CORES.items()),
|
||||
*((f"{name} 无效配置热重载", f"{core}/1.21.8/reload-invalid") for core, name in CORES.items()),
|
||||
*((f"{name} 聊天与书本协议边界", f"{core}/1.21.8/boundary") for core, name in CORES.items()),
|
||||
):
|
||||
if (RESULTS / directory / "summary.md").is_file():
|
||||
paragraphs.append(f"- [{title}]({directory}/summary.md)")
|
||||
for prefix, title in (("rich-books", "50 页书本并发"), ("smtp-queue", "SMTP 邮件队列饱和")):
|
||||
for file in sorted(RESULTS.glob(f"*/*/{prefix}-*/evidence.json")):
|
||||
outcome = jread(file)
|
||||
if outcome.get("jar_sha256") == sha:
|
||||
link = file.with_name("stress-report.html").relative_to(RESULTS).as_posix()
|
||||
paragraphs.append(f'- [{outcome["core"]} {title} · {outcome["online_and_integrity_check"]}]({link})')
|
||||
paragraphs.extend(["", "## 修复与复测", "",
|
||||
"- Windows 控制台日志转码影响关闭标记。改用固定 ASCII 标记后复测自动关闭、重启。",
|
||||
"- Spigot 新版正式构建返回包含 `-Spigot-` 的构建串。修复识别并补回归用例,四个小版本重测。",
|
||||
"- Spigot 立即重启又关闭时碰到 SQLite 尚在初始化。关闭期间协调初始化,并检查重启后数据库真正就绪。",
|
||||
"- Folia 协议客户端需要完成传送确认和常规移动同步,书本限速才会按真实客户端方式推进。三个 Folia 构建补齐协议后通过书本编辑与签署。",
|
||||
"- Folia 首轮 100 并发仅 98 个进入游戏,并且测试探针在加入事件中调用传送失败。提高测试服容量并在玩家实体调度器运行传送,保留前两轮完整证据。",
|
||||
"- 多轮失败与修复过程见各版本 `previous/` 或独立压力运行目录。",
|
||||
"", "## 发布门禁余项", "",
|
||||
"- 逐核心、逐版本的专项故障完整矩阵,以及所有队列满载、长时间内存趋势仍待完整证据。",
|
||||
"- 磁盘空间不足使用探针对真实 SQLite 连接设置页数上限模拟。操作系统级磁盘配额未测试。",
|
||||
"- 短时书本压测记录协议提交量与实际 Bukkit 事件数。Minecraft 自身的校验、频控及已签署物品会挡住部分重复提交。",
|
||||
"- 超协议长度的聊天与书本标题由服务端解码器拒绝。协议边界报告保留预期断开原因与 Bukkit 实际收到的首个书本事件。",
|
||||
"- 用户私聊命令只覆盖识别到的别名。第三方直接发消息需要服务接口。多页使用逐页本地匹配。默认禁言随进程重启清空。",
|
||||
"", "更多使用与配置说明参见 [README](../README.md)。", ""])
|
||||
(RESULTS / "README.md").write_text("\n".join(paragraphs), encoding="utf-8")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
report()
|
||||
Reference in new issue
Block a user