Exemplo n.º 1
0
    def fuzz_dom_tree(self):
        class DOMTreeMode(Enum):
            InsertNode = 1
            AppendAttribute = 2
            MutateAttribute = 3
            ReplaceAttribute = 4
            MutateText = 5

        dom_tree_modes = [
            DOMTreeMode.InsertNode, DOMTreeMode.AppendAttribute,
            DOMTreeMode.MutateAttribute, DOMTreeMode.ReplaceAttribute,
            DOMTreeMode.MutateText
        ]

        trial = 0
        while trial < 10:
            c = Random.choices(dom_tree_modes, [3, 3, 2, 5, 1])[0]
            if c == DOMTreeMode.InsertNode:
                ok = self.insert_node()
            elif c == DOMTreeMode.AppendAttribute:
                ok = self.dom_tree.append_attribute(self.dom_context)
            elif c == DOMTreeMode.MutateAttribute:
                ok = self.dom_tree.mutate_attribute(self.dom_context)
            elif c == DOMTreeMode.ReplaceAttribute:
                ok = self.dom_tree.replace_attribute(self.dom_context)
            else:
                ok = self.dom_tree.mutate_text()
            assert ok is not None
            if ok:
                return True
            trial += 1
        return False
Exemplo n.º 2
0
    def fuzz_css(self):
        class CSSMode(Enum):
            Mutate = 1
            Append = 2
            Replace = 3
            Misc = 4

        css_modes = [
            CSSMode.Mutate, CSSMode.Append, CSSMode.Replace, CSSMode.Misc
        ]
        css_weights = [10, 5, 1, 1]

        trial = 0
        while trial < 10:
            c = Random.choices(css_modes, css_weights)[0]
            if c == CSSMode.Mutate:
                ok = self.css.mutate_css_style_rule(self.dom_context)
            elif c == CSSMode.Append:
                ok = self.css.append_css_style_rule(self.dom_context)
            elif c == CSSMode.Replace:
                ok = self.css.replace_css_style_rule(self.dom_context)
            else:
                if Random.bool():
                    ok = self.css.mutate_css_keyframes_rule(self.dom_context)
                else:
                    ok = self.css.mutate_css_variable(self.dom_context)
            assert ok is not None
            if ok:
                return True
            trial += 1
        return False
Exemplo n.º 3
0
    def generate_api(self):
        # 1. select an alive object (type)
        obj_names = list(self.context.superset_at_line)
        weights = [get_api_count(name) for name in obj_names]

        # 2. select an api that uses the object name as |this|
        while True:
            name = Random.choices(obj_names, weights)[0]
            template = Random.choice(js_apis[name])
            if template.satiable(self.context):
                api = template.instantiate()
                api.generate(self.context)
                return api
Exemplo n.º 4
0
def get_attribute_templates(name, include_aria=False):
    selector = Random.choices(attribute_types, [
        TreeConfig.attribute_weight["regular"] *
        len(regular_attributes[name]) if name in regular_attributes else 0,
        TreeConfig.attribute_weight["presentation"] *
        len(presentation_attributes[name]) if name in presentation_attributes
        else 0, TreeConfig.attribute_weight["global"] *
        len(global_attributes[name]) if name in global_attributes else 0,
        TreeConfig.attribute_weight["aria"] * len(aria_attributes[name])
        if include_aria and name in aria_attributes else 0
    ])[0]

    if selector == AttributeType.REGULAR:
        return regular_attributes[name]
    elif selector == AttributeType.PRESENTATION:
        return presentation_attributes[name]
    elif selector == AttributeType.GLOBAL:
        return global_attributes[name]
    else:
        return aria_attributes[name]