fix: harden screenshot capture and diagnostics
This commit is contained in:
1 parent
ee3424cd9e
commit
f010853b97
19 files changed
+870
-70
No files matched your search
@@ -56,6 +56,8 @@ def test_distribution_manifest_blocks_project_native_binaries_outside_core_allow
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"_internal/paddle/libs/paddle.dll",
|
||||
"_internal/paddlex/.version",
|
||||
"_internal/tests/evil.pyd",
|
||||
"_internal/tools/helper.dll",
|
||||
"_internal/douyin/fudai/core/compiled.cp312-win_amd64.pyd",
|
||||
@@ -89,6 +91,8 @@ def test_distribution_manifest_allows_expected_runtime_shape():
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"_internal/paddle/libs/paddle.dll",
|
||||
"_internal/paddlex/.version",
|
||||
"assets/fudai.png",
|
||||
"assets/desktop.png",
|
||||
"assets/fudai_task_count_v2_one_task.png",
|
||||
@@ -138,6 +142,8 @@ def test_distribution_manifest_can_allow_license_for_local_test_package():
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"_internal/paddle/libs/paddle.dll",
|
||||
"_internal/paddlex/.version",
|
||||
"assets/fudai.png",
|
||||
"webui/index.html",
|
||||
"platform-tools/adb.exe",
|
||||
@@ -191,6 +197,8 @@ def test_distribution_manifest_requires_bundled_android_runtime():
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"_internal/paddle/libs/paddle.dll",
|
||||
"_internal/paddlex/.version",
|
||||
"assets/fudai.png",
|
||||
"webui/index.html",
|
||||
"config/README.md",
|
||||
@@ -208,6 +216,32 @@ def test_distribution_manifest_requires_bundled_android_runtime():
|
||||
assert "android-tools/ADBKeyboard.apk" in issues
|
||||
|
||||
|
||||
def test_distribution_manifest_requires_bundled_ocr_runtime_data():
|
||||
from tools.distribution_manifest import find_distribution_issues
|
||||
|
||||
entries = [
|
||||
"DouyinGuajiGUI.exe",
|
||||
"web_gui.exe",
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"platform-tools/adb.exe",
|
||||
"platform-tools/AdbWinApi.dll",
|
||||
"platform-tools/AdbWinUsbApi.dll",
|
||||
"android-tools/ADBKeyboard.apk",
|
||||
"config/README.md",
|
||||
"config/config.json",
|
||||
"logs/.keep",
|
||||
"pic/.keep",
|
||||
"state/.keep",
|
||||
]
|
||||
|
||||
issues = find_distribution_issues(entries)
|
||||
|
||||
assert "_internal/paddlex/.version" in issues
|
||||
assert "_internal/paddle/*" in issues
|
||||
|
||||
|
||||
def test_distribution_manifest_cli_passes_clean_tree(tmp_path):
|
||||
dist = tmp_path / "dist"
|
||||
for rel in [
|
||||
@@ -216,6 +250,8 @@ def test_distribution_manifest_cli_passes_clean_tree(tmp_path):
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"_internal/paddle/libs/paddle.dll",
|
||||
"_internal/paddlex/.version",
|
||||
"assets/fudai.png",
|
||||
"assets/fudai_task_status_dacheng_shape.png",
|
||||
"webui/css/base.css",
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import zipfile
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM
|
||||
|
||||
|
||||
def test_build_diagnostic_log_archive_tails_logs_only(tmp_path, monkeypatch):
|
||||
from douyin.core import path_config
|
||||
from douyin.diagnostics import log_upload
|
||||
|
||||
logs_dir = tmp_path / "logs"
|
||||
logs_dir.mkdir()
|
||||
(logs_dir / "unified.log").write_text("a" * 40, encoding="utf-8")
|
||||
(logs_dir / "actions.log").write_text("action\n", encoding="utf-8")
|
||||
(tmp_path / "license.dat").write_text("secret", encoding="utf-8")
|
||||
monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir))
|
||||
|
||||
archive = log_upload.build_diagnostic_log_archive(
|
||||
output_dir=tmp_path,
|
||||
max_source_bytes=10,
|
||||
max_archive_bytes=1024 * 1024,
|
||||
)
|
||||
|
||||
with zipfile.ZipFile(archive) as zf:
|
||||
names = set(zf.namelist())
|
||||
assert "logs/unified.log" in names
|
||||
assert "logs/actions.log" in names
|
||||
assert "license.dat" not in names
|
||||
assert zf.read("logs/unified.log") == b"a" * 10
|
||||
|
||||
|
||||
def test_license_server_log_upload_overwrites_same_card(tmp_path):
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from license_server import api
|
||||
from license_server.core import LicenseService
|
||||
|
||||
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
|
||||
card = service.create_cards(days=5, count=1, customer="log-user")[0]
|
||||
activated = service.activate(card.code, machine_fingerprint="machine-api")
|
||||
assert activated.license_doc is not None
|
||||
client = TestClient(api.create_app(service=service))
|
||||
|
||||
first = client.post(
|
||||
"/api/v1/logs/upload",
|
||||
data={"license": json.dumps(activated.license_doc, ensure_ascii=False)},
|
||||
files={"log_file": ("one.zip", b"first", "application/zip")},
|
||||
)
|
||||
second = client.post(
|
||||
"/api/v1/logs/upload",
|
||||
data={"license": json.dumps(activated.license_doc, ensure_ascii=False)},
|
||||
files={"log_file": ("two.zip", b"second", "application/zip")},
|
||||
)
|
||||
entries = service.list_diagnostic_logs()
|
||||
stored = service.diagnostic_log_path(entries[0].key)
|
||||
|
||||
assert first.status_code == 200
|
||||
assert second.status_code == 200
|
||||
assert len(entries) == 1
|
||||
assert entries[0].filename == "two.zip"
|
||||
assert stored is not None
|
||||
assert stored.read_bytes() == b"second"
|
||||
|
||||
|
||||
def test_upload_diagnostic_logs_posts_signed_license_and_file(tmp_path, monkeypatch):
|
||||
from tools.license_dev import create_dev_license
|
||||
from douyin.core import path_config
|
||||
from douyin.license import license_manager
|
||||
from douyin.diagnostics import log_upload
|
||||
|
||||
logs_dir = tmp_path / "logs"
|
||||
logs_dir.mkdir()
|
||||
(logs_dir / "unified.log").write_text("hello\n", encoding="utf-8")
|
||||
license_path = tmp_path / "license.dat"
|
||||
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
||||
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
||||
monkeypatch.setattr(log_upload, "get_default_license_path", lambda: license_path)
|
||||
monkeypatch.setattr(path_config, "get_logs_dir", lambda: str(logs_dir))
|
||||
create_dev_license(
|
||||
output_path=license_path,
|
||||
machine_fingerprint="machine-a",
|
||||
expires_at=datetime.now(timezone.utc) + timedelta(days=3),
|
||||
private_key_pem=TEST_PRIVATE_KEY_PEM,
|
||||
)
|
||||
calls = {}
|
||||
|
||||
def fake_post(url, *, license_doc, file_path, timeout_seconds=20):
|
||||
calls["url"] = url
|
||||
calls["license_doc"] = license_doc
|
||||
calls["file_path"] = file_path
|
||||
return {"ok": True, "message": "ok"}
|
||||
|
||||
result = log_upload.upload_diagnostic_logs(
|
||||
upload_url="https://license.example.test/api/v1/logs/upload",
|
||||
http_post_file=fake_post,
|
||||
)
|
||||
|
||||
assert result.ok is True
|
||||
assert calls["license_doc"]["payload"]["machine_fingerprint"] == "machine-a"
|
||||
assert calls["file_path"].exists()
|
||||
@@ -253,6 +253,22 @@ def test_windows_packaging_script_protects_core_modules_by_default():
|
||||
assert '"--collect-submodules", "operations"' in cli_block
|
||||
assert '"--hidden-import", "logging.handlers"' in script
|
||||
assert '"--hidden-import", "cryptography.hazmat.primitives.serialization"' in script
|
||||
assert '"--collect-submodules", "cv2"' in script
|
||||
assert '"--collect-submodules", "numpy"' in script
|
||||
assert '"--collect-submodules", "PIL"' in script
|
||||
assert '"--collect-submodules", "paddle"' in script
|
||||
assert '"--collect-submodules", "paddleocr"' in script
|
||||
assert '"--collect-submodules", "paddlex"' in script
|
||||
assert '"--collect-submodules", "pyclipper"' in script
|
||||
assert '"--collect-submodules", "shapely"' in script
|
||||
assert '"--collect-binaries", "cv2"' in script
|
||||
assert '"--collect-binaries", "numpy"' in script
|
||||
assert '"--collect-binaries", "paddle"' in script
|
||||
assert '"--collect-binaries", "pyclipper"' in script
|
||||
assert '"--collect-binaries", "shapely"' in script
|
||||
assert '"--collect-data", "PIL"' in script
|
||||
assert '"--collect-data", "paddlex"' in script
|
||||
assert '"--collect-data", "paddleocr"' 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
|
||||
|
||||
@@ -4,6 +4,9 @@ from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
PNG_BYTES = b"\x89PNG\r\n\x1a\n" + (b"x" * 2048) + b"IEND"
|
||||
|
||||
|
||||
def test_capture_screenshot_direct_invalid_fallback_png_returns_false(tmp_path):
|
||||
from douyin.core.underlying_operations_pkg.screenshot_direct import (
|
||||
capture_screenshot_direct,
|
||||
@@ -44,7 +47,7 @@ def test_capture_screenshot_direct_uses_shell_stdout_fallback_when_execout_and_p
|
||||
|
||||
pic_dir = tmp_path / "pic"
|
||||
pic_dir.mkdir()
|
||||
png_bytes = b"\x89PNG\r\n\x1a\n" + (b"x" * 2048) + b"IEND"
|
||||
png_bytes = PNG_BYTES
|
||||
calls: list[list[str]] = []
|
||||
|
||||
class FakePopen:
|
||||
@@ -77,3 +80,55 @@ def test_capture_screenshot_direct_uses_shell_stdout_fallback_when_execout_and_p
|
||||
assert result is True
|
||||
assert (pic_dir / "screenshot.png").exists()
|
||||
assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls)
|
||||
|
||||
|
||||
def test_capture_screenshot_direct_tries_stdout_after_invalid_pull_file(tmp_path):
|
||||
from douyin.core.underlying_operations_pkg.screenshot_direct import (
|
||||
capture_screenshot_direct,
|
||||
)
|
||||
|
||||
fake_self = SimpleNamespace(
|
||||
_adb_path=lambda: "adb",
|
||||
get_screenshot=lambda *_a, **_k: False,
|
||||
)
|
||||
|
||||
pic_dir = tmp_path / "pic"
|
||||
pic_dir.mkdir()
|
||||
calls: list[list[str]] = []
|
||||
|
||||
class FakePopen:
|
||||
returncode = 4294967295
|
||||
|
||||
def __init__(self, cmd, **_kwargs):
|
||||
calls.append(list(cmd))
|
||||
|
||||
def communicate(self, timeout=None): # noqa: ARG002
|
||||
return b"broken", None
|
||||
|
||||
def fake_run(cmd, **kwargs):
|
||||
calls.append(list(cmd))
|
||||
if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 7:
|
||||
return SimpleNamespace(returncode=0, stdout=b"", stderr=b"")
|
||||
if cmd[:4] == ["adb", "-s", "MOCK", "pull"]:
|
||||
with open(cmd[4], "wb") as f:
|
||||
f.write(b"not a png")
|
||||
return SimpleNamespace(returncode=0, stdout=b"", stderr=b"")
|
||||
if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6:
|
||||
return SimpleNamespace(returncode=0, stdout=PNG_BYTES.replace(b"\r\n", b"\r\r\n"), stderr=b"")
|
||||
raise RuntimeError(f"unexpected command: {cmd}")
|
||||
|
||||
with patch(
|
||||
"douyin.core.underlying_operations_pkg.screenshot_direct.path_config.get_pic_dir",
|
||||
return_value=str(pic_dir),
|
||||
), patch(
|
||||
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.Popen",
|
||||
FakePopen,
|
||||
), patch(
|
||||
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.run",
|
||||
side_effect=fake_run,
|
||||
):
|
||||
result = capture_screenshot_direct(fake_self, "MOCK", "pic")
|
||||
|
||||
assert result is True
|
||||
assert (pic_dir / "screenshot.png").read_bytes().startswith(b"\x89PNG")
|
||||
assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls)
|
||||
Reference in new issue
Block a user