Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_array(self):
tindex = TypeIndex(8)
self.assertEqual(tindex.find('int []'),
array_type(None, int_type('int', 4, True)))
self.assertEqual(tindex.find('int [20]'),
array_type(20, int_type('int', 4, True)))
self.assertEqual(tindex.find('int [0x20]'),
array_type(32, int_type('int', 4, True)))
self.assertEqual(tindex.find('int [020]'),
array_type(16, int_type('int', 4, True)))
self.assertEqual(tindex.find('int [2][3]'),
array_type(2, array_type(3, int_type('int', 4, True))))
self.assertEqual(tindex.find('int [2][3][4]'),
array_type(2, array_type(3, array_type(4, int_type('int', 4, True)))))
(int_type('int', 4, True), 'y', 32),
(int_type('int', 4, True), 'z', 64),
))
point_type = struct_type('point', 8, (
(int_type('int', 4, True), 'x', 0),
(int_type('int', 4, True), 'y', 32),
))
line_segment_type = struct_type('line_segment', 16, (
(point_type, 'a'),
(point_type, 'b', 64),
))
option_type = union_type('option', 4, (
(int_type('int', 4, True), 'i'),
(float_type('float', 4), 'f'),
))
color_type = enum_type('color', int_type('unsigned int', 4, False),
(('RED', 0), ('GREEN', 1), ('BLUE', 2)))
pid_type = typedef_type('pid_t', int_type('int', 4, True))
MOCK_32BIT_PLATFORM = Platform(Architecture.UNKNOWN,
PlatformFlags.IS_LITTLE_ENDIAN)
MOCK_PLATFORM = Platform(Architecture.UNKNOWN,
PlatformFlags.IS_64_BIT |
PlatformFlags.IS_LITTLE_ENDIAN)
class MockMemorySegment(NamedTuple):
buf: bytes
virt_addr: Optional[int] = None
phys_addr: Optional[int] = None
def test_variable(self):
mock_obj = MockObject('counter', int_type('int', 4, True),
address=0xffff0000)
prog = mock_program(objects=[mock_obj])
self.assertEqual(prog['counter'],
Object(prog, int_type('int', 4, True),
address=0xffff0000))
self.assertEqual(prog.object('counter', FindObjectFlags.VARIABLE),
prog['counter'])
self.assertTrue('counter' in prog)
obj = Object(self.prog, union_type('foo'), address=0)
self.assertRaisesRegex(TypeError, 'cannot read object with incomplete union type',
obj.value_)
self.assertRaisesRegex(TypeError,
'cannot read object with incomplete union type',
obj.read_)
obj = Object(self.prog, enum_type('foo'), address=0)
self.assertRaisesRegex(TypeError,
'cannot read object with incomplete enumerated type',
obj.value_)
self.assertRaisesRegex(TypeError,
'cannot read object with incomplete enumerated type',
obj.read_)
obj = Object(self.prog, array_type(None, int_type('int', 4, True)),
address=0)
self.assertRaisesRegex(TypeError,
'cannot read object with incomplete array type',
obj.value_)
self.assertRaisesRegex(TypeError,
'cannot read object with incomplete array type',
obj.read_)
def test_primitive_type(self):
prog = mock_program(types=[
int_type('long', 4, True),
int_type('unsigned long', 4, True),
])
self.assertEqual(prog.type('long'), int_type('long', 4, True))
# unsigned long with signed=True isn't valid, so it should be ignored.
self.assertEqual(prog.type('unsigned long'),
int_type('unsigned long', 8, False))
def test_primitive_type(self):
tindex = mock_type_index(8, [
int_type('long', 4, True),
int_type('unsigned long', 4, True),
])
self.assertEqual(tindex.find('long'), int_type('long', 4, True))
# unsigned long with signed=True isn't valid, so it should be ignored.
self.assertEqual(tindex.find('unsigned long'),
int_type('unsigned long', 8, False))
def test_pointer_to_array_of_pointers(self):
tindex = TypeIndex(8)
self.assertEqual(tindex.find('int *(*)[2]'),
pointer_type(8, array_type(2, pointer_type(8, int_type('int', 4, True)))))
self.assertEqual(tindex.find('int *((*)[2])'),
pointer_type(8, array_type(2, pointer_type(8, int_type('int', 4, True)))))
def test_bit_field_member(self):
segment = b'\x07\x10\x5e\x5f\x1f\0\0\0'
prog = mock_program(segments=[
MockMemorySegment(segment, virt_addr=0xffff0000),
])
type_ = struct_type('bits', 8, (
(int_type('int', 4, True), 'x', 0, 4),
(int_type('int', 4, True, Qualifiers.CONST), 'y', 4, 28),
(int_type('int', 4, True), 'z', 32, 5),
))
obj = Object(prog, type_, address=0xffff0000)
self.assertEqual(obj.x,
Object(prog, int_type('int', 4, True),
address=0xffff0000, bit_field_size=4))
self.assertEqual(
obj.y,
Object(prog, int_type('int', 4, True, Qualifiers.CONST),
address=0xffff0000, bit_field_size=28, bit_offset=4))
self.assertEqual(obj.z,
Object(prog, int_type('int', 4, True),
address=0xffff0004, bit_field_size=5))