pykd

Форк
0
/
customtypestest.py 
145 строк · 6.2 Кб
1
"""Custom types tests"""
2

3
import unittest
4
import target
5
import pykd
6
from pykd import baseTypes
7

8
class CustomTypesTest(unittest.TestCase):
9
    def testCommonStruct(self):
10
        mySubStruct =pykd.createStruct("MySubCustomStruct")
11
        mySubStruct.append( "m_uint1", baseTypes.UInt1B )
12
        mySubStruct.append( "m_uint2", baseTypes.UInt2B )
13

14
        mySubUnion = pykd.createUnion("MySubCustomUnion")
15
        mySubUnion.append( "m_uint1", baseTypes.UInt1B )
16
        mySubUnion.append( "m_uint2", baseTypes.UInt2B )
17

18
        myType =pykd.createStruct("MyCustomStruct")
19
        myType.append( "m_uint1", baseTypes.UInt1B  )
20
        myType.append( "m_uint4", baseTypes.UInt4B )
21
        myType.append( "m_uint2", baseTypes.UInt2B )
22
        myType.append( "m_struct", mySubStruct )
23
        myType.append( "m_union", mySubUnion )
24
        myType.append( "m_uint8", baseTypes.UInt8B )
25

26
        self.assertTrue( myType.size() != 0 )
27
        self.assertTrue( myType.size() >= myType.fieldOffset("m_uint8") + myType.m_uint8.size() )
28
        
29
        self.assertTrue( myType.fieldOffset("m_uint1") == 0 )
30

31
        self.assertTrue( myType.fieldOffset("m_uint1") < myType.fieldOffset("m_uint4") )
32
        self.assertTrue( myType.fieldOffset("m_uint1") + myType.m_uint1.size() <= myType.fieldOffset("m_uint4") )
33

34
        self.assertTrue( myType.fieldOffset("m_uint4") < myType.fieldOffset("m_uint2") )
35
        self.assertTrue( myType.fieldOffset("m_uint4") + myType.m_uint4.size() <= myType.fieldOffset("m_uint2") )
36

37
        self.assertTrue( myType.fieldOffset("m_uint2") < myType.fieldOffset("m_struct") )
38
        self.assertTrue( myType.fieldOffset("m_uint2") + myType.m_uint2.size() <= myType.fieldOffset("m_struct") )
39

40
        self.assertTrue( myType.fieldOffset("m_struct") < myType.fieldOffset("m_union") )
41
        self.assertTrue( myType.fieldOffset("m_struct") + myType.m_struct.size() <= myType.fieldOffset("m_union") )
42

43
    def testCommonUnion(self):
44

45
        myType = pykd.createUnion("MyCustomStruct")
46
        myType.append( "m_uint1", baseTypes.UInt1B )
47
        myType.append( "m_uint4", baseTypes.UInt4B )
48
        myType.append( "m_uint2", baseTypes.UInt2B )
49

50
        self.assertFalse( myType.size() == 0 )
51
        self.assertTrue( myType.fieldOffset("m_uint1") == 0 )
52
        self.assertTrue( myType.fieldOffset("m_uint4") == 0 )
53
        self.assertTrue( myType.fieldOffset("m_uint2") == 0 )
54

55
    def testEmptyType(self):
56
    
57
        myEmptyStruct1 = pykd.createStruct("EmptyStruct1")
58
        self.assertEqual( 0, myEmptyStruct1.size() )
59

60
        myEmptyStruct1.append("m_emptyStruct2", pykd.createStruct("EmptyStruct2"))
61
        self.assertEqual( 0, myEmptyStruct1.size() )
62

63
        myEmptyUnion1 = pykd.createUnion("EmptyUnion1")
64
        self.assertEqual( 0, myEmptyUnion1.size() )
65

66
        myEmptyStruct1.append("m_emptyUnion2", myEmptyUnion1)
67
        self.assertEqual( 0, myEmptyStruct1.size() )
68

69
        myEmptyUnion1.append("m_emptyStruct3", pykd.createStruct("EmptyStruct3"))
