#!/usr/bin/env python3 """Host regressions and optional build against the real, pinned OS inputs.""" import argparse import ctypes import hashlib import json import plistlib import shutil from pathlib import Path import struct import subprocess import sys import tempfile import unittest from macho import add_dylib import build import signer ROOT = Path(__file__).resolve().parent def digest(data): return hashlib.sha256(data).hexdigest() def fixture(): name = b'/System/Library/Frameworks/Foundation.framework/Foundation\0' length = (24 + len(name) + 7) & ~7 header = struct.pack('<8I', 0xfeedfacf, 0x100000c, 0x80000002, 2, 1, length, 0, 0) command = struct.pack('<6I', 0xc, length, 24, 0, 0, 0) + name return header + command.ljust(length, b'\0') + b'CODE AND CHAINED FIXUPS' class MachOTests(unittest.TestCase): def test_dependency_preserves_header_ordinals_and_code(self): original = fixture() patched = add_dylib(original, build.DYLIB, digest(original)) self.assertEqual(len(original), len(patched)) self.assertEqual(original[:56], patched[:56]) self.assertEqual(original[-22:], patched[-22:]) self.assertEqual(patched.count(build.DYLIB.encode()), 1) def test_foreign_or_modified_os_rejected(self): with self.assertRaisesRegex(ValueError, 'source pin'): add_dylib(fixture() + b'changed', build.DYLIB, digest(fixture())) def test_bad_command_and_duplicate_dependency_rejected(self): data = bytearray(fixture()) struct.pack_into('