def test_parse_struct_with_few_vars_defined_at_once_with_bitfield(self): s_info = parse_struct('struct s{int a, b : 1;};', 'file') s_expect = Struct('s', '', [Param('a', 'int'), Param('b', 'int')]) self.assertEqual(s_info, s_expect) self.assertEqual(s_info.members_list[0].size, '1') self.assertEqual(s_info.members_list[1].size, '1')
def test_parse_annotations_correctly(self): param_str = '_In_ int a, _Inout_ int b, _Out_opt_ int c' param1 = Param('a', 'int', 'in') param2 = Param('b', 'int', 'inout') param3 = Param('c', 'int', 'out_opt') self.assertEqual(parse_func_parameters(param_str), [param1, param2, param3])
def test_parse_struct_nested_union(self): self.assertEqual( parse_struct('struct a{ union c{char b;}b; int a; };', 'file'), Struct('a', '', [ Param('b', Union('c', '', [Param('b', 'char')])), Param('a', 'int') ], 'file'))
def test_parse_struct_few_vars_defined_at_once(self): self.assertEqual( parse_struct('struct s{int a, b, c;};', 'file'), Struct('s', '', [Param('a', 'int'), Param('b', 'int'), Param('c', 'int')]))
def test_parse__in_annotation(self): param_str = '__in int f' param = Param('f', '__in int') param.parse_annotations() self.assertEqual(parse_func_parameters(param_str), [param])
def test_parse_multiple_parameters(self): param_str = 'void x, unsigned int a, char * c' param1 = Param('x', 'void') param2 = Param('a', 'unsigned int') param3 = Param('c', 'char *') self.assertEqual(parse_func_parameters(param_str), [param1, param2, param3])
def test_parse_parameters_with_multiword_ret_type(self): param_str = 'unsigned int i, int f(unsigned int a, char), char c3' param1 = Param('i', 'unsigned int') param2 = Param('f', 'int (unsigned int a, char)') param3 = Param('c3', 'char') self.assertEqual(parse_func_parameters(param_str), [param1, param2, param3])
def test_parse_parameters_without_name_just_returns_type(self): param_str = 'int, void *, char **, bool' param1 = Param('', 'int') param2 = Param('', 'void *') param3 = Param('', 'char **') param4 = Param('', 'bool') self.assertEqual(parse_func_parameters(param_str), [param1, param2, param3, param4])
def test_repr_json_returns_correct_dict(self): f_info = FuncInfo('int f(char c);', 'f', 'header.h', 'int', [Param('c', 'char')]) self.assertEqual( f_info.repr_json(), { 'decl': 'int f(char c);', 'name': 'f', 'header': 'header.h', 'ret_type': 'int', 'params': [Param('c', 'char')] })
def test_parse_all_function_declarations_from_text(self): text = 'int f1(char c, ...); char b(int f);' funcs = parse_all_functions(text, 'txt', 'f_file') f1 = FuncInfo( 'int f1(char c, ...);', 'f1', 'f_file', 'int', [Param('c', 'char'), Param('vararg', '...')], True) f2 = FuncInfo('char b(int f);', 'b', 'f_file', 'char', [Param('f', 'int')], True) self.assertEqual(funcs['f1'], f1) self.assertEqual(funcs['b'], f2)
def test_get_one_func_and_struct_from_text(self): text = 'int f(int a); struct s{int b;};' functions, _, structs, _, _ = get_types_info_from_text( 'file', text, 'txt') self.assertEqual( functions, { 'f': FuncInfo('int f(int a);', 'f', 'file', 'int', [Param('a', 'int')], False) }) self.assertEqual(structs, {'s': Struct('s', '', [Param('b', 'int')], 'file')})
def test_parse_struct_with_bitfields(self): s_info = parse_struct('struct s{int a : 1; int b: 3; char c:5;};', 'file') s_expect = Struct( 's', '', [Param('a', 'int'), Param('b', 'int'), Param('c', 'char')]) self.assertEqual(s_info, s_expect) self.assertEqual(s_info.members_list[0].size, '1') self.assertEqual(s_info.members_list[1].size, '3') self.assertEqual(s_info.members_list[2].size, '5')
def test_parse_typedef_with_space_in_name_adds_no_type_to_types(self): types = {} typedef = Param('new_type xxx', 'int') parse_typedef_to_type_for_json(typedef, types) self.assertEqual(types, {})
def test_convert_functions_to_json_type(self): function = { 'f': FuncInfo('int f(char c);', 'f', 'file', 'int', [Param('c', 'char')]) } convert_func_types_to_type_for_json(function, {}) p = PrimitiveType('int') c = PrimitiveType('char') expected = { 'f': FuncInfo('int f(char c);', 'f', 'file', p.type_hash, [Param('c', c.type_hash)]) } self.assertEqual(function, expected)
def test_removes_functions_which_we_want_to_remove(self): # See the body of is_wanted() for more details. funcs_to_remove = { # Generic Windows functions whose arguments or return types are "T" # types (e.g. LPCTSTR). 'TTypeInArg': FuncInfo( 'INT TTypeInArg(INT a, LPCTSTR b);', 'TTypeInArg', 'windows.h', 'INT', [Param('a', 'INT'), Param('b', 'LPCTSTR')]), 'TTypeInRetType': FuncInfo('TBYTE TTypeInRetType(void);', 'TTypeInRetType', 'windows.h', 'TBYTE'), } self.assertEqual(remove_unwanted_functions(funcs_to_remove), {})
def test_parse_func_as_paramter_to_type_for_json(self): func_type = 'int(int a)' ret = PrimitiveType('int') param = Param('a', ret.type_hash) self.assertEqual(parse_func_as_param_to_type_for_json(func_type, {}), FunctionType(ret, [param]))
def test_parse_parameters_with_in_annotations(self): param_str = '_In_ char c_2__, _In_opt_ int f' param1 = Param('c_2__', '_In_ char') param2 = Param('f', '_In_opt_ int') param1.parse_annotations() param2.parse_annotations() self.assertEqual(parse_func_parameters(param_str), [param1, param2])
def test_get_func_and_typedef_from_text_in_json_type(self): text = 'typedef int INT; int f();' functions, types, _, _, _ = get_types_info_from_text( 'file', text, 'json') self.assertEqual(functions, {'f': FuncInfo('int f();', 'f', 'file', 'int', [])}) self.assertEqual(types, [Param('INT', 'int')])
def test_remove_underscores_from_param_names_correctly(self): f_info = FuncInfo('int f(char *__c, __type _t);', 'f', 'header.h', 'int', [Param('__c', 'char *'), Param('_t', '__type')], vararg=True) expected = FuncInfo( 'int f(char *c, __type t);', 'f', 'header.h', 'int', [Param('c', 'char *'), Param('t', '__type')], vararg=True) f_info.delete_underscores_in_param_names() self.assertEqual(f_info, expected)
def test_typedef_to_int_type_for_json(self): types = {} typedef = Param('tdef', 'int') parse_typedef_to_type_for_json(typedef, types) t = PrimitiveType('int') tdef = TypedefedType('tdef', t) expected = {t.type_hash: t, tdef.type_hash: tdef} self.assertEqual(types, expected)
def test_parse_enum_inside_struct_to_type_for_json(self): struct = 'struct s{ enum e{ X, Y }p; };' types = {} struct = parse_struct(struct, 'file') parse_struct_to_type_for_json(struct, types) e = EnumType('e', [EnumItem('X', 0), EnumItem('Y', 1)]) s = StructType('s', [Param('p', e)]) expected = {s.type_hash: s, e.type_hash: e} self.assertEqual(types, expected)
def test_convert_typedef_to_json_type(self): tdef = [Param('x', 'int')] types = {} convert_typedefs_to_type_for_json(tdef, types) x = PrimitiveType('int') self.assertEqual(types, { x.type_hash: x, TypedefedType('x', x).type_hash: TypedefedType('x', x) })
class ParamWithAttributesTestMethodsReturnValueTests(unittest.TestCase): def setUp(self): self.param = Param('x', 'void') def test_name_text_correct_return(self): self.assertEqual(self.param.name_text, 'x') def test_type_text_correct_return(self): self.assertEqual(self.param.type_text, 'void') def test_not_equal_method(self): self.assertNotEqual(Param('n', 'int'), Param('x', 'int')) def test_repr_method_returns_correct_str_with_annotations(self): self.param.type = '_In_ void' self.param.parse_annotations() self.assertEqual(self.param.__repr__(), "Param('void' 'x') _In_") def test_repr_method_returns_correct_string(self): self.assertEqual(self.param.__repr__(), "Param('void' 'x')") def test_json_repr_returns_correct_dict(self): self.assertEqual(self.param.repr_json(), {'name': 'x', 'type': 'void'})
def test_parse_ptr_to_function_to_type_for_json(self): types = {} fptr = 'void (*)(int p1, double)' fptr_type = parse_type_to_type_for_json(fptr, types) types[fptr_type.type_hash] = fptr_type ret_type = PrimitiveType('void') p1 = PrimitiveType('int') p2 = PrimitiveType('double') func_type = FunctionType( ret_type, [Param('p1', p1.type_hash), Param('', p2.type_hash)]) ptr = PointerType(func_type) expected = { ret_type.type_hash: ret_type, p1.type_hash: p1, p2.type_hash: p2, func_type.type_hash: func_type, ptr.type_hash: ptr } self.assertEqual(types, expected)
def test_call_conv_is_set_only_when_one_of_CALL_CONVENTIONS(self): types = {} fptr = 'void (__MYCALL)(int p1)' fptr_type = parse_type_to_type_for_json(fptr, types) types[fptr_type.type_hash] = fptr_type ret_type = VoidType() p1 = PrimitiveType('int') func_type = FunctionType(ret_type, [Param('p1', p1.type_hash)], False) expected = { ret_type.type_hash: ret_type, p1.type_hash: p1, func_type.type_hash: func_type } self.assertEqual(types, expected)
def test_parse_function_with_call_conv_to_type_for_json(self): types = {} fptr = 'void (__cdecl)(int p1)' fptr_type = parse_type_to_type_for_json(fptr, types) types[fptr_type.type_hash] = fptr_type ret_type = VoidType() p1 = PrimitiveType('int') func_type = FunctionType(ret_type, [Param('p1', p1.type_hash)], False, 'cdecl') expected = { ret_type.type_hash: ret_type, p1.type_hash: p1, func_type.type_hash: func_type } self.assertEqual(types, expected)
def test_parse_ptr_to_function_with_ptr_to_func_as_param(self): types = {} fptr = 'void (*)(void (*)())' fptr_type = parse_type_to_type_for_json(fptr, types) types[fptr_type.type_hash] = fptr_type ret_type = PrimitiveType('void') param = FunctionType(ret_type, []) param_ptr = PointerType(param) func_type = FunctionType(ret_type, [Param('', param_ptr.type_hash)]) ptr = PointerType(func_type) expected = { ret_type.type_hash: ret_type, param.type_hash: param, param_ptr.type_hash: param_ptr, func_type.type_hash: func_type, ptr.type_hash: ptr } self.assertEqual(types, expected)
def test_get_union_from_text(self): text = 'union s{int b;};' _, _, _, unions, _ = get_types_info_from_text('file', text, 'txt') self.assertEqual(unions, {'s': Union('s', '', [Param('b', 'int')], 'file')})
def test_parse_typedef_to_pointer_with_brackets(self): self.assertEqual(parse_typedefs('typedef void (* ptr);'), [Param('ptr', 'void *')])
def test_parse_two_typedefs_to_function_correctly(self): self.assertEqual( parse_typedefs('typedef int f(int, char), (* pf)(int, char);'), [Param('f', 'int (int, char)'), Param('pf', 'int (*)(int, char)')])