883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
53 lines
3.9 KiB
Python
53 lines
3.9 KiB
Python
import hashlib,importlib,json,sys,tempfile,types,unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
import engine_channel_policy_host as policy
|
|
import engine_wire_host as wire
|
|
class WindowsPolicy(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp=tempfile.TemporaryDirectory();self.addCleanup(self.temp.cleanup);self.root=Path(self.temp.name)
|
|
self.image=self.root/'runtime/native-client/_classic_beta_/WowB.exe';self.image.parent.mkdir(parents=True);self.image.write_bytes(b'owned-image-fixture')
|
|
self.meta={'windows_native_bootstrap':True,'pid':77,'starttime':123,'image_path':str(self.image),'patched_sha256':self.sha(self.image),'wire_unix_library':{}}
|
|
for role in ('core','wire_unix','source_codec'):
|
|
p=self.root/'build'/(role+'.dll');p.parent.mkdir(exist_ok=True);p.write_bytes(role.encode());self.meta['wire_unix_library'].update({role+'_path':str(p),role+'_sha256':self.sha(p)})
|
|
self.save();self.closed=[]
|
|
self.access=types.SimpleNamespace(handle=object(),close=lambda:self.closed.append(True))
|
|
kernel=types.SimpleNamespace(identity=lambda handle:(123,str(self.image).upper()))
|
|
module=types.SimpleNamespace(Kernel=lambda:kernel)
|
|
for patch in (mock.patch.object(policy,'ROOT',self.root),mock.patch.object(policy,'_WINDOWS',True),mock.patch.object(policy.host,'Access',lambda pid:self.access),mock.patch.dict(sys.modules,{'windows_native_bootstrap':module})):
|
|
patch.start();self.addCleanup(patch.stop)
|
|
self.kernel=kernel
|
|
def sha(self,p):return hashlib.sha256(p.read_bytes()).hexdigest()
|
|
def save(self):(self.root/'runtime/native-engine-pe.json').write_text(json.dumps(self.meta))
|
|
def test_owned_bootstrap_process_uses_actual_creation_identity(self):
|
|
self.assertEqual(policy._request_process(77),'123');self.assertEqual(self.closed,[True])
|
|
def test_stale_or_foreign_bootstrap_identity_refused(self):
|
|
for changed in ({'pid':78},{'starttime':124},{'windows_native_bootstrap':False},{'image_path':str(self.root/'foreign.exe')}):
|
|
original=dict(self.meta);self.meta.update(changed);self.save()
|
|
with self.assertRaises(ValueError):policy._request_process(77)
|
|
self.meta=original
|
|
def test_actual_process_creation_or_path_change_refused(self):
|
|
for value in ((124,str(self.image)),(123,str(self.root/'foreign.exe'))):
|
|
self.kernel.identity=lambda handle,value=value:value
|
|
with self.assertRaises(ValueError):policy._request_process(77)
|
|
self.assertEqual(len(self.closed),2)
|
|
def test_exact_library_identity_casefolded_but_foreign_path_and_digest_held(self):
|
|
rows=[(1,2,'r-x','00000000',str(Path(p)).upper().replace('/','\\')) for k,p in self.meta['wire_unix_library'].items() if k.endswith('_path')]
|
|
access=types.SimpleNamespace(maps=rows);policy._request_libraries(access,self.meta)
|
|
access.maps=rows[:-1]
|
|
with self.assertRaises(ValueError):policy._request_libraries(access,self.meta)
|
|
access.maps=rows;self.image.write_bytes(b'changed')
|
|
with self.assertRaises(ValueError):policy._request_libraries(access,self.meta)
|
|
def test_windows_map_refresh_uses_access_adapter(self):
|
|
seen=[];policy._refresh_request_maps(types.SimpleNamespace(refresh_maps=lambda:seen.append(True)),77);self.assertEqual(seen,[True])
|
|
def test_wire_module_matches_exact_casefolded_windows_path(self):
|
|
path=self.root/'runtime/native-wine-prefix/drive_c/windows/system32/GWNativeWire.dll';path.parent.mkdir(parents=True);path.write_bytes(b'wire-fixture')
|
|
self.meta['auxiliary_dependencies']=[{'file':'GWNativeWire.dll','sha256':self.sha(path),'exports':{}}];self.save()
|
|
access=types.SimpleNamespace(maps=[(0x1000,0x2000,'r-x','00000000',str(path).upper().replace('/','\\'))])
|
|
with mock.patch.object(wire,'ROOT',self.root),mock.patch.object(wire,'_WINDOWS',True):
|
|
self.assertEqual(wire.module(access)[0],0x1000)
|
|
access.maps[0]=(*access.maps[0][:-1],str(path)+'-foreign')
|
|
with self.assertRaises(ValueError):wire.module(access)
|
|
if __name__=='__main__':unittest.main()
|