def get_blocks(self, config=None):
        """ Gets request blocks for Uuid Suffix Parameters.

        @return: A request block containing the uuid key
        @rtype : List[str]

        """
        return [primitives.restler_custom_payload_uuid4_suffix(self._content)]
Beispiel #2
0
    primitives.restler_static_string("Accept: application/json\r\n"),
    primitives.restler_static_string("Host: restler.unit.test.server.com\r\n"),
    primitives.restler_static_string("Content-Type: application/json\r\n"),
    primitives.restler_refreshable_authentication_token(
        "authentication_token_tag"),
    primitives.restler_static_string("\r\n")
],
                           requestId="/city")
req_collection.add_request(request)

request = requests.Request([
    primitives.restler_static_string("PUT "),
    primitives.restler_static_string("/"),
    primitives.restler_static_string("city"),
    primitives.restler_static_string("/"),
    primitives.restler_custom_payload_uuid4_suffix("cityName"),
    primitives.restler_static_string(" HTTP/1.1\r\n"),
    primitives.restler_static_string("Accept: application/json\r\n"),
    primitives.restler_static_string("Host: restler.unit.test.server.com\r\n"),
    primitives.restler_static_string("Content-Type: application/json\r\n"),
    primitives.restler_static_string("\r\n"),
    primitives.restler_static_string("{"),
    primitives.restler_static_string('"population":'),
    primitives.restler_fuzzable_int('10000', quoted=True),
    primitives.restler_static_string(', "area": "5000",'),
    primitives.restler_fuzzable_string('strtest', quoted=True),
    primitives.restler_static_string(':'),
    primitives.restler_fuzzable_bool('true', quoted=True),
    primitives.restler_static_string("}"),
    primitives.restler_refreshable_authentication_token(
        "authentication_token_tag"),
Beispiel #3
0
    primitives.restler_static_string("Accept: application/json\r\n"),
    primitives.restler_static_string("Host: restler.unit.test.server.com\r\n"),
    primitives.restler_static_string("Content-Type: application/json\r\n"),
    primitives.restler_refreshable_authentication_token(
        "authentication_token_tag"),
    primitives.restler_static_string("\r\n")
],
                           requestId="/city")
req_collection.add_request(request)

