Files
Echoion/scripts/tests/test_aggregate_update_feed.py
T
faumaray f78004e822
Container CI / release-build (push) Successful in 16m22s
Nightly release / publish-nightly (push) Successful in 16m6s
Release: use independent component repositories
2026-07-28 21:24:30 +03:00

383 lines
13 KiB
Python
Executable File

#!/usr/bin/env python3
"""Offline regression tests for scripts/aggregate_update_feed.py."""
from __future__ import annotations
import base64
from datetime import datetime, timezone
import json
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
SOURCE_ROOT = Path(__file__).resolve().parents[2]
SCRIPT = SOURCE_ROOT / "scripts" / "aggregate_update_feed.py"
def release(version: str, channel: str = "nightly") -> dict:
return {
"component": "runtime",
"artifactId": "",
"channel": channel,
"version": version,
"cohort": "linux-glibc-2.35",
"platform": "linux",
"arch": "x86_64",
"url": f"https://updates.example.invalid/echoion-{version}.tar.zst",
"size": 123,
"sha256": "ab" * 32,
"packageType": "tar.zst",
"compatibility": {"versionContracts": {}, "exactContracts": {}},
"releaseManifestSha256": "cd" * 32,
}
def payload(path: Path, releases: list[dict], sequence: int) -> None:
path.write_text(
json.dumps(
{
"schemaVersion": 1,
"feedId": "echoion-runtime",
"sequence": sequence,
"generatedAt": "2026-07-22T00:00:00Z",
"expiresAt": "2026-08-01T00:00:00Z",
"releases": releases,
}
),
encoding="utf-8",
)
class AggregateUpdateFeedTest(unittest.TestCase):
def setUp(self) -> None:
self.temporary = tempfile.TemporaryDirectory()
self.root = Path(self.temporary.name)
self.private_key = self.root / "private.pem"
self.public_key = self.root / "public.pem"
subprocess.run(
["openssl", "genpkey", "-algorithm", "ED25519", "-out", self.private_key],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
self.private_key.chmod(0o600)
subprocess.run(
[
"openssl",
"pkey",
"-in",
self.private_key,
"-pubout",
"-out",
self.public_key,
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
def tearDown(self) -> None:
self.temporary.cleanup()
def run_script(
self,
new_payload: Path,
output_payload: Path,
output_envelope: Path,
*extra: str,
check: bool = True,
) -> subprocess.CompletedProcess:
return subprocess.run(
[
os.fspath(SCRIPT),
"--new-payload",
os.fspath(new_payload),
"--public-key",
os.fspath(self.public_key),
"--private-key",
os.fspath(self.private_key),
"--generated-at",
"2026-07-22T12:00:00Z",
"--output-payload",
os.fspath(output_payload),
"--output-envelope",
os.fspath(output_envelope),
*extra,
],
check=check,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
def test_merge_signs_and_retains_fourteen_nightly_versions(self) -> None:
first = self.root / "first.json"
first_output = self.root / "first-output.json"
first_envelope = self.root / "first-envelope.json"
payload(
first,
[release(f"0.0.0-nightly.202607{day:02d}.1") for day in range(16, 0, -1)],
100,
)
self.run_script(first, first_output, first_envelope)
first_document = json.loads(first_output.read_text(encoding="utf-8"))
self.assertEqual(len(first_document["releases"]), 14)
second = self.root / "second.json"
second_output = self.root / "second-output.json"
second_envelope = self.root / "second-envelope.json"
payload(second, [release("1.2.3", "stable")], 101)
self.run_script(
second,
second_output,
second_envelope,
"--existing-envelope",
os.fspath(first_envelope),
)
document = json.loads(second_output.read_text(encoding="utf-8"))
expected_sequence = int(
datetime(2026, 7, 22, 12, tzinfo=timezone.utc).timestamp()
) + 1
self.assertEqual(document["sequence"], expected_sequence)
self.assertEqual(document["expiresAt"], "2026-09-05T12:00:00Z")
self.assertEqual(len(document["releases"]), 15)
self.assertEqual(document["releases"][0]["channel"], "stable")
envelope = json.loads(second_envelope.read_text(encoding="utf-8"))
decoded = base64.urlsafe_b64decode(
envelope["payload"] + "=" * (-len(envelope["payload"]) % 4)
)
self.assertEqual(decoded, second_output.read_bytes())
third_output = self.root / "third-output.json"
third_envelope = self.root / "third-envelope.json"
self.run_script(
second,
third_output,
third_envelope,
"--existing-envelope",
os.fspath(second_envelope),
)
self.assertEqual(third_output.read_bytes(), second_output.read_bytes())
self.assertEqual(third_envelope.read_bytes(), second_envelope.read_bytes())
changed = json.loads(second.read_text(encoding="utf-8"))
changed["releases"][0]["sha256"] = "ef" * 32
second.write_text(json.dumps(changed), encoding="utf-8")
collision = self.run_script(
second,
self.root / "collision-output.json",
self.root / "collision-envelope.json",
"--existing-envelope",
os.fspath(second_envelope),
check=False,
)
self.assertNotEqual(collision.returncode, 0)
self.assertIn("immutable release identity", collision.stderr)
def test_multiple_component_payloads_are_signed_together(self) -> None:
runtime_payload = self.root / "runtime.json"
core_payload = self.root / "core.json"
runtime_release = release("1.2.3", "stable")
core_release = {
**release("1.2.3", "stable"),
"component": "core",
"url": "https://updates.example.invalid/echoion-core-1.2.3.so",
"packageType": "shared-library",
}
core_release.pop("releaseManifestSha256")
payload(runtime_payload, [runtime_release], 100)
payload(core_payload, [core_release], 100)
output = self.root / "components-output.json"
self.run_script(
runtime_payload,
output,
self.root / "components-envelope.json",
"--new-payload",
os.fspath(core_payload),
)
releases = json.loads(output.read_text(encoding="utf-8"))["releases"]
self.assertEqual(
{(item["component"], item["version"]) for item in releases},
{("runtime", "1.2.3"), ("core", "1.2.3")},
)
def test_tampered_existing_envelope_is_rejected(self) -> None:
first = self.root / "first.json"
first_output = self.root / "first-output.json"
first_envelope = self.root / "first-envelope.json"
payload(first, [release("1.2.3", "stable")], 100)
self.run_script(first, first_output, first_envelope)
envelope = json.loads(first_envelope.read_text(encoding="utf-8"))
envelope["payload"] = envelope["payload"][:-1] + (
"A" if envelope["payload"][-1] != "A" else "B"
)
first_envelope.write_text(json.dumps(envelope), encoding="utf-8")
second = self.root / "second.json"
payload(second, [release("1.2.4", "stable")], 101)
failed = self.run_script(
second,
self.root / "second-output.json",
self.root / "second-envelope.json",
"--existing-envelope",
os.fspath(first_envelope),
check=False,
)
self.assertNotEqual(failed.returncode, 0)
self.assertIn("verify the existing feed", failed.stderr)
def test_private_key_must_match_the_published_public_key(self) -> None:
new_payload = self.root / "new.json"
payload(new_payload, [release("1.2.3", "stable")], 100)
wrong_private_key = self.root / "wrong-private.pem"
subprocess.run(
[
"openssl",
"genpkey",
"-algorithm",
"ED25519",
"-out",
wrong_private_key,
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
wrong_private_key.chmod(0o600)
completed = subprocess.run(
[
os.fspath(SCRIPT),
"--new-payload",
os.fspath(new_payload),
"--public-key",
os.fspath(self.public_key),
"--private-key",
os.fspath(wrong_private_key),
"--generated-at",
"2026-07-22T12:00:00Z",
"--output-payload",
os.fspath(self.root / "output.json"),
"--output-envelope",
os.fspath(self.root / "envelope.json"),
],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
self.assertNotEqual(completed.returncode, 0)
self.assertIn("verify the newly signed feed", completed.stderr)
def test_key_rotation_accepts_only_an_explicitly_trusted_old_key(self) -> None:
old_payload = self.root / "old.json"
old_output = self.root / "old-output.json"
old_envelope = self.root / "old-envelope.json"
payload(old_payload, [release("1.2.3", "stable")], 100)
self.run_script(
old_payload,
old_output,
old_envelope,
"--key-id",
"old-key",
)
new_private_key = self.root / "new-private.pem"
new_public_key = self.root / "new-public.pem"
subprocess.run(
[
"openssl",
"genpkey",
"-algorithm",
"ED25519",
"-out",
new_private_key,
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
new_private_key.chmod(0o600)
subprocess.run(
[
"openssl",
"pkey",
"-in",
new_private_key,
"-pubout",
"-out",
new_public_key,
],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
new_payload = self.root / "new.json"
payload(new_payload, [release("1.2.4", "stable")], 101)
common = [
os.fspath(SCRIPT),
"--new-payload",
os.fspath(new_payload),
"--public-key",
os.fspath(new_public_key),
"--private-key",
os.fspath(new_private_key),
"--key-id",
"new-key",
"--generated-at",
"2026-07-22T12:00:00Z",
"--existing-envelope",
os.fspath(old_envelope),
]
rejected = subprocess.run(
[
*common,
"--output-payload",
os.fspath(self.root / "rejected-output.json"),
"--output-envelope",
os.fspath(self.root / "rejected-envelope.json"),
],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
self.assertNotEqual(rejected.returncode, 0)
self.assertIn("untrusted key id: old-key", rejected.stderr)
rotated_output = self.root / "rotated-output.json"
rotated_envelope = self.root / "rotated-envelope.json"
accepted = subprocess.run(
[
*common,
"--trusted-existing-key",
f"old-key={self.public_key}",
"--output-payload",
os.fspath(rotated_output),
"--output-envelope",
os.fspath(rotated_envelope),
],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
self.assertEqual(accepted.returncode, 0, accepted.stderr)
envelope = json.loads(rotated_envelope.read_text(encoding="utf-8"))
self.assertEqual(envelope["keyId"], "new-key")
releases = json.loads(rotated_output.read_text(encoding="utf-8"))[
"releases"
]
self.assertEqual([item["version"] for item in releases], ["1.2.4", "1.2.3"])
if __name__ == "__main__":
unittest.main()