3 from __future__ import print_function, division, absolute_import
5 import sys, os, subprocess, tempfile, threading
9 # https://stackoverflow.com/a/377028
11 return os.path.isfile (fpath) and os.access (fpath, os.X_OK)
13 fpath, _ = os.path.split (program)
18 for path in os.environ["PATH"].split (os.pathsep):
19 exe_file = os.path.join (path, program)
27 # https://stackoverflow.com/a/4408409
28 # https://stackoverflow.com/a/10012262
29 with tempfile.TemporaryFile () as tempf:
30 p = subprocess.Popen (command, stderr=tempf)
31 is_killed = {'value': False}
33 def timeout (p, is_killed):
34 is_killed['value'] = True
36 timeout_seconds = int (os.environ.get ("HB_TEST_SHAPE_FUZZER_TIMEOUT", "2"))
37 timer = threading.Timer (timeout_seconds, timeout, [p, is_killed])
45 #TODO: Detect debug mode with a better way
46 is_debug_mode = b"SANITIZE" in text
48 text = "" if is_debug_mode else text.decode ("utf-8").strip ()
49 returncode = p.returncode
53 if is_killed['value']:
54 text = 'error: timeout, ' + text
57 return text, returncode
60 srcdir = os.environ.get ("srcdir", ".")
61 EXEEXT = os.environ.get ("EXEEXT", "")
62 top_builddir = os.environ.get ("top_builddir", ".")
63 hb_shape_fuzzer = os.path.join (top_builddir, "hb-shape-fuzzer" + EXEEXT)
65 if not os.path.exists (hb_shape_fuzzer):
66 if len (sys.argv) == 1 or not os.path.exists (sys.argv[1]):
67 print ("""Failed to find hb-shape-fuzzer binary automatically,
68 please provide it as the first argument to the tool""")
71 hb_shape_fuzzer = sys.argv[1]
73 print ('hb_shape_fuzzer:', hb_shape_fuzzer)
76 libtool = os.environ.get ('LIBTOOL')
78 if os.environ.get ('RUN_VALGRIND', ''):
79 valgrind = which ('valgrind')
81 print ("""Valgrind requested but not found.""")
84 print ("""Valgrind support is currently autotools only and needs libtool but not found.""")
87 parent_path = os.path.join (srcdir, "fonts")
88 for file in os.listdir (parent_path):
89 path = os.path.join (parent_path, file)
92 text, returncode = cmd (libtool.split(' ') + ['--mode=execute', valgrind + ' --leak-check=full --error-exitcode=1', '--', hb_shape_fuzzer, path])
94 text, returncode = cmd ([hb_shape_fuzzer, path])
98 if (not valgrind or returncode) and text.strip ():
102 print ('failure on %s' % file)
107 print ("%i shape fuzzer related tests failed." % fails)