blob: adf8f032345a00e4912e74815d42c972dfe72d41 [file] [log] [blame]
Manuel Pégourié-Gonnard535e8aa2024-10-03 12:55:52 +02001# all-core.sh
2#
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5
6
7
8################################################################
9#### Documentation
10################################################################
11
12# Purpose
13# -------
14#
15# To run all tests possible or available on the platform.
16#
17# Notes for users
18# ---------------
19#
20# Warning: the test is destructive. It includes various build modes and
21# configurations, and can and will arbitrarily change the current CMake
22# configuration. The following files must be committed into git:
23# * include/mbedtls/mbedtls_config.h
24# * Makefile, library/Makefile, programs/Makefile, tests/Makefile,
25# programs/fuzz/Makefile
26# After running this script, the CMake cache will be lost and CMake
27# will no longer be initialised.
28#
29# The script assumes the presence of a number of tools:
30# * Basic Unix tools (Windows users note: a Unix-style find must be before
31# the Windows find in the PATH)
32# * Perl
33# * GNU Make
34# * CMake
35# * GCC and Clang (recent enough for using ASan with gcc and MemSan with clang, or valgrind)
36# * G++
37# * arm-gcc and mingw-gcc
38# * ArmCC 5 and ArmCC 6, unless invoked with --no-armcc
39# * OpenSSL and GnuTLS command line tools, in suitable versions for the
40# interoperability tests. The following are the official versions at the
41# time of writing:
42# * GNUTLS_{CLI,SERV} = 3.4.10
43# * GNUTLS_NEXT_{CLI,SERV} = 3.7.2
44# * OPENSSL = 1.0.2g (without Debian/Ubuntu patches)
45# * OPENSSL_NEXT = 3.1.2
46# See the invocation of check_tools below for details.
47#
48# This script must be invoked from the toplevel directory of a git
49# working copy of Mbed TLS.
50#
51# The behavior on an error depends on whether --keep-going (alias -k)
52# is in effect.
53# * Without --keep-going: the script stops on the first error without
54# cleaning up. This lets you work in the configuration of the failing
55# component.
56# * With --keep-going: the script runs all requested components and
57# reports failures at the end. In particular the script always cleans
58# up on exit.
59#
60# Note that the output is not saved. You may want to run
61# script -c tests/scripts/all.sh
62# or
63# tests/scripts/all.sh >all.log 2>&1
64#
65# Notes for maintainers
66# ---------------------
67#
68# The bulk of the code is organized into functions that follow one of the
69# following naming conventions:
70# * pre_XXX: things to do before running the tests, in order.
71# * component_XXX: independent components. They can be run in any order.
72# * component_check_XXX: quick tests that aren't worth parallelizing.
73# * component_build_XXX: build things but don't run them.
74# * component_test_XXX: build and test.
75# * component_release_XXX: tests that the CI should skip during PR testing.
76# * support_XXX: if support_XXX exists and returns false then
77# component_XXX is not run by default.
78# * post_XXX: things to do after running the tests.
79# * other: miscellaneous support functions.
80#
81# Each component must start by invoking `msg` with a short informative message.
82#
83# Warning: due to the way bash detects errors, the failure of a command
84# inside 'if' or '!' is not detected. Use the 'not' function instead of '!'.
85#
86# Each component is executed in a separate shell process. The component
87# fails if any command in it returns a non-zero status.
88#
89# The framework performs some cleanup tasks after each component. This
90# means that components can assume that the working directory is in a
91# cleaned-up state, and don't need to perform the cleanup themselves.
92# * Run `make clean`.
93# * Restore `include/mbedtls/mbedtls_config.h` from a backup made before running
94# the component.
95# * Check out `Makefile`, `library/Makefile`, `programs/Makefile`,
96# `tests/Makefile` and `programs/fuzz/Makefile` from git.
97# This cleans up after an in-tree use of CMake.
98#
99# The tests are roughly in order from fastest to slowest. This doesn't
100# have to be exact, but in general you should add slower tests towards
101# the end and fast checks near the beginning.
102
103
104
105################################################################
106#### Initialization and command line parsing
107################################################################
108
109# Enable ksh/bash extended file matching patterns.
110# Must come before function definitions or some of them wouldn't parse.
111shopt -s extglob
112
113pre_set_shell_options () {
114 # Abort on errors (even on the left-hand side of a pipe).
115 # Treat uninitialised variables as errors.
116 set -e -o pipefail -u
117}
118
119# For project detection
120in_mbedtls_repo () {
121 test "$PROJECT_NAME" = "Mbed TLS"
122}
123
124in_tf_psa_crypto_repo () {
125 test "$PROJECT_NAME" = "TF-PSA-Crypto"
126}
127
128pre_check_environment () {
129 # For project detection
130 PROJECT_NAME_FILE='./scripts/project_name.txt'
131 if read -r PROJECT_NAME < "$PROJECT_NAME_FILE"; then :; else
132 echo "$PROJECT_NAME_FILE does not exist... Exiting..." >&2
133 exit 1
134 fi
135
136 if in_mbedtls_repo || in_tf_psa_crypto_repo; then :; else
137 echo "Must be run from Mbed TLS / TF-PSA-Crypto root" >&2
138 exit 1
139 fi
140}
141
142# Must be called before pre_initialize_variables which sets ALL_COMPONENTS.
143pre_load_components () {
144 # Include the components from components.sh
145 test_script_dir="${0%/*}"
146 for file in "$test_script_dir"/components*.sh; do
147 source $file
148 done
149}
150
151pre_initialize_variables () {
152 if in_mbedtls_repo; then
153 CONFIG_H='include/mbedtls/mbedtls_config.h'
154 if [ -d tf-psa-crypto ]; then
155 CRYPTO_CONFIG_H='tf-psa-crypto/include/psa/crypto_config.h'
156 PSA_CORE_PATH='tf-psa-crypto/core'
157 BUILTIN_SRC_PATH='tf-psa-crypto/drivers/builtin/src'
158 else
159 CRYPTO_CONFIG_H='include/psa/crypto_config.h'
160 fi
161 else
162 CONFIG_H='drivers/builtin/include/mbedtls/mbedtls_config.h'
163 CRYPTO_CONFIG_H='include/psa/crypto_config.h'
164 PSA_CORE_PATH='core'
165 BUILTIN_SRC_PATH='drivers/builtin/src'
166 fi
167 CONFIG_TEST_DRIVER_H='tests/include/test/drivers/config_test_driver.h'
168
169 # Files that are clobbered by some jobs will be backed up. Use a different
170 # suffix from auxiliary scripts so that all.sh and auxiliary scripts can
171 # independently decide when to remove the backup file.
172 backup_suffix='.all.bak'
173 # Files clobbered by config.py
174 files_to_back_up="$CONFIG_H $CRYPTO_CONFIG_H $CONFIG_TEST_DRIVER_H"
175 if in_mbedtls_repo; then
176 # Files clobbered by in-tree cmake
177 files_to_back_up="$files_to_back_up Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile"
178 fi
179
180 append_outcome=0
181 MEMORY=0
182 FORCE=0
183 QUIET=0
184 KEEP_GOING=0
185
186 # Seed value used with the --release-test option.
187 #
188 # See also RELEASE_SEED in basic-build-test.sh. Debugging is easier if
189 # both values are kept in sync. If you change the value here because it
190 # breaks some tests, you'll definitely want to change it in
191 # basic-build-test.sh as well.
192 RELEASE_SEED=1
193
194 # Specify character collation for regular expressions and sorting with C locale
195 export LC_COLLATE=C
196
197 : ${MBEDTLS_TEST_OUTCOME_FILE=}
198 : ${MBEDTLS_TEST_PLATFORM="$(uname -s | tr -c \\n0-9A-Za-z _)-$(uname -m | tr -c \\n0-9A-Za-z _)"}
199 export MBEDTLS_TEST_OUTCOME_FILE
200 export MBEDTLS_TEST_PLATFORM
201
202 # Default commands, can be overridden by the environment
203 : ${OPENSSL:="openssl"}
204 : ${OPENSSL_NEXT:="$OPENSSL"}
205 : ${GNUTLS_CLI:="gnutls-cli"}
206 : ${GNUTLS_SERV:="gnutls-serv"}
207 : ${OUT_OF_SOURCE_DIR:=./mbedtls_out_of_source_build}
208 : ${ARMC5_BIN_DIR:=/usr/bin}
209 : ${ARMC6_BIN_DIR:=/usr/bin}
210 : ${ARM_NONE_EABI_GCC_PREFIX:=arm-none-eabi-}
211 : ${ARM_LINUX_GNUEABI_GCC_PREFIX:=arm-linux-gnueabi-}
212 : ${CLANG_LATEST:="clang-latest"}
213 : ${CLANG_EARLIEST:="clang-earliest"}
214 : ${GCC_LATEST:="gcc-latest"}
215 : ${GCC_EARLIEST:="gcc-earliest"}
216 # if MAKEFLAGS is not set add the -j option to speed up invocations of make
217 if [ -z "${MAKEFLAGS+set}" ]; then
218 export MAKEFLAGS="-j$(all_sh_nproc)"
219 fi
220 # if CC is not set, use clang by default (if present) to improve build times
221 if [ -z "${CC+set}" ] && (type clang > /dev/null 2>&1); then
222 export CC="clang"
223 fi
224
225 if [ -n "${OPENSSL_3+set}" ]; then
226 export OPENSSL_NEXT="$OPENSSL_3"
227 fi
228
229 # Include more verbose output for failing tests run by CMake or make
230 export CTEST_OUTPUT_ON_FAILURE=1
231
232 # CFLAGS and LDFLAGS for Asan builds that don't use CMake
233 # default to -O2, use -Ox _after_ this if you want another level
234 ASAN_CFLAGS='-O2 -Werror -fsanitize=address,undefined -fno-sanitize-recover=all'
235 # Normally, tests should use this compiler for ASAN testing
236 ASAN_CC=clang
237
238 # Platform tests have an allocation that returns null
239 export ASAN_OPTIONS="allocator_may_return_null=1"
240 export MSAN_OPTIONS="allocator_may_return_null=1"
241
242 # Gather the list of available components. These are the functions
243 # defined in this script whose name starts with "component_".
244 ALL_COMPONENTS=$(compgen -A function component_ | sed 's/component_//')
245
246 PSASIM_PATH='tests/psa-client-server/psasim/'
247
248 # Delay determining SUPPORTED_COMPONENTS until the command line options have a chance to override
249 # the commands set by the environment
250}
251
252setup_quiet_wrappers()
253{
254 # Pick up "quiet" wrappers for make and cmake, which don't output very much
255 # unless there is an error. This reduces logging overhead in the CI.
256 #
257 # Note that the cmake wrapper breaks unless we use an absolute path here.
258 if [[ -e ${PWD}/tests/scripts/quiet ]]; then
259 export PATH=${PWD}/tests/scripts/quiet:$PATH
260 fi
261}
262
263# Test whether the component $1 is included in the command line patterns.
264is_component_included()
265{
266 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
267 # only does word splitting.
268 set -f
269 for pattern in $COMMAND_LINE_COMPONENTS; do
270 set +f
271 case ${1#component_} in $pattern) return 0;; esac
272 done
273 set +f
274 return 1
275}
276
277usage()
278{
279 cat <<EOF
280Usage: $0 [OPTION]... [COMPONENT]...
281Run mbedtls release validation tests.
282By default, run all tests. With one or more COMPONENT, run only those.
283COMPONENT can be the name of a component or a shell wildcard pattern.
284
285Examples:
286 $0 "check_*"
287 Run all sanity checks.
288 $0 --no-armcc --except test_memsan
289 Run everything except builds that require armcc and MemSan.
290
291Special options:
292 -h|--help Print this help and exit.
293 --list-all-components List all available test components and exit.
294 --list-components List components supported on this platform and exit.
295
296General options:
297 -q|--quiet Only output component names, and errors if any.
298 -f|--force Force the tests to overwrite any modified files.
299 -k|--keep-going Run all tests and report errors at the end.
300 -m|--memory Additional optional memory tests.
301 --append-outcome Append to the outcome file (if used).
302 --arm-none-eabi-gcc-prefix=<string>
303 Prefix for a cross-compiler for arm-none-eabi
304 (default: "${ARM_NONE_EABI_GCC_PREFIX}")
305 --arm-linux-gnueabi-gcc-prefix=<string>
306 Prefix for a cross-compiler for arm-linux-gnueabi
307 (default: "${ARM_LINUX_GNUEABI_GCC_PREFIX}")
308 --armcc Run ARM Compiler builds (on by default).
309 --restore First clean up the build tree, restoring backed up
310 files. Do not run any components unless they are
311 explicitly specified.
312 --error-test Error test mode: run a failing function in addition
313 to any specified component. May be repeated.
314 --except Exclude the COMPONENTs listed on the command line,
315 instead of running only those.
316 --no-append-outcome Write a new outcome file and analyze it (default).
317 --no-armcc Skip ARM Compiler builds.
318 --no-force Refuse to overwrite modified files (default).
319 --no-keep-going Stop at the first error (default).
320 --no-memory No additional memory tests (default).
321 --no-quiet Print full output from components.
322 --out-of-source-dir=<path> Directory used for CMake out-of-source build tests.
323 --outcome-file=<path> File where test outcomes are written (not done if
324 empty; default: \$MBEDTLS_TEST_OUTCOME_FILE).
325 --random-seed Use a random seed value for randomized tests (default).
326 -r|--release-test Run this script in release mode. This fixes the seed value to ${RELEASE_SEED}.
327 -s|--seed Integer seed value to use for this test run.
328
329Tool path options:
330 --armc5-bin-dir=<ARMC5_bin_dir_path> ARM Compiler 5 bin directory.
331 --armc6-bin-dir=<ARMC6_bin_dir_path> ARM Compiler 6 bin directory.
332 --clang-earliest=<Clang_earliest_path> Earliest version of clang available
333 --clang-latest=<Clang_latest_path> Latest version of clang available
334 --gcc-earliest=<GCC_earliest_path> Earliest version of GCC available
335 --gcc-latest=<GCC_latest_path> Latest version of GCC available
336 --gnutls-cli=<GnuTLS_cli_path> GnuTLS client executable to use for most tests.
337 --gnutls-serv=<GnuTLS_serv_path> GnuTLS server executable to use for most tests.
338 --openssl=<OpenSSL_path> OpenSSL executable to use for most tests.
339 --openssl-next=<OpenSSL_path> OpenSSL executable to use for recent things like ARIA
340EOF
341}
342
343# Cleanup before/after running a component.
344# Remove built files as well as the cmake cache/config.
345# Does not remove generated source files.
346cleanup()
347{
348 if in_mbedtls_repo; then
349 command make clean
350 fi
351
352 # Remove CMake artefacts
353 find . -name .git -prune -o \
354 -iname CMakeFiles -exec rm -rf {} \+ -o \
355 \( -iname cmake_install.cmake -o \
356 -iname CTestTestfile.cmake -o \
357 -iname CMakeCache.txt -o \
358 -path './cmake/*.cmake' \) -exec rm -f {} \+
359 # Remove Makefiles generated by in-tree CMake builds
360 rm -f pkgconfig/Makefile framework/Makefile
361 rm -f include/Makefile programs/!(fuzz)/Makefile
362 rm -f tf-psa-crypto/Makefile tf-psa-crypto/include/Makefile
363 rm -f tf-psa-crypto/core/Makefile tf-psa-crypto/drivers/Makefile
364 rm -f tf-psa-crypto/tests/Makefile
365 rm -f tf-psa-crypto/drivers/everest/Makefile
366 rm -f tf-psa-crypto/drivers/p256-m/Makefile
367 rm -f tf-psa-crypto/drivers/builtin/Makefile
368 rm -f tf-psa-crypto/drivers/builtin/src/Makefile
369
370 # Remove any artifacts from the component_test_cmake_as_subdirectory test.
371 rm -rf programs/test/cmake_subproject/build
372 rm -f programs/test/cmake_subproject/Makefile
373 rm -f programs/test/cmake_subproject/cmake_subproject
374
375 # Remove any artifacts from the component_test_cmake_as_package test.
376 rm -rf programs/test/cmake_package/build
377 rm -f programs/test/cmake_package/Makefile
378 rm -f programs/test/cmake_package/cmake_package
379
380 # Remove any artifacts from the component_test_cmake_as_installed_package test.
381 rm -rf programs/test/cmake_package_install/build
382 rm -f programs/test/cmake_package_install/Makefile
383 rm -f programs/test/cmake_package_install/cmake_package_install
384
385 # Restore files that may have been clobbered by the job
386 restore_backed_up_files
387}
388
389# Restore files that may have been clobbered
390restore_backed_up_files () {
391 for x in $files_to_back_up; do
392 if [[ -e "$x$backup_suffix" ]]; then
393 cp -p "$x$backup_suffix" "$x"
394 fi
395 done
396}
397
398# Final cleanup when this script exits (except when exiting on a failure
399# in non-keep-going mode).
400final_cleanup () {
401 cleanup
402
403 for x in $files_to_back_up; do
404 rm -f "$x$backup_suffix"
405 done
406}
407
408# Executed on exit. May be redefined depending on command line options.
409final_report () {
410 :
411}
412
413fatal_signal () {
414 final_cleanup
415 final_report $1
416 trap - $1
417 kill -$1 $$
418}
419
420trap 'fatal_signal HUP' HUP
421trap 'fatal_signal INT' INT
422trap 'fatal_signal TERM' TERM
423
424# Number of processors on this machine. Used as the default setting
425# for parallel make.
426all_sh_nproc ()
427{
428 {
429 nproc || # Linux
430 sysctl -n hw.ncpuonline || # NetBSD, OpenBSD
431 sysctl -n hw.ncpu || # FreeBSD
432 echo 1
433 } 2>/dev/null
434}
435
436msg()
437{
438 if [ -n "${current_component:-}" ]; then
439 current_section="${current_component#component_}: $1"
440 else
441 current_section="$1"
442 fi
443
444 if [ $QUIET -eq 1 ]; then
445 return
446 fi
447
448 echo ""
449 echo "******************************************************************"
450 echo "* $current_section "
451 printf "* "; date
452 echo "******************************************************************"
453}
454
455err_msg()
456{
457 echo "$1" >&2
458}
459
460check_tools()
461{
462 for tool in "$@"; do
463 if ! `type "$tool" >/dev/null 2>&1`; then
464 err_msg "$tool not found!"
465 exit 1
466 fi
467 done
468}
469
470pre_parse_command_line () {
471 COMMAND_LINE_COMPONENTS=
472 all_except=0
473 error_test=0
474 list_components=0
475 restore_first=0
476 no_armcc=
477
478 # Note that legacy options are ignored instead of being omitted from this
479 # list of options, so invocations that worked with previous version of
480 # all.sh will still run and work properly.
481 while [ $# -gt 0 ]; do
482 case "$1" in
483 --append-outcome) append_outcome=1;;
484 --arm-none-eabi-gcc-prefix) shift; ARM_NONE_EABI_GCC_PREFIX="$1";;
485 --arm-linux-gnueabi-gcc-prefix) shift; ARM_LINUX_GNUEABI_GCC_PREFIX="$1";;
486 --armcc) no_armcc=;;
487 --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";;
488 --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";;
489 --clang-earliest) shift; CLANG_EARLIEST="$1";;
490 --clang-latest) shift; CLANG_LATEST="$1";;
491 --error-test) error_test=$((error_test + 1));;
492 --except) all_except=1;;
493 --force|-f) FORCE=1;;
494 --gcc-earliest) shift; GCC_EARLIEST="$1";;
495 --gcc-latest) shift; GCC_LATEST="$1";;
496 --gnutls-cli) shift; GNUTLS_CLI="$1";;
497 --gnutls-legacy-cli) shift;; # ignored for backward compatibility
498 --gnutls-legacy-serv) shift;; # ignored for backward compatibility
499 --gnutls-serv) shift; GNUTLS_SERV="$1";;
500 --help|-h) usage; exit;;
501 --keep-going|-k) KEEP_GOING=1;;
502 --list-all-components) printf '%s\n' $ALL_COMPONENTS; exit;;
503 --list-components) list_components=1;;
504 --memory|-m) MEMORY=1;;
505 --no-append-outcome) append_outcome=0;;
506 --no-armcc) no_armcc=1;;
507 --no-force) FORCE=0;;
508 --no-keep-going) KEEP_GOING=0;;
509 --no-memory) MEMORY=0;;
510 --no-quiet) QUIET=0;;
511 --openssl) shift; OPENSSL="$1";;
512 --openssl-next) shift; OPENSSL_NEXT="$1";;
513 --outcome-file) shift; MBEDTLS_TEST_OUTCOME_FILE="$1";;
514 --out-of-source-dir) shift; OUT_OF_SOURCE_DIR="$1";;
515 --quiet|-q) QUIET=1;;
516 --random-seed) unset SEED;;
517 --release-test|-r) SEED=$RELEASE_SEED;;
518 --restore) restore_first=1;;
519 --seed|-s) shift; SEED="$1";;
520 -*)
521 echo >&2 "Unknown option: $1"
522 echo >&2 "Run $0 --help for usage."
523 exit 120
524 ;;
525 *) COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS $1";;
526 esac
527 shift
528 done
529
530 # Exclude components that are not supported on this platform.
531 SUPPORTED_COMPONENTS=
532 for component in $ALL_COMPONENTS; do
533 case $(type "support_$component" 2>&1) in
534 *' function'*)
535 if ! support_$component; then continue; fi;;
536 esac
537 SUPPORTED_COMPONENTS="$SUPPORTED_COMPONENTS $component"
538 done
539
540 if [ $list_components -eq 1 ]; then
541 printf '%s\n' $SUPPORTED_COMPONENTS
542 exit
543 fi
544
545 # With no list of components, run everything.
546 if [ -z "$COMMAND_LINE_COMPONENTS" ] && [ $restore_first -eq 0 ]; then
547 all_except=1
548 fi
549
550 # --no-armcc is a legacy option. The modern way is --except '*_armcc*'.
551 # Ignore it if components are listed explicitly on the command line.
552 if [ -n "$no_armcc" ] && [ $all_except -eq 1 ]; then
553 COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*"
554 fi
555
556 # Error out if an explicitly requested component doesn't exist.
557 if [ $all_except -eq 0 ]; then
558 unsupported=0
559 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
560 # only does word splitting.
561 set -f
562 for component in $COMMAND_LINE_COMPONENTS; do
563 set +f
564 # If the requested name includes a wildcard character, don't
565 # check it. Accept wildcard patterns that don't match anything.
566 case $component in
567 *[*?\[]*) continue;;
568 esac
569 case " $SUPPORTED_COMPONENTS " in
570 *" $component "*) :;;
571 *)
572 echo >&2 "Component $component was explicitly requested, but is not known or not supported."
573 unsupported=$((unsupported + 1));;
574 esac
575 done
576 set +f
577 if [ $unsupported -ne 0 ]; then
578 exit 2
579 fi
580 fi
581
582 # Build the list of components to run.
583 RUN_COMPONENTS=
584 for component in $SUPPORTED_COMPONENTS; do
585 if is_component_included "$component"; [ $? -eq $all_except ]; then
586 RUN_COMPONENTS="$RUN_COMPONENTS $component"
587 fi
588 done
589
590 unset all_except
591 unset no_armcc
592}
593
594pre_check_git () {
595 if [ $FORCE -eq 1 ]; then
596 rm -rf "$OUT_OF_SOURCE_DIR"
597 git checkout-index -f -q $CONFIG_H
598 cleanup
599 else
600
601 if [ -d "$OUT_OF_SOURCE_DIR" ]; then
602 echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
603 echo "You can either delete this directory manually, or force the test by rerunning"
604 echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
605 exit 1
606 fi
607
608 if ! git diff --quiet "$CONFIG_H"; then
609 err_msg "Warning - the configuration file '$CONFIG_H' has been edited. "
610 echo "You can either delete or preserve your work, or force the test by rerunning the"
611 echo "script as: $0 --force"
612 exit 1
613 fi
614 fi
615}
616
617pre_restore_files () {
618 # If the makefiles have been generated by a framework such as cmake,
619 # restore them from git. If the makefiles look like modifications from
620 # the ones checked into git, take care not to modify them. Whatever
621 # this function leaves behind is what the script will restore before
622 # each component.
623 case "$(head -n1 Makefile)" in
624 *[Gg]enerated*)
625 git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
626 git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
627 ;;
628 esac
629}
630
631pre_back_up () {
632 for x in $files_to_back_up; do
633 cp -p "$x" "$x$backup_suffix"
634 done
635}
636
637pre_setup_keep_going () {
638 failure_count=0 # Number of failed components
639 last_failure_status=0 # Last failure status in this component
640
641 # See err_trap
642 previous_failure_status=0
643 previous_failed_command=
644 previous_failure_funcall_depth=0
645 unset report_failed_command
646
647 start_red=
648 end_color=
649 if [ -t 1 ]; then
650 case "${TERM:-}" in
651 *color*|cygwin|linux|rxvt*|screen|[Eex]term*)
652 start_red=$(printf '\033[31m')
653 end_color=$(printf '\033[0m')
654 ;;
655 esac
656 fi
657
658 # Keep a summary of failures in a file. We'll print it out at the end.
659 failure_summary_file=$PWD/all-sh-failures-$$.log
660 : >"$failure_summary_file"
661
662 # Whether it makes sense to keep a component going after the specified
663 # command fails (test command) or not (configure or build).
664 # This function normally receives the failing simple command
665 # ($BASH_COMMAND) as an argument, but if $report_failed_command is set,
666 # this is passed instead.
667 # This doesn't have to be 100% accurate: all failures are recorded anyway.
668 # False positives result in running things that can't be expected to
669 # work. False negatives result in things not running after something else
670 # failed even though they might have given useful feedback.
671 can_keep_going_after_failure () {
672 case "$1" in
673 "msg "*) false;;
674 "cd "*) false;;
675 "diff "*) true;;
676 *make*[\ /]tests*) false;; # make tests, make CFLAGS=-I../tests, ...
677 *test*) true;; # make test, tests/stuff, env V=v tests/stuff, ...
678 *make*check*) true;;
679 "grep "*) true;;
680 "[ "*) true;;
681 "! "*) true;;
682 *) false;;
683 esac
684 }
685
686 # This function runs if there is any error in a component.
687 # It must either exit with a nonzero status, or set
688 # last_failure_status to a nonzero value.
689 err_trap () {
690 # Save $? (status of the failing command). This must be the very
691 # first thing, before $? is overridden.
692 last_failure_status=$?
693 failed_command=${report_failed_command-$BASH_COMMAND}
694
695 if [[ $last_failure_status -eq $previous_failure_status &&
696 "$failed_command" == "$previous_failed_command" &&
697 ${#FUNCNAME[@]} == $((previous_failure_funcall_depth - 1)) ]]
698 then
699 # The same command failed twice in a row, but this time one level
700 # less deep in the function call stack. This happens when the last
701 # command of a function returns a nonzero status, and the function
702 # returns that same status. Ignore the second failure.
703 previous_failure_funcall_depth=${#FUNCNAME[@]}
704 return
705 fi
706 previous_failure_status=$last_failure_status
707 previous_failed_command=$failed_command
708 previous_failure_funcall_depth=${#FUNCNAME[@]}
709
710 text="$current_section: $failed_command -> $last_failure_status"
711 echo "${start_red}^^^^$text^^^^${end_color}" >&2
712 echo "$text" >>"$failure_summary_file"
713
714 # If the command is fatal (configure or build command), stop this
715 # component. Otherwise (test command) keep the component running
716 # (run more tests from the same build).
717 if ! can_keep_going_after_failure "$failed_command"; then
718 exit $last_failure_status
719 fi
720 }
721
722 final_report () {
723 if [ $failure_count -gt 0 ]; then
724 echo
725 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
726 echo "${start_red}FAILED: $failure_count components${end_color}"
727 cat "$failure_summary_file"
728 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
729 elif [ -z "${1-}" ]; then
730 echo "SUCCESS :)"
731 fi
732 if [ -n "${1-}" ]; then
733 echo "Killed by SIG$1."
734 fi
735 rm -f "$failure_summary_file"
736 if [ $failure_count -gt 0 ]; then
737 exit 1
738 fi
739 }
740}
741
742# '! true' does not trigger the ERR trap. Arrange to trigger it, with
743# a reasonably informative error message (not just "$@").
744not () {
745 if "$@"; then
746 report_failed_command="! $*"
747 false
748 unset report_failed_command
749 fi
750}
751
752pre_prepare_outcome_file () {
753 case "$MBEDTLS_TEST_OUTCOME_FILE" in
754 [!/]*) MBEDTLS_TEST_OUTCOME_FILE="$PWD/$MBEDTLS_TEST_OUTCOME_FILE";;
755 esac
756 if [ -n "$MBEDTLS_TEST_OUTCOME_FILE" ] && [ "$append_outcome" -eq 0 ]; then
757 rm -f "$MBEDTLS_TEST_OUTCOME_FILE"
758 fi
759}
760
761pre_print_configuration () {
762 if [ $QUIET -eq 1 ]; then
763 return
764 fi
765
766 msg "info: $0 configuration"
767 echo "MEMORY: $MEMORY"
768 echo "FORCE: $FORCE"
769 echo "MBEDTLS_TEST_OUTCOME_FILE: ${MBEDTLS_TEST_OUTCOME_FILE:-(none)}"
770 echo "SEED: ${SEED-"UNSET"}"
771 echo
772 echo "OPENSSL: $OPENSSL"
773 echo "OPENSSL_NEXT: $OPENSSL_NEXT"
774 echo "GNUTLS_CLI: $GNUTLS_CLI"
775 echo "GNUTLS_SERV: $GNUTLS_SERV"
776 echo "ARMC5_BIN_DIR: $ARMC5_BIN_DIR"
777 echo "ARMC6_BIN_DIR: $ARMC6_BIN_DIR"
778}
779
780# Make sure the tools we need are available.
781pre_check_tools () {
782 # Build the list of variables to pass to output_env.sh.
783 set env
784
785 case " $RUN_COMPONENTS " in
786 # Require OpenSSL and GnuTLS if running any tests (as opposed to
787 # only doing builds). Not all tests run OpenSSL and GnuTLS, but this
788 # is a good enough approximation in practice.
789 *" test_"* | *" release_test_"*)
790 # To avoid setting OpenSSL and GnuTLS for each call to compat.sh
791 # and ssl-opt.sh, we just export the variables they require.
792 export OPENSSL="$OPENSSL"
793 export GNUTLS_CLI="$GNUTLS_CLI"
794 export GNUTLS_SERV="$GNUTLS_SERV"
795 # Avoid passing --seed flag in every call to ssl-opt.sh
796 if [ -n "${SEED-}" ]; then
797 export SEED
798 fi
799 set "$@" OPENSSL="$OPENSSL"
800 set "$@" GNUTLS_CLI="$GNUTLS_CLI" GNUTLS_SERV="$GNUTLS_SERV"
801 check_tools "$OPENSSL" "$OPENSSL_NEXT" \
802 "$GNUTLS_CLI" "$GNUTLS_SERV"
803 ;;
804 esac
805
806 case " $RUN_COMPONENTS " in
807 *_doxygen[_\ ]*) check_tools "doxygen" "dot";;
808 esac
809
810 case " $RUN_COMPONENTS " in
811 *_arm_none_eabi_gcc[_\ ]*) check_tools "${ARM_NONE_EABI_GCC_PREFIX}gcc";;
812 esac
813
814 case " $RUN_COMPONENTS " in
815 *_mingw[_\ ]*) check_tools "i686-w64-mingw32-gcc";;
816 esac
817
818 case " $RUN_COMPONENTS " in
819 *" test_zeroize "*) check_tools "gdb";;
820 esac
821
822 case " $RUN_COMPONENTS " in
823 *_armcc*)
824 ARMC5_CC="$ARMC5_BIN_DIR/armcc"
825 ARMC5_AR="$ARMC5_BIN_DIR/armar"
826 ARMC5_FROMELF="$ARMC5_BIN_DIR/fromelf"
827 ARMC6_CC="$ARMC6_BIN_DIR/armclang"
828 ARMC6_AR="$ARMC6_BIN_DIR/armar"
829 ARMC6_FROMELF="$ARMC6_BIN_DIR/fromelf"
830 check_tools "$ARMC5_CC" "$ARMC5_AR" "$ARMC5_FROMELF" \
831 "$ARMC6_CC" "$ARMC6_AR" "$ARMC6_FROMELF";;
832 esac
833
834 # past this point, no call to check_tool, only printing output
835 if [ $QUIET -eq 1 ]; then
836 return
837 fi
838
839 msg "info: output_env.sh"
840 case $RUN_COMPONENTS in
841 *_armcc*)
842 set "$@" ARMC5_CC="$ARMC5_CC" ARMC6_CC="$ARMC6_CC" RUN_ARMCC=1;;
843 *) set "$@" RUN_ARMCC=0;;
844 esac
845 "$@" scripts/output_env.sh
846}
847
848pre_generate_files() {
849 # since make doesn't have proper dependencies, remove any possibly outdate
850 # file that might be around before generating fresh ones
851 make neat
852 if [ $QUIET -eq 1 ]; then
853 make generated_files >/dev/null
854 else
855 make generated_files
856 fi
857}
858
859pre_load_helpers () {
860 # The path is going to change when this is moved to the framework
861 test_script_dir="${0%/*}"
862 source "$test_script_dir"/all-helpers.sh
863}
864
865################################################################
866#### Termination
867################################################################
868
869post_report () {
870 msg "Done, cleaning up"
871 final_cleanup
872
873 final_report
874}
875
876################################################################
877#### Run all the things
878################################################################
879
880# Function invoked by --error-test to test error reporting.
881pseudo_component_error_test () {
882 msg "Testing error reporting $error_test_i"
883 if [ $KEEP_GOING -ne 0 ]; then
884 echo "Expect three failing commands."
885 fi
886 # If the component doesn't run in a subshell, changing error_test_i to an
887 # invalid integer will cause an error in the loop that runs this function.
888 error_test_i=this_should_not_be_used_since_the_component_runs_in_a_subshell
889 # Expected error: 'grep non_existent /dev/null -> 1'
890 grep non_existent /dev/null
891 # Expected error: '! grep -q . tests/scripts/all.sh -> 1'
892 not grep -q . "$0"
893 # Expected error: 'make unknown_target -> 2'
894 make unknown_target
895 false "this should not be executed"
896}
897
898# Run one component and clean up afterwards.
899run_component () {
900 current_component="$1"
901 export MBEDTLS_TEST_CONFIGURATION="$current_component"
902
903 # Unconditionally create a seedfile that's sufficiently long.
904 # Do this before each component, because a previous component may
905 # have messed it up or shortened it.
906 local dd_cmd
907 dd_cmd=(dd if=/dev/urandom of=./tests/seedfile bs=64 count=1)
908 case $OSTYPE in
909 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
910 esac
911 "${dd_cmd[@]}"
912
913 if [ -d tf-psa-crypto ]; then
914 dd_cmd=(dd if=/dev/urandom of=./tf-psa-crypto/tests/seedfile bs=64 count=1)
915 case $OSTYPE in
916 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
917 esac
918 "${dd_cmd[@]}"
919 fi
920
921 # Run the component in a subshell, with error trapping and output
922 # redirection set up based on the relevant options.
923 if [ $KEEP_GOING -eq 1 ]; then
924 # We want to keep running if the subshell fails, so 'set -e' must
925 # be off when the subshell runs.
926 set +e
927 fi
928 (
929 if [ $QUIET -eq 1 ]; then
930 # msg() will be silenced, so just print the component name here.
931 echo "${current_component#component_}"
932 exec >/dev/null
933 fi
934 if [ $KEEP_GOING -eq 1 ]; then
935 # Keep "set -e" off, and run an ERR trap instead to record failures.
936 set -E
937 trap err_trap ERR
938 fi
939 # The next line is what runs the component
940 "$@"
941 if [ $KEEP_GOING -eq 1 ]; then
942 trap - ERR
943 exit $last_failure_status
944 fi
945 )
946 component_status=$?
947 if [ $KEEP_GOING -eq 1 ]; then
948 set -e
949 if [ $component_status -ne 0 ]; then
950 failure_count=$((failure_count + 1))
951 fi
952 fi
953
954 # Restore the build tree to a clean state.
955 cleanup
956 unset current_component
957}
958
959################################################################
960#### Main
961################################################################
962
963main () {
964 # Preliminary setup
965 pre_set_shell_options
966 pre_check_environment
967 pre_load_helpers
968 pre_load_components
969 pre_initialize_variables
970 pre_parse_command_line "$@"
971
972 setup_quiet_wrappers
973 pre_check_git
974 pre_restore_files
975 pre_back_up
976
977 build_status=0
978 if [ $KEEP_GOING -eq 1 ]; then
979 pre_setup_keep_going
980 fi
981 pre_prepare_outcome_file
982 pre_print_configuration
983 pre_check_tools
984 cleanup
985 if in_mbedtls_repo; then
986 pre_generate_files
987 fi
988
989 # Run the requested tests.
990 for ((error_test_i=1; error_test_i <= error_test; error_test_i++)); do
991 run_component pseudo_component_error_test
992 done
993 unset error_test_i
994 for component in $RUN_COMPONENTS; do
995 run_component "component_$component"
996 done
997
998 # We're done.
999 post_report
1000}