Simon Butcher | ef50c0d | 2016-01-26 22:15:11 +0000 | [diff] [blame] | 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' |
Bence Szépkúti | b7246ad | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 10 | # |
| 11 | # Copyright (C) 2016, Arm Limited, All Rights Reserved |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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: |
Bence Szépkúti | 09b4f19 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 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. |
Bence Szépkúti | b7246ad | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 31 | # |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 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 | # |
Bence Szépkúti | b7246ad | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 53 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Simon Butcher | ef50c0d | 2016-01-26 22:15:11 +0000 | [diff] [blame] | 54 | |
| 55 | # Abort on errors |
| 56 | set -e |
| 57 | |
| 58 | if [ -z $1 ] |
| 59 | then |
| 60 | echo " [!] No test file specified" >&2 |
| 61 | echo "Usage: $0 <test data file>" >&2 |
| 62 | exit 1 |
| 63 | fi |
| 64 | |
| 65 | SRC_FILEPATH=$(dirname $1)/$(basename $1) |
| 66 | TESTSUITE=$(basename $1 .data) |
| 67 | |
| 68 | THIS_DIR=$(basename $PWD) |
| 69 | |
| 70 | if [ -d ../library -a -d ../include -a -d ../tests -a $THIS_DIR == "tests" ]; |
| 71 | then :; |
| 72 | else |
| 73 | echo " [!] Must be run from mbed TLS tests directory" >&2 |
| 74 | exit 1 |
| 75 | fi |
| 76 | |
| 77 | DEST_TESTCASE_DIR=$TESTSUITE-afl-tests |
| 78 | DEST_OUTPUT_DIR=$TESTSUITE-afl-out |
| 79 | |
| 80 | echo " [+] Creating output directories" >&2 |
| 81 | |
| 82 | if [ -e $DEST_OUTPUT_DIR/* ]; |
| 83 | then : |
| 84 | echo " [!] Test output files already exist." >&2 |
| 85 | exit 1 |
| 86 | else |
| 87 | mkdir -p $DEST_OUTPUT_DIR |
| 88 | fi |
| 89 | |
| 90 | if [ -e $DEST_TESTCASE_DIR/* ]; |
| 91 | then : |
| 92 | echo " [!] Test output files already exist." >&2 |
| 93 | else |
| 94 | mkdir -p $DEST_TESTCASE_DIR |
| 95 | fi |
| 96 | |
| 97 | echo " [+] Creating test cases" >&2 |
| 98 | cd $DEST_TESTCASE_DIR |
| 99 | |
| 100 | split -p '^\s*$' ../$SRC_FILEPATH |
| 101 | |
| 102 | for f in *; |
| 103 | do |
| 104 | # Strip out any blank lines (no trim on OS X) |
| 105 | sed '/^\s*$/d' $f >testcase_$f |
| 106 | rm $f |
| 107 | done |
| 108 | |
| 109 | cd .. |
| 110 | |
| 111 | echo " [+] Test cases in $DEST_TESTCASE_DIR" >&2 |
| 112 | |