From 3cc477de60632bfe10acb83fdf8979729f74e28e Mon Sep 17 00:00:00 2001 From: cflmy Date: Fri, 27 Nov 2026 22:07:41 +0800 Subject: [PATCH] test: verify the source of upstream issue inputs --- .woodpecker/ci.yml | 1 + tools/ci/test_verify_upstream.py | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tools/ci/test_verify_upstream.py diff --git a/.woodpecker/ci.yml b/.woodpecker/ci.yml index be21ae7..26fb2a1 100644 --- a/.woodpecker/ci.yml +++ b/.woodpecker/ci.yml @@ -19,6 +19,7 @@ steps: commands: - python3 tools/ci/test_release_metadata.py - python3 tools/ci/test_publish_gitea.py + - python3 tools/ci/test_verify_upstream.py - cargo fmt --all -- --check - cargo clippy --workspace --all-targets --locked -- -D warnings - cargo test --workspace --locked diff --git a/tools/ci/test_verify_upstream.py b/tools/ci/test_verify_upstream.py new file mode 100644 index 0000000..7e7684d --- /dev/null +++ b/tools/ci/test_verify_upstream.py @@ -0,0 +1,69 @@ +"""Regression tests for the root task and Knowledge verification boundary.""" +import hashlib +import importlib.util +import json +import unittest +from pathlib import Path +from unittest.mock import patch + +spec = importlib.util.spec_from_file_location("upstream", Path(__file__).with_name("verify-upstream.py")) +upstream = importlib.util.module_from_spec(spec) +spec.loader.exec_module(upstream) + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +class UpstreamTaskTest(unittest.TestCase): + def setUp(self): + self.source, self.consumer, self.root, self.index, self.asset = (c * n for c, n in + (("a", 40), ("b", 40), ("c", 40), ("d", 64), ("e", 64))) + self.tag = "v3.0.0-rc.6" + key = digest(f"{upstream.REPO}\n{self.tag}\n{self.source}") + inputs = {"consumer": {"id": "knowledge", "source_sha": self.consumer}, + "accepted_dependencies": [], + "release": {"repository": upstream.REPO, "tag": self.tag, "source_sha": self.source, + "index_sha256": self.index, "dependencies": [], + "assets": [{"name": f"marqdo-{self.tag[1:]}-{upstream.TARGET}.tar.gz", + "sha256": self.asset, "target": upstream.TARGET, "size": 9}]}} + value = digest(json.dumps(inputs, sort_keys=True, separators=(",", ":"))) + task = digest(f"{key}\nknowledge\n{value}") + user = {"login": upstream.COORDINATOR} + fence = chr(96) * 3 + self.issue = {"state": "open", "user": user, "body": + f"\n" + f"\n" + f"- Root tracking issue: {upstream.FORGE}/{upstream.ROOT_REPO}/issues/11\n" + f"- Root candidate: {self.root}\n- Input digest: {value}\n- State: pending\n" + f"\n{fence}json\n{json.dumps(inputs)}\n{fence}"} + self.tracker = {"state": "open", "user": user, "body": + f"\n" + f""} + self.branch = {"protected": True, "commit": {"id": self.root}} + + def forge(self, opener, path): + if "comments?" in path: + return [] + return {f"/repos/{upstream.CONSUMER}/issues/12": self.issue, + f"/repos/{upstream.ROOT_REPO}/branches/main": self.branch, + f"/repos/{upstream.ROOT_REPO}/issues/11": self.tracker}[path] + + def run_verify(self): + with patch.object(upstream, "forge_json", self.forge): + return upstream.issue_input(None, 12, self.consumer) + + def test_current_authorized_task_resolves_to_exact_cli(self): + self.assertEqual(self.run_verify(), (self.tag, self.source, self.asset)) + + def test_stale_root_and_wrong_task_author_are_rejected(self): + self.branch["commit"]["id"] = "f" * 40 + with self.assertRaisesRegex(upstream.VerificationError, "stale"): + self.run_verify() + self.issue["user"] = {"login": "untrusted"} + with self.assertRaisesRegex(upstream.VerificationError, "reconcile account"): + self.run_verify() + + +if __name__ == "__main__": + unittest.main()