Ejemplo n.º 1
0
    def else_condition(label):
        """
        Create a else conditional branch entry point. It must match a IfCondition and a EndCondition instruction
        with the same label. It is optional in conditional branch block.
        If the condition of IfCondition instruction is False, the control flow skips the 'if' branch block,
        only executes the 'else' branch block. If ElseCondition instruction is not defined,
        the control flow jumps to the end of conditional branch block defined by a EndCondition instruction.

        @param label: a user defined label (example: "if_label_1")

        @return bool: always True
        """

        log.info(
            "ElseCondition instruction is labeled with '{}' ".format(label))
        conditional_branch = Global.conditional_branch_map[label]

        if conditional_branch['condition_eval']:
            log.info(
                "Conditional branch condition is True, skip ElseCondition block,jump to EndCondition instruction"
            )
            ctf_utility.set_goto_instruction_index(
                conditional_branch['end_condition_index'])
        else:
            log.info(
                "Conditional branch condition is False, proceed with ElseCondition block instructions"
            )

        return True
Ejemplo n.º 2
0
    def control_flow_goto(command_index):
        """
        Deprecated function, may be removed in future.

        @return bool: always True .
        """
        log.info("Setting next instruction index: {}".format(command_index))
        ctf_utility.set_goto_instruction_index(command_index)
        return True
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    def end_loop(label):
        """
       Create a loop exit point. It must match a BeginLoop instruction with the same label.
       If the looping condition in BeginLoop is False, the control flow jumps to the corresponding EndLoop instruction,
       and exits the loop.

        @param label: a user defined label (example: "LOOP_1")

        @return bool: always True
        """
        # Global.label_map dict is set by lib.test.process_control_flow_label
        # each item in Global.label_map includes 3 elements: instruction "BeginLoop" index (int);
        # instruction "EndLoop" index (int); and loop_condition (bool)
        if Global.label_map[label]['condition_eval']:
            log.info(
                "Continuing Loop... Jump to the Begin_loop instruction labeled with '{}' "
                .format(label))
            ctf_utility.set_goto_instruction_index(
                Global.label_map[label]['beginloop_index'])
        else:
            log.info("Ending Loop... ")
        return True
Ejemplo n.º 6
0
    def control_flow_conditional_goto(variable_name,
                                      operator,
                                      value,
                                      true_label='',
                                      false_label=''):
        """
        Deprecated function, may be removed in future.

        @return bool: always True .
        """
        if true_label == '' and false_label == '':
            log.error("Could not both true_label and false_label be '' ")
            return False

        op_func = ctf_utility.operator_map.get(operator, None)
        variable_value = ctf_utility.get_variable(variable_name)
        if variable_value is None:
            log.error("User defined variable {} does not exist!".format(
                variable_name))
            return False

        log.info("Variable {} = {}".format(variable_name, value))
        status = op_func(variable_value, value)
        log.info("Checking {} {} {} => {}".format(variable_value, operator,
                                                  value, status))
        # ENHANCE Check index out of range
        if status:
            if true_label != '':
                index = Global.goto_label_map[true_label]
                ctf_utility.set_goto_instruction_index(index)
        else:
            if false_label != '':
                index = Global.goto_label_map[false_label]
                ctf_utility.set_goto_instruction_index(index)

        return True
Ejemplo n.º 7
0
def test_ctf_utility_set_goto_instruction_index():
    assert Global.goto_instruction_index is None
    assert ctf_utility.set_goto_instruction_index(1) is None
    assert Global.goto_instruction_index == 1
    Global.goto_instruction_index = None