Tom Rini | 0344c60 | 2024-10-08 13:56:50 -0600 | [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_rsa.data' |
| 10 | # |
| 11 | # Copyright The Mbed TLS Contributors |
| 12 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 13 | |
| 14 | # Abort on errors |
| 15 | set -e |
| 16 | |
| 17 | if [ -z $1 ] |
| 18 | then |
| 19 | echo " [!] No test file specified" >&2 |
| 20 | echo "Usage: $0 <test data file>" >&2 |
| 21 | exit 1 |
| 22 | fi |
| 23 | |
| 24 | SRC_FILEPATH=$(dirname $1)/$(basename $1) |
| 25 | TESTSUITE=$(basename $1 .data) |
| 26 | |
| 27 | THIS_DIR=$(basename $PWD) |
| 28 | |
| 29 | if [ -d ../library -a -d ../include -a -d ../tests -a $THIS_DIR == "tests" ]; |
| 30 | then :; |
| 31 | else |
| 32 | echo " [!] Must be run from Mbed TLS tests directory" >&2 |
| 33 | exit 1 |
| 34 | fi |
| 35 | |
| 36 | DEST_TESTCASE_DIR=$TESTSUITE-afl-tests |
| 37 | DEST_OUTPUT_DIR=$TESTSUITE-afl-out |
| 38 | |
| 39 | echo " [+] Creating output directories" >&2 |
| 40 | |
| 41 | if [ -e $DEST_OUTPUT_DIR/* ]; |
| 42 | then : |
| 43 | echo " [!] Test output files already exist." >&2 |
| 44 | exit 1 |
| 45 | else |
| 46 | mkdir -p $DEST_OUTPUT_DIR |
| 47 | fi |
| 48 | |
| 49 | if [ -e $DEST_TESTCASE_DIR/* ]; |
| 50 | then : |
| 51 | echo " [!] Test output files already exist." >&2 |
| 52 | else |
| 53 | mkdir -p $DEST_TESTCASE_DIR |
| 54 | fi |
| 55 | |
| 56 | echo " [+] Creating test cases" >&2 |
| 57 | cd $DEST_TESTCASE_DIR |
| 58 | |
| 59 | split -p '^\s*$' ../$SRC_FILEPATH |
| 60 | |
| 61 | for f in *; |
| 62 | do |
| 63 | # Strip out any blank lines (no trim on OS X) |
| 64 | sed '/^\s*$/d' $f >testcase_$f |
| 65 | rm $f |
| 66 | done |
| 67 | |
| 68 | cd .. |
| 69 | |
| 70 | echo " [+] Test cases in $DEST_TESTCASE_DIR" >&2 |
| 71 | |