fix(drm): keep the input when a cbcs, ClearKey, stripe or segment decrypt fails

This commit is contained in:
imSp4rky
2026-11-28 18:27:40 -07:00
parent 79cc16d290
commit 5a2680d63d
8 changed files with 214 additions and 36 deletions
+33
View File
@@ -45,6 +45,39 @@ def test_decrypt_round_trip(tmp_path: Path) -> None:
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_chunk(monkeypatch: pytest.MonkeyPatch) -> None:
def fail(self: BlowfishStripe, chunk: bytes) -> bytes:
raise ValueError("bad chunk")
monkeypatch.setattr(BlowfishStripe, "decrypt_chunk", fail)
@pytest.mark.parametrize(
("break_step", "raises"),
[pytest.param(fail_chunk, ValueError, id="decrypt"), 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.flac"
path.write_bytes(stripe_encrypt(os.urandom(CHUNK * 4)))
original = path.read_bytes()
break_step(monkeypatch)
with pytest.raises(raises):
BlowfishStripe(KEY).decrypt(path)
assert path.read_bytes() == original
assert list(tmp_path.iterdir()) == [path]
def test_short_trailing_chunk_untouched(tmp_path: Path) -> None:
# the last stripe starts at CHUNK * 3 and holds only 100 bytes, so it stays clear
plain = os.urandom(CHUNK * 3 + 100)