"""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"{html.escape(name)}{state}" for name, state in results) pending = ["压力与故障恢复", "长时间稳定性"] if args.features else PENDING skips = "".join(f"{html.escape(name)}SKIP — 未执行" for name in pending) (output / "integration-report.html").write_text( f'{core} {version}' f"

{core} {version} 兼容适配测试

{started} — {ended}

" f"{rows}{skips}
用例结果
", encoding="utf-8") stress_page = ('压力测试' '

压力测试未执行

本次服务器运行用于兼容适配验收。正式负载矩阵待执行。

') (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()