Terra-2.5 / test_archive.py
openmodelai's picture
Upload 44 files
b838b2a verified
Raw History Blame Contribute Delete
2.03 kB
import hashlib
import json
import zipfile
import pytest
from scripts.archive import REQUIRED, pack, restore
def checkpoint(tmp_path):
source = tmp_path / "source"
source.mkdir()
for name in REQUIRED:
(source / name).write_text("test data: " + name)
return source
@pytest.mark.parametrize("compression", ["deflate", "lzma"])
def test_roundtrip(tmp_path, compression):
source = checkpoint(tmp_path)
archive = tmp_path / "checkpoint.zip"
checksum = pack(source, archive, compression=compression)
assert checksum == hashlib.sha256(archive.read_bytes()).hexdigest()
dest = tmp_path / "restored"
restore(archive, dest)
assert {p.name for p in dest.iterdir()} == REQUIRED
for name in REQUIRED:
assert (source / name).read_bytes() == (dest / name).read_bytes()
with pytest.raises(ValueError, match="уже существует"):
restore(archive, dest)
@pytest.mark.parametrize("name", ["../escape", "/escape", "a/../../escape", "a\\escape", "C:/escape"])
def test_unsafe_paths(tmp_path, name):
archive = tmp_path / "bad.zip"
with zipfile.ZipFile(archive, "w") as z:
z.writestr(name, "malicious")
with pytest.raises(ValueError, match="Небезопасный"):
restore(archive, tmp_path / "restored")
assert not (tmp_path / "restored").exists()
def test_corrupted_checksum(tmp_path):
archive = tmp_path / "bad.zip"
with zipfile.ZipFile(archive, "w") as z:
for name in REQUIRED:
z.writestr(name, "corrupted")
z.writestr("manifest.json", json.dumps({name: "0" * 64 for name in REQUIRED}))
with pytest.raises(ValueError, match="контрольная сумма"):
restore(archive, tmp_path / "restored")
assert not (tmp_path / "restored").exists()
def test_pack_requires_complete_checkpoint(tmp_path):
source = tmp_path / "empty"
source.mkdir()
with pytest.raises(ValueError, match="Неполный"):
pack(source, tmp_path / "checkpoint.zip")