#!/usr/bin/env python3 """Offline tests for product-independent component release transactions.""" from __future__ import annotations import base64 import importlib.util import json import os from pathlib import Path import shutil import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] PACKAGE = ROOT / "scripts" / "package-component.py" PUBLISH = ROOT / "scripts" / "publish-component-release.py" VERIFY = ROOT / "scripts" / "verify-locked-component-feeds.py" VERIFY_RELWITHDEBINFO = ROOT / "scripts" / "verify-relwithdebinfo.py" class IndependentComponentReleaseTests(unittest.TestCase): def setUp(self) -> None: self.temporary = tempfile.TemporaryDirectory( prefix="echoion-independent-component." ) self.root = Path(self.temporary.name) self.registry = self.root / "components.json" self.output = self.root / "dist" self.feed = self.root / "feed" self.key = self.root / "key.pem" self.public_key = self.root / "public.pem" self.registry.write_text( json.dumps( { "schemaVersion": 1, "components": { "EchoionApi": { "repository": "Void/EchoionApi", "packageId": "echoion-api", "kind": "sdk", "activation": "restart-required", "dependencies": {}, }, "EchoionWeb": { "repository": "Void/EchoionWeb", "packageId": "echoion-web", "kind": "runtime-module", "activation": "restart-required", "dependencies": { "EchoionApi": ">=2.0.0 <3.0.0" }, }, }, } ) ) subprocess.run( [ "openssl", "genpkey", "-algorithm", "ED25519", "-out", str(self.key), ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) self.key.chmod(0o600) subprocess.run( [ "openssl", "pkey", "-in", str(self.key), "-pubout", "-out", str(self.public_key), ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) def tearDown(self) -> None: self.temporary.cleanup() def test_component_elf_requires_debug_information(self) -> None: compiler = shutil.which("cc") if compiler is None: self.skipTest("C compiler is unavailable") specification = importlib.util.spec_from_file_location( "echoion_package_component", PACKAGE ) self.assertIsNotNone(specification) self.assertIsNotNone(specification.loader) module = importlib.util.module_from_spec(specification) specification.loader.exec_module(module) package_root = self.root / "elf-stage" package_root.mkdir() source = self.root / "fixture.c" executable = package_root / "fixture" source.write_text("int main(void) { return 0; }\n") subprocess.run( [compiler, "-O2", "-g0", str(source), "-o", str(executable)], check=True, ) with self.assertRaisesRegex(SystemExit, "has no debug information"): module.require_debug_information(package_root) rejected = subprocess.run( [str(VERIFY_RELWITHDEBINFO), "--root", str(package_root)], check=False, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) self.assertNotEqual(rejected.returncode, 0) self.assertIn("has no debug information", rejected.stderr) subprocess.run( [compiler, "-O2", "-g", str(source), "-o", str(executable)], check=True, ) module.require_debug_information(package_root) accepted = subprocess.run( [str(VERIFY_RELWITHDEBINFO), "--root", str(package_root)], check=False, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) self.assertEqual(accepted.returncode, 0, accepted.stderr) def package(self, component: str, package_id: str, version: str) -> tuple[Path, Path]: package_root = self.root / f"stage-{component}" package_root.mkdir() (package_root / "payload.txt").write_text(component) environment = os.environ.copy() environment["SOURCE_DATE_EPOCH"] = "100" result = subprocess.run( [ str(PACKAGE), "--registry", str(self.registry), "--component", component, "--package-root", str(package_root), "--output", str(self.output), "--component-version", version, "--source-sha", ("1" if component == "EchoionApi" else "2") * 40, "--source-tree-hash", "git-sha1:" + ("3" if component == "EchoionApi" else "4") * 40, "--abi-hash", "sha256:" + ("5" if component == "EchoionApi" else "6") * 64, "--build-type", "RelWithDebInfo", ], check=False, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=environment, ) self.assertEqual(result.returncode, 0, result.stderr) stem = f"{package_id}-{version}-linux-x86_64" return ( self.output / f"{stem}.manifest.json", self.output / f"{stem}.tar.zst", ) def publish( self, component: str, manifest: Path, archive: Path, sequence: int, ) -> subprocess.CompletedProcess[str]: return subprocess.run( [ str(PUBLISH), "--registry", str(self.registry), "--component", component, "--manifest", str(manifest), "--archive", str(archive), "--feed-root", str(self.feed), "--base-url", "https://updates.example.test/echoion", "--artifact-base-url", "https://downloads.example.test/components", "--channel", "stable", "--sequence", str(sequence), "--generated-at", "2026-07-29T12:00:00Z", "--key-id", "test-key", "--private-key", str(self.key), ], check=False, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) @staticmethod def payload(path: Path) -> dict[str, object]: envelope = json.loads(path.read_text()) if envelope["schemaVersion"] == 3: encoded = envelope["signedPayload"] document = envelope["document"] else: encoded = envelope["payload"] document = None raw = base64.urlsafe_b64decode(encoded + "=" * (-len(encoded) % 4)) payload = json.loads(raw) if document is not None and document != payload: raise AssertionError("readable document differs from signed payload") return payload def test_single_component_publication_merges_catalog(self) -> None: api_manifest, api_archive = self.package( "EchoionApi", "echoion-api", "2.1.0" ) api_result = self.publish( "EchoionApi", api_manifest, api_archive, 100 ) self.assertEqual(api_result.returncode, 0, api_result.stderr) web_manifest, web_archive = self.package( "EchoionWeb", "echoion-web", "2.0.1" ) web_result = self.publish( "EchoionWeb", web_manifest, web_archive, 101 ) self.assertEqual(web_result.returncode, 0, web_result.stderr) catalog = self.payload(self.feed / "feeds/stable/catalog.json") self.assertEqual( [item["componentId"] for item in catalog["components"]], ["EchoionApi", "EchoionWeb"], ) api = self.payload( self.feed / "feeds/stable/echoion-api/2.1.0.json" ) self.assertNotIn("productVersion", api) for private_field in ( "repository", "repositoryUrl", "cloneUrl", "giteaReleaseUrl", ): self.assertNotIn(private_field, api) self.assertEqual(api["productCompatibility"], ">=2.0.0 <3.0.0") self.assertEqual(api["buildType"], "RelWithDebInfo") self.assertEqual( api["artifact"]["url"], "https://downloads.example.test/components/" "EchoionApi/2.1.0/echoion-api-2.1.0-linux-x86_64.tar.zst", ) def test_rejects_replay_without_mutating_lock(self) -> None: manifest, archive = self.package( "EchoionApi", "echoion-api", "2.0.1" ) first = self.publish("EchoionApi", manifest, archive, 200) self.assertEqual(first.returncode, 0, first.stderr) replay = self.publish("EchoionApi", manifest, archive, 200) self.assertNotEqual(replay.returncode, 0) self.assertIn("sequence is not monotonic", replay.stderr) def test_product_lock_verifies_against_independent_feeds(self) -> None: manifests: dict[str, Path] = {} for sequence, component, package_id, version in ( (300, "EchoionApi", "echoion-api", "2.1.0"), (301, "EchoionWeb", "echoion-web", "2.0.1"), ): manifest, archive = self.package( component, package_id, version ) result = self.publish(component, manifest, archive, sequence) self.assertEqual(result.returncode, 0, result.stderr) manifests[component] = manifest locked: dict[str, object] = {} for component, manifest_path in manifests.items(): manifest = json.loads(manifest_path.read_text()) locked[component] = { "componentVersion": manifest["componentVersion"], "revision": manifest["sourceSha"], "sourceTreeHash": manifest["sourceTreeHash"], "abiHash": manifest["abiHash"], "artifactIdentity": manifest["artifactIdentity"], } lock = self.root / "components.lock.json" lock.write_text( json.dumps( { "schemaVersion": 2, "productVersion": "2.0.0", "components": locked, } ) ) result = subprocess.run( [ str(VERIFY), "--registry", str(self.registry), "--lock", str(lock), "--feed-root", str(self.feed), "--channel", "stable", "--key-id", "test-key", "--public-key", str(self.public_key), ], check=False, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) self.assertEqual(result.returncode, 0, result.stderr) self.assertIn("verified 2 locked components", result.stdout) if __name__ == "__main__": unittest.main()