Files
unshackle/tests/core/test_cbcs_decrypt.py
T

65 lines
2.0 KiB
Python

"""Tests for the in-process cbcs decrypt (``unshackle.core.drm.cbcs.decrypt_file``)."""
from __future__ import annotations
import struct
from pathlib import Path
from typing import Any
import pytest
from tests.core.test_kid_windows import A, box, full, movie
from unshackle.core.drm import cbcs
def cbcs_track(path: Path) -> bytes:
"""Write a cbcs track with one protected sample entry and no fragments, and return its bytes."""
tenc = full(b"tenc", b"\x00\x00\x01\x00" + A.bytes + b"\x10" + bytes(16), version=1)
schm = full(b"schm", b"cbcs" + struct.pack(">I", 0x10000))
entry = box(b"encv", bytes(78), box(b"sinf", box(b"frma", b"avc1"), schm, box(b"schi", tenc)))
data = movie(entry) + box(b"mdat", bytes(16))
path.write_bytes(data)
return data
def test_decrypt_replaces_the_input(tmp_path: Path) -> None:
path = tmp_path / "track.mp4"
cbcs_track(path)
cbcs.decrypt_file(path, lambda track, entry: bytes(16))
assert b"encv" not in path.read_bytes()
assert list(tmp_path.iterdir()) == [path]
def refuse_replace(monkeypatch: pytest.MonkeyPatch) -> None:
def refuse(self: Path, target: Path) -> None:
raise PermissionError(target)
monkeypatch.setattr(Path, "replace", refuse)
def fail_rewrite(monkeypatch: pytest.MonkeyPatch) -> None:
def fail(*args: Any) -> None:
raise ValueError("bad moov")
monkeypatch.setattr(cbcs, "strip_moov_protection", fail)
@pytest.mark.parametrize(
("break_step", "raises"),
[pytest.param(fail_rewrite, ValueError, id="rewrite"), pytest.param(refuse_replace, PermissionError, id="replace")],
)
def test_a_failed_decrypt_keeps_the_input(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, break_step: Any, raises: type[BaseException]
) -> None:
path = tmp_path / "track.mp4"
original = cbcs_track(path)
break_step(monkeypatch)
with pytest.raises(raises):
cbcs.decrypt_file(path, lambda track, entry: bytes(16))
assert path.read_bytes() == original
assert list(tmp_path.iterdir()) == [path]