"""编译 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()