Example #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
Example #2
0
 def create_int_carray_obj(self, bits, init_val):
     cint_type = cdm.CIntType('i' + str(bits), bits, False, cdm.ENDIANESS)
     content = b''.join(map(cint_type.convert_to_c_repr, init_val))
     addrspace = VirtualAddressSpace(content)
     carray_type = cdm.CArrayType(cint_type.bind(addrspace), len(init_val),
                                  addrspace)
     return cdm.CArray(carray_type, 0)
Example #3
0
 def test_init_setsAttrs(self):
     addrspace = Mock()
     cint_type = cdm.CIntType('typename', 32, True, 'little', addrspace)
     assert cint_type.c_name == 'typename'
     assert cint_type.sizeof == 4
     assert cint_type.signed == True
     assert cint_type.endianess == 'little'
     assert cint_type.__addrspace__ is addrspace
Example #4
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')
Example #5
0
def unbound_cuint64_type(addrspace):
    return cdm.CIntType('cuint64', 64, False, cdm.ENDIANESS, None)
Example #6
0
def cint_type(addrspace):
    return cdm.CIntType('cint', 32, True, cdm.ENDIANESS, addrspace)
Example #7
0
def unbound_cint_type():
    return cdm.CIntType('cint', 32, True, cdm.ENDIANESS, None)
Example #8
0
def unbound_cint16_type(addrspace):
    return cdm.CIntType('cint16', 16, True, cdm.ENDIANESS, None)
Example #9
0
 def test_eq_onSamePointer_returnsTrue(self, diff_carr_type):
     basetype = cdm.CIntType('x', 32, True, cdm.ENDIANESS)
     assert cdm.CArrayType(basetype, 10) != diff_carr_type
Example #10
0
 def test_eq_inSameCIntType_returnsTrue(self):
     assert cdm.CIntType('name', 32, True, 'little') \
            == cdm.CIntType('name', 32, True, 'little')
Example #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
Example #12
0
 def test_cDefinition_onRefDefIsSet_returnsWithRefDef(self):
     cint_type = cdm.CIntType('typename', 32, True, 'little')
     assert cint_type.c_definition('varname') == 'typename varname'
Example #13
0
 def test_getAlignment_returnsSizeof(self, size):
     cint_type = cdm.CIntType('name', size*8, False, 'little')
     assert cint_type.alignment == size
Example #14
0
 def test_sizeof_returnsSizeInBytes(self, bits):
     cint_type = cdm.CIntType('typename', bits, True, 'little')
     assert cint_type.sizeof == bits // 8
Example #15
0
class TestCIntType:

    def test_init_setsAttrs(self):
        addrspace = Mock()
        cint_type = cdm.CIntType('typename', 32, True, 'little', addrspace)
        assert cint_type.c_name == 'typename'
        assert cint_type.sizeof == 4
        assert cint_type.signed == True
        assert cint_type.endianess == 'little'
        assert cint_type.__addrspace__ is addrspace

    def test_cDefinition_returnsCName(self):
        cint_type = cdm.CIntType('typename', 32, True, 'little')
        assert cint_type.c_definition() == 'typename'

    def test_cDefinition_onRefDefIsSet_returnsWithRefDef(self):
        cint_type = cdm.CIntType('typename', 32, True, 'little')
        assert cint_type.c_definition('varname') == 'typename varname'

    @pytest.mark.parametrize('bits', [8, 32])
    def test_sizeof_returnsSizeInBytes(self, bits):
        cint_type = cdm.CIntType('typename', bits, True, 'little')
        assert cint_type.sizeof == bits // 8

    def test_nullVal_ok(self, cint_type):
        assert cint_type.null_val == 0

    def test_repr_withAttrAndSpaceInName_ok(self):
        cint_type = cdm.CIntType('type name', 32, True, 'little')
        attr_cint_type = cint_type.with_attr('attrB').with_attr('attrA')
        assert repr(attr_cint_type) == 'ts.attrA_attrB_type_name'

    def test_eq_inSameCIntType_returnsTrue(self):
        assert cdm.CIntType('name', 32, True, 'little') \
               == cdm.CIntType('name', 32, True, 'little')

    @pytest.mark.parametrize('diff_cint_type', [
        "othertype",
        cdm.CIntType('othername', 32, True, 'little'),
        cdm.CIntType('name', 32, True, 'little').with_attr('test'),
        cdm.CIntType('name', 16, True, 'little'),
        cdm.CIntType('name', 32, False, 'little'),
        cdm.CIntType('name', 32, True, 'big'),])
    def test_eq_inDifferentCIntType_returnsFalse(self, diff_cint_type):
        assert diff_cint_type != cdm.CIntType('name', 32, True, 'little')

    @pytest.mark.parametrize(('cint_type', 'expected_val'), [
        (cdm.CIntType('name', 32, False, 'little'), b'\x21\x43\x65\x87'),
        (cdm.CIntType('name', 16, False, 'little'), b'\x21\x43'),
        (cdm.CIntType('name', 32, False, 'big'),    b'\x87\x65\x43\x21')])
    def test_convertToCRepr_returnsCRepr(self, cint_type, expected_val):
        assert cint_type.convert_to_c_repr(0x87654321) == expected_val

    def test_convertToCRepr_onSignBitSetAndSigned_returnsNegativePyVal(self):
        signed_cint_type = cdm.CIntType('name', 32, True, 'little')
        assert signed_cint_type.convert_to_c_repr(-1) == b'\xFF\xFF\xFF\xFF'

    def test_convertToCRepr_onBytesOfSize1_setsAsciiCode(self, cint16_type):
        assert cint16_type.convert_to_c_repr(b'\x12') == b'\x12\x00'

    def test_convertToCRepr_onStrOfSize1_setsUnicodeCodepoint(self, cint_type):
        assert cint_type.convert_to_c_repr('\U00012345') == b'\x45\x23\x01\x00'

    def test_convertToCRepr_onOtherType_forwardsToBaseClass(self, cint_type):
        assert cint_type.convert_to_c_repr(None) == b'\x00\x00\x00\x00'

    @pytest.mark.parametrize(('cint_type', 'c_repr', 'py_val'), [
        (cdm.CIntType('name', 32, False, 'little'), b'\x78\x56\x34\x12', 0x12345678),
        (cdm.CIntType('name', 16, False, 'little'), b'\x34\x12',         0x1234),
        (cdm.CIntType('name', 32, False, 'big'),    b'\x12\x34\x56\x78', 0x12345678),
        (cdm.CIntType('name', 32, False, 'little'), b'\xFF\xFF\xFF\xFF', 0xFFFFFFFF),
        (cdm.CIntType('name', 32, True,  'little'), b'\xFF\xFF\xFF\xFF', -1),
        (cdm.CIntType('name', 8,  True,  'little'), b'\x80',             -128)])
    def test_convertFromCRepr_returnsCRepr(self, cint_type, c_repr, py_val):
        assert cint_type.convert_from_c_repr(c_repr) == py_val

    @pytest.mark.parametrize('size', [1, 2, 4, 8])
    def test_getAlignment_returnsSizeof(self, size):
        cint_type = cdm.CIntType('name', size*8, False, 'little')
        assert cint_type.alignment == size
