run-subset-fuzzer-tests.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import sys, os, subprocess, tempfile, shutil
  3. def cmd (command):
  4. # https://stackoverflow.com/a/4408409 as we might have huge output sometimes
  5. with tempfile.TemporaryFile () as tempf:
  6. p = subprocess.Popen (command, stderr=tempf)
  7. try:
  8. p.wait ()
  9. tempf.seek (0)
  10. text = tempf.read ()
  11. #TODO: Detect debug mode with a better way
  12. is_debug_mode = b"SANITIZE" in text
  13. return ("" if is_debug_mode else text.decode ("utf-8").strip ()), p.returncode
  14. except subprocess.TimeoutExpired:
  15. return 'error: timeout, ' + ' '.join (command), 1
  16. srcdir = os.getenv ("srcdir", ".")
  17. EXEEXT = os.getenv ("EXEEXT", "")
  18. top_builddir = os.getenv ("top_builddir", ".")
  19. hb_subset_fuzzer = os.path.join (top_builddir, "hb-subset-fuzzer" + EXEEXT)
  20. if not os.path.exists (hb_subset_fuzzer):
  21. if len (sys.argv) < 2 or not os.path.exists (sys.argv[1]):
  22. sys.exit ("""Failed to find hb-subset-fuzzer binary automatically,
  23. please provide it as the first argument to the tool""")
  24. hb_subset_fuzzer = sys.argv[1]
  25. print ('hb_subset_fuzzer:', hb_subset_fuzzer)
  26. fails = 0
  27. valgrind = None
  28. if os.getenv ('RUN_VALGRIND', ''):
  29. valgrind = shutil.which ('valgrind')
  30. if valgrind is None:
  31. sys.exit ("""Valgrind requested but not found.""")
  32. def run_dir (parent_path):
  33. global fails
  34. for file in os.listdir (parent_path):
  35. path = os.path.join(parent_path, file)
  36. # TODO: Run on all the fonts not just subset related ones
  37. if "subset" not in path: continue
  38. print ("running subset fuzzer against %s" % path)
  39. if valgrind:
  40. text, returncode = cmd ([valgrind, '--leak-check=full', '--error-exitcode=1', hb_subset_fuzzer, path])
  41. else:
  42. text, returncode = cmd ([hb_subset_fuzzer, path])
  43. if 'error' in text:
  44. returncode = 1
  45. if (not valgrind or returncode) and text.strip ():
  46. print (text)
  47. if returncode != 0:
  48. print ("failed for %s" % path)
  49. fails = fails + 1
  50. run_dir (os.path.join (srcdir, "..", "subset", "data", "fonts"))
  51. run_dir (os.path.join (srcdir, "fonts"))
  52. if fails:
  53. sys.exit ("%d subset fuzzer related tests failed." % fails)