"""The challenge relay gives the server's licence callback the PSSH of the header the client licenses.""" from __future__ import annotations import json from collections.abc import Callable from types import SimpleNamespace from typing import Any, cast from uuid import UUID import pytest from tests.core.key_resolver import resolver from tests.core.test_playready_multi_header import _drm, _wrm_header from tests.remote.key_source import remote_session from tests.remote.sessions import session_entry from unshackle.core.api import handlers, server_keys from unshackle.core.drm import PlayReady from unshackle.core.manifests.hls import HLS from unshackle.core.remote_protocol import LicensingMode, Licensor pytestmark = pytest.mark.unit K1, K2 = UUID(int=0x11), UUID(int=0x22) def pr_kids(pssh_b64: str | None) -> list[UUID] | None: return PlayReady.from_pssh_b64(pssh_b64).kids if pssh_b64 else None class FakePlayReadyCdm: """A CDM that licenses every KID of the header it makes the challenge for.""" def open(self) -> bytes: return b"s" def close(self, sid: bytes) -> None: pass def get_license_challenge(self, sid: bytes, header: Any) -> str: self.kids = PlayReady.header_kids(header) return "" def parse_license(self, sid: bytes, licence: str) -> None: pass def get_keys(self, sid: bytes) -> list[Any]: return [SimpleNamespace(key_id=kid, key=bytes(16)) for kid in self.kids] class PsshService: """EXAMPLE: licence callbacks that declare the PSSH argument of the local path.""" def __init__(self) -> None: self.calls: list[dict[str, Any]] = [] def fairplay_bridge(self) -> None: return None def get_playready_license(self, *, challenge: str, title: Any, track: Any, pssh_b64: str | None = None) -> str: self.calls.append({"challenge": challenge, "pssh_b64": pssh_b64}) return "x" def get_widevine_license(self, *, challenge: bytes, title: Any, track: Any, pssh: Any = None) -> bytes: self.calls.append({"challenge": challenge, "pssh": pssh}) return b"LIC" def relay( monkeypatch: pytest.MonkeyPatch, drm: Callable[[], list[Any]], drm_system: str = "playready" ) -> tuple[PsshService, list[Any], Any, Callable[..., Any]]: """The server service, the wire PSSH list, the client track and the licence callable dl hands its DRM. The client is a RemoteService whose HTTP hop calls the real server licence handler, and each track gets ``drm()``. """ service = PsshService() session = session_entry( service_tag="EXAMPLE", service_instance=service, tracks={"vid": SimpleNamespace(id="vid", drm=drm())}, licensing=LicensingMode(Licensor.NO_DEVICE), ) monkeypatch.setattr(handlers, "ensure_track_drm", lambda *a, **k: None) monkeypatch.setattr(server_keys, "find_title_for_track", lambda tid, s: SimpleNamespace()) wire: list[Any] = [] def post(route: str, body: Any, **_: Any) -> Any: wire.append(body.get("pssh")) return json.loads(cast(bytes, handlers.license_response(body, session, "sess", None).body)) remote = remote_session(None, {}) remote.client = cast(Any, SimpleNamespace(post=post)) client_track = SimpleNamespace(id="vid", drm=drm()) keys = resolver() keys.service = remote def licence(**kw: Any) -> Any: return keys._service_licence(drm_system, title=SimpleNamespace(), track=client_track, **kw) return service, wire, client_track, licence def license_all(drms: list[Any], licence: Callable[..., Any]) -> None: for drm in drms: drm.get_content_keys(FakePlayReadyCdm(), certificate=None, licence=licence) @pytest.mark.parametrize( "kids, drm", [ ([K1], lambda: [HLS.bridge_stub("playready", [K1])]), ([K1, K2], lambda: [HLS.bridge_stub("playready", [K1, K2])]), ([K1, K2], lambda: [HLS.bridge_stub("playready", [K1]), HLS.bridge_stub("playready", [K2])]), ], ids=["one-header", "two-headers-one-object", "two-headers-two-objects"], ) def test_playready_relay_gives_each_challenge_the_pssh_of_its_header( monkeypatch: pytest.MonkeyPatch, kids: list[UUID], drm: Callable[[], list[Any]] ) -> None: service, wire, client_track, licence = relay(monkeypatch, drm) license_all(client_track.drm, licence) assert [pr_kids(c["pssh_b64"]) for c in service.calls] == [[k] for k in kids] assert [pr_kids(p) for p in wire] == [[k] for k in kids] def test_widevine_relay_gives_the_callback_the_pssh(monkeypatch: pytest.MonkeyPatch) -> None: service, wire, client_track, licence = relay(monkeypatch, lambda: [HLS.bridge_stub("widevine", [K1])], "widevine") pssh = client_track.drm[0].pssh licence(challenge=b"CHAL", pssh=pssh, session_id=b"s") assert service.calls[0]["challenge"] == b"CHAL" assert service.calls[0]["pssh"].dumps() == pssh.dumps() == wire[0] def test_playready_relay_licenses_a_key_the_server_track_does_not_name(monkeypatch: pytest.MonkeyPatch) -> None: service, _, _, licence = relay(monkeypatch, lambda: [HLS.bridge_stub("playready", [K1])]) license_all([HLS.bridge_stub("playready", [K2])], licence) assert [c["pssh_b64"] for c in service.calls] == [None] def test_playready_relay_licenses_a_header_without_a_kid(monkeypatch: pytest.MonkeyPatch) -> None: service, _, client_track, licence = relay(monkeypatch, lambda: [_drm(_wrm_header(K1), _wrm_header())]) license_all(client_track.drm, licence) assert [pr_kids(c["pssh_b64"]) for c in service.calls] == [[K1], None]