def n_opcode_set_symbol_trailing_flags_imm_helper(self, teststr, immediate): s = BindingState(is_64=True) s.index = 0 blob = teststr + b"\x00" n_opcode_set_trailing_flags_imm(s, None, immediate, blob) self.assertEqual(s.sym_name, teststr.decode('ascii')) # plus one because there is the 0 byte at the end self.assertEqual(s.index, len(teststr) + 1) self.assertEqual(s.sym_flags, immediate)
def test_check_address_bounds(self): """Ensure that exception gets thrown under the right circumstances""" self.uut_64 = BindingState(True) #TODO test_check_address_bounds: Why does this raise an exception? # address and seg_end address are not 0 when entering the function #self.uut_64.address = 0 #self.uut_64.seg_end_address = 0 #self.uut_64.check_address_bounds() self.uut_64 = BindingState(True) self.uut_64.address = 0 self.uut_64.seg_end_address = 1 self.uut_64.check_address_bounds() self.uut_64 = BindingState(True) self.uut_64.address = 10000 self.uut_64.seg_end_address = 10 with self.assertRaises(CLEInvalidBinaryError): self.uut_64.check_address_bounds() self.uut_64 = BindingState(True) self.uut_64.address = -10000 self.uut_64.seg_end_address = -100000 with self.assertRaises(CLEInvalidBinaryError): self.uut_64.check_address_bounds()
def test_n_opcode_set_type_imm(self): s = BindingState(is_64=True) immediate = 1000 n_opcode_set_type_imm(s, None, immediate, None) self.assertEqual(s.binding_type, immediate) immediate = 0 n_opcode_set_type_imm(s, None, immediate, None) self.assertEqual(s.binding_type, immediate) immediate = -250 n_opcode_set_type_imm(s, None, immediate, None) self.assertEqual(s.binding_type, immediate)
def test_n_opcode_done(self): s = BindingState(is_64=True) s.done = False n_opcode_done(s, None, None, None) self.assertTrue(s.done) s = BindingState(is_64=True) s.done = True n_opcode_done(s, None, None, None) self.assertTrue(s.done)
def test_n_opcode_set_dylib_ordinal_imm(self): s = BindingState(is_64=True) const = 0x123456789 n_opcode_set_dylib_ordinal_imm(s, None, const, None) self.assertEqual(s.lib_ord, const) const = -10000 n_opcode_set_dylib_ordinal_imm(s, None, const, None) self.assertEqual(s.lib_ord, const) const = 0 n_opcode_set_dylib_ordinal_imm(s, None, const, None) self.assertEqual(s.lib_ord, const) const = 0x123456789123456789123456789 n_opcode_set_dylib_ordinal_imm(s, None, const, None) self.assertEqual(s.lib_ord, const)
def test_n_opcode_set_dylib_special_imm(self): s = BindingState(is_64=True) n_opcode_set_dylib_special_imm(s, None, 0, None) self.assertEqual(s.lib_ord, 0) n_opcode_set_dylib_special_imm(s, None, 100, None) self.assertEqual(s.lib_ord, -12) n_opcode_set_dylib_special_imm(s, None, 156, None) self.assertEqual(s.lib_ord, -4) n_opcode_set_dylib_special_imm(s, None, 1, None) self.assertEqual(s.lib_ord, -15) n_opcode_set_dylib_special_imm(s, None, 88, None) self.assertEqual(s.lib_ord, -8) n_opcode_set_dylib_special_imm(s, None, 255, None) self.assertEqual(s.lib_ord, -1)
def test_n_opcode_set_segment_and_offset_uleb(self): # pylint: disable=unused-variable s = BindingState(is_64=True) self.skipTest( "TODO: The function needs a binary with segments to test")
def n_opcode_set_addend_sleb_helper(self, blob, expected): s = BindingState(is_64=True) s.index = 0 n_opcode_set_addend_sleb(s, None, None, blob) self.assertEqual(s.addend, expected)
def n_opcode_set_dylib_ordinal_uleb_helper(self, blob, expected): s = BindingState(is_64=True) s.index = 0 n_opcode_set_dylib_ordinal_uleb(s, None, None, blob) self.assertEqual(s.lib_ord, expected)
def setUp(self): self.uut_64 = BindingState(True) self.uut_32 = BindingState(False)
class TestBindingState(unittest.TestCase): def setUp(self): self.uut_64 = BindingState(True) self.uut_32 = BindingState(False) def test_init_64(self): """Ensure that initialization works properly for 64 bit Assertions taken from ImageLoaderMachOCompressed.cpp, start of eachBind """ self.assertEqual(self.uut_64.segment_index, 0) self.assertEqual(self.uut_64.address, 0) self.assertEqual(self.uut_64.sym_flags, 0) self.assertEqual(self.uut_64.sym_name, "") self.assertEqual(self.uut_64.lib_ord, 0) self.assertEqual(self.uut_64.addend, 0) self.assertEqual(self.uut_64.done, False) def test_init_32(self): """Ensure that initialization works properly for 32 bit Assertions taken from ImageLoaderMachOCompressed.cpp, start of eachBind """ self.assertEqual(self.uut_32.segment_index, 0) self.assertEqual(self.uut_32.address, 0) self.assertEqual(self.uut_32.sym_flags, 0) self.assertEqual(self.uut_32.sym_name, "") self.assertEqual(self.uut_32.lib_ord, 0) self.assertEqual(self.uut_32.addend, 0) self.assertEqual(self.uut_32.done, False) def test_add_address_ov_32(self): """Ensure proper updating of address and wraparound (32bits)""" self.skipTest( "TODO test_add_address_ov_32: add_address_ov does not consider the 64 bit flag and does not change the " "size of uintptr_t to 2**32 so calculation will fail") self.uut_32.add_address_ov(10000, 10000) self.assertEqual(self.uut_32.address, 20000) self.uut_32.add_address_ov(699999999, 10000000) self.assertEqual(self.uut_32.address, 709999999) self.uut_32.add_address_ov(4294967295, 10000) self.assertEqual(self.uut_32.address, 9999) self.uut_32.add_address_ov(3294967295, 1000100000) self.assertEqual(self.uut_32.address, 99999) #TODO test_add_address_ov_32: we do probably do not expect negative numbers as second argument, but then the address will be negative... and it does not wrap around. Test is commented out. self.uut_32.add_address_ov(10000, -10000) self.assertEqual(self.uut_32.address, 0) self.uut_32.add_address_ov(10000, -100000) self.assertEqual(self.uut_32.address, 4294877296) def test_add_address_ov_64(self): """Ensure proper updating of address and wraparound (64bits)""" self.uut_64.add_address_ov(10000, 10000) self.assertEqual(self.uut_64.address, 20000) self.uut_64.add_address_ov(0, 0) self.assertEqual(self.uut_64.address, 0) self.uut_64.add_address_ov(699999999999, 10000000000) self.assertEqual(self.uut_64.address, 709999999999) self.uut_64.add_address_ov(1844674407370955160, 10000000000) self.assertEqual(self.uut_64.address, 1844674417370955160) self.uut_64.add_address_ov(17000000000000000000, 1900000000000000000) self.assertEqual(self.uut_64.address, 453255926290448384) #TODO test_add_address_ov_64: we do probably do not expect negative numbers as second argument, but then the address will be negative... and it does not wrap around. Test is commented out. #self.uut_64.add_address_ov(123456, -10000000000) #self.assertEqual(self.uut_64.address, 18446744063709551616) self.uut_64.add_address_ov(1000, -1000) self.assertEqual(self.uut_64.address, 0) def test_check_address_bounds(self): """Ensure that exception gets thrown under the right circumstances""" self.uut_64 = BindingState(True) #TODO test_check_address_bounds: Why does this raise an exception? # address and seg_end address are not 0 when entering the function #self.uut_64.address = 0 #self.uut_64.seg_end_address = 0 #self.uut_64.check_address_bounds() self.uut_64 = BindingState(True) self.uut_64.address = 0 self.uut_64.seg_end_address = 1 self.uut_64.check_address_bounds() self.uut_64 = BindingState(True) self.uut_64.address = 10000 self.uut_64.seg_end_address = 10 with self.assertRaises(CLEInvalidBinaryError): self.uut_64.check_address_bounds() self.uut_64 = BindingState(True) self.uut_64.address = -10000 self.uut_64.seg_end_address = -100000 with self.assertRaises(CLEInvalidBinaryError): self.uut_64.check_address_bounds()