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