pykd

Форк
0
/
typedvar.py 
546 строк · 25.0 Кб
1
#
2
#
3
#
4

5
import unittest
6
import target
7
import pykd
8

9

10
typesSourceCode = '''
11

12
typedef struct _LIST_ENTRY {
13
    struct _LIST_ENTRY *Flink;
14
    struct _LIST_ENTRY *Blink;
15
} _LIST_ENTRY;
16

17
typedef struct _StructArray {
18
    int size;
19
    unsigned char buf[3];
20
} StructArray;
21
    
22
'''
23

24

25
class TypedVarTest( unittest.TestCase ):
26

27
    def testCtor( self ):
28
        tv = target.module.typedVar( "structTest", target.module.g_structTest )
29
        tv = target.module.typedVar( "g_structTest" )
30

31
        tv = pykd.typedVar( "structTest", target.module.g_structTest )
32
        tv = pykd.typedVar( target.moduleName + "!structTest", target.module.g_structTest )
33

34
        structTest = target.module.type( "structTest" )
35
        tv = pykd.typedVar( structTest, target.module.g_structTest )
36

37
        tv = pykd.typedVar( "g_structTest" )
38
        tv = pykd.typedVar( target.moduleName + "!g_structTest" )
39

40
                
41
    def testCompareWithNone(self):
42
        tv = target.module.typedVar( "ucharVar" )
43
        self.assertFalse(tv == None)
44
        self.assertTrue(tv != None)
45
        self.assertFalse(not tv)
46
        self.assertTrue(tv)
47

48
    def testBaseTypes(self):
49
        self.assertEqual( 10, target.module.typedVar( "ucharVar" ) )
50
        self.assertEqual( 1020, target.module.typedVar( "ushortVar" ) )
51
        self.assertEqual( 10002000, target.module.typedVar( "ulongVar" ) )
52
        self.assertEqual( 1234567890, target.module.typedVar( "ulonglongVar" ) )
53
        self.assertEqual( -5, target.module.typedVar( "charVar" ) )
54
        self.assertEqual( -1020, target.module.typedVar( "shortVar" ) )
55
        self.assertEqual( -1002000, target.module.typedVar( "longVar" ) )
56
        self.assertEqual( -1234567890, target.module.typedVar( "longlongVar" ) )
57

58
    def testPtrTo(self):
59
        tvBaseType = pykd.typedVar( pykd.typeInfo("UInt8B").ptrTo(), target.module.offset("pbigValue") )
60
        self.assertEqual( target.module.typedVar( "bigValue" ), tvBaseType.deref() )
61

62
        tvDiaStruct = pykd.typedVar( target.module.type("structTest").ptrTo(), target.module.offset("g_structTestPtr") )
63
        self.assertEqual( 500, tvDiaStruct.deref().m_field1 )
64

65
        customStructTest = pykd.createStruct(name="customStructTest", align=4)
66
        customStructTest.append("m_field0", pykd.baseTypes.UInt4B)
67
        customStructTest.append("m_field1", pykd.baseTypes.UInt8B)
68
        tvCustomStruct = pykd.typedVar( customStructTest.ptrTo(), target.module.offset("g_structTestPtr") )
69
        self.assertEqual( 500, tvCustomStruct.deref().m_field1 )
70

71
    def testArrayOf(self):
72
        arrayType = pykd.typeInfo("UInt8B").arrayOf(5)
73
        arrayVar = pykd.typedVar( arrayType, target.module.offset("ulonglongArray") )
74
        self.assertEqual( 0xFF, arrayVar[1] )
75
        self.assertEqual( 0xFFFFFFFFFFFFFFFF, arrayVar[4] )        
76

77
        arrayStructType = pykd.typeInfo("structTest").arrayOf(2) 
78
        arrayStructVar = pykd.typedVar( arrayStructType, target.module.offset("g_testArray") )
79
        self.assertEqual( True, arrayStructVar[0].m_field2 )
80
        self.assertEqual( 1, arrayStructVar[1].m_field3 ) 
