RoboHome / code /robohome /src /engine /tests /test_lazy_geometry_identity.py
RoboParty Release
Unify robohome source, assets and scenes delivery
3f15e9f
Raw History Blame Contribute Delete
5.18 kB
"""Supervisor identity must remain stdlib-only until actual mesh auditing."""
import hashlib
import json
from pathlib import Path
import subprocess
import sys
import unittest
ROOT=Path(__file__).resolve().parents[1]
class LazyGeometryIdentityTests(unittest.TestCase):
def run_isolated(self,script):
result=subprocess.run([sys.executable,'-I','-S','-c',script],
text=True,capture_output=True,check=False)
self.assertEqual(result.returncode,0,result.stdout+result.stderr)
return json.loads(result.stdout)
def test_public_runtime_identity_needs_only_stdlib(self):
script=f'''import sys,json
sys.path.insert(0,{str(ROOT)!r})
import run_chain
receipt=run_chain.runtime_implementation_identity()
assert 'numpy' not in sys.modules
assert 'triangle_contacts' not in sys.modules
assert len(receipt['validator']['helper_sha256'])==4
print(json.dumps(receipt))
'''
self.run_isolated(script)
def test_geometry_disabled_dry_run_needs_only_stdlib(self):
# The local checkout lacks its frozen BASE_SHA256SUMS inventory; mock
# that one unrelated filesystem check, while all preflight code runs.
script=f'''import sys,json,tempfile
from pathlib import Path
from unittest.mock import patch
sys.path.insert(0,{str(ROOT)!r})
import run_chain
with tempfile.TemporaryDirectory() as folder:
root=Path(folder)
manifest=dict(scene_id='review_light',floor_plan=dict(rooms=[dict(room_id='living')]),entities=[dict(entity_id='table',room_id='living',role='side_table',asset_id='native',dimensions_m=[1.,1.,.7],center_xy_m=[0.,0.],yaw_deg=0.)])
request=dict(schema='rhg.scene_request.v1',mode='locked',task_text='Read-only synthetic preflight',furniture=[dict(entity_id='table',asset_id='native')])
config=dict(schema='rhg.frontier-chain-config.v1',manifest='manifest.json',request='request.json',geometry_validation=dict(enabled=False))
for name,value in [('manifest',manifest),('request',request),('config',config)]:
(root/(name+'.json')).write_text(json.dumps(value))
with patch.object(run_chain,'base_identity',return_value=dict(passed=True)):
result=run_chain.run_chain(root/'config.json',root/'output',execute=False)
assert result['status']=='preflight_complete',result
assert 'numpy' not in sys.modules
assert 'triangle_contacts' not in sys.modules
print(json.dumps(dict(status=result['status'],numpy_loaded=False)))
'''
self.run_isolated(script)
def test_package_identity_needs_only_stdlib_and_pins_unloaded_helper(self):
script=f'''import sys,json,hashlib
from pathlib import Path
sys.path.insert(0,{str(ROOT.parent)!r})
from engine import geometry_validation as gv, runtime_identity
identity=runtime_identity.validate_validator(gv,gv.BASE)
assert 'numpy' not in sys.modules
assert 'engine.triangle_contacts' not in sys.modules
assert identity['helper_sha256']['triangle_contacts.py']==hashlib.sha256((gv.BASE/'triangle_contacts.py').read_bytes()).hexdigest()
print(json.dumps(identity))
'''
self.run_isolated(script)
def test_unloaded_helper_byte_change_rejected(self):
script=f'''import sys,json
from pathlib import Path
sys.path.insert(0,{str(ROOT)!r})
import geometry_validation as gv
original=gv._file_sha256
gv._file_sha256=lambda p: '0'*64 if Path(p).name=='triangle_contacts.py' else original(p)
try:gv.implementation_identity()
except ValueError as error:assert 'helper' in str(error)
else:raise AssertionError('Changed unloaded helper was accepted')
print(json.dumps({{'rejected':True}}))
'''
self.run_isolated(script)
def test_later_loaded_helper_from_foreign_workspace_rejected(self):
for package in ('','engine.'):
with self.subTest(package=package):
parent=ROOT.parent if package else ROOT
script=f'''import sys,json,types,importlib
sys.path.insert(0,{str(parent)!r})
gv=importlib.import_module({package+'geometry_validation'!r})
gv.implementation_identity()
sys.modules[{package+'triangle_contacts'!r}]=types.SimpleNamespace(__file__='/foreign/triangle_contacts.py')
try:gv.implementation_identity()
except ValueError as error:assert 'helper' in str(error)
else:raise AssertionError('Foreign loaded helper was accepted')
print(json.dumps({{'rejected':True}}))
'''
self.run_isolated(script)
def test_actual_contact_import_is_checked_after_geometry_work(self):
from engine import geometry_validation as gv
before=gv.implementation_identity()
# Actual geometry path imports and executes the real helper.
contact=gv._triangle_contact([dict(vertices=[[0.,0.,0.],[1.,0.,0.],[0.,1.,0.]],
triangles=[[0,1,2]])],[],(0.,0.,-1.),1e-5)
self.assertFalse(contact['passed'])
from engine import triangle_contacts
self.assertEqual(Path(triangle_contacts.__file__).resolve(),ROOT/'triangle_contacts.py')
self.assertEqual(gv.implementation_identity(),before)
from unittest.mock import patch
with patch.object(triangle_contacts,'__file__','/foreign/triangle_contacts.py'):
with self.assertRaisesRegex(ValueError,'helper'):gv.implementation_identity()