Files
OBLITERATUS/tests/test_checkpoint_inventory_boundaries.py

127 lines
5.3 KiB
Python

"""Exercise bounded inventory failures without reading checkpoint payloads."""
import json
import os
import pytest
from obliteratus.checkpoint_inspection import InspectionLimits
from obliteratus.checkpoint_service import CheckpointService
pytestmark = pytest.mark.cpu
def _inspect(service, source, mode):
return (service.inspect(source, mode="bounded_metadata") if mode == "bounded"
else service.inspect_metadata(source))
def test_unknown_inspection_mode_is_not_silently_full_inspection(tmp_path):
with pytest.raises(ValueError, match="invalid inspection mode"):
CheckpointService().inspect(tmp_path, mode="unknown")
@pytest.mark.parametrize("raw,message", [
('{"model_type":"gpt2","model_type":"llama"}', "duplicate configuration key"),
('{"hidden_size":NaN}', "nonfinite JSON"),
('[]', "invalid model configuration"),
])
def test_bounded_configuration_rejects_ambiguous_or_non_object_json(tmp_path, raw, message):
config = tmp_path / "config.json"
config.write_text(raw)
with pytest.raises(ValueError, match=message):
CheckpointService().inspect(config, mode="bounded_metadata")
def test_standalone_configuration_exposes_only_valid_recognized_fields(tmp_path):
config = tmp_path / "config.json"
config.write_text(json.dumps({
"model_type": "gpt2", "dtype": "float16", "hidden_size": 16,
"num_hidden_layers": 2, "private_path": "/not-a-public-field",
}))
descriptor = CheckpointService().inspect(config, mode="bounded_metadata")
assert descriptor.files == 1 and descriptor.directories == 0
assert descriptor.configuration.model_type == "gpt2"
assert descriptor.configuration.dtype == "float16"
assert descriptor.configuration.hidden_size == 16 and descriptor.configuration.layers == 2
assert not hasattr(descriptor.configuration, "private_path")
assert not descriptor.loadability_verified and not descriptor.tensor_headers_read
def test_bounded_inventory_rejects_hardlinked_payload_without_opening_it(tmp_path):
original = tmp_path / "original.bin"
original.write_bytes(b"not a pickle")
linked = tmp_path / "linked.bin"
os.link(original, linked)
with pytest.raises(ValueError, match="links and special files denied"):
CheckpointService().inspect(linked, mode="bounded_metadata")
@pytest.mark.parametrize("mode", ["bounded", "inventory"])
@pytest.mark.parametrize("limit", ["depth", "directories", "files", "bytes"])
def test_inventory_limits_reject_excessive_checkpoint_structure(tmp_path, mode, limit):
options = {}
if limit in {"depth", "directories"}:
directory = tmp_path
for _ in range(33 if limit == "depth" else 2):
directory = directory / "nested"
directory.mkdir()
if limit == "directories":
options["max_directories"] = 2
message = "checkpoint depth limit" if limit == "depth" else "checkpoint directory limit"
else:
(tmp_path / "first.bin").write_bytes(b"12")
(tmp_path / "second.bin").write_bytes(b"34")
options = {"max_files": 1} if limit == "files" else {"max_total_bytes": 3}
message = "checkpoint inventory limit"
service = CheckpointService(inspection_limits=InspectionLimits(**options))
with pytest.raises(ValueError, match=message):
_inspect(service, tmp_path, mode)
@pytest.mark.parametrize("mode", ["bounded", "inventory"])
def test_inventory_refuses_symbolic_link_to_external_payload(tmp_path, mode):
source = tmp_path / "source"
source.mkdir()
external = tmp_path / "external.bin"
external.write_bytes(b"outside")
(source / "model.bin").symlink_to(external)
with pytest.raises(ValueError, match="link"):
_inspect(CheckpointService(), source, mode)
assert external.read_bytes() == b"outside"
@pytest.mark.parametrize("name,tensors", [("model.bin", 1), ("README.txt", 0)])
def test_standalone_inventory_counts_bytes_without_deserializing(tmp_path, name, tensors):
source = tmp_path / name
source.write_bytes(b"not a valid checkpoint")
assert CheckpointService().inspect_metadata(source) == {
"files": 1, "directories": 0, "total_bytes": source.stat().st_size,
"tensor_files": tensors,
}
def test_standalone_inventory_rejects_oversized_file(tmp_path):
source = tmp_path / "model.bin"
source.write_bytes(b"too large")
service = CheckpointService(inspection_limits=InspectionLimits(max_total_bytes=1))
with pytest.raises(ValueError, match="invalid checkpoint file"):
service.inspect_metadata(source)
@pytest.mark.parametrize("mode", ["bounded", "inventory"])
@pytest.mark.parametrize("nested", [False, True])
def test_inventory_accepts_exact_file_directory_and_byte_limits(tmp_path, mode, nested):
directory = tmp_path / "nested" if nested else tmp_path
directory.mkdir(exist_ok=True)
(directory / "model.bin").write_bytes(b"opaque")
directories = 2 if nested else 1
service = CheckpointService(inspection_limits=InspectionLimits(
max_files=1, max_directories=directories, max_total_bytes=6,
))
report = _inspect(service, tmp_path, mode)
counts = vars(report) if mode == "bounded" else report
assert {name: counts[name] for name in ("files", "directories", "total_bytes", "tensor_files")} == {
"files": 1, "directories": directories, "total_bytes": 6, "tensor_files": 1,
}