mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 02:53:54 +01:00
Codex r1 on #380: iter(source) only mapped TypeError, so an __iter__ raising OSError, csv.Error or StopIteration (directly or as a PEP 479 RuntimeError) still leaked. Apply the same narrow provider-error and cause-checked RuntimeError mapping at initialization; normal exhaustion in next() is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
824 lines
34 KiB
Python
824 lines
34 KiB
Python
"""Supply-chain identity tests for the exact issue-60 model boundary."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import os
|
|
import site as site_module
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
import obliteratus.distributed.model_identity as identity_module
|
|
from obliteratus.distributed.contracts import ContractError, RuntimeContractError
|
|
from obliteratus.distributed.model_identity import (
|
|
CRITICAL_API_MODULES,
|
|
_api_fingerprint_from_files,
|
|
api_fingerprint_from_files,
|
|
critical_api_fingerprint_digest,
|
|
installed_package_set_digest,
|
|
model_runtime_identity_digest,
|
|
observed_external_runtime_bindings,
|
|
)
|
|
|
|
|
|
@dataclass
|
|
class _Distribution:
|
|
name: str
|
|
version: str
|
|
metadata_text: str
|
|
record_text: str | None
|
|
|
|
@property
|
|
def metadata(self) -> dict[str, str]:
|
|
return {"Name": self.name}
|
|
|
|
def read_text(self, name: str) -> str | None:
|
|
if name == "METADATA":
|
|
return self.metadata_text
|
|
if name == "RECORD":
|
|
return self.record_text
|
|
return None
|
|
|
|
|
|
def _distribution(name: str, *, record: str = "module.py,sha256=abc,3\n") -> _Distribution:
|
|
return _Distribution(
|
|
name=name,
|
|
version="1.2.3",
|
|
metadata_text=f"Name: {name}\nVersion: 1.2.3\n",
|
|
record_text=record,
|
|
)
|
|
|
|
|
|
def _wheel(site: Path, name: str = "obliteratus", version: str = "1.2.3"):
|
|
metadata = site / f"{name}-{version}.dist-info"
|
|
metadata.mkdir(parents=True)
|
|
package = site / name / "__init__.py"
|
|
package.parent.mkdir(exist_ok=True)
|
|
package.write_text(f"__version__ = {version!r}\n", encoding="utf-8")
|
|
(metadata / "METADATA").write_text(
|
|
f"Metadata-Version: 2.1\nName: {name}\nVersion: {version}\n", encoding="utf-8",
|
|
)
|
|
(metadata / "RECORD").write_text(
|
|
f"{name}/__init__.py,,\n{metadata.name}/METADATA,,\n{metadata.name}/RECORD,,\n",
|
|
encoding="utf-8",
|
|
)
|
|
return identity_module.importlib.metadata.Distribution.at(metadata)
|
|
|
|
|
|
@pytest.fixture
|
|
def installed_environment(tmp_path, monkeypatch):
|
|
prefix = tmp_path / "environment"
|
|
site = prefix / "lib" / "site-packages"
|
|
site.mkdir(parents=True)
|
|
monkeypatch.setattr(identity_module.sys, "prefix", str(prefix))
|
|
monkeypatch.setattr(identity_module.sysconfig, "get_path", lambda _key: str(site))
|
|
# Real PathDistribution discovery, isolated from the test host's packages.
|
|
monkeypatch.setattr(identity_module.sys, "path", [str(site)])
|
|
return site
|
|
|
|
|
|
@pytest.fixture(params=["bad-size", "oversized-field", "third-party-provider"])
|
|
def malformed_record_environment(installed_environment, monkeypatch, request):
|
|
_wheel(installed_environment)
|
|
oversized_record = "x" * (csv.field_size_limit() + 1) + ",,\n"
|
|
if request.param == "third-party-provider":
|
|
dependency = _distribution("dependency", record=oversized_record)
|
|
read_text = dependency.read_text
|
|
|
|
def parsed_read_text(name):
|
|
text = read_text(name)
|
|
if name == "RECORD":
|
|
# Custom providers may parse RECORD while reading it. The
|
|
# stdlib PathDistribution reads third-party RECORDs as raw text.
|
|
list(csv.reader(text.splitlines()))
|
|
return text
|
|
|
|
monkeypatch.setattr(dependency, "read_text", parsed_read_text)
|
|
discover = identity_module.importlib.metadata.distributions
|
|
|
|
def distributions(**kwargs):
|
|
yield from discover(**kwargs)
|
|
if not kwargs:
|
|
yield dependency
|
|
|
|
monkeypatch.setattr(identity_module.importlib.metadata, "distributions", distributions)
|
|
return "metadata is unavailable"
|
|
|
|
record = installed_environment / "obliteratus-1.2.3.dist-info" / "RECORD"
|
|
if request.param == "bad-size":
|
|
text = "obliteratus/__init__.py,,bad-size\n"
|
|
else:
|
|
text = "obliteratus/__init__.py,,\n" + oversized_record
|
|
record.write_text(text, encoding="utf-8")
|
|
return "RECORD is malformed"
|
|
|
|
|
|
@pytest.mark.parametrize("source_entry", ["absolute", "cwd", "alias"])
|
|
def test_source_egg_info_cannot_shadow_installed_identity(
|
|
tmp_path, monkeypatch, installed_environment, source_entry,
|
|
):
|
|
site = installed_environment
|
|
installed = _wheel(site)
|
|
dependency = _wheel(site, "dependency")
|
|
expected = installed_package_set_digest([installed, dependency])
|
|
assert installed_package_set_digest() == expected
|
|
|
|
source = tmp_path / "checkout"
|
|
egg = source / "obliteratus.egg-info"
|
|
egg.mkdir(parents=True)
|
|
(egg / "PKG-INFO").write_text("Name: obliteratus\nVersion: 0.0.0\n", encoding="utf-8")
|
|
(egg / "SOURCES.txt").write_text("obliteratus/__init__.py\n", encoding="utf-8")
|
|
source_package = source / "obliteratus"
|
|
source_package.mkdir()
|
|
(source_package / "__init__.py").write_text("__version__ = '0.0.0'\n", encoding="utf-8")
|
|
monkeypatch.setattr(identity_module, "_qualification_checkout_egg_info", lambda: egg.resolve())
|
|
monkeypatch.chdir(source)
|
|
if source_entry == "alias":
|
|
alias = tmp_path / "checkout-alias"
|
|
alias.symlink_to(source, target_is_directory=True)
|
|
entry = str(alias)
|
|
else:
|
|
entry = str(source) if source_entry == "absolute" else ""
|
|
identity_module.sys.path.insert(0, entry)
|
|
|
|
# Demonstrate the original lookup selects the stale build artifact.
|
|
assert identity_module.importlib.metadata.version("obliteratus") == "0.0.0"
|
|
resolved, metadata_path, _sites = identity_module._installed_obliteratus_distribution()
|
|
assert resolved.version == "1.2.3"
|
|
assert metadata_path == site / "obliteratus-1.2.3.dist-info"
|
|
assert installed_package_set_digest() == expected
|
|
|
|
(egg / "PKG-INFO").write_text("Name: obliteratus\nVersion: 9.9.9\n", encoding="utf-8")
|
|
assert installed_package_set_digest() == expected
|
|
(site / "dependency-1.2.3.dist-info" / "RECORD").write_text("changed,,\n", encoding="utf-8")
|
|
assert installed_package_set_digest() != expected
|
|
|
|
|
|
def test_qualification_checkout_is_derived_from_the_canonical_module_path(tmp_path, monkeypatch):
|
|
source = tmp_path / "checkout"
|
|
module = source / "obliteratus" / "distributed" / "model_identity.py"
|
|
module.parent.mkdir(parents=True)
|
|
module.touch()
|
|
alias = tmp_path / "checkout-alias"
|
|
alias.symlink_to(source, target_is_directory=True)
|
|
monkeypatch.setattr(
|
|
identity_module, "__file__", str(alias / "obliteratus" / "distributed" / module.name),
|
|
)
|
|
assert identity_module._qualification_checkout_egg_info() == source / "obliteratus.egg-info"
|
|
|
|
|
|
@pytest.mark.parametrize("target_location", ["external", "inside"])
|
|
def test_checkout_egg_info_symlinks_fail_closed(
|
|
tmp_path, monkeypatch, installed_environment, target_location,
|
|
):
|
|
_wheel(installed_environment)
|
|
checkout = tmp_path / "qualification-checkout"
|
|
module = checkout / "obliteratus" / "distributed" / "model_identity.py"
|
|
module.parent.mkdir(parents=True)
|
|
module.touch()
|
|
monkeypatch.setattr(identity_module, "__file__", str(module))
|
|
target_root = (
|
|
tmp_path / "other-installation" if target_location == "external" else checkout / "build"
|
|
)
|
|
target = target_root / "obliteratus.egg-info"
|
|
target.mkdir(parents=True)
|
|
(target / "PKG-INFO").write_text("Name: obliteratus\nVersion: 0.0.0\n", encoding="utf-8")
|
|
egg = checkout / "obliteratus.egg-info"
|
|
egg.symlink_to(target, target_is_directory=True)
|
|
identity_module.sys.path[:0] = [str(checkout), str(target_root)]
|
|
# The linked metadata is also independently visible through its own path.
|
|
visible = identity_module.importlib.metadata.distributions(name="obliteratus")
|
|
assert {identity_module._metadata_path(item) for item in visible} == {
|
|
egg, target, installed_environment / "obliteratus-1.2.3.dist-info",
|
|
}
|
|
with pytest.raises(ContractError, match="egg-info must not be a symlink"):
|
|
identity_module._qualification_checkout_egg_info()
|
|
with pytest.raises(ContractError, match="egg-info must not be a symlink"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
@pytest.mark.parametrize("metadata_kind", ["directory", "file"])
|
|
@pytest.mark.parametrize("external_entry", ["first", "last"])
|
|
def test_visible_external_egg_info_conflict_fails_closed(
|
|
tmp_path, monkeypatch, installed_environment, metadata_kind, external_entry,
|
|
):
|
|
_wheel(installed_environment)
|
|
checkout_egg = tmp_path / "qualification-checkout" / "obliteratus.egg-info"
|
|
checkout_egg.mkdir(parents=True)
|
|
metadata = "Name: obliteratus\nVersion: 0.0.0\n"
|
|
(checkout_egg / "PKG-INFO").write_text(metadata, encoding="utf-8")
|
|
monkeypatch.setattr(
|
|
identity_module, "_qualification_checkout_egg_info", lambda: checkout_egg.resolve(),
|
|
)
|
|
identity_module.sys.path.insert(0, str(checkout_egg.parent))
|
|
|
|
external = tmp_path / "other-installation"
|
|
external.mkdir()
|
|
egg = external / "obliteratus.egg-info"
|
|
if metadata_kind == "directory":
|
|
egg.mkdir()
|
|
(egg / "PKG-INFO").write_text(metadata, encoding="utf-8")
|
|
else:
|
|
egg.write_text(metadata, encoding="utf-8")
|
|
if external_entry == "first":
|
|
identity_module.sys.path.insert(0, str(external))
|
|
else:
|
|
identity_module.sys.path.append(str(external))
|
|
|
|
with pytest.raises(ContractError, match="distributions disagree"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_wheel_execution_does_not_exempt_prefix_egg_info(monkeypatch, installed_environment):
|
|
_wheel(installed_environment)
|
|
monkeypatch.setattr(
|
|
identity_module, "__file__",
|
|
str(installed_environment / "obliteratus" / "distributed" / "model_identity.py"),
|
|
)
|
|
egg = installed_environment / "obliteratus.egg-info"
|
|
egg.mkdir()
|
|
(egg / "PKG-INFO").write_text("Name: obliteratus\nVersion: 0.0.0\n", encoding="utf-8")
|
|
assert identity_module._qualification_checkout_egg_info() == egg
|
|
with pytest.raises(ContractError, match="duplicate installations are ambiguous"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_nested_site_egg_info_is_not_exempted(monkeypatch, installed_environment):
|
|
_wheel(installed_environment)
|
|
egg = installed_environment / "nested" / "obliteratus.egg-info"
|
|
egg.mkdir(parents=True)
|
|
(egg / "PKG-INFO").write_text("Name: obliteratus\nVersion: 0.0.0\n", encoding="utf-8")
|
|
monkeypatch.setattr(identity_module, "_qualification_checkout_egg_info", lambda: egg)
|
|
identity_module.sys.path.insert(0, str(egg.parent))
|
|
# Prefix discovery only scans the site itself, so this reaches containment.
|
|
with pytest.raises(ContractError, match="distributions disagree"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_source_egg_info_alone_is_not_an_installed_distribution(
|
|
tmp_path, monkeypatch, installed_environment,
|
|
):
|
|
egg = tmp_path / "checkout" / "obliteratus.egg-info"
|
|
egg.mkdir(parents=True)
|
|
(egg / "PKG-INFO").write_text("Name: obliteratus\nVersion: 0.0.0\n", encoding="utf-8")
|
|
identity_module.sys.path.insert(0, str(egg.parent))
|
|
with pytest.raises(ContractError, match="exactly one installed obliteratus wheel.*--no-editable"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_installed_egg_info_cannot_replace_wheel_metadata(installed_environment):
|
|
egg = installed_environment / "obliteratus.egg-info"
|
|
egg.mkdir()
|
|
(egg / "PKG-INFO").write_text("Name: obliteratus\nVersion: 0.0.0\n", encoding="utf-8")
|
|
with pytest.raises(ContractError, match="installed .dist-info wheel"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_disagreeing_installed_distributions_fail_closed(installed_environment):
|
|
_wheel(installed_environment)
|
|
_wheel(installed_environment, version="9.9.9")
|
|
with pytest.raises(ContractError, match="duplicate installations are ambiguous"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_visible_external_wheel_conflict_fails_closed(tmp_path, installed_environment):
|
|
_wheel(installed_environment)
|
|
external = tmp_path / "other-installation"
|
|
_wheel(external, version="9.9.9")
|
|
identity_module.sys.path.insert(0, str(external))
|
|
with pytest.raises(ContractError, match="distributions disagree"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_visible_site_alias_resolves_to_the_same_installed_wheel(tmp_path, installed_environment):
|
|
installed = _wheel(installed_environment)
|
|
alias = tmp_path / "site-alias"
|
|
alias.symlink_to(installed_environment, target_is_directory=True)
|
|
identity_module.sys.path[:] = [str(alias)]
|
|
assert installed_package_set_digest() == installed_package_set_digest([installed])
|
|
|
|
|
|
def test_metadata_from_one_site_cannot_describe_package_in_another(
|
|
monkeypatch, installed_environment,
|
|
):
|
|
site = installed_environment
|
|
other_site = site.parent / "platlib"
|
|
_wheel(site)
|
|
other_package = other_site / "obliteratus"
|
|
other_package.mkdir(parents=True)
|
|
(other_package / "__init__.py").write_text("VALUE = 1\n", encoding="utf-8")
|
|
monkeypatch.setattr(
|
|
identity_module.sysconfig, "get_path",
|
|
lambda key: str(other_site if key == "purelib" else site),
|
|
)
|
|
with pytest.raises(ContractError, match="RECORD does not own"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
@pytest.mark.parametrize("broken", ["record", "ownership", "package", "alias"])
|
|
def test_installed_wheel_requires_record_owned_package(installed_environment, broken):
|
|
site = installed_environment
|
|
_wheel(site)
|
|
record = site / "obliteratus-1.2.3.dist-info" / "RECORD"
|
|
package = site / "obliteratus" / "__init__.py"
|
|
if broken == "record":
|
|
record.unlink()
|
|
elif broken == "ownership":
|
|
record.write_text("other.py,,\n", encoding="utf-8")
|
|
else:
|
|
package.unlink()
|
|
if broken == "alias":
|
|
target = site / "other.py"
|
|
target.write_text("VALUE = 1\n", encoding="utf-8")
|
|
package.symlink_to(target)
|
|
with pytest.raises(ContractError, match="RECORD"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_malformed_record_is_a_contract_error(malformed_record_environment):
|
|
with pytest.raises(ContractError, match=malformed_record_environment):
|
|
installed_package_set_digest()
|
|
|
|
|
|
@pytest.mark.parametrize("tier", ["candidate_local_gpu", "candidate_two_host_gpu"])
|
|
def test_malformed_record_is_dependency_drift_for_gpu_candidates(
|
|
monkeypatch, malformed_record_environment, tier,
|
|
):
|
|
from obliteratus.distributed.model_session import _validate_preload_bindings
|
|
|
|
monkeypatch.setattr(identity_module, "critical_api_fingerprint_digest", lambda: "a" * 64)
|
|
config = SimpleNamespace(
|
|
device_kind="cuda", evidence_tier="candidate_preflight", require_model=lambda: None,
|
|
)
|
|
with pytest.raises(RuntimeContractError) as failure:
|
|
_validate_preload_bindings(
|
|
config, SimpleNamespace(), SimpleNamespace(topology=()),
|
|
SimpleNamespace(evidence_tier=tier),
|
|
source_mount_digest="b" * 64, control_group_digest="c" * 64,
|
|
)
|
|
assert failure.value.code == "LMS_DEPENDENCY_DRIFT"
|
|
|
|
|
|
def test_distribution_discovery_csv_error_is_a_contract_error(monkeypatch, installed_environment):
|
|
def distributions(**_kwargs):
|
|
return list(csv.reader(["x" * (csv.field_size_limit() + 1)]))
|
|
|
|
monkeypatch.setattr(identity_module.importlib.metadata, "distributions", distributions)
|
|
with pytest.raises(ContractError, match="metadata is unavailable"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_pep660_finder_install_without_a_prefix_package_fails_closed(
|
|
tmp_path, monkeypatch, installed_environment,
|
|
):
|
|
prefix_site = installed_environment
|
|
_wheel(prefix_site)
|
|
installed_package = prefix_site / "obliteratus" / "__init__.py"
|
|
installed_package.unlink()
|
|
installed_package.parent.rmdir()
|
|
source_package = tmp_path / "editable-checkout" / "obliteratus" / "__init__.py"
|
|
source_package.parent.mkdir(parents=True)
|
|
source_package.write_text("__version__ = '1.2.3'\n", encoding="utf-8")
|
|
|
|
finder_name = "__editable___obliteratus_1_2_3_finder"
|
|
finder_file = prefix_site / f"{finder_name}.py"
|
|
finder_file.write_text(
|
|
"import importlib.util\nimport sys\n"
|
|
"class _EditableFinder:\n"
|
|
" @classmethod\n"
|
|
" def find_spec(cls, fullname, path=None, target=None):\n"
|
|
" if fullname == 'obliteratus':\n"
|
|
f" return importlib.util.spec_from_file_location(fullname, {str(source_package)!r})\n"
|
|
"def install():\n"
|
|
" sys.meta_path.append(_EditableFinder)\n",
|
|
encoding="utf-8",
|
|
)
|
|
pth = prefix_site / "__editable__.obliteratus-1.2.3.pth"
|
|
pth.write_text(f"import {finder_name}; {finder_name}.install()\n", encoding="utf-8")
|
|
metadata = prefix_site / "obliteratus-1.2.3.dist-info"
|
|
(metadata / "RECORD").write_text(
|
|
f"{finder_file.name},,\n{pth.name},,\n"
|
|
f"{metadata.name}/METADATA,,\n{metadata.name}/RECORD,,\n",
|
|
encoding="utf-8",
|
|
)
|
|
# Activate the .pth without leaving a finder or module in the test process.
|
|
specification = identity_module.importlib.util.spec_from_file_location(finder_name, finder_file)
|
|
finder = identity_module.importlib.util.module_from_spec(specification)
|
|
monkeypatch.setitem(identity_module.sys.modules, finder_name, finder)
|
|
specification.loader.exec_module(finder)
|
|
monkeypatch.setattr(identity_module.sys, "meta_path", list(identity_module.sys.meta_path))
|
|
site_module.addsitedir(str(prefix_site))
|
|
assert finder._EditableFinder in identity_module.sys.meta_path
|
|
assert finder._EditableFinder.find_spec("obliteratus").origin == str(source_package)
|
|
# GPU identity requires a RECORD-owned prefix package, even with a working finder.
|
|
with pytest.raises(ContractError, match="RECORD does not own"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
@pytest.mark.parametrize("prefix_wheel", [False, True])
|
|
def test_legacy_egg_link_and_pth_install_fails_closed(
|
|
tmp_path, monkeypatch, installed_environment, prefix_wheel,
|
|
):
|
|
prefix_site = installed_environment
|
|
if prefix_wheel:
|
|
_wheel(prefix_site)
|
|
source = tmp_path / "legacy-checkout"
|
|
egg = source / "obliteratus.egg-info"
|
|
egg.mkdir(parents=True)
|
|
(egg / "PKG-INFO").write_text("Name: obliteratus\nVersion: 0.0.0\n", encoding="utf-8")
|
|
package = source / "obliteratus" / "__init__.py"
|
|
package.parent.mkdir()
|
|
package.write_text("__version__ = '0.0.0'\n", encoding="utf-8")
|
|
monkeypatch.setattr(
|
|
identity_module, "_qualification_checkout_egg_info",
|
|
lambda: tmp_path / "qualification-checkout" / "obliteratus.egg-info",
|
|
)
|
|
(prefix_site / "obliteratus.egg-link").write_text(f"{source}\n.\n", encoding="utf-8")
|
|
(prefix_site / "easy-install.pth").write_text(f"{source}\n", encoding="utf-8")
|
|
site_module.addsitedir(str(prefix_site))
|
|
assert str(source) in identity_module.sys.path
|
|
# No wheel is unqualified; a wheel plus another checkout's egg-info is ambiguous.
|
|
message = "distributions disagree" if prefix_wheel else "exactly one installed obliteratus wheel"
|
|
with pytest.raises(ContractError, match=message):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_installed_identity_retains_dependency_metadata_checks(installed_environment):
|
|
_wheel(installed_environment)
|
|
_wheel(installed_environment, "dependency")
|
|
record = installed_environment / "dependency-1.2.3.dist-info" / "RECORD"
|
|
record.unlink()
|
|
with pytest.raises(ContractError, match="RECORD"):
|
|
installed_package_set_digest()
|
|
record.write_text("dependency/__init__.py,,\n", encoding="utf-8")
|
|
_wheel(installed_environment, "dependency", version="9.9.9")
|
|
with pytest.raises(ContractError, match="duplicate name"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_third_party_record_fields_are_hashed_without_csv_parsing(installed_environment):
|
|
installed = _wheel(installed_environment)
|
|
dependency = _wheel(installed_environment, "dependency")
|
|
original = installed_package_set_digest()
|
|
record = installed_environment / "dependency-1.2.3.dist-info" / "RECORD"
|
|
record.write_text("x" * (csv.field_size_limit() + 1) + ",,\n", encoding="utf-8")
|
|
expected = installed_package_set_digest([installed, dependency])
|
|
assert installed_package_set_digest() == expected
|
|
assert expected != original
|
|
|
|
|
|
def test_installation_paths_outside_active_prefix_are_rejected(
|
|
tmp_path, monkeypatch, installed_environment,
|
|
):
|
|
monkeypatch.setattr(identity_module.sysconfig, "get_path", lambda _key: str(tmp_path))
|
|
with pytest.raises(ContractError, match="under sys.prefix"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def test_installed_package_set_digest_is_order_independent_and_content_bound():
|
|
first = _distribution("Example_Package")
|
|
second = _distribution("Dependency")
|
|
expected = installed_package_set_digest([first, second])
|
|
|
|
assert installed_package_set_digest([second, first]) == expected
|
|
assert (
|
|
installed_package_set_digest(
|
|
[first, _distribution("Dependency", record="module.py,sha256=changed,3\n")]
|
|
)
|
|
!= expected
|
|
)
|
|
|
|
|
|
def test_installed_package_set_rejects_duplicate_or_unrecorded_distributions():
|
|
with pytest.raises(ContractError, match="duplicate"):
|
|
installed_package_set_digest([_distribution("same"), _distribution("Same")])
|
|
with pytest.raises(ContractError, match="RECORD"):
|
|
installed_package_set_digest([_Distribution("missing", "1.2.3", "Name: missing\n", None)])
|
|
|
|
|
|
def test_api_fingerprint_is_bound_to_actual_file_bytes(tmp_path):
|
|
source = (tmp_path / "module.py").resolve()
|
|
source.write_text("VALUE = 1\n", encoding="utf-8")
|
|
first = _api_fingerprint_from_files({"test.fixture": source}, allowed_modules=("test.fixture",))
|
|
source.write_text("VALUE = 2\n", encoding="utf-8")
|
|
second = _api_fingerprint_from_files({"test.fixture": source}, allowed_modules=("test.fixture",))
|
|
|
|
assert first != second
|
|
|
|
|
|
def test_api_fingerprint_rejects_unreviewed_or_aliased_files(tmp_path):
|
|
source = (tmp_path / "module.py").resolve()
|
|
source.write_text("VALUE = 1\n", encoding="utf-8")
|
|
with pytest.raises(ContractError, match="allowlist"):
|
|
api_fingerprint_from_files({"unreviewed.module": source})
|
|
alias = tmp_path / "alias.py"
|
|
alias.symlink_to(source)
|
|
with pytest.raises(ContractError, match="path"):
|
|
_api_fingerprint_from_files(
|
|
{"test.fixture": alias.absolute()}, allowed_modules=("test.fixture",)
|
|
)
|
|
|
|
|
|
def test_public_api_fingerprint_rejects_test_prefix(tmp_path):
|
|
source = (tmp_path / "module.py").resolve()
|
|
source.write_text("VALUE = 1\n", encoding="utf-8")
|
|
|
|
with pytest.raises(ContractError, match="allowlist"):
|
|
api_fingerprint_from_files({"test.fixture": source})
|
|
|
|
|
|
def test_runtime_identity_is_deterministic_and_value_bound():
|
|
first = {"python": "3.12.11", "torch": "2.13.0", "cuda": "13.0"}
|
|
assert model_runtime_identity_digest(first) == model_runtime_identity_digest(
|
|
dict(reversed(tuple(first.items())))
|
|
)
|
|
assert model_runtime_identity_digest({**first, "cuda": "13.1"}) != (
|
|
model_runtime_identity_digest(first)
|
|
)
|
|
|
|
|
|
def test_current_reviewed_api_allowlist_resolves_to_one_fingerprint():
|
|
assert tuple(sorted(CRITICAL_API_MODULES)) == CRITICAL_API_MODULES
|
|
assert len(critical_api_fingerprint_digest()) == 64
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("distribution", "message"),
|
|
[
|
|
(_Distribution(7, "1.0", "Name: x\n", "r"), "name is unavailable"), # type: ignore[arg-type]
|
|
(_Distribution("-bad", "1.0", "Name: x\n", "r"), "outside the closed schema"),
|
|
(_Distribution("ok", "1.0 beta", "Name: ok\n", "r"), "version is outside"),
|
|
(_Distribution("ok", "1.0", "\ud800", "r"), "not strict UTF-8"),
|
|
(_Distribution("ok", "1.0", "", "r"), "exceeds its byte bound"),
|
|
],
|
|
)
|
|
def test_installed_package_set_refuses_out_of_schema_metadata(distribution, message):
|
|
with pytest.raises(ContractError, match=message):
|
|
installed_package_set_digest([distribution])
|
|
|
|
|
|
def test_installed_package_set_refuses_unreadable_empty_or_unbounded_sets(monkeypatch):
|
|
unreadable = SimpleNamespace(metadata={}, version="1.0", read_text=lambda _name: "")
|
|
with pytest.raises(ContractError, match="metadata is unavailable"):
|
|
installed_package_set_digest([unreadable])
|
|
with pytest.raises(ContractError, match="set is empty"):
|
|
installed_package_set_digest([])
|
|
monkeypatch.setattr(identity_module, "MAX_DISTRIBUTIONS", 1)
|
|
with pytest.raises(ContractError, match="count bound"):
|
|
installed_package_set_digest([_distribution("one"), _distribution("two")])
|
|
|
|
|
|
class _RaisingProvider:
|
|
def __init__(self, error, where):
|
|
self._error, self._where = error, where
|
|
|
|
@property
|
|
def metadata(self):
|
|
if self._where == "metadata":
|
|
raise self._error
|
|
return {"Name": "provider"}
|
|
|
|
@property
|
|
def version(self):
|
|
if self._where == "version":
|
|
raise self._error
|
|
return "1.0"
|
|
|
|
def read_text(self, name):
|
|
if self._where == name:
|
|
raise self._error
|
|
return "Name: provider\nVersion: 1.0\n" if name == "METADATA" else "provider.py,,\n"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("error", "where"),
|
|
[
|
|
(PermissionError("denied"), "METADATA"),
|
|
(OSError("io failure"), "RECORD"),
|
|
(StopIteration(), "version"),
|
|
(StopIteration(), "metadata"),
|
|
],
|
|
)
|
|
def test_installed_package_set_maps_provider_errors_to_the_contract(error, where):
|
|
with pytest.raises(ContractError, match="metadata is unavailable"):
|
|
installed_package_set_digest([_RaisingProvider(error, where)])
|
|
|
|
|
|
def test_live_discovery_stop_iteration_is_a_contract_error(monkeypatch, installed_environment):
|
|
# Raised inside the discovery generator, StopIteration becomes RuntimeError (PEP 479).
|
|
_wheel(installed_environment)
|
|
discover = identity_module.importlib.metadata.distributions
|
|
|
|
def distributions(**kwargs):
|
|
yield from discover(**kwargs)
|
|
if not kwargs:
|
|
yield _RaisingProvider(StopIteration(), "metadata")
|
|
|
|
monkeypatch.setattr(identity_module.importlib.metadata, "distributions", distributions)
|
|
with pytest.raises(ContractError, match="metadata is unavailable"):
|
|
installed_package_set_digest()
|
|
|
|
|
|
def _failing_source(error):
|
|
yield _distribution("alpha")
|
|
raise error
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"source",
|
|
[
|
|
lambda: _failing_source(OSError("io failure")),
|
|
lambda: _failing_source(PermissionError("denied")),
|
|
lambda: _failing_source(csv.Error("bad record")),
|
|
lambda: _failing_source(StopIteration()),
|
|
lambda: 42,
|
|
],
|
|
ids=["oserror", "permission", "csv", "stop-iteration", "not-iterable"],
|
|
)
|
|
def test_installed_package_set_maps_source_iterator_errors_to_the_contract(source):
|
|
with pytest.raises(ContractError, match="metadata is unavailable"):
|
|
installed_package_set_digest(source())
|
|
|
|
|
|
class _FailingIterable:
|
|
def __init__(self, error):
|
|
self._error = error
|
|
|
|
def __iter__(self):
|
|
raise self._error
|
|
|
|
|
|
class _GeneratorStopIterable:
|
|
def __iter__(self):
|
|
# A StopIteration escaping a nested generator becomes RuntimeError (PEP 479).
|
|
def broken():
|
|
raise StopIteration
|
|
yield # pragma: no cover
|
|
|
|
return iter([next(broken())])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"source",
|
|
[
|
|
_FailingIterable(OSError("unreadable source")),
|
|
_FailingIterable(csv.Error("bad record")),
|
|
_FailingIterable(StopIteration()),
|
|
_GeneratorStopIterable(),
|
|
],
|
|
ids=["oserror", "csv", "stop-iteration", "pep479-runtime-error"],
|
|
)
|
|
def test_installed_package_set_maps_iterator_initialization_errors_to_the_contract(source):
|
|
with pytest.raises(ContractError, match="metadata is unavailable"):
|
|
installed_package_set_digest(source)
|
|
|
|
|
|
def test_unrelated_runtime_errors_from_iterator_initialization_are_not_masked():
|
|
with pytest.raises(RuntimeError, match="unrelated"):
|
|
installed_package_set_digest(_FailingIterable(RuntimeError("unrelated")))
|
|
|
|
|
|
def test_unrelated_runtime_errors_from_the_source_are_not_masked():
|
|
with pytest.raises(RuntimeError, match="unrelated"):
|
|
installed_package_set_digest(_failing_source(RuntimeError("unrelated")))
|
|
|
|
|
|
def test_installed_package_set_defaults_to_the_validated_live_distribution_iterator(monkeypatch):
|
|
live = [_distribution("alpha"), _distribution("beta")]
|
|
monkeypatch.setattr(identity_module, "_installed_package_distributions", lambda: iter(live))
|
|
assert installed_package_set_digest() == installed_package_set_digest(live)
|
|
|
|
|
|
def test_api_fingerprint_refuses_invalid_profiles_paths_and_files(tmp_path):
|
|
source = (tmp_path / "module.py").resolve()
|
|
source.write_text("VALUE = 1\n", encoding="utf-8")
|
|
allowed = ("test.fixture",)
|
|
with pytest.raises(ContractError, match="profile is invalid"):
|
|
_api_fingerprint_from_files(
|
|
{"test.fixture": source}, allowed_modules=allowed, profile_digest="nope"
|
|
)
|
|
with pytest.raises(ContractError, match="profile is invalid"):
|
|
_api_fingerprint_from_files({}, allowed_modules=allowed)
|
|
with pytest.raises(ContractError, match="source path is invalid"):
|
|
_api_fingerprint_from_files({"test.fixture": "module.py"}, allowed_modules=allowed)
|
|
with pytest.raises(ContractError, match="source path is invalid"):
|
|
_api_fingerprint_from_files({"test.fixture": Path("module.py")}, allowed_modules=allowed)
|
|
|
|
empty = (tmp_path / "empty.py").resolve()
|
|
empty.write_text("", encoding="utf-8")
|
|
with pytest.raises(ContractError, match="bounded regular file"):
|
|
_api_fingerprint_from_files({"test.fixture": empty}, allowed_modules=allowed)
|
|
with pytest.raises(ContractError, match="bounded regular file"):
|
|
_api_fingerprint_from_files({"test.fixture": tmp_path.resolve()}, allowed_modules=allowed)
|
|
with pytest.raises(ContractError, match="source is unavailable"):
|
|
_api_fingerprint_from_files(
|
|
{"test.fixture": (tmp_path / "missing.py").resolve()}, allowed_modules=allowed
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["truncated", "grew", "metadata"])
|
|
def test_api_fingerprint_detects_files_changed_during_inspection(tmp_path, monkeypatch, change):
|
|
source = (tmp_path / "module.py").resolve()
|
|
source.write_text("VALUE = 1\n", encoding="utf-8")
|
|
real_read, real_fstat = os.read, os.fstat
|
|
stats: list[int] = []
|
|
|
|
def read(descriptor, size):
|
|
if change == "truncated":
|
|
return b""
|
|
if change == "grew" and size == 1:
|
|
return b"x"
|
|
return real_read(descriptor, size)
|
|
|
|
def fstat(descriptor):
|
|
stats.append(descriptor)
|
|
result = real_fstat(descriptor)
|
|
if change == "metadata" and len(stats) == 2:
|
|
values = list(result)
|
|
values[1] += 1
|
|
return os.stat_result(values)
|
|
return result
|
|
|
|
monkeypatch.setattr(identity_module.os, "read", read)
|
|
monkeypatch.setattr(identity_module.os, "fstat", fstat)
|
|
with pytest.raises(ContractError, match={"truncated": "truncated", "grew": "grew",
|
|
"metadata": "changed"}[change]):
|
|
_api_fingerprint_from_files({"test.fixture": source}, allowed_modules=("test.fixture",))
|
|
|
|
|
|
def test_critical_api_fingerprint_requires_the_reviewed_allowlist():
|
|
with pytest.raises(ContractError, match="differs from the reviewed profile"):
|
|
critical_api_fingerprint_digest(CRITICAL_API_MODULES[:-1])
|
|
|
|
|
|
@pytest.mark.parametrize("origin", [None, "built-in", "frozen", "lookup-error"])
|
|
def test_critical_api_fingerprint_requires_a_resolvable_source_origin(monkeypatch, origin):
|
|
def find_spec(_name):
|
|
if origin == "lookup-error":
|
|
raise ValueError("no spec")
|
|
return SimpleNamespace(origin=origin)
|
|
|
|
monkeypatch.setattr(identity_module.importlib.util, "find_spec", find_spec)
|
|
with pytest.raises(ContractError, match="origin is unavailable"):
|
|
critical_api_fingerprint_digest()
|
|
|
|
|
|
def test_critical_api_fingerprint_refuses_missing_or_aliased_sources(monkeypatch, tmp_path):
|
|
missing = str((tmp_path / "missing.py").resolve())
|
|
monkeypatch.setattr(
|
|
identity_module.importlib.util, "find_spec", lambda _name: SimpleNamespace(origin=missing)
|
|
)
|
|
with pytest.raises(ContractError, match="source is unavailable"):
|
|
critical_api_fingerprint_digest()
|
|
|
|
real = (tmp_path / "real.py").resolve()
|
|
real.write_text("VALUE = 1\n", encoding="utf-8")
|
|
alias = tmp_path / "alias.py"
|
|
alias.symlink_to(real)
|
|
monkeypatch.setattr(
|
|
identity_module.importlib.util,
|
|
"find_spec",
|
|
lambda _name: SimpleNamespace(origin=str(alias.absolute())),
|
|
)
|
|
with pytest.raises(ContractError, match="aliased"):
|
|
critical_api_fingerprint_digest()
|
|
|
|
|
|
@pytest.mark.parametrize("software", [{}, {"python": ""}, {"": "3.12"}, {"python": 3}])
|
|
def test_runtime_identity_refuses_empty_or_untyped_software(software):
|
|
with pytest.raises(ContractError, match="runtime identity is invalid"):
|
|
model_runtime_identity_digest(software)
|
|
|
|
|
|
def test_observed_runtime_bindings_combine_the_three_live_identities(monkeypatch):
|
|
monkeypatch.setattr(identity_module, "runtime_software_versions", lambda: {"python": "3.x"})
|
|
monkeypatch.setattr(identity_module, "critical_api_fingerprint_digest", lambda: "a" * 64)
|
|
monkeypatch.setattr(identity_module, "installed_package_set_digest", lambda: "b" * 64)
|
|
|
|
assert observed_external_runtime_bindings() == {
|
|
"api_digest": "a" * 64,
|
|
"package_digest": "b" * 64,
|
|
"runtime_digest": model_runtime_identity_digest({"python": "3.x"}),
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("outcome", ["digest", "contract-error"])
|
|
def test_api_fingerprint_maps_close_failures_to_the_contract(tmp_path, monkeypatch, outcome):
|
|
source = (tmp_path / "module.py").resolve()
|
|
source.write_text("" if outcome == "contract-error" else "VALUE = 1\n", encoding="utf-8")
|
|
real_close = os.close
|
|
closed: list[int] = []
|
|
|
|
def close(descriptor):
|
|
closed.append(descriptor)
|
|
real_close(descriptor)
|
|
raise OSError("deferred write-back failure")
|
|
|
|
monkeypatch.setattr(identity_module.os, "close", close)
|
|
with pytest.raises(ContractError, match="source is unavailable"):
|
|
_api_fingerprint_from_files({"test.fixture": source}, allowed_modules=("test.fixture",))
|
|
assert len(closed) == 1
|