forked from kenzuya/unshackle
refactor(api): move the server CDM licence cap into one module that measures tracks by the -q rule
This commit is contained in:
@@ -108,20 +108,9 @@ def test_the_named_services_are_the_global_list_else_every_keys_list():
|
||||
assert api_keys({"users": users}) == ["a", "b", "c", "d"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("setting", "service", "cap"),
|
||||
[
|
||||
(1080, "EXAMPLE1", 1080),
|
||||
({"example1": 720, "default": 1080}, "EXAMPLE1", 720),
|
||||
({"example1": 720, "default": 1080}, "EXAMPLE2", 1080),
|
||||
({"EXAMPLE1": 1080}, "EXAMPLE2", None),
|
||||
({"EXAMPLE1": None, "default": 1080}, "EXAMPLE1", 1080),
|
||||
(None, "EXAMPLE1", None),
|
||||
(True, "EXAMPLE1", None),
|
||||
],
|
||||
)
|
||||
def test_the_height_cap_per_service(setting, service, cap):
|
||||
assert KeyPolicy({"server_cdm_max_height": setting}).max_height(service) == cap
|
||||
def test_the_policy_hands_over_the_cap_of_the_service():
|
||||
policy = KeyPolicy({"server_cdm_max_height": {"example1": 720, "default": 1080}})
|
||||
assert (policy.max_height("EXAMPLE1"), policy.max_height(None)) == (720, 1080)
|
||||
|
||||
|
||||
def test_a_key_with_no_entry_gets_every_server_device():
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
"""The licence cap module: the setting, the track measure, and the request and track questions."""
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.api import licence_cap
|
||||
from unshackle.core.api.errors import APIError
|
||||
from unshackle.core.remote_protocol import APIErrorCode
|
||||
from unshackle.core.tracks import Audio, Video
|
||||
from unshackle.core.tracks.tracks import at_resolution
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
CAP_DETAILS = {"reason": "server_cdm_max_height", "max_height": 1080}
|
||||
|
||||
|
||||
def video(height, width=None):
|
||||
track = Video.__new__(Video)
|
||||
track.height = height
|
||||
track.width = width
|
||||
return track
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("setting", "service", "cap"),
|
||||
[
|
||||
(1080, "EXAMPLE1", 1080),
|
||||
({"example1": 720, "default": 1080}, "EXAMPLE1", 720),
|
||||
({"example1": 720, "default": 1080}, "EXAMPLE2", 1080),
|
||||
({"example1": 720, "default": 1080}, None, 1080),
|
||||
({"EXAMPLE1": 1080}, "EXAMPLE2", None),
|
||||
({"EXAMPLE1": None, "default": 1080}, "EXAMPLE1", 1080),
|
||||
(None, "EXAMPLE1", None),
|
||||
(True, "EXAMPLE1", None),
|
||||
],
|
||||
)
|
||||
def test_the_cap_per_service(setting, service, cap):
|
||||
assert licence_cap.cap_for(setting, service) == cap
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("value", "problem"),
|
||||
[
|
||||
("1080", "u.server_cdm_max_height must be"),
|
||||
(True, "u.server_cdm_max_height must be"),
|
||||
({"EXAMPLE": -1}, "u.server_cdm_max_height values"),
|
||||
(None, None),
|
||||
(1080, None),
|
||||
({"EXAMPLE1": 720, "default": None}, None),
|
||||
],
|
||||
)
|
||||
def test_the_setting_check(value, problem):
|
||||
error = licence_cap.setting_error("u", value)
|
||||
assert error is None if problem is None else problem in error
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("track", "refused"),
|
||||
[
|
||||
(video(2160), True),
|
||||
(video(1080), False),
|
||||
(video(None), False),
|
||||
(Audio.__new__(Audio), False),
|
||||
(video(1088, 2048), False),
|
||||
(video(1600, 3840), True),
|
||||
(video(None, 3840), True),
|
||||
(video(1080, 2560), False),
|
||||
(video(1080, 1920), False),
|
||||
],
|
||||
)
|
||||
def test_a_track_above_the_cap_gets_no_live_licence(track, refused):
|
||||
assert (licence_cap.track_refusal(1080, track) is not None) is refused
|
||||
|
||||
|
||||
def test_no_cap_refuses_no_track():
|
||||
assert licence_cap.track_refusal(None, video(4320)) is None
|
||||
|
||||
|
||||
def test_the_track_refusal_gives_the_client_the_cap():
|
||||
refusal = licence_cap.track_refusal(1080, video(2160))
|
||||
assert refusal.error_code is APIErrorCode.SERVER_CDM_CAPPED
|
||||
assert refusal.details == CAP_DETAILS
|
||||
assert "2160p track" in refusal.message
|
||||
|
||||
|
||||
@pytest.mark.parametrize("size", [(1088, 2048), (1188, 2112), (800, 1920), (1080, 2560), (1080, 1920), (None, 1920)])
|
||||
def test_a_track_that_the_cap_quality_selects_gets_a_live_licence(size):
|
||||
track = video(*size)
|
||||
assert at_resolution(track, 1080)
|
||||
assert licence_cap.track_refusal(1080, track) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("cap", "quality", "has_device", "server_cdm"),
|
||||
[
|
||||
(None, [2160], False, True),
|
||||
(1080, [1080], False, True),
|
||||
(1080, [], False, True),
|
||||
(1080, [2160, 1080], True, False),
|
||||
(1080, ["2160p"], True, False),
|
||||
],
|
||||
)
|
||||
def test_the_session_question(cap, quality, has_device, server_cdm):
|
||||
assert licence_cap.session_fits(cap, "EXAMPLE1", quality, has_device) is server_cdm
|
||||
|
||||
|
||||
def test_a_session_above_the_cap_without_a_device_is_refused():
|
||||
with pytest.raises(APIError) as ei:
|
||||
licence_cap.session_fits(1080, "EXAMPLE1", [2160], False)
|
||||
assert ei.value.error_code is APIErrorCode.SERVER_CDM_CAPPED
|
||||
assert ei.value.details == CAP_DETAILS
|
||||
|
||||
|
||||
@pytest.mark.parametrize("quality", [["best"], [-1], [0], [1080, 0]])
|
||||
@pytest.mark.parametrize("cap", [None, 1080])
|
||||
def test_a_bad_quality_is_invalid_parameters(quality, cap):
|
||||
with pytest.raises(APIError) as ei:
|
||||
licence_cap.session_fits(cap, "EXAMPLE1", quality, True)
|
||||
assert ei.value.error_code is APIErrorCode.INVALID_PARAMETERS
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"params",
|
||||
[
|
||||
{"quality": [2160]},
|
||||
{"quality": []},
|
||||
{},
|
||||
{"quality": [1080], "best_available": True},
|
||||
{"quality": [1080], "range": "HYBRID"},
|
||||
{"quality": [1080], "range": ["SDR", "hybrid"]},
|
||||
{"quality": [1080], "range": [Video.Range.HYBRID]},
|
||||
],
|
||||
)
|
||||
def test_a_job_that_could_pass_the_cap_is_refused(params):
|
||||
with pytest.raises(APIError) as ei:
|
||||
licence_cap.check_job(1080, params)
|
||||
assert ei.value.error_code is APIErrorCode.SERVER_CDM_CAPPED
|
||||
assert ei.value.details == CAP_DETAILS
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("cap", "params"),
|
||||
[
|
||||
(1080, {"quality": [1080]}),
|
||||
(1080, {"quality": [720, 1080], "range": ["HDR10"]}),
|
||||
(None, {"best_available": True}),
|
||||
],
|
||||
)
|
||||
def test_a_job_inside_the_cap_passes(cap, params):
|
||||
licence_cap.check_job(cap, params)
|
||||
@@ -108,19 +108,11 @@ def test_above_the_cap_without_a_device_is_refused_at_create():
|
||||
assert ei.value.details == {"reason": "server_cdm_max_height", "max_height": 1080}
|
||||
|
||||
|
||||
def test_bad_quality_is_invalid_parameters():
|
||||
with pytest.raises(APIError) as ei:
|
||||
handlers.choose_session_cdm(KeyPolicy(USERS["flat"]), "EXAMPLE1", {"quality": ["best"]})
|
||||
assert ei.value.error_code is APIErrorCode.INVALID_PARAMETERS
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("session", "track", "refused"),
|
||||
[
|
||||
(session_entry(server_cdm_max_height=1080), video(2160), True),
|
||||
(session_entry(server_cdm_max_height=1080), video(1080), False),
|
||||
(session_entry(server_cdm_max_height=1080), video(None), False),
|
||||
(session_entry(server_cdm_max_height=1080), Audio.__new__(Audio), False),
|
||||
(session_entry(server_cdm_max_height=None), video(2160), False),
|
||||
(session_entry(server_cdm=False, server_cdm_max_height=1080), video(720), True),
|
||||
(session_entry(), video(2160), False),
|
||||
@@ -167,7 +159,7 @@ def test_a_vault_miss_raises_the_refusal_before_the_device_loads(widevine_env, m
|
||||
assert ei.value is refusal
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("quality", "allowed"), [([1080], True), ([720, 1080], True), ([2160], False), ([], False)])
|
||||
@pytest.mark.parametrize(("quality", "allowed"), [([1080], True), ([2160], False)])
|
||||
def test_download_jobs_stay_inside_the_cap(quality, allowed):
|
||||
params = {"service": "EXAMPLE1", "quality": quality}
|
||||
if allowed:
|
||||
@@ -239,30 +231,6 @@ def test_a_server_vault_miss_without_the_local_drm_system_fails_at_once():
|
||||
assert "-q" not in ei.value.message
|
||||
|
||||
|
||||
def test_an_ultrawide_track_is_measured_as_the_smallest_quality_that_selects_it():
|
||||
"""-q matches a height or a 16:9 width, so a track passes the cap when either one is inside it."""
|
||||
session = session_entry(server_cdm_max_height=1080)
|
||||
assert handlers.live_licence_refusal(session, video(1600, 3840)) is not None
|
||||
assert handlers.live_licence_refusal(session, video(None, 3840)) is not None
|
||||
assert handlers.live_licence_refusal(session, video(1080, 2560)) is None # -q 1080 selects it
|
||||
assert handlers.live_licence_refusal(session, video(1080, 1920)) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("quality", [[-1], [0], [1080, 0]])
|
||||
def test_a_height_of_zero_or_less_is_invalid_parameters(quality):
|
||||
with pytest.raises(APIError) as ei:
|
||||
job_admission.enforce_download_gates({"service": "EXAMPLE1", "quality": quality}, request("flat"))
|
||||
assert ei.value.error_code is APIErrorCode.INVALID_PARAMETERS
|
||||
|
||||
|
||||
def test_a_capped_download_job_cannot_set_best_available():
|
||||
with pytest.raises(APIError) as ei:
|
||||
job_admission.enforce_download_gates(
|
||||
{"service": "EXAMPLE1", "quality": [1080], "best_available": True}, request("flat")
|
||||
)
|
||||
assert ei.value.error_code is APIErrorCode.SERVER_CDM_CAPPED
|
||||
|
||||
|
||||
def test_the_download_gate_reads_the_serve_dl_defaults(monkeypatch):
|
||||
serve = {"users": USERS, "quality": [1080], "best_available": True}
|
||||
monkeypatch.setattr(handlers.config, "serve", serve, raising=False)
|
||||
@@ -280,19 +248,6 @@ def test_the_download_gate_reads_the_serve_dl_defaults(monkeypatch):
|
||||
admit()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("hdr_range", ["HYBRID", ["SDR", "hybrid"], [Video.Range.HYBRID]])
|
||||
def test_a_capped_download_job_cannot_ask_for_hybrid(hdr_range):
|
||||
"""HYBRID always keeps the lowest DV track, whatever the quality."""
|
||||
with pytest.raises(APIError) as ei:
|
||||
job_admission.enforce_download_gates(
|
||||
{"service": "EXAMPLE1", "quality": [1080], "range": hdr_range}, request("flat")
|
||||
)
|
||||
assert ei.value.error_code is APIErrorCode.SERVER_CDM_CAPPED
|
||||
job_admission.enforce_download_gates(
|
||||
{"service": "EXAMPLE1", "quality": [1080], "range": ["HDR10"]}, request("flat")
|
||||
)
|
||||
|
||||
|
||||
def test_a_service_click_default_stays_out_of_the_download_gate(monkeypatch):
|
||||
"""A service's own option is not the dl option of the same name, so its default never trips the gate."""
|
||||
import asyncio
|
||||
@@ -437,14 +392,16 @@ def test_batch_restores_the_manifest_drm_on_a_track_refused_twice(licence_env, i
|
||||
|
||||
def test_batch_serves_a_capped_track_from_a_vault_hit_on_its_init_pssh(licence_env, init_segment):
|
||||
licence_env.vault["init"] = {INIT_KID.hex: "v"}
|
||||
track = media(Video, "vid", [_PSSHDrm("manifest", [KID])], height=2160, get_key_id=lambda init: INIT_KID)
|
||||
track = media(
|
||||
Video, "vid", [_PSSHDrm("manifest", [KID])], height=2160, width=3840, get_key_id=lambda init: INIT_KID
|
||||
)
|
||||
payload = batch(licence_env, track)
|
||||
assert "capped_tracks" not in payload
|
||||
assert payload["keys"] == {"vid": {INIT_KID.hex: "v"}}
|
||||
|
||||
|
||||
def test_single_licence_refuses_a_capped_track_with_details(licence_env):
|
||||
track = media(Video, "vid", [_PSSHDrm("manifest", [KID])], height=2160)
|
||||
track = media(Video, "vid", [_PSSHDrm("manifest", [KID])], height=2160, width=3840)
|
||||
licence_env.session.tracks = {"vid": track}
|
||||
data = {"mode": "server_cdm", "track_id": "vid", "drm_type": "widevine"}
|
||||
with pytest.raises(APIError) as ei:
|
||||
|
||||
Reference in New Issue
Block a user