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