347 lines
12 KiB
Python
347 lines
12 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
|
|
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM
|
|
|
|
|
|
def _create_test_license(*args, **kwargs):
|
|
from tools.license_dev import create_dev_license
|
|
|
|
kwargs.setdefault("private_key_pem", TEST_PRIVATE_KEY_PEM)
|
|
return create_dev_license(*args, **kwargs)
|
|
|
|
|
|
def test_signed_license_is_accepted_for_current_machine(tmp_path, monkeypatch):
|
|
from douyin.license import license_manager
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
license_path = tmp_path / "license.dat"
|
|
|
|
_create_test_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
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-b")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
license_path = tmp_path / "license.dat"
|
|
|
|
_create_test_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
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
license_path = tmp_path / "license.dat"
|
|
|
|
_create_test_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
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
license_path = tmp_path / "license.dat"
|
|
|
|
_create_test_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
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
source_path = tmp_path / "source_license.dat"
|
|
target_path = tmp_path / "license.dat"
|
|
_create_test_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 encode_activation_code
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
source_path = tmp_path / "source_license.dat"
|
|
target_path = tmp_path / "license.dat"
|
|
_create_test_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_activate_license_code_installs_server_license(tmp_path, monkeypatch):
|
|
from douyin.license import license_manager
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
source_path = tmp_path / "server_license.dat"
|
|
target_path = tmp_path / "license.dat"
|
|
_create_test_license(
|
|
output_path=source_path,
|
|
machine_fingerprint="machine-a",
|
|
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
|
|
customer="online-user",
|
|
)
|
|
server_doc = json.loads(source_path.read_text(encoding="utf-8"))
|
|
calls = {}
|
|
|
|
def fake_post(url, payload, timeout_seconds=10):
|
|
calls["url"] = url
|
|
calls["payload"] = payload
|
|
calls["timeout_seconds"] = timeout_seconds
|
|
return {"ok": True, "license": server_doc, "reason": "ok", "message": "授权有效"}
|
|
|
|
result = license_manager.install_license_or_activate(
|
|
"DG-AAAA-BBBB-CCCC",
|
|
target_path,
|
|
activation_url="https://license.example.test/api/v1/activate",
|
|
http_post=fake_post,
|
|
)
|
|
|
|
assert result.ok is True
|
|
assert target_path.exists()
|
|
assert calls["payload"] == {
|
|
"card_key": "DG-AAAA-BBBB-CCCC",
|
|
"machine_fingerprint": "machine-a",
|
|
}
|
|
assert license_manager.check_license(target_path).payload["customer"] == "online-user"
|
|
|
|
|
|
def test_plain_card_uses_default_activation_url(tmp_path, monkeypatch):
|
|
from douyin.license import license_manager
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
monkeypatch.setattr(
|
|
license_manager,
|
|
"get_default_activation_url",
|
|
lambda: "https://license.example.test/api/v1/activate",
|
|
)
|
|
source_path = tmp_path / "server_license.dat"
|
|
target_path = tmp_path / "license.dat"
|
|
_create_test_license(
|
|
output_path=source_path,
|
|
machine_fingerprint="machine-a",
|
|
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
|
|
customer="default-url-user",
|
|
)
|
|
server_doc = json.loads(source_path.read_text(encoding="utf-8"))
|
|
calls = {}
|
|
|
|
def fake_post(url, payload, timeout_seconds=10):
|
|
calls["url"] = url
|
|
calls["payload"] = payload
|
|
return {"ok": True, "license": server_doc}
|
|
|
|
result = license_manager.install_license_or_activate(
|
|
"DG-DEFAULT-TEST",
|
|
target_path,
|
|
http_post=fake_post,
|
|
)
|
|
|
|
assert result.ok is True
|
|
assert calls["url"] == "https://license.example.test/api/v1/activate"
|
|
assert calls["payload"]["card_key"] == "DG-DEFAULT-TEST"
|
|
|
|
|
|
def test_online_verify_posts_signed_license_doc(tmp_path, monkeypatch):
|
|
from douyin.license import license_manager
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
license_path = tmp_path / "license.dat"
|
|
_create_test_license(
|
|
output_path=license_path,
|
|
machine_fingerprint="machine-a",
|
|
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
|
|
customer="online-check",
|
|
)
|
|
raw = json.loads(license_path.read_text(encoding="utf-8"))
|
|
raw["payload"]["card_hash"] = "a" * 64
|
|
license_path.write_text(json.dumps(raw, ensure_ascii=False), encoding="utf-8")
|
|
calls = {}
|
|
|
|
def fake_post(url, payload, timeout_seconds=10):
|
|
calls["url"] = url
|
|
calls["payload"] = payload
|
|
calls["timeout_seconds"] = timeout_seconds
|
|
return {"ok": True, "reason": "ok", "message": "授权有效"}
|
|
|
|
status = license_manager.verify_license_online(
|
|
license_path,
|
|
verify_url="https://license.example.test/api/v1/verify_license",
|
|
http_post=fake_post,
|
|
)
|
|
|
|
assert status.ok is False
|
|
assert status.reason == "invalid_signature"
|
|
|
|
|
|
def test_online_verify_rejects_server_revocation(tmp_path, monkeypatch):
|
|
from douyin.license import license_manager
|
|
from license_server.core import LicenseService
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
|
|
card = service.create_cards(days=3, count=1)[0]
|
|
activated = service.activate(card.code, machine_fingerprint="machine-a")
|
|
assert activated.license_doc is not None
|
|
license_path = tmp_path / "license.dat"
|
|
license_path.write_text(json.dumps(activated.license_doc, ensure_ascii=False), encoding="utf-8")
|
|
|
|
def fake_post(url, payload, timeout_seconds=10):
|
|
result = service.verify_license_doc(payload["license"])
|
|
return {"ok": result.ok, "reason": result.reason, "message": result.message}
|
|
|
|
assert license_manager.verify_license_online(license_path, http_post=fake_post).ok is True
|
|
service.revoke(card.code)
|
|
status = license_manager.verify_license_online(license_path, http_post=fake_post)
|
|
|
|
assert status.ok is False
|
|
assert status.reason == "revoked"
|
|
|
|
|
|
def test_online_verify_uses_verify_url_from_activation_url(monkeypatch):
|
|
from douyin.license import license_manager
|
|
|
|
monkeypatch.delenv("DOUYIN_LICENSE_VERIFY_URL", raising=False)
|
|
monkeypatch.setattr(
|
|
license_manager,
|
|
"get_default_activation_url",
|
|
lambda: "https://license.example.test/api/v1/activate",
|
|
)
|
|
|
|
assert license_manager.get_default_verify_url() == "https://license.example.test/api/v1/verify_license"
|
|
|
|
|
|
def test_license_status_public_payload_hides_signature(tmp_path, monkeypatch):
|
|
from douyin.license import license_manager
|
|
|
|
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
|
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
|
|
license_path = tmp_path / "license.dat"
|
|
_create_test_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"]
|