Files
Echoion/scripts/tests/test_publish_component_feeds.py
faumaray 8ac39621fd
Modular container CI / release-build (push) Has been cancelled
Harden signed feeds and RelWithDebInfo releases
2026-08-01 18:02:55 +03:00

272 lines
9.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Offline tests for signed channel-separated component feed publication."""
from __future__ import annotations
import base64
import json
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
SCRIPT = ROOT / "scripts" / "publish-component-feeds.py"
class ComponentFeedTests(unittest.TestCase):
def setUp(self) -> None:
self.temporary = tempfile.TemporaryDirectory(
prefix="echoion-component-feed-test."
)
self.root = Path(self.temporary.name)
self.artifacts = self.root / "artifacts"
self.feeds = self.root / "feed"
self.artifacts.mkdir()
self.key = self.root / "feed-key.pem"
subprocess.run(
[
"openssl",
"genpkey",
"-algorithm",
"ED25519",
"-out",
str(self.key),
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
self.key.chmod(0o600)
components = {
"EchoionApi": {
"repository": "Void/EchoionApi",
"packageId": "echoion-api",
"kind": "sdk",
"activation": "restart-required",
"dependencies": {},
},
"EchoionPluginExample": {
"repository": "Void/EchoionPluginExample",
"packageId": "echoion-plugin-example",
"pluginId": "example",
"kind": "plugin",
"activation": "reloadable",
"dependencies": {"EchoionApi": ">=2.0.0 <3.0.0"},
},
}
self.registry = {"schemaVersion": 1, "components": components}
locked = {}
for index, component_id in enumerate(components, start=1):
locked[component_id] = {
"repository": components[component_id]["repository"],
"revision": f"{index:040x}",
"componentVersion": "2.0.0",
"abiHash": f"sha256:{index:064x}",
"sourceTreeHash": f"git-sha1:{index + 10:040x}",
"artifactIdentity": (
f"{components[component_id]['packageId']}-"
"2.0.0-linux-x86_64"
),
}
self.lock = {
"schemaVersion": 2,
"productVersion": "2.0.0",
"components": locked,
}
self.registry_path = self.root / "components.json"
self.lock_path = self.root / "components.lock.json"
self.registry_path.write_text(json.dumps(self.registry))
self.lock_path.write_text(json.dumps(self.lock))
def tearDown(self) -> None:
self.temporary.cleanup()
def write_packages(self, channel: str, sequence: int) -> None:
for component_id, definition in self.registry["components"].items():
version = "2.0.0"
if channel != "stable":
version += f"-{channel}.{sequence}"
stem = (
f"{definition['packageId']}-{version}-linux-x86_64"
)
archive = self.artifacts / f"{stem}.tar.zst"
archive.write_bytes(f"{component_id}:{channel}:{sequence}".encode())
locked = self.lock["components"][component_id]
manifest = {
"schemaVersion": 1,
"componentId": component_id,
"packageId": definition["packageId"],
"kind": definition["kind"],
"productVersion": "2.0.0",
"componentVersion": version,
"sourceSha": locked["revision"],
"sourceTreeHash": locked["sourceTreeHash"],
"abiHash": locked["abiHash"],
"artifactIdentity": stem,
"activation": definition["activation"],
"dependencies": definition["dependencies"],
"platform": "linux",
"architecture": "x86_64",
"buildType": "RelWithDebInfo",
"toolchainContract": "linux-x86_64-glibc-2.35-cxx26",
}
(self.artifacts / f"{stem}.manifest.json").write_text(
json.dumps(manifest)
)
def publish(
self, channel: str, sequence: int, *, expect_success: bool = True
) -> subprocess.CompletedProcess[str]:
result = subprocess.run(
[
str(SCRIPT),
"--registry",
str(self.registry_path),
"--lock",
str(self.lock_path),
"--artifacts",
str(self.artifacts),
"--feed-root",
str(self.feeds),
"--base-url",
"https://updates.example.test/echoion",
"--artifact-base-url",
"https://updates.example.test/releases/v2.0.0",
"--channel",
channel,
"--sequence",
str(sequence),
"--generated-at",
"2026-07-28T12:00:00Z",
"--key-id",
"test-key",
"--private-key",
str(self.key),
],
check=False,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if expect_success and result.returncode != 0:
self.fail(result.stderr)
return result
@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
decoded = base64.urlsafe_b64decode(encoded + "=" * (-len(encoded) % 4))
payload = json.loads(decoded)
if document is not None and document != payload:
raise AssertionError("readable document differs from signed payload")
return payload
def test_stable_layout_and_catalog(self) -> None:
self.write_packages("stable", 100)
self.publish("stable", 100)
catalog_path = self.feeds / "feeds/stable/catalog.json"
catalog_text = catalog_path.read_text()
catalog_envelope = json.loads(catalog_text)
self.assertTrue(catalog_text.startswith("{\n"))
self.assertIn('\n "document": {\n', catalog_text)
self.assertEqual(catalog_envelope["schemaVersion"], 3)
self.assertEqual(catalog_envelope["keyId"], "test-key")
catalog = self.payload(catalog_path)
self.assertEqual(len(catalog["components"]), 2)
self.assertTrue(
(self.feeds / "feeds/stable/echoion-api/2.0.0.json").is_file()
)
self.assertTrue(
(
self.feeds
/ "feeds/stable/plugins/example/2.0.0.json"
).is_file()
)
state = json.loads(
(
self.feeds
/ "state/components/EchoionPluginExample.json"
).read_text()
)
self.assertEqual(state["semVer"], "2.0.0")
self.assertEqual(state["sequence"], 100)
release = self.payload(
self.feeds / "feeds/stable/echoion-api/2.0.0.json"
)
self.assertEqual(release["artifactIdentity"], "echoion-api-2.0.0-linux-x86_64")
self.assertEqual(release["platform"], "linux")
self.assertEqual(release["architecture"], "x86_64")
self.assertEqual(release["buildType"], "RelWithDebInfo")
self.assertEqual(
release["toolchainContract"],
"linux-x86_64-glibc-2.35-cxx26",
)
def test_rejects_replay_and_immutable_artifact_change(self) -> None:
self.write_packages("stable", 100)
self.publish("stable", 100)
replay = self.publish("stable", 100, expect_success=False)
self.assertNotEqual(replay.returncode, 0)
for archive in self.artifacts.glob("*.tar.zst"):
archive.write_bytes(archive.read_bytes() + b"changed")
conflict = self.publish("stable", 101, expect_success=False)
self.assertNotEqual(conflict.returncode, 0)
self.assertIn("unchanged source SHA", conflict.stderr)
def test_rejects_tampered_readable_document(self) -> None:
self.write_packages("stable", 100)
self.publish("stable", 100)
release_path = (
self.feeds / "feeds/stable/echoion-api/2.0.0.json"
)
envelope = json.loads(release_path.read_text())
envelope["document"]["sequence"] = 101
release_path.write_text(json.dumps(envelope))
rejected = self.publish("stable", 101, expect_success=False)
self.assertNotEqual(rejected.returncode, 0)
self.assertIn(
"readable document differs from signed payload", rejected.stderr
)
def test_beta_does_not_advance_stable_state(self) -> None:
self.write_packages("stable", 100)
self.publish("stable", 100)
for path in self.artifacts.iterdir():
path.unlink()
self.write_packages("beta", 200)
self.publish("beta", 200)
state = json.loads(
(
self.feeds / "state/components/EchoionApi.json"
).read_text()
)
self.assertEqual(state["semVer"], "2.0.0")
self.assertEqual(state["sequence"], 100)
self.assertEqual(state["channels"]["beta"]["version"], "2.0.0-beta.200")
beta_release = self.payload(
self.feeds
/ "feeds/beta/echoion-api/2.0.0-beta.200.json"
)
self.assertEqual(
beta_release["artifactIdentity"],
"echoion-api-2.0.0-beta.200-linux-x86_64",
)
self.assertTrue(
(
self.feeds
/ "feeds/beta/echoion-api/2.0.0-beta.200.json"
).is_file()
)
if __name__ == "__main__":
unittest.main()