Files
OBLITERATUS/tests/test_release_bytecode_policy.py
T

216 lines
9.5 KiB
Python

"""Real disposable venvs exercise startup, ownership and deliberate bypasses."""
import json
import os
from pathlib import Path
import subprocess
import sys
import pytest
from scripts import release_bytecode_policy as policy
from scripts.release_checkout_identity import verify_checkout
ROOT = Path(__file__).parents[1]
HELPER = ROOT / "scripts/release_bytecode_policy.py"
@pytest.fixture
def environment(tmp_path):
root = tmp_path / "owned-venv"
policy.create(root)
return root
def run(environment, *args, check=True):
# Even negative controls must not write caches in the shared stdlib.
env = {key: value for key, value in os.environ.items() if not key.startswith("PYTHON")}
result = subprocess.run(
[str(environment / "bin/python"), "-X", f"pycache_prefix={environment / 'probe-cache'}", *map(str, args)],
env=env, check=False, capture_output=True, text=True, timeout=30,
)
if check:
assert result.returncode == 0, result.stdout + result.stderr
return result
def site_directory(environment):
return Path(run(environment, "-B", "-c", "import sysconfig; print(sysconfig.get_path('purelib'))").stdout.strip())
def install(environment):
run(environment, "-B", HELPER, "install", "--environment", environment)
return site_directory(environment) / policy.FILENAME
def verify(environment, *, check=True):
return run(environment, HELPER, "verify", "--environment", environment, check=check)
def test_fresh_environment_is_exclusively_claimed_and_policy_matches_review(environment):
path = install(environment)
assert path.read_bytes() == b"import sys; sys.dont_write_bytecode=True\n"
assert policy.POLICY["sha256"] == "4a776f2bd659b11c7e0882976e722732ad5986539f5b92c20c49bcce3c2bf22b"
receipt = json.loads(verify(environment).stdout)
assert receipt == {"bytecode_policy": policy.POLICY, "bytecode_policy_path": str(path)}
with pytest.raises(FileExistsError):
policy.create(environment)
duplicate = run(environment, "-B", HELPER, "install", "--environment", environment, check=False)
assert duplicate.returncode != 0 and "FileExistsError" in duplicate.stderr
assert path.read_bytes() == policy.CONTENT
@pytest.mark.parametrize("flags", [[], ["-E"], ["-I"]], ids=["normal", "ignore-env", "isolated"])
@pytest.mark.parametrize("enabled", [False, True], ids=["control", "policy"])
def test_import_cache_behavior_with_real_startup(environment, tmp_path, flags, enabled):
if enabled:
install(environment)
module = tmp_path / "startup_emitter.py"
module.write_text("answer = 42\n")
code = (
"import importlib.util, sys; "
f"s = importlib.util.spec_from_file_location('startup_emitter', {str(module)!r}); "
"m = importlib.util.module_from_spec(s); s.loader.exec_module(m); "
"assert m.answer == 42; print(sys.dont_write_bytecode)"
)
result = run(environment, *flags, "-c", code)
assert result.stdout.strip() == str(enabled)
assert bool(list(environment.rglob("startup_emitter*.pyc"))) is (not enabled)
@pytest.mark.parametrize("change", ["missing", "altered", "symlink", "owner", "later-hook"])
def test_missing_changed_or_ineffective_policy_cannot_verify(environment, tmp_path, change):
path = install(environment)
verify(environment)
if change == "missing":
path.unlink()
elif change == "altered":
path.write_bytes(policy.CONTENT + b"# tampered\n")
elif change == "symlink":
copy = tmp_path / "copied-policy"
copy.write_bytes(policy.CONTENT)
path.unlink()
path.symlink_to(copy)
elif change == "owner":
(environment / policy.OWNER).write_text("{}")
else:
(path.parent / "zz_interference.pth").write_text("import sys; sys.dont_write_bytecode=False\n")
result = verify(environment, check=False)
assert result.returncode != 0
assert "release" in result.stderr and ("policy" in result.stderr or "venv" in result.stderr)
def test_environment_without_claim_and_shared_environment_are_never_adopted(tmp_path):
import venv
root = tmp_path / "unowned"
venv.EnvBuilder(with_pip=False, symlinks=True).create(root)
result = run(root, "-B", HELPER, "install", "--environment", root, check=False)
assert result.returncode != 0 and "freshly owned" in result.stderr
assert not list(root.rglob(policy.FILENAME))
with pytest.raises(FileExistsError):
policy.create(Path(sys.prefix))
global_attempt = run(root, "-B", HELPER, "install", "--environment", sys.base_prefix, check=False)
assert global_attempt.returncode != 0 and "freshly owned" in global_attempt.stderr
def test_copied_ownership_marker_cannot_adopt_a_different_environment(environment, tmp_path):
import venv
other = tmp_path / "other"
venv.EnvBuilder(with_pip=False, symlinks=True).create(other)
(other / policy.OWNER).write_bytes((environment / policy.OWNER).read_bytes())
result = run(other, "-B", HELPER, "install", "--environment", other, check=False)
assert result.returncode != 0 and "freshly owned" in result.stderr
assert not list(other.rglob(policy.FILENAME))
@pytest.mark.parametrize("existing", ["file", "symlink"])
def test_existing_startup_file_is_never_overwritten(environment, tmp_path, existing):
path = site_directory(environment) / policy.FILENAME
content = b"# another owner\n"
if existing == "symlink":
destination = tmp_path / "other-owner"
destination.write_bytes(content)
path.symlink_to(destination)
else:
path.write_bytes(content)
result = run(environment, "-B", HELPER, "install", "--environment", environment, check=False)
assert result.returncode != 0 and "FileExistsError" in result.stderr
assert path.read_bytes() == content
def test_symlinked_environment_and_site_packages_are_rejected(environment, tmp_path):
alias = tmp_path / "alias"
alias.symlink_to(environment, target_is_directory=True)
with pytest.raises(ValueError, match="symlinks"):
policy.create(alias)
directory = site_directory(environment)
moved = directory.with_name("moved-site")
directory.rename(moved)
directory.symlink_to(moved, target_is_directory=True)
result = run(environment, "-B", HELPER, "install", "--environment", environment, check=False)
assert result.returncode != 0 and "symlinks" in result.stderr
assert not list(moved.glob(policy.FILENAME))
@pytest.mark.parametrize("flags", [["-E", "-S"], ["-I", "-S"]])
def test_no_site_is_an_explicit_policy_limit(environment, flags):
install(environment)
assert run(environment, *flags, "-c", "import sys; print(sys.dont_write_bytecode)").stdout.strip() == "False"
def test_explicit_compilation_is_not_prevented_or_deleted(environment, tmp_path):
install(environment)
source, cache = tmp_path / "explicit.py", tmp_path / "explicit.pyc"
source.write_text("value = 1\n")
run(environment, "-I", "-c", f"import py_compile; py_compile.compile({str(source)!r}, cfile={str(cache)!r}, doraise=True)")
assert cache.is_file()
verify(environment)
assert cache.is_file()
@pytest.mark.parametrize("target", ["head", "base"])
def test_unchanged_real_emitter_preserves_strict_git_guard(environment, tmp_path, monkeypatch, target):
revision = subprocess.check_output(["git", "rev-parse", "HEAD" if target == "head" else "HEAD^"], cwd=ROOT, text=True).strip()
objects = subprocess.check_output(["git", "rev-parse", "--git-path", "objects"], cwd=ROOT, text=True).strip()
monkeypatch.setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", str((ROOT / objects).resolve()))
checkout = tmp_path / "checkout"
subprocess.run(["git", "init", "--quiet", str(checkout)], check=True)
subprocess.run(["git", "checkout", "--quiet", "--detach", revision], cwd=checkout, check=True)
before = verify_checkout(checkout, revision)
path = install(environment)
# The real test launches a nested -E interpreter without our probe flags.
# Protect shared stdlib even if this regression fails to suppress caches.
(path.parent / "01_probe_cache.pth").write_text(
f"import sys; sys.pycache_prefix={str(environment / 'nested-cache')!r}\n",
)
# Only the copied test's import of pytest needs caller dependencies; no sync,
# mutation, or startup hook is applied to the caller's shared environment.
import pytest as installed_pytest
code = (
"import importlib.util, sys; "
f"sys.path.insert(0, {str(Path(installed_pytest.__file__).parent.parent)!r}); "
f"sys.path.insert(0, {str(checkout)!r}); "
f"s = importlib.util.spec_from_file_location('real_emitter', {str(checkout / 'tests/test_conditional_gate_scripts.py')!r}); "
"m = importlib.util.module_from_spec(s); s.loader.exec_module(m); "
f"m.test_conditional_gate_scripts_support_direct_execution({str(tmp_path)!r}, 'conditional_gate_summary.py'); "
f"m.test_conditional_gate_scripts_support_direct_execution({str(tmp_path)!r}, 'run_conditional_gate.py')"
)
run(environment, "-I", "-c", code)
assert verify_checkout(checkout, revision) == before
assert not list(checkout.rglob("*.pyc"))
assert not list((environment / "nested-cache").rglob("*.pyc"))
verify(environment)
# An injected cache still fails both pre/post-style checks; no blanket allow.
injected = checkout / "scripts/__pycache__/injected.pyc"
injected.parent.mkdir(parents=True, exist_ok=True)
injected.write_bytes(b"untrusted cache")
with pytest.raises(ValueError, match="uncommitted test/source/config"):
verify_checkout(checkout, revision)
assert injected.read_bytes() == b"untrusted cache"