mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 15:35:06 +00:00
refactor(cdm): leave key vault reads and writes to content-key resolution in an HTTP CDM run
This commit is contained in:
@@ -9,7 +9,6 @@ key pair and a blocking request to its host.
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -32,7 +31,7 @@ def builds(monkeypatch) -> list[tuple[str, str]]:
|
||||
"""Record every ``load_cdm`` call and hand back a fresh stub for each one."""
|
||||
calls: list[tuple[str, str]] = []
|
||||
|
||||
def fake_load_cdm(cdm_name: str, *, service_name: str = "", vaults: Any | None = None) -> StubCdm:
|
||||
def fake_load_cdm(cdm_name: str, *, service_name: str = "") -> StubCdm:
|
||||
calls.append((cdm_name, service_name))
|
||||
return StubCdm(cdm_name, service_name)
|
||||
|
||||
|
||||
+101
-20
@@ -1,8 +1,10 @@
|
||||
"""The shared bookkeeping of the HTTP CDMs: sessions, vault and cached keys, and their merge with licence keys."""
|
||||
"""The shared bookkeeping of the HTTP CDMs: sessions, API cache keys and their merge with licence keys; the key vaults stay with content-key resolution."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
from collections.abc import Callable
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
@@ -10,11 +12,13 @@ import pytest
|
||||
from pywidevine.cdm import Cdm as WidevineCdm
|
||||
from pywidevine.pssh import PSSH
|
||||
|
||||
from tests.core.key_resolver import MemoryVault, vaults_of
|
||||
from tests.core.key_resolver import MemoryVault, key_table, resolver, vaults_of
|
||||
from tests.core.test_playready_multi_header import _drm, _wrm_header
|
||||
from unshackle.core.cdm.custom_remote_cdm import CustomRemoteCDM, CustomRemoteCDMExceptions
|
||||
from unshackle.core.cdm.decrypt_labs_remote_cdm import DecryptLabsRemoteCDM, DecryptLabsRemoteCDMExceptions
|
||||
from unshackle.core.cdm.http_cdm import HttpCdm, HttpCdmExceptions
|
||||
from unshackle.core.drm import Widevine
|
||||
from unshackle.core.cdm.loader import load_remote_cdm
|
||||
from unshackle.core.drm import PlayReady, Widevine
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
@@ -30,9 +34,9 @@ class StubCdm(HttpCdm):
|
||||
self,
|
||||
cached: list[dict[str, Any]] | None = None,
|
||||
licence: list[dict[str, Any]] | None = None,
|
||||
vaults: Any = None,
|
||||
device: str = "L3",
|
||||
) -> None:
|
||||
super().__init__("https://cdm.test/", "L3", None, "EXAMPLE", vaults, {})
|
||||
super().__init__("https://cdm.example/", device, None, "EXAMPLE", {})
|
||||
self.cached = cached
|
||||
self.licence = licence or []
|
||||
self.challenges = 0
|
||||
@@ -61,6 +65,14 @@ def key(kid: UUID, value: str) -> dict[str, str]:
|
||||
return {"kid": kid.hex, "key": value, "type": "CONTENT"}
|
||||
|
||||
|
||||
def widevine(*kids: UUID) -> Widevine:
|
||||
return Widevine(pssh=PSSH.new(system_id=PSSH.SystemId.Widevine, key_ids=list(kids)), kid=kids[-1])
|
||||
|
||||
|
||||
def playready(*kids: UUID) -> PlayReady:
|
||||
return _drm(_wrm_header(*kids))
|
||||
|
||||
|
||||
def licensed(cdm: StubCdm, *kids: UUID) -> dict[UUID, str]:
|
||||
"""Run one challenge and licence round and return the content keys of the session."""
|
||||
session_id = cdm.open()
|
||||
@@ -70,19 +82,6 @@ def licensed(cdm: StubCdm, *kids: UUID) -> dict[UUID, str]:
|
||||
return {k.kid: k.key.hex() for k in cdm.get_keys(session_id, "CONTENT")}
|
||||
|
||||
|
||||
def test_vaults_holding_every_kid_skip_the_api() -> None:
|
||||
cdm = StubCdm(vaults=vaults_of(MemoryVault({KID_A: KEY_A, KID_B: KEY_B})))
|
||||
assert licensed(cdm, KID_A, KID_B) == {KID_A: KEY_A, KID_B: KEY_B}
|
||||
assert cdm.challenges == 0
|
||||
|
||||
|
||||
def test_a_vault_key_wins_over_the_licence_and_licence_keys_go_to_the_vaults() -> None:
|
||||
vault = MemoryVault({KID_A: KEY_A})
|
||||
cdm = StubCdm(licence=[key(KID_A, KEY_X), key(KID_B, KEY_B), key(KID_B, KEY_X)], vaults=vaults_of(vault))
|
||||
assert licensed(cdm, KID_A, KID_B) == {KID_A: KEY_A, KID_B: KEY_B}
|
||||
assert vault.added == {KID_A: KEY_A, KID_B: KEY_B}
|
||||
|
||||
|
||||
def test_cached_keys_covering_every_kid_need_no_licence() -> None:
|
||||
cdm = StubCdm(cached=[key(KID_A, KEY_A)])
|
||||
assert licensed(cdm, KID_A) == {KID_A: KEY_A}
|
||||
@@ -108,7 +107,7 @@ def test_widevine_skips_the_licence_when_the_cdm_holds_cached_keys() -> None:
|
||||
def licence(**_: Any) -> bytes:
|
||||
raise AssertionError("no licence request when the cache holds every key")
|
||||
|
||||
drm = Widevine(pssh=PSSH.new(system_id=PSSH.SystemId.Widevine, key_ids=[KID_A]), kid=KID_A)
|
||||
drm = widevine(KID_A)
|
||||
cdm = StubCdm(cached=[key(KID_A, KEY_A)])
|
||||
drm.get_content_keys(cdm, certificate=lambda **_: None, licence=licence)
|
||||
assert drm.content_keys == {KID_A: KEY_A}
|
||||
@@ -143,8 +142,90 @@ class L1StubCdm(StubCdm):
|
||||
|
||||
|
||||
def test_widevine_gives_a_listed_device_the_common_privacy_certificate_when_the_service_has_none() -> None:
|
||||
drm = Widevine(pssh=PSSH.new(system_id=PSSH.SystemId.Widevine, key_ids=[KID_A]), kid=KID_A)
|
||||
drm = widevine(KID_A)
|
||||
cdm = L1StubCdm()
|
||||
drm.get_content_keys(cdm, certificate=lambda **_: None, licence=lambda **_: b"licence")
|
||||
assert base64.b64decode(cdm.sent[0]["service_certificate"]) == base64.b64decode(WidevineCdm.common_privacy_cert)
|
||||
assert drm.content_keys == {KID_A: KEY_A}
|
||||
|
||||
|
||||
SYSTEMS = pytest.mark.parametrize(
|
||||
("make_drm", "device"), [(widevine, "L3"), (playready, "SL3000")], ids=["widevine", "playready"]
|
||||
)
|
||||
|
||||
|
||||
def resolve(drm: Widevine | PlayReady, cdm: StubCdm, vaults: Any, track_kid: UUID) -> int:
|
||||
"""License ``drm`` for ``track_kid`` as dl does and return the number of licence requests to the service."""
|
||||
keys = resolver(vaults, cdm=cdm)
|
||||
sent: list[int] = []
|
||||
|
||||
def licence(**_: Any) -> str:
|
||||
sent.append(1)
|
||||
return "<License>licence</License>"
|
||||
|
||||
keys.service.get_widevine_license = keys.service.get_playready_license = licence
|
||||
keys.licence_for(SimpleNamespace(drm=[drm]), None, key_table(), False)(drm, track_kid=track_kid)
|
||||
keys.wait_vault_writes()
|
||||
return len(sent)
|
||||
|
||||
|
||||
@SYSTEMS
|
||||
def test_each_vault_is_asked_once_and_gets_one_push(make_drm: Callable[..., Any], device: str) -> None:
|
||||
stores = [
|
||||
MemoryVault(name="local"),
|
||||
MemoryVault(local=False, name="remote"),
|
||||
MemoryVault(local=False, name="other"),
|
||||
]
|
||||
vaults = vaults_of(*stores)
|
||||
cdm = StubCdm(licence=[key(KID_A, KEY_A)], device=device)
|
||||
assert resolve(make_drm(KID_A), cdm, vaults, KID_A) == 1
|
||||
for store in stores:
|
||||
assert store.asked == [KID_A], store.name
|
||||
assert store.pushes == [{KID_A: KEY_A}], store.name
|
||||
|
||||
|
||||
@SYSTEMS
|
||||
def test_a_vault_key_no_decode_proved_is_in_no_push(make_drm: Callable[..., Any], device: str) -> None:
|
||||
stores = [MemoryVault(name="local"), MemoryVault({KID_A: KEY_X}, local=False, name="remote")]
|
||||
stores.append(MemoryVault(local=False, name="other"))
|
||||
vaults = vaults_of(*stores)
|
||||
cdm = StubCdm(licence=[key(KID_B, KEY_B)], device=device)
|
||||
drm = make_drm(KID_A, KID_B)
|
||||
assert resolve(drm, cdm, vaults, KID_B) == 1
|
||||
assert drm.content_keys == {KID_A: KEY_X, KID_B: KEY_B}
|
||||
assert [push for store in stores for push in store.pushes if KID_A in push] == []
|
||||
|
||||
|
||||
@SYSTEMS
|
||||
def test_a_vault_key_and_the_api_cache_covering_the_track_send_no_licence(
|
||||
make_drm: Callable[..., Any], device: str
|
||||
) -> None:
|
||||
vaults = vaults_of(MemoryVault(name="local"), MemoryVault({KID_A: KEY_A}, local=False, name="remote"))
|
||||
cdm = StubCdm(cached=[key(KID_B, KEY_B)], licence=[key(KID_A, KEY_A), key(KID_B, KEY_B)], device=device)
|
||||
drm = make_drm(KID_A, KID_B)
|
||||
assert resolve(drm, cdm, vaults, KID_B) == 0
|
||||
assert drm.content_keys == {KID_A: KEY_A, KID_B: KEY_B}
|
||||
assert cdm.required == [[KID_B.hex]]
|
||||
|
||||
|
||||
@SYSTEMS
|
||||
def test_a_licence_without_the_vault_kid_is_not_sent_again(make_drm: Callable[..., Any], device: str) -> None:
|
||||
vaults = vaults_of(MemoryVault({KID_B: KEY_B}, name="local"))
|
||||
cdm = StubCdm(licence=[key(KID_A, KEY_A)], device=device)
|
||||
drm = make_drm(KID_A, KID_B)
|
||||
assert resolve(drm, cdm, vaults, KID_A) == 1
|
||||
assert drm.content_keys == {KID_A: KEY_A, KID_B: KEY_B}
|
||||
|
||||
|
||||
def test_a_caching_block_in_a_remote_cdm_entry_still_loads() -> None:
|
||||
entry = {"type": "custom_api", "host": "https://cdm.example", "caching": {"enabled": True, "use_vaults": True}}
|
||||
cdm = load_remote_cdm("EXAMPLE_CDM", entry, "widevine", "EXAMPLE")
|
||||
assert isinstance(cdm, CustomRemoteCDM)
|
||||
|
||||
|
||||
def test_playready_keeps_its_required_kids_when_every_kid_is_known() -> None:
|
||||
drm = playready(KID_A)
|
||||
drm.content_keys[KID_A] = KEY_A
|
||||
cdm = StubCdm(cached=[], licence=[key(KID_A, KEY_A)], device="SL3000")
|
||||
drm._license_header(cdm, drm.pssh.wrm_headers[0], drm.pssh_b64, [KID_A], lambda **_: "<License>licence</License>")
|
||||
assert cdm.required == [[KID_A.hex]]
|
||||
|
||||
Reference in New Issue
Block a user