Example #16
0
 def test_convertToCRepr_onSignBitSetAndSigned_returnsNegativePyVal(self):
     signed_cint_type = cdm.CIntType('name', 32, True, 'little')
     assert signed_cint_type.convert_to_c_repr(-1) == b'\xFF\xFF\xFF\xFF'
Example #17
0
 def test_eq_inDifferentCIntType_returnsFalse(self, diff_cint_type):
     assert diff_cint_type != cdm.CIntType('name', 32, True, 'little')
Example #18
0
def cint16_type(addrspace):
    return cdm.CIntType('cint16', 16, True, cdm.ENDIANESS, addrspace)
Example #19
0
 def test_convertToCRepr_onUtf8WithBigCodepoint_returnsArrayOfCorrectSize(
         self):
     carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 4)
     c_repr = carray_type.convert_to_c_repr('A\u1122')
     assert c_repr == b'\x00\x00\x00\x41\x00\x00\x11\x22' \
                      b'\x00\x00\x00\x00\x00\x00\x00\x00'
Example #20
0
def cuint64_type(addrspace):
    return cdm.CIntType('cuint64', 64, False, cdm.ENDIANESS, addrspace)
Example #21
0
 def test_cDefinition_returnsCName(self):
     cint_type = cdm.CIntType('typename', 32, True, 'little')
     assert cint_type.c_definition() == 'typename'