81

82
    def testConst(self):
83
        self.assertEqual( True, target.module.typedVar( "boolConst" ) )
84
        self.assertEqual( 0x5555, target.module.typedVar( "ulongConst" ) )
85
        self.assertEqual( 0xffffff000000, target.module.typedVar( "ulonglongConst" ) )
86
        self.assertEqual( 3, target.module.typedVar( "g_constEnumThree" ) )
87

88
    def testGetAddress( self ):
89
        tv = target.module.typedVar( "structTest", target.module.g_structTest )
90
        self.assertEqual( tv.getAddress(), target.module.g_structTest )
91

92
    def testGetSize( self ):
93
        tv1 = target.module.typedVar( "structTest", target.module.g_structTest )
94
        self.assertEqual( 16 + pykd.ptrSize(), tv1.sizeof() )
95
        tv2 = target.module.typedVar( "structTest[2]", target.module.g_testArray )
96
        self.assertEqual( tv1.sizeof()*2, tv2.sizeof() )
97

98
        self.assertEqual( pykd.sizeof("g_structTest"), tv1.sizeof() )
99
        self.assertEqual( pykd.sizeof("g_testArray"), tv2.sizeof() )
100
        self.assertEqual( pykd.sizeof("ucharVar"), 1 )
101

102
    def testByAddress( self ):
103
        tv1 = target.module.typedVar( "structTest", target.module.g_structTest )
104
        tv2 = target.module.typedVar( tv1.getAddress() )
105
        self.assertEqual( tv2.getAddress(), tv1.getAddress() )
106

107
    def testStruct(self):
108
        tv1 = target.module.typedVar( "structTest", target.module.g_structTest )
109
        self.assertEqual( 0, tv1.m_field0 )
110
        self.assertEqual( 500, tv1.m_field1 )
111
        self.assertEqual( True, tv1.m_field2 )
112
        self.assertEqual( 1, tv1.m_field3 )
113
        self.assertEqual( 1, tv1["m_field3"] )
114
        self.assertRaises( AttributeError, lambda t: t.not_exists, tv1) # non-exsisting field
115
        self.assertRaises( KeyError, lambda t: t["not_exists"], tv1) # non-exsisting field
116

117
    def testPtrField(self):
118
        tv = target.module.typedVar( "g_structTest" )
119
        self.assertEqual( 0, tv.m_field4 )
120
        tv1 = target.module.typedVar( "g_structTest1" )
121
        self.assertEqual( tv.getAddress(), tv1.m_field4 )
122

123
    def testFieldOffset(self):
124
        tv = target.module.typedVar( "g_structTest" )
125
        self.assertEqual( 0, tv.fieldOffset("m_field0") )
126
        self.assertEqual( 4, tv.fieldOffset("m_field1") )
127
        self.assertEqual( 16, tv.fieldOffset("m_field4") )
128

129
    def testArrayField(self):
130
        tv = target.module.typedVar( "g_structWithArray" )
131
        self.assertEqual( tv + tv.fieldOffset("m_arrayField"),tv.m_arrayField)
132
        self.assertEqual( 2, len(tv.m_arrayField) )
133
        self.assertEqual( 0, tv.m_arrayField[0] )
134
        self.assertEqual( 2, tv.m_arrayField[1] )
135
        self.assertEqual( 3, tv.m_noArrayField )
136
        self.assertNotEqual( -1, tv.m_arrayField[0] )
137
        self.assertNotEqual( 0, tv.m_noArrayField )
138
        tv.m_arrayField[len(tv.m_arrayField)]
139

140
    def testArrayIteration(self):
141
        tv = target.module.typedVar( "g_structWithArray" )
142
        self.assertEqual([0, 2], [x for x in tv.m_arrayField])
143

144
    def testGlobalVar(self):
145
        self.assertEqual( 10002000, target.module.typedVar( "ulongVar" ) )
146
        self.assertEqual( 0x80000000, target.module.typedVar( "ulongArray" )[3] )
