beffroi-tools/tests/test_macos_ffprobe.py

123 lines
6.0 KiB
Python

import hashlib
import io
import json
from pathlib import Path
import struct
import sys
import tarfile
import tempfile
import unittest
from unittest.mock import patch
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
import build_macos_ffprobe as builder
from macos_binary import validate_macos
def macho(cpu=0x100000C, minimum=13 << 16, dependency="/usr/lib/libSystem.B.dylib"):
name = dependency.encode() + b"\0"
length = (24 + len(name) + 7) // 8 * 8
version = struct.pack("<6I", 0x32, 24, 1, minimum, 0, 0)
dylib = struct.pack("<6I", 0xC, length, 24, 0, 0, 0) + name
dylib += b"\0" * (length - len(dylib))
return struct.pack("<8I", 0xFEEDFACF, cpu, 0, 2, 2, len(version + dylib), 0, 0) + version + dylib
def source_archive():
stream = io.BytesIO()
with tarfile.open(fileobj=stream, mode="w:xz") as archive:
for name, data in [("COPYING.LGPLv2.1", b"license"), ("LICENSE.md", b"notices")]:
entry = tarfile.TarInfo("ffmpeg-9.0.2/" + name)
entry.size = len(data)
archive.addfile(entry, io.BytesIO(data))
return stream.getvalue()
class MacosProbeTests(unittest.TestCase):
def test_macho_compatibility_and_dependency_checks(self):
with tempfile.TemporaryDirectory() as temp:
path = Path(temp) / "ffprobe"
for target, cpu in builder.TARGETS.items():
code = 0x100000C if cpu == "aarch64" else 0x1000007
path.write_bytes(macho(code))
self.assertEqual(validate_macos(path, target)["minimum_macos"], "13.0.0")
invalid = [
macho(0x1000007), macho(minimum=14 << 16),
macho(dependency="@rpath/libavcodec.dylib"), macho(dependency="/tmp/library"),
macho(dependency="/usr/lib/../../tmp/library"), macho()[:20], macho()[:-5],
]
for data in invalid:
with self.subTest(data=data[:32]):
path.write_bytes(data)
with self.assertRaises(ValueError):
validate_macos(path, "aarch64-apple-darwin")
data = bytearray(macho())
struct.pack_into("<I", data, 36, 0)
path.write_bytes(data)
with self.assertRaisesRegex(ValueError, "command size"):
validate_macos(path, "aarch64-apple-darwin")
def test_source_build_preserves_licenses_and_source_then_verifies_cache(self):
archive = source_archive()
pin = {"release": "9.0.2", "asset": "ffmpeg-9.0.2.tar.xz",
"sha256": hashlib.sha256(archive).hexdigest()}
def compile_fixture(command, **kwargs):
self.assertIn("--network=none", command)
self.assertIn(builder.IMAGE, command)
work = Path(command[command.index("-v") + 1].removesuffix(":/work"))
source = work / "ffmpeg-9.0.2"
(source / "config.h").write_text(
"#define CONFIG_GPL 0\n#define CONFIG_GPLV3 0\n#define CONFIG_NONFREE 0\n")
(source / "ffprobe").write_bytes(macho())
with tempfile.TemporaryDirectory() as temp, \
patch.object(builder, "download", side_effect=lambda url, path: path.write_bytes(archive)) as download, \
patch.object(builder.subprocess, "run", side_effect=compile_fixture) as run, \
patch.object(builder.platform, "system", return_value="Linux"), \
patch.dict("os.environ", {"CROSS_CONTAINER_ENGINE": "podman"}):
cache = Path(temp)
binary, notices = builder.build("aarch64-apple-darwin", cache, pin)
self.assertEqual(binary.read_bytes(), macho())
self.assertEqual((notices / pin["asset"]).read_bytes(), archive)
self.assertTrue((notices / "COPYING.LGPLv2.1").is_file())
self.assertTrue((notices / "BUILDING.txt").is_file())
receipt = json.loads((notices / "provenance.json").read_text())
self.assertIsNone(receipt["native_version_check"])
self.assertEqual(receipt["source_sha256"], pin["sha256"])
self.assertEqual(builder.build("aarch64-apple-darwin", cache, pin), (binary, notices))
self.assertEqual(run.call_count, 1)
self.assertEqual(download.call_count, 1)
(notices / "COPYING.LGPLv2.1").write_text("corrupted")
with self.assertRaisesRegex(ValueError, "Corrupt"):
builder.build("aarch64-apple-darwin", cache, pin)
def test_wrong_digest_never_compiles_or_installs(self):
with tempfile.TemporaryDirectory() as temp, \
patch.object(builder, "download", side_effect=lambda url, path: path.write_bytes(b"wrong")), \
patch.object(builder.subprocess, "run") as run:
with self.assertRaisesRegex(ValueError, "SHA-256"):
builder.build("x86_64-apple-darwin", Path(temp), {
"release": "9.0.2", "asset": "ffmpeg-9.0.2.tar.xz", "sha256": "0" * 64})
run.assert_not_called()
self.assertEqual(list(Path(temp).iterdir()), [])
def test_unsafe_source_and_cache_paths(self):
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
archive = root / "source.tar.xz"
with tarfile.open(archive, "w:xz") as tar:
member = tarfile.TarInfo("ffmpeg-9.0.2/link")
member.type = tarfile.SYMTYPE
member.linkname = "/etc/passwd"
tar.addfile(member)
with self.assertRaisesRegex(ValueError, "Unsafe"):
builder.extract_source(archive, root, "9.0.2")
for relative in ("../escape", "/absolute", "a//b", "a/./b", "C:escape"):
with self.assertRaises(ValueError):
builder.regular_under(root, relative)
(root / "file").write_text("fixture")
(root / "link").symlink_to(root / "file")
with self.assertRaisesRegex(ValueError, "Symlinked"):
builder.regular_under(root, "link")