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