147
        self.assertEqual( 0x8000000000000000, target.module.typedVar( "ulonglongArray" )[3] )
148
        self.assertEqual( 0x7FFFFFFF, target.module.typedVar( "longArray" )[3])
149
        self.assertEqual( -1, target.module.typedVar( "longlongArray" )[4])
150
        self.assertEqual( target.module.g_structTest, target.module.typedVar( "g_structTestPtr" ) )
151

152
    def testContainingRecord(self):
153
        off1 = target.module.type( "structTest" ).fieldOffset("m_field2")
154
        off2 = target.module.offset( "g_structTest" )
155
        tv = target.module.containingRecord( off2 + off1, "structTest", "m_field2" )
156
        self.assertEqual( True, tv.m_field2 )
157

158
    def testBitField(self):
159
        tv = target.module.typedVar("g_structWithBits")
160
        self.assertEqual( 4, tv.m_bit0_4 )
161
        self.assertEqual( 1, tv.m_bit5 )
162
        self.assertEqual( 5, tv.m_bit6_8 )
163
        tv = target.module.typedVar("g_structWithSignBits")
164
        self.assertEqual( 4, tv.m_bit0_4 )
165
        self.assertEqual( -1, tv.m_bit5 )
166
        self.assertEqual( -3, tv.m_bit6_8 )
167

168
    def testTypedVarList(self):
169
        tvl = target.module.typedVarList( target.module.g_listHead, "listStruct", "next.flink" )
170
        self.assertEqual( 5, len( tvl ) )
171
        self.assertEqual( [ i for i in range(5)], [ tv.num for tv in tvl ] )
172

173
        tvl = pykd.typedVarList( target.module.g_listHead, target.module.type("listStruct"), "next.flink" )
174
        self.assertEqual( 5, len( tvl ) )
175
        self.assertEqual( [ i for i in range(5)], [ tv.num for tv in tvl ] )
176

177
        tvl = pykd.typedVarList( target.module.g_listHead, target.module.type("listStruct"), "next.flink" )
178
        self.assertEqual( 5, len( tvl ) )
179
        self.assertEqual( [ i for i in range(5)], [ tv.num for tv in tvl ] )
180

181
        #tvl = target.module.typedVarList( target.module.g_listHead1, "listStruct1", "next" )
182
        #self.assertEqual( 3, len( tvl ) )
183
        #self.assertEqual( [100,200,300], [ tv.num for tv in tvl ] )
184

185
        #tvl = pykd.typedVarList( target.module.g_listHead1, target.module.type("listStruct1"), "next" )
186
        #self.assertEqual( 3, len( tvl ) )
187
        #self.assertEqual( [100,200,300], [ tv.num for tv in tvl ] )
188

189
        #tvl = pykd.typedVarList( target.module.g_childListHead, target.module.type("ChildEntryTest"), "m_next" )
190
        #self.assertEqual( 3, len( tvl ) )
191
        #self.assertEqual( [1000,2000,3000], [ tv.m_someBaseFiled2 for tv in tvl ] )
192
        #self.assertEqual( [1001,2001,3001], [ tv.m_childFiled1 for tv in tvl ] )
193

194
        #tvl1 = target.module.typedVarList( target.module.g_listHead, "listStruct", "listEntry" )
195
        #tvl2 = pykd.typedVarList( target.module.g_listHead, target.moduleName + "!listStruct", "listEntry" )
196
        #self.assertEqual( tvl1, tvl2 )
197

198
    def testTypedVarArray(self):
199
        tvl = target.module.typedVarArray( target.module.g_testArray, "structTest", 2 )
200
        self.assertEqual( 2, len( tvl ) )
201
        self.assertEqual( 500, tvl[0].m_field1 )
202
        self.assertEqual( False, tvl[1].m_field2 )
203

204
        tvl = pykd.typedVarArray( target.module.g_testArray, target.module.type("structTest"), 2 )
205
        self.assertEqual( 2, len( tvl ) )
