예제 #1
0
def create_form_helper(form_data):
    """
    Creates a dc.Form object from a dict container

    :param form_data: A list containing dicts representing a form's
        internal structure
    :return: A dc.Form object from `form_data`
    """
    new_form = form.Form()

    for elem_data in form_data:
        elem_type = elem_data['tagname']
        attrs = elem_data.items()

        if elem_type == 'input':
            _type = elem_data['type']

            if _type == 'radio':
                new_form.add_radio(attrs)
            elif _type == 'checkbox':
                new_form.add_check_box(attrs)
            elif _type in ('text', 'hidden'):
                new_form.add_input(attrs)

        elif elem_type == 'select':
            new_form.add_select(elem_data['name'], elem_data['options'])

    return new_form
예제 #2
0
    def test_all_variants(self):
        # 'all' mode variants
        bigform_data = form_with_radio + form_select_misc
        clean_data = get_gruped_data(bigform_data)
        new_bigform = create_form_helper(bigform_data)
        total_variants = 2 * 5 * 10
        variants_set = set()

        for i, form_variant in enumerate(new_bigform.get_variants(mode="all")):

            if i == 0:  # First element must be the created `new_bigform`
                self.assertEquals(id(new_bigform), id(form_variant))
                continue
            for name, all_values in clean_data.items():
                self.assertTrue(form_variant[name][0] in all_values)

            variants_set.add(repr(form_variant))

        # Ensure we actually got the expected number of variants
        f = form.Form()
        expected = min(total_variants, f.TOP_VARIANTS)
        self.assertEquals(expected, i)

        # Variants shouldn't duplicated
        self.assertEquals(expected, len(variants_set))
예제 #3
0
    def test_tmb_variants(self):
        # 'top-middle-bottom' mode variants
        def filter_tmb(values):
            if len(values) > 3:
                values = (values[0], values[len(values) / 2], values[-1])
            return values

        bigform_data = form_with_radio + form_select_misc
        clean_data = get_gruped_data(bigform_data)
        new_bigform = create_form_helper(bigform_data)
        total_variants = 2 * 2 * 3
        variants_set = set()

        for i, form_variant in enumerate(new_bigform.get_variants(mode="tmb")):

            if i == 0:  # First element must be the created `new_bigform`
                self.assertEquals(id(new_bigform), id(form_variant))
                continue

            for name, values in clean_data.items():
                tmb_values = filter_tmb(values)
                msg = 'Failed to find "%s" in "%s"' % (form_variant[name][0],
                                                       tmb_values)
                self.assertTrue(form_variant[name][0] in tmb_values, msg)

            variants_set.add(repr(form_variant))

        # Ensure we actually got the expected number of variants
        f = form.Form()
        expected = min(total_variants, f.TOP_VARIANTS)
        self.assertEquals(i, expected)

        # Variants shouldn't appear duplicated
        self.assertEquals(len(variants_set), expected)
예제 #4
0
    def test_tmb_variants_large(self):
        """
        Note that this test has several changes from test_tmb_variants:
            * It uses form_select_misc_large, which exceeds the form's TOP_VARIANTS = 15
            * Doesn't use filter_tmb since variants are based on a "random pick"
        """
        bigform_data = form_with_radio + form_select_cars + \
            form_select_misc_large
        clean_data = get_gruped_data(bigform_data)
        new_bigform = create_form_helper(bigform_data)
        total_variants = 2 * 3 * 3 * 3
        variants_set = set()

        # Please note that this depends completely in form.SEED AND form.TOP_VARIANTS
        RANDOM_PICKS = {
            1: ('volvo', 'black', 'd', 'female'),
            2: ('volvo', 'blue', 'i', 'male'),
            3: ('volvo', 'blue', 'f', 'female'),
            4: ('volvo', 'black', 'g', 'female'),
            5: ('volvo', 'black', 'm', 'male'),
            6: ('volvo', 'black', 'l', 'male'),
            7: ('volvo', 'blue', 'b', 'female'),
            8: ('volvo', 'blue', 'e', 'female'),
            9: ('volvo', 'black', 'c', 'male'),
            10: ('volvo', 'black', 'a', 'female'),
            11: ('volvo', 'blue', 'e', 'male'),
            12: ('volvo', 'black', 'j', 'male'),
            13: ('volvo', 'blue', 'c', 'male'),
            14: ('volvo', 'black', 'a', 'male'),
            15: ('volvo', 'black', 'i', 'female')
        }

        for i, form_variant in enumerate(new_bigform.get_variants(mode="tmb")):

            if i == 0:  # First element must be the created `new_bigform`
                self.assertEquals(id(new_bigform), id(form_variant))
                continue

            for name, values in clean_data.items():
                current_random_values = RANDOM_PICKS[i]
                msg = 'Failed to find "%s" in "%s"' % (form_variant[name][0],
                                                       current_random_values)
                self.assertTrue(form_variant[name][0] in current_random_values,
                                msg)

            variants_set.add(repr(form_variant))

        # Ensure we actually got the expected number of variants
        f = form.Form()
        expected = min(total_variants, f.TOP_VARIANTS)
        self.assertEquals(i, expected)

        # Variants shouldn't appear duplicated
        self.assertEquals(len(variants_set), expected)
예제 #5
0
파일: html.py 프로젝트: johnjohnsp1/w3af
    def _handle_form_tag_start(self, tag, attrs):
        """
        Handle the form tags.

        This method also looks if there are "pending inputs" in the
        self._saved_inputs list and parses them.
        """
        SGMLParser._handle_form_tag_start(self, tag, attrs)

        # Get the 'method'
        method = attrs.get('method', 'GET').upper()

        # Get the action
        action = attrs.get('action', None)
        missing_action = action is None

        if missing_action:
            action = self._source_url
        else:
            action = self._decode_url(action)
            try:
                action = self._base_url.url_join(action,
                                                 encoding=self._encoding)
            except ValueError:
                # The URL in the action is invalid, the best thing we can do
                # is to guess, and our best guess is that the URL will be the
                # current one.
                action = self._source_url

        # Create the form object and store everything for later use
        form_obj = form.Form(encoding=self._encoding)
        form_obj.set_method(method)
        form_obj.set_action(action)
        self._forms.append(form_obj)

        # Now I verify if there are any input tags that were found
        # outside the scope of a form tag
        for inputattrs in self._saved_inputs:
            # Parse them just like if they were found AFTER the
            # form tag opening
            if isinstance(inputattrs, dict):
                self._handle_input_tag_inside_form('input', inputattrs)

        # All parsed, remove them.
        self._saved_inputs = []
예제 #6
0
    def _handle_go_tag_start(self, tag, attrs):

        # Find method
        method = attrs.get('method', 'GET').upper()

        # Find action
        action = attrs.get('href', '')
        if action:
            self._inside_form = True
            action = unicode(self._base_url.url_join(action))
            action = URL(self._decode_url(action), encoding=self._encoding)
            # Create the form
            f = form.Form(encoding=self._encoding)
            f.set_method(method)
            f.set_action(action)
            self._forms.append(f)
        else:
            om.out.debug('WMLParser found a form without an action. '
                         'Javascript is being used.')