Files
breeze/tests/daochi_native_zi_test.py
T
waozi 458f4fb5eb Run the test harnesses on the Ziran 0.6 toolchain
Point every harness's default toolchain at build/packages/ziran/build,
drop the removed `zi2zx bundle` form, link the generated C each harness
actually gets (whole generated folders where the test has no stubs, and
Kryon's package-prefixed frame pacing module), and stop listing the
header-only type module in the modal rules test. Twenty-two more test
targets pass from a clean export; plans/2026-11-17-test-harnesses.md
lists the 54 that still fail and why.
2026-11-17 03:12:21 -03:00

202 lines
8.8 KiB
Python

#!/usr/bin/env python3
"""Exercise Inbe's Ziran Daochi adapter through a disposable HTTP server."""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
import hashlib
import json
import os
import re
import subprocess
import sys
import tempfile
import threading
ROOT = Path(__file__).resolve().parent.parent
COMPILER = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT / "build/packages/ziran/build/bin/zi2c"
USER_ID = "a" * 64
NONCE = "c" * 64
def GeneratedSource(generated, name):
"""A generated C file; package modules carry their package's prefix."""
path = generated / name
if path.exists():
return path
prefixed = sorted(path.parent.glob("p" + "[0-9a-f]" * 16 + "_" + path.name))
return prefixed[0] if len(prefixed) == 1 else path
def ResolveGeneratedAuthSessionType(header_text):
"""Resolve the common Session pointer in the generated account API."""
signatures = {
"LoginAccount": r"String base_url, AccountKeys\* account, String client_id, {session}\* session",
"RequestAccount": r"String base_url, AccountKeys\* account, String client_id, {session}\* session, int64_t now, String method, String path, String body, Slice output, int32_t\* status",
"SyncAccountPayload": r"String base_url, AccountKeys\* account, String client_id, {session}\* session, Device device, int64_t now, String body, Slice output, int32_t\* status",
}
types = set()
for name, parameters in signatures.items():
declarations = re.findall(r"\b" + name + r"\s*\([^;{}]*\)\s*;", header_text)
pattern = r"AuthResult\s+" + name + r"\s*\(\s*" + parameters.format(
session=r"(?P<session>[A-Za-z_]\w*)"
) + r"\s*\)\s*;"
matches = list(re.finditer(pattern, header_text))
if len(declarations) != 1 or len(matches) != 1:
raise ValueError(f"generated auth contract: missing, ambiguous or malformed {name} declaration")
types.add(matches[0].group("session"))
if len(types) != 1:
raise ValueError("generated auth contract: account functions use different Session types")
session_type = types.pop()
typedef = rf"\btypedef\s+struct\s+{re.escape(session_type)}\s+{re.escape(session_type)}\s*;"
if len(re.findall(typedef, header_text)) != 1:
raise ValueError(f"generated auth contract: missing or ambiguous typedef for {session_type}")
return session_type
class Handler(BaseHTTPRequestHandler):
friends_requests = 0
registrations = 0
syncs = 0
def log_message(self, *_args):
pass
def respond(self, status, body):
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_GET(self):
if self.path == f"/api/v1/sync/challenge?user_id={USER_ID}":
self.respond(200, json.dumps({"nonce": NONCE}).encode())
return
if self.path == "/api/v1/friends":
assert self.headers.get("Authorization") == "Bearer token-from-server"
assert self.headers.get("X-Daochi-User") == USER_ID
assert self.headers.get("X-Daochi-Client") == "client-1"
type(self).friends_requests += 1
if type(self).friends_requests == 1:
self.respond(401, b"{}")
else:
self.respond(200, b'{"friends":[]}')
return
self.respond(404, b"{}")
def do_POST(self):
body = self.rfile.read(int(self.headers.get("Content-Length", "0")))
if self.path == "/api/v1/account/devices":
data = json.loads(body)
assert self.headers.get("Authorization") == "Bearer token-from-server"
assert data["app_id"] == "inbe"
assert data["device_key_id"] == "b" * 64
assert data["public_key"] == "b" * 64
assert data["nonce"] == "01" * 32
assert data["expires_at"] == 1700000301
assert data["signature"] == "bb" * 2420
type(self).registrations += 1
self.respond(200, b"{}")
return
if self.path == "/api/v1/sync":
envelope = json.loads(self.headers["X-Daochi-Tx"])
assert self.headers.get("Authorization") == "Bearer token-from-server"
assert envelope["protocol_version"] == 6
assert envelope["app_id"] == "inbe"
assert envelope["device_key_id"] == "b" * 64
assert envelope["tx_id"] == "02" * 32
assert envelope["nonce"] == "03" * 32
assert envelope["expires_at"] == 1700000301
assert envelope["body_sha256"] == hashlib.sha256(body).hexdigest()
assert envelope["signature"] == "bb" * 2420
assert envelope["device_signature"] == "d" * 128
assert body == b'{"protocol_version":6}'
type(self).syncs += 1
self.respond(200, b'{"server_version":7}')
return
try:
data = json.loads(body)
signature = bytes.fromhex(self.headers.get("X-Daochi-Signature", ""))
except ValueError:
data = {}
signature = b""
valid = (self.path == "/api/v1/sync/login" and
self.headers.get("X-Daochi-User") == USER_ID and
self.headers.get("Content-Type") == "application/json" and
data.get("user_id_hash") == USER_ID and
data.get("public_key") == "pub" and
len(signature) == 2420 and
signature[:64] == hashlib.sha256(body).hexdigest().encode() and
signature[64:128] == NONCE.encode() and
signature[128:] == b"\xbb" * (2420 - 128))
if not valid:
self.respond(400, b"{}")
elif data.get("client_id") == "bad-client":
self.respond(401, b"{}")
elif data.get("client_id") == "client-1":
self.respond(200, json.dumps({
"auth_token": "token-from-server",
"expires_in_seconds": 1800,
"server_time": 1700000000,
}).encode())
else:
self.respond(400, b"{}")
with tempfile.TemporaryDirectory(prefix="inbe-daochi-native-") as temporary:
generated = Path(temporary) / "generated"
executable = Path(temporary) / "client"
subprocess.run([
str(COMPILER), "--no-main", "--root", str(ROOT / "src"),
"--module-path", str(ROOT / "build/packages/ziran/std"),
"--module-path", str(ROOT / "build/packages/daochi-client"),
"--module-path", str(ROOT / "build/packages/kryon/src/ui"),
*subprocess.check_output(["sh", str(ROOT / "scripts/package-paths.sh"), str(ROOT / "build/packages")], text=True).split(),
"--module-path", str(ROOT / "build/packages/kryon/src/backend"),
"--module-path", str(ROOT / "build/packages/css/src"),
"--module-path", str(ROOT / "build/packages/game2d/src"),
"-o", str(generated), str(ROOT / "src/storage/daochi_native.zi"),
], check=True)
session_type = ResolveGeneratedAuthSessionType(
(generated / "storage/daochi_native.h").read_text()
+ "\n" + (generated / "auth.h").read_text()
)
# Only the account path is linked; unused storage code is dropped.
sources = [
"storage/daochi_native.c", "daochi_account.c", "delegate_store.c",
"client.c", "auth.c", "url.c", "wire.c",
"sync.c", "transaction.c", "keys.c", "crypto.c", "sha256.c", "Oqs.c",
"net_http_curl_linux.c", "json_scan.c", "text.c", "text_buffer.c",
"byte_text_linux.c", "c_string.c",
]
subprocess.run([
os.environ.get("CC", "cc"), "-std=c11", "-O0", "-Wall", "-Wextra",
"-Werror", "-Wno-unused-function", "-Wno-unused-variable",
"-ffunction-sections", "-fdata-sections", "-Wl,--gc-sections",
f"-DDAOCHI_FIXTURE_SESSION_TYPE={session_type}",
f"-I{ROOT / 'build/packages/ziran/include'}", f"-I{generated}",
f"-I{generated / 'storage'}",
str(ROOT / "tests/daochi_native_zi_test.c"),
*(str(GeneratedSource(generated, source)) for source in sources),
"-l:libcurl.so.4", "-lz", "-o", str(executable),
], check=True)
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
environment = os.environ.copy()
environment.pop("DISPLAY", None)
environment.pop("WAYLAND_DISPLAY", None)
subprocess.run([
str(executable), f"127.0.0.1:{server.server_port}"
], check=True, env=environment)
assert Handler.friends_requests == 2
assert Handler.registrations == 1
assert Handler.syncs == 1
finally:
server.shutdown()
server.server_close()
thread.join()