206
        self.assertEqual( 1, tvl[0].m_field3 )
207
        self.assertEqual( 0, tvl[1].m_field4 )
208
        
209
        tvl1 = target.module.typedVarArray( target.module.g_testArray, "structTest", 2 )
210
        tvl2 = pykd.typedVarArray( target.module.g_testArray, target.moduleName + "!structTest", 2 )
211
        self.assertEqual( tvl1, tvl2 )
212

213
    def testEqual(self):
214
        tv1 = target.module.typedVar("g_structTest")
215
        tv2 = target.module.typedVar("intMatrix")
216
        self.assertEqual( tv1.m_field3, tv2[0][1] )
217

218
    def testEnum(self):
219
        tv = target.module.typedVar("g_constEnumThree")
220
        self.assertEqual( 3, tv )
221
        self.assertEqual( target.module.type("enumType").THREE, tv )
222

223
    def testIndex(self):
224
        ind  = target.module.typedVar( "g_structTest" ).m_field3
225
        self.assertEqual( 5, [0,5,10][ind] )
226
      
227
        self.assertTrue( ind in [0,1,2] )
228
    
229
        tv = target.module.typedVar( "g_structWithArray" )
230
        self.assertEqual( 2, tv.m_arrayField[ind] )
231
        
232
        self.assertTrue( ind in { 1 : "1", 4 : "2" } )
233
        self.assertEqual( "1", { 1 : "1", 4 : "2" }[ind] )
234

235
    def testDeref(self):
236
        tv = target.module.typedVar( "g_structTest1" )
237
        self.assertEqual( target.module.g_structTest, tv.m_field4.deref().getAddress() )
238

239
        tv = target.module.typedVar( "g_structTest" )
240
        self.assertEqual( 0, tv.m_field4.deref().getAddress() )
241

242
        try:
243
            tv.m_field1.deref()
244
            self.assertTrue(False)
245
        except pykd.TypeException: 
246
            pass
247
            
248
    def testSkipDeref(self):
249
        tv = target.module.typedVar( "g_structTest1" )
250
        self.assertEqual( tv.m_field4.deref().m_field1, tv.m_field4.m_field1 )
251

252
    def testUnNamedStruct(self):
253
        tv = target.module.typedVar( "g_unNamedStruct" )
254
        self.assertEqual( 4, tv.m_fieldNestedStruct )
255
        self.assertEqual( 5, tv.m_fieldOfUnNamed )
256

257
    def testFunctionPtr(self):
258
        funcptr = target.module.typedVar( "CdeclFuncPtr" )
259
        # CdeclFuncPtr -> jmp     targetapp!CdeclFunc (00bd1ba0)
260
        # self.assertEqual( funcptr, target.module.CdeclFunc )
261
        self.assertEqual( funcptr.type().deref().name(), target.module.typedVar("CdeclFunc").type().name() )
262
        self.assertEqual( funcptr.type().name(), target.module.typedVar("CdeclFunc").type().ptrTo().name() )
263

264
    def testFunctionRange(self):
265
        func1 = target.module.typedVar("CdeclFunc")
266
        self.assertTrue( func1.getAddress() >= target.module.begin() )
267
        self.assertTrue( func1.getAddress() + func1.sizeof() <= target.module.end() )
268

269
        func2 = target.module.typedVar( target.module.StdcallFunc )
270
        self.assertTrue( func2.getAddress() >= target.module.begin() )
271
        self.assertTrue( func2.getAddress() + func2.sizeof() <= target.module.end() )
272

273
    def testTypeVarArg(self):
274
        tv1 = target.module.typedVar( "structTest", target.module.g_structTest )
275
        tv2 = target.module.typedVar( "structTest", tv1 )
276
        self.assertEqual( tv1, tv2 )  
277
        self.assertTrue( tv1 )
278

279
    def testPrint(self):
280
        self.assertTrue( str(target.module.typedVar( "ucharVar" ) ) )
281
        self.assertTrue( str(target.module.typedVar( "ushortVar" ) ) )
