fix(import): keep the FairPlay bridged mark and every KID of a DRM through an export

This commit is contained in:
imSp4rky
2026-12-01 21:46:44 -07:00
parent 1288a8e9b7
commit 992fff3626
5 changed files with 124 additions and 18 deletions
+106
View File
@@ -0,0 +1,106 @@
"""A FairPlay-bridged DRM keeps its bridged mark and every KID through an export and an import."""
from __future__ import annotations
import json
from pathlib import Path
from types import SimpleNamespace
from typing import Any
import pytest
from tests.core.key_resolver import resolver
from tests.core.test_export import make_writer, written
from tests.core.test_import_track_keys import AUDIO_KID, MPD, MPD_URL, VIDEO_KID, StubService, import_service
from tests.core.test_vault_bad_keys import BAD, GOOD
from unshackle.core.drm import key_resolution
from unshackle.core.manifests import DASH, HLS
from unshackle.core.titles import Movie
from unshackle.core.wire import drm_from_dict, drm_to_dict
@pytest.mark.parametrize("system", ["widevine", "playready"])
def test_the_export_form_keeps_the_bridged_mark_and_every_kid(system: str) -> None:
stub = HLS.bridge_stub(system, [VIDEO_KID, AUDIO_KID])
back: Any = drm_from_dict(json.loads(json.dumps(drm_to_dict(stub))))
assert back.bridged
assert back.kids == stub.kids == [VIDEO_KID, AUDIO_KID]
assert drm_to_dict(back) == drm_to_dict(stub)
def export_bridged(
tmp_path: Path, system: str = "widevine", row_kids: bool = True, mark: bool = True
) -> tuple[Path, str]:
"""Export a title whose video and audio each carry a bridged stub with a bad content key.
``row_kids=False`` drops the per-track KID lists, and ``mark=False`` the bridged mark, as in older exports.
"""
title = Movie(id_="movie-1", service=StubService, name="Example Movie", year=2024, language="en")
for track in DASH.from_text(MPD, MPD_URL).to_tracks(language="en"):
title.tracks.add(track)
writer = make_writer(tmp_path)
video = title.tracks.videos[0]
for track, kid in ((video, VIDEO_KID), (title.tracks.audio[0], AUDIO_KID)):
drm = HLS.bridge_stub(system, [kid])
drm.content_keys[kid] = BAD
writer.record(title, track, drm)
export = written(writer)
doc = json.loads(export.read_text(encoding="utf8"))
entry = doc["titles"][0]
if not row_kids:
for row in entry["tracks"]:
row.pop("kids", None)
if not mark:
for d in entry["x-unshackle"]["drm"]:
d.pop("bridged", None)
export.write_text(json.dumps(doc), encoding="utf8")
return export, str(video.id)
@pytest.mark.parametrize("system", ["widevine", "playready"])
@pytest.mark.parametrize("row_kids", [True, False], ids=["row-kids", "stub-fallback"])
def test_an_imported_bridged_track_stops_on_a_key_that_does_not_decrypt(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, system: str, row_kids: bool
) -> None:
export, video_id = export_bridged(tmp_path, system, row_kids)
drm: Any = import_service(export).rebuild_drm({"id": video_id, "type": "Video"}, "movie-1")[0] # type: ignore[index]
checked: list[Path] = []
def ffmpeg_decodes(path: Path, start: Any = None, seconds: int = 3, video: Any = None) -> bool:
checked.append(path)
return path.read_bytes() == b"plain:" + GOOD.encode()
def decrypt(path: Path) -> None:
path.write_bytes(b"plain:" + drm.content_keys[VIDEO_KID].encode())
def licence(d: Any, track_kid: Any = None) -> Any:
raise AssertionError("an import licenses nothing")
monkeypatch.setattr(key_resolution.binaries, "FFMPEG", "ffmpeg")
monkeypatch.setattr(key_resolution, "ffmpeg_decodes", ffmpeg_decodes)
monkeypatch.setattr(drm, "decrypt", decrypt, raising=False)
path = tmp_path / "track.mp4"
path.write_bytes(b"cipher")
keys = resolver(None, tag="EXAMPLE")
with pytest.raises(ValueError, match="does not decrypt track vid") as e:
try:
keys.decrypt_verified(drm, path, licence, VIDEO_KID, True, SimpleNamespace(id="vid")) # type: ignore[arg-type]
finally:
keys.wait_vault_writes()
assert drm.bridged
assert checked
assert "\n" not in str(e.value)
assert not path.exists()
def test_an_export_without_the_mark_imports_a_plain_stub(tmp_path: Path) -> None:
export, video_id = export_bridged(tmp_path, mark=False)
drm = import_service(export).rebuild_drm({"id": video_id, "type": "Video"}, "movie-1")[0] # type: ignore[index]
assert not drm.bridged
assert drm.kids == [VIDEO_KID]