pptx-reporter/tests/test_fetch_ffprobe.py

169 lines
8.4 KiB
Python

import hashlib
import io
import json
from pathlib import Path
import struct
import subprocess
import tarfile
import tempfile
import unittest
from unittest.mock import patch
import zipfile
from scripts import fetch_ffprobe as fetcher
WINDOWS = "x86_64-pc-windows-gnu"
LINUX = "x86_64-unknown-linux-gnu"
def pe():
header = bytearray(90)
header[:2] = b"MZ"
struct.pack_into("<I", header, 60, 64)
header[64:70] = b"PE\0\0\x64\x86"
header[88:] = b"\x0b\x02"
return bytes(header)
def bundle(entries=None):
data = io.BytesIO()
with zipfile.ZipFile(data, "w") as archive:
for name, content in (entries or [
("build/bin/ffprobe.exe", pe()), ("build/LICENSE.txt", b"upstream notice"),
("build/README.txt", b"build details"), ("build/bin/ffmpeg.exe", b"unused"),
]):
archive.writestr(name, content)
return data.getvalue()
class FetchTests(unittest.TestCase):
def test_fetch_checksums_caches_preserves_notices_and_rejects_corruption(self):
content = bundle()
digest = hashlib.sha256(content).hexdigest()
name = fetcher.ASSETS[WINDOWS]
release = {"id": 1, "assets": [{"id": 2, "name": name, "digest": "sha256:" + digest,
"size": len(content),
"browser_download_url": "https://github.com/FFmpeg-Builds/FFmpeg-Builds/releases/download/latest/" + name}]}
def download(url, destination, limit=None):
destination.write_bytes(json.dumps(release).encode() if url.startswith(fetcher.API) else content)
with tempfile.TemporaryDirectory() as temp, patch.object(fetcher, "download", side_effect=download) as get, \
patch.object(fetcher.platform, "machine", return_value="test-host"):
cache = Path(temp)
binary, notices = fetcher.fetch(WINDOWS, cache)
self.assertEqual(binary.read_bytes(), pe())
self.assertEqual((notices / "build/LICENSE.txt").read_text(), "upstream notice")
receipt = json.loads((notices / "provenance.json").read_text())
self.assertEqual(receipt["archive_sha256"], digest)
self.assertIsNone(receipt["native_version_check"])
self.assertFalse((binary.parent / "ffmpeg.exe").exists())
dated_name = "ffmpeg-n9.0.2-29-g6dfaba896f-win64-lgpl-9.0.zip"
release["assets"][0]["name"] = dated_name
self.assertEqual(fetcher.fetch(WINDOWS, cache, "autobuild-fixture", digest,
asset_name=dated_name), (binary, notices))
get.reset_mock()
with patch.object(fetcher, "download", side_effect=AssertionError("network used")):
self.assertEqual(fetcher.fetch(WINDOWS, cache, "autobuild-fixture", digest,
asset_name=dated_name), (binary, notices))
self.assertEqual(get.call_count, 0)
release["assets"][0]["name"] = name
get.reset_mock()
self.assertEqual(fetcher.fetch(WINDOWS, cache), (binary, notices))
self.assertEqual(get.call_count, 1) # metadata refreshed, archive reused
with self.assertRaisesRegex(ValueError, "requested pin"):
fetcher.fetch(WINDOWS, cache, expected_sha256="0" * 64)
binary.write_bytes(b"corrupted")
with patch.object(fetcher, "download", side_effect=AssertionError("network used")):
with self.assertRaisesRegex(ValueError, "Corrupt"):
fetcher.fetch(WINDOWS, cache, expected_sha256=digest)
with self.assertRaisesRegex(ValueError, "Corrupt"):
fetcher.fetch(WINDOWS, cache)
self.assertFalse(list(cache.glob(".fetch-*")))
def test_checksum_mismatch_leaves_no_installed_files(self):
name = fetcher.ASSETS[WINDOWS]
release = {"id": 1, "assets": [{"id": 2, "name": name, "digest": "sha256:" + "0" * 64,
"size": 5, "browser_download_url":
"https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/" + name}]}
def download(url, destination, limit=None):
destination.write_bytes(json.dumps(release).encode() if url.startswith(fetcher.API) else b"bad")
with tempfile.TemporaryDirectory() as temp, patch.object(fetcher, "download", side_effect=download):
with self.assertRaisesRegex(ValueError, "SHA-256 mismatch"):
fetcher.fetch(WINDOWS, Path(temp))
self.assertEqual(list(Path(temp).iterdir()), [])
def test_unsafe_missing_duplicate_and_wrong_architecture_archives(self):
cases = [
[("../LICENSE", b"unsafe"), ("build/bin/ffprobe.exe", pe())],
[("build/bin/ffprobe.exe", pe())],
[("build/LICENSE", b"notice"), ("build/bin/ffprobe.exe", b"MZwrong")],
[("build/LICENSE", b"notice"), ("build/bin/ffprobe.exe", pe()),
("other/bin/ffprobe.exe", pe())],
]
for entries in cases:
with self.subTest(entries=entries), tempfile.TemporaryDirectory() as temp:
root = Path(temp)
archive = root / "input.zip"
archive.write_bytes(bundle(entries))
output = root / "out"
output.mkdir()
with self.assertRaises(ValueError):
fetcher.unpack(archive, output, WINDOWS)
self.assertFalse((root / "LICENSE").exists())
def test_linux_tar_extracts_only_binary_and_notices(self):
header = bytearray(64)
header[:6] = b"\x7fELF\x02\x01"
header[18:20] = b"\x3e\0"
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
archive = root / "input.tar.xz"
with tarfile.open(archive, "w:xz") as tar:
for name, content in [("build/bin/ffprobe", bytes(header)), ("build/LICENSE", b"notice")]:
info = tarfile.TarInfo(name)
info.size = len(content)
tar.addfile(info, io.BytesIO(content))
output = root / "out"
output.mkdir()
binary, notices = fetcher.unpack(archive, output, LINUX)
self.assertEqual(binary.read_bytes(), bytes(header))
self.assertTrue((notices / "build/LICENSE").exists())
def test_rejects_non_https_and_unsupported_targets(self):
with self.assertRaises(ValueError):
fetcher.download("http://example.invalid/file", Path("/unused"))
with self.assertRaises(ValueError):
fetcher.fetch("unknown-target")
for asset in ("../probe.zip", "ffmpeg-n9.0-latest-win64-gpl-9.0.zip",
fetcher.ASSETS[LINUX]):
with self.subTest(asset=asset), self.assertRaisesRegex(ValueError, "LGPL"):
fetcher.fetch(WINDOWS, asset_name=asset)
def test_native_smoke_failure_does_not_install_cache(self):
header = bytearray(64)
header[:6] = b"\x7fELF\x02\x01"
header[18:20] = b"\x3e\0"
stream = io.BytesIO()
with tarfile.open(fileobj=stream, mode="w:xz") as tar:
for name, content in [("build/bin/ffprobe", bytes(header)), ("build/LICENSE", b"notice")]:
info = tarfile.TarInfo(name)
info.size = len(content)
tar.addfile(info, io.BytesIO(content))
content = stream.getvalue()
name = fetcher.ASSETS[LINUX]
release = {"id": 1, "assets": [{"id": 2, "name": name,
"digest": "sha256:" + hashlib.sha256(content).hexdigest(), "size": len(content),
"browser_download_url": "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/" + name}]}
def download(url, destination, limit=None):
destination.write_bytes(json.dumps(release).encode() if url.startswith(fetcher.API) else content)
with tempfile.TemporaryDirectory() as temp, patch.object(fetcher, "download", side_effect=download), \
patch.object(fetcher.platform, "system", return_value="Linux"), \
patch.object(fetcher.platform, "machine", return_value="x86_64"), \
patch.object(fetcher.subprocess, "run", side_effect=subprocess.CalledProcessError(1, "ffprobe")):
with self.assertRaises(subprocess.CalledProcessError):
fetcher.fetch(LINUX, Path(temp))
self.assertEqual(list(Path(temp).iterdir()), [])
if __name__ == "__main__":
unittest.main()