From bbdfec2174ab4c43ee24a97d8eb3a769b69c9d34 Mon Sep 17 00:00:00 2001 From: cflmy Date: Tue, 24 Nov 2026 14:41:40 +0800 Subject: [PATCH] test: cover snapshot integrity, disconnects and timeout isolation --- scripts/test-complete-reader.py | 41 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/scripts/test-complete-reader.py b/scripts/test-complete-reader.py index 9ed7f37..d3f993a 100755 --- a/scripts/test-complete-reader.py +++ b/scripts/test-complete-reader.py @@ -9,8 +9,11 @@ import os from pathlib import Path, PurePosixPath import re import shutil +import socket +import struct import subprocess import tempfile +import time import urllib.error import urllib.request import zipfile @@ -179,6 +182,30 @@ def check(root): assert headers.get_content_type() == "application/zip" and "attachment" in headers["Content-Disposition"] assert headers["Cache-Control"] == "no-store" and headers["X-Content-Type-Options"] == "nosniff" verify_zip(payload, os.environ.get("SNAPSHOT_ZIP"), headers["X-Marqdo-Manifest-SHA256"]) + manifest_digest = headers["X-Marqdo-Manifest-SHA256"] + with zipfile.ZipFile(io.BytesIO(payload)) as archive: + manifest = json.loads(archive.read("manifest.json")) + assert sha(archive.read("manifest.json") + b" ") != manifest_digest + for entry in manifest["files"] + manifest["assets"]: + assert sha(archive.read(entry["path"]) + b" ") != entry["sha256"] + post(browser, base, "/api/exports", token, {**selection, "unknown": "x" * 8192}, 429) + + auth = next(handler.cookiejar for handler in browser.handlers if hasattr(handler, "cookiejar")) + cookies = "; ".join(f"{cookie.name}={cookie.value}" for cookie in auth) + body = json.dumps(selection).encode() + host, port = base.removeprefix("http://").rsplit(":", 1) + request = (f"POST /api/exports HTTP/1.1\r\nHost: {host}:{port}\r\n" + f"Origin: {base}\r\nX-View-CSRF: {token}\r\nCookie: {cookies}\r\n" + f"Content-Type: application/json\r\nContent-Length: {len(body)}\r\n\r\n").encode() + with socket.create_connection((host, int(port)), timeout=10) as abandoned: + abandoned.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4096) + abandoned.sendall(request + body) + assert abandoned.recv(16).startswith(b"HTTP/1.1 200") + abandoned.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)) + deadline = time.monotonic() + 5 + while list(scratch.iterdir()) and time.monotonic() < deadline: + time.sleep(.01) + assert not list(scratch.iterdir()), list(scratch.iterdir()) subprocess.run([*args, "export", str(project), "--out", str(root / "pure")], check=True, env=env) assert "本次执行快照" not in (root / "pure/index.html").read_text() assert count.read_text().splitlines() == ["call"] @@ -238,7 +265,10 @@ def check(root): html = archive.read("index.html").decode() assert "