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