Files
iOSGuard/tests/test_gcd_file_integration.py
T
shenlei 704ee66834 feat: 集成 LLVM IR 混淆体系、Route 2 全量合规与系统调用调度加固
- defender: 扩展 IGDispatch/IGArchiveGuard/IGAntiHook/IGObjCBaseline/IGResolve,支持 Route 2 静态库桥接 (IGThreadID.c) 与防重签名检测
- llvm_passes: 新增 IGObfuscator LLVM IR 插件源码(控制流平坦化、字符串加密、间接跳转/调用、不透明常量/字段、MBA、函数外提、基本块拆分、反 Hook 序言)
- tools & config: 新增 Xcode 归档编译/链接包装器、LLVM 自愈工具链 (ig_toolchain_resolver)、IPA 审计门禁、崩溃符号还原及最新 ig_llvm_policy.json(文件级匹配 + 3.0 全业务 Handler 平坦化)
- tests & docs: 补齐 M0~M3、Pass 单测及系统调用调度/Route 2/LLVM 方案文档;.gitignore 忽略 *.bc 与 .obfuscate_extraction_cache.json
2026-11-24 15:00:07 +09:00

83 lines
4.3 KiB
Python

"""编译 App 副本的真实 GCDWebServer 和 Defender,验证文件流程与异常处置。"""
from pathlib import Path
import os
import signal
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
APP = ROOT / 'readoor-test/readoor'
GCD = APP / 'BookShelf/Classes/Factorys/GCDWebServer'
DEFENDER = APP / '3.0/Base/Security/IGDefender'
@unittest.skipUnless(sys.platform == 'darwin' and GCD.exists(), '需要 macOS 和 App 副本')
class GCDFileIntegrationTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.tmp = tempfile.TemporaryDirectory()
cls.exe = Path(cls.tmp.name) / 'gcd-file-test'
cls.env = dict(os.environ)
cls.env.pop('DYLD_INSERT_LIBRARIES', None)
cls.includes = [arg for p in (GCD/'Core', GCD/'Requests', GCD/'Responses',
DEFENDER, DEFENDER/'include') for arg in ('-I', str(p))]
sources = [str(p) for p in GCD.rglob('*.m')]
sources += [str(DEFENDER/p) for p in ('dispatch/IGDispatch.c', 'resolve/IGResolve.c',
'anti_hook/IGAntiHook.c')]
command = ['xcrun', '--sdk', 'macosx', 'clang', '-O2', '-fobjc-arc',
'-DIG_DISPATCH_TESTING', '-DIG_HOOK_TERMINATE_DEFAULT=0',
*cls.includes, str(ROOT/'tests/fixtures/gcd_file_integration.m'), *sources,
'-framework', 'Foundation', '-framework', 'CFNetwork',
'-framework', 'SystemConfiguration', '-framework', 'Security',
'-framework', 'CoreServices',
'-lz', '-o', str(cls.exe)]
result = subprocess.run(command, capture_output=True, text=True, env=cls.env)
if result.returncode:
raise RuntimeError(result.stderr)
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def run_mode(self, mode, expected=0):
result = subprocess.run([str(self.exe), mode], capture_output=True, text=True,
env=self.env, timeout=30)
self.assertEqual(result.returncode, expected, result.stdout + result.stderr)
def test_upload_range_read_and_cleanup(self): self.run_mode('normal')
def test_multipart_stream_and_cleanup(self): self.run_mode('multipart-normal')
def test_multipart_failed_write(self): self.run_mode('multipart-tamper')
def test_descriptor_zero_is_valid(self): self.run_mode('fd-zero')
def test_open_tamper_creates_no_file(self): self.run_mode('open-tamper')
def test_write_tamper_writes_no_data(self): self.run_mode('write-tamper')
def test_read_tamper_returns_error(self): self.run_mode('read-tamper')
def test_existing_termination_policy_is_preserved(self):
result = subprocess.run([str(self.exe), 'terminate'], capture_output=True, text=True,
env=self.env, timeout=30)
self.assertIn(result.returncode, (-signal.SIGTRAP, -signal.SIGILL), result.stdout + result.stderr)
def test_ios_release_objects_use_protected_entries(self):
for relative, protected in [
('Core/GCDWebServer.m', {'close'}),
('Core/GCDWebServerConnection.m', {'close'}),
('Requests/GCDWebServerMultiPartFormRequest.m', {'open', 'write', 'close', 'unlink'}),
('Requests/GCDWebServerFileRequest.m', {'open', 'write', 'close', 'unlink'}),
('Responses/GCDWebServerFileResponse.m', {'open', 'read', 'close'})
]:
obj = Path(self.tmp.name) / (Path(relative).stem + '.o')
result = subprocess.run(['xcrun', '--sdk', 'iphoneos', 'clang', '-arch', 'arm64',
'-miphoneos-version-min=15.0', '-O2', '-fobjc-arc', *self.includes,
'-c', str(GCD/relative), '-o', str(obj)], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
result = subprocess.run(['xcrun', 'nm', '-u', str(obj)],
capture_output=True, text=True, check=True)
symbols = {line.split()[-1] for line in result.stdout.splitlines() if line.split()}
self.assertTrue({'_IG_dispatch_' + n for n in protected} <= symbols, symbols)
self.assertFalse({'_' + n for n in protected} & symbols, symbols)
if __name__ == '__main__':
unittest.main()