110 lines
3.4 KiB
Python
110 lines
3.4 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"
|