"""DASH.download_track refuses an encrypted track that no DRM it can license covers.""" from __future__ import annotations import struct from functools import partial from pathlib import Path from typing import Any from uuid import UUID import pytest from tests.core.test_dash_collapse_integration import clear_cancel as clear_cancel # noqa: F401 from tests.core.test_dash_collapse_integration import server as server # noqa: F401 from tests.core.test_kid_windows import box, full, moof, movie from unshackle.core.api.errors import APIErrorCode, categorize_exception from unshackle.core.constants import DOWNLOAD_LICENCE_ONLY from unshackle.core.manifests import DASH from unshackle.core.tracks.track import DownloadContext KID = UUID("9eb4050d-e44b-4802-932e-27d75083e266") TENC = full(b"tenc", b"\x00\x00\x01\x00" + KID.bytes + b"\x10" + bytes(16), version=1) SCHEME = box(b"schm", bytes(4) + b"cbcs" + struct.pack(">I", 0x10000)) ENCRYPTED = movie(box(b"encv", bytes(78), box(b"sinf", box(b"frma", b"avc1"), SCHEME, box(b"schi", TENC)))) CLEAR = movie(box(b"avc1", bytes(78))) SEGMENT = moof(1, 0, full(b"senc", bytes(4))) FAIRPLAY = '' MP4 = f'' TEMPLATE = ( '' ) ONE_FILE = "whole.mp4" def download( srv: Any, tmp_path: Path, init: bytes, protections: str, segments: str = TEMPLATE, **kwargs: Any ) -> tuple[DownloadContext, list]: srv.routes = {"/init.mp4": init, "/whole.mp4": init + SEGMENT + SEGMENT, "/1.m4s": SEGMENT, "/2.m4s": SEGMENT} mpd = f""" http://127.0.0.1:{srv.server_address[1]}/ {protections} {segments} """ track = DASH.from_text(mpd, url="http://x/manifest.mpd").to_tracks(language="en").videos[0] licences: list = [] ctx = DownloadContext( save_path=tmp_path / "out.mp4", save_dir=tmp_path / "v0_segments", progress=partial(lambda **_: None), license_widevine=lambda drm, track_kid=None: licences.append(drm), cdm=None, **kwargs, ) DASH.download_track(track, ctx) return ctx, licences @pytest.mark.parametrize( ("protections", "systems"), [ pytest.param( FAIRPLAY + MP4, "urn:uuid:94ce86fb-07ff-4f43-adb8-93d2fa968ca2, urn:mpeg:dash:mp4protection:2011", id="fairplay-and-mp4protection", ), pytest.param(FAIRPLAY, "urn:uuid:94ce86fb-07ff-4f43-adb8-93d2fa968ca2", id="fairplay"), pytest.param("", "no ContentProtection", id="no-content-protection"), ], ) @pytest.mark.parametrize("licence_only", [False, True], ids=["download", "licence-only"]) def test_an_encrypted_init_with_no_drm_is_refused( server: Any, tmp_path: Path, protections: str, systems: str, licence_only: bool ) -> None: if licence_only: DOWNLOAD_LICENCE_ONLY.set() try: with pytest.raises(NotImplementedError) as refused: download(server, tmp_path, ENCRYPTED, protections) finally: DOWNLOAD_LICENCE_ONLY.clear() assert str(refused.value) == f"None of the key systems are supported: {systems}" assert categorize_exception(refused.value).error_code is APIErrorCode.DRM_ERROR assert not (tmp_path / "out.mp4").exists() assert not any(path == "/1.m4s" for path, _ in server.requests) def test_an_encrypted_file_with_no_init_segment_is_refused_and_removed(server: Any, tmp_path: Path) -> None: with pytest.raises(NotImplementedError, match="^None of the key systems are supported: no ContentProtection$"): download(server, tmp_path, ENCRYPTED, "", ONE_FILE) assert any(path == "/whole.mp4" for path, _ in server.requests) assert not (tmp_path / "out.mp4").exists() @pytest.mark.parametrize("segments", [TEMPLATE, ONE_FILE], ids=["init-segment", "one-file"]) def test_a_clear_track_still_downloads(server: Any, tmp_path: Path, segments: str) -> None: ctx, licences = download(server, tmp_path, CLEAR, "", segments) assert ctx.save_path.read_bytes().startswith(CLEAR) assert licences == [] def test_a_service_sample_decrypter_takes_the_encrypted_track(server: Any, tmp_path: Path) -> None: entries: list[int] = [] def decrypter(entry: int) -> bytes: entries.append(entry) return bytes(16) ctx, licences = download(server, tmp_path, ENCRYPTED, "", sample_decrypter=decrypter) assert ctx.save_path.exists() assert entries assert licences == []