2012-09-05 22:55:29 +08:00
|
|
|
"""Custom types tests"""
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import target
|
|
|
|
import pykd
|
|
|
|
|
|
|
|
class CustomTypesTest(unittest.TestCase):
|
|
|
|
def testCommonStruct(self):
|
2012-12-21 15:06:53 +08:00
|
|
|
tb = pykd.typeBuilder()
|
2013-12-17 21:38:07 +08:00
|
|
|
|
2012-12-21 15:06:53 +08:00
|
|
|
mySubStruct =tb.createStruct("MySubCustomStruct")
|
|
|
|
mySubStruct.append( "m_uint1", tb.UInt1B )
|
|
|
|
mySubStruct.append( "m_uint2", tb.UInt2B )
|
2012-09-05 22:55:29 +08:00
|
|
|
|
2012-12-21 15:06:53 +08:00
|
|
|
mySubUnion = tb.createUnion("MySubCustomUnion")
|
|
|
|
mySubUnion.append( "m_uint1", tb.UInt1B )
|
|
|
|
mySubUnion.append( "m_uint2", tb.UInt2B )
|
|
|
|
|
|
|
|
myType = tb.createStruct("MyCustomStruct")
|
|
|
|
myType.append( "m_uint1", tb.UInt1B )
|
|
|
|
myType.append( "m_uint4", tb.UInt4B )
|
|
|
|
myType.append( "m_uint2", tb.UInt2B )
|
2012-09-05 22:55:29 +08:00
|
|
|
myType.append( "m_struct", mySubStruct )
|
|
|
|
myType.append( "m_union", mySubUnion )
|
2012-12-21 15:06:53 +08:00
|
|
|
myType.append( "m_uint8", tb.UInt8B )
|
2012-09-05 22:55:29 +08:00
|
|
|
|
|
|
|
self.assertTrue( myType.size() != 0 )
|
|
|
|
self.assertTrue( myType.size() >= myType.fieldOffset("m_uint8") + myType.m_uint8.size() )
|
|
|
|
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint1") == 0 )
|
|
|
|
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint1") < myType.fieldOffset("m_uint4") )
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint1") + myType.m_uint1.size() <= myType.fieldOffset("m_uint4") )
|
|
|
|
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint4") < myType.fieldOffset("m_uint2") )
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint4") + myType.m_uint4.size() <= myType.fieldOffset("m_uint2") )
|
|
|
|
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint2") < myType.fieldOffset("m_struct") )
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint2") + myType.m_uint2.size() <= myType.fieldOffset("m_struct") )
|
|
|
|
|
|
|
|
self.assertTrue( myType.fieldOffset("m_struct") < myType.fieldOffset("m_union") )
|
|
|
|
self.assertTrue( myType.fieldOffset("m_struct") + myType.m_struct.size() <= myType.fieldOffset("m_union") )
|
|
|
|
|
|
|
|
def testCommonUnion(self):
|
2012-12-21 15:06:53 +08:00
|
|
|
tb = pykd.typeBuilder()
|
2013-12-17 21:38:07 +08:00
|
|
|
|
2012-12-21 15:06:53 +08:00
|
|
|
myType = tb.createUnion("MyCustomStruct")
|
|
|
|
myType.append( "m_uint1", tb.UInt1B )
|
|
|
|
myType.append( "m_uint4", tb.UInt4B )
|
|
|
|
myType.append( "m_uint2", tb.UInt2B )
|
2012-09-05 22:55:29 +08:00
|
|
|
|
|
|
|
self.assertFalse( myType.size() == 0 )
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint1") == 0 )
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint4") == 0 )
|
|
|
|
self.assertTrue( myType.fieldOffset("m_uint2") == 0 )
|
|
|
|
|
2013-01-10 19:07:54 +08:00
|
|
|
def testEmptyType(self):
|
|
|
|
tb = pykd.typeBuilder()
|
|
|
|
|
|
|
|
myEmptyStruct1 = tb.createStruct("EmptyStruct1")
|
|
|
|
self.assertEqual( 0, myEmptyStruct1.size() )
|
|
|
|
|
|
|
|
myEmptyStruct1.append("m_emptyStruct2", tb.createStruct("EmptyStruct2"))
|
|
|
|
self.assertEqual( 0, myEmptyStruct1.size() )
|
|
|
|
|
|
|
|
myEmptyUnion1 = tb.createUnion("EmptyUnion1")
|
|
|
|
self.assertEqual( 0, myEmptyUnion1.size() )
|
|
|
|
|
|
|
|
myEmptyStruct1.append("m_emptyUnion2", myEmptyUnion1)
|
|
|
|
self.assertEqual( 0, myEmptyStruct1.size() )
|
|
|
|
|
|
|
|
myEmptyUnion1.append("m_emptyStruct3", tb.createStruct("EmptyStruct3"))
|
|
|
|
self.assertEqual( 0, myEmptyUnion1.size() )
|
|
|
|
|
2012-09-05 22:55:29 +08:00
|
|
|
def testDupFieldName(self):
|
2012-12-21 15:06:53 +08:00
|
|
|
tb = pykd.typeBuilder()
|
2013-12-17 21:38:07 +08:00
|
|
|
|
2012-12-21 15:06:53 +08:00
|
|
|
myType = tb.createStruct("MyCustomStruct")
|
2012-09-05 22:55:29 +08:00
|
|
|
exceptionRised = False
|
2012-12-21 15:06:53 +08:00
|
|
|
myType.append( "m_uint1", tb.UInt1B )
|
2012-09-05 22:55:29 +08:00
|
|
|
try:
|
2012-12-21 15:06:53 +08:00
|
|
|
myType.append( "m_uint1", tb.UInt1B )
|
2012-09-05 22:55:29 +08:00
|
|
|
except pykd.TypeException:
|
|
|
|
exceptionRised = True
|
|
|
|
self.assertTrue(exceptionRised)
|
|
|
|
|
2012-12-21 15:06:53 +08:00
|
|
|
myType = tb.createUnion("MyCustomStruct")
|
2012-09-05 22:55:29 +08:00
|
|
|
exceptionRised = False
|
2012-12-21 15:06:53 +08:00
|
|
|
myType.append( "m_uint1", tb.UInt1B )
|
2012-09-05 22:55:29 +08:00
|
|
|
try:
|
2012-12-21 15:06:53 +08:00
|
|
|
myType.append( "m_uint1", tb.UInt1B )
|
2012-09-05 22:55:29 +08:00
|
|
|
except pykd.TypeException:
|
|
|
|
exceptionRised = True
|
|
|
|
self.assertTrue(exceptionRised)
|
2013-12-17 21:38:07 +08:00
|
|
|
|
2013-01-09 21:14:44 +08:00
|
|
|
def testBasicType(self):
|
|
|
|
tb = pykd.typeBuilder()
|
|
|
|
self.assertEqual( 1, tb.UInt1B.size() )
|
|
|
|
self.assertEqual( 2, tb.UInt2B.size() )
|
|
|
|
self.assertEqual( 4, tb.UInt4B.size() )
|
|
|
|
self.assertEqual( 8, tb.UInt8B.size() )
|
|
|
|
self.assertEqual( 1, tb.Int1B.size() )
|
|
|
|
self.assertEqual( 2, tb.Int2B.size() )
|
|
|
|
self.assertEqual( 4, tb.Int4B.size() )
|
|
|
|
self.assertEqual( 8, tb.Int8B.size() )
|
|
|
|
self.assertEqual( 1, tb.Bool.size() )
|
|
|
|
self.assertEqual( 1, tb.Char.size() )
|
|
|
|
self.assertEqual( 2, tb.WChar.size() )
|
|
|
|
self.assertEqual( 4, tb.Long.size() )
|
|
|
|
self.assertEqual( 4, tb.ULong.size() )
|
2013-01-10 19:07:54 +08:00
|
|
|
|
2013-01-09 21:14:44 +08:00
|
|
|
def testVoidPtr(self):
|
|
|
|
self.assertEqual( 4, pykd.typeBuilder(4).VoidPtr.size() )
|
|
|
|
self.assertEqual( 8, pykd.typeBuilder(8).VoidPtr.size() )
|
|
|
|
self.assertEqual( pykd.ptrSize(), pykd.typeBuilder().VoidPtr.size() )
|
2013-01-10 19:20:57 +08:00
|
|
|
self.assertEqual( 4, pykd.typeBuilder(4).UInt1B.ptrTo().size() )
|
|
|
|
self.assertEqual( 8, pykd.typeBuilder(8).UInt1B.ptrTo().size() )
|
|
|
|
self.assertEqual( pykd.ptrSize(), pykd.typeBuilder().UInt1B.ptrTo().size() )
|
2013-12-17 21:38:07 +08:00
|
|
|
|
2013-01-17 20:02:07 +08:00
|
|
|
def testPtrToCustomType(self):
|
|
|
|
tb = pykd.typeBuilder()
|
|
|
|
mySubStruct =tb.createStruct("MySubCustomStruct")
|
|
|
|
mySubStruct.append( "m_uint1", tb.UInt1B )
|
|
|
|
mySubStruct.append( "m_uint2", tb.UInt2B )
|
|
|
|
mySubStructPtr = mySubStruct.ptrTo()
|
|
|
|
self.assertEqual( pykd.ptrSize(), mySubStructPtr.size() )
|
2013-12-17 21:38:07 +08:00
|
|
|
|
2013-12-14 00:29:51 +08:00
|
|
|
def testAlign(self):
|
|
|
|
tb = pykd.typeBuilder()
|
|
|
|
struct = tb.createStruct(name ="MyAlignStruct", align=4)
|
|
|
|
struct.append( "m_field1", tb.UInt1B )
|
|
|
|
self.assertEqual( 1, struct.size() )
|
|
|
|
struct.append( "m_field2", tb.UInt1B )
|
|
|
|
self.assertEqual( 2, struct.size() )
|
|
|
|
struct.append( "m_field3", tb.UInt1B )
|
|
|
|
struct.append( "m_field4", tb.UInt2B )
|
|
|
|
self.assertEqual( 6, struct.size() )
|
|
|
|
struct.append( "m_field5", tb.UInt4B )
|
|
|
|
self.assertEqual( 12, struct.size() )
|
|
|
|
struct.append( "m_field6", tb.UInt1B )
|
|
|
|
self.assertEqual( 16, struct.size() )
|
|
|
|
struct.append( "m_field7", tb.UInt1B.arrayOf(5) )
|
|
|
|
self.assertEqual( 20, struct.size() )
|
2013-12-17 21:38:07 +08:00
|
|
|
|
2014-01-21 00:38:22 +08:00
|
|
|
struct = tb.createStruct(name ="MyAlignStruct", align=1)
|
|
|
|
struct.append( "m_field1", tb.UInt1B )
|
|
|
|
struct.append( "m_field2", tb.UInt8B )
|
|
|
|
self.assertEqual( 9, struct.size() )
|
|
|
|
|
2013-12-17 22:13:48 +08:00
|
|
|
def testUnionAlignedSize(self):
|
|
|
|
tb = pykd.typeBuilder()
|
|
|
|
union = tb.createUnion("MyCustomUnion", align=4)
|
|
|
|
union.append( "m_field1", tb.UInt2B )
|
|
|
|
self.assertEqual( 2, union.size() )
|
|
|
|
union.append( "m_field2", tb.UInt1B.arrayOf(3) )
|
|
|
|
self.assertEqual( 4, union.size() )
|
|
|
|
|
2013-12-17 21:38:07 +08:00
|
|
|
def testWi12591(self):
|
|
|
|
tb = pykd.typeBuilder()
|
|
|
|
struct = tb.createStruct(name ="MyAlignStruct", align=4)
|
|
|
|
struct.append( "m_field1", tb.UInt1B )
|
|
|
|
struct.append( "m_field2", tb.UInt1B.arrayOf(2) )
|
|
|
|
self.assertEqual( struct.size(), 3 )
|
|
|
|
self.assertEqual( struct.fieldOffset("m_field2"), 1 )
|
|
|
|
|
|
|
|
def testWi12592(self):
|
|
|
|
tb = pykd.typeBuilder()
|
|
|
|
struct = tb.createStruct(name ="MyAlignStruct", align=4)
|
|
|
|
struct.append( "field1", tb.UInt4B )
|
|
|
|
struct.append( "field2", tb.UInt1B )
|
|
|
|
self.assertEqual( struct.size(), 8 )
|
|
|
|
self.assertEqual( struct.fieldOffset("field2"), 4 )
|