forked from kenzuya/unshackle
refactor(tracks): make complete_track the one finish step for every track kind
This commit is contained in:
@@ -16,6 +16,7 @@ from concurrent.futures import Future
|
||||
from functools import partial
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
@@ -254,9 +255,9 @@ def no_decrypt(monkeypatch, mark: bytes = b"") -> list[tuple[object, bool]]:
|
||||
if mark:
|
||||
path.write_bytes(mark + path.read_bytes())
|
||||
|
||||
for module in (segment_pipeline, hls_module, track_module):
|
||||
for module in (segment_pipeline, hls_module):
|
||||
monkeypatch.setattr(module, "decrypt_track", decrypt_track)
|
||||
monkeypatch.setattr(module, "assert_fragments_decrypted", lambda *a, **kw: None)
|
||||
monkeypatch.setattr(segment_pipeline, "assert_fragments_decrypted", lambda *a, **kw: None)
|
||||
return calls
|
||||
|
||||
|
||||
@@ -509,6 +510,64 @@ def test_track_downloaded_fires_once_on_the_decrypted_file(
|
||||
assert (head == MARK) == encrypted
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"kind, merge_segments",
|
||||
[("dash", True), ("hls", False), ("hls", True), ("ism", True), ("url", False)],
|
||||
ids=["dash", "hls-post-merge", "hls-rolling", "ism", "url"],
|
||||
)
|
||||
def test_a_service_sample_decrypt_runs_before_track_downloaded(server, tmp_path, monkeypatch, kind, merge_segments):
|
||||
monkeypatch.setattr(config.directories, "temp", tmp_path)
|
||||
monkeypatch.setattr(config, "merge_segments", merge_segments)
|
||||
monkeypatch.setattr(segment_pipeline, "assert_fragments_decrypted", lambda *a, **kw: None)
|
||||
entries: list[int] = []
|
||||
|
||||
def decrypt_file(path, key_for):
|
||||
entries.append(key_for(None, SimpleNamespace(index=1)))
|
||||
path.write_bytes(MARK + path.read_bytes())
|
||||
|
||||
monkeypatch.setattr(segment_pipeline.cbcs, "decrypt_file", decrypt_file)
|
||||
order: list[tuple[str, object]] = []
|
||||
|
||||
def downloaded(track):
|
||||
order.append(("downloaded", track.path.read_bytes()[: len(MARK)]))
|
||||
|
||||
def decrypted(track, drm, segment):
|
||||
order.append(("decrypted", drm))
|
||||
|
||||
events.subscribe(events.Types.TRACK_DOWNLOADED, downloaded)
|
||||
events.subscribe(events.Types.TRACK_DECRYPTED, decrypted)
|
||||
try:
|
||||
make_track(server, kind, encrypted=False).download(
|
||||
requests.Session(),
|
||||
partial(lambda *a, **kw: None),
|
||||
progress=partial(lambda **kw: None),
|
||||
sample_decrypter=lambda entry: f"key{entry}",
|
||||
)
|
||||
finally:
|
||||
events.unsubscribe(events.Types.TRACK_DOWNLOADED, downloaded)
|
||||
events.unsubscribe(events.Types.TRACK_DECRYPTED, decrypted)
|
||||
|
||||
assert entries == ["key1"]
|
||||
assert order == [("decrypted", None), ("downloaded", MARK)]
|
||||
|
||||
|
||||
def test_a_url_track_decrypts_with_its_track_kid(server, tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(config.directories, "temp", tmp_path)
|
||||
monkeypatch.setattr(track_module.Track, "get_key_id", lambda *a, **kw: KID)
|
||||
monkeypatch.setattr(segment_pipeline, "assert_fragments_decrypted", lambda *a, **kw: None)
|
||||
kids: list[UUID | None] = []
|
||||
|
||||
def decrypt_track(drm, path, licence=None, track_kid=None, decrypt=True, track=None):
|
||||
kids.append(track_kid)
|
||||
|
||||
monkeypatch.setattr(segment_pipeline, "decrypt_track", decrypt_track)
|
||||
make_track(server, "url", encrypted=True).download(
|
||||
requests.Session(), partial(lambda *a, **kw: None), progress=partial(lambda **kw: None)
|
||||
)
|
||||
|
||||
assert kids == [KID]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kind", ["dash", "hls", "ism", "url"])
|
||||
def test_a_failing_track_downloaded_hook_fails_the_track_and_removes_its_file(server, tmp_path, monkeypatch, kind):
|
||||
monkeypatch.setattr(config.directories, "temp", tmp_path)
|
||||
|
||||
@@ -16,6 +16,7 @@ from tests.core.test_all_drm import playready, widevine
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED
|
||||
from unshackle.core.drm import ClearKey, ClearKeyCENC, ClearKeyCTR, MonaLisa, verify
|
||||
from unshackle.core.events import events
|
||||
from unshackle.core.manifests import segment_pipeline
|
||||
from unshackle.core.manifests.segment_pipeline import (
|
||||
RollingMerge,
|
||||
check_merged,
|
||||
@@ -356,6 +357,53 @@ def test_complete_track_after_segment_decryption_only_checks(tmp_path, seen, no_
|
||||
assert [name for name, _ in seen] == ["decrypted"]
|
||||
|
||||
|
||||
def test_complete_track_passes_the_track_kid_and_flags_a_repack(tmp_path, seen, monkeypatch):
|
||||
kids: list[UUID | None] = []
|
||||
monkeypatch.setattr(verify, "DECRYPT_HOOK", lambda drm, path, licence, kid, decrypt, track: kids.append(kid))
|
||||
monkeypatch.setattr(segment_pipeline, "has_encrypted_sample_entry", lambda path: True)
|
||||
ctx = make_ctx(tmp_path)
|
||||
ctx.save_path.write_bytes(b"merged")
|
||||
drm = FakeDRM(seen)
|
||||
track = SimpleNamespace(path=None, drm=[drm], needs_repack=False)
|
||||
|
||||
complete_track(track, ctx, drm, kid=KID) # type: ignore[arg-type]
|
||||
|
||||
assert kids == [KID]
|
||||
assert track.needs_repack
|
||||
|
||||
|
||||
def test_complete_track_runs_the_service_sample_decrypter(tmp_path, seen, monkeypatch):
|
||||
asked: list[int] = []
|
||||
|
||||
def decrypt_file(path, key_for):
|
||||
key_for(None, SimpleNamespace(index=2))
|
||||
path.write_bytes(b"clear:" + path.read_bytes())
|
||||
|
||||
monkeypatch.setattr(segment_pipeline.cbcs, "decrypt_file", decrypt_file)
|
||||
ctx = make_ctx(tmp_path, sample_decrypter=lambda entry: asked.append(entry) or b"k" * 16)
|
||||
ctx.save_path.write_bytes(b"merged")
|
||||
track = SimpleNamespace(path=None, drm=None, needs_repack=False)
|
||||
|
||||
complete_track(track, ctx, None) # type: ignore[arg-type]
|
||||
|
||||
assert asked == [2]
|
||||
assert seen == [("decrypted", None)]
|
||||
assert ctx.save_path.read_bytes() == b"clear:merged"
|
||||
assert ctx.statuses == ["Decrypting", "Decrypted", "Downloaded"]
|
||||
assert not track.needs_repack
|
||||
|
||||
|
||||
def test_complete_track_skips_the_sample_decrypter_after_a_cancel(tmp_path, seen):
|
||||
ctx = make_ctx(tmp_path, sample_decrypter=lambda entry: pytest.fail("decrypter asked after a cancel"))
|
||||
ctx.save_path.write_bytes(b"partial")
|
||||
DOWNLOAD_CANCELLED.set()
|
||||
|
||||
complete_track(SimpleNamespace(path=None, drm=None), ctx, None) # type: ignore[arg-type]
|
||||
|
||||
assert seen == []
|
||||
assert ctx.save_path.read_bytes() == b"partial"
|
||||
|
||||
|
||||
def test_complete_track_without_drm_skips_the_decryption(tmp_path, seen):
|
||||
ctx = make_ctx(tmp_path)
|
||||
ctx.save_path.write_bytes(b"merged")
|
||||
|
||||
@@ -95,9 +95,24 @@ def test_the_source_defaults_leave_a_track_alone() -> None:
|
||||
assert svc.get_playready_license(challenge=b"", title=None, track=None) is None # type: ignore[arg-type]
|
||||
with pytest.raises(NotImplementedError):
|
||||
svc.get_sample_decrypter(None, 1) # type: ignore[arg-type]
|
||||
assert svc.sample_decrypter(None) is None # type: ignore[arg-type]
|
||||
svc.close()
|
||||
|
||||
|
||||
def test_a_source_that_decrypts_samples_hands_its_decrypter_for_the_track() -> None:
|
||||
class Decrypting(ExampleService):
|
||||
def decrypts_samples(self, track: Any) -> bool:
|
||||
return track == "EXAMPLE"
|
||||
|
||||
def get_sample_decrypter(self, track: Any, entry: int) -> bytes:
|
||||
return f"{track}:{entry}".encode()
|
||||
|
||||
svc = object.__new__(Decrypting)
|
||||
decrypter = svc.sample_decrypter("EXAMPLE") # type: ignore[arg-type]
|
||||
assert decrypter is not None and decrypter(2) == b"EXAMPLE:2"
|
||||
assert svc.sample_decrypter("OTHER") is None # type: ignore[arg-type]
|
||||
|
||||
|
||||
def test_a_playready_stub_names_every_kid() -> None:
|
||||
stub = drm_stub("playready", KIDS)
|
||||
assert isinstance(stub, PlayReady)
|
||||
|
||||
@@ -190,15 +190,14 @@ def test_downloaders_route_through_the_hook(monkeypatch: pytest.MonkeyPatch) ->
|
||||
"""Every real decrypt site calls decrypt_track, so the dl hook sees it."""
|
||||
import inspect
|
||||
|
||||
from unshackle.core.manifests import dash, hls, ism, segment_pipeline
|
||||
from unshackle.core.manifests import dash, hls, ism, segment_pipeline, url
|
||||
from unshackle.core.tracks import track
|
||||
|
||||
for module in (segment_pipeline, hls, track):
|
||||
for module in (segment_pipeline, hls):
|
||||
assert "decrypt_track(" in inspect.getsource(module), module.__name__
|
||||
# DASH and ISM decrypt through the pipeline's complete_track, which calls decrypt_track
|
||||
for module in (dash, ism):
|
||||
for module in (dash, hls, ism, url):
|
||||
assert "complete_track(" in inspect.getsource(module), module.__name__
|
||||
for module in (dash, hls, ism, segment_pipeline, track):
|
||||
for module in (dash, hls, ism, segment_pipeline, track, url):
|
||||
src = inspect.getsource(module)
|
||||
assert "drm.decrypt(save_path)" not in src and "drm.decrypt(merged_path)" not in src, module.__name__
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.tracks.track import has_encrypted_sample_entry
|
||||
from unshackle.core.drm.verify import has_encrypted_sample_entry
|
||||
|
||||
|
||||
def box(fmt: bytes, payload: bytes = b"") -> bytes:
|
||||
|
||||
Reference in New Issue
Block a user