282
        self.assertTrue( str(target.module.typedVar( "ulongVar" ) ) )
283
        self.assertTrue( str(target.module.typedVar( "ulonglongVar" ) ) )
284
        self.assertTrue( str(target.module.typedVar( "g_structWithBits" ) ) )
285
        self.assertTrue( str(target.module.typedVar( "g_structTest" ) ) )
286
        self.assertTrue( str(target.module.typedVar( "g_structTest1" ) ) )
287
        self.assertTrue( str(target.module.typedVar( "g_testArray" ) ) )
288
        self.assertTrue( str(target.module.typedVar( "g_structTestPtr" ) ) )
289
        self.assertTrue( str(target.module.typedVar( "g_structTestPtrPtr" ) ) )
290
        self.assertTrue( str(target.module.typedVar( "longlongArray" ) ) )
291
        self.assertTrue( str(target.module.typedVar( "intMatrix4" ) ) )
292
        self.assertTrue( str(target.module.typedVar( "ptrIntMatrix" ) ) )
293
        self.assertTrue( str(target.module.typedVar( "g_classChild" ) ) )
294
        #self.assertTrue( str(target.module.typedVar( "g_struct3" ) ) )
295
        self.assertTrue( str(target.module.typedVar( "g_listHead" ) ) )
296
        self.assertTrue( str(target.module.typedVar( "voidPtr" ) ) )
297
        #self.assertTrue( str(target.module.typedVar( "g_arrOfPtrToFunc" ) ) )
298
        #self.assertTrue( str(target.module.typedVar( "g_unTypedPtrToFunction" ) ) )
299
        
300
    def testNotValidPrint(self):
301
        types = ("structTest", "ULong[100]", "ULong*" )
302
        for ti in types:
303
            self.assertTrue( str(pykd.typedVar( target.module.type(ti), 0 ) ) )
304

305
    def testStaticField(self):
306
        tv = pykd.typedVar( "g_classChild" )
307
        self.assertEqual( 200, tv.m_staticField )
308
        self.assertEqual( 100, tv.m_staticConst )
309

310
    def testAmbiguousFieldAccess(self):
311
       # derivedFiledVal = pykd.loadCStr( pykd.typedVar( "g_fieldSameNameStruct" ).m_field )
312
       # self.assertEqual( derivedFiledVal, "toaster" )
313
         self.assertEqual( 678, pykd.typedVar( "g_virtChild" ).m_member )
314

315
    def testDiamondVirtualInherit(self):
316
        tv = pykd.typedVar( "g_virtChild" )
317
        self.assertEqual( -100, tv.m_baseField )
318

319
    def testUdtSubscribe(self):
320
        tv = pykd.typedVar( "g_virtChild" )
321
        self.assertEqual( 6, len(tv) )
322
        fieldName = tv.fieldName(3)
323
        fieldVal = tv.field(3)
324

325
        self.assertEqual( fieldName, "m_baseField" )
326
        self.assertEqual( fieldVal, tv.m_baseField )
327

328
        for field in tv.fields():
329
            str( field )
330

331
    #def testDeadlockList(self):
332
    #    lst = []
333
    #    entry = pykd.typedVar("deadlockEntry").flink
334
    #    for i in xrange( 0, 100000 ):
335
    #        lst.append(entry)
336
    #        entry = entry.deref().flink
337

338
    def testHresult(self):
339
        tv = pykd.typedVar( "g_atlException" )
340
        self.assertEqual( tv.m_hr, 0x8000FFFF )
341

342
    def testFunctionDebugRange(self):
343
        tv = pykd.typedVar( "startChildProcess" )
344

345
        self.assertTrue( tv.getDebugStart() >= tv.getAddress() )
346
        self.assertTrue( tv.getDebugEnd() <= tv.getAddress() + tv.sizeof() )
347

348
    def testFields(self):
349
        tv = pykd.typedVar( "g_classChild")
350
        self.assertTrue( len(tv.fields())>0 )
