1
|
|
|
import random |
2
|
|
|
import requests |
3
|
|
|
from fuzzer import random_integers, random_ascii_chars, random_float |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def build_one_of_type(item_type, p=1.0): |
7
|
|
|
empty_values_for_types = { |
8
|
|
|
"str": random_ascii_chars, |
9
|
|
|
"int": random_integers, |
10
|
|
|
"float": random_float, |
11
|
|
|
"list": lambda: [], |
12
|
|
|
"dict": lambda: {}, |
13
|
|
|
"null": lambda: None |
14
|
|
|
} |
15
|
|
|
basic_types = ['str', 'int', 'float', 'list', 'dict', 'null'] |
16
|
|
|
mutation = random.random() |
17
|
|
|
return empty_values_for_types[item_type] \ |
18
|
|
|
if p > mutation \ |
19
|
|
|
else empty_values_for_types[random.choice(basic_types)] |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
def schema_to_object_builder(schema, p=1.0): |
23
|
|
|
mutation = random.random() |
24
|
|
|
type_of_object = schema['type'] if isinstance(schema, dict) else "null" |
25
|
|
|
root_object = build_one_of_type(type_of_object, p)() |
26
|
|
|
|
27
|
|
|
if isinstance(root_object, list): |
28
|
|
|
if p > mutation: |
29
|
|
|
root_object.append(schema_to_object_builder(schema['inner'], p=mutation)) |
30
|
|
|
|
31
|
|
|
elif isinstance(root_object, dict): |
32
|
|
|
for prop in schema['inner']: |
33
|
|
|
if p > mutation: |
34
|
|
|
root_object[prop['name']] = schema_to_object_builder(prop, p=mutation) |
35
|
|
|
|
36
|
|
|
return root_object |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def rules(host, port, api_list): |
40
|
|
|
""" |
41
|
|
|
api_list = [{ |
42
|
|
|
"url": "/some/path", |
43
|
|
|
"method": "POST", |
44
|
|
|
"body": {} |
45
|
|
|
}] |
46
|
|
|
""" |
47
|
|
|
|
48
|
|
|
for api in api_list: |
49
|
|
|
requests.request( |
50
|
|
|
method=api['method'], |
51
|
|
|
url='{host}:{port}{url}'.format(host=host, port=port, url=api['url']) |
52
|
|
|
) |
53
|
|
|
|
54
|
|
|
return {} |
55
|
|
|
|