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 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: 19# 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. 31# 32# ********** 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 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 111