forked from microsoft/restler-fuzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestler-quick-start.py
108 lines (89 loc) · 3.84 KB
/
restler-quick-start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import argparse
import contextlib
import os
import subprocess
from pathlib import Path
RESTLER_TEMP_DIR = 'restler_working_dir'
@contextlib.contextmanager
def usedir(dir):
""" Helper for 'with' statements that changes the current directory to
@dir and then changes the directory back to its original once the 'with' ends.
Can be thought of like pushd with an auto popd after the 'with' scope ends
"""
curr = os.getcwd()
os.chdir(dir)
try:
yield
finally:
os.chdir(curr)
def compile_spec(api_spec_path, restler_dll_path):
""" Compiles a specified api spec
@param api_spec_path: The absolute path to the Swagger file to compile
@type api_spec_path: Str
@param restler_dll_path: The absolute path to the RESTler driver's dll
@type restler_dll_path: Str
@return: None
@rtype : None
"""
if not os.path.exists(RESTLER_TEMP_DIR):
os.makedirs(RESTLER_TEMP_DIR)
with usedir(RESTLER_TEMP_DIR):
subprocess.run(f'dotnet {restler_dll_path} compile --api_spec {api_spec_path}', shell=True)
def test_spec(ip, port, host, use_ssl, restler_dll_path):
""" Runs RESTler's test mode on a specified Compile directory
@param ip: The IP of the service to test
@type ip: Str
@param port: The port of the service to test
@type port: Str
@param host: The hostname of the service to test
@type host: Str
@param use_ssl: If False, set the --no_ssl parameter when executing RESTler
@type use_ssl: Boolean
@param restler_dll_path: The absolute path to the RESTler driver's dll
@type restler_dll_path: Str
@return: None
@rtype : None
"""
with usedir(RESTLER_TEMP_DIR):
compile_dir = Path(f'Compile')
command = (
f"dotnet {restler_dll_path} test --grammar_file {compile_dir.joinpath('grammar.py')} --dictionary_file {compile_dir.joinpath('dict.json')}"
f" --settings {compile_dir.joinpath('engine_settings.json')}"
)
if not use_ssl:
command = f"{command} --no_ssl"
if ip is not None:
command = f"{command} --target_ip {ip}"
if port is not None:
command = f"{command} --target_port {port}"
if host is not None:
command = f"{command} --host {host}"
subprocess.run(command, shell=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--api_spec_path',
help='The API Swagger specification to compile and test',
type=str, required=True)
parser.add_argument('--ip',
help='The IP of the service to test',
type=str, required=False, default=None)
parser.add_argument('--port',
help='The port of the service to test',
type=str, required=False, default=None)
parser.add_argument('--restler_drop_dir',
help="The path to the RESTler drop",
type=str, required=True)
parser.add_argument('--use_ssl',
help='Set this flag if you want to use SSL validation for the socket',
action='store_true')
parser.add_argument('--host',
help='The hostname of the service to test',
type=str, required=False, default=None)
args = parser.parse_args()
api_spec_path = os.path.abspath(args.api_spec_path)
restler_dll_path = Path(os.path.abspath(args.restler_drop_dir)).joinpath('restler', 'Restler.dll')
compile_spec(api_spec_path, restler_dll_path.absolute())
test_spec(args.ip, args.port, args.host, args.use_ssl, restler_dll_path.absolute())
print(f"Test complete.\nSee {os.path.abspath(RESTLER_TEMP_DIR)} for results.")