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