blob: 22bea9d2be8fdbe7f42a481fef62be9e73219c10 [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#
Bence Szépkúti44bfbe32020-08-19 16:54:51 +020011# Copyright The Mbed TLS Contributors
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# **********
Simon Butcheref50c0d2016-01-26 22:15:11 +000052
53# Abort on errors
54set -e
55
56if [ -z $1 ]
57then
58 echo " [!] No test file specified" >&2
59 echo "Usage: $0 <test data file>" >&2
60 exit 1
61fi
62
63SRC_FILEPATH=$(dirname $1)/$(basename $1)
64TESTSUITE=$(basename $1 .data)
65
66THIS_DIR=$(basename $PWD)
67
68if [ -d ../library -a -d ../include -a -d ../tests -a $THIS_DIR == "tests" ];
69then :;
70else
71 echo " [!] Must be run from mbed TLS tests directory" >&2
72 exit 1
73fi
74
75DEST_TESTCASE_DIR=$TESTSUITE-afl-tests
76DEST_OUTPUT_DIR=$TESTSUITE-afl-out
77
78echo " [+] Creating output directories" >&2
79
80if [ -e $DEST_OUTPUT_DIR/* ];
81then :
82 echo " [!] Test output files already exist." >&2
83 exit 1
84else
85 mkdir -p $DEST_OUTPUT_DIR
86fi
87
88if [ -e $DEST_TESTCASE_DIR/* ];
89then :
90 echo " [!] Test output files already exist." >&2
91else
92 mkdir -p $DEST_TESTCASE_DIR
93fi
94
95echo " [+] Creating test cases" >&2
96cd $DEST_TESTCASE_DIR
97
98split -p '^\s*$' ../$SRC_FILEPATH
99
100for f in *;
101do
102 # Strip out any blank lines (no trim on OS X)
103 sed '/^\s*$/d' $f >testcase_$f
104 rm $f
105done
106
107cd ..
108
109echo " [+] Test cases in $DEST_TESTCASE_DIR" >&2
110