351

352
    def testDir(self):
353
        tv = target.module.typedVar( "structTest", target.module.g_structTest )
354
        self.assertEqual(5, len(dir(tv)))
355
        self.assertTrue("m_field3" in dir(tv))
356
        self.assertFalse("m_field33" in dir(tv))
357

358
    def testMemoryException(self):
359
        self.assertRaises(pykd.MemoryException, 
360
                          int,
361
                          pykd.typedVar(pykd.baseTypes.UInt4B, 0))
362
        if pykd.getCPUType() == pykd.CPUType.I386:
363
            self.assertRaises(pykd.MemoryException, 
364
                              int,
365
                              pykd.typedVar(pykd.baseTypes.UInt4B, 0xFFFFFFFF))
366
        self.assertRaises(pykd.MemoryException, 
367
                          int,
368
                          pykd.typedVar(pykd.baseTypes.UInt4B, 0xFFFFFFFFFFFFFFFF))
369

370
    def testLocation(self):
371
        tv = target.module.typedVar( "structTest", target.module.g_structTest )
372
        self.assertEqual( ( pykd.Location.Memory, tv.getAddress()), tv.getLocation() )
373

374
    def testGetAddress(self):
375
        if not pykd.is64bitSystem():
376
            tv = target.module.typedVar( "structTest", 0x80000000 )
377
            self.assertEqual( 0xFFFFFFFF80000000, tv.getAddress() )
378
            self.assertEqual( 0xFFFFFFFF80000000, tv )
379

380
    def testCompare(self):
381
        self.assertTrue( 10002000 ==  target.module.typedVar( "ulongVar" ) )
382
        self.assertTrue( 10002001 !=  target.module.typedVar( "ulongVar" ) )
383
        self.assertTrue( 10002001 > target.module.typedVar( "ulongVar" ) )
384
        self.assertTrue( 10001999 < target.module.typedVar( "ulongVar" ) )
385
        self.assertTrue( 10002001 >= target.module.typedVar( "ulongVar" ) )
386
        self.assertTrue( 10001999 <= target.module.typedVar( "ulongVar" ) )
387
        self.assertTrue( True if target.module.typedVar( "ulongVar" )  else False )
388
        self.assertTrue( False if not target.module.typedVar( "ulongVar" ) else True )
389

390
 
391
    def testCompareWihNone(self):
392
        tv = target.module.typedVar( "structTest", target.module.g_structTest )
393
        self.assertFalse(tv==None)
394
        self.assertTrue(tv!=None)
395

396
    def testCastTo(self):
397
        self.assertEqual(0xD2, target.module.typedVar( "ulonglongVar" ).castTo("UInt1B"))
398
        self.assertEqual(0, target.module.typedVar( "g_structTest" ).castTo("UInt4B"))
399

400
    def testCallFunction(self):
401
        funcptr = target.module.typedVar("StdcallFuncRet");
402
        self.assertEqual( 200000/10, pykd.callFunctionByPtr( funcptr, 10, 200000 ) )
403

404
        funcptr = target.module.typedVar("CdeclFuncLong");
405
        self.assertEqual( 0xffffff000000 + 5, pykd.callFunctionByPtr( funcptr, target.module.typedVar("ulonglongConst") ) )
406
        self.assertEqual( 0x7777 + 5, funcptr.call(0x7777) )
407
        self.assertEqual( 0x11223344556677 + 5, funcptr(0x11223344556677) );
408

409
        functype = pykd.defineFunction( pykd.baseTypes.Int4B, pykd.callingConvention.NearStd)
410
        functype.append("arg1", pykd.baseTypes.Int1B)
411
        functype.append("arg2", pykd.baseTypes.Long)
412
        self.assertEqual( 500 / 25, pykd.callFunctionByAddr(functype, target.module.offset("StdcallFuncRet"), 25, 500 ) )
413
        
414
        self.assertTrue( target.module.typedVar("OverloadedFunc", "Bool(__cdecl)(Int4B,Int4B)").call(100,299) )
