cmvr-visual/inference/tests/test_download_integrity.py

42 lines
1.8 KiB
Python

import hashlib
import importlib.util
from pathlib import Path
import tempfile
import unittest
spec = importlib.util.spec_from_file_location("model_downloader", Path(__file__).parents[1] / "scripts/download_models.py")
downloader = importlib.util.module_from_spec(spec)
spec.loader.exec_module(downloader)
class DownloadIntegrityTests(unittest.TestCase):
def test_equal_size_configuration_corruption_is_rejected(self):
canonical = b'{"a":1}'
blob = hashlib.sha1(b"blob 7\0" + canonical).hexdigest()
with tempfile.TemporaryDirectory() as root:
path = Path(root) / "config.json"
path.write_bytes(b'{"a":2}')
self.assertFalse(downloader.matches_checkpoint_file(path, {"size": 7, "blobId": blob}))
path.write_bytes(canonical)
self.assertTrue(downloader.matches_checkpoint_file(path, {"size": 7, "blobId": blob}))
def test_lfs_digest_is_checked_without_using_git_pointer_digest(self):
data = b"example checkpoint"
with tempfile.TemporaryDirectory() as root:
path = Path(root) / "weights.safetensors"
path.write_bytes(data)
item = {"size": len(data), "blobId": "pointer-not-data", "lfs": {"sha256": hashlib.sha256(data).hexdigest()}}
self.assertTrue(downloader.matches_checkpoint_file(path, item))
path.write_bytes(b"x" * len(data))
self.assertFalse(downloader.matches_checkpoint_file(path, item))
def test_remote_filename_cannot_escape_model_directory(self):
with tempfile.TemporaryDirectory() as root:
with self.assertRaises(ValueError):
downloader.fetch_file("Qwen/Qwen-Image", {"rfilename": "../outside.json", "size": 1}, Path(root))
if __name__ == "__main__":
unittest.main()