Exemplo n.º 1
0
 def test_pointer_to_function_returning_pointer(self):
     i = int_type('int', 4, True)
     self.assertTypeName(
         pointer_type(8, function_type(pointer_type(8, i), ((i, ), ),
                                       False)), 'int *(*)(int)', True)
     self.assertTypeName(
         pointer_type(
             8,
             function_type(pointer_type(8, i), ((pointer_type(8, i), ), ),
                           False)), 'int *(*)(int *)', True)
Exemplo n.º 2
0
 def test_function(self):
     mock_obj = MockObject(
         "func", function_type(void_type(), (), False), address=0xFFFF0000
     )
     prog = mock_program(objects=[mock_obj])
     self.assertEqual(
         prog["func"],
         Object(prog, function_type(void_type(), (), False), address=0xFFFF0000),
     )
     self.assertEqual(prog.object("func", FindObjectFlags.FUNCTION), prog["func"])
     self.assertTrue("func" in prog)
Exemplo n.º 3
0
 def test_pointer_to_function(self):
     i = int_type('int', 4, True)
     self.assertTypeName(
         pointer_type(8, function_type(i, ((i, ), ), False)),
         'int (*)(int)', True)
     self.assertTypeName(
         pointer_type(8, function_type(i, ((i, 'x'), ), False)),
         'int (*)(int x)', True)
     self.assertTypeName(
         pointer_type(
             8, function_type(i, ((i, ), (float_type('float', 4), )),
                              False)), 'int (*)(int, float)', True)
Exemplo n.º 4
0
 def test_function(self):
     mock_obj = MockObject('func',
                           function_type(void_type(), (), False),
                           address=0xffff0000)
     prog = mock_program(objects=[mock_obj])
     self.assertEqual(
         prog['func'],
         Object(prog,
                function_type(void_type(), (), False),
                address=0xffff0000))
     self.assertEqual(prog.object('func', FindObjectFlags.FUNCTION),
                      prog['func'])
     self.assertTrue('func' in prog)
Exemplo n.º 5
0
    def test_function(self):
        t = function_type(void_type(), ((int_type('int', 4, True), 'n'), ))
        self.assertEqual(t.kind, TypeKind.FUNCTION)
        self.assertIsNone(t.primitive)
        self.assertEqual(t.type, void_type())
        self.assertEqual(t.parameters, ((int_type('int', 4, True), 'n'), ))
        self.assertFalse(t.is_variadic)
        self.assertTrue(t.is_complete())

        self.assertEqual(
            t, function_type(void_type(), ((int_type('int', 4, True), 'n'), )))
        # Different return type.
        self.assertNotEqual(
            t,
            function_type(int_type('int', 4, True),
                          ((int_type('int', 4, True), 'n'), )))
        # Different parameter name.
        self.assertNotEqual(
            t, function_type(void_type(), ((int_type('int', 4, True), 'x'), )))
        # Unnamed parameter.
        self.assertNotEqual(
            t, function_type(void_type(), ((int_type('int', 4, True), ), )))
        # Different number of parameters.
        self.assertNotEqual(
            t,
            function_type(void_type(), ((int_type('int', 4, True), 'n'),
                                        (pointer_type(8, void_type()), 'p'))))
        # One is variadic.
        self.assertNotEqual(
            t,
            function_type(void_type(), ((int_type('int', 4, True), 'n'), ),
                          True))

        self.assertEqual(
            repr(t),
            "function_type(type=void_type(), parameters=((int_type(name='int', size=4, is_signed=True), 'n'),), is_variadic=False)"
        )
        self.assertRaises(TypeError, sizeof, t)

        self.assertFalse(function_type(void_type(), (), False).is_variadic)
        self.assertTrue(function_type(void_type(), (), True).is_variadic)

        self.assertRaisesRegex(TypeError, 'must be Type', function_type, None,
                               ())
        self.assertRaisesRegex(TypeError, 'must be sequence', function_type,
                               void_type(), None)
        self.assertRaisesRegex(TypeError, 'must be.*sequence', function_type,
                               void_type(), (4, ))
        self.assertRaisesRegex(ValueError, 'must be.*sequence', function_type,
                               void_type, ((), ))
        self.assertRaisesRegex(TypeError,
                               'must be string or None', function_type,
                               void_type(), ((int_type('int', 4, True), 4), ))
        self.assertRaisesRegex(TypeError, 'must be Type', function_type,
                               void_type(), ((None, 'n'), ))