Example #22
0
class TestCArrayType:
    def test_init_returnsArrayCProxy(self, unbound_cint_type):
        carray_type = cdm.CArrayType(unbound_cint_type, 10)
        assert carray_type.__addrspace__ is None
        assert carray_type.base_type is unbound_cint_type
        assert carray_type.element_count == 10

    def test_init_onBaseTypeWithDifferentAddrSpaceSet_raisesInvalidAddressSpace(
            self, cint_type):
        other_addrspace = VirtualAddressSpace()
        with pytest.raises(cdm.InvalidAddressSpaceError):
            _ = cdm.CArrayType(cint_type, 10, other_addrspace)

    def test_bind_bindsAlsoBaseElement(self, addrspace):
        ctype = cdm.CProxyType(1)
        carray_type = cdm.CArrayType(ctype, 10)
        bound_carray_type = carray_type.bind(addrspace)
        assert bound_carray_type.base_type.__addrspace__ is addrspace

    def test_shallowIterSubTypes_returnsBaseType(self, carray_type):
        assert list(carray_type.shallow_iter_subtypes()) \
               == [carray_type.base_type]

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

    @pytest.mark.parametrize('diff_carr_type', [
        "othertype",
        cdm.CArrayType(cdm.CIntType('x', 32, True, cdm.ENDIANESS),
                       10).with_attr('attr'),
        cdm.CArrayType(cdm.CIntType('x', 32, True, cdm.ENDIANESS), 1000),
        cdm.CArrayType(cdm.CIntType('y', 16, False, cdm.ENDIANESS), 10)
    ])
    def test_eq_onSamePointer_returnsTrue(self, diff_carr_type):
        basetype = cdm.CIntType('x', 32, True, cdm.ENDIANESS)
        assert cdm.CArrayType(basetype, 10) != diff_carr_type

    def test_len_returnsSizeOfObject(self, carray_type):
        assert len(carray_type) == carray_type.element_count

    def test_sizeof_returnsSizeInBytes(self, carray_type):
        assert carray_type.sizeof \
               == carray_type.element_count * carray_type.base_type.sizeof

    @patch.object(cdm.CIntType, 'null_val')
    def test_nullValue_ok(self, null_val, carray_type):
        assert carray_type.null_val == [null_val] * carray_type.element_count

    def test_cDefinition_onRefDef_returnsWithRefDef(self, cint_type):
        assert cint_type.array(12).c_definition('x') == 'cint x[12]'

    def test_cDefinition_onNoRefDef_returnsWithoutRefDef(self, cint_type):
        assert cint_type.array(12).c_definition() == 'cint [12]'

    def test_cDefinition_onArrayOfArrays_ok(self, cint_type):
        assert cint_type.array(11).array(22).c_definition() == 'cint [22][11]'

    def test_cDefinition_onArrayOfPtr_ok(self, cint_type):
        assert cint_type.ptr.array(10).c_definition('x') == 'cint *x[10]'

    def test_cDefinition_onPtrToArray_ok(self, cint_type):
        assert cint_type.array(10).ptr.c_definition('x') == 'cint (*x)[10]'

    def test_repr_returnsBaseNamePlusArray(self, unbound_cint_type):
        cptr_type = cdm.CArrayType(unbound_cint_type, 123).with_attr('attr')
        assert repr(cptr_type) == 'ts.cint_attr_array123'

    def test_convertToCRepr_onPyIterable_initializesElementsWithIterablePlusNullVals(
            self):
        carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 5)
        c_repr = carray_type.convert_to_c_repr([0x11, 0x22, 0x33445566])
        assert c_repr == b'\x00\x00\x00\x11\x00\x00\x00\x22\x33\x44\x55\x66' \
                         b'\x00\x00\x00\x00\x00\x00\x00\x00'

    def test_convertToCRepr_onUtf8WithBigCodepoint_returnsArrayOfCorrectSize(
            self):
        carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 4)
        c_repr = carray_type.convert_to_c_repr('A\u1122')
        assert c_repr == b'\x00\x00\x00\x41\x00\x00\x11\x22' \
                         b'\x00\x00\x00\x00\x00\x00\x00\x00'

    def test_convertFromCRepr_returnsArrayOfCorrectSize(self):
        carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 5)
        py_repr = carray_type.convert_from_c_repr(
            b'\x00\x00\x00\x11\x00\x00\x00\x22\x33\x44\x55\x66')
        assert py_repr == [0x11, 0x22, 0x33445566, 0, 0]

    def test_init_onConstArray_ok(self, cint_type):
        carray_type = cint_type.with_attr('const').array(1)
        _ = carray_type()

    @pytest.mark.parametrize('size', [1, 4])
    def test_getAlignment_returnsAlignmentOfBase(self, size,
                                                 unbound_cint_type):
        with patch.object(cdm.CIntType, 'alignment', size):
            carray_type = cdm.CArrayType(unbound_cint_type, 4)
            assert carray_type.alignment == size
Example #23
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
Example #24
0
 def test_convertToCRepr_onPyIterable_initializesElementsWithIterablePlusNullVals(
         self):
     carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 5)
     c_repr = carray_type.convert_to_c_repr([0x11, 0x22, 0x33445566])
     assert c_repr == b'\x00\x00\x00\x11\x00\x00\x00\x22\x33\x44\x55\x66' \
                      b'\x00\x00\x00\x00\x00\x00\x00\x00'
Example #25
0
 def test_repr_withAttrAndSpaceInName_ok(self):
     cint_type = cdm.CIntType('type name', 32, True, 'little')
     attr_cint_type = cint_type.with_attr('attrB').with_attr('attrA')
     assert repr(attr_cint_type) == 'ts.attrA_attrB_type_name'
Example #26
0
 def test_convertFromCRepr_returnsArrayOfCorrectSize(self):
     carray_type = cdm.CArrayType(cdm.CIntType('i', 32, False, 'big'), 5)
     py_repr = carray_type.convert_from_c_repr(
         b'\x00\x00\x00\x11\x00\x00\x00\x22\x33\x44\x55\x66')
     assert py_repr == [0x11, 0x22, 0x33445566, 0, 0]
Example #27
0
 def test_repr_onSigned8Bit_returnsBytes(self, addrspace):
     cchar_type = cdm.CIntType('char', 8, True, cdm.ENDIANESS, addrspace)
     cchar_obj = cchar_type(b'A')
     assert repr(cchar_obj) == "ts.char(b'A')"