Files
douyi_guaji_main/tests/test_license_manager.py
T
2026-06-17 23:01:21 +08:00

191 lines
6.2 KiB
Python

from __future__ import annotations
import json
from datetime import datetime, timedelta, timezone
import pytest
def test_signed_license_is_accepted_for_current_machine(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="local-test",
)
status = license_manager.check_license(license_path)
assert status.ok is True
assert status.reason == "ok"
assert status.payload["customer"] == "local-test"
def test_license_rejects_different_machine(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-b")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
)
status = license_manager.check_license(license_path)
assert status.ok is False
assert status.reason == "machine_mismatch"
def test_license_rejects_expired_file(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) - timedelta(days=1),
)
status = license_manager.check_license(license_path)
assert status.ok is False
assert status.reason == "expired"
def test_license_rejects_tampered_payload(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
)
raw = json.loads(license_path.read_text(encoding="utf-8"))
raw["payload"]["customer"] = "tampered"
license_path.write_text(json.dumps(raw, ensure_ascii=False), encoding="utf-8")
status = license_manager.check_license(license_path)
assert status.ok is False
assert status.reason == "invalid_signature"
def test_missing_license_is_allowed_in_source_mode(tmp_path, monkeypatch):
from douyin.license import license_manager
monkeypatch.delenv("DOUYIN_LICENSE_REQUIRED", raising=False)
status = license_manager.check_license(tmp_path / "missing.dat")
assert status.ok is True
assert status.reason == "dev_bypass"
def test_missing_license_can_be_required(tmp_path, monkeypatch):
from douyin.license import license_manager
monkeypatch.setenv("DOUYIN_LICENSE_REQUIRED", "1")
status = license_manager.check_license(tmp_path / "missing.dat")
assert status.ok is False
assert status.reason == "missing"
def test_install_license_accepts_json_text(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
source_path = tmp_path / "source_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="json-text",
)
result = license_manager.install_license_text(
source_path.read_text(encoding="utf-8"),
target_path,
)
assert result.ok is True
assert result.reason == "ok"
assert target_path.exists()
assert license_manager.check_license(target_path).payload["customer"] == "json-text"
def test_install_license_accepts_activation_code(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license, encode_activation_code
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
source_path = tmp_path / "source_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="activation-code",
)
code = encode_activation_code(source_path.read_text(encoding="utf-8"))
result = license_manager.install_license_text(code, target_path)
assert result.ok is True
assert target_path.exists()
assert license_manager.check_license(target_path).payload["customer"] == "activation-code"
def test_install_license_rejects_invalid_text(tmp_path):
from douyin.license import license_manager
target_path = tmp_path / "license.dat"
result = license_manager.install_license_text("not-a-license", target_path)
assert result.ok is False
assert result.reason == "invalid_format"
assert not target_path.exists()
def test_license_status_public_payload_hides_signature(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
license_path = tmp_path / "license.dat"
create_dev_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
customer="public-status",
)
payload = license_manager.get_license_status_payload(license_path)
assert payload["ok"] is True
assert payload["reason"] == "ok"
assert payload["customer"] == "public-status"
assert "signature" not in payload
assert payload["machine_fingerprint"]