mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-04 06:55:09 +00:00
feat(drm): license a plugin DRM at the licence step and over --remote with the client's plugin device
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
"""A plugin DRM licenses at the licence step of content-key resolution, like a CDM track: vaults first, then a device."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
import click
|
||||
import pytest
|
||||
|
||||
from tests.core.key_resolver import MemoryVault, key_table, resolver, vaults_of
|
||||
from tests.drm_plugins import ExampleDevice
|
||||
from unshackle.core.drm import CustomDRM
|
||||
from unshackle.core.tracks import Video
|
||||
from unshackle.core.tracks.track import Track
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
KID = UUID(hex="0123456789abcdef0123456789abcdef")
|
||||
KEY = "5a" * 16
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def device(tmp_path: Path) -> ExampleDevice:
|
||||
path = tmp_path / "EXAMPLE_DEVICE.exd"
|
||||
path.write_bytes(bytes.fromhex(KEY))
|
||||
return ExampleDevice(path)
|
||||
|
||||
|
||||
def plugin_track(data: dict[str, Any] | None = None) -> Video:
|
||||
drm = CustomDRM("example", data={"ticket": KID.hex} if data is None else data)
|
||||
return Video(
|
||||
id_="vid",
|
||||
url="https://example.test/v.m3u8",
|
||||
language="en",
|
||||
descriptor=Track.Descriptor.HLS,
|
||||
height=1080,
|
||||
drm=[drm],
|
||||
)
|
||||
|
||||
|
||||
def run_licence(track: Video, vault: MemoryVault, device: Any, loads: list, **flags: Any) -> CustomDRM:
|
||||
"""Run the licence step of ``track`` with ``vault``; ``loads`` records each device the run loads."""
|
||||
|
||||
def cdm_for(system: str, quality: int | None = None) -> Any:
|
||||
loads.append((system, quality))
|
||||
return device
|
||||
|
||||
keys = resolver(vaults_of(vault), cdm_for=cdm_for, **flags)
|
||||
drm = track.drm[0]
|
||||
keys.licence_for(track, SimpleNamespace(tracks=SimpleNamespace(videos=[track])), key_table())(drm)
|
||||
keys.wait_vault_writes()
|
||||
return drm
|
||||
|
||||
|
||||
def test_the_drm_reads_its_kids_from_the_plugin_data_before_any_licence(example_plugins: None) -> None:
|
||||
assert plugin_track().drm[0].kids == [KID]
|
||||
|
||||
|
||||
def test_a_vault_key_stands_in_for_the_licence_and_loads_no_device(example_plugins: None, device: Any) -> None:
|
||||
loads: list = []
|
||||
drm = run_licence(plugin_track(), MemoryVault({KID: KEY}), device, loads)
|
||||
|
||||
assert drm.content_keys == {KID: KEY} and loads == []
|
||||
|
||||
|
||||
def test_a_vault_miss_licenses_with_the_plugin_device_and_stores_the_key(example_plugins: None, device: Any) -> None:
|
||||
loads: list = []
|
||||
vault = MemoryVault()
|
||||
drm = run_licence(plugin_track(), vault, device, loads)
|
||||
|
||||
assert drm.content_keys == {KID: KEY} and loads == [("example", None)]
|
||||
assert vault.asked == [KID] and vault.added == {KID: KEY}
|
||||
|
||||
|
||||
def test_vaults_only_stops_a_miss_before_the_device(example_plugins: None, device: Any) -> None:
|
||||
loads: list = []
|
||||
|
||||
with pytest.raises(click.ClickException, match=f"^No Vault has a Key for {KID.hex} and --vaults-only was used$"):
|
||||
run_licence(plugin_track(), MemoryVault(), device, loads, vaults_only=True)
|
||||
assert loads == []
|
||||
|
||||
|
||||
def test_cdm_only_licenses_with_the_device_and_asks_no_vault(example_plugins: None, device: Any) -> None:
|
||||
loads: list = []
|
||||
vault = MemoryVault({KID: "00" * 16})
|
||||
drm = run_licence(plugin_track(), vault, device, loads, cdm_only=True)
|
||||
|
||||
assert drm.content_keys == {KID: KEY} and vault.asked == [] and loads == [("example", None)]
|
||||
|
||||
|
||||
def test_a_run_without_a_plugin_device_stops_the_track_with_one_line(example_plugins: None) -> None:
|
||||
with pytest.raises(click.ClickException, match="^No EXAMPLE device to license track vid$"):
|
||||
run_licence(plugin_track(), MemoryVault(), None, [])
|
||||
|
||||
|
||||
def test_a_drm_whose_data_names_no_kid_stops_the_track_with_one_line(example_plugins: None, device: Any) -> None:
|
||||
with pytest.raises(click.ClickException, match="^The EXAMPLE DRM of track vid names no KID$"):
|
||||
run_licence(plugin_track({}), MemoryVault(), device, [])
|
||||
Reference in New Issue
Block a user