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# Copyright The Mbed TLS Contributors
12# SPDX-License-Identifier: Apache-2.0
13#
14# Licensed under the Apache License, Version 2.0 (the "License"); you may
15# not use this file except in compliance with the License.
16# You may obtain a copy of the License at
17#
18# http://www.apache.org/licenses/LICENSE-2.0
19#
20# Unless required by applicable law or agreed to in writing, software
21# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23# See the License for the specific language governing permissions and
24# limitations under the License.
25
26# Abort on errors
27set -e
28
29if [ -z $1 ]
30then
31    echo " [!] No test file specified" >&2
32    echo "Usage: $0 <test data file>" >&2
33    exit 1
34fi
35
36SRC_FILEPATH=$(dirname $1)/$(basename $1)
37TESTSUITE=$(basename $1 .data)
38
39THIS_DIR=$(basename $PWD)
40
41if [ -d ../library -a -d ../include -a -d ../tests -a $THIS_DIR == "tests" ];
42then :;
43else
44    echo " [!] Must be run from mbed TLS tests directory" >&2
45    exit 1
46fi
47
48DEST_TESTCASE_DIR=$TESTSUITE-afl-tests
49DEST_OUTPUT_DIR=$TESTSUITE-afl-out
50
51echo " [+] Creating output directories" >&2
52
53if [ -e $DEST_OUTPUT_DIR/* ];
54then :
55    echo " [!] Test output files already exist." >&2
56    exit 1
57else
58    mkdir -p $DEST_OUTPUT_DIR
59fi
60
61if [ -e $DEST_TESTCASE_DIR/* ];
62then :
63    echo " [!] Test output files already exist." >&2
64else
65    mkdir -p $DEST_TESTCASE_DIR
66fi
67
68echo " [+] Creating test cases" >&2
69cd $DEST_TESTCASE_DIR
70
71split -p '^\s*$' ../$SRC_FILEPATH
72
73for f in *;
74do
75    # Strip out any blank lines (no trim on OS X)
76    sed '/^\s*$/d' $f >testcase_$f
77    rm $f
78done
79
80cd ..
81
82echo " [+] Test cases in $DEST_TESTCASE_DIR" >&2
83
84