diff options
| author | negasora <negasora@gmail.com> | 2018-06-07 20:27:27 -0400 |
|---|---|---|
| committer | Ryan Snyder <ryan@vector35.com> | 2018-07-10 18:10:57 -0400 |
| commit | 8028bd325bc94a385f99122d87ac906ba717ecbf (patch) | |
| tree | c8607ebf40c1dc97113f316897cfaa0d5cc2cce3 /suite/testcommon.py | |
| parent | 838cb56a8505fc78d09befedd58dd632eeb2ee62 (diff) | |
updated unit tests with descriptive output
move logging endpoint to environment variable
Diffstat (limited to 'suite/testcommon.py')
| -rw-r--r-- | suite/testcommon.py | 412 |
1 files changed, 219 insertions, 193 deletions
diff --git a/suite/testcommon.py b/suite/testcommon.py index 4e004630..47e32e00 100644 --- a/suite/testcommon.py +++ b/suite/testcommon.py @@ -19,6 +19,11 @@ def get_file_list(test_store): return all_files +def remove_low_confidence(type_string): + low_confidence_types = ["int32_t", "void"] + for lct in low_confidence_types: + type_string = type_string.replace(lct + " ", '') # done to resolve confidence ties + return type_string class Builder(object): def __init__(self, test_store): @@ -57,26 +62,29 @@ class BinaryViewTestBuilder(Builder): def test_available_types(self): """Available types don't match""" - return [x.name for x in BinaryView(FileMetadata()).open(self.filename).available_view_types] + return ["Available Type: " + x.name for x in BinaryView(FileMetadata()).open(self.filename).available_view_types] def test_function_starts(self): """Function starts list doesnt match""" - return [hex(x.start) for x in self.bv.functions] + return ["Function start: " + hex(x.start) for x in self.bv.functions] def test_function_symbol_names(self): """Function.symbol.name list doesnt match""" - return [x.symbol.name + ' ' + str(x.symbol.type) + ' ' + hex(x.symbol.address) for x in self.bv.functions] + return ["Symbol: " + x.symbol.name + ' ' + str(x.symbol.type) + ' ' + hex(x.symbol.address) for x in self.bv.functions] def test_function_can_return(self): """Function.can_return list doesnt match""" - return [x.symbol.name + ' ' + str(x.symbol.type) + ' ' + hex(x.symbol.address) + ' ' + str(bool(x.can_return)) for x in self.bv.functions] + return ["function name: " + x.symbol.name + ' type: ' + str(x.symbol.type) + ' address: ' + hex(x.symbol.address) + ' can_return: ' + str(bool(x.can_return)) for x in self.bv.functions] def test_function_basic_blocks(self): """Function basic_block list doesnt match (start, end, has_undetermined_outgoing_edges)""" bblist = [] for func in self.bv.functions: for bb in func.basic_blocks: - bblist.append(hex(bb.start) + ' ' + hex(bb.end) + ' ' + str(bb.has_undetermined_outgoing_edges)) + bblist.append("basic block {} start: ".format(str(bb)) + hex(bb.start) + ' end: ' + hex(bb.end) + ' undetermined outgoing edges: ' + str(bb.has_undetermined_outgoing_edges)) + for anno in func.get_block_annotations(bb.start): + bblist.append("basic block {} function annotation: ".format(str(bb)) + str(anno)) + bblist.append("basic block {} test get self: ".format(str(bb)) + str(func.get_basic_block_at(bb.start))) return bblist def test_function_low_il_basic_blocks(self): @@ -84,24 +92,24 @@ class BinaryViewTestBuilder(Builder): ilbblist = [] for func in self.bv.functions: for bb in func.low_level_il.basic_blocks: - ilbblist.append(hex(bb.start) + ' ' + hex(bb.end) + ' ' + str(len(bb.outgoing_edges))) + ilbblist.append("LLIL basic block {} start: ".format(str(bb)) + hex(bb.start) + ' end: ' + hex(bb.end) + ' outgoing edges: ' + str(len(bb.outgoing_edges))) return ilbblist - + def test_function_med_il_basic_blocks(self): """"Function med_il_basic_block list doesn't match""" ilbblist = [] for func in self.bv.functions: for bb in func.medium_level_il.basic_blocks: - ilbblist.append(hex(bb.start) + ' ' + hex(bb.end) + ' ' + str(len(bb.outgoing_edges))) + ilbblist.append("MLIL basic block {} start: ".format(str(bb)) + hex(bb.start) + ' end: ' + hex(bb.end) + ' outgoing_edges: ' + str(len(bb.outgoing_edges))) return ilbblist def test_symbols(self): """"Symbols list doesn't match""" - return [str(i) for i in sorted(self.bv.symbols)] + return ["Symbol: " + str(i) for i in sorted(self.bv.symbols)] def test_strings(self): """Strings list doesn't match""" - return [x.value + ' ' + str(x.type) + ' ' + hex(x.start) for x in self.bv.strings] + return ["String: " + x.value + ' type: ' + str(x.type) + ' at: ' + hex(x.start) for x in self.bv.strings] def test_low_il_instructions(self): @@ -110,39 +118,39 @@ class BinaryViewTestBuilder(Builder): for func in self.bv.functions: for bb in func.low_level_il.basic_blocks: for ins in bb: - retinfo.append(str(ins.medium_level_il)) - retinfo.append(str(ins.mapped_medium_level_il)) - retinfo.append(str(ins.value)) - retinfo.append(str(ins.possible_values)) - retinfo.append(str(ins.prefix_operands)) - retinfo.append(str(ins.postfix_operands)) - retinfo.append(str(ins.ssa_form)) - retinfo.append(str(ins.non_ssa_form)) + retinfo.append("MLIL: " + str(ins.medium_level_il)) + retinfo.append("Mapped MLIL: " + str(ins.mapped_medium_level_il)) + retinfo.append("Value: " + str(ins.value)) + retinfo.append("Possible Values: " + str(ins.possible_values)) + retinfo.append("Prefix operands: " + str(ins.prefix_operands)) + retinfo.append("Postfix operands: " + str(ins.postfix_operands)) + retinfo.append("SSA form: " + str(ins.ssa_form)) + retinfo.append("Non-SSA form: " + str(ins.non_ssa_form)) return retinfo - + def test_low_il_ssa(self): """LLIL ssa produced different output""" retinfo = [] - reg_list = [binja.SSARegister(i,1) for i in self.bv.arch.regs] - flag_list = [binja.SSAFlag(i,1) for i in self.bv.arch.flags] for func in self.bv.functions: func = func.low_level_il - for reg in reg_list: - retinfo.append(str(func.get_ssa_reg_definition(reg))) - retinfo.append(str(func.get_ssa_reg_uses(reg))) - retinfo.append(str(func.get_ssa_reg_value(reg))) - for flag in flag_list: - retinfo.append(str(func.get_ssa_flag_uses(flag))) - retinfo.append(str(func.get_ssa_flag_value(flag))) + for reg_name in self.bv.arch.regs: + reg = binja.SSARegister(reg_name, 1) + retinfo.append("Reg {} SSA definition: ".format(reg_name) + str(func.get_ssa_reg_definition(reg))) + retinfo.append("Reg {} SSA uses: ".format(reg_name) + str(func.get_ssa_reg_uses(reg))) + retinfo.append("Reg {} SSA value: ".format(reg_name) + str(func.get_ssa_reg_value(reg))) + for flag_name in self.bv.arch.flags: + flag = binja.SSAFlag(flag_name, 1) + retinfo.append("Flag {} SSA uses: ".format(flag_name) + str(func.get_ssa_flag_uses(flag))) + retinfo.append("Flag {} SSA value: ".format(flag_name) + str(func.get_ssa_flag_value(flag))) for bb in func.basic_blocks: for ins in bb: tempind = func.get_non_ssa_instruction_index(ins.instr_index) - retinfo.append(str(tempind)) - retinfo.append(str(func.get_ssa_instruction_index(tempind))) - retinfo.append(str(func.get_medium_level_il_instruction_index(ins.instr_index))) - retinfo.append(str(func.get_mapped_medium_level_il_instruction_index(ins.instr_index))) + retinfo.append("Non-SSA instruction index: " + str(tempind)) + retinfo.append("SSA instruction index: " + str(func.get_ssa_instruction_index(tempind))) + retinfo.append("MLIL instruction index: " + str(func.get_medium_level_il_instruction_index(ins.instr_index))) + retinfo.append("Mapped MLIL instruction index: " + str(func.get_mapped_medium_level_il_instruction_index(ins.instr_index))) return retinfo @@ -153,19 +161,19 @@ class BinaryViewTestBuilder(Builder): for func in self.bv.functions: for bb in func.medium_level_il.basic_blocks: for ins in bb: - retinfo.append(str(ins.expr_type)) - retinfo.append(str(ins.low_level_il)) - retinfo.append(str(ins.value)) - retinfo.append(str(ins.possible_values)) - retinfo.append(str(ins.branch_dependence)) - retinfo.append(str(sorted([str(i) for i in ins.prefix_operands]))) - retinfo.append(str(sorted([str(i) for i in ins.postfix_operands]))) - retinfo.append(str(ins.ssa_form)) - retinfo.append(str(ins.non_ssa_form)) + retinfo.append("Expression type: " + str(ins.expr_type)) + retinfo.append("LLIL: " + str(ins.low_level_il)) + retinfo.append("Value: " + str(ins.value)) + retinfo.append("Possible values: " + str(ins.possible_values)) + retinfo.append("Branch dependence: " + str(ins.branch_dependence)) + retinfo.append("Prefix operands: " + str(sorted([str(i) for i in ins.prefix_operands]))) + retinfo.append("Postfix operands: " + str(sorted([str(i) for i in ins.postfix_operands]))) + retinfo.append("SSA form: " + str(ins.ssa_form)) + retinfo.append("Non-SSA form" + str(ins.non_ssa_form)) return retinfo - + def test_med_il_vars(self): """"Function med_il_vars doesn't match""" @@ -178,11 +186,11 @@ class BinaryViewTestBuilder(Builder): for var in (instruction.vars_read + instruction.vars_written): #varlist.append((func.get_var_uses(var), func.get_var_definitions(var))) if hasattr(var, "var"): - varlist.append(str(func.get_ssa_var_definition(var)) + ' ' + \ - str(func.get_ssa_var_uses(var)) + ' ' + \ - str(func.get_ssa_var_value(var)) + ' ' + \ - str(instruction.get_ssa_var_possible_values(var)) + ' ' + \ - str(instruction.get_ssa_var_version)) + varlist.append("SSA var definition: " + str(func.get_ssa_var_definition(var))) + varlist.append("SSA var uses: " + str(func.get_ssa_var_uses(var))) + varlist.append("SSA var value: " + str(func.get_ssa_var_value(var))) + varlist.append("SSA var possible values: " + str(instruction.get_ssa_var_possible_values(var))) + varlist.append("SSA var version: " + str(instruction.get_ssa_var_version)) return varlist def test_function_stack(self): @@ -194,14 +202,13 @@ class BinaryViewTestBuilder(Builder): func.create_user_stack_var(0, binja.Type.int(4), "testuservar") func.create_auto_stack_var(4, binja.Type.int(4), "testautovar") - temp = [] - for i in func.stack_layout: - temp.append(str(i)) - funcinfo.append(str(temp)) + sl = func.stack_layout + for i in range(len(sl)): + funcinfo.append("Stack position {}: ".format(i) + str(sl[i])) - funcinfo.append(str(func.get_stack_contents_at(func.start + 0x10, 0, 0x10))) - funcinfo.append(str(func.get_stack_contents_after(func.start + 0x10, 0, 0x10))) - funcinfo.append(str(func.get_stack_var_at_frame_offset(0, 0))) + funcinfo.append("Stack content sample: " + str(func.get_stack_contents_at(func.start + 0x10, 0, 0x10))) + funcinfo.append("Stack content range sample: " + str(func.get_stack_contents_after(func.start + 0x10, 0, 0x10))) + funcinfo.append("Sample stack var: " + str(func.get_stack_var_at_frame_offset(0, 0))) func.delete_user_stack_var(0) func.delete_auto_stack_var(0) @@ -213,17 +220,17 @@ class BinaryViewTestBuilder(Builder): for func in self.bv.functions: for llilbb in func.llil_basic_blocks: - retinfo.append(str(llilbb)) + retinfo.append("LLIL basic block: " + str(llilbb)) for llilins in func.llil_instructions: - retinfo.append(str(llilins)) - + retinfo.append("LLIL instruction: " + str(llilins)) + for mlilbb in func.mlil_basic_blocks: - retinfo.append(str(mlilbb)) + retinfo.append("MLIL basic block: " + str(mlilbb)) for mlilins in func.mlil_instructions: - retinfo.append(str(mlilins)) + retinfo.append("MLIL instruction: " + str(mlilins)) for ins in func.instructions: - retinfo.append(str(ins)) + retinfo.append("Instructiin: {}: ".format(hex(ins[1])) + ''.join([str(i) for i in ins[0]])) return retinfo @@ -245,34 +252,38 @@ class BinaryViewTestBuilder(Builder): func.clobbered_regs = func.clobbered_regs func.set_user_instr_highlight(func.start, binja.highlight.HighlightColor(red=0xff, blue=0xff, green=0)) func.set_auto_instr_highlight(func.start, binja.highlight.HighlightColor(red=0xff, blue=0xfe, green=0)) - - funcinfo.append(str([str(i) for i in func.vars])) - funcinfo.append(str(func.indirect_branches)) - funcinfo.append(str(func.session_data)) - funcinfo.append(len(func.analysis_performance_info)) - funcinfo.append(str(func.clobbered_regs)) - funcinfo.append(str(func.explicitly_defined_type)) - funcinfo.append(str(func.needs_update)) - funcinfo.append(len(func.lifted_il)) - funcinfo.append(str(func.global_pointer_value)) - funcinfo.append(str(func.comment)) - funcinfo.append(str(func.too_large)) - funcinfo.append(str(func.analysis_skipped)) - funcinfo.append(str(func.get_low_level_il_at(func.start))) - funcinfo.append(str(func.get_low_level_il_exits_at(func.start+0x100))) - funcinfo.append(str(func.get_regs_read_by(func.start))) - funcinfo.append(str(func.get_regs_written_by(func.start))) - funcinfo.append(str(func.get_stack_vars_referenced_by(func.start))) - funcinfo.append(str(func.get_constants_referenced_by(func.start))) - funcinfo.append(str(func.get_lifted_il_at(func.start))) - funcinfo.append(str(func.get_flags_read_by_lifted_il_instruction(0))) - funcinfo.append(str(func.get_flags_written_by_lifted_il_instruction(0))) - funcinfo.append(str(func.create_graph())) - funcinfo.append(str(func.get_indirect_branches_at(func.start+0x10))) - funcinfo.append(str(func.get_block_annotations(func.start))) - funcinfo.append(str(func.get_basic_block_at(func.start))) - funcinfo.append(str(func.get_instr_highlight(func.start))) - funcinfo.append(str(func.get_type_tokens())) + + for var in func.vars: + funcinfo.append("Function {} var: ".format(func.name) + str(var)) + + for branch in func.indirect_branches: + funcinfo.append("Function {} indirect branch: ".format(func.name) + str(branch)) + funcinfo.append("Function {} session data: ".format(func.name) + str(func.session_data)) + funcinfo.append("Function {} analysis perf length: ".format(func.name) + str(len(func.analysis_performance_info))) + for cr in func.clobbered_regs: + funcinfo.append("Function {} clobbered reg: ".format(func.name) + str(cr)) + funcinfo.append("Function {} explicitly defined type: ".format(func.name) + str(func.explicitly_defined_type)) + funcinfo.append("Function {} needs update: ".format(func.name) + str(func.needs_update)) + funcinfo.append("Function {} global pointer value: ".format(func.name) + str(func.global_pointer_value)) + funcinfo.append("Function {} comment: ".format(func.name) + str(func.comment)) + funcinfo.append("Function {} too large: ".format(func.name) + str(func.too_large)) + funcinfo.append("Function {} analysis skipped: ".format(func.name) + str(func.analysis_skipped)) + funcinfo.append("Function {} first ins LLIL: ".format(func.name) + str(func.get_low_level_il_at(func.start))) + funcinfo.append("Function {} LLIL exit test: ".format(func.name) + str(func.get_low_level_il_exits_at(func.start+0x100))) + funcinfo.append("Function {} regs read test: ".format(func.name) + str(func.get_regs_read_by(func.start))) + funcinfo.append("Function {} regs written test: ".format(func.name) + str(func.get_regs_written_by(func.start))) + funcinfo.append("Function {} stack var test: ".format(func.name) + str(func.get_stack_vars_referenced_by(func.start))) + funcinfo.append("Function {} constant reference test: ".format(func.name) + str(func.get_constants_referenced_by(func.start))) + funcinfo.append("Function {} first ins lifted IL: ".format(func.name) + str(func.get_lifted_il_at(func.start))) + funcinfo.append("Function {} flags read by lifted IL ins: ".format(func.name) + str(func.get_flags_read_by_lifted_il_instruction(0))) + funcinfo.append("Function {} flags written by lifted IL ins: ".format(func.name) + str(func.get_flags_written_by_lifted_il_instruction(0))) + funcinfo.append("Function {} create graph: ".format(func.name) + str(func.create_graph())) + funcinfo.append("Function {} indirect branches test: ".format(func.name) + str(func.get_indirect_branches_at(func.start+0x10))) + funcinfo.append("Function {} test instr highlight: ".format(func.name) + str(func.get_instr_highlight(func.start))) + for token in func.get_type_tokens(): + token = str(token) + token = remove_low_confidence(token) + funcinfo.append("Function {} type token: ".format(func.name) + str(token)) return funcinfo @@ -281,24 +292,29 @@ class BinaryViewTestBuilder(Builder): """BinaryView produced different results""" retinfo = [] - retinfo += [str(i[1]) for i in self.bv.types.items()] - retinfo.append(str(self.bv.segments)) - retinfo.append(str(self.bv.sections)) - retinfo.append(str(self.bv.allocated_ranges)) - retinfo.append(str(self.bv.session_data)) + for type in self.bv.types.items(): + retinfo.append("BV Type: " + str(type)) + for segment in sorted([str(i) for i in self.bv.segments]): + retinfo.append("BV segment: " + str(segment)) + for section in sorted(self.bv.sections): + retinfo.append("BV section: " + str(section)) + for allrange in self.bv.allocated_ranges: + retinfo.append("BV allocated range: " + str(allrange)) + retinfo.append("Session Data: " + str(self.bv.session_data)) for var in self.bv.data_vars: - retinfo.append(str(var)) - retinfo.append(str(self.bv.entry_function)) + retinfo.append("BV data var: " + str(var)) + retinfo.append("BV Entry function: " + str(self.bv.entry_function)) for i in self.bv: - retinfo.append(str(i)) - retinfo.append(hex(self.bv.entry_point)) - retinfo.append(hex(self.bv.start)) - retinfo.append("length: " + hex(len(self.bv))) + retinfo.append("BV function: " + str(i)) + retinfo.append("BV entry point: " + hex(self.bv.entry_point)) + retinfo.append("BV start: " + hex(self.bv.start)) + retinfo.append("BV length: " + hex(len(self.bv))) return retinfo + class TestBuilder(Builder): """ The TestBuilder is for tests that need to be checked against stored oracle data that isn't from a binary. These test are @@ -312,62 +328,64 @@ class TestBuilder(Builder): def test_BinaryViewType_list(self): """BinaryViewType list doesnt match""" - return [x.name for x in binja.BinaryViewType.list] + return ["BinaryViewType: " + x.name for x in binja.BinaryViewType.list] def test_Architecture_list(self): """Architecture list doesnt match""" - return [x.name for x in binja.Architecture.list] + return ["Arch name: " + x.name for x in binja.Architecture.list] def test_Assemble(self): """unexpected assemble result""" result = [] # success cases - result.append(binja.Architecture["x86"].assemble("xor eax, eax")) - result.append(binja.Architecture["x86_64"].assemble("xor rax, rax")) - result.append(binja.Architecture["mips32"].assemble("move $ra, $zero")) - result.append(binja.Architecture["mipsel32"].assemble("move $ra, $zero")) - result.append(binja.Architecture["armv7"].assemble("str r2, [sp, #-0x4]!")) - result.append(binja.Architecture["aarch64"].assemble("mov x0, x0")) - result.append(binja.Architecture["thumb2"].assemble("ldr r4, [r4]")) - result.append(binja.Architecture["thumb2eb"].assemble("ldr r4, [r4]")) + result.append("x86 assembly: " + str(binja.Architecture["x86"].assemble("xor eax, eax"))) + result.append("x86_64 assembly: " + str(binja.Architecture["x86_64"].assemble("xor rax, rax"))) + result.append("mips32 assembly: " + str(binja.Architecture["mips32"].assemble("move $ra, $zero"))) + result.append("mipsel32 assembly: " + str(binja.Architecture["mipsel32"].assemble("move $ra, $zero"))) + result.append("armv7 assembly: " + str(binja.Architecture["armv7"].assemble("str r2, [sp, #-0x4]!"))) + result.append("aarch64 assembly: " + str(binja.Architecture["aarch64"].assemble("mov x0, x0"))) + result.append("thumb2 assembly: " + str(binja.Architecture["thumb2"].assemble("ldr r4, [r4]"))) + result.append("thumb2eb assembly: " + str(binja.Architecture["thumb2eb"].assemble("ldr r4, [r4]"))) # fail cases - result.append(binja.Architecture["x86"].assemble("thisisnotaninstruction")) - result.append(binja.Architecture["x86_64"].assemble("thisisnotaninstruction")) - result.append(binja.Architecture["mips32"].assemble("thisisnotaninstruction")) - result.append(binja.Architecture["mipsel32"].assemble("thisisnotaninstruction")) - result.append(binja.Architecture["armv7"].assemble("thisisnotaninstruction")) - result.append(binja.Architecture["aarch64"].assemble("thisisnotaninstruction")) - result.append(binja.Architecture["thumb2"].assemble("thisisnotaninstruction")) - result.append(binja.Architecture["thumb2eb"].assemble("thisisnotaninstruction")) + result.append("x86 assembly: " + str(binja.Architecture["x86"].assemble("thisisnotaninstruction"))) + result.append("x86_64 assembly: " + str(binja.Architecture["x86_64"].assemble("thisisnotaninstruction"))) + result.append("mips32 assembly: " + str(binja.Architecture["mips32"].assemble("thisisnotaninstruction"))) + result.append("mipsel32 assembly: " + str(binja.Architecture["mipsel32"].assemble("thisisnotaninstruction"))) + result.append("armv7 assembly: " + str(binja.Architecture["armv7"].assemble("thisisnotaninstruction"))) + result.append("aarch64 assembly: " + str(binja.Architecture["aarch64"].assemble("thisisnotaninstruction"))) + result.append("thumb2 assembly: " + str(binja.Architecture["thumb2"].assemble("thisisnotaninstruction"))) + result.append("thumb2eb assembly: " + str(binja.Architecture["thumb2eb"].assemble("thisisnotaninstruction"))) return result def test_Architecture(self): """Architecture failure""" if not os.path.exists(os.path.join(os.path.expanduser("~"), '.binaryninja', 'plugins', 'nes.py')): - return + return [""] retinfo = [] file_name = os.path.join(self.test_store, "..", "pwnadventurez.nes") bv = binja.BinaryViewType["NES Bank 0"].open(file_name) - - retinfo.append(str([str(i) for i in bv.platform.arch.calling_conventions])) - retinfo.append(str(bv.platform.arch.full_width_regs)) + + for i in bv.platform.arch.calling_conventions: + retinfo.append("Custom arch calling convention: " + str(i)) + for i in bv.platform.arch.full_width_regs: + retinfo.append("Custom arch full width reg: " + str(i)) reg = binja.RegisterValue() - retinfo.append(str(reg.entry_value(bv.platform.arch, 'x'))) - retinfo.append(str(reg.constant(0xfe))) - retinfo.append(str(reg.constant_ptr(0xcafebabe))) - retinfo.append(str(reg.stack_frame_offset(0x10))) - retinfo.append(str(reg.imported_address(0xdeadbeef))) - retinfo.append(str(reg.return_address())) + retinfo.append("Reg entry value: " + str(reg.entry_value(bv.platform.arch, 'x'))) + retinfo.append("Reg constant: " + str(reg.constant(0xfe))) + retinfo.append("Reg constant pointer: " + str(reg.constant_ptr(0xcafebabe))) + retinfo.append("Reg stack frame offset: " + str(reg.stack_frame_offset(0x10))) + retinfo.append("Reg imported address: " + str(reg.imported_address(0xdeadbeef))) + retinfo.append("Reg return address: " + str(reg.return_address())) bv.update_analysis_and_wait() for func in bv.functions: for bb in func.low_level_il.basic_blocks: for ins in bb: - retinfo.append(str(bv.platform.arch.get_instruction_info(0x10, ins.address))) - retinfo.append(str(bv.platform.arch.get_instruction_text(0x10, ins.address))) - retinfo.append(str(ins)) + retinfo.append("Instruction info: " + str(bv.platform.arch.get_instruction_info(0x10, ins.address))) + retinfo.append("Instruction test: " + str(bv.platform.arch.get_instruction_text(0x10, ins.address))) + retinfo.append("Instruction: " + str(ins)) return retinfo @@ -376,10 +394,11 @@ class TestBuilder(Builder): """Function produced different result""" inttype = binja.Type.int(4) testfunction = binja.Type.function(inttype, [inttype, inttype, inttype]) - return [str(testfunction.parameters), str(testfunction.pointer(binja.Architecture["x86"], testfunction))] + return ["Test_function params: " + str(testfunction.parameters), "Test_function pointer: " + str(testfunction.pointer(binja.Architecture["x86"], testfunction))] def test_Struct(self): """Struct produced different result""" + retinfo = [] inttype = binja.Type.int(4) struct = binja.Structure() struct.a = 1 @@ -387,22 +406,23 @@ class TestBuilder(Builder): struct.append(inttype) struct.replace(0, inttype) struct.remove(1) - retinfo = [str(i) for i in struct.members] - retinfo += [struct.width] + for i in struct.members: + retinfo.append("Struct member: " + str(i)) + retinfo.append("Struct width: " + str(struct.width)) struct.width = 16 - retinfo += [struct.width] - retinfo += [struct.alignment] + retinfo.append("Struct width after adjustment: " + str(struct.width)) + retinfo.append("Struct alignment: " + str(struct.alignment)) struct.alignment = 8 - retinfo += [struct.alignment] - retinfo += [struct.packed] + retinfo.append("Struct alignment after adjustment: " + str(struct.alignment)) + retinfo.append("Struct packed: " + str(struct.packed)) struct.packed = 1 - retinfo += [struct.packed] - retinfo += [struct.type] - retinfo = [str(i) for i in retinfo] + retinfo.append("Struct packed after adjustment: " + str(struct.packed)) + retinfo.append("Struct type: " + str(struct.type)) return retinfo def test_Enumeration(self): """Enumeration produced different result""" + retinfo = [] inttype = binja.Type.int(4) enum = binja.Enumeration() enum.a = 1 @@ -410,8 +430,8 @@ class TestBuilder(Builder): enum.append("b", 2) enum.replace(0, "a", 2) enum.remove(0) - retinfo = [str(enum)] - retinfo += [str((enum == enum) and not (enum != enum))] + retinfo.append(str(enum)) + retinfo.append(str((enum == enum) and not (enum != enum))) return retinfo def test_Types(self): @@ -419,7 +439,7 @@ class TestBuilder(Builder): file_name = os.path.join(self.test_store, "helloworld") bv = binja.BinaryViewType.get_view_of_file(file_name) - + preprocessed = binja.preprocess_source(""" #ifdef nonexistant int foo = 1; @@ -436,11 +456,13 @@ class TestBuilder(Builder): tokens = inttype.get_tokens() + inttype.get_tokens_before_name() + inttype.get_tokens_after_name() namedtype = binja.NamedTypeReference() - retinfo = [[str(i) for i in typelist.variables.popitem()][::-1] for i in range(len(typelist.variables))] - retinfo += [str(namedtype)] - - equalcheck = (inttype == inttype) and not (inttype != inttype) - retinfo += [str(equalcheck)] + retinfo = [] + for i in range(len(typelist.variables)): + for j in typelist.variables.popitem(): + retinfo.append("Type: " + str(j)) + retinfo.append("Named Type: " + str(namedtype)) + + retinfo.append("Type equality: " + str((inttype == inttype) and not (inttype != inttype))) return retinfo @@ -458,12 +480,13 @@ class TestBuilder(Builder): file = os.path.join(self.test_store, "helloworld") self.unpackage_file(file) bv = binja.BinaryViewType['ELF'].open(file) - settings = binja.DisassemblySettings() disass = bv.linear_disassembly - res = [] + retinfo = [] for i in disass: - res.append(str(i)) - #return res + i = str(i) + i = remove_low_confidence(i) + retinfo.append(i) + return retinfo def test_partial_register_dataflow(self): """partial_register_dataflow produced different results""" @@ -477,10 +500,11 @@ class TestBuilder(Builder): for func in bv.functions: llil = func.low_level_il for i in range(0, llil.__len__()-1): - result.append(["LLIL:" + str(i).replace('L', '') + ":" + x + ":" + str(llil[i].get_reg_value(x)).replace('L', '') for x in reg_list]) - result.append(["LLIL:" + str(i).replace('L', '') + ":" + x + ":" + str(llil[i].get_possible_reg_values(x)).replace('L', '') for x in reg_list]) - result.append(["LLIL:" + str(i).replace('L', '') + ":" + x + ":" + str(llil[i].get_reg_value_after(x)).replace('L', '') for x in reg_list]) - result.append(["LLIL:" + str(i).replace('L', '') + ":" + x + ":" + str(llil[i].get_possible_reg_values_after(x)).replace('L', '') for x in reg_list]) + for x in reg_list: + result.append("LLIL:" + str(i).replace('L', '') + ":" + x + ":" + str(llil[i].get_reg_value(x)).replace('L', '')) + result.append("LLIL:" + str(i).replace('L', '') + ":" + x + ":" + str(llil[i].get_possible_reg_values(x)).replace('L', '')) + result.append("LLIL:" + str(i).replace('L', '') + ":" + x + ":" + str(llil[i].get_reg_value_after(x)).replace('L', '')) + result.append("LLIL:" + str(i).replace('L', '') + ":" + x + ":" + str(llil[i].get_possible_reg_values_after(x)).replace('L', '')) bv.file.close() del bv finally: @@ -500,17 +524,18 @@ class TestBuilder(Builder): for func in bv.functions: for bb in func.low_level_il.basic_blocks: for ins in bb: - retinfo.append(str(ins.get_stack_contents(0,1))) - retinfo.append(str(ins.get_stack_contents_after(0,1))) - retinfo.append(str(ins.get_possible_stack_contents(0,1))) - retinfo.append(str(ins.get_possible_stack_contents_after(0,1))) + retinfo.append("LLIL first stack element: " + str(ins.get_stack_contents(0,1))) + retinfo.append("LLIL second stack element: " + str(ins.get_stack_contents_after(0,1))) + retinfo.append("LLIL possible first stack element: " + str(ins.get_possible_stack_contents(0,1))) + retinfo.append("LLIL possible second stack element: " + str(ins.get_possible_stack_contents_after(0,1))) for flag in flag_list: - retinfo.append(str(ins.get_flag_value(flag))) - retinfo.append(str(ins.get_flag_value_after(flag))) - retinfo.append(str(ins.get_possible_flag_values(flag))) - retinfo.append(str(ins.get_possible_flag_values_after(flag))) + retinfo.append("LLIL flag {} value at: ".format(flag, hex(ins.address)) + str(ins.get_flag_value(flag))) + retinfo.append("LLIL flag {} value after {}: ".format(flag, hex(ins.address)) + str(ins.get_flag_value_after(flag))) + + retinfo.append("LLIL flag {} possible value at {}: ".format(flag, hex(ins.address)) + str(ins.get_possible_flag_values(flag))) + retinfo.append("LLIL flag {} possible value after {}: ".format(flag, hex(ins.address)) + str(ins.get_possible_flag_values_after(flag))) os.unlink(file_name) @@ -527,25 +552,26 @@ class TestBuilder(Builder): for func in bv.functions: for bb in func.medium_level_il.basic_blocks: for ins in bb: - retinfo.append(str(ins.get_var_for_stack_location(0))) - retinfo.append(str(ins.get_stack_contents(0, 0x10))) - retinfo.append(str(ins.get_stack_contents_after(0, 0x10))) - retinfo.append(str(ins.get_possible_stack_contents(0, 0x10))) - retinfo.append(str(ins.get_possible_stack_contents_after(0, 0x10))) + retinfo.append("MLIL stack begin var: " + str(ins.get_var_for_stack_location(0))) + retinfo.append("MLIL first stack element: " + str(ins.get_stack_contents(0, 1))) + retinfo.append("MLIL second stack element: " + str(ins.get_stack_contents_after(0, 1))) + retinfo.append("MLIL possible first stack element: " + str(ins.get_possible_stack_contents(0, 1))) + retinfo.append("MLIL possible second stack element: " + str(ins.get_possible_stack_contents_after(0, 1))) for reg in reg_list: - retinfo.append(str(ins.get_var_for_reg(reg))) - retinfo.append(str(ins.get_reg_value(reg))) - retinfo.append(str(ins.get_reg_value_after(reg))) - retinfo.append(str(ins.get_possible_reg_values(reg))) - retinfo.append(str(ins.get_possible_reg_values_after(reg))) + retinfo.append("MLIL reg {} var at {}: ".format(reg, hex(ins.address)) + str(ins.get_var_for_reg(reg))) + retinfo.append("MLIL reg {} value at {}: ".format(reg, hex(ins.address)) + str(ins.get_reg_value(reg))) + retinfo.append("MLIL reg {} value after {}: ".format(reg, hex(ins.address)) + str(ins.get_reg_value_after(reg))) + retinfo.append("MLIL reg {} possible value at {}: ".format(reg, hex(ins.address)) + str(ins.get_possible_reg_values(reg))) + retinfo.append("MLIL reg {} possible value after {}: ".format(reg, hex(ins.address)) + str(ins.get_possible_reg_values_after(reg))) for flag in flag_list: - retinfo.append(str(ins.get_flag_value(flag))) - retinfo.append(str(ins.get_flag_value_after(flag))) - retinfo.append(str(ins.get_possible_flag_values(flag))) - retinfo.append(str(ins.get_possible_flag_values_after(flag))) + retinfo.append("MLIL flag {} value at: ".format(flag, hex(ins.address)) + str(ins.get_flag_value(flag))) + retinfo.append("MLIL flag {} value after {}: ".format(flag, hex(ins.address)) + str(ins.get_flag_value_after(flag))) + retinfo.append("MLIL flag {} possible value at {}: ".format(flag, hex(ins.address)) + str(ins.get_possible_flag_values(flag))) + retinfo.append("MLIL flag {} possible value after {}: ".format(flag, hex(ins.address)) + str(ins.get_possible_flag_values(flag))) + os.unlink(file_name) @@ -570,72 +596,72 @@ class TestBuilder(Builder): def data_written(self, view, offset, length): def data_written_complete(self): - results.append("data written {0} {1}".format(offset, length)) + results.append("data written: offset {0} length {1}".format(hex(offset), hex(length))) evt = binja.AnalysisCompletionEvent(bv, data_written_complete) def data_inserted(self, view, offset, length): def data_inserted_complete(self): - results.append("data inserted {0} {1}".format(offset, length)) + results.append("data inserted: offset {0} length {1}".format(hex(offset), hex(length))) evt = binja.AnalysisCompletionEvent(bv, data_inserted_complete) def data_removed(self, view, offset, length): def data_removed_complete(self): - results.append("data removed {0} {1}".format(offset, length)) + results.append("data removed: offset {0} length {1}".format(hex(offset), hex(length))) evt = binja.AnalysisCompletionEvent(bv, data_removed_complete) def function_added(self, view, func): def function_added_complete(self): - results.append("function added {0}".format(func.name)) + results.append("function added: {0}".format(func.name)) evt = binja.AnalysisCompletionEvent(bv, function_added_complete) def function_removed(self, view, func): def function_removed_complete(self): - results.append("function removed {0}".format(func.name)) + results.append("function removed: {0}".format(func.name)) evt = binja.AnalysisCompletionEvent(bv, function_removed_complete) def function_updated(self, view, func): def function_updated_complete(self): - results.append("function updated {0}".format(func.name)) + results.append("function updated: {0}".format(func.name)) evt = binja.AnalysisCompletionEvent(bv, function_updated_complete) def function_update_requested(self, view, func): def function_update_requested_complete(self): - results.append("function update requested {0}".format(func.name)) + results.append("function update requested: {0}".format(func.name)) evt = binja.AnalysisCompletionEvent(bv, function_update_requested_complete) def data_var_added(self, view, var): def data_var_added_complete(self): - results.append("data var added {0}".format(var.name)) + results.append("data var added: {0}".format(var.name)) evt = binja.AnalysisCompletionEvent(bv, data_var_added_complete) def data_var_removed(self, view, var): def data_var_removed_complete(self): - results.append("data var removed {0}".format(var.name)) + results.append("data var removed: {0}".format(var.name)) evt = binja.AnalysisCompletionEvent(bv, data_var_removed_complete) def data_var_updated(self, view, var): def data_var_updated_complete(self): - results.append("data var updated {0}".format(var.name)) + results.append("data var updated: {0}".format(var.name)) evt = binja.AnalysisCompletionEvent(bv, data_var_updated_complete) def string_found(self, view, string_type, offset, length): def string_found_complete(self): - results.append("string found {0} {1}".format(offset, length)) + results.append("string found: offset {0} length {1}".format(hex(offset), hex(length))) evt = binja.AnalysisCompletionEvent(bv, string_found_complete) def string_removed(self, view, string_type, offset, length): def string_removed_complete(self): - results.append("string removed {0} {1}".format(offset, length)) + results.append("string removed: offset {0} length {1}".format(hex(offset), hex(length))) evt = binja.AnalysisCompletionEvent(bv, string_removed_complete) def type_defined(self, view, name, type): def type_defined_complete(self): - results.append("type defined {0} {1}".format(name)) + results.append("type defined: {0}".format(name)) evt = binja.AnalysisCompletionEvent(bv, type_defined_complete) def type_undefined(self, view, name, type): def type_undefined_complete(self): - results.append("type undefined {0} {1}".format(name)) + results.append("type undefined: {0}".format(name)) evt = binja.AnalysisCompletionEvent(bv, type_undefined_complete) @@ -664,7 +690,7 @@ class TestBuilder(Builder): bv.unregister_notification(test) - return str(sorted(results)) + return sorted(results) def unpackage(self, fileName): testname = None |
