run-shape-fuzzer-tests.py 2.3 KB
Newer Older
1 2
#!/usr/bin/env python

3 4
from __future__ import print_function, division, absolute_import

5 6 7
import sys, os, subprocess, tempfile, threading


8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
def which(program):
	# https://stackoverflow.com/a/377028
	def is_exe(fpath):
		return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

	fpath, _ = os.path.split(program)
	if fpath:
		if is_exe(program):
			return program
	else:
		for path in os.environ["PATH"].split(os.pathsep):
			exe_file = os.path.join(path, program)
			if is_exe(exe_file):
				return exe_file

	return None


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
def cmd(command):
	# https://stackoverflow.com/a/4408409
	# https://stackoverflow.com/a/10012262
	with tempfile.TemporaryFile() as tempf:
		p = subprocess.Popen (command, stderr=tempf)
		is_killed = {'value': False}

		def timeout(p, is_killed):
			is_killed['value'] = True
			p.kill()
		timer = threading.Timer (2, timeout, [p, is_killed])

		try:
			timer.start()
			p.wait ()
			tempf.seek (0)
			text = tempf.read().decode ("utf-8").strip ()
			returncode = p.returncode
		finally:
			timer.cancel()

		if is_killed['value']:
			text = 'error: timeout, ' + text
			returncode = 1

		return text, returncode

53 54 55 56

srcdir = os.environ.get ("srcdir", ".")
EXEEXT = os.environ.get ("EXEEXT", "")
top_builddir = os.environ.get ("top_builddir", ".")
57
hb_shape_fuzzer = os.path.join (top_builddir, "hb-shape-fuzzer" + EXEEXT)
58

59
if not os.path.exists (hb_shape_fuzzer):
60
	if len (sys.argv) == 1 or not os.path.exists (sys.argv[1]):
61
		print ("""Failed to find hb-shape-fuzzer binary automatically,
62 63 64
please provide it as the first argument to the tool""")
		sys.exit (1)

65
	hb_shape_fuzzer = sys.argv[1]
66

67
print ('hb_shape_fuzzer:', hb_shape_fuzzer)
68 69
fails = 0

70 71 72
valgrind = None
if os.environ.get('RUN_VALGRIND', ''):
	valgrind = which ('valgrind')
73

74 75
parent_path = os.path.join (srcdir, "fonts")
for file in os.listdir (parent_path):
B
Behdad Esfahbod 已提交
76
	path = os.path.join(parent_path, file)
77

78
	text, returncode = cmd ([hb_shape_fuzzer, path])
79 80
	if text.strip ():
		print (text)
81

82
	failed = False
83 84
	if returncode != 0 or 'error' in text:
		print ('failure on %s' % file)
85 86 87 88 89 90 91 92 93 94
		failed = True

	if valgrind:
		text, returncode = cmd ([valgrind, '--error-exitcode=1', hb_shape_fuzzer, path])
		if returncode:
			print (text)
			print ('failure on %s' % file)
			failed = True

	if failed:
95 96 97
		fails = fails + 1

if fails:
98
	print ("%i shape fuzzer related tests failed." % fails)
99
	sys.exit (1)