blob: 8deb2d4f2b3b1684994fdee0522fa02eac0ed642 [file] [log] [blame]
Simon Butcheref50c0d2016-01-26 22:15:11 +00001#!/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'
Bence Szépkútib7246ad2020-05-26 00:33:31 +020010#
11# Copyright (C) 2016, Arm Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020012# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
13#
14# This file is provided under the Apache License 2.0, or the
15# GNU General Public License v2.0 or later.
16#
17# **********
18# Apache License 2.0:
Bence Szépkúti09b4f192020-05-26 01:54:15 +020019#
20# Licensed under the Apache License, Version 2.0 (the "License"); you may
21# not use this file except in compliance with the License.
22# You may obtain a copy of the License at
23#
24# http://www.apache.org/licenses/LICENSE-2.0
25#
26# Unless required by applicable law or agreed to in writing, software
27# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29# See the License for the specific language governing permissions and
30# limitations under the License.
Bence Szépkútib7246ad2020-05-26 00:33:31 +020031#
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020032# **********
33#
34# **********
35# GNU General Public License v2.0 or later:
36#
37# This program is free software; you can redistribute it and/or modify
38# it under the terms of the GNU General Public License as published by
39# the Free Software Foundation; either version 2 of the License, or
40# (at your option) any later version.
41#
42# This program is distributed in the hope that it will be useful,
43# but WITHOUT ANY WARRANTY; without even the implied warranty of
44# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45# GNU General Public License for more details.
46#
47# You should have received a copy of the GNU General Public License along
48# with this program; if not, write to the Free Software Foundation, Inc.,
49# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
50#
51# **********
52#
Bence Szépkútib7246ad2020-05-26 00:33:31 +020053# This file is part of Mbed TLS (https://tls.mbed.org)
Simon Butcheref50c0d2016-01-26 22:15:11 +000054
55# Abort on errors
56set -e
57
58if [ -z $1 ]
59then
60 echo " [!] No test file specified" >&2
61 echo "Usage: $0 <test data file>" >&2
62 exit 1
63fi
64
65SRC_FILEPATH=$(dirname $1)/$(basename $1)
66TESTSUITE=$(basename $1 .data)
67
68THIS_DIR=$(basename $PWD)
69
70if [ -d ../library -a -d ../include -a -d ../tests -a $THIS_DIR == "tests" ];
71then :;
72else
73 echo " [!] Must be run from mbed TLS tests directory" >&2
74 exit 1
75fi
76
77DEST_TESTCASE_DIR=$TESTSUITE-afl-tests
78DEST_OUTPUT_DIR=$TESTSUITE-afl-out
79
80echo " [+] Creating output directories" >&2
81
82if [ -e $DEST_OUTPUT_DIR/* ];
83then :
84 echo " [!] Test output files already exist." >&2
85 exit 1
86else
87 mkdir -p $DEST_OUTPUT_DIR
88fi
89
90if [ -e $DEST_TESTCASE_DIR/* ];
91then :
92 echo " [!] Test output files already exist." >&2
93else
94 mkdir -p $DEST_TESTCASE_DIR
95fi
96
97echo " [+] Creating test cases" >&2
98cd $DEST_TESTCASE_DIR
99
100split -p '^\s*$' ../$SRC_FILEPATH
101
102for f in *;
103do
104 # Strip out any blank lines (no trim on OS X)
105 sed '/^\s*$/d' $f >testcase_$f
106 rm $f
107done
108
109cd ..
110
111echo " [+] Test cases in $DEST_TESTCASE_DIR" >&2
112