mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 07:25:08 +00:00
101 lines
4.5 KiB
Python
101 lines
4.5 KiB
Python
"""``decrypter_for`` sends a track whose sample offsets are not relative to the moof to mp4decrypt."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import struct
|
|
import subprocess
|
|
from pathlib import Path
|
|
from uuid import UUID
|
|
|
|
import pytest
|
|
|
|
from tests.core.test_ffmpeg_decodes import GOP, video
|
|
from tests.core.test_kid_windows import box, full
|
|
from unshackle.core import binaries
|
|
from unshackle.core.config import config
|
|
from unshackle.core.drm import ClearKeyCENC
|
|
from unshackle.core.drm.verify import decrypter_for, offsets_not_moof_relative
|
|
from unshackle.core.utils.subprocess import ffmpeg_decodes
|
|
|
|
KID = UUID("11223344556677889900aabbccddeeff")
|
|
KEY = "00112233445566778899aabbccddeeff"
|
|
GPAC_DRM = f"""<?xml version="1.0" encoding="UTF-8"?>
|
|
<GPACDRM type="CENC AES-CTR">
|
|
<CrypTrack trackID="1" IsEncrypted="1" IV_size="8" first_IV="0x0123456789abcdef" saiSavedBox="senc">
|
|
<key KID="0x{KID.hex}" value="0x{KEY}"/>
|
|
</CrypTrack>
|
|
</GPACDRM>
|
|
"""
|
|
|
|
|
|
def trun(flags: int) -> bytes:
|
|
return full(b"trun", struct.pack(">I", 0) + (struct.pack(">i", 100) if flags & 1 else b""), flags=flags)
|
|
|
|
|
|
ABSOLUTE = full(b"tfhd", struct.pack(">IQ", 1, 2322), flags=0x000001)
|
|
MOOF_BASE = full(b"tfhd", struct.pack(">I", 1), flags=0x020000)
|
|
|
|
|
|
def fragment(*truns: bytes, tfhd: bytes = MOOF_BASE) -> bytes:
|
|
return box(b"moof", full(b"mfhd", struct.pack(">I", 1)), box(b"traf", tfhd, *truns)) + box(b"mdat", bytes(16))
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("data", "expected"),
|
|
[
|
|
pytest.param(box(b"moov") + fragment(trun(0xA04)), True, id="no-offset"),
|
|
pytest.param(box(b"moov") + fragment(trun(0xA05)), False, id="offset"),
|
|
pytest.param(box(b"moov") + fragment(trun(0xA04), tfhd=ABSOLUTE), True, id="gpac"),
|
|
pytest.param(box(b"moov") + fragment(trun(0xA05), tfhd=ABSOLUTE), True, id="base-and-offset"),
|
|
pytest.param(box(b"moov") + fragment(trun(0xA04), trun(0xA05)), True, id="first-of-two"),
|
|
pytest.param(box(b"moov") + fragment(trun(0xA05)) + fragment(trun(0xA04)), False, id="second-moof"),
|
|
pytest.param(box(b"moov") + box(b"mdat", bytes(16)), False, id="progressive"),
|
|
pytest.param(box(b"moov") + fragment(trun(0xA04))[:30], False, id="truncated"),
|
|
pytest.param(box(b"moov") + box(b"moof", box(b"traf", box(b"trun"))), False, id="empty-trun"),
|
|
pytest.param(box(b"moov") + box(b"moof", b"\x00\x00\x00\x01traf\x00"), False, id="cut-largesize"),
|
|
pytest.param(b"\xff" * 64, False, id="garbage"),
|
|
pytest.param(box(b"moov") + b"\x00\x00\x00\x01moof" + (2**62).to_bytes(8, "big"), False, id="huge-moof"),
|
|
pytest.param(box(b"moov") + b"\x00\x00\x00\x01free" + (2**64 - 1).to_bytes(8, "big"), False, id="huge-box"),
|
|
],
|
|
)
|
|
def test_offsets_not_moof_relative(tmp_path: Path, data: bytes, expected: bool) -> None:
|
|
(path := tmp_path / "track.mp4").write_bytes(data)
|
|
assert offsets_not_moof_relative(path) is expected
|
|
|
|
|
|
def test_a_missing_file_is_not_detected(tmp_path: Path) -> None:
|
|
assert offsets_not_moof_relative(tmp_path / "gone.mp4") is False
|
|
|
|
|
|
def test_decrypter_for_routes_only_the_shaka_default(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
(flagged := tmp_path / "gpac.mp4").write_bytes(box(b"moov") + fragment(trun(0xA04), tfhd=ABSOLUTE))
|
|
(normal := tmp_path / "dash.mp4").write_bytes(box(b"moov") + fragment(trun(0xA05)))
|
|
monkeypatch.setattr(config, "decryption", "shaka")
|
|
assert decrypter_for(flagged) == "mp4decrypt"
|
|
assert decrypter_for(normal) == "shaka"
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
not (binaries.FFMPEG and binaries.MP4Box and binaries.Mp4decrypt and binaries.ShakaPackager),
|
|
reason="needs FFmpeg, MP4Box, mp4decrypt and shaka-packager",
|
|
)
|
|
def test_a_gpac_fragmented_track_decrypts_under_the_shaka_default(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
def run(*cmd: object) -> None:
|
|
subprocess.run([str(part) for part in cmd], cwd=tmp_path, check=True, capture_output=True)
|
|
|
|
video(tmp_path / "src.mp4", *GOP, size="640x360")
|
|
(tmp_path / "drm.xml").write_text(GPAC_DRM)
|
|
run(binaries.MP4Box, "-quiet", "-crypt", "drm.xml", "src.mp4", "-out", "enc.mp4")
|
|
run(binaries.MP4Box, "-quiet", "-frag", "2000", "enc.mp4", "-out", "track.mp4")
|
|
track = tmp_path / "track.mp4"
|
|
monkeypatch.setattr(config, "decryption", "shaka")
|
|
monkeypatch.setattr(config.directories, "temp", tmp_path / "temp")
|
|
|
|
picked = decrypter_for(track)
|
|
ClearKeyCENC(kids=[KID], content_keys={KID: KEY}).decrypt(track)
|
|
|
|
assert ffmpeg_decodes(track, video=True)
|
|
assert picked == "mp4decrypt"
|