Simon Butcher | ef50c0d | 2016-01-26 22:15:11 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # This script splits the data test files containing the test cases into |
| 4 | # individual files (one test case per file) suitable for use with afl |
| 5 | # (American Fuzzy Lop). http://lcamtuf.coredump.cx/afl/ |
| 6 | # |
| 7 | # Usage: generate-afl-tests.sh <test data file path> |
| 8 | # <test data file path> - should be the path to one of the test suite files |
| 9 | # such as 'test_suite_mpi.data' |
| 10 | |
| 11 | # Abort on errors |
| 12 | set -e |
| 13 | |
| 14 | if [ -z $1 ] |
| 15 | then |
| 16 | echo " [!] No test file specified" >&2 |
| 17 | echo "Usage: $0 <test data file>" >&2 |
| 18 | exit 1 |
| 19 | fi |
| 20 | |
| 21 | SRC_FILEPATH=$(dirname $1)/$(basename $1) |
| 22 | TESTSUITE=$(basename $1 .data) |
| 23 | |
| 24 | THIS_DIR=$(basename $PWD) |
| 25 | |
| 26 | if [ -d ../library -a -d ../include -a -d ../tests -a $THIS_DIR == "tests" ]; |
| 27 | then :; |
| 28 | else |
| 29 | echo " [!] Must be run from mbed TLS tests directory" >&2 |
| 30 | exit 1 |
| 31 | fi |
| 32 | |
| 33 | DEST_TESTCASE_DIR=$TESTSUITE-afl-tests |
| 34 | DEST_OUTPUT_DIR=$TESTSUITE-afl-out |
| 35 | |
| 36 | echo " [+] Creating output directories" >&2 |
| 37 | |
| 38 | if [ -e $DEST_OUTPUT_DIR/* ]; |
| 39 | then : |
| 40 | echo " [!] Test output files already exist." >&2 |
| 41 | exit 1 |
| 42 | else |
| 43 | mkdir -p $DEST_OUTPUT_DIR |
| 44 | fi |
| 45 | |
| 46 | if [ -e $DEST_TESTCASE_DIR/* ]; |
| 47 | then : |
| 48 | echo " [!] Test output files already exist." >&2 |
| 49 | else |
| 50 | mkdir -p $DEST_TESTCASE_DIR |
| 51 | fi |
| 52 | |
| 53 | echo " [+] Creating test cases" >&2 |
| 54 | cd $DEST_TESTCASE_DIR |
| 55 | |
| 56 | split -p '^\s*$' ../$SRC_FILEPATH |
| 57 | |
| 58 | for f in *; |
| 59 | do |
| 60 | # Strip out any blank lines (no trim on OS X) |
| 61 | sed '/^\s*$/d' $f >testcase_$f |
| 62 | rm $f |
| 63 | done |
| 64 | |
| 65 | cd .. |
| 66 | |
| 67 | echo " [+] Test cases in $DEST_TESTCASE_DIR" >&2 |
| 68 | |