Fix encrypted package runtime validation
This commit is contained in:
1 parent
abcee63776
commit
cc4caef6e1
7 files changed
+352
-21
No files matched your search
@@ -35,7 +35,28 @@ def test_distribution_manifest_blocks_source_and_debug_artifacts():
|
||||
|
||||
|
||||
def test_distribution_manifest_allows_expected_runtime_shape():
|
||||
from tools.distribution_manifest import find_forbidden_distribution_entries
|
||||
from tools.distribution_manifest import find_distribution_issues
|
||||
|
||||
entries = [
|
||||
"DouyinGuajiGUI.exe",
|
||||
"web_gui.exe",
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"assets/fudai_task_count_v2_one_task.png",
|
||||
"webui/index.html",
|
||||
"config/README.md",
|
||||
"logs/.keep",
|
||||
"pic/.keep",
|
||||
"state/.keep",
|
||||
"license.dat",
|
||||
]
|
||||
|
||||
assert find_distribution_issues(entries) == []
|
||||
|
||||
|
||||
def test_distribution_manifest_requires_python_runtime_dll():
|
||||
from tools.distribution_manifest import find_distribution_issues
|
||||
|
||||
entries = [
|
||||
"DouyinGuajiGUI.exe",
|
||||
@@ -48,10 +69,11 @@ def test_distribution_manifest_allows_expected_runtime_shape():
|
||||
"logs/.keep",
|
||||
"pic/.keep",
|
||||
"state/.keep",
|
||||
"license.dat",
|
||||
]
|
||||
|
||||
assert find_forbidden_distribution_entries(entries) == []
|
||||
issues = find_distribution_issues(entries)
|
||||
|
||||
assert "_internal/python*.dll" in issues
|
||||
|
||||
|
||||
def test_distribution_manifest_cli_passes_clean_tree(tmp_path):
|
||||
@@ -61,6 +83,7 @@ def test_distribution_manifest_cli_passes_clean_tree(tmp_path):
|
||||
"web_gui.exe",
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"assets/fudai_task_count_v2_one_task.png",
|
||||
"webui/index.html",
|
||||
"config/README.md",
|
||||
|
||||
@@ -65,21 +65,43 @@ def test_smoke_check_packed_exes_runs_after_packing(tmp_path):
|
||||
_write_exe(dist / name)
|
||||
calls: list[list[str]] = []
|
||||
|
||||
def fake_runner(cmd, *, capture_output, text, timeout):
|
||||
def fake_runner(cmd, *, capture_output, text, timeout, env):
|
||||
calls.append([str(x) for x in cmd])
|
||||
assert capture_output is True
|
||||
assert text is True
|
||||
assert timeout == 20
|
||||
assert env["DOUYIN_LICENSE_REQUIRED"] == "0"
|
||||
assert env["SKIP_OCR_INIT"] == "1"
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout="ok")
|
||||
|
||||
smoke_check_distribution_exes(dist, runner=fake_runner)
|
||||
|
||||
assert calls == [
|
||||
[str(dist / "run_cli.exe"), "--help"],
|
||||
[str(dist / "web_gui.exe"), "--help"],
|
||||
]
|
||||
|
||||
|
||||
def test_smoke_check_cleans_runtime_logs_it_creates(tmp_path):
|
||||
from tools.exe_packing import smoke_check_distribution_exes
|
||||
|
||||
dist = tmp_path / "dist"
|
||||
for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"):
|
||||
_write_exe(dist / name)
|
||||
(dist / "logs").mkdir(parents=True)
|
||||
(dist / "logs" / ".keep").write_text("", encoding="utf-8")
|
||||
|
||||
def fake_runner(cmd, *, capture_output, text, timeout, env):
|
||||
(dist / "logs" / "unified.log").write_text("smoke log", encoding="utf-8")
|
||||
(dist / "logs" / "error.log").write_text("smoke error", encoding="utf-8")
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout="ok")
|
||||
|
||||
smoke_check_distribution_exes(dist, runner=fake_runner)
|
||||
|
||||
assert (dist / "logs" / ".keep").exists()
|
||||
assert not (dist / "logs" / "unified.log").exists()
|
||||
assert not (dist / "logs" / "error.log").exists()
|
||||
|
||||
|
||||
def test_smoke_check_packed_exes_fails_on_broken_exe(tmp_path):
|
||||
from tools.exe_packing import smoke_check_distribution_exes
|
||||
|
||||
@@ -87,7 +109,7 @@ def test_smoke_check_packed_exes_fails_on_broken_exe(tmp_path):
|
||||
for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"):
|
||||
_write_exe(dist / name)
|
||||
|
||||
def fake_runner(cmd, *, capture_output, text, timeout):
|
||||
def fake_runner(cmd, *, capture_output, text, timeout, env):
|
||||
return subprocess.CompletedProcess(cmd, 1, stdout="", stderr="Failed to load Python DLL")
|
||||
|
||||
with pytest.raises(RuntimeError, match="smoke check failed"):
|
||||
@@ -122,4 +144,27 @@ def test_windows_packaging_script_exposes_pack_exe_switch():
|
||||
assert "[switch]$PackExeForce" in script
|
||||
assert "[string]$UpxDir" in script
|
||||
assert "tools.exe_packing" in script
|
||||
assert "--smoke-only" in script
|
||||
assert "if ($LASTEXITCODE -ne 0)" in script
|
||||
|
||||
|
||||
def test_exe_packing_cli_smoke_only_skips_upx(monkeypatch, tmp_path):
|
||||
from tools import exe_packing
|
||||
|
||||
dist = tmp_path / "dist"
|
||||
for name in ("DouyinGuajiGUI.exe", "web_gui.exe", "run_cli.exe"):
|
||||
_write_exe(dist / name)
|
||||
called = {"pack": False, "smoke": False}
|
||||
|
||||
def fake_pack(*args, **kwargs):
|
||||
called["pack"] = True
|
||||
return []
|
||||
|
||||
def fake_smoke(*args, **kwargs):
|
||||
called["smoke"] = True
|
||||
|
||||
monkeypatch.setattr(exe_packing, "pack_distribution_exes", fake_pack)
|
||||
monkeypatch.setattr(exe_packing, "smoke_check_distribution_exes", fake_smoke)
|
||||
|
||||
assert exe_packing.main([str(dist), "--smoke-only"]) == 0
|
||||
assert called == {"pack": False, "smoke": True}
|
||||
@@ -111,11 +111,78 @@ def test_create_pyarmor_staging_moves_flat_entry_script_back_to_bin(tmp_path, mo
|
||||
assert not (staging / "run_cli.py").exists()
|
||||
|
||||
|
||||
def test_collect_runtime_hidden_imports_uses_original_sources(tmp_path):
|
||||
from tools.obfuscation_staging import collect_runtime_hidden_imports
|
||||
|
||||
root = tmp_path / "project"
|
||||
_write(
|
||||
root / "douyin" / "license" / "license_manager.py",
|
||||
"\n".join(
|
||||
[
|
||||
"import json",
|
||||
"import uuid",
|
||||
"from pathlib import Path",
|
||||
"from douyin.core import path_config",
|
||||
"from .public_key import PUBLIC_KEY_PEM",
|
||||
]
|
||||
),
|
||||
)
|
||||
_write(root / "gui" / "web_gui.py", "import secrets\nfrom http.server import ThreadingHTTPServer\n")
|
||||
_write(root / "operations" / "__init__.py")
|
||||
_write(root / "bin" / "run_cli.py", "from douyin.license.license_manager import check_license\n")
|
||||
|
||||
hidden_imports = collect_runtime_hidden_imports(root)
|
||||
|
||||
assert "json" in hidden_imports
|
||||
assert "uuid" in hidden_imports
|
||||
assert "pathlib" in hidden_imports
|
||||
assert "secrets" in hidden_imports
|
||||
assert "http.server" in hidden_imports
|
||||
assert "douyin.core" not in hidden_imports
|
||||
assert "douyin.license.license_manager" not in hidden_imports
|
||||
|
||||
|
||||
def test_collect_runtime_hidden_imports_skips_optional_heavy_modules(tmp_path):
|
||||
from tools.obfuscation_staging import collect_runtime_hidden_imports
|
||||
|
||||
root = tmp_path / "project"
|
||||
_write(
|
||||
root / "operations" / "ocr_operations.py",
|
||||
"import easyocr\nimport torch\nimport pandas\nimport scipy\nimport paddleocr\n",
|
||||
)
|
||||
_write(root / "douyin" / "__init__.py")
|
||||
_write(root / "gui" / "gui_app.py")
|
||||
_write(root / "bin" / "run_cli.py")
|
||||
|
||||
hidden_imports = collect_runtime_hidden_imports(root)
|
||||
|
||||
assert "easyocr" not in hidden_imports
|
||||
assert "torch" not in hidden_imports
|
||||
assert "pandas" not in hidden_imports
|
||||
assert "scipy" not in hidden_imports
|
||||
|
||||
|
||||
def test_windows_packaging_script_exposes_obfuscate_switch():
|
||||
script = Path("packaging/pyinstaller/build_windows.ps1").read_text(encoding="utf-8")
|
||||
|
||||
assert "[switch]$Obfuscate" in script
|
||||
assert "tools.obfuscation_staging" in script
|
||||
assert "--hidden-imports-output $HiddenImportsFile" in script
|
||||
assert "ConvertFrom-Json" in script
|
||||
assert '$CliOnlyHiddenImportRoots = @("cv2", "numpy", "paddleocr", "paddle")' in script
|
||||
assert '$ExcludedOptionalModules = @(' in script
|
||||
assert '"torch"' in script
|
||||
assert '"modelscope"' in script
|
||||
assert "python -m PyInstaller @CommonArgs @RunCliArgs --name run_cli" in script
|
||||
assert "PyArmor obfuscation failed" in script
|
||||
assert "--paths\", $BuildSourceRoot" in script
|
||||
assert '"--collect-submodules", "douyin"' in script
|
||||
assert '"--collect-submodules", "gui"' in script
|
||||
assert '"--collect-submodules", "operations"' in script
|
||||
assert '"--hidden-import", "logging.handlers"' in script
|
||||
assert "tools.exe_packing $DistRoot --smoke-only" in script
|
||||
assert "Executable smoke check failed" in script
|
||||
assert "Post-smoke distribution manifest check failed" in script
|
||||
assert "Get-ChildItem -LiteralPath $srcInternal -Force | Copy-Item" in script
|
||||
assert "Join-Path $BuildSourceRoot \"gui\\gui_app.py\"" in script
|
||||
assert "Join-Path $BuildSourceRoot \"bin\\run_cli.py\"" in script
|
||||
Reference in new issue
Block a user