-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtest
executable file
·364 lines (319 loc) · 9.48 KB
/
runtest
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python3
import sys
import codecs
import glob
import tempfile
import pathlib
import subprocess
import atexit
import argparse
def reconfigure(stream):
return codecs.getwriter(stream.encoding)(stream.detach(), errors='surrogateescape')
sys.stdout = reconfigure(sys.stdout)
sys.stderr = reconfigure(sys.stderr)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('testfile', metavar='TEST_FILE', type=pathlib.Path)
parser.add_argument('-u', '--update', action='store_const', const=True)
parser.add_argument('-v', '--verbose', action='store_const', const=True)
parser.add_argument('-k', '--keep', action='store_const', const=True)
parser.add_argument('--reference', action='store_const', const=True)
parser.add_argument('--no-setrlimit', action='store_const', const=True)
return parser.parse_args()
args = parse_args()
testfile = args.testfile.open('rt')
in_source = []
in_data = bytearray()
in_data_at = 0
in_choices = []
expect_msg = []
expect_data = in_data
expect_diswarn = False
expect_exit = 0
test_dis = not args.reference
test_skipout = False
mode = 'source'
for line in testfile:
if line.startswith(';;;#'):
continue
if line.startswith(';;;;'):
line = line[len(';;;;'):].strip()
sect, _, param = line.partition(' ')
if sect == 'source':
mode = 'source'
elif sect == 'output':
mode = 'output'
elif sect == 'data':
mode = 'data'
elif sect == 'data-at':
in_data_at = int(param.strip(), 0)
elif sect == 'target':
mode = 'target'
if expect_data is in_data:
expect_data = bytearray()
elif sect == 'comment':
mode = 'comment'
elif sect == 'choose':
in_choices.append(int(param.strip(), 0))
elif sect == 'diswarn':
if param == 'ignore':
expect_diswarn = None
else:
expect_diswarn = True
elif sect == 'skipoutref':
test_skipout = args.reference
elif sect == 'exit':
param = param.strip()
if param == 'fatal':
expect_exit = None
else:
expect_exit = int(param.strip(), 0)
else:
print("=== Invalid section: {} ===".format(repr(line)))
sys.exit(1)
continue
if mode == 'comment':
continue
elif mode == 'source':
in_source.append(line)
elif mode == 'output':
expect_msg.append(line)
elif mode == 'data':
in_data += bytes.fromhex(line.strip())
elif mode == 'target':
expect_data += bytes.fromhex(line.strip())
else:
print("=== No mode ===")
sys.exit(1)
if expect_msg is not None:
expect_msg = ''.join(expect_msg)
in_source = ''.join(in_source).encode('utf-8')
fname_tmpdir = pathlib.Path(tempfile.mkdtemp(prefix='bsptest-'))
fname_bsp0 = fname_tmpdir / 'bsp0'
fname_input = fname_tmpdir / 'input'
fname_target = fname_tmpdir / 'output'
fname_dis = fname_tmpdir / 'dis'
fname_bsp1 = fname_tmpdir / 'bsp1'
discrepancy = False
# compile
if args.verbose:
print("=== Compiling ===")
compiler = subprocess.Popen(
['bspcomp/bspcomp', '/dev/stdin', str(fname_bsp0)],
stdin=subprocess.PIPE
)
compiler.stdin.write(in_source)
compiler.stdin.close()
if compiler.wait(timeout=10) != 0:
print("=== Compilation error ===")
sys.exit(1)
if test_dis:
if args.verbose:
print("=== Disassembling ===")
disassembler = subprocess.Popen(
['./bspdis', '-n', '-o', str(fname_dis), str(fname_bsp0)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
(_, dis_err) = disassembler.communicate(timeout=10)
if dis_err != b'':
if expect_diswarn:
if args.verbose:
print("=== Expected disassembler warnings emitted ===")
elif expect_diswarn is None:
if args.verbose:
print("=== Disassembler warnings (ignored) ===")
else:
print("=== Unexpected disassembler warnings ===")
discrepancy = True
if args.verbose or discrepancy:
print(dis_err.decode('utf-8', 'surrogateescape'))
elif expect_diswarn:
print("=== Expected disassembler warnings not emitted ===")
discrepancy = True
if args.verbose:
print("=== Disassembly ===")
with fname_dis.open('r', errors='surrogateescape') as dis:
print(dis.read())
print("=== Compiling again ===")
compiler = subprocess.Popen(
['bspcomp/bspcomp', str(fname_dis), str(fname_bsp1)]
)
if compiler.wait(timeout=10) != 0:
print("=== Second-pass compilation error ===")
discrepancy = True
else:
if args.verbose:
print("=== Verifying ===")
with fname_bsp0.open('rb') as bsp0:
with fname_bsp1.open('rb') as bsp1:
data0 = bsp0.read()
data1 = bsp1.read()
if data0 != data1:
print("=== Disassembly verification failed ===")
print("first : {}".format(data0))
print("second: {}".format(data1))
discrepancy = True
# prepare input
with fname_input.open('wb') as f:
f.seek(in_data_at)
if in_data:
f.write(in_data)
else:
f.truncate()
if args.verbose:
print("=== Running ===")
import resource
def set_limits():
as_limit = 1024 * 1024 * 1024
resource.setrlimit(resource.RLIMIT_AS, (as_limit, as_limit))
if args.reference:
argv = ['node', 'bspcomp/patcher.js', str(fname_bsp0), str(fname_input), str(fname_target)]
else:
argv = ['./bsp', '-N16', '-I8192', '-dttk' if args.verbose else '-dk', str(fname_bsp0), str(fname_input), str(fname_target)]
interpreter = subprocess.Popen(argv,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
preexec_fn=None if args.no_setrlimit else set_limits
)
with interpreter:
try:
(actual_msg, actual_err) = interpreter.communicate(
input=(''.join('{}\n'.format(ch) for ch in in_choices) + 'q\n').encode('ascii'),
timeout=10)
except subprocess.TimeoutExpired as e:
interpreter.kill()
(actual_msg, actual_err) = interpreter.communicate()
actual_err = actual_err.decode('utf-8', 'surrogateescape')
actual_msg = actual_msg.decode('utf-8')
if actual_msg and not actual_msg.endswith('\n'):
actual_msg += '\n'
actual_exit = None
else:
actual_err = actual_err.decode('utf-8', 'surrogateescape')
actual_msg = actual_msg.decode('utf-8')
if actual_msg and not actual_msg.endswith('\n'):
actual_msg += '\n'
if args.reference:
if interpreter.returncode != 0:
actual_msg_lines = actual_msg.rstrip('\n').split('\n')
last_line = actual_msg_lines.pop()
if last_line.startswith('Error: '):
actual_exit = None
actual_msg = '\n'.join(actual_msg_lines)
elif last_line.startswith('Patch exited with exit status '):
actual_exit = int(last_line[len('Patch exited with exit status '):])
actual_msg = '\n'.join(actual_msg_lines)
else:
actual_exit = None
else:
actual_exit = 0
else:
if interpreter.returncode == 255:
actual_exit = None
elif interpreter.returncode == 254:
for line in reversed(actual_err.splitlines()):
left, sep, line = line.partition(': ')
if sep != ': ':
continue
left, sep, line = line.partition(': ')
if sep != ': ':
continue
if line.startswith('patching failed, exit code '):
actual_exit = int(line[len('patching failed, exit code '):])
break
else:
raise ValueError(actual_err)
else:
actual_exit = interpreter.returncode
try:
with fname_target.open('rb') as f:
f.seek(in_data_at)
actual_data = f.read()
except FileNotFoundError:
actual_data = None
def rm_rf(node):
if node.is_dir():
for child in node.iterdir():
rm_rf(child)
node.rmdir()
else:
node.unlink()
if args.keep:
print("=== Files kept in {} ===".format(fname_tmpdir))
else:
rm_rf(fname_tmpdir)
if expect_exit != actual_exit:
discrepancy = True
if not test_skipout and expect_msg != actual_msg:
discrepancy = True
if actual_data is not None and expect_data != actual_data:
discrepancy = True
if discrepancy or args.verbose:
print("=== Standard error ===")
print(actual_err)
if expect_msg is not None:
print("=== Expected output ===")
print(expect_msg)
else:
print("=== No expected output ===")
if expect_msg != actual_msg:
print("=== Actual output ===")
print(actual_msg)
print("=== Expected final data ===")
print(expect_data.hex())
if actual_data is not None:
if expect_data != actual_data:
print("=== Actual final data ===")
print(actual_data.hex())
else:
print("=== Actual final data matches ===")
else:
print("=== No actual final data ===")
if expect_exit is not None:
print("=== Expected exit code: {} ===".format(expect_exit))
else:
print("=== Expected a fatal error ===")
if expect_exit != actual_exit:
if actual_exit is not None:
print("=== Actual exit code: {} ===".format(actual_exit))
else:
if actual_exit is not None:
print("=== Actual exit code matches ===")
else:
print("=== Expected fatal error raised ===")
_OUTPUT_SECTIONS = ('output', 'target', 'exit')
if discrepancy:
if args.update:
import os
newname = args.testfile.with_suffix('.newtest')
emitted = set()
def emit(sect):
if sect in emitted:
return
emitted.add(sect)
if sect == 'output':
if actual_msg:
newfile.write(';;;; output\n')
newfile.write(actual_msg)
elif sect == 'target':
if actual_data is not None and actual_data != in_data:
newfile.write(';;;; target\n')
newfile.write(actual_data.hex() + '\n')
elif sect == 'exit':
if actual_exit is None:
newfile.write(';;;; exit fatal\n')
elif actual_exit:
newfile.write(';;;; exit %u\n' % (actual_exit))
with args.testfile.open('rt') as testfile, newname.open('wt') as newfile:
skip = False
for line in testfile:
if line.startswith(';;;;'):
sect, _, param = line[len(';;;;'):].strip().partition(' ')
skip = sect in _OUTPUT_SECTIONS
if skip:
emit(sect)
if not skip:
newfile.write(line)
for sect in _OUTPUT_SECTIONS:
emit(sect)
newname.replace(args.testfile)
sys.exit(1)