mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-05 23:45:07 +00:00
88 lines
3.5 KiB
Python
88 lines
3.5 KiB
Python
"""A remote session's licensing mode: one value that the server decides at session create and the client adopts."""
|
|
|
|
import pytest
|
|
|
|
from unshackle.core.api import handlers
|
|
from unshackle.core.api.key_policy import KeyPolicy
|
|
from unshackle.core.remote_protocol import LicensingMode, Licensor
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
SERVER_CDM = LicensingMode(Licensor.SERVER_CDM, 1080)
|
|
CLIENT_DEVICE = LicensingMode(Licensor.CLIENT_DEVICE, server_vault=True)
|
|
LENT_DEVICE = LicensingMode(Licensor.LENT_DEVICE)
|
|
NO_DEVICE = LicensingMode(Licensor.NO_DEVICE)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("mode", "answers"),
|
|
[
|
|
(SERVER_CDM, {"server_cdm", "keys_from_server", "reads_server_keys"}),
|
|
(CLIENT_DEVICE, {"client_device", "client_identity", "vault_only", "reads_server_keys", "vault_first"}),
|
|
(LENT_DEVICE, {"server_device", "client_device", "vault_only", "keys_from_server", "local_vaults_only"}),
|
|
(NO_DEVICE, {"vault_only"}),
|
|
],
|
|
ids=["server-cdm", "client-device", "lent-device", "no-device"],
|
|
)
|
|
def test_each_mode_answers_the_licensing_questions(mode, answers):
|
|
questions = (
|
|
"server_cdm",
|
|
"server_device",
|
|
"client_device",
|
|
"client_identity",
|
|
"vault_only",
|
|
"keys_from_server",
|
|
"reads_server_keys",
|
|
"vault_first",
|
|
"local_vaults_only",
|
|
)
|
|
assert {q for q in questions if getattr(mode, q)} == answers
|
|
|
|
|
|
@pytest.mark.parametrize("licensor", [Licensor.SERVER_CDM, Licensor.NO_DEVICE])
|
|
def test_only_a_client_device_session_reads_the_server_vault(licensor):
|
|
with pytest.raises(ValueError, match="cannot read the server vault"):
|
|
LicensingMode(licensor, server_vault=True)
|
|
|
|
|
|
def test_only_a_client_device_session_can_take_the_lent_device():
|
|
assert CLIENT_DEVICE.lent() == LicensingMode(Licensor.LENT_DEVICE, server_vault=True)
|
|
with pytest.raises(ValueError, match="no client device"):
|
|
SERVER_CDM.lent()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("mode", "adopted"),
|
|
[
|
|
(SERVER_CDM, SERVER_CDM),
|
|
(CLIENT_DEVICE, CLIENT_DEVICE),
|
|
(CLIENT_DEVICE.lent(), CLIENT_DEVICE.lent()),
|
|
(LicensingMode(Licensor.CLIENT_DEVICE, 1080), LicensingMode(Licensor.CLIENT_DEVICE, 1080)),
|
|
(NO_DEVICE, LicensingMode(Licensor.CLIENT_DEVICE)),
|
|
],
|
|
)
|
|
def test_the_client_reads_the_mode_back_from_the_older_fields(mode, adopted):
|
|
fields = mode.created_fields()
|
|
assert set(fields) == {"server_cdm", "server_cdm_max_height", "server_vault", "server_device"}
|
|
assert LicensingMode.from_created({"session_id": "s", **fields}) == adopted
|
|
|
|
|
|
def test_an_older_server_reply_with_vault_and_server_cdm_is_a_server_cdm_session():
|
|
assert LicensingMode.from_created({"server_cdm": True, "server_vault": True}) == LicensingMode(Licensor.SERVER_CDM)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("choice", "data", "policy", "mode"),
|
|
[
|
|
((True, 1080), {"cdm_type": "widevine"}, {}, SERVER_CDM),
|
|
((False, None), {"cdm_type": "widevine"}, {"server_vault": True}, CLIENT_DEVICE),
|
|
((False, None), {"cdm_type": "widevine"}, {}, LicensingMode(Licensor.CLIENT_DEVICE)),
|
|
((False, 1080), {"cdm_type": "widevine"}, {}, LicensingMode(Licensor.CLIENT_DEVICE, 1080)),
|
|
((False, None), {}, {"server_vault": True}, NO_DEVICE),
|
|
],
|
|
ids=["server-cdm", "relay-vault", "relay", "capped", "no-device"],
|
|
)
|
|
def test_session_create_decides_one_mode(choice, data, policy, mode, monkeypatch):
|
|
monkeypatch.setattr(handlers, "choose_session_cdm", lambda *args: choice)
|
|
assert handlers.session_licensing(KeyPolicy(policy), "EXAMPLE", data) == mode
|