210 lines
6.9 KiB
Python
210 lines
6.9 KiB
Python
import base64
|
|
import json
|
|
from pathlib import Path
|
|
import zipfile
|
|
|
|
import pytest
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
|
|
|
from dx_productivity.update import keys
|
|
from dx_productivity.update.models import SemVer, UpdateManifest
|
|
from dx_productivity.update.stager import UpdateStager, UpdateVerificationError
|
|
|
|
|
|
def test_stager_uses_the_embedded_public_key_by_default():
|
|
stager = UpdateStager()
|
|
|
|
assert stager.public_key == keys.release_public_key()
|
|
|
|
|
|
def test_stable_channel_signed_package_passes_end_to_end(tmp_path):
|
|
private_key = Ed25519PrivateKey.generate()
|
|
public_key = private_key.public_key().public_bytes(
|
|
serialization.Encoding.Raw, serialization.PublicFormat.Raw
|
|
)
|
|
package = tmp_path / "pkg.zip"
|
|
with zipfile.ZipFile(package, "w") as archive:
|
|
archive.writestr("DxDesignerTools.exe", "binary")
|
|
from dx_productivity.update.verification import sha256_file
|
|
|
|
manifest = UpdateManifest.from_mapping(
|
|
{
|
|
"product": "yiyezhqiu-refactored",
|
|
"version": "0.2.0",
|
|
"channel": "stable",
|
|
"min_supported_version": "0.1.0",
|
|
"package_url": package.as_uri(),
|
|
"sha256": sha256_file(package),
|
|
"release_notes": "",
|
|
"published_at": "2026-09-29",
|
|
"signature": base64.b64encode(
|
|
private_key.sign(package.read_bytes())
|
|
).decode("ascii"),
|
|
}
|
|
)
|
|
|
|
staged = UpdateStager(public_key=public_key).download_and_verify(
|
|
manifest, tmp_path / "updates"
|
|
)
|
|
|
|
assert staged.is_file()
|
|
|
|
|
|
def test_staged_archive_is_reused_when_digest_matches(tmp_path):
|
|
package = tmp_path / "pkg.zip"
|
|
with zipfile.ZipFile(package, "w") as archive:
|
|
archive.writestr("DxDesignerTools.exe", "binary")
|
|
from dx_productivity.update.verification import sha256_file
|
|
|
|
manifest = UpdateManifest.from_mapping(
|
|
{
|
|
"product": "yiyezhqiu-refactored",
|
|
"version": "0.2.0",
|
|
"channel": "development",
|
|
"min_supported_version": "0.1.0",
|
|
"package_url": package.as_uri(),
|
|
"sha256": sha256_file(package),
|
|
"release_notes": "",
|
|
"published_at": "2026-09-29",
|
|
}
|
|
)
|
|
destination = tmp_path / "updates"
|
|
first = UpdateStager().download_and_verify(manifest, destination)
|
|
first_mtime = first.stat().st_mtime_ns
|
|
|
|
second = UpdateStager().download_and_verify(manifest, destination)
|
|
|
|
assert second == first
|
|
assert second.stat().st_mtime_ns == first_mtime
|
|
|
|
|
|
def test_corrupt_staged_archive_is_replaced(tmp_path):
|
|
package = tmp_path / "pkg.zip"
|
|
with zipfile.ZipFile(package, "w") as archive:
|
|
archive.writestr("DxDesignerTools.exe", "binary")
|
|
from dx_productivity.update.verification import sha256_file
|
|
|
|
manifest = UpdateManifest.from_mapping(
|
|
{
|
|
"product": "yiyezhqiu-refactored",
|
|
"version": "0.2.0",
|
|
"channel": "development",
|
|
"min_supported_version": "0.1.0",
|
|
"package_url": package.as_uri(),
|
|
"sha256": sha256_file(package),
|
|
"release_notes": "",
|
|
"published_at": "2026-09-29",
|
|
}
|
|
)
|
|
destination = tmp_path / "updates"
|
|
UpdateStager().download_and_verify(manifest, destination)
|
|
(destination / "0.2.0.zip").write_text("corrupted", encoding="utf-8")
|
|
|
|
staged = UpdateStager().download_and_verify(manifest, destination)
|
|
|
|
assert staged.read_bytes() == package.read_bytes()
|
|
|
|
|
|
def test_package_url_rejects_plain_http():
|
|
with pytest.raises(ValueError, match="not allowed"):
|
|
UpdateManifest.from_mapping(
|
|
{
|
|
"product": "yiyezhqiu-refactored",
|
|
"version": "0.2.0",
|
|
"channel": "development",
|
|
"min_supported_version": "0.1.0",
|
|
"package_url": "http://intranet/pkg.zip",
|
|
"sha256": "a" * 64,
|
|
"release_notes": "",
|
|
"published_at": "2026-09-29",
|
|
}
|
|
)
|
|
|
|
|
|
def test_semver_prerelease_multidigit_compare():
|
|
assert SemVer.parse("1.0.0-rc.10") > SemVer.parse("1.0.0-rc.9")
|
|
assert SemVer.parse("1.0.0-rc.9") < SemVer.parse("1.0.0")
|
|
assert SemVer.parse("1.0.0") > SemVer.parse("1.0.0-rc.99")
|
|
|
|
|
|
def test_installer_reverifies_staged_digest(tmp_path, monkeypatch):
|
|
from dx_productivity.update.installer import InstallPlan, UpdateInstaller
|
|
|
|
current = tmp_path / "app"
|
|
current.mkdir()
|
|
(current / "DxDesignerTools.exe").write_bytes(b"old")
|
|
staged_zip = tmp_path / "staged.zip"
|
|
with zipfile.ZipFile(staged_zip, "w") as archive:
|
|
archive.writestr("DxDesignerTools.exe", "binary")
|
|
plan = InstallPlan(
|
|
current_root=current,
|
|
staged_zip=staged_zip,
|
|
backup_root=tmp_path / "backups",
|
|
restart_command=("cmd", "/c", "exit", "0"),
|
|
sha256="b" * 64,
|
|
)
|
|
|
|
result = UpdateInstaller().install(plan)
|
|
|
|
assert result.state == "failed"
|
|
assert "digest" in result.message
|
|
|
|
|
|
def test_installer_waits_for_parent_exit(tmp_path):
|
|
from dx_productivity.update import installer as installer_module
|
|
from dx_productivity.update.installer import InstallPlan, UpdateInstaller
|
|
|
|
current = tmp_path / "app"
|
|
current.mkdir()
|
|
exe = current / "DxDesignerTools.exe"
|
|
exe.write_bytes(b"old")
|
|
staged_zip = tmp_path / "staged.zip"
|
|
with zipfile.ZipFile(staged_zip, "w") as archive:
|
|
archive.writestr("DxDesignerTools.exe", "new")
|
|
plan = InstallPlan(
|
|
current_root=current,
|
|
staged_zip=staged_zip,
|
|
backup_root=tmp_path / "backups",
|
|
restart_command=("cmd", "/c", "exit", "0"),
|
|
)
|
|
|
|
called = []
|
|
def fake_wait(self, exe_path):
|
|
called.append(exe_path)
|
|
return True
|
|
|
|
monkeypatch = pytest.MonkeyPatch()
|
|
monkeypatch.setattr(
|
|
installer_module.UpdateInstaller, "_wait_for_parent_exit", fake_wait
|
|
)
|
|
result = UpdateInstaller().install(plan)
|
|
monkeypatch.undo()
|
|
|
|
assert called == [exe]
|
|
assert result.state == "installed"
|
|
assert (current / "DxDesignerTools.exe").read_bytes() == b"new"
|
|
|
|
|
|
def test_installer_rollback_failure_is_reported(tmp_path, monkeypatch):
|
|
from dx_productivity.update.installer import InstallPlan, UpdateInstaller
|
|
|
|
current = tmp_path / "app"
|
|
current.mkdir()
|
|
(current / "DxDesignerTools.exe").write_bytes(b"old")
|
|
staged_zip = tmp_path / "staged.zip"
|
|
with zipfile.ZipFile(staged_zip, "w") as archive:
|
|
archive.writestr("DxDesignerTools.exe", "new")
|
|
plan = InstallPlan(
|
|
current_root=current,
|
|
staged_zip=staged_zip,
|
|
backup_root=tmp_path / "backups",
|
|
restart_command=("cmd", "/c", "exit", "3"), # failing probe
|
|
)
|
|
|
|
result = UpdateInstaller().install(plan)
|
|
|
|
assert result.state == "rolled-back"
|
|
assert (current / "DxDesignerTools.exe").read_bytes() == b"old"
|