780 lines
27 KiB
Python
780 lines
27 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM
|
|
|
|
|
|
def test_compare_versions_handles_v_prefix_and_patch_order():
|
|
from douyin.updater.force_update import compare_versions
|
|
|
|
assert compare_versions("V3.3.10", "3.3.9") > 0
|
|
assert compare_versions("3.3.3", "V3.3.3") == 0
|
|
assert compare_versions("3.3.2", "3.3.3") < 0
|
|
|
|
|
|
def test_load_settings_uses_default_server_without_manifest_url(tmp_path, monkeypatch):
|
|
from douyin.core import path_config
|
|
from douyin.updater import force_update
|
|
from douyin.updater.force_update import load_update_settings
|
|
|
|
cfg = tmp_path / "config.json"
|
|
cfg.write_text(json.dumps({}), encoding="utf-8")
|
|
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg))
|
|
monkeypatch.delenv("DOUYIN_UPDATE_MANIFEST_URL", raising=False)
|
|
monkeypatch.delenv("DOUYIN_AUTO_UPDATE_ENABLED", raising=False)
|
|
monkeypatch.delenv("DOUYIN_DISABLE_AUTO_UPDATE", raising=False)
|
|
|
|
settings = load_update_settings()
|
|
|
|
assert settings.enabled is True
|
|
assert settings.manifest_url == force_update.DEFAULT_UPDATE_MANIFEST_URL
|
|
assert settings.require_signed_manifest is True
|
|
|
|
|
|
def test_load_settings_can_disable_default_auto_update(tmp_path, monkeypatch):
|
|
from douyin.core import path_config
|
|
from douyin.updater.force_update import load_update_settings
|
|
|
|
cfg = tmp_path / "config.json"
|
|
cfg.write_text(json.dumps({"auto_update": {"enabled": False}}), encoding="utf-8")
|
|
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg))
|
|
monkeypatch.delenv("DOUYIN_UPDATE_MANIFEST_URL", raising=False)
|
|
monkeypatch.delenv("DOUYIN_AUTO_UPDATE_ENABLED", raising=False)
|
|
monkeypatch.delenv("DOUYIN_DISABLE_AUTO_UPDATE", raising=False)
|
|
|
|
settings = load_update_settings()
|
|
|
|
assert settings.enabled is False
|
|
assert settings.manifest_url
|
|
|
|
|
|
def test_manual_update_settings_enable_check_without_fail_closed(tmp_path, monkeypatch):
|
|
from douyin.core import path_config
|
|
from douyin.updater.force_update import load_manual_update_settings
|
|
|
|
cfg = tmp_path / "config.json"
|
|
cfg.write_text(json.dumps({"auto_update": {"enabled": False}}), encoding="utf-8")
|
|
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg))
|
|
monkeypatch.delenv("DOUYIN_UPDATE_MANIFEST_URL", raising=False)
|
|
monkeypatch.delenv("DOUYIN_DISABLE_AUTO_UPDATE", raising=False)
|
|
|
|
settings = load_manual_update_settings()
|
|
|
|
assert settings.enabled is True
|
|
assert settings.fail_closed is False
|
|
assert settings.manifest_url
|
|
|
|
|
|
def test_load_settings_uses_default_server_for_frozen_exe(tmp_path, monkeypatch):
|
|
from douyin.core import path_config
|
|
from douyin.updater import force_update
|
|
from douyin.updater.force_update import load_update_settings
|
|
|
|
cfg = tmp_path / "config.json"
|
|
cfg.write_text(json.dumps({}), encoding="utf-8")
|
|
monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg))
|
|
monkeypatch.setattr(force_update.sys, "frozen", True, raising=False)
|
|
monkeypatch.delenv("DOUYIN_UPDATE_MANIFEST_URL", raising=False)
|
|
monkeypatch.delenv("DOUYIN_AUTO_UPDATE_ENABLED", raising=False)
|
|
|
|
settings = load_update_settings()
|
|
|
|
assert settings.enabled is True
|
|
assert settings.manifest_url == force_update.DEFAULT_UPDATE_MANIFEST_URL
|
|
assert settings.require_https is False
|
|
assert settings.require_signed_manifest is True
|
|
|
|
|
|
def test_forced_update_downloads_verifies_and_launches_installer(tmp_path):
|
|
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
|
|
|
payload = b"fake installer bytes"
|
|
expected_sha = hashlib.sha256(payload).hexdigest()
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://example.test/update.json",
|
|
download_dir=tmp_path,
|
|
installer_args=("/VERYSILENT", "/CURRENTUSER"),
|
|
)
|
|
downloaded: list[str] = []
|
|
launched: list[list[str]] = []
|
|
|
|
def fetch_manifest(url: str, timeout: float) -> dict:
|
|
assert url == settings.manifest_url
|
|
return {
|
|
"version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
|
"sha256": expected_sha,
|
|
"force": True,
|
|
}
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
downloaded.append(url)
|
|
assert dest.parent.exists()
|
|
dest.write_bytes(payload)
|
|
|
|
def launch(cmd: list[str]) -> None:
|
|
launched.append(cmd)
|
|
|
|
result = perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=fetch_manifest,
|
|
download_file=download,
|
|
launch_installer=launch,
|
|
)
|
|
|
|
assert result.status == "update_started"
|
|
assert result.exit_required is True
|
|
assert downloaded == ["https://example.test/DouyinGuaji-3.3.4-Setup.exe"]
|
|
assert launched
|
|
assert launched[0][:3] == [
|
|
str(tmp_path / "DouyinGuaji-3.3.4-Setup.exe"),
|
|
"/VERYSILENT",
|
|
"/CURRENTUSER",
|
|
]
|
|
assert any(arg.startswith("/LOG=") for arg in launched[0])
|
|
|
|
|
|
def test_default_update_settings_launch_visible_installer_and_disable_delta(tmp_path):
|
|
from douyin.updater.force_update import DEFAULT_INSTALLER_ARGS, UpdateSettings
|
|
|
|
settings = UpdateSettings(enabled=True, manifest_url="https://example.test/update.json", download_dir=tmp_path)
|
|
|
|
assert "/VERYSILENT" not in DEFAULT_INSTALLER_ARGS
|
|
assert "/SILENT" not in DEFAULT_INSTALLER_ARGS
|
|
assert "/SUPPRESSMSGBOXES" not in DEFAULT_INSTALLER_ARGS
|
|
assert settings.allow_delta is False
|
|
|
|
|
|
def test_forced_update_prefers_matching_delta_package(tmp_path):
|
|
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
|
|
|
import zipfile
|
|
|
|
file_payload = b"new exe bytes"
|
|
file_sha = hashlib.sha256(file_payload).hexdigest()
|
|
delta_zip = tmp_path / "delta-source.zip"
|
|
with zipfile.ZipFile(delta_zip, "w") as zf:
|
|
zf.writestr(
|
|
"manifest.json",
|
|
json.dumps(
|
|
{
|
|
"schema": 1,
|
|
"from_version": "3.3.3",
|
|
"to_version": "3.3.4",
|
|
"files": [{"path": "DouyinGuajiGUI.exe", "sha256": file_sha, "size": len(file_payload)}],
|
|
"delete": [],
|
|
},
|
|
ensure_ascii=False,
|
|
),
|
|
)
|
|
zf.writestr("files/DouyinGuajiGUI.exe", file_payload)
|
|
delta_payload = delta_zip.read_bytes()
|
|
delta_sha = hashlib.sha256(delta_payload).hexdigest()
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://example.test/update.json",
|
|
download_dir=tmp_path,
|
|
allow_delta=True,
|
|
)
|
|
downloaded: list[str] = []
|
|
launched: list[Path] = []
|
|
|
|
def fetch_manifest(url: str, timeout: float) -> dict:
|
|
return {
|
|
"version": "3.3.4",
|
|
"url": "https://example.test/full.exe",
|
|
"sha256": "0" * 64,
|
|
"delta": {
|
|
"from_version": "3.3.3",
|
|
"to_version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"filename": "DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"sha256": delta_sha,
|
|
},
|
|
}
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
downloaded.append(url)
|
|
dest.write_bytes(delta_payload)
|
|
|
|
result = perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=fetch_manifest,
|
|
download_file=download,
|
|
launch_installer=lambda cmd: pytest.fail("should not launch full installer"),
|
|
launch_delta_update=lambda path, version: launched.append(path),
|
|
)
|
|
|
|
assert result.status == "delta_update_started"
|
|
assert result.exit_required is True
|
|
assert downloaded == ["https://example.test/DouyinGuaji-3.3.3_to_3.3.4_delta.zip"]
|
|
assert launched == [tmp_path / "DouyinGuaji-3.3.3_to_3.3.4_delta.zip"]
|
|
assert result.delta_path == launched[0]
|
|
|
|
|
|
def test_forced_update_picks_matching_delta_from_list(tmp_path):
|
|
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
|
|
|
import zipfile
|
|
|
|
file_payload = b"latest bytes"
|
|
file_sha = hashlib.sha256(file_payload).hexdigest()
|
|
delta_zip = tmp_path / "delta-source.zip"
|
|
with zipfile.ZipFile(delta_zip, "w") as zf:
|
|
zf.writestr(
|
|
"manifest.json",
|
|
json.dumps(
|
|
{
|
|
"schema": 1,
|
|
"from_version": "3.3.7",
|
|
"to_version": "3.3.9",
|
|
"files": [{"path": "DouyinGuajiGUI.exe", "sha256": file_sha, "size": len(file_payload)}],
|
|
"delete": [],
|
|
},
|
|
ensure_ascii=False,
|
|
),
|
|
)
|
|
zf.writestr("files/DouyinGuajiGUI.exe", file_payload)
|
|
delta_payload = delta_zip.read_bytes()
|
|
delta_sha = hashlib.sha256(delta_payload).hexdigest()
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://example.test/update.json",
|
|
download_dir=tmp_path,
|
|
allow_delta=True,
|
|
)
|
|
downloaded: list[str] = []
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
downloaded.append(url)
|
|
dest.write_bytes(delta_payload)
|
|
|
|
result = perform_forced_update(
|
|
settings,
|
|
current_version="3.3.7",
|
|
fetch_manifest=lambda url, timeout: {
|
|
"version": "3.3.9",
|
|
"url": "https://example.test/full.exe",
|
|
"sha256": "0" * 64,
|
|
"delta": [
|
|
{
|
|
"from_version": "3.3.8",
|
|
"to_version": "3.3.9",
|
|
"url": "https://example.test/DouyinGuaji-3.3.8_to_3.3.9_delta.zip",
|
|
"filename": "DouyinGuaji-3.3.8_to_3.3.9_delta.zip",
|
|
"sha256": "1" * 64,
|
|
},
|
|
{
|
|
"from_version": "3.3.7",
|
|
"to_version": "3.3.9",
|
|
"url": "https://example.test/DouyinGuaji-3.3.7_to_3.3.9_delta.zip",
|
|
"filename": "DouyinGuaji-3.3.7_to_3.3.9_delta.zip",
|
|
"sha256": delta_sha,
|
|
},
|
|
],
|
|
},
|
|
download_file=download,
|
|
launch_installer=lambda cmd: pytest.fail("should not launch full installer"),
|
|
launch_delta_update=lambda path, version: None,
|
|
)
|
|
|
|
assert result.status == "delta_update_started"
|
|
assert downloaded == ["https://example.test/DouyinGuaji-3.3.7_to_3.3.9_delta.zip"]
|
|
|
|
|
|
def test_forced_update_falls_back_to_full_installer_when_delta_download_fails(tmp_path):
|
|
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
|
|
|
full_payload = b"full installer"
|
|
full_sha = hashlib.sha256(full_payload).hexdigest()
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://example.test/update.json",
|
|
download_dir=tmp_path,
|
|
allow_delta=True,
|
|
)
|
|
downloaded: list[str] = []
|
|
launched: list[list[str]] = []
|
|
|
|
def fetch_manifest(url: str, timeout: float) -> dict:
|
|
return {
|
|
"version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
|
"filename": "DouyinGuaji-3.3.4-Setup.exe",
|
|
"sha256": full_sha,
|
|
"delta": {
|
|
"from_version": "3.3.3",
|
|
"to_version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"filename": "DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"sha256": "1" * 64,
|
|
},
|
|
}
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
downloaded.append(url)
|
|
if url.endswith("_delta.zip"):
|
|
raise OSError("network interrupted")
|
|
dest.write_bytes(full_payload)
|
|
|
|
result = perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=fetch_manifest,
|
|
download_file=download,
|
|
launch_installer=lambda cmd: launched.append(cmd),
|
|
launch_delta_update=lambda path, version: pytest.fail("should not launch broken delta"),
|
|
)
|
|
|
|
assert result.status == "update_started"
|
|
assert downloaded == [
|
|
"https://example.test/DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
|
]
|
|
assert launched
|
|
assert launched[0][0] == str(tmp_path / "DouyinGuaji-3.3.4-Setup.exe")
|
|
|
|
|
|
def test_forced_update_logs_delta_failure_before_full_fallback(tmp_path, monkeypatch):
|
|
from douyin.core import path_config
|
|
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
|
|
|
full_payload = b"full installer"
|
|
full_sha = hashlib.sha256(full_payload).hexdigest()
|
|
logs_dir = tmp_path / "logs"
|
|
monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir))
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://example.test/update.json",
|
|
download_dir=tmp_path,
|
|
allow_delta=True,
|
|
)
|
|
|
|
def fetch_manifest(url: str, timeout: float) -> dict:
|
|
return {
|
|
"version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
|
"filename": "DouyinGuaji-3.3.4-Setup.exe",
|
|
"sha256": full_sha,
|
|
"delta": {
|
|
"from_version": "3.3.3",
|
|
"to_version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"filename": "DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"sha256": "1" * 64,
|
|
},
|
|
}
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
if url.endswith("_delta.zip"):
|
|
raise OSError("network interrupted")
|
|
dest.write_bytes(full_payload)
|
|
|
|
result = perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=fetch_manifest,
|
|
download_file=download,
|
|
launch_installer=lambda cmd: None,
|
|
launch_delta_update=lambda path, version: pytest.fail("should not launch broken delta"),
|
|
)
|
|
|
|
log_path = logs_dir / "updates" / "update-client.log"
|
|
lines = [json.loads(line) for line in log_path.read_text(encoding="utf-8").splitlines()]
|
|
|
|
assert result.status == "update_started"
|
|
assert any(item["event"] == "delta_download_start" for item in lines)
|
|
failed = [item for item in lines if item["event"] == "delta_failed_fallback_full"]
|
|
assert failed
|
|
assert "network interrupted" in failed[0]["error"]
|
|
|
|
|
|
def test_forced_update_skips_delta_after_previous_started_attempt_still_old_version(tmp_path):
|
|
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
|
|
|
import zipfile
|
|
|
|
full_payload = b"full installer"
|
|
full_sha = hashlib.sha256(full_payload).hexdigest()
|
|
file_payload = b"new exe"
|
|
file_sha = hashlib.sha256(file_payload).hexdigest()
|
|
delta_zip = tmp_path / "delta-source.zip"
|
|
with zipfile.ZipFile(delta_zip, "w") as zf:
|
|
zf.writestr(
|
|
"manifest.json",
|
|
json.dumps(
|
|
{
|
|
"schema": 1,
|
|
"from_version": "3.3.3",
|
|
"to_version": "3.3.4",
|
|
"files": [{"path": "DouyinGuajiGUI.exe", "sha256": file_sha, "size": len(file_payload)}],
|
|
"delete": [],
|
|
},
|
|
ensure_ascii=False,
|
|
),
|
|
)
|
|
zf.writestr("files/DouyinGuajiGUI.exe", file_payload)
|
|
delta_payload = delta_zip.read_bytes()
|
|
delta_sha = hashlib.sha256(delta_payload).hexdigest()
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://example.test/update.json",
|
|
download_dir=tmp_path,
|
|
allow_delta=True,
|
|
)
|
|
launched_installers: list[list[str]] = []
|
|
launched_deltas: list[Path] = []
|
|
|
|
manifest = {
|
|
"version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
|
"filename": "DouyinGuaji-3.3.4-Setup.exe",
|
|
"sha256": full_sha,
|
|
"delta": {
|
|
"from_version": "3.3.3",
|
|
"to_version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"filename": "DouyinGuaji-3.3.3_to_3.3.4_delta.zip",
|
|
"sha256": delta_sha,
|
|
},
|
|
}
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
if url.endswith("_delta.zip"):
|
|
dest.write_bytes(delta_payload)
|
|
else:
|
|
dest.write_bytes(full_payload)
|
|
|
|
first = perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=lambda url, timeout: manifest,
|
|
download_file=download,
|
|
launch_installer=lambda cmd: pytest.fail("first attempt should prefer delta"),
|
|
launch_delta_update=lambda path, version: launched_deltas.append(path),
|
|
)
|
|
|
|
second = perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=lambda url, timeout: manifest,
|
|
download_file=download,
|
|
launch_installer=lambda cmd: launched_installers.append(cmd),
|
|
launch_delta_update=lambda path, version: pytest.fail("second attempt should skip failed delta"),
|
|
)
|
|
|
|
assert first.status == "delta_update_started"
|
|
assert launched_deltas == [tmp_path / "DouyinGuaji-3.3.3_to_3.3.4_delta.zip"]
|
|
assert second.status == "update_started"
|
|
assert launched_installers
|
|
|
|
|
|
def test_forced_update_accepts_signed_manifest(tmp_path, monkeypatch):
|
|
from douyin.updater import force_update
|
|
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
|
|
|
private_key = serialization.load_pem_private_key(TEST_PRIVATE_KEY_PEM, password=None)
|
|
monkeypatch.setattr(force_update, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
payload = b"signed installer"
|
|
expected_sha = hashlib.sha256(payload).hexdigest()
|
|
manifest = {
|
|
"enabled": True,
|
|
"version": "3.3.4",
|
|
"url": "https://dy.absfan.cn/api/v1/update/download/DouyinGuaji-3.3.4-Setup.exe",
|
|
"filename": "DouyinGuaji-3.3.4-Setup.exe",
|
|
"sha256": expected_sha,
|
|
"force": True,
|
|
}
|
|
manifest["signature"] = force_update.base64.b64encode(
|
|
private_key.sign(force_update._canonical_payload_bytes(dict(manifest)))
|
|
).decode("ascii")
|
|
launched: list[list[str]] = []
|
|
|
|
result = perform_forced_update(
|
|
UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://dy.absfan.cn/api/v1/update/manifest",
|
|
download_dir=tmp_path,
|
|
require_https=False,
|
|
require_signed_manifest=True,
|
|
),
|
|
current_version="3.3.3",
|
|
fetch_manifest=lambda url, timeout: manifest,
|
|
download_file=lambda url, dest, timeout: dest.write_bytes(payload),
|
|
launch_installer=lambda cmd: launched.append(cmd),
|
|
)
|
|
|
|
assert result.status == "update_started"
|
|
assert launched
|
|
|
|
|
|
def test_forced_update_rejects_bad_signed_manifest(tmp_path, monkeypatch):
|
|
from douyin.updater import force_update
|
|
from douyin.updater.force_update import UpdateError, UpdateSettings, perform_forced_update
|
|
|
|
monkeypatch.setattr(force_update, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
manifest = {
|
|
"version": "3.3.4",
|
|
"url": "https://dy.absfan.cn/api/v1/update/download/app.exe",
|
|
"sha256": "0" * 64,
|
|
"signature": "bad",
|
|
}
|
|
|
|
with pytest.raises(UpdateError, match="签名"):
|
|
perform_forced_update(
|
|
UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://dy.absfan.cn/api/v1/update/manifest",
|
|
download_dir=tmp_path,
|
|
require_https=False,
|
|
require_signed_manifest=True,
|
|
),
|
|
current_version="3.3.3",
|
|
fetch_manifest=lambda url, timeout: manifest,
|
|
download_file=lambda url, dest, timeout: None,
|
|
launch_installer=lambda cmd: None,
|
|
)
|
|
|
|
|
|
def test_launch_installer_on_windows_waits_for_parent_before_setup(monkeypatch, tmp_path):
|
|
from douyin.updater import force_update
|
|
|
|
calls: list[tuple[list[str], dict]] = []
|
|
monkeypatch.setattr(force_update.sys, "platform", "win32")
|
|
monkeypatch.setattr(force_update.os, "getpid", lambda: 24680)
|
|
|
|
def fake_popen(cmd, **kwargs):
|
|
calls.append((list(cmd), kwargs))
|
|
|
|
class Proc:
|
|
pass
|
|
|
|
return Proc()
|
|
|
|
monkeypatch.setattr(force_update.subprocess, "Popen", fake_popen)
|
|
|
|
installer = tmp_path / "DouyinGuaji Setup.exe"
|
|
force_update.launch_installer([str(installer), "/VERYSILENT", "/LOG=C:\\tmp\\install.log"])
|
|
|
|
assert calls
|
|
cmd, kwargs = calls[0]
|
|
assert cmd[:4] == ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass"]
|
|
assert "-WindowStyle" in cmd
|
|
script = cmd[-1]
|
|
assert "Wait-Process -Id 24680" in script
|
|
assert "Start-Process" in script
|
|
assert str(installer) in script
|
|
assert kwargs["cwd"] == str(tmp_path)
|
|
assert kwargs["close_fds"] is True
|
|
assert kwargs["stdin"] is force_update.subprocess.DEVNULL
|
|
|
|
|
|
def test_forced_update_rejects_hash_mismatch(tmp_path):
|
|
from douyin.updater.force_update import (
|
|
UpdateError,
|
|
UpdateSettings,
|
|
perform_forced_update,
|
|
)
|
|
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://example.test/update.json",
|
|
download_dir=tmp_path,
|
|
)
|
|
|
|
def fetch_manifest(url: str, timeout: float) -> dict:
|
|
return {
|
|
"version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
|
"sha256": "0" * 64,
|
|
"force": True,
|
|
}
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
dest.write_bytes(b"tampered")
|
|
|
|
with pytest.raises(UpdateError, match="sha256"):
|
|
perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=fetch_manifest,
|
|
download_file=download,
|
|
launch_installer=lambda cmd: None,
|
|
)
|
|
|
|
|
|
def test_forced_update_rejects_insecure_manifest_url_by_default(tmp_path):
|
|
from douyin.updater.force_update import (
|
|
UpdateError,
|
|
UpdateSettings,
|
|
perform_forced_update,
|
|
)
|
|
|
|
called = False
|
|
|
|
def fetch_manifest(url: str, timeout: float) -> dict:
|
|
nonlocal called
|
|
called = True
|
|
return {}
|
|
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="http://example.test/update.json",
|
|
download_dir=tmp_path,
|
|
)
|
|
|
|
with pytest.raises(UpdateError, match="HTTPS"):
|
|
perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=fetch_manifest,
|
|
download_file=lambda url, dest, timeout: None,
|
|
launch_installer=lambda cmd: None,
|
|
)
|
|
|
|
assert called is False
|
|
|
|
|
|
def test_forced_update_rejects_cross_origin_installer_url_by_default(tmp_path):
|
|
from douyin.updater.force_update import (
|
|
UpdateError,
|
|
UpdateSettings,
|
|
perform_forced_update,
|
|
)
|
|
|
|
payload = b"fake installer bytes"
|
|
expected_sha = hashlib.sha256(payload).hexdigest()
|
|
downloaded: list[str] = []
|
|
settings = UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://updates.example.test/update.json",
|
|
download_dir=tmp_path,
|
|
)
|
|
|
|
def fetch_manifest(url: str, timeout: float) -> dict:
|
|
return {
|
|
"version": "3.3.4",
|
|
"url": "https://evil.example.test/DouyinGuaji-3.3.4-Setup.exe",
|
|
"sha256": expected_sha,
|
|
}
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
downloaded.append(url)
|
|
dest.write_bytes(payload)
|
|
|
|
with pytest.raises(UpdateError, match="同源"):
|
|
perform_forced_update(
|
|
settings,
|
|
current_version="3.3.3",
|
|
fetch_manifest=fetch_manifest,
|
|
download_file=download,
|
|
launch_installer=lambda cmd: None,
|
|
)
|
|
|
|
assert downloaded == []
|
|
|
|
|
|
def test_forced_update_ignores_manifest_force_false_for_newer_version(tmp_path):
|
|
from douyin.updater.force_update import UpdateSettings, perform_forced_update
|
|
|
|
payload = b"forced installer"
|
|
expected_sha = hashlib.sha256(payload).hexdigest()
|
|
launched: list[list[str]] = []
|
|
|
|
def fetch_manifest(url: str, timeout: float) -> dict:
|
|
return {
|
|
"version": "3.3.4",
|
|
"url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe",
|
|
"sha256": expected_sha,
|
|
"force": False,
|
|
}
|
|
|
|
def download(url: str, dest: Path, timeout: float) -> None:
|
|
dest.write_bytes(payload)
|
|
|
|
result = perform_forced_update(
|
|
UpdateSettings(enabled=True, manifest_url="https://example.test/update.json", download_dir=tmp_path),
|
|
current_version="3.3.3",
|
|
fetch_manifest=fetch_manifest,
|
|
download_file=download,
|
|
launch_installer=lambda cmd: launched.append(cmd),
|
|
)
|
|
|
|
assert result.status == "update_started"
|
|
assert launched
|
|
|
|
|
|
def test_startup_update_raises_system_exit_after_launch(tmp_path):
|
|
from douyin.updater.force_update import UpdateResult, run_startup_update_check
|
|
|
|
called: list[str] = []
|
|
|
|
def perform(*, settings, current_version):
|
|
called.append(current_version)
|
|
return UpdateResult("update_started", "started", exit_required=True)
|
|
|
|
with pytest.raises(SystemExit) as exc:
|
|
run_startup_update_check(
|
|
app_name="test_app",
|
|
current_version="3.3.3",
|
|
perform_update=perform,
|
|
)
|
|
|
|
assert exc.value.code == 0
|
|
assert called == ["3.3.3"]
|
|
|
|
|
|
def test_startup_update_fail_closed_blocks_old_client(tmp_path, monkeypatch):
|
|
from douyin.updater.force_update import UpdateError, run_startup_update_check
|
|
|
|
monkeypatch.setenv("DOUYIN_AUTO_UPDATE_ENABLED", "1")
|
|
monkeypatch.setenv("DOUYIN_UPDATE_MANIFEST_URL", "https://example.test/update.json")
|
|
|
|
def perform(*, settings, current_version):
|
|
raise UpdateError("network down")
|
|
|
|
with pytest.raises(SystemExit) as exc:
|
|
run_startup_update_check(
|
|
app_name="test_app",
|
|
current_version="3.3.3",
|
|
perform_update=perform,
|
|
)
|
|
|
|
assert exc.value.code == 3
|
|
|
|
|
|
def test_early_bootstrap_fail_closed_blocks_if_updater_crashes(monkeypatch):
|
|
from douyin.updater import bootstrap
|
|
from douyin.updater.force_update import UpdateSettings
|
|
|
|
monkeypatch.setattr(
|
|
bootstrap,
|
|
"load_update_settings",
|
|
lambda: UpdateSettings(
|
|
enabled=True,
|
|
manifest_url="https://example.test/update.json",
|
|
download_dir=Path("updates"),
|
|
fail_closed=True,
|
|
),
|
|
)
|
|
|
|
def crash(*, app_name: str):
|
|
raise RuntimeError("broken updater import")
|
|
|
|
with pytest.raises(SystemExit) as exc:
|
|
bootstrap.run_startup_update_check_early(
|
|
app_name="test_app",
|
|
update_check=crash,
|
|
)
|
|
|
|
assert exc.value.code == 3
|