"""A remote session's licensing mode: one value that the server decides at session create and the client adopts.""" import logging from types import SimpleNamespace import pytest from unshackle.core.api import handlers from unshackle.core.api.key_policy import KeyPolicy from unshackle.core.remote_protocol import LicensingMode, Licensor from unshackle.core.remote_service import RemoteService 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", "cdm_relay": True}, {}, SERVER_CDM), ((False, None), {"cdm_type": "widevine", "cdm_relay": True}, {"server_vault": True}, CLIENT_DEVICE), ((False, None), {"cdm_type": "widevine", "cdm_relay": True}, {}, LicensingMode(Licensor.CLIENT_DEVICE)), ((False, 1080), {"cdm_type": "widevine"}, {}, LicensingMode(Licensor.CLIENT_DEVICE, 1080)), ((False, None), {"cdm_type": "widevine"}, {"server_vault": True}, NO_DEVICE), ((False, None), {}, {"server_vault": True}, NO_DEVICE), ], ids=["server-cdm", "relay-vault", "relay", "capped-old-client", "old-client", "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 @pytest.mark.parametrize( ("held", "request_", "reply", "mode", "fallback"), [ (None, None, True, LicensingMode(Licensor.SERVER_CDM), False), (None, True, False, LicensingMode(Licensor.CLIENT_DEVICE), True), (None, False, True, LicensingMode(Licensor.CLIENT_DEVICE), False), (SERVER_CDM, None, False, LicensingMode(Licensor.CLIENT_DEVICE, 1080), True), (LENT_DEVICE, None, False, LENT_DEVICE, False), (SERVER_CDM, None, True, SERVER_CDM, False), ], ids=["older-server", "older-server-refuses", "declined", "reopen-on-older-server", "lent", "same"], ) def test_the_tracks_reply_settles_the_mode_an_older_server_left_out(held, request_, reply, mode, fallback, caplog): service = object.__new__(RemoteService) service.__dict__.update( licensing=held, _server_cdm_request=request_, _tracks_by_title={}, _chapters_by_title={}, _title_alias={}, _sample_decrypt_tracks=set(), _session_id="s", service_tag="EXAMPLE", ctx=SimpleNamespace(obj=None), log=logging.getLogger("test"), client=SimpleNamespace(post=lambda path, body: {"server_cdm": reply}, probe=lambda endpoint: {}), drain_server_logs=lambda: None, ) service.session = SimpleNamespace(headers={}) with caplog.at_level(logging.INFO): service.get_tracks(SimpleNamespace(id="t1")) assert service.licensing == mode assert ("falling back to the local CDM" in caplog.text) is fallback