generate-afl-tests.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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
  12. #
  13. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. # not use this file except in compliance with the License.
  15. # You may obtain a copy of the License at
  16. #
  17. # http://www.apache.org/licenses/LICENSE-2.0
  18. #
  19. # Unless required by applicable law or agreed to in writing, software
  20. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. # See the License for the specific language governing permissions and
  23. # limitations under the License.
  24. # Abort on errors
  25. set -e
  26. if [ -z $1 ]
  27. then
  28. echo " [!] No test file specified" >&2
  29. echo "Usage: $0 <test data file>" >&2
  30. exit 1
  31. fi
  32. SRC_FILEPATH=$(dirname $1)/$(basename $1)
  33. TESTSUITE=$(basename $1 .data)
  34. THIS_DIR=$(basename $PWD)
  35. if [ -d ../library -a -d ../include -a -d ../tests -a $THIS_DIR == "tests" ];
  36. then :;
  37. else
  38. echo " [!] Must be run from mbed TLS tests directory" >&2
  39. exit 1
  40. fi
  41. DEST_TESTCASE_DIR=$TESTSUITE-afl-tests
  42. DEST_OUTPUT_DIR=$TESTSUITE-afl-out
  43. echo " [+] Creating output directories" >&2
  44. if [ -e $DEST_OUTPUT_DIR/* ];
  45. then :
  46. echo " [!] Test output files already exist." >&2
  47. exit 1
  48. else
  49. mkdir -p $DEST_OUTPUT_DIR
  50. fi
  51. if [ -e $DEST_TESTCASE_DIR/* ];
  52. then :
  53. echo " [!] Test output files already exist." >&2
  54. else
  55. mkdir -p $DEST_TESTCASE_DIR
  56. fi
  57. echo " [+] Creating test cases" >&2
  58. cd $DEST_TESTCASE_DIR
  59. split -p '^\s*$' ../$SRC_FILEPATH
  60. for f in *;
  61. do
  62. # Strip out any blank lines (no trim on OS X)
  63. sed '/^\s*$/d' $f >testcase_$f
  64. rm $f
  65. done
  66. cd ..
  67. echo " [+] Test cases in $DEST_TESTCASE_DIR" >&2