import pathlib import struct import tempfile import unittest from .testing import build_script HEADER = "\n" for name, body in ((b"__.SYMDEF SORTED\x00\x00\x00\x00", b"symdef\x00\x00"), (b"assert.o\x00\x00\x00\x00", dev)): size = len(name) + len(body) header = ( f"#1/{len(name)}".ljust(16) + "0".ljust(12) + "0".ljust(6) + "0".ljust(6) + "100644".ljust(8) + str(size).ljust(10) + "`\n" ) members += header.encode() + name + body lib.write_bytes(members) self.assertEqual(build_script.simulator_archive(lib), 1) ((header, name, contents),) = build_script.archive_members(lib.read_bytes()) self.assertEqual(name, b"assert.o\x00\x00\x00\x00") self.assertEqual(int(header[48:58]), len(name) + len(dev) + 16) self.assertEqual(contents, build_script.simulator_object(dev)) EXECUTE = 0x2 CODE_START = 0x400 PAGE = 0x4000 def executable(code=DATA * 8, linkedit=b"symbols!" * 3, room=True): """A thin arm64 executable as ld leaves it unsigned: __TEXT and __LINKEDIT, the code and the __LINKEDIT data, and free space after the load commands unless room is False.""" sizeofcmds = 2 * 72 linkedit_off = CODE_START + len(code) text = struct.pack("II", 0xCAFEBABE, len(slices)) offset, arches, body = PAGE, b"", b"" for i, thin in enumerate(slices): arches += struct.pack(">iiIII", CPU_ARM64 + i, 0, offset, len(thin), 14) body += bytes(offset - PAGE - len(body)) + thin offset += -(-len(thin) // PAGE) * PAGE return header + arches + bytes(PAGE - len(header) - len(arches)) + body DEVELOPER_ID = b"\xfa\xde\x0c\xc0" + b"cms and timestamp" * 40 AD_HOC = b"\xfa\xde\x0c\xc0" + b"ad hoc" * 3 class CodeSignatureTest(unittest.TestCase): def test_signature_of_a_release_is_copied_onto_an_unsigned_build(self): built = executable() released = codesign(built, DEVELOPER_ID) self.assertEqual(build_script.copy_code_signature(released, built), released) self.assertTrue(build_script.same_code(released, built)) def test_signature_of_a_release_replaces_the_ad_hoc_signature_of_a_build(self): built = codesign(executable(), AD_HOC) released = codesign(built, DEVELOPER_ID) self.assertNotEqual(len(built), len(released)) self.assertEqual(build_script.copy_code_signature(released, built), released) def test_a_signed_build_of_other_code_does_not_match(self): released = codesign(executable(), DEVELOPER_ID) other_code = executable(code=DATA * 7 + b"\x00\x00\x00\x00" * 4) other_symbols = codesign(executable(linkedit=b"symbols?" * 3), AD_HOC) for built in (other_code, other_symbols, executable(code=DATA * 9)): self.assertFalse(build_script.same_code(released, built)) def test_signature_command_does_not_hide_a_build_with_no_free_space(self): released = codesign(executable(), DEVELOPER_ID) crowded = executable(room=False) self.assertEqual(build_script.copy_code_signature(released, crowded), crowded) self.assertFalse(build_script.same_code(released, crowded)) def test_an_unsigned_release_and_other_files_are_compared_as_they_are(self): built = executable() self.assertTrue(build_script.same_code(built, built)) self.assertFalse(build_script.same_code(executable(code=DATA * 9), built)) self.assertFalse(build_script.same_code(b"#!/bin/sh\n", b"#!/bin/bash\n")) self.assertEqual(build_script.copy_code_signature(b"text", built), built) def test_each_architecture_of_a_universal_file_gets_its_signature(self): thin = executable(), executable(code=DATA * 16) built = universal(codesign(thin[0], AD_HOC), thin[1]) released = universal(codesign(thin[0], DEVELOPER_ID), codesign(thin[1], DEVELOPER_ID)) self.assertTrue(build_script.same_code(released, built)) self.assertFalse(build_script.same_code(released, universal(thin[1], thin[0]))) self.assertFalse(build_script.same_code(released, universal(thin[0]))) def test_java_classes_are_not_universal_files(self): java_class = struct.pack(">IHH", 0xCAFEBABE, 0, 65) + b"class" self.assertFalse(build_script.is_macho(java_class)) self.assertTrue(build_script.is_macho(universal(executable()))) def test_truncated_files_do_not_match(self): released = codesign(executable(), DEVELOPER_ID) self.assertFalse(build_script.same_code(released, released[:40]))