"""Exercise legacy lazy binding on the reviewed AliyunPlayer arm64 SDK.""" import hashlib from pathlib import Path import struct import sys import unittest import zipfile ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tools/macho_guard')) from batch import arm64_slices, Slice from redirect_legacy_fopen import rewrite from machox import Macho IPA = ROOT / 'appstore.ipa' @unittest.skipUnless(IPA.exists(), '需要归档 IPA 中的 AliyunPlayer') class LegacyFopenTests(unittest.TestCase): @classmethod def setUpClass(cls): with zipfile.ZipFile(IPA) as archive: names = [n for n in archive.namelist() if n.endswith('/AliyunPlayer.framework/AliyunPlayer')] if len(names) != 1: raise ValueError('需要唯一 AliyunPlayer') data = archive.read(names[0]) offset, size = arm64_slices(data)[0] cls.data = data[offset:offset + size] def apply(self, data): return rewrite(data, hashlib.sha256(data).hexdigest()) def test_real_lazy_binding(self): result, report = self.apply(self.data) self.assertTrue(report['lazy_offsets_preserved']) self.assertTrue(report['section_bytes_unchanged']) self.assertEqual(Slice(self.data, 0).scan()['_fopen'], 28) self.assertEqual(Slice(result, 0).scan()['_fopen'], 0) self.assertEqual(len(result), len(self.data)) before = Macho(self.data) after = Macho(result) old = next(o for c, _, o in before.cmds if c == 0x80000022) new = next(o for c, _, o in after.cmds if c == 0x80000022) self.assertEqual(self.data[old + 8:old + 48], result[new + 8:new + 48]) self.assertEqual(report['new_ordinal'], 33) def test_hash_mismatch(self): with self.assertRaises(ValueError): rewrite(self.data, '0' * 64) def test_repeat_rejected(self): result, _ = self.apply(self.data) with self.assertRaises(ValueError): self.apply(result) def test_no_header_space(self): data = bytearray(self.data) data[32 + Macho(data).sizeofcmds] = 1 with self.assertRaises(ValueError): self.apply(bytes(data)) def test_wrong_lazy_provider(self): data = bytearray(self.data) m = Macho(data) off = next(o for c, _, o in m.cmds if c == 0x80000022) lazy, size = struct.unpack_from('