Exemplo n.º 6
0
 def test_pointer_to_function(self):
     i = int_type("int", 4, True)
     self.assertTypeName(
         pointer_type(8, function_type(i, ((i,),), False)), "int (*)(int)", True
     )
     self.assertTypeName(
         pointer_type(8, function_type(i, ((i, "x"),), False)),
         "int (*)(int x)",
         True,
     )
     self.assertTypeName(
         pointer_type(8, function_type(i, ((i,), (float_type("float", 4),)), False)),
         "int (*)(int, float)",
         True,
     )
Exemplo n.º 7
0
 def test_array_of_const_pointers_to_functions(self):
     i = int_type('int', 4, True)
     self.assertTypeName(
         array_type(
             None,
             pointer_type(8, function_type(i, ((i, ), ), False),
                          Qualifiers.CONST)), 'int (* const [])(int)', True)
Exemplo n.º 8
0
 def test_pointer_to_function_returning_const_pointer(self):
     i = int_type('int', 4, True)
     self.assertTypeName(
         pointer_type(
             8,
             function_type(pointer_type(8, i, Qualifiers.CONST), ((i, ), ),
                           False)), 'int * const (*)(int)', True)
Exemplo n.º 9
0
 def test_pointer_to_variadic_function(self):
     i = int_type("int", 4, True)
     self.assertTypeName(
         pointer_type(8, function_type(i, (TypeParameter(i), ), True)),
         "int (*)(int, ...)",
         True,
     )
Exemplo n.º 10
0
 def test_function(self):
     sym = Symbol(function_type(void_type(), (), False),
                  address=0xffff0000,
                  byteorder='little')
     prog = mock_program(symbols=[('func', sym)])
     self.assertEqual(prog._symbol('func', FindObjectFlags.FUNCTION), sym)
     self.assertEqual(prog._symbol('func', FindObjectFlags.ANY), sym)
Exemplo n.º 11
0
 def test_pointer_to_function_with_no_parameters(self):
     self.assertTypeName(
         pointer_type(8, function_type(int_type("int", 4, True), (),
                                       False)),
         "int (*)(void)",
         True,
     )
Exemplo n.º 12
0
 def test_array_of_pointers_to_functions(self):
     i = int_type("int", 4, True)
     self.assertTypeName(
         array_type(4, pointer_type(8, function_type(i, ((i,),), False))),
         "int (*[4])(int)",
         True,
     )
Exemplo n.º 13
0
 def test_function_no_name(self):
     self.assertRaisesRegex(
         ValueError,
         "function must have name",
         str,
         struct_type(
             "foo", 8, ((function_type(int_type("int", 4, True), (), False), None),)
         ),
     )
Exemplo n.º 14
0
 def test_const_pointer_to_function_returning_pointer(self):
     i = int_type("int", 4, True)
     self.assertTypeName(
         pointer_type(
             8, function_type(pointer_type(8, i), ((i,),), False), Qualifiers.CONST
         ),
         "int *(* const)(int)",
         True,
     )
Exemplo n.º 15
0
 def test_pointer_to_function_returning_pointer(self):
     i = int_type("int", 4, True)
     self.assertTypeName(
         pointer_type(
             8,
             function_type(pointer_type(8, i), (TypeParameter(i), ),
                           False)),
         "int *(*)(int)",
         True,
     )
     self.assertTypeName(
         pointer_type(
             8,
             function_type(pointer_type(8, i),
                           (TypeParameter(pointer_type(8, i)), ), False),
         ),
         "int *(*)(int *)",
         True,
     )
Exemplo n.º 16
0
 def test_array_of_const_pointers_to_functions(self):
     i = int_type("int", 4, True)
     self.assertTypeName(
         array_type(
             None,
             pointer_type(8, function_type(i, (TypeParameter(i), ), False),
                          Qualifiers.CONST),
         ),
         "int (* const [])(int)",
         True,
     )
