"""处置链宿主自测:poison + policy + anti_jailbreak 闭环(检测→分级→数据毒化)。 固化此前只在临时目录跑过的闭环为可复现基线,作为真机验证(docs/19)前的宿主绿灯。 编译 tests/fixtures/poison_host.c 并运行,退出码 0=全部用例通过。 """ from pathlib import Path import os import shutil import subprocess import sys import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] SOURCES = [ 'poison/IGPoison.c', 'policy/IGPolicy.c', 'anti_jailbreak/IGAntiJailbreak.c', 'syscall/IGSyscall.c', 'IGDefender.c', 'anti_hook/IGAntiHook.c', 'anti_hook/IGObjCBaseline.c', 'anti_frida/IGAntiInject.c', 'anti_debug/IGAntiDebug.c', 'resolve/IGResolve.c', 'dispatch/IGDispatch.c', 'dispatch/IGArchiveGuard.c', 'call_bridge/IGFileBridge.c', 'anti_tamper/IGAntiTamper.c', ] @unittest.skipUnless(sys.platform == 'darwin' and shutil.which('xcrun'), '需要 Xcode/macOS') class PoisonPolicyTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.tmp = tempfile.TemporaryDirectory() folder = Path(cls.tmp.name) cls.exe = folder / 'poison-host' cls.env = dict(os.environ) cls.env.pop('DYLD_INSERT_LIBRARIES', None) args = ['xcrun', '--sdk', 'macosx', 'clang', '-O2', '-I', str(ROOT / 'defender/include'), '-I', str(ROOT / 'defender'), str(ROOT / 'tests/fixtures/poison_host.c'), *(str(ROOT / 'defender' / p) for p in SOURCES), '-framework', 'Foundation', '-lobjc', '-o', str(cls.exe)] p = subprocess.run(args, capture_output=True, text=True, env=cls.env) if p.returncode: raise RuntimeError(p.stdout + p.stderr) @classmethod def tearDownClass(cls): cls.tmp.cleanup() def test_chain(self): p = subprocess.run([str(self.exe)], capture_output=True, text=True, env=self.env) # 保留逐例证据到 unittest 日志。 print(p.stdout.strip(), flush=True) self.assertEqual(p.returncode, 0, p.stdout + p.stderr) self.assertIn('0 fail', p.stdout) if __name__ == '__main__': unittest.main()