|
14 | 14 | import shutil
|
15 | 15 | import glob
|
16 | 16 | from pathlib import Path
|
| 17 | +from threading import Thread |
17 | 18 |
|
18 | 19 | RESTLER_WORKING_DIR = 'restler_working_dir'
|
19 | 20 |
|
20 | 21 | class QuickStartFailedException(Exception):
|
21 | 22 | pass
|
22 | 23 |
|
| 24 | +def check_output_errors(output): |
| 25 | + if output.stderr: |
| 26 | + raise QuickStartFailedException(f"Failing because stderr was detected after running restler-quick-start:\n{output.stderr!s}") |
| 27 | + try: |
| 28 | + output.check_returncode() |
| 29 | + except subprocess.CalledProcessError: |
| 30 | + raise QuickStartFailedException(f"Failing because restler-quick-start exited with a non-zero return code: {output.returncode!s}") |
| 31 | + |
| 32 | +def check_expected_output(restler_working_dir, expected_strings, output, task_dir_name): |
| 33 | + stdout = str(output.stdout) |
| 34 | + |
| 35 | + for expected_str in expected_strings: |
| 36 | + if expected_str not in stdout: |
| 37 | + stdout = stdout.replace('\\r\\n', '\r\n') |
| 38 | + |
| 39 | + # Print the engine logs to the console |
| 40 | + out_file_path = os.path.join(restler_working_dir, task_dir_name, 'EngineStdOut.txt') |
| 41 | + err_file_path = os.path.join(restler_working_dir, task_dir_name, 'EngineStdErr.txt') |
| 42 | + results_dir = os.path.join(restler_working_dir, task_dir_name, 'RestlerResults') |
| 43 | + # Return the newest experiments directory in RestlerResults |
| 44 | + net_log_dir = max(glob.glob(os.path.join(results_dir, 'experiment*/')), key=os.path.getmtime) |
| 45 | + net_log_path = glob.glob(os.path.join(net_log_dir, 'logs', f'network.testing.*.1.txt'))[0] |
| 46 | + with open(out_file_path) as of, open(err_file_path) as ef, open(net_log_path) as nf: |
| 47 | + out = of.read() |
| 48 | + err = ef.read() |
| 49 | + net_log = nf.read() |
| 50 | + raise QuickStartFailedException(f"Failing because expected output '{expected_str}' was not found:\n{stdout}{out}{err}{net_log}") |
| 51 | + |
| 52 | +def test_test_task(restler_working_dir, swagger_path, restler_drop_dir): |
| 53 | + # Run the quick start script |
| 54 | + output = subprocess.run( |
| 55 | + f'python ./restler-quick-start.py --api_spec_path {swagger_path} --restler_drop_dir {restler_drop_dir} --task test', |
| 56 | + shell=True, capture_output=True |
| 57 | + ) |
| 58 | + expected_strings = [ |
| 59 | + 'Request coverage (successful / total): 6 / 6', |
| 60 | + 'No bugs were found.' , |
| 61 | + 'Task Test succeeded.' |
| 62 | + ] |
| 63 | + check_output_errors(output) |
| 64 | + check_expected_output(restler_working_dir, expected_strings, output, "Test") |
| 65 | + |
| 66 | +def test_fuzzlean_task(restler_working_dir, swagger_path, restler_drop_dir): |
| 67 | + # Run the quick start script |
| 68 | + output = subprocess.run( |
| 69 | + f'python ./restler-quick-start.py --api_spec_path {swagger_path} --restler_drop_dir {restler_drop_dir} --task fuzz-lean', |
| 70 | + shell=True, capture_output=True |
| 71 | + ) |
| 72 | + expected_strings = [ |
| 73 | + 'Request coverage (successful / total): 6 / 6', |
| 74 | + 'Bugs were found!' , |
| 75 | + 'InvalidDynamicObjectChecker_20x: 2', |
| 76 | + 'InvalidDynamicObjectChecker_500: 1', |
| 77 | + 'PayloadBodyChecker_500: 1', |
| 78 | + 'Task FuzzLean succeeded.' |
| 79 | + ] |
| 80 | + check_output_errors(output) |
| 81 | + check_expected_output(restler_working_dir, expected_strings, output, "FuzzLean") |
| 82 | + |
| 83 | +def test_fuzz_task(restler_working_dir, swagger_path, restler_drop_dir): |
| 84 | + import json |
| 85 | + compile_dir = Path(restler_working_dir, f'Compile') |
| 86 | + settings_file_path = compile_dir.joinpath('engine_settings.json') |
| 87 | + # Set the maximum number of generations (i.e. sequence length) to limit fuzzing |
| 88 | + settings_json=json.load(open(settings_file_path)) |
| 89 | + settings_json["max_sequence_length"] = 5 |
| 90 | + json.dump(settings_json, open(settings_file_path, "w", encoding='utf-8')) |
| 91 | + |
| 92 | + expected_strings = [ |
| 93 | + 'Request coverage (successful / total): 6 / 6', |
| 94 | + 'Bugs were found!' , |
| 95 | + 'InvalidDynamicObjectChecker_20x: 2', |
| 96 | + 'InvalidDynamicObjectChecker_500: 1', |
| 97 | + 'PayloadBodyChecker_500: 1', |
| 98 | + 'Task Fuzz succeeded.' |
| 99 | + ] |
| 100 | + output = subprocess.run( |
| 101 | + f'python ./restler-quick-start.py --api_spec_path {swagger_path} --restler_drop_dir {restler_drop_dir} --task fuzz', |
| 102 | + shell=True, capture_output=True |
| 103 | + ) |
| 104 | + check_output_errors(output) |
| 105 | + # check_expected_output(restler_working_dir, expected_strings, output) |
| 106 | + |
| 107 | +def test_replay_task(restler_working_dir, task_output_dir, restler_drop_dir): |
| 108 | + # Run the quick start script |
| 109 | + print(f"Testing replay for bugs found in task output dir: {task_output_dir}") |
| 110 | + output = subprocess.run( |
| 111 | + f'python ./restler-quick-start.py --replay_bug_buckets_dir {task_output_dir} --restler_drop_dir {restler_drop_dir} --task replay', |
| 112 | + shell=True, capture_output=True |
| 113 | + ) |
| 114 | + check_output_errors(output) |
| 115 | + # Check that the Replay directory is present and that it contains a bug bucket with the |
| 116 | + # same bug. |
| 117 | + original_bug_buckets_file_path = glob.glob(os.path.join(task_output_dir, 'RestlerResults/*/bug_buckets/bug_buckets.txt'))[0] |
| 118 | + |
| 119 | + # TODO: it would be better if the replay command also produced a bug bucket, so they could be |
| 120 | + # diff'ed with the original bug buckets. |
| 121 | + # Until this is implemented, check that a 500 is found in the log. |
| 122 | + # replay_buckets = glob.glob(os.path.join(restler_working_dir, 'Replay/RestlerResults/*/bug_buckets/bug_buckets.txt')) |
| 123 | + network_log = glob.glob(os.path.join(restler_working_dir, 'Replay/RestlerResults/**/logs/network.*.txt')) |
| 124 | + if network_log: |
| 125 | + network_log = network_log[0] |
| 126 | + else: |
| 127 | + output = str(output.stdout) |
| 128 | + raise QuickStartFailedException(f"No bug buckets were found after replay. Output: {output}") |
| 129 | + |
| 130 | + with open(network_log) as rf, open(original_bug_buckets_file_path) as of: |
| 131 | + orig_buckets = of.read() |
| 132 | + log_contents = rf.read() |
| 133 | + if 'HTTP/1.1 500 INTERNAL SERVER ERROR' not in log_contents: |
| 134 | + raise QuickStartFailedException(f"Failing because bug buckets {orig_buckets} were not reproduced. Replay log: {log_contents}.") |
| 135 | + else: |
| 136 | + print("500 error was reproduced.") |
| 137 | + |
| 138 | +demo_server_output=[] |
| 139 | + |
| 140 | +def get_demo_server_output(demo_server_process): |
| 141 | + demo_server_output.clear() |
| 142 | + while True: |
| 143 | + output = demo_server_process.stdout.readline() |
| 144 | + if output: |
| 145 | + demo_server_output.append(output) |
| 146 | + else: |
| 147 | + result = demo_server_process.poll() |
| 148 | + if result is not None: |
| 149 | + break |
| 150 | + |
| 151 | + return result |
| 152 | + |
23 | 153 | if __name__ == '__main__':
|
24 | 154 | curr = os.getcwd()
|
| 155 | + |
25 | 156 | # Run demo server in background
|
| 157 | + # Note: demo_server must be started in its directory |
26 | 158 | os.chdir('demo_server')
|
27 | 159 | demo_server_path = Path('demo_server', 'app.py')
|
28 | 160 | demo_server_process = subprocess.Popen([sys.executable, demo_server_path],
|
29 | 161 | stdout=subprocess.PIPE,
|
30 | 162 | stderr=subprocess.STDOUT)
|
31 | 163 |
|
| 164 | + thread = Thread(target = get_demo_server_output, args = (demo_server_process, )) |
| 165 | + thread.start() |
| 166 | + |
32 | 167 | os.chdir(curr)
|
33 | 168 |
|
34 | 169 | swagger_path = Path('demo_server', 'swagger.json')
|
35 | 170 | # argv 1 = path to RESTler drop
|
36 | 171 | restler_drop_dir = sys.argv[1]
|
37 |
| - |
| 172 | + restler_working_dir = os.path.join(curr, RESTLER_WORKING_DIR) |
| 173 | + test_failed = False |
38 | 174 | try:
|
39 |
| - # Run the quick start script |
40 |
| - output = subprocess.run( |
41 |
| - f'python ./restler-quick-start.py --api_spec_path {swagger_path} --restler_drop_dir {restler_drop_dir}', |
42 |
| - shell=True, capture_output=True |
43 |
| - ) |
44 |
| - # Kill demo server |
45 |
| - demo_server_process.terminate() |
46 |
| - demo_server_out, _ = demo_server_process.communicate() |
47 |
| - # Check if restler-quick-start succeeded |
48 |
| - if output.stderr: |
49 |
| - raise QuickStartFailedException(f"Failing because stderr was detected after running restler-quick-start:\n{output.stderr!s}") |
50 |
| - try: |
51 |
| - output.check_returncode() |
52 |
| - except subprocess.CalledProcessError: |
53 |
| - raise QuickStartFailedException(f"Failing because restler-quick-start exited with a non-zero return code: {output.returncode!s}") |
| 175 | + print("+++++++++++++++++++++++++++++test...") |
| 176 | + test_test_task(restler_working_dir, swagger_path, restler_drop_dir) |
54 | 177 |
|
55 |
| - stdout = str(output.stdout) |
| 178 | + print("+++++++++++++++++++++++++++++fuzzlean...") |
| 179 | + test_fuzzlean_task(restler_working_dir, swagger_path, restler_drop_dir) |
56 | 180 |
|
57 |
| - if 'Request coverage (successful / total): 6 / 6' not in stdout or\ |
58 |
| - 'No bugs were found.' not in stdout or\ |
59 |
| - 'Task Test succeeded.' not in stdout: |
60 |
| - print(f"Demo server output: {demo_server_out}") |
| 181 | + print("+++++++++++++++++++++++++++++replay...") |
| 182 | + fuzzlean_task_dir = os.path.join(curr, RESTLER_WORKING_DIR, 'FuzzLean') |
| 183 | + test_replay_task(restler_working_dir, fuzzlean_task_dir, restler_drop_dir) |
61 | 184 |
|
62 |
| - stdout = stdout.replace('\\r\\n', '\r\n') |
63 |
| - # Print the engine logs to the console |
64 |
| - out_file_path = os.path.join(curr, RESTLER_WORKING_DIR, 'Test', 'EngineStdOut.txt') |
65 |
| - err_file_path = os.path.join(curr, RESTLER_WORKING_DIR, 'Test', 'EngineStdErr.txt') |
66 |
| - results_dir = os.path.join(curr, RESTLER_WORKING_DIR, 'Test', 'RestlerResults') |
67 |
| - # Return the newest experiments directory in RestlerResults |
68 |
| - net_log_dir = max(glob.glob(os.path.join(results_dir, 'experiment*/')), key=os.path.getmtime) |
69 |
| - net_log_path = glob.glob(os.path.join(net_log_dir, 'logs', f'network.testing.*.1.txt'))[0] |
70 |
| - with open(out_file_path) as of, open(err_file_path) as ef, open(net_log_path) as nf: |
71 |
| - out = of.read() |
72 |
| - err = ef.read() |
73 |
| - net_log = nf.read() |
| 185 | + #print("+++++++++++++++++++++++++++++fuzz...") |
| 186 | + #test_fuzz_task(restler_working_dir, swagger_path, restler_drop_dir) |
74 | 187 |
|
75 |
| - raise QuickStartFailedException(f"Failing because expected output was not found:\n{stdout}{out}{err}{net_log}") |
| 188 | + except Exception: |
| 189 | + test_failed = True |
| 190 | + raise |
76 | 191 | finally:
|
| 192 | + # Kill demo server |
| 193 | + demo_server_process.terminate() |
| 194 | + thread.join() |
| 195 | + demo_server_out, _ = demo_server_process.communicate() |
| 196 | + if test_failed: |
| 197 | + print(f"Demo server output: {demo_server_output} {demo_server_out}") |
| 198 | + |
77 | 199 | # Delete the working directory that was created during restler quick start
|
78 | 200 | shutil.rmtree(RESTLER_WORKING_DIR)
|
79 | 201 |
|
0 commit comments