示例#1
0
 def test_sub_onCPointerOfDifferrentType_raisesTypeError(self):
     cptr_type1 = cdm.CPointerType(
         cdm.CIntType('name1', 32, True, 'little'), 32, 'little')
     cptr_type2 = cdm.CPointerType(
         cdm.CIntType('name2', 32, True, 'little'), 32, 'little')
     cptr_obj1 = cdm.CPointer(cptr_type1, 0)
     cptr_obj2 = cdm.CPointer(cptr_type2, 0)
     with pytest.raises(TypeError):
         _ = cptr_obj2 - cptr_obj1
示例#2
0
 def cptr_to_array_of(self, val_list):
     addrspace = VirtualAddressSpace(b'some-data')
     cptr_type = cdm.CPointerType(
         cdm.CIntType('i32', 32, False, 'little', addrspace), 32, 'little',
         addrspace)
     adr = addrspace.alloc_memory(len(val_list) * 4)
     addrspace.write_memory(
         adr, b''.join(v.to_bytes(4, 'little') for v in val_list))
     yield cptr_type(adr)
     content = addrspace.read_memory(adr, len(val_list) * 4)
     for ndx in range(len(val_list)):
         val_list[ndx] = int.from_bytes(content[ndx * 4:ndx * 4 + 4],
                                        'little')
示例#3
0
 def test_eq_onSamePointer_returnsTrue(self, cint_type):
     assert cdm.CPointerType(cint_type, 32, 'little') \
            == cdm.CPointerType(cint_type, 32, 'little')
示例#4
0
def cptr_type(cint_type, addrspace):
    return cdm.CPointerType(cint_type, 32, 'big', addrspace)
示例#5
0
 def test_convertToCRepr_onInt_returnsCRepr(self, bitsize, endianess,
                                            expected_val, cint_type,
                                            addrspace):
     cptr_type = cdm.CPointerType(cint_type, bitsize, endianess, addrspace)
     assert cptr_type.convert_to_c_repr(0x87654321) == expected_val
示例#6
0
 def test_repr_returnsBaseNamePlusPtr(self, unbound_cint_type):
     cptr_type = cdm.CPointerType(unbound_cint_type, 32,
                                  'little').with_attr('attr')
     assert repr(cptr_type) == 'ts.cint_attr_ptr'
示例#7
0
 def test_eq_onDifferentPointer_returnsFalse(self, diff_cptr_type):
     basetype = cdm.CIntType('base', 32, True, 'little')
     assert cdm.CPointerType(basetype, 32, 'little') != diff_cptr_type
示例#8
0
 def test_bind_bindsAlsoBaseType(self, addrspace, unbound_cint_type):
     cptr_type = cdm.CPointerType(unbound_cint_type, 32, 'little')
     bound_cptr_type = cptr_type.bind(addrspace)
     assert bound_cptr_type.base_type.__addrspace__ is not None
示例#9
0
 def test_init_onBaseTypeWithDifferentAddrSpaceSet_raisesInvalidAddressSpaceError(
         self, cint_type):
     with pytest.raises(cdm.InvalidAddressSpaceError):
         _ = cdm.CPointerType(cint_type, 32, 'little',
                              VirtualAddressSpace())
示例#10
0
 def test_init_onPtrType_returnsInitializedPointerObj(
         self, unbound_cint_type, addrspace, endianess, wordsize):
     cptr_type = cdm.CPointerType(unbound_cint_type, wordsize, endianess)
     assert cptr_type.base_type is unbound_cint_type
     assert cptr_type.sizeof == wordsize // 8
     assert cptr_type.endianess == endianess
