mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
179 lines
7.1 KiB
Python
179 lines
7.1 KiB
Python
"""The services.<TAG>.preferences block: a remote client sends its own entries in every licensing mode."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from types import SimpleNamespace
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from tests.remote.key_source import remote_session
|
|
from tests.remote.sessions import launch_session
|
|
from unshackle.core.api import handlers, key_policy
|
|
from unshackle.core.api.errors import APIError
|
|
from unshackle.core.remote_protocol import Licensor
|
|
from unshackle.core.remote_service import RemoteService
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
SERVER = {"preferences": {"language": "en-US", "profile": 0}, "esn": "SERVER", "other": 1}
|
|
CLIENT = {"language": "ja-JP", "remove_dub_cards": True, "ratio": 1.5}
|
|
MERGED = {"language": "ja-JP", "profile": 0, "remove_dub_cards": True, "ratio": 1.5}
|
|
WIDEVINE_CDM = SimpleNamespace(is_playready=False)
|
|
|
|
|
|
class Plain:
|
|
"""A service that declares nothing about its config."""
|
|
|
|
|
|
class Identity:
|
|
CLIENT_CONFIG = ("esn",)
|
|
|
|
|
|
def launched(monkeypatch: pytest.MonkeyPatch, licensor: Licensor, supplied: Any, service: type = Plain) -> dict:
|
|
"""The config and launch keys that the server gives a remote session of ``service``."""
|
|
built: dict = {}
|
|
monkeypatch.setattr(
|
|
key_policy, "load_service_config", lambda tag: {**SERVER, "preferences": {**SERVER["preferences"]}}
|
|
)
|
|
monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: "server-device")
|
|
monkeypatch.setattr(
|
|
handlers, "served_context", lambda obj, plan, keys: built.update(cdm=obj.cdm, config=obj.config, keys=keys)
|
|
)
|
|
monkeypatch.setattr(handlers.Services, "load", lambda tag: service)
|
|
monkeypatch.setattr(handlers, "instantiate_service", lambda *a, **k: SimpleNamespace())
|
|
data = {"cdm_type": "widevine", "cdm_relay": True, "service_config": supplied}
|
|
launch_session("EXAMPLE1", "t", data, licensor=licensor)
|
|
return built
|
|
|
|
|
|
@pytest.mark.parametrize("service", [Plain, Identity])
|
|
def test_a_server_cdm_session_takes_the_client_preferences(
|
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, service: type
|
|
) -> None:
|
|
built = launched(monkeypatch, Licensor.SERVER_CDM, {"preferences": CLIENT, "esn": "CLIENT"}, service)
|
|
|
|
assert built["config"]["preferences"] == MERGED
|
|
assert built["config"]["esn"] == "SERVER"
|
|
assert built["keys"]["client_identity"] is False
|
|
assert "Ignoring service_config.preferences" not in caplog.text
|
|
|
|
|
|
def test_a_client_device_session_takes_the_identity_and_the_preferences(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
built = launched(monkeypatch, Licensor.CLIENT_DEVICE, {"preferences": CLIENT, "esn": "CLIENT"}, Identity)
|
|
|
|
assert built["config"] == {"preferences": MERGED, "esn": "CLIENT", "other": 1}
|
|
assert built["keys"]["client_identity"] is True
|
|
|
|
|
|
def test_preferences_alone_are_no_identity(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
built = launched(monkeypatch, Licensor.CLIENT_DEVICE, {"preferences": {"language": "ja-JP"}}, Identity)
|
|
|
|
assert built["config"] == {**SERVER, "preferences": {"language": "ja-JP", "profile": 0}}
|
|
assert built["keys"]["client_identity"] is False
|
|
|
|
|
|
def test_a_service_that_lists_preferences_as_an_identity_still_merges_them(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
class Listed:
|
|
CLIENT_CONFIG = ("esn", "preferences")
|
|
|
|
built = launched(monkeypatch, Licensor.SERVER_CDM, {"preferences": {"language": "ja-JP"}}, Listed)
|
|
|
|
assert built["config"]["preferences"] == {"language": "ja-JP", "profile": 0}
|
|
|
|
|
|
def test_a_lent_device_keeps_the_client_preferences(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
built = launched(monkeypatch, Licensor.CLIENT_DEVICE, {"preferences": CLIENT, "esn": "CLIENT"}, Identity)
|
|
|
|
assert built["cdm"].lend_server_device() == "server-device"
|
|
assert built["config"] == {**SERVER, "preferences": MERGED}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"supplied",
|
|
[
|
|
{"preferences": "ja-JP"},
|
|
{"preferences": ["ja-JP"]},
|
|
{"preferences": {"language": None}},
|
|
{"preferences": {"language": ["ja"]}},
|
|
{"preferences": {"language": {"nested": "ja"}}},
|
|
{"preferences": {"ratio": float("inf")}},
|
|
pytest.param({"preferences": {"language": "x" * 20000}}, id="over-the-cap"),
|
|
],
|
|
)
|
|
def test_ill_formed_preferences_are_refused(monkeypatch: pytest.MonkeyPatch, supplied: Any) -> None:
|
|
with pytest.raises(APIError):
|
|
launched(monkeypatch, Licensor.SERVER_CDM, supplied)
|
|
|
|
|
|
def test_the_identity_rules_stay_as_they_are() -> None:
|
|
merged, identity = handlers.client_config_overlay({"esn": "SERVER", "Kph": "k"}, ("esn", "Kph"), {"esn": "CLIENT"})
|
|
|
|
assert (merged, identity) == ({"esn": "CLIENT"}, True)
|
|
with pytest.raises(APIError):
|
|
handlers.client_config_overlay({}, ("esn",), {"esn": True})
|
|
|
|
|
|
def test_the_client_reads_its_own_preferences(
|
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
import unshackle.core.remote_service as remote_mod
|
|
|
|
own = {
|
|
"preferences": {"language": "ja-JP", "dub": False, "skip": None, "nested": {"a": 1}, "nan": float("nan")},
|
|
"password": "secret",
|
|
}
|
|
listing = [{"tag": "EXAMPLE1", "client_config": ["preferences"]}]
|
|
monkeypatch.setattr(remote_mod, "resolve_server", lambda name: ("http://127.0.0.1:1", "", {}))
|
|
monkeypatch.setattr(remote_mod, "remote_service_list", lambda name: listing)
|
|
monkeypatch.setattr(handlers.config, "services", {"example1": own})
|
|
caplog.set_level(logging.DEBUG)
|
|
name = RemoteService.__name__
|
|
try:
|
|
svc = remote_mod.make_remote_service(SimpleNamespace(obj=None), None, "EXAMPLE1", "t1", {})
|
|
finally:
|
|
RemoteService.__name__ = name # the constructor renames the class
|
|
assert svc._client_preferences == {"language": "ja-JP", "dub": False}
|
|
assert svc._client_config == {}
|
|
assert "preferences.skip" in caplog.text
|
|
assert "preferences.nested" in caplog.text
|
|
|
|
|
|
class _SentCreate(Exception):
|
|
pass
|
|
|
|
|
|
def sent_service_config(local_cdm: Any) -> Any:
|
|
"""The service_config of the session create body of a client with ``local_cdm``, an identity and a preference."""
|
|
|
|
def post(path: str, data: dict) -> dict:
|
|
raise _SentCreate(data)
|
|
|
|
svc = remote_session(local_cdm, {})
|
|
svc.ctx.obj.cdm_entry = False
|
|
svc.licensing = None
|
|
svc._server_cdm_request = None
|
|
svc.service_tag = "EXAMPLE1"
|
|
svc.title_id = "t1"
|
|
svc._server_accounts = {"global": True}
|
|
svc._service_params = {}
|
|
svc._client_config = {"esn": "CLIENT"}
|
|
svc._client_preferences = {"language": "ja-JP"}
|
|
svc.ctx.parent = SimpleNamespace(params={"no_proxy": True})
|
|
svc.client = SimpleNamespace(post=post)
|
|
with pytest.raises(_SentCreate) as exc:
|
|
svc.authenticate()
|
|
return exc.value.args[0].get("service_config")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("local_cdm", "expected"),
|
|
[
|
|
pytest.param(None, {"preferences": {"language": "ja-JP"}}, id="no-local-cdm"),
|
|
pytest.param(WIDEVINE_CDM, {"preferences": {"language": "ja-JP"}, "esn": "CLIENT"}, id="local-cdm"),
|
|
],
|
|
)
|
|
def test_the_client_sends_its_preferences_whatever_its_cdm(local_cdm: Any, expected: dict) -> None:
|
|
assert sent_service_config(local_cdm) == expected
|