request = requests.Request([
    primitives.restler_static_string("PUT "),
    primitives.restler_static_string("/"),
    primitives.restler_static_string("city"),
    primitives.restler_static_string("/"),
    primitives.restler_custom_payload_uuid4_suffix("cityName"),
    primitives.restler_static_string(" HTTP/1.1\r\n"),
    primitives.restler_static_string("Accept: application/json\r\n"),
    primitives.restler_static_string("Host: restler.unit.test.server.com\r\n"),
    primitives.restler_static_string("Content-Type: application/json\r\n"),
    primitives.restler_refreshable_authentication_token(
        "authentication_token_tag"),
    primitives.restler_static_string("\r\n"),
    primitives.restler_static_string("{"),
    primitives.restler_static_string('"properties":'),
    primitives.restler_static_string("{"),
    primitives.restler_static_string('"population":'),
    primitives.restler_fuzzable_int('10000', quoted=True),
    primitives.restler_static_string(', "area": "5000",'),
    primitives.restler_fuzzable_string('strtest', quoted=True),
    primitives.restler_static_string(':'),
Beispiel #4
0
    def render_iter(self, candidate_values_pool, skip=0, preprocessing=False):
        """ This is the core method that renders values combinations in a
        request template. It basically is a generator which lazily iterates over
        a pool of possible combination of values that fit the template of the
        requst. Static primitive types (such as, string or delimiters) are
        picked from @param candidate_values_pool; dynamic primitive types
        (such as, uuid4) are generated "fresh" every time the generator yields
        with the help of @method resolve_dynamic_primitives.

        @param candidate_values_pool: The pool of values for primitive types.
        @type candidate_values_pool: Dict
        @param skip: Number of combinations to skip (i.e., not render). This is
                        useful when we use a request as an "ingredient" of a
                        sequence and we are only interested in using a specific
                        rendering that, we know, leads to a valid rendering with
                        a desired status code response.
        @type skip: Int
        @param preprocessing: Set to True if this rendering is happening during preprocessing
        @type  preprocessing: Bool

        @return: (rendered request's payload, response's parser function)
        @rtype : (Str, Function Pointer)

        """
        def _raise_dict_err(type, tag):
            logger.write_to_main(
                f"Error for request {self.method} {self.endpoint_no_dynamic_objects}.\n"
                f"{type} exception: {tag} not found.\n"
                "Make sure you are using the dictionary created during compilation.",
                print_to_console=True)
            raise InvalidDictionaryException

        def _handle_exception(type, tag, err):
            logger.write_to_main(
                f"Exception when rendering request {self.method} {self.endpoint_no_dynamic_objects}.\n"
                f"Type: {type}. Tag: {tag}.\n"
                f"  Exception: {err!s}",
                print_to_console=True)
            raise InvalidDictionaryException

        if not candidate_values_pool:
            print("Candidate values pool empty")
            print(self._definition)
            yield []
            return

        definition = self.definition
        if not definition:
            yield []
            return

        parser = None
        # If request had post_send metadata, register parsers etc.
        if bool(self.metadata) and 'post_send' in self.metadata\
        and 'parser' in self.metadata['post_send']:
            parser = self.metadata['post_send']['parser']

        fuzzable = []
        for request_block in definition:
            primitive_type = request_block[0]
            default_val = request_block[1]
            quoted = request_block[2]

            values = []
            # Handling dynamic primitives that need fresh rendering every time
            if primitive_type == primitives.FUZZABLE_UUID4:
                values = [primitives.restler_fuzzable_uuid4]
            # Handle enums that have a list of values instead of one default val
            elif primitive_type == primitives.FUZZABLE_GROUP:
                values = list(request_block[1])
            # Handle static whose value is the field name
            elif primitive_type == primitives.STATIC_STRING:
                values = [request_block[1]]
            # Handle multipart form data
            elif primitive_type == primitives.FUZZABLE_MULTIPART_FORMDATA:
                try:
                    current_fuzzable_values = candidate_values_pool.\
                        get_candidate_values(primitive_type, request_id=self._request_id, tag=default_val, quoted=quoted)
                    values = [
                        multipart_formdata.render(current_fuzzable_values)
                    ]
                except primitives.CandidateValueException:
                    _raise_dict_err(primitive_type, default_val)
                except Exception as err:
                    _handle_exception(primitive_type, default_val, err)
            # Handle custom (user defined) static payload
            elif primitive_type == primitives.CUSTOM_PAYLOAD:
                try:
                    current_fuzzable_values = candidate_values_pool.\
                        get_candidate_values(primitive_type, request_id=self._request_id, tag=default_val, quoted=quoted)
                    # handle case where custom payload have more than one values
                    if isinstance(current_fuzzable_values, list):
                        values = current_fuzzable_values
                    else:
                        values = [current_fuzzable_values]
                except primitives.CandidateValueException:
                    _raise_dict_err(primitive_type, default_val)
                except Exception as err:
                    _handle_exception(primitive_type, default_val, err)
            # Handle custom (user defined) static payload on header (Adds \r\n)
            elif primitive_type == primitives.CUSTOM_PAYLOAD_HEADER:
                try:
                    current_fuzzable_values = candidate_values_pool.\
                        get_candidate_values(primitive_type, request_id=self._request_id, tag=default_val, quoted=quoted)
                    # handle case where custom payload have more than one values
                    if isinstance(current_fuzzable_values, list):
                        values = current_fuzzable_values
                    else:
                        values = [current_fuzzable_values]
                    if values:
                        values = list(
                            map(
                                lambda x: "{}: {}\r\n".\
                                format(default_val, x),
                                values
                            )
                        )
                except primitives.CandidateValueException:
                    _raise_dict_err(primitive_type, default_val)
                except Exception as err:
                    _handle_exception(primitive_type, default_val, err)
            # Handle custom (user defined) static payload with uuid4 suffix
            elif primitive_type == primitives.CUSTOM_PAYLOAD_UUID4_SUFFIX:
                try:
                    current_fuzzable_value = candidate_values_pool.\
                        get_candidate_values(primitive_type, request_id=self._request_id, tag=default_val, quoted=quoted)
                    values = [
                        primitives.restler_custom_payload_uuid4_suffix(
                            current_fuzzable_value)
                    ]
                except primitives.CandidateValueException:
                    _raise_dict_err(primitive_type, default_val)
                except Exception as err:
                    _handle_exception(primitive_type, default_val, err)
            elif primitive_type == primitives.REFRESHABLE_AUTHENTICATION_TOKEN:
                values = [primitives.restler_refreshable_authentication_token]
            # Handle all the rest
            else:
                values = candidate_values_pool.get_fuzzable_values(
                    primitive_type, default_val, self._request_id, quoted)

            if Settings().fuzzing_mode == 'random-walk' and not preprocessing:
                random.shuffle(values)

            if len(values) == 0:
                _raise_dict_err(primitive_type, current_fuzzable_tag)
            fuzzable.append(values)

        # lazy generation of pool for candidate values
        combinations_pool = itertools.product(*fuzzable)
        combinations_pool = itertools.islice(combinations_pool,
                                             Settings().max_combinations)

        # skip combinations, if asked to
        for _ in range(skip):
            next(combinations_pool)

        # for each combination's values render dynamic primitives and resolve
        # dependent variables
        for ind, values in enumerate(combinations_pool):
            values = list(values)
            values = request_utilities.resolve_dynamic_primitives(
                values, candidate_values_pool)

            rendered_data = "".join(values)
            yield rendered_data, parser
    primitives.restler_static_string("/"),
    primitives.restler_static_string("city"),
    primitives.restler_static_string(" HTTP/1.1\r\n"),
    primitives.restler_refreshable_authentication_token(
        "authentication_token_tag"),
    primitives.restler_static_string("\r\n")
],
                           requestId="/city")
req_collection.add_request(request)

request = requests.Request([
    primitives.restler_static_string("PUT "),
    primitives.restler_static_string("/"),
    primitives.restler_static_string("city"),
    primitives.restler_static_string("/"),
    primitives.restler_custom_payload_uuid4_suffix("cityName"),
    primitives.restler_static_string(" HTTP/1.1\r\n"),
    primitives.restler_refreshable_authentication_token(
        "authentication_token_tag"),
    primitives.restler_static_string("\r\n"),
    primitives.restler_static_string("{"),
    primitives.restler_static_string("}"),
    primitives.restler_static_string("\r\n"),
    {
        'post_send': {
            'parser': parse_cityNamePut,
            'dependencies': [_city_put_name.writer()]
        }
    },
],
                           requestId="/city/{cityName}")