mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-16 14:05:10 +00:00
fix(remote): let the server's CDM mapping decide the DRM system under server_cdm
A client with a local PlayReady device asked the server for a PlayReady licence on a service the server maps to a Widevine device. Both server_cdm routes now share one candidate list from one config lookup, report the system that licensed in drm_type, and the client adopts it.
This commit is contained in:
@@ -62,6 +62,7 @@ async def _run_batch(monkeypatch, tracks, cdm_type, calls):
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(handlers, "detect_cdm_type_for_service", lambda tag, cfg: cdm_type)
|
||||
monkeypatch.setattr(handlers, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(handlers, "handle_single_server_cdm", fake_single)
|
||||
monkeypatch.setattr(handlers.config, "serve", {"users": {}}, raising=False)
|
||||
resp = await handlers.session_license_handler(
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
"""The client's single-track server_cdm licence follows the server's DRM system.
|
||||
|
||||
The server's config.cdm mapping decides the system, so the client sends the header of the
|
||||
system the server reported at session create, not the one its local device asked for, and
|
||||
takes the system the server answered with for the next call.
|
||||
"""
|
||||
|
||||
import logging
|
||||
from types import SimpleNamespace
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.remote_service import RemoteService
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
KID = UUID("161219ec3df64e0eadbb3827c0ccc46d")
|
||||
|
||||
|
||||
class _PlayReady:
|
||||
def __init__(self):
|
||||
self.data = {"pssh_b64": "PR"}
|
||||
self.content_keys: dict = {}
|
||||
|
||||
|
||||
class _Widevine:
|
||||
def __init__(self):
|
||||
self.pssh = SimpleNamespace(dumps=lambda: "WV")
|
||||
self.content_keys: dict = {}
|
||||
|
||||
|
||||
# track_pssh matches on the DRM class name, so name the fakes after the DRM classes
|
||||
_PlayReady.__name__ = "PlayReady"
|
||||
_Widevine.__name__ = "Widevine"
|
||||
|
||||
|
||||
class _Client:
|
||||
def __init__(self, answer):
|
||||
self.answer = answer
|
||||
self.posts: list = []
|
||||
|
||||
def post(self, endpoint, payload):
|
||||
self.posts.append(payload)
|
||||
return self.answer
|
||||
|
||||
|
||||
def _service(server_type, answer):
|
||||
svc = RemoteService.__new__(RemoteService)
|
||||
svc._server_cdm = True
|
||||
svc._server_cdm_type = server_type
|
||||
svc._session_id = "sess"
|
||||
svc.server_vault_keys = {}
|
||||
svc.log = logging.getLogger("test-remote-license")
|
||||
svc.client = _Client(answer)
|
||||
return svc
|
||||
|
||||
|
||||
def test_server_type_picks_the_header_and_the_posted_type():
|
||||
svc = _service("widevine", {"keys": {KID.hex: "k"}, "drm_type": "widevine"})
|
||||
track = SimpleNamespace(id="vid", drm=[_PlayReady(), _Widevine()])
|
||||
|
||||
svc.proxy_license(b"", track, "playready")
|
||||
|
||||
assert svc.client.posts == [{"track_id": "vid", "drm_type": "widevine", "mode": "server_cdm", "pssh": "WV"}]
|
||||
assert all(drm.content_keys == {KID: "k"} for drm in track.drm)
|
||||
|
||||
|
||||
def test_missing_header_of_the_server_type_sends_the_other_one():
|
||||
"""A PlayReady-only track under a Widevine server: the header goes with its own system name."""
|
||||
svc = _service("widevine", {"keys": {KID.hex: "k"}, "drm_type": "widevine"})
|
||||
track = SimpleNamespace(id="vid", drm=[_PlayReady()])
|
||||
|
||||
svc.proxy_license(b"", track, "widevine")
|
||||
|
||||
assert svc.client.posts[0]["drm_type"] == "playready"
|
||||
assert svc.client.posts[0]["pssh"] == "PR"
|
||||
|
||||
|
||||
def test_response_type_refreshes_the_server_type():
|
||||
svc = _service("widevine", {"keys": {KID.hex: "k"}, "drm_type": "playready"})
|
||||
track = SimpleNamespace(id="vid", drm=[_PlayReady(), _Widevine()])
|
||||
|
||||
svc.proxy_license(b"", track, "widevine")
|
||||
|
||||
assert svc._server_cdm_type == "playready"
|
||||
|
||||
|
||||
def test_proxy_mode_keeps_the_callers_type():
|
||||
svc = _service("widevine", {"license": "bGlj"})
|
||||
svc._server_cdm = False
|
||||
svc.drain_server_logs = lambda: None
|
||||
track = SimpleNamespace(id="vid", drm=[_PlayReady(), _Widevine()])
|
||||
|
||||
licence = svc.proxy_license(b"chal", track, "playready")
|
||||
|
||||
assert licence == b"lic"
|
||||
assert svc.client.posts[0]["drm_type"] == "playready"
|
||||
assert svc.client.posts[0]["pssh"] == "PR"
|
||||
@@ -117,6 +117,7 @@ async def test_batch_reports_vault_keys_from_the_init_segment_pssh(monkeypatch):
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(handlers, "detect_cdm_type_for_service", lambda tag, cfg: "widevine")
|
||||
monkeypatch.setattr(handlers, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(handlers, "fetch_init_segment", lambda track, sess: b"init")
|
||||
monkeypatch.setattr(
|
||||
handlers, "drm_from_init_segment", lambda track, session=None, init_data=None: [_WV("init", [CDM_KID])]
|
||||
|
||||
@@ -140,6 +140,7 @@ async def test_batch_shares_full_bundle_per_pssh(monkeypatch):
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(handlers, "detect_cdm_type_for_service", lambda tag, cfg: "playready")
|
||||
monkeypatch.setattr(handlers, "resolve_device_name", lambda *a, **k: "dev")
|
||||
monkeypatch.setattr(handlers, "drm_preference_name", lambda track: None)
|
||||
monkeypatch.setattr(handlers, "extract_pssh_from_track", lambda track, drm: track_pssh[track.id])
|
||||
monkeypatch.setattr(handlers, "handle_single_server_cdm", fake_single)
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
"""The single-track server_cdm route licenses with the server's configured DRM system.
|
||||
|
||||
A client that holds a local PlayReady device asks for PlayReady, but a server whose
|
||||
config.cdm maps the service to a Widevine device has no PlayReady device to answer with.
|
||||
The batch route already ignores the client's choice; this route must match it, down to
|
||||
licensing a PlayReady-only track with the Widevine device. With no server mapping, the
|
||||
client's choice decides, and the type the routes plan with must be the type of the device
|
||||
they load.
|
||||
"""
|
||||
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
from uuid import UUID
|
||||
|
||||
import pyplayready.system.pssh as pr_pssh_mod
|
||||
import pytest
|
||||
import pywidevine.pssh as wv_pssh_mod
|
||||
|
||||
import unshackle.core.cdm as cdm_mod
|
||||
import unshackle.core.cdm.detect as detect_mod
|
||||
import unshackle.core.drm as drm_mod
|
||||
from unshackle.core.api import handlers
|
||||
from unshackle.core.api.errors import APIError
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
KID = UUID("161219ec3df64e0eadbb3827c0ccc46d")
|
||||
WV_PSSH = "AAAAOHBzc2gAAAAA7e+LqXnWSs6jyCfc1R0h7QAAABgSEBYSGew99k4OrbtjJ8DMxG1I49yVmwY="
|
||||
PR_PSSH = "UFI="
|
||||
|
||||
|
||||
class _FakeWidevine:
|
||||
def __init__(self, pssh=None, **kwargs):
|
||||
self.kids = [KID]
|
||||
self.content_keys: dict = {}
|
||||
self.pssh = pssh
|
||||
|
||||
def get_content_keys(self, cdm, certificate, licence):
|
||||
self.content_keys = {KID: "key_wv"}
|
||||
|
||||
|
||||
class _FakePlayReady:
|
||||
def __init__(self, pssh=None, pssh_b64=None, kid=None, **kwargs):
|
||||
self.kids = [KID]
|
||||
self.content_keys: dict = {}
|
||||
self.pssh_b64 = pssh_b64
|
||||
self.data = {"pssh_b64": pssh_b64} if pssh_b64 else {}
|
||||
|
||||
def absorb(self, *others):
|
||||
pass
|
||||
|
||||
def get_content_keys(self, cdm, certificate, licence):
|
||||
self.content_keys = {KID: "key_pr"}
|
||||
|
||||
|
||||
# extract_pssh_from_track matches on class name, so name the fakes after the DRM classes
|
||||
_FakePlayReady.__name__ = "PlayReady"
|
||||
_FakeWidevine.__name__ = "Widevine"
|
||||
|
||||
|
||||
def _pr_drm():
|
||||
return _FakePlayReady(pssh_b64=PR_PSSH)
|
||||
|
||||
|
||||
def _wv_drm():
|
||||
return _FakeWidevine(pssh=SimpleNamespace(dumps=lambda: WV_PSSH))
|
||||
|
||||
|
||||
def _session(track):
|
||||
# handle_single_server_cdm looks config.cdm up by the service class name, so the fake needs the tag as its name
|
||||
service = type("PCOK", (SimpleNamespace,), {})(
|
||||
get_widevine_service_certificate=lambda **k: None,
|
||||
get_widevine_license=lambda **k: b"",
|
||||
get_playready_license=lambda **k: b"",
|
||||
)
|
||||
return SimpleNamespace(
|
||||
service_tag="PCOK",
|
||||
service_instance=service,
|
||||
served_keys={},
|
||||
tracks=SimpleNamespace(get={"vid": track}.get),
|
||||
log_buffer=None,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def env(monkeypatch):
|
||||
"""Fake DRM classes and PSSH parsers; the CDM is a namespace that names its device."""
|
||||
monkeypatch.setattr(drm_mod, "Widevine", _FakeWidevine)
|
||||
monkeypatch.setattr(drm_mod, "PlayReady", _FakePlayReady)
|
||||
monkeypatch.setattr(pr_pssh_mod, "PSSH", lambda data: SimpleNamespace(data=data))
|
||||
monkeypatch.setattr(wv_pssh_mod, "PSSH", lambda data: SimpleNamespace(data=data))
|
||||
monkeypatch.setitem(cdm_mod.__dict__, "load_cdm", lambda name, **k: SimpleNamespace(device=name))
|
||||
monkeypatch.setattr(detect_mod, "is_widevine_cdm", lambda cdm: cdm.device.startswith("wv"))
|
||||
monkeypatch.setattr(detect_mod, "is_playready_cdm", lambda cdm: cdm.device.startswith("pr"))
|
||||
monkeypatch.setattr(handlers, "ensure_track_drm", lambda track, session=None, init_data=None: None)
|
||||
monkeypatch.setattr(handlers, "check_vaults", lambda kids, name: None)
|
||||
monkeypatch.setattr(handlers, "cache_to_vaults", lambda keys, name: None)
|
||||
monkeypatch.setattr(handlers, "server_cdm_allowed", lambda request, tag: True)
|
||||
monkeypatch.setattr(handlers, "find_title_for_track", lambda tid, session: SimpleNamespace())
|
||||
monkeypatch.setattr(handlers.config, "serve", {"users": {}}, raising=False)
|
||||
# the device name says what the device is, in place of the .wvd / .prd file on disk
|
||||
monkeypatch.setattr(
|
||||
handlers, "detect_cdm_type", lambda name, cfg: {"wv": "widevine", "pr": "playready"}.get(name[:2])
|
||||
)
|
||||
|
||||
|
||||
async def _license(monkeypatch, track, body):
|
||||
session = _session(track)
|
||||
|
||||
async def fake_get_session(sid, req):
|
||||
return session
|
||||
|
||||
monkeypatch.setattr(handlers, "get_validated_session", fake_get_session)
|
||||
resp = await handlers.session_license_handler({"track_id": "vid", "mode": "server_cdm", **body}, "sess", None)
|
||||
return json.loads(resp.body)
|
||||
|
||||
|
||||
async def test_server_type_wins_over_client_request(env, monkeypatch):
|
||||
monkeypatch.setattr(handlers, "detect_cdm_type_for_service", lambda tag, cfg: "widevine")
|
||||
monkeypatch.setattr(handlers, "resolve_device_name", lambda *a, **k: "wv_dev")
|
||||
monkeypatch.setattr(handlers, "require_track_pssh", lambda track, pssh, drm_type: None)
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_pr_drm(), _wv_drm()])
|
||||
body = await _license(monkeypatch, track, {"drm_type": "playready", "pssh": PR_PSSH})
|
||||
|
||||
assert body["drm_type"] == "widevine"
|
||||
assert body["keys"] == {KID.hex: "key_wv"}
|
||||
assert not hasattr(track, "pr_pssh"), "the discarded client PSSH must not persist on the track"
|
||||
|
||||
|
||||
async def test_playready_only_track_licenses_with_widevine_server(env, monkeypatch):
|
||||
"""The batch route licenses a PlayReady-only header with the Widevine device; so must this one."""
|
||||
monkeypatch.setattr(handlers, "detect_cdm_type_for_service", lambda tag, cfg: "widevine")
|
||||
monkeypatch.setattr(handlers, "resolve_device_name", lambda *a, **k: "wv_dev")
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_pr_drm()])
|
||||
body = await _license(monkeypatch, track, {"drm_type": "playready"})
|
||||
|
||||
assert body["drm_type"] == "widevine"
|
||||
assert body["keys"] == {KID.hex: "key_wv"}
|
||||
|
||||
|
||||
async def test_client_type_decides_without_server_mapping(env, monkeypatch):
|
||||
"""No config.cdm entry for the service: the tier's devices answer in the client's order."""
|
||||
monkeypatch.setattr(handlers.config, "cdm", {}, raising=False)
|
||||
monkeypatch.setattr(
|
||||
handlers, "serve_user_config", lambda key: {"devices": ["wv_dev"], "playready_devices": ["pr_dev"]}
|
||||
)
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_pr_drm(), _wv_drm()])
|
||||
body = await _license(monkeypatch, track, {"drm_type": "playready"})
|
||||
assert body["drm_type"] == "playready"
|
||||
assert body["keys"] == {KID.hex: "key_pr"}
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_pr_drm(), _wv_drm()])
|
||||
body = await _license(monkeypatch, track, {"drm_type": "widevine"})
|
||||
assert body["drm_type"] == "widevine"
|
||||
assert body["keys"] == {KID.hex: "key_wv"}
|
||||
|
||||
|
||||
async def test_quality_keyed_mapping_resolves_to_the_device_it_loads(env, monkeypatch):
|
||||
"""A quality-tier mapping falls through to cdm.default on both the type probe and the device lookup.
|
||||
|
||||
The type probe used to take the first tier's device (Widevine) while the device lookup
|
||||
returned the global default (PlayReady), so the route loaded a PlayReady device for a
|
||||
Widevine licence and failed with "is not a Widevine device".
|
||||
"""
|
||||
monkeypatch.setattr(
|
||||
handlers.config,
|
||||
"cdm",
|
||||
{"PCOK": {"<=1080": "wv_tier", ">1080": {"widevine": "wv_tier", "playready": "pr_tier"}}, "default": "pr_dev"},
|
||||
raising=False,
|
||||
)
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_pr_drm(), _wv_drm()])
|
||||
body = await _license(monkeypatch, track, {"drm_type": "widevine"})
|
||||
|
||||
assert body["drm_type"] == "playready"
|
||||
assert body["keys"] == {KID.hex: "key_pr"}
|
||||
|
||||
|
||||
async def test_both_systems_mapped_follow_the_client(env, monkeypatch):
|
||||
"""A mapping with a device per system settles nothing, so the client's choice goes first."""
|
||||
monkeypatch.setattr(handlers.config, "cdm", {"PCOK": {"widevine": "wv_dev", "playready": "pr_dev"}}, raising=False)
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_pr_drm(), _wv_drm()])
|
||||
body = await _license(monkeypatch, track, {"drm_type": "widevine"})
|
||||
|
||||
assert body["drm_type"] == "widevine"
|
||||
|
||||
|
||||
async def test_no_candidate_header_is_a_client_error(env, monkeypatch):
|
||||
monkeypatch.setattr(handlers.config, "cdm", {"PCOK": "pr_dev"}, raising=False)
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_wv_drm()])
|
||||
with pytest.raises(APIError, match="No PSSH on track"):
|
||||
await _license(monkeypatch, track, {"drm_type": "widevine"})
|
||||
|
||||
|
||||
async def test_track_preference_goes_first_when_the_server_has_that_device(env, monkeypatch):
|
||||
"""A track that prefers PlayReady licenses with PlayReady even on a Widevine-mapped server."""
|
||||
monkeypatch.setattr(handlers.config, "cdm", {"PCOK": {"widevine": "wv_dev", "default": "pr_dev"}}, raising=False)
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_pr_drm(), _wv_drm()], drm_preference="pr")
|
||||
body = await _license(monkeypatch, track, {"drm_type": "widevine"})
|
||||
|
||||
assert body["drm_type"] == "playready"
|
||||
assert body["keys"] == {KID.hex: "key_pr"}
|
||||
|
||||
|
||||
async def test_track_preference_falls_back_with_a_warning(env, monkeypatch, caplog):
|
||||
"""With no PlayReady device the preference cannot be met: Widevine licenses and the client hears why."""
|
||||
monkeypatch.setattr(handlers.config, "cdm", {"PCOK": "wv_dev"}, raising=False)
|
||||
|
||||
track = SimpleNamespace(id="vid", drm=[_pr_drm(), _wv_drm()], drm_preference="pr")
|
||||
with caplog.at_level("WARNING", logger=handlers.log.name):
|
||||
body = await _license(monkeypatch, track, {"drm_type": "playready"})
|
||||
|
||||
assert body["drm_type"] == "widevine"
|
||||
assert "wants playready DRM but the server has no device for it" in caplog.text
|
||||
Reference in New Issue
Block a user