import importlib.util import pathlib import tempfile import unittest spec = importlib.util.spec_from_file_location('runtime', pathlib.Path(__file__).with_name('corporate_runtime.py')) r = importlib.util.module_from_spec(spec) spec.loader.exec_module(r) class Safety(unittest.TestCase): def test_urls_fail_closed(self): for url in ['http://example.com/', 'https://localhost/', 'https://127.0.0.1/', 'https://example.com:444/', 'https://user:pass@example.com/', 'file:///etc/passwd']: with self.subTest(url=url), self.assertRaises(ValueError): r.source_target(url, {'example.com'}) def test_only_exact_operation(self): for desc in ['rm -rf /', '{"operation":"publish"}', '{"operation":"source_inventory_v1","researchId":"x","command":"id"}']: with self.assertRaises(ValueError): r.operation(desc) def test_store_digest_tamper(self): with tempfile.TemporaryDirectory() as tmp: store = r.Store(tmp) digest = store.put(b'actual bytes') self.assertEqual(store.get(digest), b'actual bytes') (pathlib.Path(tmp) / digest).write_bytes(b'changed') with self.assertRaises(ValueError): store.get(digest) def test_artifact_recomputed(self): research = {'id':'research', 'sources':[{'url':'https://example.com/', 'sha256':r.sha(b'hello'), 'retrievedAt':'now'}]} with tempfile.TemporaryDirectory() as tmp: store = r.Store(tmp); store.put(b'hello') artifact = r.inventory('task', research, store) self.assertEqual(artifact['sources'][0]['bytes'],5) self.assertEqual(artifact, r.inventory('task',research,store)) def test_checkpoint_replays_exact_receipt(self): client = object.__new__(r.Client) calls = [] client.call = lambda path, body, key: calls.append((path, body, key)) or {'id': 'persisted'} with tempfile.TemporaryDirectory() as tmp: store = r.Store(tmp) path = 'tasks/618a6800-d241-41e2-b093-969164c83cfa/evidence' digest = client.checkpoint(store, 'stable-key', path, {'result': 'original timestamp'}) self.assertEqual(client.replay(store, digest), {'id': 'persisted'}) self.assertEqual(calls, [(path, {'result':'original timestamp'}, 'stable-key')]) def test_api_target(self): with self.assertRaises(ValueError): r.api_target('http://example.com/api/admin/hermes',False) with self.assertRaises(ValueError): r.api_target('https://example.com/api/admin/hermes',False) self.assertEqual(r.api_target('http://127.0.0.1:1234/api/admin/hermes',False),'http://127.0.0.1:1234/api/admin/hermes') if __name__ == '__main__': unittest.main()