blob: 5cb1da8ed8bdacf603e2559ba68b24bda0d513ab [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
Manuel Pégourié-Gonnard705690a2024-10-29 11:39:41 +010054# * ArmCC 6 (aka armclang), unless invoked with --no-armcc
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +020055# * 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}
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200229 : ${ARMC6_BIN_DIR:=/usr/bin}
230 : ${ARM_NONE_EABI_GCC_PREFIX:=arm-none-eabi-}
231 : ${ARM_LINUX_GNUEABI_GCC_PREFIX:=arm-linux-gnueabi-}
Bence Szépkúti1524b9c2024-07-11 09:50:45 +0100232 : ${ARM_LINUX_GNUEABIHF_GCC_PREFIX:=arm-linux-gnueabihf-}
Bence Szépkútid1d26132024-07-11 15:52:07 +0100233 : ${AARCH64_LINUX_GNU_GCC_PREFIX:=aarch64-linux-gnu-}
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200234 : ${CLANG_LATEST:="clang-latest"}
235 : ${CLANG_EARLIEST:="clang-earliest"}
236 : ${GCC_LATEST:="gcc-latest"}
237 : ${GCC_EARLIEST:="gcc-earliest"}
238 # if MAKEFLAGS is not set add the -j option to speed up invocations of make
239 if [ -z "${MAKEFLAGS+set}" ]; then
240 export MAKEFLAGS="-j$(all_sh_nproc)"
241 fi
242 # if CC is not set, use clang by default (if present) to improve build times
243 if [ -z "${CC+set}" ] && (type clang > /dev/null 2>&1); then
244 export CC="clang"
245 fi
246
247 if [ -n "${OPENSSL_3+set}" ]; then
248 export OPENSSL_NEXT="$OPENSSL_3"
249 fi
250
251 # Include more verbose output for failing tests run by CMake or make
252 export CTEST_OUTPUT_ON_FAILURE=1
253
254 # CFLAGS and LDFLAGS for Asan builds that don't use CMake
255 # default to -O2, use -Ox _after_ this if you want another level
256 ASAN_CFLAGS='-O2 -Werror -fsanitize=address,undefined -fno-sanitize-recover=all'
257 # Normally, tests should use this compiler for ASAN testing
258 ASAN_CC=clang
259
260 # Platform tests have an allocation that returns null
261 export ASAN_OPTIONS="allocator_may_return_null=1"
262 export MSAN_OPTIONS="allocator_may_return_null=1"
263
264 # Gather the list of available components. These are the functions
265 # defined in this script whose name starts with "component_".
266 ALL_COMPONENTS=$(compgen -A function component_ | sed 's/component_//')
267
268 PSASIM_PATH='tests/psa-client-server/psasim/'
269
270 # Delay determining SUPPORTED_COMPONENTS until the command line options have a chance to override
271 # the commands set by the environment
272}
273
274setup_quiet_wrappers()
275{
276 # Pick up "quiet" wrappers for make and cmake, which don't output very much
277 # unless there is an error. This reduces logging overhead in the CI.
278 #
279 # Note that the cmake wrapper breaks unless we use an absolute path here.
280 if [[ -e ${PWD}/tests/scripts/quiet ]]; then
281 export PATH=${PWD}/tests/scripts/quiet:$PATH
282 fi
283}
284
285# Test whether the component $1 is included in the command line patterns.
286is_component_included()
287{
288 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
289 # only does word splitting.
290 set -f
291 for pattern in $COMMAND_LINE_COMPONENTS; do
292 set +f
293 case ${1#component_} in $pattern) return 0;; esac
294 done
295 set +f
296 return 1
297}
298
299usage()
300{
301 cat <<EOF
302Usage: $0 [OPTION]... [COMPONENT]...
303Run mbedtls release validation tests.
304By default, run all tests. With one or more COMPONENT, run only those.
305COMPONENT can be the name of a component or a shell wildcard pattern.
306
307Examples:
308 $0 "check_*"
309 Run all sanity checks.
310 $0 --no-armcc --except test_memsan
311 Run everything except builds that require armcc and MemSan.
312
313Special options:
314 -h|--help Print this help and exit.
315 --list-all-components List all available test components and exit.
316 --list-components List components supported on this platform and exit.
317
318General options:
319 -q|--quiet Only output component names, and errors if any.
320 -f|--force Force the tests to overwrite any modified files.
321 -k|--keep-going Run all tests and report errors at the end.
322 -m|--memory Additional optional memory tests.
323 --append-outcome Append to the outcome file (if used).
324 --arm-none-eabi-gcc-prefix=<string>
325 Prefix for a cross-compiler for arm-none-eabi
326 (default: "${ARM_NONE_EABI_GCC_PREFIX}")
327 --arm-linux-gnueabi-gcc-prefix=<string>
328 Prefix for a cross-compiler for arm-linux-gnueabi
329 (default: "${ARM_LINUX_GNUEABI_GCC_PREFIX}")
Bence Szépkútibc8c1572024-10-27 22:46:48 +0100330 --arm-linux-gnueabihf-gcc-prefix=<string>
331 Prefix for a cross-compiler for arm-linux-gnueabihf
332 (default: "${ARM_LINUX_GNUEABIHF_GCC_PREFIX}")
333 --aarch64-linux-gnu-gcc-prefix=<string>
334 Prefix for a cross-compiler for aarch64-linux-gnu
335 (default: "${AARCH64_LINUX_GNU_GCC_PREFIX}")
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200336 --armcc Run ARM Compiler builds (on by default).
337 --restore First clean up the build tree, restoring backed up
338 files. Do not run any components unless they are
339 explicitly specified.
340 --error-test Error test mode: run a failing function in addition
341 to any specified component. May be repeated.
342 --except Exclude the COMPONENTs listed on the command line,
343 instead of running only those.
344 --no-append-outcome Write a new outcome file and analyze it (default).
345 --no-armcc Skip ARM Compiler builds.
346 --no-force Refuse to overwrite modified files (default).
347 --no-keep-going Stop at the first error (default).
348 --no-memory No additional memory tests (default).
349 --no-quiet Print full output from components.
350 --out-of-source-dir=<path> Directory used for CMake out-of-source build tests.
351 --outcome-file=<path> File where test outcomes are written (not done if
352 empty; default: \$MBEDTLS_TEST_OUTCOME_FILE).
353 --random-seed Use a random seed value for randomized tests (default).
354 -r|--release-test Run this script in release mode. This fixes the seed value to ${RELEASE_SEED}.
355 -s|--seed Integer seed value to use for this test run.
356
357Tool path options:
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200358 --armc6-bin-dir=<ARMC6_bin_dir_path> ARM Compiler 6 bin directory.
359 --clang-earliest=<Clang_earliest_path> Earliest version of clang available
360 --clang-latest=<Clang_latest_path> Latest version of clang available
361 --gcc-earliest=<GCC_earliest_path> Earliest version of GCC available
362 --gcc-latest=<GCC_latest_path> Latest version of GCC available
363 --gnutls-cli=<GnuTLS_cli_path> GnuTLS client executable to use for most tests.
364 --gnutls-serv=<GnuTLS_serv_path> GnuTLS server executable to use for most tests.
365 --openssl=<OpenSSL_path> OpenSSL executable to use for most tests.
366 --openssl-next=<OpenSSL_path> OpenSSL executable to use for recent things like ARIA
367EOF
368}
369
370# Cleanup before/after running a component.
371# Remove built files as well as the cmake cache/config.
372# Does not remove generated source files.
373cleanup()
374{
375 if in_mbedtls_repo; then
376 command make clean
377 fi
378
379 # Remove CMake artefacts
380 find . -name .git -prune -o \
381 -iname CMakeFiles -exec rm -rf {} \+ -o \
382 \( -iname cmake_install.cmake -o \
383 -iname CTestTestfile.cmake -o \
384 -iname CMakeCache.txt -o \
385 -path './cmake/*.cmake' \) -exec rm -f {} \+
386 # Remove Makefiles generated by in-tree CMake builds
Manuel Pégourié-Gonnardf48d4ed2024-10-16 10:38:55 +0200387 # (Not all files will exist in all branches, but that's OK.)
388 rm -f 3rdparty/Makefile 3rdparty/*/Makefile
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200389 rm -f pkgconfig/Makefile framework/Makefile
390 rm -f include/Makefile programs/!(fuzz)/Makefile
391 rm -f tf-psa-crypto/Makefile tf-psa-crypto/include/Makefile
392 rm -f tf-psa-crypto/core/Makefile tf-psa-crypto/drivers/Makefile
393 rm -f tf-psa-crypto/tests/Makefile
394 rm -f tf-psa-crypto/drivers/everest/Makefile
395 rm -f tf-psa-crypto/drivers/p256-m/Makefile
396 rm -f tf-psa-crypto/drivers/builtin/Makefile
397 rm -f tf-psa-crypto/drivers/builtin/src/Makefile
398
399 # Remove any artifacts from the component_test_cmake_as_subdirectory test.
400 rm -rf programs/test/cmake_subproject/build
401 rm -f programs/test/cmake_subproject/Makefile
402 rm -f programs/test/cmake_subproject/cmake_subproject
403
404 # Remove any artifacts from the component_test_cmake_as_package test.
405 rm -rf programs/test/cmake_package/build
406 rm -f programs/test/cmake_package/Makefile
407 rm -f programs/test/cmake_package/cmake_package
408
409 # Remove any artifacts from the component_test_cmake_as_installed_package test.
410 rm -rf programs/test/cmake_package_install/build
411 rm -f programs/test/cmake_package_install/Makefile
412 rm -f programs/test/cmake_package_install/cmake_package_install
413
414 # Restore files that may have been clobbered by the job
415 restore_backed_up_files
416}
417
418# Restore files that may have been clobbered
419restore_backed_up_files () {
420 for x in $files_to_back_up; do
421 if [[ -e "$x$backup_suffix" ]]; then
422 cp -p "$x$backup_suffix" "$x"
423 fi
424 done
425}
426
427# Final cleanup when this script exits (except when exiting on a failure
428# in non-keep-going mode).
429final_cleanup () {
430 cleanup
431
432 for x in $files_to_back_up; do
433 rm -f "$x$backup_suffix"
434 done
435}
436
437# Executed on exit. May be redefined depending on command line options.
438final_report () {
439 :
440}
441
442fatal_signal () {
443 final_cleanup
444 final_report $1
445 trap - $1
446 kill -$1 $$
447}
448
Manuel Pégourié-Gonnarde4e65aa2024-10-09 11:20:06 +0200449pre_set_signal_handlers () {
450 trap 'fatal_signal HUP' HUP
451 trap 'fatal_signal INT' INT
452 trap 'fatal_signal TERM' TERM
453}
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200454
455# Number of processors on this machine. Used as the default setting
456# for parallel make.
457all_sh_nproc ()
458{
459 {
460 nproc || # Linux
461 sysctl -n hw.ncpuonline || # NetBSD, OpenBSD
462 sysctl -n hw.ncpu || # FreeBSD
463 echo 1
464 } 2>/dev/null
465}
466
467msg()
468{
469 if [ -n "${current_component:-}" ]; then
470 current_section="${current_component#component_}: $1"
471 else
472 current_section="$1"
473 fi
474
475 if [ $QUIET -eq 1 ]; then
476 return
477 fi
478
479 echo ""
480 echo "******************************************************************"
481 echo "* $current_section "
482 printf "* "; date
483 echo "******************************************************************"
484}
485
486err_msg()
487{
488 echo "$1" >&2
489}
490
491check_tools()
492{
493 for tool in "$@"; do
494 if ! `type "$tool" >/dev/null 2>&1`; then
495 err_msg "$tool not found!"
496 exit 1
497 fi
498 done
499}
500
501pre_parse_command_line () {
502 COMMAND_LINE_COMPONENTS=
503 all_except=0
504 error_test=0
505 list_components=0
506 restore_first=0
507 no_armcc=
508
509 # Note that legacy options are ignored instead of being omitted from this
510 # list of options, so invocations that worked with previous version of
511 # all.sh will still run and work properly.
512 while [ $# -gt 0 ]; do
513 case "$1" in
514 --append-outcome) append_outcome=1;;
515 --arm-none-eabi-gcc-prefix) shift; ARM_NONE_EABI_GCC_PREFIX="$1";;
516 --arm-linux-gnueabi-gcc-prefix) shift; ARM_LINUX_GNUEABI_GCC_PREFIX="$1";;
Bence Szépkútibc8c1572024-10-27 22:46:48 +0100517 --arm-linux-gnueabihf-gcc-prefix) shift; ARM_LINUX_GNUEABIHF_GCC_PREFIX="$1";;
518 --aarch64-linux-gnu-gcc-prefix) shift; AARCH64_LINUX_GNU_GCC_PREFIX="$1";;
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200519 --armcc) no_armcc=;;
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200520 --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";;
521 --clang-earliest) shift; CLANG_EARLIEST="$1";;
522 --clang-latest) shift; CLANG_LATEST="$1";;
523 --error-test) error_test=$((error_test + 1));;
524 --except) all_except=1;;
525 --force|-f) FORCE=1;;
526 --gcc-earliest) shift; GCC_EARLIEST="$1";;
527 --gcc-latest) shift; GCC_LATEST="$1";;
528 --gnutls-cli) shift; GNUTLS_CLI="$1";;
529 --gnutls-legacy-cli) shift;; # ignored for backward compatibility
530 --gnutls-legacy-serv) shift;; # ignored for backward compatibility
531 --gnutls-serv) shift; GNUTLS_SERV="$1";;
532 --help|-h) usage; exit;;
533 --keep-going|-k) KEEP_GOING=1;;
534 --list-all-components) printf '%s\n' $ALL_COMPONENTS; exit;;
535 --list-components) list_components=1;;
536 --memory|-m) MEMORY=1;;
537 --no-append-outcome) append_outcome=0;;
538 --no-armcc) no_armcc=1;;
539 --no-force) FORCE=0;;
540 --no-keep-going) KEEP_GOING=0;;
541 --no-memory) MEMORY=0;;
542 --no-quiet) QUIET=0;;
543 --openssl) shift; OPENSSL="$1";;
544 --openssl-next) shift; OPENSSL_NEXT="$1";;
545 --outcome-file) shift; MBEDTLS_TEST_OUTCOME_FILE="$1";;
546 --out-of-source-dir) shift; OUT_OF_SOURCE_DIR="$1";;
547 --quiet|-q) QUIET=1;;
548 --random-seed) unset SEED;;
549 --release-test|-r) SEED=$RELEASE_SEED;;
550 --restore) restore_first=1;;
551 --seed|-s) shift; SEED="$1";;
552 -*)
553 echo >&2 "Unknown option: $1"
554 echo >&2 "Run $0 --help for usage."
555 exit 120
556 ;;
557 *) COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS $1";;
558 esac
559 shift
560 done
561
562 # Exclude components that are not supported on this platform.
563 SUPPORTED_COMPONENTS=
564 for component in $ALL_COMPONENTS; do
565 case $(type "support_$component" 2>&1) in
566 *' function'*)
567 if ! support_$component; then continue; fi;;
568 esac
569 SUPPORTED_COMPONENTS="$SUPPORTED_COMPONENTS $component"
570 done
571
572 if [ $list_components -eq 1 ]; then
573 printf '%s\n' $SUPPORTED_COMPONENTS
574 exit
575 fi
576
577 # With no list of components, run everything.
578 if [ -z "$COMMAND_LINE_COMPONENTS" ] && [ $restore_first -eq 0 ]; then
579 all_except=1
580 fi
581
582 # --no-armcc is a legacy option. The modern way is --except '*_armcc*'.
583 # Ignore it if components are listed explicitly on the command line.
584 if [ -n "$no_armcc" ] && [ $all_except -eq 1 ]; then
585 COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*"
586 fi
587
588 # Error out if an explicitly requested component doesn't exist.
589 if [ $all_except -eq 0 ]; then
590 unsupported=0
591 # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS
592 # only does word splitting.
593 set -f
594 for component in $COMMAND_LINE_COMPONENTS; do
595 set +f
596 # If the requested name includes a wildcard character, don't
597 # check it. Accept wildcard patterns that don't match anything.
598 case $component in
599 *[*?\[]*) continue;;
600 esac
601 case " $SUPPORTED_COMPONENTS " in
602 *" $component "*) :;;
603 *)
604 echo >&2 "Component $component was explicitly requested, but is not known or not supported."
605 unsupported=$((unsupported + 1));;
606 esac
607 done
608 set +f
609 if [ $unsupported -ne 0 ]; then
610 exit 2
611 fi
612 fi
613
614 # Build the list of components to run.
615 RUN_COMPONENTS=
616 for component in $SUPPORTED_COMPONENTS; do
617 if is_component_included "$component"; [ $? -eq $all_except ]; then
618 RUN_COMPONENTS="$RUN_COMPONENTS $component"
619 fi
620 done
621
622 unset all_except
623 unset no_armcc
624}
625
626pre_check_git () {
627 if [ $FORCE -eq 1 ]; then
628 rm -rf "$OUT_OF_SOURCE_DIR"
Manuel Pégourié-Gonnard3d411542024-10-23 09:53:54 +0200629 git checkout-index -f -q $config_files
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200630 cleanup
631 else
632
633 if [ -d "$OUT_OF_SOURCE_DIR" ]; then
634 echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
635 echo "You can either delete this directory manually, or force the test by rerunning"
636 echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
637 exit 1
638 fi
639
Manuel Pégourié-Gonnard3d411542024-10-23 09:53:54 +0200640 for config in $config_files; do
641 if ! git diff --quiet "$config"; then
642 err_msg "Warning - the configuration file '$config' has been edited. "
643 echo "You can either delete or preserve your work, or force the test by rerunning the"
644 echo "script as: $0 --force"
645 exit 1
646 fi
647 done
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200648 fi
649}
650
651pre_restore_files () {
652 # If the makefiles have been generated by a framework such as cmake,
653 # restore them from git. If the makefiles look like modifications from
654 # the ones checked into git, take care not to modify them. Whatever
655 # this function leaves behind is what the script will restore before
656 # each component.
657 case "$(head -n1 Makefile)" in
658 *[Gg]enerated*)
659 git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
660 git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile
661 ;;
662 esac
663}
664
665pre_back_up () {
666 for x in $files_to_back_up; do
667 cp -p "$x" "$x$backup_suffix"
668 done
669}
670
671pre_setup_keep_going () {
672 failure_count=0 # Number of failed components
673 last_failure_status=0 # Last failure status in this component
674
675 # See err_trap
676 previous_failure_status=0
677 previous_failed_command=
678 previous_failure_funcall_depth=0
679 unset report_failed_command
680
681 start_red=
682 end_color=
683 if [ -t 1 ]; then
684 case "${TERM:-}" in
685 *color*|cygwin|linux|rxvt*|screen|[Eex]term*)
686 start_red=$(printf '\033[31m')
687 end_color=$(printf '\033[0m')
688 ;;
689 esac
690 fi
691
692 # Keep a summary of failures in a file. We'll print it out at the end.
693 failure_summary_file=$PWD/all-sh-failures-$$.log
694 : >"$failure_summary_file"
695
696 # Whether it makes sense to keep a component going after the specified
697 # command fails (test command) or not (configure or build).
698 # This function normally receives the failing simple command
699 # ($BASH_COMMAND) as an argument, but if $report_failed_command is set,
700 # this is passed instead.
701 # This doesn't have to be 100% accurate: all failures are recorded anyway.
702 # False positives result in running things that can't be expected to
703 # work. False negatives result in things not running after something else
704 # failed even though they might have given useful feedback.
705 can_keep_going_after_failure () {
706 case "$1" in
707 "msg "*) false;;
708 "cd "*) false;;
709 "diff "*) true;;
710 *make*[\ /]tests*) false;; # make tests, make CFLAGS=-I../tests, ...
711 *test*) true;; # make test, tests/stuff, env V=v tests/stuff, ...
712 *make*check*) true;;
713 "grep "*) true;;
714 "[ "*) true;;
715 "! "*) true;;
716 *) false;;
717 esac
718 }
719
720 # This function runs if there is any error in a component.
721 # It must either exit with a nonzero status, or set
722 # last_failure_status to a nonzero value.
723 err_trap () {
724 # Save $? (status of the failing command). This must be the very
725 # first thing, before $? is overridden.
726 last_failure_status=$?
727 failed_command=${report_failed_command-$BASH_COMMAND}
728
729 if [[ $last_failure_status -eq $previous_failure_status &&
730 "$failed_command" == "$previous_failed_command" &&
731 ${#FUNCNAME[@]} == $((previous_failure_funcall_depth - 1)) ]]
732 then
733 # The same command failed twice in a row, but this time one level
734 # less deep in the function call stack. This happens when the last
735 # command of a function returns a nonzero status, and the function
736 # returns that same status. Ignore the second failure.
737 previous_failure_funcall_depth=${#FUNCNAME[@]}
738 return
739 fi
740 previous_failure_status=$last_failure_status
741 previous_failed_command=$failed_command
742 previous_failure_funcall_depth=${#FUNCNAME[@]}
743
744 text="$current_section: $failed_command -> $last_failure_status"
745 echo "${start_red}^^^^$text^^^^${end_color}" >&2
746 echo "$text" >>"$failure_summary_file"
747
748 # If the command is fatal (configure or build command), stop this
749 # component. Otherwise (test command) keep the component running
750 # (run more tests from the same build).
751 if ! can_keep_going_after_failure "$failed_command"; then
752 exit $last_failure_status
753 fi
754 }
755
756 final_report () {
757 if [ $failure_count -gt 0 ]; then
758 echo
759 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
760 echo "${start_red}FAILED: $failure_count components${end_color}"
761 cat "$failure_summary_file"
762 echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
763 elif [ -z "${1-}" ]; then
764 echo "SUCCESS :)"
765 fi
766 if [ -n "${1-}" ]; then
767 echo "Killed by SIG$1."
768 fi
769 rm -f "$failure_summary_file"
770 if [ $failure_count -gt 0 ]; then
771 exit 1
772 fi
773 }
774}
775
776# '! true' does not trigger the ERR trap. Arrange to trigger it, with
777# a reasonably informative error message (not just "$@").
778not () {
779 if "$@"; then
780 report_failed_command="! $*"
781 false
782 unset report_failed_command
783 fi
784}
785
786pre_prepare_outcome_file () {
787 case "$MBEDTLS_TEST_OUTCOME_FILE" in
788 [!/]*) MBEDTLS_TEST_OUTCOME_FILE="$PWD/$MBEDTLS_TEST_OUTCOME_FILE";;
789 esac
790 if [ -n "$MBEDTLS_TEST_OUTCOME_FILE" ] && [ "$append_outcome" -eq 0 ]; then
791 rm -f "$MBEDTLS_TEST_OUTCOME_FILE"
792 fi
793}
794
795pre_print_configuration () {
796 if [ $QUIET -eq 1 ]; then
797 return
798 fi
799
800 msg "info: $0 configuration"
801 echo "MEMORY: $MEMORY"
802 echo "FORCE: $FORCE"
803 echo "MBEDTLS_TEST_OUTCOME_FILE: ${MBEDTLS_TEST_OUTCOME_FILE:-(none)}"
804 echo "SEED: ${SEED-"UNSET"}"
805 echo
806 echo "OPENSSL: $OPENSSL"
807 echo "OPENSSL_NEXT: $OPENSSL_NEXT"
808 echo "GNUTLS_CLI: $GNUTLS_CLI"
809 echo "GNUTLS_SERV: $GNUTLS_SERV"
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200810 echo "ARMC6_BIN_DIR: $ARMC6_BIN_DIR"
811}
812
813# Make sure the tools we need are available.
814pre_check_tools () {
815 # Build the list of variables to pass to output_env.sh.
816 set env
817
818 case " $RUN_COMPONENTS " in
819 # Require OpenSSL and GnuTLS if running any tests (as opposed to
820 # only doing builds). Not all tests run OpenSSL and GnuTLS, but this
821 # is a good enough approximation in practice.
822 *" test_"* | *" release_test_"*)
823 # To avoid setting OpenSSL and GnuTLS for each call to compat.sh
824 # and ssl-opt.sh, we just export the variables they require.
825 export OPENSSL="$OPENSSL"
826 export GNUTLS_CLI="$GNUTLS_CLI"
827 export GNUTLS_SERV="$GNUTLS_SERV"
828 # Avoid passing --seed flag in every call to ssl-opt.sh
829 if [ -n "${SEED-}" ]; then
830 export SEED
831 fi
832 set "$@" OPENSSL="$OPENSSL"
833 set "$@" GNUTLS_CLI="$GNUTLS_CLI" GNUTLS_SERV="$GNUTLS_SERV"
834 check_tools "$OPENSSL" "$OPENSSL_NEXT" \
835 "$GNUTLS_CLI" "$GNUTLS_SERV"
836 ;;
837 esac
838
839 case " $RUN_COMPONENTS " in
840 *_doxygen[_\ ]*) check_tools "doxygen" "dot";;
841 esac
842
843 case " $RUN_COMPONENTS " in
844 *_arm_none_eabi_gcc[_\ ]*) check_tools "${ARM_NONE_EABI_GCC_PREFIX}gcc";;
845 esac
846
847 case " $RUN_COMPONENTS " in
848 *_mingw[_\ ]*) check_tools "i686-w64-mingw32-gcc";;
849 esac
850
851 case " $RUN_COMPONENTS " in
852 *" test_zeroize "*) check_tools "gdb";;
853 esac
854
855 case " $RUN_COMPONENTS " in
856 *_armcc*)
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200857 ARMC6_CC="$ARMC6_BIN_DIR/armclang"
858 ARMC6_AR="$ARMC6_BIN_DIR/armar"
859 ARMC6_FROMELF="$ARMC6_BIN_DIR/fromelf"
Manuel Pégourié-Gonnard705690a2024-10-29 11:39:41 +0100860 check_tools "$ARMC6_CC" "$ARMC6_AR" "$ARMC6_FROMELF";;
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200861 esac
862
863 # past this point, no call to check_tool, only printing output
864 if [ $QUIET -eq 1 ]; then
865 return
866 fi
867
868 msg "info: output_env.sh"
869 case $RUN_COMPONENTS in
870 *_armcc*)
Manuel Pégourié-Gonnard705690a2024-10-29 11:39:41 +0100871 set "$@" ARMC6_CC="$ARMC6_CC" RUN_ARMCC=1;;
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200872 *) set "$@" RUN_ARMCC=0;;
873 esac
Manuel Pégourié-Gonnard8da0e9e2024-10-23 09:42:47 +0200874 # Use a path relative to the currently-sourced file.
875 "$@" "${BASH_SOURCE%/*}"/../../scripts/output_env.sh
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200876}
877
878pre_generate_files() {
879 # since make doesn't have proper dependencies, remove any possibly outdate
880 # file that might be around before generating fresh ones
881 make neat
882 if [ $QUIET -eq 1 ]; then
883 make generated_files >/dev/null
884 else
885 make generated_files
886 fi
887}
888
889pre_load_helpers () {
Manuel Pégourié-Gonnard8da0e9e2024-10-23 09:42:47 +0200890 # Use a path relative to the currently-sourced file.
891 test_script_dir="${BASH_SOURCE%/*}"
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200892 source "$test_script_dir"/all-helpers.sh
893}
894
895################################################################
896#### Termination
897################################################################
898
899post_report () {
900 msg "Done, cleaning up"
901 final_cleanup
902
903 final_report
904}
905
906################################################################
907#### Run all the things
908################################################################
909
910# Function invoked by --error-test to test error reporting.
911pseudo_component_error_test () {
912 msg "Testing error reporting $error_test_i"
913 if [ $KEEP_GOING -ne 0 ]; then
914 echo "Expect three failing commands."
915 fi
916 # If the component doesn't run in a subshell, changing error_test_i to an
917 # invalid integer will cause an error in the loop that runs this function.
918 error_test_i=this_should_not_be_used_since_the_component_runs_in_a_subshell
919 # Expected error: 'grep non_existent /dev/null -> 1'
920 grep non_existent /dev/null
921 # Expected error: '! grep -q . tests/scripts/all.sh -> 1'
922 not grep -q . "$0"
923 # Expected error: 'make unknown_target -> 2'
924 make unknown_target
925 false "this should not be executed"
926}
927
928# Run one component and clean up afterwards.
929run_component () {
930 current_component="$1"
931 export MBEDTLS_TEST_CONFIGURATION="$current_component"
932
933 # Unconditionally create a seedfile that's sufficiently long.
934 # Do this before each component, because a previous component may
935 # have messed it up or shortened it.
936 local dd_cmd
937 dd_cmd=(dd if=/dev/urandom of=./tests/seedfile bs=64 count=1)
938 case $OSTYPE in
939 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
940 esac
941 "${dd_cmd[@]}"
942
943 if [ -d tf-psa-crypto ]; then
944 dd_cmd=(dd if=/dev/urandom of=./tf-psa-crypto/tests/seedfile bs=64 count=1)
945 case $OSTYPE in
946 linux*|freebsd*|openbsd*) dd_cmd+=(status=none)
947 esac
948 "${dd_cmd[@]}"
949 fi
950
951 # Run the component in a subshell, with error trapping and output
952 # redirection set up based on the relevant options.
953 if [ $KEEP_GOING -eq 1 ]; then
954 # We want to keep running if the subshell fails, so 'set -e' must
955 # be off when the subshell runs.
956 set +e
957 fi
958 (
959 if [ $QUIET -eq 1 ]; then
960 # msg() will be silenced, so just print the component name here.
961 echo "${current_component#component_}"
962 exec >/dev/null
963 fi
964 if [ $KEEP_GOING -eq 1 ]; then
965 # Keep "set -e" off, and run an ERR trap instead to record failures.
966 set -E
967 trap err_trap ERR
968 fi
969 # The next line is what runs the component
970 "$@"
971 if [ $KEEP_GOING -eq 1 ]; then
972 trap - ERR
973 exit $last_failure_status
974 fi
975 )
976 component_status=$?
977 if [ $KEEP_GOING -eq 1 ]; then
978 set -e
979 if [ $component_status -ne 0 ]; then
980 failure_count=$((failure_count + 1))
981 fi
982 fi
983
984 # Restore the build tree to a clean state.
985 cleanup
986 unset current_component
987}
988
989################################################################
990#### Main
991################################################################
992
993main () {
994 # Preliminary setup
995 pre_set_shell_options
Manuel Pégourié-Gonnarde4e65aa2024-10-09 11:20:06 +0200996 pre_set_signal_handlers
Manuel Pégourié-Gonnard1cb8ee82024-10-03 12:55:52 +0200997 pre_check_environment
998 pre_load_helpers
999 pre_load_components
1000 pre_initialize_variables
1001 pre_parse_command_line "$@"
1002
1003 setup_quiet_wrappers
1004 pre_check_git
1005 pre_restore_files
1006 pre_back_up
1007
1008 build_status=0
1009 if [ $KEEP_GOING -eq 1 ]; then
1010 pre_setup_keep_going
1011 fi
1012 pre_prepare_outcome_file
1013 pre_print_configuration
1014 pre_check_tools
1015 cleanup
1016 if in_mbedtls_repo; then
1017 pre_generate_files
1018 fi
1019
1020 # Run the requested tests.
1021 for ((error_test_i=1; error_test_i <= error_test; error_test_i++)); do
1022 run_component pseudo_component_error_test
1023 done
1024 unset error_test_i
1025 for component in $RUN_COMPONENTS; do
1026 run_component "component_$component"
1027 done
1028
1029 # We're done.
1030 post_report
1031}