"""CPU provenance fixtures; no Kit launch or installed-tree mutation.""" import ast import importlib import importlib.machinery import importlib.util import sys from pathlib import Path from types import CodeType, FunctionType, ModuleType, SimpleNamespace import pytest from test_contact_cycle_sim_evaluation_binding import ROOT, binding, load, replay from test_contact_cycle_sim_evaluation_namespaces import namespace # noqa: F401 @pytest.fixture def installed_links(tmp_path, monkeypatch): physical = tmp_path / 'sim-6' physical.mkdir() source = physical / 'module.py' source.write_text('VALUE = 1\n') intermediate, alias = tmp_path / 'sim', tmp_path / '_sim' intermediate.symlink_to(physical) alias.symlink_to(intermediate) links = tuple((str(p), str(p.readlink()), p.lstat().st_dev, p.lstat().st_ino) for p in (alias, intermediate)) monkeypatch.setattr(binding, 'SIM_ROOT', physical) monkeypatch.setattr(binding, 'RUNTIME_LINKS', links) return alias, intermediate, source def test_installed_alias_chain_binds_raw_physical_links_and_source(installed_links, monkeypatch): alias, intermediate, source = installed_links module = load('_runtime_alias_cpu', alias / source.name, monkeypatch) module.__spec__.origin = str(source) record = binding._dependency_module_identity(module.__name__) assert record['raw_path'] == str(alias / source.name) assert record['path'] == str(source) assert record['sha256'] == binding.sha256(source.read_bytes()) assert [link['path'] for link in record['links']] == [str(alias), str(intermediate)] assert all(link['target_sha256'] == binding.sha256(link['target'].encode()) for link in record['links']) assert record['origin']['raw_path'] == str(source) with pytest.raises(ValueError, match='Noncanonical'): binding._file(alias / source.name) # project/config helper remains strict @pytest.mark.parametrize('change', ['target', 'inode', 'foreign', 'extra-link', 'dot', 'origin']) def test_installed_alias_rejects_unbound_paths(installed_links, monkeypatch, tmp_path, change): alias, intermediate, source = installed_links module = load('_runtime_alias_cpu', alias / source.name, monkeypatch) if change == 'target': alias.unlink() alias.symlink_to(source.parent) elif change == 'inode': monkeypatch.setattr(binding, 'RUNTIME_LINKS', tuple((p, t, d, i + 1) for p, t, d, i in binding.RUNTIME_LINKS)) elif change == 'foreign': monkeypatch.setattr(binding, 'SIM_ROOT', tmp_path / 'foreign') elif change == 'extra-link': extra = source.parent / 'extra.py' extra.symlink_to(source) module.__file__ = str(alias / extra.name) elif change == 'dot': module.__file__ = str(alias) + '/./module.py' else: other = tmp_path / 'other.py' other.write_bytes(source.read_bytes()) module.__spec__.origin = str(other) with pytest.raises(ValueError): binding._dependency_module_identity(module.__name__) def test_inspected_installed_aliases_read_only(): physical = binding.SIM_ROOT / binding.FAST_IMPORTER_RELATIVE if not physical.exists(): pytest.skip('inspected installation absent') record = binding._runtime_path(Path('/home/cen/IsaacLab/_isaac_sim') / binding.FAST_IMPORTER_RELATIVE) assert record['sha256'] == binding.FAST_IMPORTER_SHA256 assert len(record['links']) == 2 assert record['path'] == str(physical) @pytest.fixture def fast_namespace(namespace, monkeypatch): # noqa: F811 module, directory = namespace path = binding.SIM_ROOT / binding.FAST_IMPORTER_RELATIVE if not path.exists(): pytest.skip('inspected installation absent') # Execute only the installed FastFinder method, with CPU-only cache fixtures. # Its function code is still independently compared against the full source. spec = importlib.util.spec_from_file_location(binding.FAST_IMPORTER, path) importer = importlib.util.module_from_spec(spec) importer._bootstrap = importlib.import_module('_frozen_importlib') importer._bootstrap_external = importlib.import_module('_frozen_importlib_external') importer._cache = SimpleNamespace(modules={}, namespaces={module.__name__: [str(directory)]}) compiled = compile(path.read_bytes(), str(path), 'exec', dont_inherit=True) cls_code = next(c for c in compiled.co_consts if isinstance(c, CodeType) and c.co_name == 'FastFinder') method_code = next(c for c in cls_code.co_consts if isinstance(c, CodeType) and c.co_name == 'find_spec') importer.FastFinder = type('FastFinder', (), { 'find_spec': classmethod(FunctionType(method_code, vars(importer), argdefs=(None, None)))}) monkeypatch.setitem(sys.modules, binding.FAST_IMPORTER, importer) monkeypatch.setattr(sys, 'meta_path', [importer.FastFinder, *sys.meta_path]) locations = importer.FastFinder.find_spec(module.__name__, [str(directory.parent)]).submodule_search_locations module.__path__ = module.__spec__.submodule_search_locations = module.__loader__._path = locations return module, directory, importer def test_actual_pinned_fastfinder_code_and_duplicate_paths(fast_namespace, tmp_path): module, directory, _ = fast_namespace record = binding._namespace_identity(module.__name__, tmp_path) assert record['importer']['kind'] == 'kit-fastfinder' assert record['importer']['source']['sha256'] == binding.FAST_IMPORTER_SHA256 assert record['search_locations'] == [str(directory)] assert record['raw_search_locations'] == [str(directory), str(directory)] @pytest.mark.parametrize('change', ['callback', 'pin', 'meta-path', 'foreign-location', 'source']) def test_fastfinder_rejects_changed_provenance(fast_namespace, tmp_path, monkeypatch, change): module, directory, importer = fast_namespace if change == 'callback': module.__path__._path_finder = lambda *args: None elif change == 'pin': monkeypatch.setattr(binding, 'FAST_IMPORTER_SHA256', '0' * 64) elif change == 'meta-path': monkeypatch.setattr(sys, 'meta_path', [v for v in sys.meta_path if v is not importer.FastFinder]) elif change == 'foreign-location': module.__path__._path.append(str(tmp_path)) else: other = tmp_path / 'fast_importer.py' other.write_bytes(Path(importer.__file__).read_bytes()) importer.__file__ = importer.__spec__.origin = str(other) with pytest.raises(ValueError): binding._namespace_identity(module.__name__, tmp_path) def test_multiprocessing_alias_requires_real_main_object(tmp_path, monkeypatch): monkeypatch.setattr(binding, 'ISAAC_PINS', {}) for name in tuple(sys.modules): if name.split('.')[0] in ('SkillFactory', 'isaaclab', 'isaaclab_physx'): monkeypatch.delitem(sys.modules, name) path = tmp_path / 'entry.py' path.write_text('VALUE = 1\n') main = load('__main__', path, monkeypatch) monkeypatch.setitem(sys.modules, '__mp_main__', main) record = binding._imports({'__main__'}, tmp_path)['__main__'] assert record['standard_aliases']['__mp_main__']['same_module_object'] duplicate = ModuleType('__main__') duplicate.__file__ = str(path) monkeypatch.setitem(sys.modules, '__mp_main__', duplicate) with pytest.raises(ValueError, match='Unbound imported-path alias'): binding._imports({'__main__'}, tmp_path) monkeypatch.setitem(sys.modules, '__mp_main__', main) monkeypatch.setitem(sys.modules, 'arbitrary_alias', main) with pytest.raises(ValueError, match='Unbound imported-path alias'): binding._imports({'__main__'}, tmp_path) def make_pxr_wrapper(package, short_name, monkeypatch): path = package / short_name / '__init__.py' path.parent.mkdir(parents=True) path.write_text('# CPU wrapper fixture\n') name = 'pxr.' + short_name wrapper = load(name, path, monkeypatch) native_name = name + '._' + short_name[0].lower() + short_name[1:] native_path = path.parent / (native_name.rsplit('.', 1)[1] + '.so') native_path.write_bytes(b'CPU fixture only; never dlopen') native = ModuleType(native_name) native.__file__ = str(native_path) native.__spec__ = importlib.util.spec_from_file_location(native_name, native_path) native.__loader__ = native.__spec__.loader monkeypatch.setitem(sys.modules, native_name, native) wrapper.__spec__, wrapper.__loader__ = native.__spec__, native.__loader__ return wrapper, native @pytest.fixture(params=['Usd', 'PhysxSchema', 'PhysicsSchemaTools']) def pxr_wrapper(request, tmp_path, monkeypatch): monkeypatch.setattr(binding, 'SIM_ROOT', tmp_path) libs = tmp_path / 'extscache/omni.usd.libs-pinned/pxr' schema = tmp_path / 'extscache/omni.usd.schema.physx-pinned/pxr' manifests = {} for package in (libs, schema): manifest = package.parent / 'config/extension.toml' manifest.parent.mkdir(parents=True) manifest.write_text('# owner: ' + package.parent.name + '\n[[python.module]]\nname = "pxr"\n') manifests[str(manifest)] = binding.sha256(manifest.read_bytes()) monkeypatch.setattr(binding, 'RUNTIME_LAYOUT_PINS', manifests) tf, _ = make_pxr_wrapper(libs, 'Tf', monkeypatch) wrapper, native = make_pxr_wrapper(libs if request.param == 'Usd' else schema, request.param, monkeypatch) monkeypatch.setattr(binding, 'REQUIRED_RUNTIME', { module.__name__: ('usd-wrapper', module.__file__, binding.sha256(Path(module.__file__).read_bytes())) for module in (tf, wrapper)}) return wrapper, native def test_pxr_copied_native_spec_binds_wrapper_and_binary(pxr_wrapper): wrapper, native = pxr_wrapper record = binding._dependency_module_identity(wrapper.__name__) assert record['spec_name'] == native.__name__ assert record['wrapper']['native']['path'] == native.__file__ assert record['wrapper']['wrapper_source']['path'] == wrapper.__file__ tf = sys.modules['pxr.Tf'] tf_record = record['wrapper']['prepare_module_identity'] assert tf_record['module'] == 'pxr.Tf' assert tf_record['observation'] == binding.module_diagnostic('pxr.Tf') assert tf_record['wrapper_source'] == record['wrapper']['prepare_module_source'] assert tf_record['wrapper_source']['path'] == tf.__file__ assert tf_record['native']['path'] == sys.modules['pxr.Tf._tf'].__file__ assert tf_record['native']['sha256'] == binding.sha256(Path(sys.modules['pxr.Tf._tf'].__file__).read_bytes()) assert tf_record['extension_manifest']['path'] == str(Path(tf.__file__).parents[2] / 'config/extension.toml') if wrapper.__name__ != 'pxr.Usd': assert tf_record['extension_manifest']['path'] != record['wrapper']['extension_manifest']['path'] assert not (Path(wrapper.__file__).parents[1] / 'Tf').exists() # Tf's self-use is finite, not recursive or dependent on an invented spec. assert binding._dependency_module_identity('pxr.Tf')['wrapper']['prepare_module_identity'] == tf_record @pytest.mark.parametrize('change', ['name', 'object', 'loader', 'spec_loader', 'binary', 'wrapper']) def test_pxr_rejects_foreign_or_replaced_native_identity(pxr_wrapper, monkeypatch, tmp_path, change): wrapper, native = pxr_wrapper if change == 'name': native.__spec__.name = wrapper.__name__ + '._foreign' elif change == 'object': wrapper.__spec__ = importlib.util.spec_from_file_location(native.__name__, native.__file__) elif change == 'loader': wrapper.__loader__ = object() elif change == 'spec_loader': native.__spec__.loader = importlib.machinery.ExtensionFileLoader(native.__name__, native.__file__) elif change == 'binary': foreign = tmp_path / '_foreign.so' foreign.write_bytes(b'foreign') native.__file__ = native.__spec__.origin = str(foreign) else: foreign = tmp_path / '__init__.py' foreign.write_bytes(Path(wrapper.__file__).read_bytes()) wrapper.__file__ = str(foreign) with pytest.raises(ValueError): binding._dependency_module_identity(wrapper.__name__) @pytest.mark.parametrize('change', [ 'missing', 'swapped', 'foreign', 'source_missing', 'source_changed', 'native_missing', 'native_foreign', 'spec_replaced', 'loader_replaced', 'manifest_missing', 'manifest_changed', 'manifest_unpinned', 'manifest_swapped', 'manifest_foreign', 'schema_manifest_missing', 'schema_manifest_changed', ]) def test_pxr_rejects_missing_swapped_foreign_tf_and_manifests(pxr_wrapper, monkeypatch, tmp_path, change): wrapper, _ = pxr_wrapper tf, native = sys.modules['pxr.Tf'], sys.modules['pxr.Tf._tf'] path = Path(tf.__file__) if change == 'missing': monkeypatch.delitem(sys.modules, 'pxr.Tf') elif change == 'swapped': monkeypatch.setitem(sys.modules, 'pxr.Tf', wrapper) elif change == 'foreign': # Identical bytes under even the pinned schema owner cannot replace Tf. foreign = tmp_path / 'extscache/omni.usd.schema.physx-pinned/pxr/Tf/__init__.py' foreign.parent.mkdir(parents=True) foreign.write_bytes(path.read_bytes()) tf.__file__ = str(foreign) elif change == 'source_missing': path.unlink() elif change == 'source_changed': path.write_text('# changed Tf\n') elif change == 'native_missing': monkeypatch.delitem(sys.modules, native.__name__) elif change == 'native_foreign': foreign = tmp_path / '_tf.so' foreign.write_bytes(Path(native.__file__).read_bytes()) native.__file__ = native.__spec__.origin = native.__loader__.path = str(foreign) elif change == 'spec_replaced': tf.__spec__ = importlib.util.spec_from_file_location(native.__name__, native.__file__) elif change == 'loader_replaced': tf.__loader__ = object() else: owner = Path(wrapper.__file__) if change.startswith('schema_') else path manifest = owner.parents[2] / 'config/extension.toml' if change.endswith('missing'): manifest.unlink() elif change.endswith('changed'): manifest.write_text('# changed owner\n') elif change.endswith('swapped'): other = next(Path(p) for p in binding.RUNTIME_LAYOUT_PINS if p != str(manifest)) manifest.write_bytes(other.read_bytes()) elif change.endswith('foreign'): foreign = tmp_path / 'foreign-extension.toml' foreign.write_bytes(manifest.read_bytes()) manifest.unlink() manifest.symlink_to(foreign) else: monkeypatch.delitem(binding.RUNTIME_LAYOUT_PINS, str(manifest)) with pytest.raises((ValueError, FileNotFoundError)): binding._dependency_module_identity(wrapper.__name__) def test_pxr_tf_binds_actual_raw_alias_not_schema_adjacent_path(pxr_wrapper, monkeypatch, tmp_path): wrapper, _ = pxr_wrapper tf = sys.modules['pxr.Tf'] physical = Path(tf.__file__) alias = tmp_path / 'usd-libs-alias' alias.symlink_to(physical.parents[2]) monkeypatch.setattr(binding, 'RUNTIME_LINKS', ( (str(alias), str(alias.readlink()), alias.lstat().st_dev, alias.lstat().st_ino),)) tf.__file__ = str(alias / 'pxr/Tf/__init__.py') record = binding._dependency_module_identity(wrapper.__name__)['wrapper'] source = record['prepare_module_source'] assert source['raw_path'] == tf.__file__ assert source['path'] == str(physical) assert source['links'][0]['path'] == str(alias) assert record['prepare_module_identity']['wrapper_source'] == source def test_installed_tf_prepare_module_copies_native_spec_loader_read_only(): # Execute only the inspected pure-Python definition, never import pxr/Kit or dlopen. _, filename, digest = binding.REQUIRED_RUNTIME['pxr.Tf'] source = Path(filename).read_bytes() assert binding.sha256(source) == digest tree = ast.parse(source) prepare = next(node for node in tree.body if isinstance(node, ast.FunctionDef) and node.name == 'PrepareModule') definitions = {} exec(compile(ast.Module(body=[prepare], type_ignores=[]), filename, 'exec'), definitions) native = ModuleType('pxr.Tf._tf') native.__file__ = '/not-loaded/_tf.so' native.__spec__ = importlib.util.spec_from_file_location(native.__name__, native.__file__) native.__loader__ = native.__spec__.loader result = {'__name__': 'pxr.Tf', '__file__': filename, '__package__': 'pxr.Tf', '__path__': ['original']} definitions['PrepareModule'](native, result) assert result['__spec__'] is native.__spec__ assert result['__loader__'] is native.__loader__ assert result['__name__'] == result['__package__'] == 'pxr.Tf' assert result['__file__'] == filename and result['__path__'] == ['original'] assert any(isinstance(node, ast.Expr) and isinstance(node.value, ast.Call) and isinstance(node.value.func, ast.Name) and node.value.func.id == 'PreparePythonModule' for node in tree.body) @pytest.fixture(params=['omni', 'usdrt', 'omni.metrics.assembler.core.bindings', 'omni.metrics.assembler.core.scripts', 'omni.kit', 'usdrt.internal.namespace']) def provider(request, tmp_path, monkeypatch): name = request.param for key in tuple(sys.modules): if key.split('.')[0] in ('omni', 'usdrt'): monkeypatch.delitem(sys.modules, key) monkeypatch.setattr(binding, 'SIM_ROOT', tmp_path) extension = tmp_path / 'exts/provider' directory = extension.joinpath(*name.split('.')) directory.mkdir(parents=True) (extension / 'config').mkdir() manifest = extension / 'config/extension.toml' manifest.write_text('[[python.module]]\nname = "' + name + '.child"\n') (directory / 'child.py').write_text('VALUE = 1\n') monkeypatch.syspath_prepend(str(extension)) for child in ['.'.join(name.split('.')[:i]) for i in range(1, len(name.split('.')) + 1)] + [name + '.child']: monkeypatch.setitem(sys.modules, child, None) del sys.modules[child] module = importlib.import_module(name) importlib.import_module(name + '.child') return name, module, manifest def test_explicit_provider_records_manifest_children_and_limitation(provider): name, module, manifest = provider record = binding._dependency_module_identity(name) assert record['kind'] == 'kit-provider-namespace' assert record['implementation_identity'] == 'unobserved dynamic-provider internals' assert record['imported_child_files'][name + '.child']['sha256'] assert record['extension_manifests'][str(manifest)]['sha256'] assert 'sha256' not in record and not module.__file__ manifest.write_text('# changed manifest\n') assert record != binding._dependency_module_identity(name) @pytest.mark.parametrize('change', ['loader', 'manifest', 'child', 'foreign', 'forged-path', 'changed-location']) def test_provider_rejects_unbound_evidence(provider, monkeypatch, tmp_path, change): name, module, manifest = provider if change == 'loader': module.__loader__ = object() elif change == 'manifest': manifest.unlink() elif change == 'child': monkeypatch.delitem(sys.modules, name + '.child') elif change == 'forged-path': module.__path__ = module.__spec__.submodule_search_locations = module.__loader__._path = list(module.__path__) elif change == 'changed-location': other = tmp_path / 'foreign' other.mkdir() module.__path__._path[:] = [str(other)] else: monkeypatch.setattr(binding, 'SIM_ROOT', tmp_path / 'foreign') with pytest.raises(ValueError): binding._dependency_module_identity(name) def test_frozen_verifier_real_artifacts_unchanged(): if not (ROOT / replay.CANDIDATE).is_file(): pytest.skip('frozen artifacts absent; never synthesize a candidate') manifest, record = replay.verify_bundle(ROOT) assert record['candidate_sha256'] == replay.CANDIDATE_SHA256 assert record['manifest_sha256'] == replay.MANIFEST_SHA256 assert record['snapshot_files_verified'] == len(manifest['sources']) def test_unknown_provider_is_not_an_allowlist_skip(tmp_path, monkeypatch): unknown = ModuleType('omni.unknown') monkeypatch.setitem(sys.modules, unknown.__name__, unknown) with pytest.raises(ValueError, match='Unbound provider namespace spec'): binding._dependency_module_identity(unknown.__name__) foreign = ModuleType('other_provider') monkeypatch.setitem(sys.modules, foreign.__name__, foreign) with pytest.raises(ValueError, match='Unreviewed fileless provider'): binding._dependency_module_identity(foreign.__name__) def test_diagnostic_does_not_recalculate_namespace_path(namespace): # noqa: F811 module, directory = namespace module.__path__._path_finder = lambda *args: pytest.fail('must not rediscover') before = dict(vars(module.__path__)) report = binding.module_diagnostic(module.__name__) assert report['search_locations'] == [str(directory)] assert report['spec_name'] == module.__name__ assert vars(module.__path__) == before binding.canonical_json(report)