415

416
    def testCallFunctionWithTypedVar(self):
417
        funcptr = target.module.typedVar("StdcallFuncRet");
418
        ucharVar = target.module.typedVar( "ucharVar" );
419
        self.assertEqual( 10, ucharVar )
420
        self.assertEqual( 200000/10, funcptr( ucharVar, 200000 ) )
421

422
    def testCallWithWrongArgs(self):
423
        self.assertRaises( pykd.TypeException, target.module.typedVar("StdcallFuncRet"), *(1,) )
424
        self.assertRaises( pykd.TypeException, target.module.typedVar("StdcallFuncRet"), *(1,2,3) )
425
        self.assertRaises( pykd.TypeException, target.module.typedVar("StdcallFuncRet"), *(10, target.module.typedVar("g_classChild") ))
426

427
    def testCallMethod(self):
428
        g_classChild = target.module.typedVar("g_classChild")
429
        self.assertEqual( 1000*5, g_classChild.method("childMethod").call(10) )
430
        self.assertEqual( 1000*5, g_classChild.childMethod(10) )
431

432
    def testCallStdStr(self):
433
        g_stdString = target.module.typedVar("g_stdString")
434
        self.assertEqual( "testString".find('S'), g_stdString.find_first_of(ord('S'), 0) )
435

436
    def testCallOverloadMethod(self):
437
        g_classChild = target.module.typedVar("g_classChild")
438
        self.assertEqual( 10*10, g_classChild.method("overloadMethod", "Int4B(__thiscall)(Int4B)").call(10))
439
        self.assertEqual( 5*8, g_classChild.method("overloadMethod", "Int4B(__thiscall)(Int4B,Int4B)").call(5,8))
440

441
    def testGetTypedVar(self):
442
        addr = pykd.getOffset("g_structTest")
443
        self.assertTrue( None != target.module.type( "structTest" ).getTypedVar(addr) )
444

445
    def testByteSequence(self):
446
        self.assertEqual( 0x44332211, pykd.typedVar("UInt4B", [0x11, 0x22, 0x33, 0x44]) )
447
        self.assertEqual( -1, pykd.typedVar( pykd.baseTypes.Int4B, [0xFF, 0xFF, 0xFF, 0xFF] ) )
448
       
449
    def testRawBytes(self):
450
        self.assertEqual( [ 0x55, 0x55, 0, 0], target.module.typedVar( "ulongConst" ).rawBytes() )
451

452
    def testSetField(self):
453
        byteseq = [0x55] * 20
454
        var = target.module.typedVar("structTest", byteseq)
455
        var.setField("m_field1", 0xFF000000000000AA)
456
        self.assertEqual( [0xAA, 0, 0, 0, 0, 0, 0, 0xFF], byteseq[4:12] )
457
        var.m_field3 = 0xAAAA
458
        self.assertEqual( 0xAAAA, var.m_field3)
459
        self.assertEqual( 0xAAAA, var.field("m_field3") )
460

461
      
462
    def testAttr(self):
463
        var = target.module.typedVar("structTest", [0x55] * 20 )
464
        setattr(var, "m_field1", 11)
465
        self.assertEqual(11, getattr(var, "m_field1"))
466
        self.assertRaises(AttributeError, lambda x: getattr(x, "noexists"), var)
467

468
    def testEvalPyScope(self):
469
        var = target.module.typedVar("structTest", [0x55] * 20 )
470
        self.assertEqual( var.m_field1 * 17, eval("m_field1 * 17", globals(), var) )
471

472
    def testEvalExpr(self):
473
        self.assertEqual( 2+2, pykd.evalExpr("2+2") )
474
        self.assertEqual( target.module.typedVar("g_structTest").m_field1, pykd.evalExpr("g_structTest.m_field1") )
475
        self.assertEqual( target.module.typedVar("g_testArray")[1].m_field3, pykd.evalExpr("(g_testArray + 1)->m_field3") )
476

477
    def testEvalExprScope(self):
