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:
@@ -1,12 +1,16 @@
|
||||
"""MonaLisa's external decrypt tool: a missing tool raises, and a failed run leaves the ciphertext untouched."""
|
||||
"""MonaLisa's decrypt step: the external tool, or the built-in decrypter when this machine has none."""
|
||||
|
||||
import functools
|
||||
import hashlib
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.drm.monalisa import MonaLisa
|
||||
from tests.core import bbts_sample
|
||||
from unshackle.core.drm import monalisa
|
||||
from unshackle.core.drm.monalisa import MonaLisa, bbtsdecrypt_command
|
||||
from unshackle.core.utils import subprocess as tool_runner
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
@@ -27,7 +31,7 @@ def make_drm(monkeypatch: pytest.MonkeyPatch):
|
||||
def build(tool: Path, **kwargs) -> MonaLisa:
|
||||
drm = MonaLisa("ticket", Path("unused.mld"), tool, **kwargs)
|
||||
drm._kid = UUID(int=1)
|
||||
drm._key = "00" * 16
|
||||
drm.content_keys[drm._kid] = "00" * 16
|
||||
return drm
|
||||
|
||||
return build
|
||||
@@ -114,7 +118,8 @@ def test_a_service_command_builder_replaces_the_default_command(
|
||||
drm = make_drm(tool, command_builder=command, segment_decrypt=False)
|
||||
else:
|
||||
drm = MonaLisa.from_ticket("ticket", Path("unused.mld"), tool, command, False)
|
||||
drm._kid, drm._key = UUID(int=1), "00" * 16
|
||||
drm._kid = UUID(int=1)
|
||||
drm.content_keys[drm._kid] = "00" * 16
|
||||
assert not drm.supports_segment_decrypt
|
||||
drm.decrypt(track)
|
||||
output = track.with_name("track_decrypted.mp4")
|
||||
@@ -157,3 +162,104 @@ def test_a_segment_tool_decrypts_every_file_it_gets_an_output_path_for(
|
||||
for output in outputs:
|
||||
drm.decrypt(track, output)
|
||||
assert [output.read_bytes() for output in outputs] == [b"plain", b"plain"]
|
||||
|
||||
|
||||
def local_tools(monkeypatch: pytest.MonkeyPatch, bbtsdecrypt: Path | None, shaka: Path | None) -> None:
|
||||
"""Make `bbtsdecrypt` and `shaka` the binaries that this machine has."""
|
||||
monkeypatch.setattr(monalisa, "local_tool", functools.cache(monalisa.local_tool.__wrapped__))
|
||||
monkeypatch.setattr(
|
||||
monalisa.binaries, "find", lambda *names, **kwargs: bbtsdecrypt if "bbtsdecrypt" in names else None
|
||||
)
|
||||
monkeypatch.setattr(monalisa.binaries, "ShakaPackager", shaka, raising=False)
|
||||
|
||||
|
||||
def test_a_drm_without_a_tool_runs_the_bbtsdecrypt_of_this_machine_per_segment(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, track: Path
|
||||
) -> None:
|
||||
tool = tmp_path / "bbtsdecrypt"
|
||||
tool.write_bytes(b"")
|
||||
local_tools(monkeypatch, tool, None)
|
||||
commands = []
|
||||
|
||||
def run(cmd, **kwargs):
|
||||
commands.append(cmd)
|
||||
Path(cmd[cmd.index("-o") + 1]).write_bytes(b"plain")
|
||||
return subprocess.CompletedProcess(cmd, 0, b"", b"")
|
||||
|
||||
monkeypatch.setattr(tool_runner.subprocess, "run", run)
|
||||
drm = MonaLisa(kid=UUID(int=1))
|
||||
drm.content_keys[UUID(int=1)] = "ab" * 16
|
||||
|
||||
assert drm.supports_segment_decrypt
|
||||
drm.decrypt(track)
|
||||
|
||||
output = track.with_name("track_decrypted.mp4")
|
||||
assert commands == [[str(arg) for arg in bbtsdecrypt_command(tool, track, output, UUID(int=1).hex, "ab" * 16)]]
|
||||
assert track.read_bytes() == b"plain"
|
||||
|
||||
|
||||
def built_in_drm(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> MonaLisa:
|
||||
"""A MonaLisa on a machine with no bbtsdecrypt but a shaka-packager built with BBTS, keyed for the synthetic file."""
|
||||
shaka = tmp_path / "packager"
|
||||
shaka.write_bytes(b"..bbts_decryptor.cc..")
|
||||
local_tools(monkeypatch, None, shaka)
|
||||
drm = MonaLisa(kid=UUID(int=1))
|
||||
drm.content_keys[UUID(int=1)] = bbts_sample.KEY.hex()
|
||||
return drm
|
||||
|
||||
|
||||
def test_with_no_tool_the_built_in_decrypter_decrypts_each_segment(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
drm = built_in_drm(monkeypatch, tmp_path)
|
||||
segment = tmp_path / "0.ts"
|
||||
segment.write_bytes(bbts_sample.bbts())
|
||||
|
||||
assert drm.tool is None and drm.supports_segment_decrypt
|
||||
drm.decrypt_segment(segment)
|
||||
|
||||
assert hashlib.sha256(segment.read_bytes()).hexdigest() == bbts_sample.GOLDEN[bbts_sample.HEVC]
|
||||
assert [path.name for path in tmp_path.iterdir() if path.suffix == ".ts"] == ["0.ts"]
|
||||
|
||||
|
||||
def test_with_no_tool_a_whole_track_drm_decrypts_the_merged_file_built_in(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
drm = built_in_drm(monkeypatch, tmp_path)
|
||||
drm._segment_decrypt = False
|
||||
merged = tmp_path / "merged.ts"
|
||||
merged.write_bytes(bbts_sample.bbts())
|
||||
|
||||
assert not drm.supports_segment_decrypt
|
||||
drm.decrypt(merged)
|
||||
|
||||
assert hashlib.sha256(merged.read_bytes()).hexdigest() == bbts_sample.GOLDEN[bbts_sample.HEVC]
|
||||
|
||||
|
||||
def test_a_failed_built_in_run_keeps_the_ciphertext(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, track: Path
|
||||
) -> None:
|
||||
def fail(src: Path, dst: Path, key: bytes) -> None:
|
||||
dst.write_bytes(b"partial")
|
||||
raise OSError("disk full")
|
||||
|
||||
drm = built_in_drm(monkeypatch, tmp_path)
|
||||
monkeypatch.setattr(monalisa, "decrypt_bbts", fail)
|
||||
|
||||
with pytest.raises(OSError, match="disk full"):
|
||||
drm.decrypt(track)
|
||||
assert track.read_bytes() == b"cipher"
|
||||
assert not track.with_name("track_decrypted.mp4").exists()
|
||||
|
||||
|
||||
def test_a_copy_with_only_a_kid_holds_no_key_until_one_arrives() -> None:
|
||||
drm = MonaLisa(kid=UUID(int=1).hex)
|
||||
|
||||
assert (drm.kid, drm.kids, drm.key, drm.content_keys) == (UUID(int=1), [UUID(int=1)], None, {})
|
||||
drm.content_keys[UUID(int=1)] = "ab" * 16
|
||||
assert drm.key_pair == f"{UUID(int=1).hex}:{'ab' * 16}"
|
||||
|
||||
|
||||
def test_no_ticket_and_no_kid_is_refused() -> None:
|
||||
with pytest.raises(MonaLisa.Exceptions.TicketNotFound):
|
||||
MonaLisa()
|
||||
|
||||
Reference in New Issue
Block a user