mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-09-01 21:53:42 +08:00
96 lines
3.5 KiB
Python
96 lines
3.5 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
def _load_patch_module():
|
|
path = Path(__file__).resolve().parent / "patch_system_reset_image.py"
|
|
spec = importlib.util.spec_from_file_location("patch_system_reset_image_under_test", path)
|
|
assert spec is not None and spec.loader is not None
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
patch_image = _load_patch_module()
|
|
|
|
|
|
def test_only_version_and_additive_runtime_packages_are_mutable():
|
|
assert patch_image.ALLOWED_IMAGE_MUTATIONS == {
|
|
patch_image.VERSION_PATH_IN_IMAGE,
|
|
*patch_image.STAR_PILOT_DEPENDENCY_PATHS,
|
|
}
|
|
assert set(patch_image.C3_DEPENDENCY_PATHS) < set(patch_image.STAR_PILOT_DEPENDENCY_PATHS)
|
|
|
|
|
|
def test_upstream_profile_covers_factory_reset_and_runtime_paths():
|
|
assert patch_image.UPSTREAM_VERSION == "19.6"
|
|
assert patch_image.UPSTREAM_RAW_SHA256 == "5b6ce7965904a157fd3a134ccfcb854f9ca5c1cc2a26b7cb80a4fa4e1cc4aaa3"
|
|
assert set(patch_image.UPSTREAM_REQUIRED_VENV_PATHS) == {"capnp", "numpy", "Crypto", "tqdm", "raylib"}
|
|
assert set(patch_image.REQUIRED_VENV_PATHS) == {
|
|
"crcmod", "serial", "kaitaistruct", "cv2", "mapbox_earcut", "jsonrpc", "xattr", "onnx",
|
|
"aiohttp", "pyaudio", "capnp", "numpy", "Crypto", "tqdm", "raylib",
|
|
}
|
|
assert patch_image.CANDIDATE_SITE_PACKAGES_COUNT == (
|
|
patch_image.UPSTREAM_SITE_PACKAGES_COUNT + len(patch_image.STAR_PILOT_DEPENDENCY_PATHS)
|
|
)
|
|
assert len(patch_image.STAR_PILOT_DEPENDENCY_PATHS) == len(set(patch_image.STAR_PILOT_DEPENDENCY_PATHS))
|
|
assert set(patch_image.PROTECTED_PAYLOAD_HASHES) >= {
|
|
"/etc/NetworkManager/NetworkManager.conf",
|
|
"/lib/systemd/system/NetworkManager.service",
|
|
"/usr/comma/updater",
|
|
"/usr/comma/reset",
|
|
"/usr/comma/installer",
|
|
"/usr/comma/setup",
|
|
"/usr/comma/comma.sh",
|
|
"/usr/comma/magic.py",
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("version", ["19.6.1", "19.6.5", "19.6.99"])
|
|
def test_target_version_accepts_starpilot_revision(version):
|
|
assert patch_image.validate_target_version(version) == version
|
|
|
|
|
|
@pytest.mark.parametrize("version", ["19.6", "19.6.0", "19.7.1", "20.6.1", "latest"])
|
|
def test_target_version_rejects_non_revision(version):
|
|
with pytest.raises(RuntimeError):
|
|
patch_image.validate_target_version(version)
|
|
|
|
|
|
def test_write_version_fails_closed_if_allowlist_changes(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(patch_image, "ALLOWED_IMAGE_MUTATIONS", frozenset({"/VERSION", "/usr/comma/setup"}))
|
|
with pytest.raises(RuntimeError, match="allowlist"):
|
|
patch_image.write_version("debugfs", tmp_path / "system.img", tmp_path / "VERSION")
|
|
|
|
|
|
def test_update_manifest_changes_only_system_entry():
|
|
original = [
|
|
{"name": "boot", "url": "custom-boot", "hash": "boot-hash"},
|
|
{"name": "system", "url": "old", "hash": "old", "alt": {"url": "old-alt"}},
|
|
]
|
|
updated = patch_image.update_manifest_system_entry(original, "hosted", "new-hash", 123)
|
|
assert updated[0] == original[0]
|
|
assert updated[1] == {
|
|
"name": "system",
|
|
"url": "hosted",
|
|
"hash": "new-hash",
|
|
"hash_raw": "new-hash",
|
|
"size": 123,
|
|
"sparse": False,
|
|
"full_check": False,
|
|
"has_ab": True,
|
|
"ondevice_hash": "new-hash",
|
|
}
|
|
assert json.dumps(original)
|
|
|
|
|
|
def test_protected_payload_validation_reports_any_drift():
|
|
patch_image.validate_protected_payloads(dict(patch_image.PROTECTED_PAYLOAD_HASHES))
|
|
changed = dict(patch_image.PROTECTED_PAYLOAD_HASHES)
|
|
changed["/usr/comma/reset"] = "0" * 64
|
|
with pytest.raises(RuntimeError, match="/usr/comma/reset"):
|
|
patch_image.validate_protected_payloads(changed)
|