fix(subprocess): name the tool, exit and reason in external-tool errors

This commit is contained in:
imSp4rky
2026-11-28 14:43:29 -07:00
parent 7856d53950
commit 0bd5c4de4f
12 changed files with 305 additions and 165 deletions
+29 -30
View File
@@ -14,6 +14,7 @@ from tests.core.test_kid_windows import A, B, box, clear_audio, encv, moof, movi
from tests.core.test_zero_kid_fallback import _playready, _widevine
from tests.test_segment_decrypt import read_argv
from unshackle.core import binaries
from unshackle.core.api.errors import categorize_exception
from unshackle.core.config import config
from unshackle.core.drm import ClearKeyCENC
from unshackle.core.drm import decrypters as decrypters_module
@@ -21,6 +22,7 @@ from unshackle.core.drm.decrypters import mp4decrypt_or_shaka
from unshackle.core.drm.key_args import shaka_noise, track_key_args
from unshackle.core.drm.segment_decrypt import SegmentDecrypter, can_use
from unshackle.core.drm.verify import kidless_track_kids
from unshackle.core.remote_protocol import APIErrorCode
KEY = "b5df2ae3f63b8dda9049d6e41422e075"
OTHER_KEY = "11" * 16
@@ -251,59 +253,55 @@ class FakePackager:
@pytest.fixture
def fake_packager(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> list[str]:
"""Route shaka-packager to ``FakePackager`` and collect what the console prints."""
printed: list[str] = []
def fake_packager(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Route shaka-packager to ``FakePackager``."""
FakePackager.code, FakePackager.log = 0, ""
monkeypatch.setattr(decrypters_module.subprocess, "Popen", FakePackager)
monkeypatch.setattr(binaries, "ShakaPackager", Path("packager"))
monkeypatch.setattr(config.directories, "temp", tmp_path)
monkeypatch.setattr(decrypters_module.console, "log", lambda text: printed.append(str(text)))
return printed
def shaka_run(tmp_path: Path, make: Callable[[], Any], raises: type[BaseException]) -> None:
"""Decrypt a one-key track of ``make()`` through ``FakePackager`` and expect ``raises``."""
def shaka_run(tmp_path: Path, make: Callable[[], Any], raises: type[BaseException]) -> BaseException:
"""Decrypt a one-key track of ``make()`` through ``FakePackager`` and return the ``raises`` it raised."""
path = tmp_path / "track.mp4"
path.write_bytes(b"encrypted")
drm = make()
drm.content_keys[A] = KEY
with pytest.raises(raises):
with pytest.raises(raises) as caught:
drm.decrypt_with_shaka_packager(path)
assert path.read_bytes() == b"encrypted"
return caught.value
@pytest.mark.parametrize("make", DRMS)
def test_every_drm_passes_the_fake_clock(tmp_path: Path, fake_packager: list[str], make: Callable[[], Any]) -> None:
@pytest.mark.usefixtures("fake_packager")
def test_every_drm_passes_the_fake_clock(tmp_path: Path, make: Callable[[], Any]) -> None:
shaka_run(tmp_path, make, FileNotFoundError)
assert "--use_fake_clock_for_muxer" in FakePackager.argv
def test_clearkey_gives_no_zero_kid_fallback(tmp_path: Path, fake_packager: list[str]) -> None:
@pytest.mark.usefixtures("fake_packager")
def test_clearkey_gives_no_zero_kid_fallback(tmp_path: Path) -> None:
shaka_run(tmp_path, lambda: ClearKeyCENC(kids=[A]), FileNotFoundError)
assert FakePackager.argv[FakePackager.argv.index("--keys") + 1] == f"label=0:key_id={A.hex}:key={KEY}"
@pytest.mark.parametrize(
("make", "label"),
[
pytest.param(lambda: _widevine(A), "Widevine", id="widevine"),
pytest.param(lambda: _playready(A), "PlayReady", id="playready"),
pytest.param(lambda: ClearKeyCENC(kids=[A]), "ClearKey", id="clearkey"),
],
)
def test_a_shaka_failure_logs_under_the_drm_label(
tmp_path: Path, fake_packager: list[str], make: Callable[[], Any], label: str
) -> None:
FakePackager.code, FakePackager.log = 1, "[1103/153702:ERROR:main.cc(1)] Bad key\n"
shaka_run(tmp_path, make, subprocess.CalledProcessError)
assert len(fake_packager) == 1 and f"[{label}]: " in fake_packager[0] and "Bad key" in fake_packager[0]
@pytest.mark.usefixtures("fake_packager")
@pytest.mark.parametrize("make", DRMS)
def test_a_skipped_shaka_stream_keeps_the_input(
tmp_path: Path, fake_packager: list[str], make: Callable[[], Any]
) -> None:
def test_a_shaka_failure_names_the_reason_and_keeps_the_key(tmp_path: Path, make: Callable[[], Any]) -> None:
FakePackager.code = 2
FakePackager.log = (
"I1103 15:37:02 demuxer.cc(89)] Demuxer::Run()\n[1103/153702:ERROR:main.cc(1)] Packaging Error: 5\n"
)
error = shaka_run(tmp_path, make, subprocess.CalledProcessError)
assert str(error).startswith("shaka-packager failed (exit 2): Packaging Error: 5; command: ")
assert KEY in str(error) and "Demuxer" not in str(error)
assert categorize_exception(error).error_code == APIErrorCode.DRM_ERROR
@pytest.mark.usefixtures("fake_packager")
@pytest.mark.parametrize("make", DRMS)
def test_a_skipped_shaka_stream_keeps_the_input(tmp_path: Path, make: Callable[[], Any]) -> None:
FakePackager.log = (
"[1128/120000:INFO:mp4_muxer.cc(1)] Skip stream 'track_decrypted.mp4' which does not contain any sample.\n"
)
@@ -320,7 +318,8 @@ def test_a_skipped_shaka_stream_keeps_the_input(
assert not stray.exists()
def test_a_shaka_ctrl_c_exit_is_a_keyboard_interrupt(tmp_path: Path, fake_packager: list[str]) -> None:
@pytest.mark.usefixtures("fake_packager")
def test_a_shaka_ctrl_c_exit_is_a_keyboard_interrupt(tmp_path: Path) -> None:
FakePackager.code = 0xC000013A
shaka_run(tmp_path, lambda: _widevine(A), KeyboardInterrupt)