Exemplo n.º 17
0
 def test_pointer_to_function_returning_pointer_to_const(self):
     i = int_type("int", 4, True)
     self.assertTypeName(
         pointer_type(
             8,
             function_type(
                 pointer_type(8, int_type("int", 4, True, Qualifiers.CONST)),
                 (TypeParameter(i),),
                 False,
             ),
         ),
         "const int *(*)(int)",
         True,
     )
Exemplo n.º 18
0
    def test_function(self):
        t = function_type(void_type(),
                          (TypeParameter(int_type("int", 4, True), "n"), ))
        self.assertEqual(t.kind, TypeKind.FUNCTION)
        self.assertIsNone(t.primitive)
        self.assertEqual(t.language, DEFAULT_LANGUAGE)
        self.assertEqual(t.type, void_type())
        self.assertEqual(t.parameters,
                         (TypeParameter(int_type("int", 4, True), "n"), ))
        self.assertFalse(t.is_variadic)
        self.assertTrue(t.is_complete())

        self.assertEqual(
            t,
            function_type(void_type(),
                          (TypeParameter(int_type("int", 4, True), "n"), )),
        )
        # Different return type.
        self.assertNotEqual(
            t,
            function_type(
                int_type("int", 4, True),
                (TypeParameter(int_type("int", 4, True), "n"), ),
            ),
        )
        # Different parameter name.
        self.assertNotEqual(
            t,
            function_type(void_type(),
                          (TypeParameter(int_type("int", 4, True), "x"), )),
        )
        # Unnamed parameter.
        self.assertNotEqual(
            t,
            function_type(void_type(),
                          (TypeParameter(int_type("int", 4, True), ), )))
        # Different number of parameters.
        self.assertNotEqual(
            t,
            function_type(
                void_type(),
                (
                    TypeParameter(int_type("int", 4, True), "n"),
                    TypeParameter(pointer_type(8, void_type()), "p"),
                ),
            ),
        )
        # One is variadic.
        self.assertNotEqual(
            t,
            function_type(void_type(),
                          (TypeParameter(int_type("int", 4, True), "n"), ),
                          True),
        )

        self.assertEqual(
            repr(t),
            "function_type(type=void_type(), parameters=(TypeParameter(type=int_type(name='int', size=4, is_signed=True), name='n'),), is_variadic=False)",
        )
        self.assertRaises(TypeError, sizeof, t)

        self.assertFalse(function_type(void_type(), (), False).is_variadic)
        self.assertTrue(function_type(void_type(), (), True).is_variadic)

        self.assertRaisesRegex(TypeError, "must be Type", function_type, None,
                               ())
        self.assertRaisesRegex(TypeError, "must be sequence", function_type,
                               void_type(), None)
        self.assertRaisesRegex(TypeError, "must be TypeParameter",
                               function_type, void_type(), (4, ))
Exemplo n.º 19
0
 def test_pointer_to_variadic_function(self):
     i = int_type('int', 4, True)
     self.assertTypeName(pointer_type(8, function_type(i, ((i, ), ), True)),
                         'int (*)(int, ...)', True)
Exemplo n.º 20
0
 def test_function_typedef(self):
     self.assertPrettyPrint(
         typedef_type("fn", function_type(int_type("int", 4, True), (), False)),
         "typedef int fn(void)",
     )
Exemplo n.º 21
0
 def test_function(self):
     self.assertTypeName(
         function_type(int_type("int", 4, True), (), False), "int (void)"
     )
Exemplo n.º 22
0
 def test_pointer_to_function_with_no_parameter_specification(self):
     self.assertTypeName(
         pointer_type(8, function_type(int_type("int", 4, True), (), True)),
         "int (*)()",
         True,
     )
Exemplo n.º 23
0
 def test_function(self):
     self.assertTypeName(function_type(int_type('int', 4, True), (), False),
                         'int (void)')
Exemplo n.º 24
0
 def test_function_typedef(self):
     self.assertPrettyPrint(
         typedef_type('fn',
                      function_type(int_type('int', 4, True), (), False)),
         'typedef int fn(void)')
Exemplo n.º 25
0
 def test_function_no_name(self):
     self.assertRaisesRegex(
         ValueError, 'function must have name', str,
         struct_type('foo', 8, ((function_type(int_type('int', 4, True),
                                               (), False), None), )))