feat(container): implement songpack pack and secure unpack per spec
This commit is contained in:
99
tests/test_container.py
Normal file
99
tests/test_container.py
Normal file
@ -0,0 +1,99 @@
|
||||
"""Tests for songpack container packing and secure unpacking."""
|
||||
|
||||
import json
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from songsplit.container import (
|
||||
SongpackSecurityError,
|
||||
build_manifest_for_dir,
|
||||
pack_songpack,
|
||||
unpack_songpack,
|
||||
)
|
||||
|
||||
|
||||
def test_build_manifest(tmp_path: Path):
|
||||
song_dir = tmp_path / "song"
|
||||
song_dir.mkdir()
|
||||
(song_dir / "meta.json").write_text('{"schema_version": "1.0"}', encoding="utf-8")
|
||||
stems_dir = song_dir / "stems"
|
||||
stems_dir.mkdir()
|
||||
(stems_dir / "vocals.wav").write_bytes(b"RIFFdummyWAVdata")
|
||||
|
||||
manifest = build_manifest_for_dir(song_dir, variant="full")
|
||||
assert manifest.format == "songpack"
|
||||
assert manifest.variant == "full"
|
||||
assert manifest.audio_included is True
|
||||
paths = [f.path for f in manifest.files]
|
||||
assert "meta.json" in paths
|
||||
assert "stems/vocals.wav" in paths
|
||||
|
||||
manifest_data = build_manifest_for_dir(song_dir, variant="data")
|
||||
assert manifest_data.variant == "data"
|
||||
assert manifest_data.audio_included is False
|
||||
assert "stems/vocals.wav" not in [f.path for f in manifest_data.files]
|
||||
|
||||
|
||||
def test_pack_and_unpack_roundtrip(tmp_path: Path):
|
||||
song_dir = tmp_path / "song"
|
||||
song_dir.mkdir()
|
||||
(song_dir / "meta.json").write_text('{"title": "Test Song"}', encoding="utf-8")
|
||||
(song_dir / "tempo.json").write_text('{"schema_version": "1.0"}', encoding="utf-8")
|
||||
stems_dir = song_dir / "stems"
|
||||
stems_dir.mkdir()
|
||||
(stems_dir / "vocals.wav").write_bytes(b"audio-bytes-vocals")
|
||||
|
||||
tracks_dir = song_dir / "tracks"
|
||||
tracks_dir.mkdir()
|
||||
(tracks_dir / "guitar.json").write_text('{"kind": "strumming"}', encoding="utf-8")
|
||||
|
||||
archive_path = tmp_path / "song.songpack"
|
||||
pack_songpack(song_dir, archive_path)
|
||||
|
||||
assert archive_path.is_file()
|
||||
|
||||
# Check zip properties
|
||||
with zipfile.ZipFile(archive_path, "r") as zf:
|
||||
infolist = zf.infolist()
|
||||
# First entry must be manifest.json
|
||||
assert infolist[0].filename == "manifest.json"
|
||||
# Audio stems must be ZIP_STORED
|
||||
stem_info = zf.getinfo("stems/vocals.wav")
|
||||
assert stem_info.compress_type == zipfile.ZIP_STORED
|
||||
# JSON must be ZIP_DEFLATED
|
||||
meta_info = zf.getinfo("meta.json")
|
||||
assert meta_info.compress_type == zipfile.ZIP_DEFLATED
|
||||
|
||||
# Unpack
|
||||
dest_dir = tmp_path / "unpacked_song"
|
||||
unpack_songpack(archive_path, dest_dir)
|
||||
|
||||
assert (dest_dir / "manifest.json").is_file()
|
||||
assert (dest_dir / "meta.json").is_file()
|
||||
assert (dest_dir / "tracks" / "guitar.json").is_file()
|
||||
assert (dest_dir / "stems" / "vocals.wav").read_bytes() == b"audio-bytes-vocals"
|
||||
|
||||
|
||||
def test_unpack_security_traversal(tmp_path: Path):
|
||||
bad_zip = tmp_path / "traversal.songpack"
|
||||
with zipfile.ZipFile(bad_zip, "w") as zf:
|
||||
zf.writestr("manifest.json", '{"format": "songpack", "schema_version": "1.0", "created": "", "modified": ""}')
|
||||
zf.writestr("../evil.txt", "evil")
|
||||
|
||||
dest = tmp_path / "out"
|
||||
with pytest.raises(SongpackSecurityError, match="Unsafe path"):
|
||||
unpack_songpack(bad_zip, dest)
|
||||
|
||||
|
||||
def test_unpack_security_max_entries(tmp_path: Path):
|
||||
bad_zip = tmp_path / "many_entries.songpack"
|
||||
with zipfile.ZipFile(bad_zip, "w") as zf:
|
||||
zf.writestr("manifest.json", '{"format": "songpack", "schema_version": "1.0", "created": "", "modified": ""}')
|
||||
for i in range(70):
|
||||
zf.writestr(f"tracks/track_{i}.json", "{}")
|
||||
|
||||
dest = tmp_path / "out"
|
||||
with pytest.raises(SongpackSecurityError, match="Too many archive entries"):
|
||||
unpack_songpack(bad_zip, dest)
|
||||
Reference in New Issue
Block a user