refactor(remote): hold who licenses a remote session as one licensing mode

This commit is contained in:
imSp4rky
2026-11-29 10:16:08 -07:00
parent d4af4c77f4
commit efd8ca65c7
18 changed files with 458 additions and 160 deletions
+35 -32
View File
@@ -22,7 +22,7 @@ from unshackle.core.api.errors import APIError
from unshackle.core.api.key_policy import KeyPolicy
from unshackle.core.config import cdm_entry_names_device
from unshackle.core.drm import Widevine
from unshackle.core.remote_protocol import APIErrorCode
from unshackle.core.remote_protocol import APIErrorCode, LicensingMode, Licensor
from unshackle.core.remote_service import RemoteClient, RemoteService
from unshackle.core.tracks import Audio, Video
@@ -111,10 +111,9 @@ def test_above_the_cap_without_a_device_is_refused_at_create():
@pytest.mark.parametrize(
("session", "track", "refused"),
[
(session_entry(server_cdm_max_height=1080), video(2160), True),
(session_entry(server_cdm_max_height=1080), video(1080), False),
(session_entry(server_cdm_max_height=None), video(2160), False),
(session_entry(server_cdm=False, server_cdm_max_height=1080), video(720), True),
(session_entry(licensing=LicensingMode(Licensor.SERVER_CDM, 1080)), video(2160), True),
(session_entry(licensing=LicensingMode(Licensor.SERVER_CDM, 1080)), video(1080), False),
(session_entry(licensing=LicensingMode(Licensor.CLIENT_DEVICE, 1080)), video(720), True),
(session_entry(), video(2160), False),
],
)
@@ -147,13 +146,17 @@ def single(refusal):
def test_a_vault_hit_is_served_above_the_cap(widevine_env, monkeypatch):
monkeypatch.setattr(handlers, "check_vaults", lambda kids, name, **_: ({"11" * 16: "key"}, {"11" * 16: "sqlite"}))
refusal = handlers.live_licence_refusal(session_entry(server_cdm_max_height=1080), video(2160))
refusal = handlers.live_licence_refusal(
session_entry(licensing=LicensingMode(Licensor.SERVER_CDM, 1080)), video(2160)
)
assert single(refusal) == {"11" * 16: "key"}
def test_a_vault_miss_raises_the_refusal_before_the_device_loads(widevine_env, monkeypatch):
monkeypatch.setattr(handlers, "check_vaults", lambda kids, name, **_: None)
refusal = handlers.live_licence_refusal(session_entry(server_cdm_max_height=1080), video(2160))
refusal = handlers.live_licence_refusal(
session_entry(licensing=LicensingMode(Licensor.SERVER_CDM, 1080)), video(2160)
)
with pytest.raises(APIError) as ei:
single(refusal)
assert ei.value is refusal
@@ -224,8 +227,7 @@ def test_a_server_vault_miss_without_the_local_drm_system_fails_at_once():
"""Only a server-CDM session reads the local vaults before the licence, so a server-vault session raises at once."""
track = capped_track()
svc = remote_session(PR_DEVICE, {"keys": {}, "capped_tracks": {"vid": {}}})
svc._server_cdm = False
svc._server_vault = True
svc.licensing = LicensingMode(Licensor.CLIENT_DEVICE, server_vault=True)
with pytest.raises(click.ClickException, match="server vault") as ei:
svc.resolve_server_keys(one_title(track))
assert "-q" not in ei.value.message
@@ -294,7 +296,9 @@ def playready_env(monkeypatch):
def test_playready_raises_the_refusal_before_the_device_loads(playready_env):
refusal = handlers.live_licence_refusal(session_entry(server_cdm_max_height=1080), video(2160))
refusal = handlers.live_licence_refusal(
session_entry(licensing=LicensingMode(Licensor.SERVER_CDM, 1080)), video(2160)
)
with pytest.raises(APIError) as ei:
handlers.handle_single_server_cdm(
SimpleNamespace(), SimpleNamespace(), video(2160), "cHNzaA==", "playready", None, {}, refusal
@@ -329,7 +333,7 @@ def media(kind, tid, drm, **attrs):
@pytest.fixture
def licence_env(monkeypatch):
"""A capped remote session and a stand-in server licence that honours the refusal like the real one."""
session = session_entry(service_tag="EXAMPLE1", server_cdm_max_height=1080)
session = session_entry(service_tag="EXAMPLE1", licensing=LicensingMode(Licensor.SERVER_CDM, 1080))
calls: list = []
vault = {}
@@ -452,7 +456,7 @@ def test_the_tracks_response_echoes_the_session_choice(monkeypatch):
service_tag="EXAMPLE1", service_instance=StubService(), session_id="capped", owner_key=None
)
entry.title_map = {"t1": "Title"}
entry.server_cdm = False
entry.licensing = LicensingMode(Licensor.CLIENT_DEVICE)
try:
resp = await handlers.session_tracks_handler({"title_id": "t1"}, "capped")
return json.loads(resp.body)
@@ -600,7 +604,8 @@ def sent_create(server_cdm, cdm_name=None, local_cdm=WIDEVINE_CDM, cdm_entry=Fal
handlers.config.cdm = cdm_map or {}
svc = remote_session(local_cdm, {})
svc.ctx.obj.cdm_entry = cdm_entry
svc._server_cdm = server_cdm
svc.licensing = None
svc._server_cdm_request = server_cdm
svc.service_tag = "EXAMPLE1"
svc.title_id = "t1"
svc._server_accounts = {"global": True}
@@ -628,17 +633,17 @@ def test_the_client_declines_the_server_cdm_when_the_user_picked_a_device(server
assert (data.get("server_cdm") is False) is declines
assert data["cdm_type"] == "widevine"
assert svc._server_cdm is (False if declines else server_cdm)
assert svc._server_cdm_request is (False if declines else server_cdm)
# the server built the service, so its answer is final
svc.adopt_session_cdm(True, None)
assert svc._server_cdm is True
svc.adopt_session_cdm(LicensingMode(Licensor.SERVER_CDM), svc._server_cdm_request)
assert svc.expected_server_cdm is True
def test_a_device_loaded_from_the_services_cdm_entry_declines_the_server_cdm():
svc, data = sent_create(None, cdm_entry=True, cdm_map={"EXAMPLE1": "my_device"})
assert data["server_cdm"] is False
assert svc._server_cdm is False
assert svc._server_cdm_request is False
def test_server_cdm_true_outranks_the_services_cdm_entry(caplog):
@@ -646,7 +651,7 @@ def test_server_cdm_true_outranks_the_services_cdm_entry(caplog):
svc, data = sent_create(True, cdm_entry=True, cdm_map={"EXAMPLE1": "my_device"})
assert "server_cdm" not in data
assert data["cdm_relay"] is True
assert svc._server_cdm is True
assert svc._server_cdm_request is True
assert not caplog.text
@@ -661,7 +666,7 @@ def test_a_device_loaded_from_the_default_cdm_entry_does_not_decline(caplog):
with caplog.at_level(logging.WARNING):
svc, data = sent_create(True, cdm_map={"default": "my_device", "EXAMPLE2": "other"})
assert "server_cdm" not in data
assert svc._server_cdm is True
assert svc._server_cdm_request is True
assert not caplog.text
@@ -670,7 +675,7 @@ def test_a_cdm_entry_that_did_not_load_the_device_warns_and_does_not_decline(loc
with caplog.at_level(logging.WARNING):
svc, data = sent_create(None, local_cdm=local_cdm, cdm_map={"example1": {">=1080": "my_device"}})
assert "server_cdm" not in data
assert svc._server_cdm is None
assert svc._server_cdm_request is None
assert "cdm entry for EXAMPLE1" in caplog.text
@@ -706,7 +711,7 @@ def test_a_client_with_no_device_does_not_decline():
def created(answer, monkeypatch):
"""Run ``authenticate()`` to its end for a device the service's cdm entry loaded."""
svc, _ = sent_create(None, cdm_entry=True, cdm_map={"EXAMPLE1": "my_device"})
svc._server_cdm = None
svc._server_cdm_request = None
svc.client = SimpleNamespace(post=lambda path, data: {"session_id": "sess", **answer})
svc.close = lambda: None
channel = SimpleNamespace(keep_alive=lambda: None, drain_logs=lambda: None, in_flight=None)
@@ -718,14 +723,14 @@ def created(answer, monkeypatch):
def test_the_client_says_when_a_cdm_entry_gave_the_licensing_to_its_device(caplog, monkeypatch):
with caplog.at_level(logging.INFO):
svc = created({"server_cdm": False}, monkeypatch)
assert svc._server_cdm is False
assert svc.expected_server_cdm is False
assert "cdm config has an entry for EXAMPLE1" in caplog.text
def test_the_client_does_not_claim_its_device_when_the_server_keeps_the_server_cdm(caplog, monkeypatch):
with caplog.at_level(logging.INFO):
svc = created({"server_cdm": True, "server_cdm_max_height": 1080}, monkeypatch)
assert svc._server_cdm is True
assert svc.expected_server_cdm is True
assert "your own device is not used" in caplog.text
assert "cdm config has an entry" not in caplog.text
@@ -752,17 +757,15 @@ def test_dl_records_whether_the_services_cdm_entry_named_the_loaded_device(cdm,
def test_a_declined_server_cdm_that_the_server_keeps_is_reported(max_height, caplog):
svc = remote_session(SimpleNamespace(is_playready=False), {})
svc.service_tag = "EXAMPLE1"
svc._server_cdm = False
with caplog.at_level(logging.INFO):
svc.adopt_session_cdm(True, max_height)
assert svc._server_cdm is True
svc.adopt_session_cdm(LicensingMode(Licensor.SERVER_CDM, max_height), False)
assert svc.expected_server_cdm is True
assert "your own device is not used" in caplog.text
assert "pass a higher -q" not in caplog.text
caplog.clear()
svc._server_cdm = True
with caplog.at_level(logging.WARNING):
svc.adopt_session_cdm(True, max_height)
svc.adopt_session_cdm(LicensingMode(Licensor.SERVER_CDM, max_height), True)
assert "your own device is not used" not in caplog.text
@@ -772,8 +775,8 @@ def test_a_refused_server_cdm_warns_only_when_no_cdm_entry_loaded_the_fallback(c
svc.service_tag = "EXAMPLE1"
svc.ctx.obj.cdm_entry = cdm_entry
with caplog.at_level(logging.INFO):
svc.adopt_session_cdm(False, None)
assert svc._server_cdm is False
svc.adopt_session_cdm(LicensingMode(Licensor.CLIENT_DEVICE), True)
assert svc.expected_server_cdm is False
[record] = [r for r in caplog.records if "falling back to the local CDM" in r.getMessage()]
assert record.levelno == level
@@ -804,7 +807,7 @@ def test_a_per_service_server_cdm_overrides_the_server_entry(
)
finally:
RemoteService.__name__ = name # the constructor renames the class
assert svc._server_cdm is expected
assert svc._server_cdm_request is expected
assert handlers.config.services.get("EXAMPLE1") == ({"title_map": {"a": "b"}} if apply_overrides else None)
assert live["EXAMPLE1"] == {**service_entry, "title_map": {"a": "b"}}
@@ -822,7 +825,7 @@ def test_a_per_service_entry_matches_the_tag_in_any_case(config_key, tag, monkey
svc = remote_mod.make_remote_service(SimpleNamespace(obj=None), None, tag, "t1", {})
finally:
RemoteService.__name__ = name # the constructor renames the class
assert svc._server_cdm is False
assert svc._server_cdm_request is False
assert svc._anonymous_search is True
assert handlers.config.services == {tag: {"title_map": {"a": "b"}}}