示例#11
0
class TestCPointerType:
    @pytest.mark.parametrize(('endianess', 'wordsize'), [('little', 32),
                                                         ('little', 64),
                                                         ('big', 32)])
    def test_init_onPtrType_returnsInitializedPointerObj(
            self, unbound_cint_type, addrspace, endianess, wordsize):
        cptr_type = cdm.CPointerType(unbound_cint_type, wordsize, endianess)
        assert cptr_type.base_type is unbound_cint_type
        assert cptr_type.sizeof == wordsize // 8
        assert cptr_type.endianess == endianess

    def test_init_onBaseTypeWithDifferentAddrSpaceSet_raisesInvalidAddressSpaceError(
            self, cint_type):
        with pytest.raises(cdm.InvalidAddressSpaceError):
            _ = cdm.CPointerType(cint_type, 32, 'little',
                                 VirtualAddressSpace())

    def test_bind_bindsAlsoBaseType(self, addrspace, unbound_cint_type):
        cptr_type = cdm.CPointerType(unbound_cint_type, 32, 'little')
        bound_cptr_type = cptr_type.bind(addrspace)
        assert bound_cptr_type.base_type.__addrspace__ is not None

    def test_shallowIterSubTypes_onNotEmbeddedDefsOnlyIsFalse_returnsReferredTypeElementaryTypes(
            self, cptr_type):
        assert list(cptr_type.shallow_iter_subtypes()) \
               == [cptr_type.base_type]

    def test_eq_onSamePointer_returnsTrue(self, unbound_cint_type):
        assert cdm.CPointerType(unbound_cint_type, 32, 'little') \
               == cdm.CPointerType(unbound_cint_type, 32, 'little')

    @pytest.mark.parametrize('diff_cptr_type', [
        cdm.CPointerType(cdm.CIntType('other', 16, True, 'little'), 32,
                         'little'),
        cdm.CPointerType(cdm.CIntType('base', 32, True, 'little'), 64,
                         'little'),
        cdm.CPointerType(cdm.CIntType('base', 32, True, 'little'), 32, 'big')
    ])
    def test_eq_onDifferentPointer_returnsFalse(self, diff_cptr_type):
        basetype = cdm.CIntType('base', 32, True, 'little')
        assert cdm.CPointerType(basetype, 32, 'little') != diff_cptr_type

    def test_nullValue_ok(self, cptr_type):
        assert cptr_type.null_val == 0

    def test_cDefinition_onNoRefDef_returnsCNameWithoutRefDef(
            self, unbound_cint_type):
        assert unbound_cint_type.ptr.c_definition() == 'cint *'

    def test_cDefinition_onPtrToPtr_returnsTwoStars(self, unbound_cint_type):
        assert unbound_cint_type.ptr.ptr.c_definition() == 'cint **'

    def test_cDefinition_onPtrToArray_returnsParentethizedStar(
            self, unbound_cint_type):
        assert unbound_cint_type.array(10).ptr.c_definition() == 'cint (*)[10]'

    def test_cDefinition_onArrayOfPtrs_returnsUnParentethizedStar(
            self, unbound_cint_type):
        assert unbound_cint_type.ptr.array(10).c_definition() == 'cint *[10]'

    def test_cDefinition_onRefDef_returnsCNameWithRefDef(
            self, unbound_cint_type):
        assert unbound_cint_type.ptr.c_definition('ab') == 'cint *ab'

    def test_repr_returnsBaseNamePlusPtr(self, unbound_cint_type):
        cptr_type = cdm.CPointerType(unbound_cint_type, 32,
                                     'little').with_attr('attr')
        assert repr(cptr_type) == 'ts.cint_attr_ptr'

    def test_convertFromCRepr_returnsAddressOfReferredObj(self, cptr_type):
        assert cptr_type.convert_from_c_repr(b'\x12\x34\x56\x78') == 0x12345678

    @pytest.mark.parametrize(('bitsize', 'endianess', 'expected_val'),
                             [(32, 'little', b'\x21\x43\x65\x87'),
                              (16, 'little', b'\x21\x43'),
                              (32, 'big', b'\x87\x65\x43\x21')])
    def test_convertToCRepr_onInt_returnsCRepr(self, bitsize, endianess,
                                               expected_val, cint_type,
                                               addrspace):
        cptr_type = cdm.CPointerType(cint_type, bitsize, endianess, addrspace)
        assert cptr_type.convert_to_c_repr(0x87654321) == expected_val

    def test_convertToCRepr_onIterable_allocatesPtrAndCastsIt(
            self, cptr_type, addrspace):
        init_val = Mock(spec=list)
        cptr_type.base_type.alloc_ptr = alloc_ptr = Mock()
        alloc_ptr.return_value.val = 0x123
        assert cptr_type.convert_to_c_repr(init_val) == b'\x00\x00\x01\x23'
        alloc_ptr.assert_called_once_with(init_val)

    def test_convertToCRepr_onCArray_setsAddressOfArray(
            self, cptr_type, cint_type):
        int_arr = cint_type.alloc_array(3)
        assert cptr_type.convert_to_c_repr(int_arr) \
               == int_arr.__address__.to_bytes(4, 'big')

    @pytest.mark.parametrize(('bitsize', 'endianess', 'c_repr', 'py_val'),
                             [(32, 'little', b'\x78\x56\x34\x12', 0x12345678),
                              (16, 'little', b'\x34\x12', 0x1234),
                              (32, 'big', b'\x12\x34\x56\x78', 0x12345678)])
    def test_convertFromCRepr_returnsCRepr(self, bitsize, endianess, c_repr,
                                           py_val, cint_type, addrspace):
        cptr_type = cdm.CPointerType(cint_type, bitsize, endianess, addrspace)
        assert cptr_type.convert_from_c_repr(c_repr) == py_val

    @pytest.mark.parametrize('size', [4, 8])
    def test_getAlignment_returnsSizeof(self, size, unbound_cint_type):
        cptr_type = cdm.CPointerType(unbound_cint_type, size * 8, 'little')
        assert cptr_type.alignment == size
示例#12
0
 def test_getAlignment_returnsSizeof(self, size, unbound_cint_type):
     cptr_type = cdm.CPointerType(unbound_cint_type, size * 8, 'little')
     assert cptr_type.alignment == size
示例#13
0
 def test_convertFromCRepr_returnsCRepr(self, bitsize, endianess, c_repr,
                                        py_val, cint_type, addrspace):
     cptr_type = cdm.CPointerType(cint_type, bitsize, endianess, addrspace)
     assert cptr_type.convert_from_c_repr(c_repr) == py_val