Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / test / fuzzing / run-subset-fuzzer-tests.py
1 #!/usr/bin/env python3
2
3 import sys, os, subprocess, tempfile, shutil
4
5
6 def cmd (command):
7         # https://stackoverflow.com/a/4408409 as we might have huge output sometimes
8         with tempfile.TemporaryFile () as tempf:
9                 p = subprocess.Popen (command, stderr=tempf)
10
11                 try:
12                         p.wait (timeout=int (os.environ.get ("HB_TEST_SUBSET_FUZZER_TIMEOUT", "12")))
13                         tempf.seek (0)
14                         text = tempf.read ()
15
16                         #TODO: Detect debug mode with a better way
17                         is_debug_mode = b"SANITIZE" in text
18
19                         return ("" if is_debug_mode else text.decode ("utf-8").strip ()), p.returncode
20                 except subprocess.TimeoutExpired:
21                         return 'error: timeout, ' + ' '.join (command), 1
22
23
24 srcdir = os.environ.get ("srcdir", ".")
25 EXEEXT = os.environ.get ("EXEEXT", "")
26 top_builddir = os.environ.get ("top_builddir", ".")
27 hb_subset_fuzzer = os.path.join (top_builddir, "hb-subset-fuzzer" + EXEEXT)
28
29 if not os.path.exists (hb_subset_fuzzer):
30         if len (sys.argv) < 2 or not os.path.exists (sys.argv[1]):
31                 sys.exit ("""Failed to find hb-subset-fuzzer binary automatically,
32 please provide it as the first argument to the tool""")
33
34         hb_subset_fuzzer = sys.argv[1]
35
36 print ('hb_subset_fuzzer:', hb_subset_fuzzer)
37 fails = 0
38
39 libtool = os.environ.get('LIBTOOL')
40 valgrind = None
41 if os.environ.get('RUN_VALGRIND', ''):
42         valgrind = shutil.which ('valgrind')
43         if valgrind is None:
44                 sys.exit ("""Valgrind requested but not found.""")
45         if libtool is None:
46                 print ("""Valgrind support is currently autotools only and needs libtool but not found.""")
47
48
49 def run_dir (parent_path):
50         global fails
51         for file in os.listdir (parent_path):
52                 path = os.path.join(parent_path, file)
53                 # TODO: Run on all the fonts not just subset related ones
54                 if "subset" not in path: continue
55
56                 print ("running subset fuzzer against %s" % path)
57                 if valgrind:
58                         text, returncode = cmd (libtool.split(' ') + ['--mode=execute', valgrind + ' --leak-check=full --show-leak-kinds=all --error-exitcode=1', '--', hb_subset_fuzzer, path])
59                 else:
60                         text, returncode = cmd ([hb_subset_fuzzer, path])
61                         if 'error' in text:
62                                 returncode = 1
63
64                 if (not valgrind or returncode) and text.strip ():
65                         print (text)
66
67                 if returncode != 0:
68                         print ("failed for %s" % path)
69                         fails = fails + 1
70
71
72 run_dir (os.path.join (srcdir, "..", "subset", "data", "fonts"))
73 run_dir (os.path.join (srcdir, "fonts"))
74
75 if fails:
76         sys.exit ("%d subset fuzzer related tests failed." % fails)