Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | |
| 7 | ''' |
| 8 | Please read .../tf_fuzz/regression/README to understand this code. |
| 9 | |
| 10 | Please also read the comments in .../tf_fuzz/regression/regress_lib/line_by_line.py. |
| 11 | ''' |
| 12 | |
| 13 | import sys, os, re, string, pdb |
| 14 | sys.path.append ("../regress_lib") |
| 15 | import line_by_line |
| 16 | |
| 17 | |
| 18 | # Describe script usage: |
| 19 | def usage(): |
| 20 | print (''' |
| 21 | Command-line parameters: |
| 22 | 1. The test-template file, nominally named "template", |
| 23 | 2. The expected combined stdout/stderr output, nominally named "exp_stdout_stderr", |
| 24 | 3. The actual/generated combined stdout/stderr output, nominally named "stdout_stderr", |
| 25 | 4. The expected .c test file, nominally named "exp_test.c", and |
| 26 | 5. The actual, generated .c test file, nominally named "test.c". |
| 27 | |
| 28 | Optionally, *before* these five arguments, you may add switches thus: |
| 29 | "--v" for verbose mode, to view line-by-line comparisons actual vs. expected, |
| 30 | "--q" to only print error messages and a successful-completion message, |
| 31 | "--qq" same as --q but not even printing out the completion message, and |
| 32 | "--s 12345" to run TF-Fuzz with seed value 12345 (or whatever). |
| 33 | ''') |
| 34 | |
| 35 | |
| 36 | def main(): |
| 37 | # See if we're supposed to be quiet: |
| 38 | loud = quiet = ultra_quiet = False |
| 39 | seed = "" |
| 40 | while sys.argv[1][0] == "-": |
| 41 | if sys.argv[1] == "--v": |
| 42 | loud = True |
| 43 | sys.argv.pop(1) |
| 44 | elif sys.argv[1] == "--q": |
| 45 | quiet = True |
| 46 | sys.argv.pop(1) |
| 47 | elif sys.argv[1] == "--qq": |
| 48 | quiet = ultra_quiet = True |
| 49 | sys.argv.pop(1) |
| 50 | elif sys.argv[1] == "--s": |
| 51 | sys.argv.pop(1) |
| 52 | seed = sys.argv[1] |
| 53 | sys.argv.pop(1) |
| 54 | if not seed.isnumeric(): |
| 55 | print ('The --s seed argument was not a number.') |
| 56 | usage() |
| 57 | sys.exit(1) |
| 58 | |
| 59 | # Run TF-Fuzz: |
| 60 | if not quiet: print ("Running TF-Fuzz... ") |
| 61 | os.system ('rm -f stdout_stderr test.c') |
| 62 | command = '../../tfz -v ./' + sys.argv[1] + ' ./' + sys.argv[5] |
| 63 | command += ' ' + seed + ' >' + sys.argv[3] + ' 2>&1' |
| 64 | if loud: |
| 65 | print (command) |
| 66 | if os.system (command) == 0: |
| 67 | if not quiet: print ("TF-Fuzz run complete.") |
| 68 | else: |
| 69 | print ('Could not run TF-Fuzz; please see stdout_stderr file.') |
| 70 | sys.exit(2) |
| 71 | |
| 72 | # Attempt to open files indicated on command line: |
| 73 | if len(sys.argv) != 6: |
| 74 | message = '{} requires 5 command-line arguments. Exiting.' |
| 75 | print (message.format(sys.argv[0]), file=sys.stderr) |
| 76 | usage() |
| 77 | sys.exit(3) |
| 78 | template_file_name = sys.argv[1] |
| 79 | exp_stdout_file_name = sys.argv[2] |
| 80 | act_stdout_file_name = sys.argv[3] |
| 81 | exp_test_file_name = sys.argv[4] |
| 82 | act_test_file_name = sys.argv[5] |
| 83 | |
| 84 | try: |
| 85 | template_file = open (template_file_name, 'rt') |
| 86 | exp_stdout_file = open (exp_stdout_file_name, 'rt') |
| 87 | act_stdout_file = open (act_stdout_file_name, 'rt') |
| 88 | exp_test_file = open (exp_test_file_name, 'rt') |
| 89 | act_test_file = open (act_test_file_name, 'rt') |
| 90 | except FileNotFoundError: |
| 91 | print ('One or more files could not be found.') |
| 92 | usage(); |
| 93 | sys.exit(4) |
| 94 | except: |
| 95 | print ('Something went wrong trying to open the input files.') |
| 96 | usage(); |
| 97 | sys.exit(5) |
| 98 | else: |
| 99 | message = '\nInput files:\n {},\n {},\n {},\n' |
| 100 | message += ' {}, and\n {}\nopened successfully.\n' |
| 101 | if not quiet: |
| 102 | print (message.format (template_file_name, exp_stdout_file_name, |
| 103 | act_stdout_file_name, exp_test_file_name, act_test_file_name)) |
| 104 | |
| 105 | # Check it all: |
| 106 | if not quiet: print ("\nChecking test C file: ", end="") |
| 107 | line_by_line.check_file ( exp_test_file, exp_test_file_name, |
| 108 | act_test_file, act_test_file_name, |
| 109 | loud, quiet, ultra_quiet ) |
| 110 | if not quiet: print ("Checking stdout and stderr: ", end="") |
| 111 | line_by_line.check_file ( exp_stdout_file, exp_stdout_file_name, |
| 112 | act_stdout_file, act_stdout_file_name, |
| 113 | loud, quiet, ultra_quiet ) |
| 114 | |
| 115 | # Ran to completion normally, so pass: |
| 116 | if not ultra_quiet: print ("Regression test passed.") |
| 117 | sys.exit(0) |
| 118 | |
| 119 | if __name__ == "__main__": main() |