feat(remote): name the missing KID in the server-device error

This commit is contained in:
imSp4rky
2026-11-30 11:03:47 -07:00
parent ea073209da
commit 40114fff19
4 changed files with 98 additions and 16 deletions
@@ -9,10 +9,13 @@ import logging
from types import SimpleNamespace
from uuid import UUID
import click
import pytest
from tests.core.key_resolver import key_table, resolver
from tests.remote.key_source import remote_keys
from unshackle.core.remote_protocol import LicensingMode, Licensor
from unshackle.core.api.errors import categorize_exception
from unshackle.core.remote_protocol import APIErrorCode, LicensingMode, Licensor
pytestmark = pytest.mark.unit
@@ -100,8 +103,6 @@ def test_proxy_mode_keeps_the_callers_type():
def test_a_refused_relayed_licence_raises_a_click_exception_with_the_server_message(caplog):
import click
from unshackle.core.remote_service import RemoteError
svc = _service("widevine", {})
@@ -163,11 +164,77 @@ def test_a_track_with_no_header_sends_nothing_and_gets_no_keys():
def test_a_deferred_refusal_raises_before_any_request():
import click
svc = _service("widevine", {})
svc.deferred_refusals["vid"] = "capped"
with pytest.raises(click.ClickException, match="^capped$"):
svc.license_track(SimpleNamespace(id="vid", drm=[_Widevine()]), None)
assert svc.client.posts == []
class _Track(SimpleNamespace):
"""A track whose text never holds the KIDs of its DRM."""
def __str__(self):
return "VID | [H.265] | ko"
KID_404 = UUID("40400000-0000-4000-8000-000000000001")
KID_429 = UUID("00000429-0000-4000-8000-000000000001")
PLACEHOLDER = UUID("00010203-0405-0607-0809-0a0b0c0d0e0f")
LOCAL_VAULTS = "none of your vaults, and this API key cannot read the server vault"
ALL_VAULTS = "no vault, neither the server's nor yours"
def _drm(*kids, keys=()):
drm = _Widevine()
drm.kids = list(kids)
drm.content_keys = dict.fromkeys(keys, "k")
return drm
def _lent_device(vaults):
"""A remote session on the server's device whose refusal comes from the call of license_track that gives ``vaults``."""
svc = _service("widevine", {"error_code": APIErrorCode.SERVER_CDM_CAPPED, "details": {"reason": "server_device"}})
svc.licensing = LicensingMode(Licensor.LENT_DEVICE, server_vault=vaults == ALL_VAULTS)
return svc
@pytest.mark.parametrize("vaults", [LOCAL_VAULTS, ALL_VAULTS])
@pytest.mark.parametrize(
("drm", "track_drm", "kid", "named"),
[
pytest.param(_drm(KID, KID_429), [], KID_404, [KID_404], id="the-kid-dl-needs"),
pytest.param(_drm(KID, KID_404), [], KID_429, [KID_429], id="the-kid-dl-needs-with-429"),
pytest.param(_drm(KID, KID_404, keys=[KID]), [], None, [KID_404], id="the-drm-without-the-held-kid"),
pytest.param(_drm(KID_404, KID_429, PLACEHOLDER), [], None, [KID_429, KID_404], id="sorted-real-kids"),
pytest.param(None, [_drm(KID_404), _drm(KID_404, KID_429)], None, [KID_429, KID_404], id="the-track-drm"),
pytest.param(_drm(KID_404), [_drm(KID)], None, [KID_404], id="the-drm-before-the-track-drm"),
pytest.param(_drm(KID_404), [], PLACEHOLDER, [KID_404], id="a-placeholder-kid"),
pytest.param(_drm(PLACEHOLDER), [], PLACEHOLDER, [], id="only-placeholders"),
pytest.param(_drm(KID, keys=[KID]), [], None, [], id="every-kid-held"),
],
)
def test_the_server_device_refusal_is_a_drm_error_that_names_the_missing_kid(vaults, drm, track_drm, kid, named):
svc = _lent_device(vaults)
with pytest.raises(click.ClickException) as caught:
svc.license_track(_Track(id="vid", drm=track_drm or [drm]), drm, kid)
assert len(svc.client.posts) == (vaults == ALL_VAULTS)
assert categorize_exception(caught.value).error_code is APIErrorCode.DRM_ERROR
article = "A" if len(named) > 1 else "The"
of_kid = f" of KID {', '.join(k.hex for k in named)}" if named else ""
assert caught.value.message == (
f"{article} content key{of_kid} for VID | [H.265] | ko is in {vaults}; "
"drop the service's server-identity option to license it locally"
)
@pytest.mark.parametrize("vaults", [LOCAL_VAULTS, ALL_VAULTS])
def test_the_server_device_refusal_in_a_download_names_only_the_kid_dl_needs(vaults):
drm = _drm(KID, KID_404)
prepare_drm = resolver(source=_lent_device(vaults)).licence_for(_Track(id="vid", drm=[drm]), None, key_table())
with pytest.raises(click.ClickException, match=f"^The content key of KID {KID_404.hex} for .* is in {vaults};"):
prepare_drm(drm, track_kid=KID_404)