9f38e38f6a
Each installation generates a private Ed25519 key once (0600, under ~/.config/pipelines), signs version.json with a detached version.json.sig when publishing APK and desktop builds, and embeds its own and listed trusted public keys in app builds. The app verifies the feed signature before trusting the artifact sha256, refuses unsigned or foreign feeds when it has keys, and requires the sha256 and Android signer in signed feeds.
201 lines
10 KiB
Python
201 lines
10 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tarfile
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
|
|
def load(name):
|
|
spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(name + ".py"))
|
|
result = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(result)
|
|
return result
|
|
|
|
|
|
core = load("app_build")
|
|
desktop = load("app_desktop_build")
|
|
|
|
|
|
class Runner:
|
|
def __init__(self):
|
|
self.calls = []
|
|
self.native_env = None
|
|
self.fail_native = False
|
|
self.version = "3.44.6"
|
|
|
|
def __call__(self, command, **kwargs):
|
|
self.calls.append(command)
|
|
stdout = ""
|
|
code = 0
|
|
if "--machine" in command:
|
|
stdout = json.dumps({"frameworkVersion": self.version})
|
|
elif command[:2] == ["xcodebuild", "-version"]:
|
|
stdout = "Xcode 27.0\nBuild version 27A266a"
|
|
elif command[:2] == ["docker", "run"]:
|
|
output = Path(next(arg[:-5] for arg in command if arg.endswith(":/out")))
|
|
(output / "pipelines-app").write_bytes(b"linux-client")
|
|
(output / "pipelines-app").chmod(0o755)
|
|
(output / "data/flutter_assets").mkdir(parents=True)
|
|
(output / "data/flutter_assets/font.ttf").write_bytes(b"font")
|
|
(output / "lib").mkdir()
|
|
(output / "lib/engine.so").write_bytes(b"engine")
|
|
elif command[1:3] == ["build", "macos"]:
|
|
self.native_env = kwargs.get("env")
|
|
if self.fail_native:
|
|
code = 1
|
|
else:
|
|
app = Path(kwargs["cwd"]) / "build/macos/Build/Products/Release/Pipelines.app"
|
|
(app / "Contents/MacOS").mkdir(parents=True)
|
|
(app / "Contents/MacOS/Pipelines").write_bytes(b"macos-client")
|
|
(app / "Contents/MacOS/Pipelines").chmod(0o755)
|
|
framework = app / "Contents/Frameworks/Fixture.framework"
|
|
(framework / "Versions/A").mkdir(parents=True)
|
|
(framework / "Versions/A/Fixture").write_bytes(b"framework")
|
|
(framework / "Versions/Current").symlink_to("A")
|
|
(framework / "Fixture").symlink_to("Versions/Current/Fixture")
|
|
elif command[:2] == ["lipo", "-archs"]:
|
|
stdout = "x86_64 arm64"
|
|
elif command[0] == "ditto":
|
|
Path(command[-1]).write_bytes(b"archive")
|
|
return subprocess.CompletedProcess(command, code, stdout=stdout, stderr="")
|
|
|
|
|
|
class DesktopBuildTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temporary = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.temporary.cleanup)
|
|
self.root = Path(self.temporary.name)
|
|
self.app = self.root / "app"
|
|
for directory in ("lib", "fonts", "linux", "macos", "docker"):
|
|
(self.app / directory).mkdir(parents=True, exist_ok=True)
|
|
(self.app / "lib/main.dart").write_text("main() {}")
|
|
(self.app / ".flutter-version").write_text("3.44.6\n")
|
|
self.runner = Runner()
|
|
|
|
def build(self, kind, **kwargs):
|
|
return desktop.build_desktop(kind, app=self.app, cache=self.root / "cache",
|
|
runner=self.runner, source_digest=core.digest, docker_build=core.build,
|
|
docker_ready=lambda *_: True, log=lambda *_: None, **kwargs)
|
|
|
|
def test_linux_is_x64_and_packages_full_bundle_without_installation(self):
|
|
bundle, archive = self.build("linux", output=self.root / "artifacts")
|
|
builds = [c for c in self.runner.calls if c[:2] == ["docker", "build"]]
|
|
self.assertEqual(builds[0][builds[0].index("--target") + 1], "linux")
|
|
for command in [builds[0], next(c for c in self.runner.calls if c[:2] == ["docker", "run"])]:
|
|
self.assertEqual(command[command.index("--platform") + 1], "linux/amd64")
|
|
with tarfile.open(archive) as package:
|
|
self.assertTrue({"pipelines-linux-x64/pipelines-app", "pipelines-linux-x64/lib/engine.so",
|
|
"pipelines-linux-x64/data/flutter_assets/font.ttf"}.issubset(package.getnames()))
|
|
self.assertEqual(package.getmember("pipelines-linux-x64/pipelines-app").mode & 0o111, 0o111)
|
|
self.assertTrue(bundle.is_dir())
|
|
before = len(self.runner.calls)
|
|
self.build("linux")
|
|
self.assertEqual(len(self.runner.calls), before)
|
|
|
|
def test_font_and_target_sources_invalidate_correct_caches(self):
|
|
keys = {kind: core.digest(kind, self.app) for kind in core.SOURCES}
|
|
(self.app / "fonts/font.ttf").write_bytes(b"new font")
|
|
for kind in core.SOURCES:
|
|
self.assertNotEqual(keys[kind], core.digest(kind, self.app), kind)
|
|
linux = core.digest("linux", self.app)
|
|
macos = core.digest("macos", self.app)
|
|
(self.app / "linux/CMakeLists.txt").write_text("changed native runner")
|
|
self.assertNotEqual(linux, core.digest("linux", self.app))
|
|
self.assertEqual(macos, core.digest("macos", self.app))
|
|
generated = self.app / "macos/Flutter/ephemeral"
|
|
generated.mkdir(parents=True)
|
|
(generated / "local-path").write_text("machine-specific generated value")
|
|
self.assertEqual(macos, core.digest("macos", self.app))
|
|
|
|
def test_pub_get_generated_linux_registries_do_not_change_source_key(self):
|
|
before = core.digest("linux", self.app)
|
|
directory = self.app / "linux/flutter"
|
|
directory.mkdir()
|
|
for name in ("generated_plugin_registrant.cc", "generated_plugin_registrant.h", "generated_plugins.cmake"):
|
|
(directory / name).write_text("generated from locked dependencies")
|
|
self.assertEqual(before, core.digest("linux", self.app))
|
|
(self.app / "pubspec.lock").write_text("changed dependency lock")
|
|
self.assertNotEqual(before, core.digest("linux", self.app))
|
|
|
|
@patch.object(desktop.platform, "system", return_value="Darwin")
|
|
def test_native_macos_preserves_framework_symlinks_and_reuses_build(self, _):
|
|
bundle, archive = self.build("macos", flutter="/fixture/flutter")
|
|
self.assertTrue((bundle / "Contents/Frameworks/Fixture.framework/Versions/Current").is_symlink())
|
|
self.assertEqual((bundle / "Contents/MacOS/Pipelines").stat().st_mode & 0o111, 0o111)
|
|
self.assertTrue(archive.is_file())
|
|
self.build("macos", flutter="/fixture/flutter")
|
|
self.assertEqual(sum(c[1:3] == ["build", "macos"] for c in self.runner.calls), 1)
|
|
self.assertFalse(any(c[0] == "docker" for c in self.runner.calls))
|
|
self.assertFalse((self.app / "build").exists())
|
|
expected = {"aarch64": "arm64", "x86_64": "x86_64"}.get(desktop.platform.machine(), desktop.platform.machine())
|
|
self.assertEqual(self.runner.native_env["FLUTTER_XCODE_ARCHS"], expected)
|
|
|
|
@patch.object(desktop.platform, "system", return_value="Darwin")
|
|
def test_failed_native_build_preserves_previous_artifact(self, _):
|
|
bundle, archive = self.build("macos", flutter="/fixture/flutter")
|
|
previous = archive.read_bytes()
|
|
(self.app / "lib/main.dart").write_text("changed application")
|
|
self.runner.fail_native = True
|
|
with self.assertRaisesRegex(RuntimeError, "failed"):
|
|
self.build("macos", flutter="/fixture/flutter")
|
|
self.assertEqual(archive.read_bytes(), previous)
|
|
self.assertTrue((bundle / "Contents/MacOS/Pipelines").is_file())
|
|
|
|
@patch.object(desktop.platform, "system", return_value="Darwin")
|
|
def test_wrong_sdk_fails_before_building_or_changing_any_sdk(self, _):
|
|
self.runner.version = "3.47.0"
|
|
with self.assertRaisesRegex(ValueError, "3.44.6 is required"):
|
|
self.build("macos", flutter="/fixture/flutter")
|
|
self.assertEqual(len(self.runner.calls), 1)
|
|
|
|
@patch.object(desktop.platform, "system", return_value="Linux")
|
|
def test_macos_refuses_non_macos_host_without_docker_fallback(self, _):
|
|
with self.assertRaisesRegex(ValueError, "natively on macOS"):
|
|
self.build("macos")
|
|
self.assertEqual(self.runner.calls, [])
|
|
|
|
def test_linux_publication_has_matching_hash_and_rejects_downgrade(self):
|
|
bundle, archive = self.build("linux")
|
|
updates = self.root / "updates"
|
|
signed = []
|
|
signer = lambda data: signed.append(data) or b"signature document\n"
|
|
metadata = desktop.publish_desktop("linux", bundle, archive, updates, signer=signer, log=lambda *_: None)
|
|
target = updates / "linux-x64"
|
|
self.assertEqual(json.loads((target / "version.json").read_text()), metadata)
|
|
# The detached signature covers the exact published feed bytes.
|
|
self.assertEqual(signed, [(target / "version.json").read_bytes()])
|
|
self.assertEqual((target / "version.json.sig").read_bytes(), b"signature document\n")
|
|
self.assertEqual(metadata["architectures"], ["x64"])
|
|
self.assertEqual(metadata["sha256"], desktop.hashlib.sha256((target / metadata["file"]).read_bytes()).hexdigest())
|
|
metadata["versionCode"] += 1
|
|
(target / "version.json").write_text(json.dumps(metadata))
|
|
with self.assertRaisesRegex(ValueError, "older build"):
|
|
desktop.publish_desktop("linux", bundle, archive, updates, signer=signer)
|
|
|
|
@patch.object(desktop.platform, "system", return_value="Darwin")
|
|
def test_macos_feed_uses_actual_architectures_and_native_build_stamp(self, _):
|
|
bundle, archive = self.build("macos", flutter="/fixture/flutter")
|
|
metadata = desktop.publish_desktop("macos", bundle, archive, self.root / "updates",
|
|
signer=lambda data: b"{}\n", log=lambda *_: None)
|
|
self.assertTrue((self.root / "updates/macos/version.json.sig").is_file())
|
|
self.assertEqual(metadata["architectures"], ["arm64", "x64"])
|
|
command = next(c for c in self.runner.calls if c[1:3] == ["build", "macos"])
|
|
self.assertIn(f"--dart-define=PIPELINES_NATIVE_BUILD_CODE={metadata['versionCode']}", command)
|
|
|
|
def test_desktop_builds_embed_the_trusted_update_keys(self):
|
|
self.build("linux", update_keys=("--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=abc",))
|
|
run = next(c for c in self.runner.calls if c[:2] == ["docker", "run"])
|
|
self.assertIn("--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=abc", run)
|
|
|
|
def test_desktop_server_prefill_rejects_embedded_credentials(self):
|
|
with self.assertRaisesRegex(ValueError, "embedded credentials"):
|
|
self.build("linux", server_url="wss://secret@example.com/connect")
|
|
self.assertEqual(self.runner.calls, [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|