def test_load_ctypes_default(self): """Test if the default proxy works""" ctypes = types.build_ctypes_proxy(4, 4, 8) self.assertTrue(ctypes.proxy) # test ctypes = types.load_ctypes_default() self.assertTrue(ctypes.proxy) for name, value in make_types(ctypes).items(): globals()[name] = value # default ctypes should be similar to host ctypes. self.assertEqual( ctypes.sizeof(arra1), 4 * ctypes.sizeof( ctypes.get_real_ctypes_member('c_long'))) self.assertEqual( ctypes.sizeof(stp), ctypes.sizeof( ctypes.get_real_ctypes_member('c_void_p'))) self.assertEqual( ctypes.sizeof(arra1), 4 * ctypes.sizeof( ctypes.c_long)) self.assertEqual(ctypes.sizeof(stp), ctypes.sizeof(ctypes.c_void_p)) return
def test_cast(self): ctypes = types.load_ctypes_default() i = ctypes.c_int(42) a = ctypes.c_void_p(ctypes.addressof(i)) p = ctypes.cast(a, ctypes.POINTER(ctypes.c_int)) # ctypes is 32bits, local is 64, pointer is 64 bytes # pointer value is truncated. # why is ctypes 32 bites in the first place ? because its called # in the 32 bits unit test class that inherits this one if ctypes.sizeof(ctypes.c_void_p) != ctypes.sizeof( ctypes.get_real_ctypes_member("c_void_p")): self.skipTest('cant cast memory pointer cross platform') self.assertEqual(ctypes.addressof(i), a.value) self.assertEqual(ctypes.addressof(i), ctypes.addressof(p.contents)) i = St() a = ctypes.c_void_p(ctypes.addressof(i)) p = ctypes.cast(a, stp) self.assertEqual(ctypes.addressof(i), a.value) self.assertEqual(ctypes.addressof(i), ctypes.addressof(p.contents))
def test_mmap_hack32(self): ctypes = types.reload_ctypes(4, 4, 8) real_ctypes_long = ctypes.get_real_ctypes_member('c_ulong') fname = os.path.normpath(os.path.abspath(__file__)) fin = file(fname) local_mmap_bytebuffer = mmap.mmap( fin.fileno(), 1024, access=mmap.ACCESS_READ) fin.close() fin = None # yeap, that right, I'm stealing the pointer value. DEAL WITH IT. heapmap = struct.unpack('L', (real_ctypes_long).from_address(id(local_mmap_bytebuffer) + 2 * (ctypes.sizeof(real_ctypes_long))))[0] log.debug('MMAP HACK: heapmap: 0x%0.8x' % (heapmap)) maps = readLocalProcessMappings() ret = [m for m in maps if heapmap in m] # heapmap is a pointer value in local memory self.assertEquals(len(ret), 1) # heapmap is a pointer value to this executable? self.assertEquals(ret[0].pathname, fname) import ctypes self.assertIn('CTypesProxy-4:4:8', str(ctypes))
def test_cfunctype(self): #verify this is our proxy module self.assertTrue(hasattr(ctypes,'get_real_ctypes_member')) self.assertTrue(hasattr(ctypes,'CFUNCTYPE')) self.assertEqual(ctypes.get_real_ctypes_member('CFUNCTYPE'), ctypes.CFUNCTYPE)