70
        self.assertEqual( 0, myEmptyUnion1.size() )
71

72
    def testDupFieldName(self):
73
    
74
        myType = pykd.createStruct("MyCustomStruct")
75
        exceptionRised = False
76
        myType.append( "m_uint1", baseTypes.UInt1B )
77
        try:
78
            myType.append( "m_uint1", baseTypes.UInt1B )
79
        except pykd.TypeException:
80
            exceptionRised = True
81
        self.assertTrue(exceptionRised)
82

83
        myType = pykd.createUnion("MyCustomStruct")
84
        exceptionRised = False
85
        myType.append( "m_uint1", baseTypes.UInt1B )
86
        try:
87
            myType.append( "m_uint1", baseTypes.UInt1B )
88
        except pykd.TypeException:
89
            exceptionRised = True
90
        self.assertTrue(exceptionRised)
91

92
    def testPtrToCustomType(self):
93
        mySubStruct =pykd.createStruct("MySubCustomStruct")
94
        mySubStruct.append( "m_uint1", baseTypes.UInt1B )
95
        mySubStruct.append( "m_uint2", baseTypes.UInt2B )
96
        mySubStructPtr = mySubStruct.ptrTo()
97
        self.assertEqual( pykd.ptrSize(), mySubStructPtr.size() )
98

99
    def testAlign(self):
100
        struct = pykd.createStruct(name ="MyAlignStruct", align=4)
101
        struct.append( "m_field1", baseTypes.UInt1B )
102
        self.assertEqual( 1, struct.size() )
103
        struct.append( "m_field2", baseTypes.UInt1B )
104
        self.assertEqual( 2, struct.size() )
105
        struct.append( "m_field3", baseTypes.UInt1B )
106
        struct.append( "m_field4", baseTypes.UInt2B )
107
        self.assertEqual( 6, struct.size() )
108
        struct.append( "m_field5", baseTypes.UInt4B )
109
        self.assertEqual( 12, struct.size() )
110
        struct.append( "m_field6", baseTypes.UInt1B )
111
        self.assertEqual( 16, struct.size() )
112
        struct.append( "m_field7", baseTypes.UInt1B.arrayOf(5) )
113
        self.assertEqual( 20, struct.size() )
114

115
        struct = pykd.createStruct(name ="MyAlignStruct", align=1)
116
        struct.append( "m_field1", baseTypes.UInt1B )
117
        struct.append( "m_field2", baseTypes.UInt8B )
118
        self.assertEqual( 9, struct.size() )
119

120
    def testUnionAlignedSize(self):
121
        union = pykd.createUnion("MyCustomUnion", align=4)
122
        union.append( "m_field1", baseTypes.UInt2B )
123
        self.assertEqual( 2, union.size() )
124
        union.append( "m_field2", baseTypes.UInt1B.arrayOf(3) )
125
        self.assertEqual( 4, union.size() )
126

127
    def testWi12591(self):
128
        struct = pykd.createStruct(name ="MyAlignStruct", align=4)
129
        struct.append( "m_field1", baseTypes.UInt1B )
130
        struct.append( "m_field2", baseTypes.UInt1B.arrayOf(2) )
131
        self.assertEqual( struct.size(), 3 )
132
        self.assertEqual( struct.fieldOffset("m_field2"), 1 )
133

134
    def testWi12592(self):
135
        struct = pykd.createStruct(name ="MyAlignStruct", align=4)
136
        struct.append( "field1", baseTypes.UInt4B )
137
        struct.append( "field2", baseTypes.UInt1B )
138
        self.assertEqual( struct.size(), 8 )
139
        self.assertEqual( struct.fieldOffset("field2"), 4 )
140

141
    def testCustomFunction(self):
142
        functype = pykd.defineFunction( baseTypes.UInt4B )
143
        functype.append( "var1", baseTypes.WChar)
144
        functype.append( "var2", baseTypes.UInt4B.ptrTo() )
145
        self.assertEqual( "UInt4B(__cdecl)(WChar, UInt4B*)", functype.name() )
146

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.