"""Boundary regressions; actual namespace execution is a separate Linux check.""" import hashlib import io import json import os from pathlib import Path import tempfile import sys import subprocess import textwrap import unittest from unittest.mock import patch import zipfile import numpy as np from harness import recipe_runtime as runtime class RecipeBoundary(unittest.TestCase): def setUp(self): self.temporary = tempfile.TemporaryDirectory() self.root = Path(self.temporary.name).resolve() self.snapshot = self.root / "snapshot" self.snapshot.mkdir() (self.snapshot / "candidate.py").write_text("raise RuntimeError('must not import')") (self.snapshot / "reference.py").write_text("import common\n") (self.snapshot / "common.py").write_text("official = True\n") (self.snapshot / "weights.bin").write_bytes(b"actual weights") self.recipe = {"root": str(self.snapshot), "entrypoint": "candidate.py", "weights": "weights.bin", "input_spec": "1x3x2x2", "reference": {"entrypoint": "reference.py", "files": { name: hashlib.sha256((self.snapshot / name).read_bytes()).hexdigest() for name in ("reference.py", "common.py")}, "weights_sha256": hashlib.sha256(b"actual weights").hexdigest()}} def tearDown(self): self.temporary.cleanup() def test_batch_numeric_roundtrip(self): values = {"sample/out0": np.zeros((1, 2, 3), dtype=np.float32)} path = self.root / "result.npz" np.savez(path, **values) np.testing.assert_array_equal(runtime.load_arrays(path)["sample/out0"], values["sample/out0"]) def test_rejects_python_objects_nonfinite_and_unsafe_keys(self): for values in ({"s": np.array([object()], dtype=object)}, {"s": np.array([np.nan])}, {"../../escape": np.zeros(1)}, {"s": np.array(["not numeric"])}, {"s": np.ones(1, dtype=complex)}): with self.subTest(values=list(values)), self.assertRaises(ValueError): runtime.arrays_checked(values) def test_rejects_npy_header_allocation_attack_before_numpy_load(self): header = io.BytesIO() np.lib.format.write_array_header_1_0(header, {"descr": "