478

479
        v1 = target.module.typedVar( "ulongVar" )
480
        v2 = target.module.typedVar( "g_structTest" )
481

482
        scope = { "v1" : v1, "v2" : v2 }
483
        self.assertEqual( v1 + v2.m_field1, pykd.evalExpr("v1 + v2.m_field1", scope) )
484

485
    def testEvalExprScopeLong(self):
486
        v1 = 100
487
        v2 = -500
488
        scope = { "v1" : v1, "v2" : v2 }
489
        self.assertEqual( v1 + v2, pykd.evalExpr("v1 + v2", scope))
490
        self.assertEqual( v1 * v2, pykd.evalExpr("v1 * v2", locals()))
491

492
    def testEvalExprScopeStruct(self):
493
        var = pykd.typedVar("g_structTest1")
494
        self.assertEqual(var.m_field1, pykd.evalExpr("m_field1", var))
495
        self.assertEqual(var.m_field4.deref().m_field1, pykd.evalExpr("m_field4->m_field1", var))
496

497
    def testEvalExprSizeof(self):
498
        self.assertEqual(4, pykd.evalExpr("sizeof(int)"))
499
        self.assertEqual(4, pykd.evalExpr("sizeof(int&)"))
500
        self.assertEqual(pykd.ptrSize() * 10, pykd.evalExpr("sizeof(int*[10])"))
501
        self.assertEqual(pykd.ptrSize(), pykd.evalExpr("sizeof(int(*)[10])"))
502

503
    def testContain(self):
504
        var = pykd.typedVar("g_structTest")
505
        self.assertTrue("m_field1" in var)
506
        self.assertFalse("NotExist" in var)
507
        self.assertRaises(Exception, lambda var : 2 in var, var)
508

509
    def testHasField(self):
510
        var = pykd.typedVar("g_structTest")
511
        self.assertTrue(var.hasField("m_field1"))
512
        self.assertFalse(var.hasField("NotExist"))
513

514
    def testHasMethod(self):
515
        var = pykd.typedVar("g_classChild")
516
        self.assertTrue(var.hasMethod("childMethod"))
517
        self.assertTrue(var.hasMethod("staticMethod"))
518
        self.assertTrue(var.hasMethod("virtMethod1"))
519
        self.assertFalse(var.hasMethod("notExist"))
520
        
521
    def testDumpAccessor(self):
522
        typesProvider = pykd.getTypeInfoProviderFromSource (typesSourceCode)
523
        ti = typesProvider.getTypeByName('_LIST_ENTRY')
524
        a = pykd.typedVar (ti, 0, [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
525
        b = a.Flink
526
        self.assertEqual (a.Flink, 1)
527
        self.assertEqual (a.Blink, 0)
528
        self.assertEqual (b.Flink, 0)
529
        self.assertEqual (b.Blink, 0)
530
        self.assertEqual (b.Flink.Flink, 1)
531
        self.assertEqual (b.Flink.Flink.Flink, 0)
532
        self.assertEqual (b.Flink.Flink.Flink.Flink, 1)
533

534
        dumpAccessor = pykd.dataAccessor ([0,1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0,0], 0x0807060504030200, "list_entry_dump")
535
        
536
        s = pykd.typedVar (typesProvider.getTypeByName('StructArray'), dumpAccessor.nestedCopy(1))
537
        self.assertEqual (s.size, 0x04030201)
538
        self.assertEqual (s.buf[0], 5)
539
        self.assertEqual (s.buf[1], 6)
540

541
        a = pykd.typedVar (typesProvider.getTypeByName('_LIST_ENTRY'), dumpAccessor.nestedCopy(1))
542
        b = a.Flink
543
        self.assertEqual (b, 0x0807060504030201)
544
        self.assertEqual (b.Flink, 0x0807060504030201)
545
        self.assertEqual (b.Flink.Flink, 0x0807060504030201)
546
        self.assertEqual (b.Flink.Flink.Flink, 0x0807060504030201)

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

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

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

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