generate-afl-tests.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/sh
  2. # This script splits the data test files containing the test cases into
  3. # individual files (one test case per file) suitable for use with afl
  4. # (American Fuzzy Lop). http://lcamtuf.coredump.cx/afl/
  5. #
  6. # Usage: generate-afl-tests.sh <test data file path>
  7. # <test data file path> - should be the path to one of the test suite files
  8. # such as 'test_suite_mpi.data'
  9. #
  10. # Copyright The Mbed TLS Contributors
  11. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  12. #
  13. # This file is provided under the Apache License 2.0, or the
  14. # GNU General Public License v2.0 or later.
  15. #
  16. # **********
  17. # Apache License 2.0:
  18. #
  19. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  20. # not use this file except in compliance with the License.
  21. # You may obtain a copy of the License at
  22. #
  23. # http://www.apache.org/licenses/LICENSE-2.0
  24. #
  25. # Unless required by applicable law or agreed to in writing, software
  26. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  27. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. # See the License for the specific language governing permissions and
  29. # limitations under the License.
  30. #
  31. # **********
  32. #
  33. # **********
  34. # GNU General Public License v2.0 or later:
  35. #
  36. # This program is free software; you can redistribute it and/or modify
  37. # it under the terms of the GNU General Public License as published by
  38. # the Free Software Foundation; either version 2 of the License, or
  39. # (at your option) any later version.
  40. #
  41. # This program is distributed in the hope that it will be useful,
  42. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. # GNU General Public License for more details.
  45. #
  46. # You should have received a copy of the GNU General Public License along
  47. # with this program; if not, write to the Free Software Foundation, Inc.,
  48. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  49. #
  50. # **********
  51. # Abort on errors
  52. set -e
  53. if [ -z $1 ]
  54. then
  55. echo " [!] No test file specified" >&2
  56. echo "Usage: $0 <test data file>" >&2
  57. exit 1
  58. fi
  59. SRC_FILEPATH=$(dirname $1)/$(basename $1)
  60. TESTSUITE=$(basename $1 .data)
  61. THIS_DIR=$(basename $PWD)
  62. if [ -d ../library -a -d ../include -a -d ../tests -a $THIS_DIR == "tests" ];
  63. then :;
  64. else
  65. echo " [!] Must be run from mbed TLS tests directory" >&2
  66. exit 1
  67. fi
  68. DEST_TESTCASE_DIR=$TESTSUITE-afl-tests
  69. DEST_OUTPUT_DIR=$TESTSUITE-afl-out
  70. echo " [+] Creating output directories" >&2
  71. if [ -e $DEST_OUTPUT_DIR/* ];
  72. then :
  73. echo " [!] Test output files already exist." >&2
  74. exit 1
  75. else
  76. mkdir -p $DEST_OUTPUT_DIR
  77. fi
  78. if [ -e $DEST_TESTCASE_DIR/* ];
  79. then :
  80. echo " [!] Test output files already exist." >&2
  81. else
  82. mkdir -p $DEST_TESTCASE_DIR
  83. fi
  84. echo " [+] Creating test cases" >&2
  85. cd $DEST_TESTCASE_DIR
  86. split -p '^\s*$' ../$SRC_FILEPATH
  87. for f in *;
  88. do
  89. # Strip out any blank lines (no trim on OS X)
  90. sed '/^\s*$/d' $f >testcase_$f
  91. rm $f
  92. done
  93. cd ..
  94. echo " [+] Test cases in $DEST_TESTCASE_DIR" >&2