Files
unshackle/tests/core/test_licence_turns.py
T

181 lines
6.2 KiB
Python

"""Licences take turns across DRMs with different KIDs, so one licence that carries every key is sent once."""
from __future__ import annotations
import threading
from collections.abc import Callable
from types import SimpleNamespace
from typing import Any
from uuid import UUID
import click
import pytest
from pywidevine.pssh import PSSH
from rich.table import Table
from tests.core.key_resolver import MemoryVault, key_table, resolver, vaults_of
from tests.core.test_all_drm import playready
from unshackle.core.cdm.detect import cdm_type_stub
from unshackle.core.constants import DOWNLOAD_CANCELLED, DownloadCancelled
from unshackle.core.drm import ClearKeyCENC, PlayReady, Widevine
from unshackle.core.drm.key_resolution import ContentKeyResolver
pytestmark = pytest.mark.unit
KID_A, KID_B, KID_C = (UUID(c * 32) for c in "abc")
KEYS = {KID_A: "aa" * 16, KID_B: "bb" * 16, KID_C: "cc" * 16}
DEADLINE = 10
class Licences(list):
"""The thread name of each licence. A licence returns every content key of the title."""
def __init__(self) -> None:
super().__init__()
self.second_track_looked_up = threading.Event()
@pytest.fixture
def licences(monkeypatch: pytest.MonkeyPatch) -> Licences:
"""The first licence returns only once the other track has asked the vaults, so the tracks overlap."""
calls = Licences()
def get_content_keys(self: Widevine, **_: Any) -> None:
calls.append(threading.current_thread().name)
if threading.current_thread().name == "track-0":
calls.second_track_looked_up.wait(DEADLINE)
self.content_keys = dict(KEYS)
monkeypatch.setattr(Widevine, "get_content_keys", get_content_keys)
DOWNLOAD_CANCELLED.clear()
yield calls
DOWNLOAD_CANCELLED.clear()
def widevine(*kids: UUID) -> Widevine:
return Widevine(pssh=PSSH.new(system_id=PSSH.SystemId.Widevine, key_ids=list(kids)))
def prepare(keys: ContentKeyResolver, drm: Any, track_kid: UUID | None, table: Table, all_drm: bool = False) -> None:
keys.licence_for(SimpleNamespace(drm=[drm]), None, table, all_drm)(drm, track_kid=track_kid)
def test_a_track_waiting_for_its_turn_sends_no_licence_after_a_failure(licences: Licences) -> None:
DOWNLOAD_CANCELLED.set()
with pytest.raises(DownloadCancelled):
prepare(resolver(), widevine(KID_C), KID_C, key_table())
assert licences == []
@pytest.mark.parametrize(
"drm_class, build, cdm",
[
(Widevine, lambda: widevine(KID_A), "widevine"),
(PlayReady, lambda: playready(KID_A), "playready"),
(ClearKeyCENC, lambda: ClearKeyCENC(kids=[KID_A]), None),
],
ids=["widevine", "playready", "clearkey"],
)
def test_an_all_zero_licence_key_is_dropped(
monkeypatch: pytest.MonkeyPatch, drm_class: type, build: Callable[[], Any], cdm: str | None
) -> None:
def get_content_keys(self: Any, **_: Any) -> None:
self.content_keys.update({KID_A: KEYS[KID_A], KID_C: "0" * 32})
monkeypatch.setattr(drm_class, "get_content_keys", get_content_keys)
keys = resolver(cdm=cdm_type_stub(cdm or "widevine"))
drm = build()
prepare(keys, drm, KID_A, key_table())
assert drm.content_keys == {KID_A: KEYS[KID_A]}
assert ContentKeyResolver.LICENSE_KEY_CACHE == {KID_A: KEYS[KID_A]}
def test_vaults_only_stops_at_the_first_kid_no_vault_holds(licences: Licences) -> None:
empty = MemoryVault()
with pytest.raises(Widevine.Exceptions.CEKNotFound, match=KID_A.hex):
prepare(resolver(vaults_of(empty), vaults_only=True), widevine(KID_A, KID_B), KID_A, key_table())
assert empty.asked == [KID_A]
assert licences == []
def refuse_licence(self: Widevine, **_: Any) -> None:
raise click.ClickException("Licence request failed")
def test_a_refused_licence_without_the_track_key_reports_the_licence_error(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
monkeypatch.setattr(Widevine, "get_content_keys", refuse_licence)
keys = resolver()
ContentKeyResolver.LICENSE_KEY_CACHE[KID_B] = KEYS[KID_B]
table = key_table()
with pytest.raises(click.ClickException, match="^Licence request failed$"):
prepare(keys, widevine(KID_A, KID_B), KID_A, table)
assert not [r for r in caplog.records if r.levelname == "WARNING"]
tree = list(table.columns[0].cells)[-1]
assert "Licence request failed" in str(tree.children[-1].label)
@pytest.mark.parametrize("all_drm", [False, True])
def test_a_licence_that_the_user_cancelled_shows_no_error(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, all_drm: bool
) -> None:
def cancel(self: Widevine, **_: Any) -> None:
raise DownloadCancelled
monkeypatch.setattr(Widevine, "get_content_keys", cancel)
keys = resolver(best_available=True)
keys.all_drm_warned.add("PlayReady")
ContentKeyResolver.LICENSE_KEY_CACHE[KID_B] = KEYS[KID_B]
table = key_table()
with pytest.raises(DownloadCancelled):
prepare(keys, widevine(KID_A, KID_B), None, table, all_drm)
assert not [r for r in caplog.records if r.levelname == "WARNING"]
assert list(table.columns[0].cells) == [""]
def test_a_held_track_key_needs_no_licence(licences: Licences) -> None:
keys = resolver()
ContentKeyResolver.LICENSE_KEY_CACHE[KID_A] = KEYS[KID_A]
drm = widevine(KID_A, KID_B)
prepare(keys, drm, KID_A, key_table())
assert licences == []
assert drm.content_keys == {KID_A: KEYS[KID_A]}
def test_force_licenses_a_held_track_key_again(licences: Licences) -> None:
keys = resolver(cdm_only=True)
ContentKeyResolver.LICENSE_KEY_CACHE[KID_A] = KEYS[KID_A]
prepare(keys, widevine(KID_A, KID_B), KID_A, key_table(), all_drm=True)
assert len(licences) == 1
def test_a_refused_licence_keeps_the_known_keys_when_the_track_kid_is_unknown(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
) -> None:
monkeypatch.setattr(Widevine, "get_content_keys", refuse_licence)
keys = resolver()
ContentKeyResolver.LICENSE_KEY_CACHE[KID_A] = KEYS[KID_A]
drm = widevine(KID_A, KID_B)
prepare(keys, drm, None, key_table())
assert drm.content_keys == {KID_A: KEYS[KID_A]}
assert [r.getMessage() for r in caplog.records if r.levelname == "WARNING"] == [
"Licence failed, using the 1 known key(s): Licence request failed"
]