def test_control_flow_control_flow_conditional_goto_pass(control_flow_plugin): assert VariablePlugin.set_user_defined_variable('my_var', '=', 10) Global.goto_label_map['label_1'] = 10 Global.goto_label_map['label_2'] = 20 assert control_flow_plugin.control_flow_conditional_goto('my_var', '<', 11, 'label_1') assert control_flow_plugin.control_flow_conditional_goto('my_var', '>', 11, 'label_1', 'label_2') Global.goto_label_map.clear() Global.goto_instruction_index = None
def test_control_flow_if_condition_endif(control_flow_plugin): label = "if_label_1" conditions = [{"variable": "my_var", "compare": "<", "value": 20}] assert VariablePlugin.set_user_defined_variable('my_var', '=', 21) Global.conditional_branch_map[label] = {"condition_eval": None, "end_condition_index": None, "else_condition_index": None, } assert control_flow_plugin.if_condition(label, conditions) Global.conditional_branch_map.clear()
def test_control_flow_begin_loop_fail(control_flow_plugin): label = "LOOP_1" conditions = [{"variable": "my_var", "compare": "<", "value": 20}] Global.label_map[label] = {"condition_eval": False, "beginloop_index": 1, "endloop_index": 20} assert VariablePlugin.set_user_defined_variable('my_var', '=', 100) assert Global.goto_instruction_index is None assert control_flow_plugin.begin_loop(label, conditions) assert Global.goto_instruction_index == 20 Global.label_map.clear()
def if_condition(label, conditions): """ Create a if conditional branch block entry point. It is identified by a unique label per test script. The IfCondition must be in pairs with EndCondition instruction. ElseCondition instruction is optional. The if condition is defined in parameter "conditions" as a list of variables and the associated comparison operations. The condition is True, only if all comparison operations are True. @param label: a user defined label (example: "if_label_1") @param conditions: a list of comparison conditions. Each includes "name", "operator" and "value". (example: {"name": "my_var", "operator": "<", "value": 20}) @return bool: return True, unless conditions argument is not a list . """ log.info( "IfCondition instruction is labeled with '{}', start the conditional branch" .format(label)) conditional_branch = Global.conditional_branch_map[label] status = True if not isinstance(conditions, list): log.info( "Conditional branch condition should be defined as a list of object" ) return False for condition in conditions: status = status and VariablePlugin.check_user_defined_variable( condition['variable'], condition['compare'], condition['value']) if status: log.info( "Conditional branch condition is True, proceed to the Next test instruction" ) conditional_branch['condition_eval'] = True else: if conditional_branch['else_condition_index']: info_str = 'ElseCondition' next_instruction_index = conditional_branch[ 'else_condition_index'] else: info_str = 'EndCondition' next_instruction_index = conditional_branch[ 'end_condition_index'] log.info( "Conditional branch condition is False, jump to the {} instruction" .format(info_str)) conditional_branch['condition_eval'] = False ctf_utility.set_goto_instruction_index(next_instruction_index) return True
def begin_loop(label, conditions): """ Create a loop entry point. The loop is identified by a unique label. The BeginLoop must be in pairs with EndLoop instruction. The loop condition is defined in parameter "conditions" as a list of variables and the associated comparison operations. The condition is True, only if all comparison operations are True. @param label: a user defined label (example: "LOOP_1") @param conditions: a list of comparison conditions. Each includes "name", "operator" and "value". (example: {"name": "my_var", "operator": "<", "value": 20}) @return bool: always True . """ log.info("Begin_loop instruction is labeled with '{}'".format(label)) control_flow = Global.label_map[label] status = True if isinstance(conditions, dict): # instruction = conditions # log.info("Execute instruction {}... ".format(instruction)) # status = Global.plugin_manager.find_plugin_for_command_and_execute(instruction) # log.info("Wrapped instruction execution status: {} ".format(status)) # condition as test instruction result is disabled # conditions is a test instruction status = False elif isinstance(conditions, list): copied_conditions = deepcopy(conditions) for condition in copied_conditions: condition['variable'] = resolve_variable(condition['variable']) condition['value'] = resolve_variable(condition['value']) status = status and VariablePlugin.check_user_defined_variable( condition['variable'], condition['compare'], condition['value']) log.info("{} ".format(conditions)) if status: log.info( "Continuing Loop... Proceed To The Next Test Instruction") Global.label_map[label]['condition_eval'] = True else: log.info("Ending Loop... Jump to the End_loop") Global.label_map[label]['condition_eval'] = False ctf_utility.set_goto_instruction_index( control_flow['endloop_index']) return True
def _variable_plugin_instance(): return VariablePlugin()