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