#!/usr/bin/env python3 """Write the committed cross-language conformance vectors. The bytes under `packages/node/native/testdata/` are the reference both the Python and the C++ conformance tests assert against. They are committed so the C++ test can run without a Python step, and `--check` exists so they can never drift from the schema unnoticed: if a schema edit changes the canonical message's encoding, `--check` fails and the change has to be acknowledged. Usage:: python scripts/generate_protocol_goldens.py # rewrite vectors python scripts/generate_protocol_goldens.py --check # fail if stale """ from __future__ import annotations import argparse import pathlib import sys REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent sys.path.insert(0, str(REPO_ROOT / "packages/node")) from meshnet_node.native_protocol import conformance # noqa: E402 def _vectors() -> dict[str, bytes]: return { conformance.GOLDEN_SESSION_REQUEST: conformance.serialize( conformance.canonical_session_request() ), conformance.GOLDEN_CAPABILITY_REPORT: conformance.serialize( conformance.canonical_capability_report() ), } def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--check", action="store_true") args = parser.parse_args() out_dir = conformance.TESTDATA_DIR out_dir.mkdir(parents=True, exist_ok=True) stale = [] for name, payload in _vectors().items(): path = out_dir / name if args.check: if not path.is_file() or path.read_bytes() != payload: stale.append(name) continue path.write_bytes(payload) print(f"wrote {path.relative_to(REPO_ROOT)} ({len(payload)} bytes)") if stale: print( "conformance vectors are stale: " + ", ".join(stale) + "\n" "The canonical message no longer encodes to the committed bytes. If " "that is intended, run: python scripts/generate_protocol_goldens.py", file=sys.stderr, ) return 1 if args.check: print("conformance vectors are up to date") return 0 if __name__ == "__main__": raise SystemExit(main())