from __future__ import annotations from pathlib import Path from typing import Any import click import pytest import rich_click from click.testing import CliRunner from pyplayready import main as prd_upstream from pywidevine import main as wvd_upstream from unshackle.commands import prd as prd_module from unshackle.commands import wvd as wvd_module from unshackle.core.config import config def record(monkeypatch: pytest.MonkeyPatch, module: Any, name: str) -> list[dict[str, Any]]: """Swap an upstream command for one with its parameters whose callback records the call.""" real = getattr(module, name) calls: list[dict[str, Any]] = [] fake = click.Command(real.name, params=real.params, callback=lambda **kwargs: calls.append(kwargs)) monkeypatch.setattr(module, name, fake) return calls def names(module: Any, name: str) -> set[str]: return {param.name for param in getattr(module, name).params if param.name} def files(folder: Path, *names: str) -> Path: folder.mkdir(parents=True, exist_ok=True) for name in names: (folder / name).write_bytes(b"x") return folder def test_prd_new_from_a_folder_passes_its_files_and_saves_into_the_folder( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: folder = files(tmp_path / "dev", "zgpriv.dat", "bgroupcert.dat") calls = record(monkeypatch, prd_upstream, "create_device") result = CliRunner().invoke(prd_module.prd, ["new", str(folder)]) assert result.exit_code == 0, result.output assert calls == [ { "group_key": folder / "zgpriv.dat", "protected_group_key": None, "group_certificate": folder / "bgroupcert.dat", "encryption_key": None, "signing_key": None, "output": folder, } ] assert set(calls[0]) == names(prd_upstream, "create_device") def test_prd_new_from_two_paths_passes_the_keys_and_output(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: src = files(tmp_path, "key.dat", "cert.dat", "enc.dat", "sig.dat") monkeypatch.setattr(config.directories, "prds", tmp_path / "PRDs") calls = record(monkeypatch, prd_upstream, "create_device") result = CliRunner().invoke( prd_module.prd, [ "new", str(src / "key.dat"), str(src / "cert.dat"), "-e", str(src / "enc.dat"), "-s", str(src / "sig.dat"), "-o", str(tmp_path / "out.prd"), ], ) default = CliRunner().invoke(prd_module.prd, ["new", str(src / "key.dat"), str(src / "cert.dat")]) assert result.exit_code == 0, result.output assert default.exit_code == 0, default.output assert calls[1]["output"] == tmp_path / "PRDs" assert calls[0]["group_key"] == src / "key.dat" assert calls[0]["group_certificate"] == src / "cert.dat" assert calls[0]["encryption_key"] == src / "enc.dat" assert calls[0]["signing_key"] == src / "sig.dat" assert calls[0]["output"] == tmp_path / "out.prd" @pytest.mark.parametrize( ("args", "message"), [ (["dev"], "Folder must contain zgpriv.dat and bgroupcert.dat"), (["dev", "dev", "dev"], "Provide either a folder path"), (["dev", "-e", "missing.dat"], None), (["dev/zgpriv.dat", "missing.dat"], None), ], ) def test_prd_new_refuses_bad_paths_before_the_upstream_command( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, args: list[str], message: str | None ) -> None: files(tmp_path / "dev", "zgpriv.dat") monkeypatch.chdir(tmp_path) calls = record(monkeypatch, prd_upstream, "create_device") result = CliRunner().invoke(prd_module.prd, ["new", *args]) assert result.exit_code == 2 assert message is None or message in result.output assert calls == [] def test_prd_reprovision_passes_its_arguments(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: device = files(tmp_path, "d.prd") / "d.prd" calls = record(monkeypatch, prd_upstream, "reprovision_device") result = CliRunner().invoke(prd_module.prd, ["reprovision", str(device), "-o", str(tmp_path / "new.prd")]) assert result.exit_code == 0, result.output assert calls == [{"prd_path": device, "encryption_key": None, "signing_key": None, "output": tmp_path / "new.prd"}] assert set(calls[0]) == names(prd_upstream, "reprovision_device") def test_prd_test_sends_the_upstream_content_key_type(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: device = files(tmp_path, "d.prd") / "d.prd" calls = record(monkeypatch, prd_upstream, "test") result = CliRunner().invoke(prd_module.prd, ["test", str(device), "-c", "AESCBC", "-sl", "3000"]) assert result.exit_code == 0, result.output assert calls == [{"device": device, "ckt": "AES128BitCBC", "security_level": "3000"}] ckt = next(param for param in prd_upstream.test.params if param.name == "ckt") assert set(prd_module.CONTENT_KEY_TYPES.values()) == set(ckt.type.choices) assert set(calls[0]) == names(prd_upstream, "test") def test_prd_lists_the_upstream_export_and_inspect_commands() -> None: assert prd_module.prd.commands["export"] is prd_upstream.export_device assert prd_module.prd.commands["inspect"] is prd_upstream.inspect def test_wvd_dump_exports_each_device_and_resolves_a_bare_name_in_the_wvds_directory( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setattr(config.directories, "wvds", files(tmp_path / "WVDs", "example.wvd")) monkeypatch.chdir(tmp_path) other = files(tmp_path / "elsewhere", "other.wvd") / "other.wvd" calls = record(monkeypatch, wvd_upstream, "export_device") result = CliRunner().invoke(wvd_module.wvd, ["dump", "example", str(other), "out"]) assert result.exit_code == 0, result.output assert calls == [ {"wvd_path": tmp_path / "WVDs" / "example.wvd", "out_dir": Path("out")}, {"wvd_path": other, "out_dir": Path("out")}, ] assert set(calls[0]) == names(wvd_upstream, "export_device") def test_wvd_dump_names_a_missing_device_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(config.directories, "wvds", tmp_path / "WVDs") calls = record(monkeypatch, wvd_upstream, "export_device") result = CliRunner().invoke(wvd_module.wvd, ["dump", "missing", "out"], standalone_mode=False) assert isinstance(result.exception, click.UsageError) assert str(tmp_path / "WVDs" / "missing.wvd") in result.exception.message assert calls == [] def test_ctx_invoke_runs_a_command_of_another_package_in_a_sub_context() -> None: seen: list[tuple[str | None, int]] = [] foreign = rich_click.RichCommand( "foreign", params=[click.Option(["--value"], type=int, default=5)], callback=click.pass_context(lambda ctx, value: seen.append((ctx.info_name, value))), ) outer = click.Command("outer", callback=lambda: click.get_current_context().invoke(foreign)) result = CliRunner().invoke(outer) assert result.exit_code == 0, result.output assert seen == [("foreign", 5)]