mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
feat(drm): license MonaLisa over --remote on the server and decrypt it in core, with or without bbtsdecrypt
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
"""The built-in BBTS decrypter against output of the bbtsdecrypt binary, on a synthetic file built in the test."""
|
||||
|
||||
import hashlib
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.core import bbts_sample
|
||||
from unshackle.core import binaries
|
||||
from unshackle.core.drm.bbts import decrypt_bbts, renumber_continuity
|
||||
from unshackle.core.utils.hdr10plus import ts_packets, ts_pid
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
def packets_of(data: bytes, pid: int) -> list[bytes]:
|
||||
"""The TS packets of one PID, or of every PID for -1."""
|
||||
return [packet for packet in ts_packets(data) if pid in (-1, ts_pid(packet))]
|
||||
|
||||
|
||||
@pytest.fixture(params=[bbts_sample.HEVC, bbts_sample.H264], ids=["hevc", "h264"])
|
||||
def sample(request: pytest.FixtureRequest, tmp_path: Path) -> tuple[int, Path]:
|
||||
path = tmp_path / "segment.bbts"
|
||||
path.write_bytes(bbts_sample.bbts(request.param))
|
||||
return request.param, path
|
||||
|
||||
|
||||
def test_the_clear_file_is_the_one_bbtsdecrypt_wrote(sample: tuple[int, Path], tmp_path: Path) -> None:
|
||||
video_type, src = sample
|
||||
decrypt_bbts(src, tmp_path / "clear.ts", bbts_sample.KEY)
|
||||
|
||||
assert hashlib.sha256((tmp_path / "clear.ts").read_bytes()).hexdigest() == bbts_sample.GOLDEN[video_type]
|
||||
|
||||
|
||||
def test_only_the_video_pid_changes(sample: tuple[int, Path], tmp_path: Path) -> None:
|
||||
_, src = sample
|
||||
decrypt_bbts(src, tmp_path / "clear.ts", bbts_sample.KEY)
|
||||
before, after = src.read_bytes(), (tmp_path / "clear.ts").read_bytes()
|
||||
|
||||
assert len(after) == len(before)
|
||||
for pid in (0x0000, 0x0011, 0x1000, 0x0101):
|
||||
assert packets_of(after, pid) == packets_of(before, pid)
|
||||
assert packets_of(after, 0x0100) != packets_of(before, 0x0100)
|
||||
|
||||
|
||||
def test_the_binary_writes_the_same_file(sample: tuple[int, Path], tmp_path: Path) -> None:
|
||||
if not (tool := binaries.find("bbtsdecrypt")):
|
||||
pytest.skip("no bbtsdecrypt binary on this machine")
|
||||
_, src = sample
|
||||
decrypt_bbts(src, tmp_path / "builtin.ts", bbts_sample.KEY)
|
||||
subprocess.run(
|
||||
[tool, "-i", src, "-o", tmp_path / "tool.ts", "-k", f"{'0' * 32}:{bbts_sample.KEY.hex()}"],
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
assert (tmp_path / "builtin.ts").read_bytes() == (tmp_path / "tool.ts").read_bytes()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("video_type", [bbts_sample.HEVC, bbts_sample.H264], ids=["hevc", "h264"])
|
||||
@pytest.mark.parametrize(
|
||||
"layout",
|
||||
[{"pmt_first": True}, {"name_format": "mdcm|a|b|{iv}|c"}],
|
||||
ids=["pmt-before-sdt", "iv-in-fourth-field"],
|
||||
)
|
||||
def test_other_table_layouts_give_the_same_video(video_type: int, layout: dict, tmp_path: Path) -> None:
|
||||
(tmp_path / "usual.bbts").write_bytes(bbts_sample.bbts(video_type))
|
||||
(tmp_path / "other.bbts").write_bytes(bbts_sample.bbts(video_type, **layout))
|
||||
for name in ("usual", "other"):
|
||||
decrypt_bbts(tmp_path / f"{name}.bbts", tmp_path / f"{name}.ts", bbts_sample.KEY)
|
||||
usual, other = ((tmp_path / f"{name}.ts").read_bytes() for name in ("usual", "other"))
|
||||
|
||||
assert hashlib.sha256(usual).hexdigest() == bbts_sample.GOLDEN[video_type]
|
||||
assert packets_of(other, 0x0100) == packets_of(usual, 0x0100)
|
||||
|
||||
|
||||
def test_a_file_with_no_iv_is_copied(tmp_path: Path) -> None:
|
||||
data = b"".join(packet for packet in packets_of(bbts_sample.bbts(), -1) if packet[1:3] != b"\x40\x11")
|
||||
(tmp_path / "in.ts").write_bytes(data)
|
||||
decrypt_bbts(tmp_path / "in.ts", tmp_path / "out.ts", bbts_sample.KEY)
|
||||
|
||||
assert (tmp_path / "out.ts").read_bytes() == data
|
||||
|
||||
|
||||
def test_a_key_of_another_length_is_refused(tmp_path: Path) -> None:
|
||||
with pytest.raises(ValueError, match="16-byte key"):
|
||||
decrypt_bbts(tmp_path / "in.ts", tmp_path / "out.ts", b"short")
|
||||
|
||||
|
||||
def test_the_counter_steps_only_on_a_packet_with_payload(tmp_path: Path) -> None:
|
||||
def packet(pid: int, cc: int, payload: bool = True) -> bytes:
|
||||
body = b"\x00" * 184 if payload else bytes([183, 0]) + b"\xff" * 182
|
||||
return bytes([0x47, pid >> 8, pid & 0xFF, (0x10 if payload else 0x20) | cc]) + body
|
||||
|
||||
path = tmp_path / "clear.ts"
|
||||
sent = [
|
||||
(0x100, 5, True),
|
||||
(0x100, 6, False),
|
||||
(0x1FFF, 3, True),
|
||||
(0x100, 7, True),
|
||||
(0x100, 7, True),
|
||||
(0x100, 8, True),
|
||||
]
|
||||
path.write_bytes(b"".join(packet(*p) for p in sent))
|
||||
renumber_continuity(path)
|
||||
|
||||
assert [p[3] & 0x0F for p in packets_of(path.read_bytes(), -1)] == [5, 5, 3, 6, 6, 7]
|
||||
Reference in New Issue
Block a user