Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / tests / scripts / all.sh
1 #! /usr/bin/env sh
2
3 # all.sh
4 #
5 # This file is part of mbed TLS (https://tls.mbed.org)
6 #
7 # Copyright (c) 2014-2017, ARM Limited, All Rights Reserved
8
9
10
11 ################################################################
12 #### Documentation
13 ################################################################
14
15 # Purpose
16 # -------
17 #
18 # To run all tests possible or available on the platform.
19 #
20 # Notes for users
21 # ---------------
22 #
23 # Warning: the test is destructive. It includes various build modes and
24 # configurations, and can and will arbitrarily change the current CMake
25 # configuration. The following files must be committed into git:
26 #    * include/mbedtls/config.h
27 #    * Makefile, library/Makefile, programs/Makefile, tests/Makefile
28 # After running this script, the CMake cache will be lost and CMake
29 # will no longer be initialised.
30 #
31 # The script assumes the presence of a number of tools:
32 #   * Basic Unix tools (Windows users note: a Unix-style find must be before
33 #     the Windows find in the PATH)
34 #   * Perl
35 #   * GNU Make
36 #   * CMake
37 #   * GCC and Clang (recent enough for using ASan with gcc and MemSan with clang, or valgrind)
38 #   * G++
39 #   * arm-gcc and mingw-gcc
40 #   * ArmCC 5 and ArmCC 6, unless invoked with --no-armcc
41 #   * OpenSSL and GnuTLS command line tools, recent enough for the
42 #     interoperability tests. If they don't support SSLv3 then a legacy
43 #     version of these tools must be present as well (search for LEGACY
44 #     below).
45 # See the invocation of check_tools below for details.
46 #
47 # This script must be invoked from the toplevel directory of a git
48 # working copy of Mbed TLS.
49 #
50 # Note that the output is not saved. You may want to run
51 #   script -c tests/scripts/all.sh
52 # or
53 #   tests/scripts/all.sh >all.log 2>&1
54 #
55 # Notes for maintainers
56 # ---------------------
57 #
58 # The bulk of the code is organized into functions that follow one of the
59 # following naming conventions:
60 #  * pre_XXX: things to do before running the tests, in order.
61 #  * component_XXX: independent components. They can be run in any order.
62 #      * component_check_XXX: quick tests that aren't worth parallelizing.
63 #      * component_build_XXX: build things but don't run them.
64 #      * component_test_XXX: build and test.
65 #  * support_XXX: if support_XXX exists and returns false then
66 #    component_XXX is not run by default.
67 #  * post_XXX: things to do after running the tests.
68 #  * other: miscellaneous support functions.
69 #
70 # Each component must start by invoking `msg` with a short informative message.
71 #
72 # The framework performs some cleanup tasks after each component. This
73 # means that components can assume that the working directory is in a
74 # cleaned-up state, and don't need to perform the cleanup themselves.
75 # * Run `make clean`.
76 # * Restore `include/mbedtks/config.h` from a backup made before running
77 #   the component.
78 # * Check out `Makefile`, `library/Makefile`, `programs/Makefile` and
79 #   `tests/Makefile` from git. This cleans up after an in-tree use of
80 #   CMake.
81 #
82 # Any command that is expected to fail must be protected so that the
83 # script keeps running in --keep-going mode despite `set -e`. In keep-going
84 # mode, if a protected command fails, this is logged as a failure and the
85 # script will exit with a failure status once it has run all components.
86 # Commands can be protected in any of the following ways:
87 # * `make` is a function which runs the `make` command with protection.
88 #   Note that you must write `make VAR=value`, not `VAR=value make`,
89 #   because the `VAR=value make` syntax doesn't work with functions.
90 # * Put `report_status` before the command to protect it.
91 # * Put `if_build_successful` before a command. This protects it, and
92 #   additionally skips it if a prior invocation of `make` in the same
93 #   component failed.
94 #
95 # The tests are roughly in order from fastest to slowest. This doesn't
96 # have to be exact, but in general you should add slower tests towards
97 # the end and fast checks near the beginning.
98
99
100
101 ################################################################
102 #### Initialization and command line parsing
103 ################################################################
104
105 # Abort on errors (and uninitialised variables)
106 set -eu
107
108 pre_check_environment () {
109     if [ -d library -a -d include -a -d tests ]; then :; else
110         echo "Must be run from mbed TLS root" >&2
111         exit 1
112     fi
113 }
114
115 pre_initialize_variables () {
116     CONFIG_H='include/mbedtls/config.h'
117     CONFIG_BAK="$CONFIG_H.bak"
118
119     MEMORY=0
120     FORCE=0
121     KEEP_GOING=0
122
123     # Default commands, can be overridden by the environment
124     : ${OPENSSL:="openssl"}
125     : ${OPENSSL_LEGACY:="$OPENSSL"}
126     : ${OPENSSL_NEXT:="$OPENSSL"}
127     : ${GNUTLS_CLI:="gnutls-cli"}
128     : ${GNUTLS_SERV:="gnutls-serv"}
129     : ${GNUTLS_LEGACY_CLI:="$GNUTLS_CLI"}
130     : ${GNUTLS_LEGACY_SERV:="$GNUTLS_SERV"}
131     : ${OUT_OF_SOURCE_DIR:=./mbedtls_out_of_source_build}
132     : ${ARMC5_BIN_DIR:=/usr/bin}
133     : ${ARMC6_BIN_DIR:=/usr/bin}
134
135     # if MAKEFLAGS is not set add the -j option to speed up invocations of make
136     if [ -z "${MAKEFLAGS+set}" ]; then
137         export MAKEFLAGS="-j"
138     fi
139
140     # Gather the list of available components. These are the functions
141     # defined in this script whose name starts with "component_".
142     # Parse the script with sed, because in sh there is no way to list
143     # defined functions.
144     ALL_COMPONENTS=$(sed -n 's/^ *component_\([0-9A-Z_a-z]*\) *().*/\1/p' <"$0")
145
146     # Exclude components that are not supported on this platform.
147     SUPPORTED_COMPONENTS=
148     for component in $ALL_COMPONENTS; do
149         case $(type "support_$component" 2>&1) in
150             *' function'*)
151                 if ! support_$component; then continue; fi;;
152         esac
153         SUPPORTED_COMPONENTS="$SUPPORTED_COMPONENTS $component"
154     done
155 }
156
157 # Test whether the component $1 is included in the command line patterns.
158 is_component_included()
159 {
160     set -f
161     for pattern in $COMMAND_LINE_COMPONENTS; do
162         set +f
163         case ${1#component_} in $pattern) return 0;; esac
164     done
165     set +f
166     return 1
167 }
168
169 usage()
170 {
171     cat <<EOF
172 Usage: $0 [OPTION]... [COMPONENT]...
173 Run mbedtls release validation tests.
174 By default, run all tests. With one or more COMPONENT, run only those.
175 COMPONENT can be the name of a component or a shell wildcard pattern.
176
177 Examples:
178   $0 "check_*"
179     Run all sanity checks.
180   $0 --no-armcc --except test_memsan
181     Run everything except builds that require armcc and MemSan.
182
183 Special options:
184   -h|--help             Print this help and exit.
185   --list-all-components List all available test components and exit.
186   --list-components     List components supported on this platform and exit.
187
188 General options:
189   -f|--force            Force the tests to overwrite any modified files.
190   -k|--keep-going       Run all tests and report errors at the end.
191   -m|--memory           Additional optional memory tests.
192      --armcc            Run ARM Compiler builds (on by default).
193      --except           Exclude the COMPONENTs listed on the command line,
194                         instead of running only those.
195      --no-armcc         Skip ARM Compiler builds.
196      --no-force         Refuse to overwrite modified files (default).
197      --no-keep-going    Stop at the first error (default).
198      --no-memory        No additional memory tests (default).
199      --out-of-source-dir=<path>  Directory used for CMake out-of-source build tests.
200      --random-seed      Use a random seed value for randomized tests (default).
201   -r|--release-test     Run this script in release mode. This fixes the seed value to 1.
202   -s|--seed             Integer seed value to use for this test run.
203
204 Tool path options:
205      --armc5-bin-dir=<ARMC5_bin_dir_path>       ARM Compiler 5 bin directory.
206      --armc6-bin-dir=<ARMC6_bin_dir_path>       ARM Compiler 6 bin directory.
207      --gnutls-cli=<GnuTLS_cli_path>             GnuTLS client executable to use for most tests.
208      --gnutls-serv=<GnuTLS_serv_path>           GnuTLS server executable to use for most tests.
209      --gnutls-legacy-cli=<GnuTLS_cli_path>      GnuTLS client executable to use for legacy tests.
210      --gnutls-legacy-serv=<GnuTLS_serv_path>    GnuTLS server executable to use for legacy tests.
211      --openssl=<OpenSSL_path>                   OpenSSL executable to use for most tests.
212      --openssl-legacy=<OpenSSL_path>            OpenSSL executable to use for legacy tests e.g. SSLv3.
213      --openssl-next=<OpenSSL_path>              OpenSSL executable to use for recent things like ARIA
214 EOF
215 }
216
217 # remove built files as well as the cmake cache/config
218 cleanup()
219 {
220     if [ -n "${MBEDTLS_ROOT_DIR+set}" ]; then
221         cd "$MBEDTLS_ROOT_DIR"
222     fi
223
224     command make clean
225     cd crypto
226     command make clean
227     cd ..
228
229     # Remove CMake artefacts
230     find . -name .git -prune -o \
231            -iname CMakeFiles -exec rm -rf {} \+ -o \
232            \( -iname cmake_install.cmake -o \
233               -iname CTestTestfile.cmake -o \
234               -iname CMakeCache.txt \) -exec rm {} \+
235     # Recover files overwritten by in-tree CMake builds
236     rm -f include/Makefile include/mbedtls/Makefile programs/*/Makefile
237     git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile
238     git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile
239     cd crypto
240     rm -f include/Makefile include/mbedtls/Makefile programs/*/Makefile
241     git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile
242     git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile
243     cd ..
244
245     # Remove any artifacts from the component_test_cmake_as_subdirectory test.
246     rm -rf programs/test/cmake_subproject/build
247     rm -f programs/test/cmake_subproject/Makefile
248     rm -f programs/test/cmake_subproject/cmake_subproject
249
250     if [ -f "$CONFIG_BAK" ]; then
251         mv "$CONFIG_BAK" "$CONFIG_H"
252     fi
253 }
254
255 # Executed on exit. May be redefined depending on command line options.
256 final_report () {
257     :
258 }
259
260 fatal_signal () {
261     cleanup
262     final_report $1
263     trap - $1
264     kill -$1 $$
265 }
266
267 trap 'fatal_signal HUP' HUP
268 trap 'fatal_signal INT' INT
269 trap 'fatal_signal TERM' TERM
270
271 msg()
272 {
273     if [ -n "${current_component:-}" ]; then
274         current_section="${current_component#component_}: $1"
275     else
276         current_section="$1"
277     fi
278     echo ""
279     echo "******************************************************************"
280     echo "* $current_section "
281     printf "* "; date
282     echo "******************************************************************"
283 }
284
285 armc6_build_test()
286 {
287     FLAGS="$1"
288
289     msg "build: ARM Compiler 6 ($FLAGS), make"
290     ARM_TOOL_VARIANT="ult" CC="$ARMC6_CC" AR="$ARMC6_AR" CFLAGS="$FLAGS" \
291                     WARNING_CFLAGS='-xc -std=c99' make lib
292     make clean
293 }
294
295 err_msg()
296 {
297     echo "$1" >&2
298 }
299
300 check_tools()
301 {
302     for TOOL in "$@"; do
303         if ! `type "$TOOL" >/dev/null 2>&1`; then
304             err_msg "$TOOL not found!"
305             exit 1
306         fi
307     done
308 }
309
310 check_headers_in_cpp () {
311     ls include/mbedtls | grep "\.h$" >headers.txt
312     <programs/test/cpp_dummy_build.cpp sed -n 's/"$//; s!^#include "mbedtls/!!p' |
313     sort |
314     diff headers.txt -
315     rm headers.txt
316 }
317
318 pre_parse_command_line () {
319     COMMAND_LINE_COMPONENTS=
320     all_except=0
321     no_armcc=
322
323     while [ $# -gt 0 ]; do
324         case "$1" in
325             --armcc) no_armcc=;;
326             --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";;
327             --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";;
328             --except) all_except=1;;
329             --force|-f) FORCE=1;;
330             --gnutls-cli) shift; GNUTLS_CLI="$1";;
331             --gnutls-legacy-cli) shift; GNUTLS_LEGACY_CLI="$1";;
332             --gnutls-legacy-serv) shift; GNUTLS_LEGACY_SERV="$1";;
333             --gnutls-serv) shift; GNUTLS_SERV="$1";;
334             --help|-h) usage; exit;;
335             --keep-going|-k) KEEP_GOING=1;;
336             --list-all-components) printf '%s\n' $ALL_COMPONENTS; exit;;
337             --list-components) printf '%s\n' $SUPPORTED_COMPONENTS; exit;;
338             --memory|-m) MEMORY=1;;
339             --no-armcc) no_armcc=1;;
340             --no-force) FORCE=0;;
341             --no-keep-going) KEEP_GOING=0;;
342             --no-memory) MEMORY=0;;
343             --openssl) shift; OPENSSL="$1";;
344             --openssl-legacy) shift; OPENSSL_LEGACY="$1";;
345             --openssl-next) shift; OPENSSL_NEXT="$1";;
346             --out-of-source-dir) shift; OUT_OF_SOURCE_DIR="$1";;
347             --random-seed) unset SEED;;
348             --release-test|-r) SEED=1;;
349             --seed|-s) shift; SEED="$1";;
350             -*)
351                 echo >&2 "Unknown option: $1"
352                 echo >&2 "Run $0 --help for usage."
353                 exit 120
354                 ;;
355             *) COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS $1";;
356         esac
357         shift
358     done
359
360     # With no list of components, run everything.
361     if [ -z "$COMMAND_LINE_COMPONENTS" ]; then
362         all_except=1
363     fi
364
365     # --no-armcc is a legacy option. The modern way is --except '*_armcc*'.
366     # Ignore it if components are listed explicitly on the command line.
367     if [ -n "$no_armcc" ] && [ $all_except -eq 1 ]; then
368         COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*"
369     fi
370
371     # Build the list of components to run.
372     RUN_COMPONENTS=
373     for component in $SUPPORTED_COMPONENTS; do
374         if is_component_included "$component"; [ $? -eq $all_except ]; then
375             RUN_COMPONENTS="$RUN_COMPONENTS $component"
376         fi
377     done
378
379     unset all_except
380     unset no_armcc
381 }
382
383 pre_check_git () {
384     if [ $FORCE -eq 1 ]; then
385         rm -rf "$OUT_OF_SOURCE_DIR"
386         git checkout-index -f -q $CONFIG_H
387         cleanup
388     else
389
390         if [ -d "$OUT_OF_SOURCE_DIR" ]; then
391             echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
392             echo "You can either delete this directory manually, or force the test by rerunning"
393             echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
394             exit 1
395         fi
396
397         if ! git diff --quiet include/mbedtls/config.h; then
398             err_msg "Warning - the configuration file 'include/mbedtls/config.h' has been edited. "
399             echo "You can either delete or preserve your work, or force the test by rerunning the"
400             echo "script as: $0 --force"
401             exit 1
402         fi
403     fi
404     if ! [ -f crypto/Makefile ]; then
405         echo "Please initialize the crypto submodule" >&2
406         exit 1
407     fi
408 }
409
410 pre_check_seedfile () {
411     if [ ! -f "./tests/seedfile" ]; then
412         dd if=/dev/urandom of=./tests/seedfile bs=32 count=1
413     fi
414     if [ ! -f "./crypto/tests/seedfile" ]; then
415         dd if=/dev/urandom of=./crypto/tests/seedfile bs=32 count=1
416     fi
417 }
418
419 pre_setup_keep_going () {
420     failure_summary=
421     failure_count=0
422     start_red=
423     end_color=
424     if [ -t 1 ]; then
425         case "${TERM:-}" in
426             *color*|cygwin|linux|rxvt*|screen|[Eex]term*)
427                 start_red=$(printf '\033[31m')
428                 end_color=$(printf '\033[0m')
429                 ;;
430         esac
431     fi
432     record_status () {
433         if "$@"; then
434             last_status=0
435         else
436             last_status=$?
437             text="$current_section: $* -> $last_status"
438             failure_summary="$failure_summary
439 $text"
440             failure_count=$((failure_count + 1))
441             echo "${start_red}^^^^$text^^^^${end_color}"
442         fi
443     }
444     make () {
445         case "$*" in
446             *test|*check)
447                 if [ $build_status -eq 0 ]; then
448                     record_status command make "$@"
449                 else
450                     echo "(skipped because the build failed)"
451                 fi
452                 ;;
453             *)
454                 record_status command make "$@"
455                 build_status=$last_status
456                 ;;
457         esac
458     }
459     final_report () {
460         if [ $failure_count -gt 0 ]; then
461             echo
462             echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
463             echo "${start_red}FAILED: $failure_count${end_color}$failure_summary"
464             echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
465             exit 1
466         elif [ -z "${1-}" ]; then
467             echo "SUCCESS :)"
468         fi
469         if [ -n "${1-}" ]; then
470             echo "Killed by SIG$1."
471         fi
472     }
473 }
474
475 if_build_succeeded () {
476     if [ $build_status -eq 0 ]; then
477         record_status "$@"
478     fi
479 }
480
481 # to be used instead of ! for commands run with
482 # record_status or if_build_succeeded
483 not() {
484     ! "$@"
485 }
486
487 pre_print_configuration () {
488     msg "info: $0 configuration"
489     echo "MEMORY: $MEMORY"
490     echo "FORCE: $FORCE"
491     echo "SEED: ${SEED-"UNSET"}"
492     echo "OPENSSL: $OPENSSL"
493     echo "OPENSSL_LEGACY: $OPENSSL_LEGACY"
494     echo "OPENSSL_NEXT: $OPENSSL_NEXT"
495     echo "GNUTLS_CLI: $GNUTLS_CLI"
496     echo "GNUTLS_SERV: $GNUTLS_SERV"
497     echo "GNUTLS_LEGACY_CLI: $GNUTLS_LEGACY_CLI"
498     echo "GNUTLS_LEGACY_SERV: $GNUTLS_LEGACY_SERV"
499     echo "ARMC5_BIN_DIR: $ARMC5_BIN_DIR"
500     echo "ARMC6_BIN_DIR: $ARMC6_BIN_DIR"
501 }
502
503 # Make sure the tools we need are available.
504 pre_check_tools () {
505     # Build the list of variables to pass to output_env.sh.
506     set env
507
508     case " $RUN_COMPONENTS " in
509         # Require OpenSSL and GnuTLS if running any tests (as opposed to
510         # only doing builds). Not all tests run OpenSSL and GnuTLS, but this
511         # is a good enough approximation in practice.
512         *" test_"*)
513             # To avoid setting OpenSSL and GnuTLS for each call to compat.sh
514             # and ssl-opt.sh, we just export the variables they require.
515             export OPENSSL_CMD="$OPENSSL"
516             export GNUTLS_CLI="$GNUTLS_CLI"
517             export GNUTLS_SERV="$GNUTLS_SERV"
518             # Avoid passing --seed flag in every call to ssl-opt.sh
519             if [ -n "${SEED-}" ]; then
520                 export SEED
521             fi
522             set "$@" OPENSSL="$OPENSSL" OPENSSL_LEGACY="$OPENSSL_LEGACY"
523             set "$@" GNUTLS_CLI="$GNUTLS_CLI" GNUTLS_SERV="$GNUTLS_SERV"
524             set "$@" GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI"
525             set "$@" GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV"
526             check_tools "$OPENSSL" "$OPENSSL_LEGACY" "$OPENSSL_NEXT" \
527                         "$GNUTLS_CLI" "$GNUTLS_SERV" \
528                         "$GNUTLS_LEGACY_CLI" "$GNUTLS_LEGACY_SERV"
529             ;;
530     esac
531
532     case " $RUN_COMPONENTS " in
533         *_doxygen[_\ ]*) check_tools "doxygen" "dot";;
534     esac
535
536     case " $RUN_COMPONENTS " in
537         *_arm_none_eabi_gcc[_\ ]*) check_tools "arm-none-eabi-gcc";;
538     esac
539
540     case " $RUN_COMPONENTS " in
541         *_mingw[_\ ]*) check_tools "i686-w64-mingw32-gcc";;
542     esac
543
544     case " $RUN_COMPONENTS " in
545         *" test_zeroize "*) check_tools "gdb";;
546     esac
547
548     case " $RUN_COMPONENTS " in
549         *_armcc*)
550             ARMC5_CC="$ARMC5_BIN_DIR/armcc"
551             ARMC5_AR="$ARMC5_BIN_DIR/armar"
552             ARMC6_CC="$ARMC6_BIN_DIR/armclang"
553             ARMC6_AR="$ARMC6_BIN_DIR/armar"
554             check_tools "$ARMC5_CC" "$ARMC5_AR" "$ARMC6_CC" "$ARMC6_AR";;
555     esac
556
557     msg "info: output_env.sh"
558     case $RUN_COMPONENTS in
559         *_armcc*)
560             set "$@" ARMC5_CC="$ARMC5_CC" ARMC6_CC="$ARMC6_CC" RUN_ARMCC=1;;
561         *) set "$@" RUN_ARMCC=0;;
562     esac
563     "$@" scripts/output_env.sh
564 }
565
566
567
568 ################################################################
569 #### Basic checks
570 ################################################################
571
572 #
573 # Test Suites to be executed
574 #
575 # The test ordering tries to optimize for the following criteria:
576 # 1. Catch possible problems early, by running first tests that run quickly
577 #    and/or are more likely to fail than others (eg I use Clang most of the
578 #    time, so start with a GCC build).
579 # 2. Minimize total running time, by avoiding useless rebuilds
580 #
581 # Indicative running times are given for reference.
582
583 component_check_recursion () {
584     msg "test: recursion.pl" # < 1s
585     record_status tests/scripts/recursion.pl library/*.c
586 }
587
588 component_check_generated_files () {
589     msg "test: freshness of generated source files" # < 1s
590     record_status tests/scripts/check-generated-files.sh
591 }
592
593 component_check_doxy_blocks () {
594     msg "test: doxygen markup outside doxygen blocks" # < 1s
595     record_status tests/scripts/check-doxy-blocks.pl
596 }
597
598 component_check_files () {
599     msg "test: check-files.py" # < 1s
600     record_status tests/scripts/check-files.py
601 }
602
603 component_check_names () {
604     msg "test/build: declared and exported names" # < 3s
605     record_status tests/scripts/check-names.sh -v
606 }
607
608 component_check_doxygen_warnings () {
609     msg "test: doxygen warnings" # ~ 3s
610     record_status tests/scripts/doxygen.sh
611 }
612
613
614
615 ################################################################
616 #### Build and test many configurations and targets
617 ################################################################
618
619 component_test_default_out_of_box () {
620     msg "build: make, default config (out-of-box)" # ~1min
621     make
622
623     msg "test: main suites make, default config (out-of-box)" # ~10s
624     make test
625
626     msg "selftest: make, default config (out-of-box)" # ~10s
627     programs/test/selftest
628 }
629
630 component_test_default_cmake_gcc_asan () {
631     msg "build: cmake, gcc, ASan" # ~ 1 min 50s
632     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
633     make
634
635     msg "test: main suites (inc. selftests) (ASan build)" # ~ 50s
636     make test
637
638     msg "test: ssl-opt.sh (ASan build)" # ~ 1 min
639     if_build_succeeded tests/ssl-opt.sh
640
641     msg "test: compat.sh (ASan build)" # ~ 6 min
642     if_build_succeeded tests/compat.sh
643 }
644
645 component_test_ref_configs () {
646     msg "test/build: ref-configs (ASan build)" # ~ 6 min 20s
647     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
648     record_status tests/scripts/test-ref-configs.pl
649 }
650
651 component_test_sslv3 () {
652     msg "build: Default + SSLv3 (ASan build)" # ~ 6 min
653     scripts/config.pl set MBEDTLS_SSL_PROTO_SSL3
654     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
655     make
656
657     msg "test: SSLv3 - main suites (inc. selftests) (ASan build)" # ~ 50s
658     make test
659
660     msg "build: SSLv3 - compat.sh (ASan build)" # ~ 6 min
661     if_build_succeeded tests/compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2'
662     if_build_succeeded env OPENSSL_CMD="$OPENSSL_LEGACY" tests/compat.sh -m 'ssl3'
663
664     msg "build: SSLv3 - ssl-opt.sh (ASan build)" # ~ 6 min
665     if_build_succeeded tests/ssl-opt.sh
666 }
667
668 component_test_no_renegotiation () {
669     msg "build: Default + !MBEDTLS_SSL_RENEGOTIATION (ASan build)" # ~ 6 min
670     scripts/config.pl unset MBEDTLS_SSL_RENEGOTIATION
671     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
672     make
673
674     msg "test: !MBEDTLS_SSL_RENEGOTIATION - main suites (inc. selftests) (ASan build)" # ~ 50s
675     make test
676
677     msg "test: !MBEDTLS_SSL_RENEGOTIATION - ssl-opt.sh (ASan build)" # ~ 6 min
678     if_build_succeeded tests/ssl-opt.sh
679 }
680
681 component_test_rsa_no_crt () {
682     msg "build: Default + RSA_NO_CRT (ASan build)" # ~ 6 min
683     scripts/config.pl set MBEDTLS_RSA_NO_CRT
684     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
685     make
686
687     msg "test: RSA_NO_CRT - main suites (inc. selftests) (ASan build)" # ~ 50s
688     make test
689
690     msg "test: RSA_NO_CRT - RSA-related part of ssl-opt.sh (ASan build)" # ~ 5s
691     if_build_succeeded tests/ssl-opt.sh -f RSA
692
693     msg "test: RSA_NO_CRT - RSA-related part of compat.sh (ASan build)" # ~ 3 min
694     if_build_succeeded tests/compat.sh -t RSA
695 }
696
697 component_test_small_ssl_out_content_len () {
698     msg "build: small SSL_OUT_CONTENT_LEN (ASan build)"
699     scripts/config.pl set MBEDTLS_SSL_IN_CONTENT_LEN 16384
700     scripts/config.pl set MBEDTLS_SSL_OUT_CONTENT_LEN 4096
701     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
702     make
703
704     msg "test: small SSL_OUT_CONTENT_LEN - ssl-opt.sh MFL and large packet tests"
705     if_build_succeeded tests/ssl-opt.sh -f "Max fragment\|Large packet"
706 }
707
708 component_test_small_ssl_in_content_len () {
709     msg "build: small SSL_IN_CONTENT_LEN (ASan build)"
710     scripts/config.pl set MBEDTLS_SSL_IN_CONTENT_LEN 4096
711     scripts/config.pl set MBEDTLS_SSL_OUT_CONTENT_LEN 16384
712     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
713     make
714
715     msg "test: small SSL_IN_CONTENT_LEN - ssl-opt.sh MFL tests"
716     if_build_succeeded tests/ssl-opt.sh -f "Max fragment"
717 }
718
719 component_test_small_ssl_dtls_max_buffering () {
720     msg "build: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #0"
721     scripts/config.pl set MBEDTLS_SSL_DTLS_MAX_BUFFERING 1000
722     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
723     make
724
725     msg "test: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #0 - ssl-opt.sh specific reordering test"
726     if_build_succeeded tests/ssl-opt.sh -f "DTLS reordering: Buffer out-of-order hs msg before reassembling next, free buffered msg"
727 }
728
729 component_test_small_mbedtls_ssl_dtls_max_buffering () {
730     msg "build: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #1"
731     scripts/config.pl set MBEDTLS_SSL_DTLS_MAX_BUFFERING 240
732     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
733     make
734
735     msg "test: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #1 - ssl-opt.sh specific reordering test"
736     if_build_succeeded tests/ssl-opt.sh -f "DTLS reordering: Buffer encrypted Finished message, drop for fragmented NewSessionTicket"
737 }
738
739 component_test_full_cmake_clang () {
740     msg "build: cmake, full config, clang" # ~ 50s
741     scripts/config.pl full
742     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # too slow for tests
743     CC=clang cmake -D CMAKE_BUILD_TYPE:String=Check -D ENABLE_TESTING=On .
744     make
745
746     msg "test: main suites (full config)" # ~ 5s
747     make test
748
749     msg "test: ssl-opt.sh default, ECJPAKE, SSL async (full config)" # ~ 1s
750     if_build_succeeded tests/ssl-opt.sh -f 'Default\|ECJPAKE\|SSL async private'
751
752     msg "test: compat.sh RC4, DES, 3DES & NULL (full config)" # ~ 2 min
753     if_build_succeeded env OPENSSL_CMD="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" tests/compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR'
754
755     msg "test: compat.sh ARIA + ChachaPoly"
756     if_build_succeeded env OPENSSL_CMD="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA'
757 }
758
759 component_build_deprecated () {
760     msg "build: make, full config + DEPRECATED_WARNING, gcc -O" # ~ 30s
761     scripts/config.pl full
762     scripts/config.pl set MBEDTLS_DEPRECATED_WARNING
763     # Build with -O -Wextra to catch a maximum of issues.
764     make CC=gcc CFLAGS='-O -Werror -Wall -Wextra' lib programs
765     make CC=gcc CFLAGS='-O -Werror -Wall -Wextra -Wno-unused-function' tests
766
767     msg "build: make, full config + DEPRECATED_REMOVED, clang -O" # ~ 30s
768     # No cleanup, just tweak the configuration and rebuild
769     make clean
770     scripts/config.pl unset MBEDTLS_DEPRECATED_WARNING
771     scripts/config.pl set MBEDTLS_DEPRECATED_REMOVED
772     # Build with -O -Wextra to catch a maximum of issues.
773     make CC=clang CFLAGS='-O -Werror -Wall -Wextra' lib programs
774     make CC=clang CFLAGS='-O -Werror -Wall -Wextra -Wno-unused-function' tests
775 }
776
777 component_test_depends_curves () {
778     msg "test/build: curves.pl (gcc)" # ~ 4 min
779     record_status tests/scripts/curves.pl
780 }
781
782 component_test_depends_hashes () {
783     msg "test/build: depends-hashes.pl (gcc)" # ~ 2 min
784     record_status tests/scripts/depends-hashes.pl
785 }
786
787 component_test_depends_pkalgs () {
788     msg "test/build: depends-pkalgs.pl (gcc)" # ~ 2 min
789     record_status tests/scripts/depends-pkalgs.pl
790 }
791
792 component_build_key_exchanges () {
793     msg "test/build: key-exchanges (gcc)" # ~ 1 min
794     record_status tests/scripts/key-exchanges.pl
795 }
796
797 component_build_default_make_gcc_and_cxx () {
798     msg "build: Unix make, -Os (gcc)" # ~ 30s
799     make CC=gcc CFLAGS='-Werror -Wall -Wextra -Os'
800
801     msg "test: verify header list in cpp_dummy_build.cpp"
802     record_status check_headers_in_cpp
803
804     msg "build: Unix make, incremental g++"
805     make TEST_CPP=1
806 }
807
808 component_test_no_use_psa_crypto_full_cmake_asan() {
809     # full minus MBEDTLS_USE_PSA_CRYPTO: run the same set of tests as basic-build-test.sh
810     msg "build: cmake, full config + MBEDTLS_USE_PSA_CRYPTO, ASan"
811     scripts/config.pl full
812     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # too slow for tests
813     scripts/config.pl unset MBEDTLS_ECP_RESTARTABLE  # restartable ECC not supported through PSA
814     scripts/config.pl set MBEDTLS_PSA_CRYPTO_C
815     scripts/config.pl unset MBEDTLS_USE_PSA_CRYPTO
816     scripts/config.pl unset MBEDTLS_PSA_ITS_FILE_C
817     scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C
818     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
819     make
820
821     msg "test: main suites (full minus MBEDTLS_USE_PSA_CRYPTO)"
822     make test
823
824     msg "test: ssl-opt.sh (full minus MBEDTLS_USE_PSA_CRYPTO)"
825     if_build_succeeded tests/ssl-opt.sh
826
827     msg "test: compat.sh default (full minus MBEDTLS_USE_PSA_CRYPTO)"
828     if_build_succeeded tests/compat.sh
829
830     msg "test: compat.sh ssl3 (full minus MBEDTLS_USE_PSA_CRYPTO)"
831     if_build_succeeded env OPENSSL_CMD="$OPENSSL_LEGACY" tests/compat.sh -m 'ssl3'
832
833     msg "test: compat.sh RC4, DES & NULL (full minus MBEDTLS_USE_PSA_CRYPTO)"
834     if_build_succeeded env OPENSSL_CMD="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" tests/compat.sh -e '3DES\|DES-CBC3' -f 'NULL\|DES\|RC4\|ARCFOUR'
835
836     msg "test: compat.sh ARIA + ChachaPoly (full minus MBEDTLS_USE_PSA_CRYPTO)"
837     if_build_succeeded env OPENSSL_CMD="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA'
838 }
839
840 component_test_check_params_without_platform () {
841     msg "build+test: MBEDTLS_CHECK_PARAMS without MBEDTLS_PLATFORM_C"
842     scripts/config.pl full # includes CHECK_PARAMS
843     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # too slow for tests
844     scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
845     scripts/config.pl unset MBEDTLS_PLATFORM_EXIT_ALT
846     scripts/config.pl unset MBEDTLS_PLATFORM_TIME_ALT
847     scripts/config.pl unset MBEDTLS_PLATFORM_FPRINTF_ALT
848     scripts/config.pl unset MBEDTLS_PLATFORM_MEMORY
849     scripts/config.pl unset MBEDTLS_PLATFORM_PRINTF_ALT
850     scripts/config.pl unset MBEDTLS_PLATFORM_SNPRINTF_ALT
851     scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
852     scripts/config.pl unset MBEDTLS_PLATFORM_C
853     make CC=gcc CFLAGS='-Werror -O1' all test
854 }
855
856 component_test_check_params_silent () {
857     msg "build+test: MBEDTLS_CHECK_PARAMS with alternative MBEDTLS_PARAM_FAILED()"
858     scripts/config.pl full # includes CHECK_PARAMS
859     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # too slow for tests
860     sed -i 's/.*\(#define MBEDTLS_PARAM_FAILED( cond )\).*/\1/' "$CONFIG_H"
861     make CC=gcc CFLAGS='-Werror -O1' all test
862 }
863
864 component_test_no_platform () {
865     # Full configuration build, without platform support, file IO and net sockets.
866     # This should catch missing mbedtls_printf definitions, and by disabling file
867     # IO, it should catch missing '#include <stdio.h>'
868     msg "build: full config except platform/fsio/net, make, gcc, C99" # ~ 30s
869     scripts/config.pl full
870     scripts/config.pl unset MBEDTLS_PLATFORM_C
871     scripts/config.pl unset MBEDTLS_NET_C
872     scripts/config.pl unset MBEDTLS_PLATFORM_MEMORY
873     scripts/config.pl unset MBEDTLS_PLATFORM_PRINTF_ALT
874     scripts/config.pl unset MBEDTLS_PLATFORM_FPRINTF_ALT
875     scripts/config.pl unset MBEDTLS_PLATFORM_SNPRINTF_ALT
876     scripts/config.pl unset MBEDTLS_PLATFORM_TIME_ALT
877     scripts/config.pl unset MBEDTLS_PLATFORM_EXIT_ALT
878     scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
879     scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
880     scripts/config.pl unset MBEDTLS_FS_IO
881     scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C
882     scripts/config.pl unset MBEDTLS_PSA_ITS_FILE_C
883     # Note, _DEFAULT_SOURCE needs to be defined for platforms using glibc version >2.19,
884     # to re-enable platform integration features otherwise disabled in C99 builds
885     make CC=gcc CFLAGS='-Werror -Wall -Wextra -std=c99 -pedantic -O0 -D_DEFAULT_SOURCE' lib programs
886     make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0' test
887 }
888
889 component_build_no_std_function () {
890     # catch compile bugs in _uninit functions
891     msg "build: full config with NO_STD_FUNCTION, make, gcc" # ~ 30s
892     scripts/config.pl full
893     scripts/config.pl set MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
894     scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
895     make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0'
896 }
897
898 component_build_no_ssl_srv () {
899     msg "build: full config except ssl_srv.c, make, gcc" # ~ 30s
900     scripts/config.pl full
901     scripts/config.pl unset MBEDTLS_SSL_SRV_C
902     make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0'
903 }
904
905 component_build_no_ssl_cli () {
906     msg "build: full config except ssl_cli.c, make, gcc" # ~ 30s
907     scripts/config.pl full
908     scripts/config.pl unset MBEDTLS_SSL_CLI_C
909     make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0'
910 }
911
912 component_build_no_sockets () {
913     # Note, C99 compliance can also be tested with the sockets support disabled,
914     # as that requires a POSIX platform (which isn't the same as C99).
915     msg "build: full config except net_sockets.c, make, gcc -std=c99 -pedantic" # ~ 30s
916     scripts/config.pl full
917     scripts/config.pl unset MBEDTLS_NET_C # getaddrinfo() undeclared, etc.
918     scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY # uses syscall() on GNU/Linux
919     make CC=gcc CFLAGS='-Werror -Wall -Wextra -O0 -std=c99 -pedantic' lib
920 }
921
922 component_test_no_max_fragment_length () {
923     # Run max fragment length tests with MFL disabled
924     msg "build: default config except MFL extension (ASan build)" # ~ 30s
925     scripts/config.pl unset MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
926     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
927     make
928
929     msg "test: ssl-opt.sh, MFL-related tests"
930     if_build_succeeded tests/ssl-opt.sh -f "Max fragment length"
931 }
932
933 component_test_asan_remove_peer_certificate () {
934     msg "build: default config with MBEDTLS_SSL_KEEP_PEER_CERTIFICATE disabled (ASan build)"
935     scripts/config.pl unset MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
936     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
937     make
938
939     msg "test: !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE"
940     make test
941
942     msg "test: ssl-opt.sh, !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE"
943     if_build_succeeded tests/ssl-opt.sh
944
945     msg "test: compat.sh, !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE"
946     if_build_succeeded tests/compat.sh
947 }
948
949 component_test_no_max_fragment_length_small_ssl_out_content_len () {
950     msg "build: no MFL extension, small SSL_OUT_CONTENT_LEN (ASan build)"
951     scripts/config.pl unset MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
952     scripts/config.pl set MBEDTLS_SSL_IN_CONTENT_LEN 16384
953     scripts/config.pl set MBEDTLS_SSL_OUT_CONTENT_LEN 4096
954     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
955     make
956
957     msg "test: MFL tests (disabled MFL extension case) & large packet tests"
958     if_build_succeeded tests/ssl-opt.sh -f "Max fragment length\|Large buffer"
959 }
960
961 component_test_when_no_ciphersuites_have_mac () {
962     msg "build: when no ciphersuites have MAC"
963     scripts/config.pl unset MBEDTLS_CIPHER_NULL_CIPHER
964     scripts/config.pl unset MBEDTLS_ARC4_C
965     scripts/config.pl unset MBEDTLS_CIPHER_MODE_CBC
966     make
967
968     msg "test: !MBEDTLS_SSL_SOME_MODES_USE_MAC"
969     make test
970
971     msg "test ssl-opt.sh: !MBEDTLS_SSL_SOME_MODES_USE_MAC"
972     if_build_succeeded tests/ssl-opt.sh -f 'Default\|EtM' -e 'without EtM'
973 }
974
975 component_test_null_entropy () {
976     msg "build: default config with  MBEDTLS_TEST_NULL_ENTROPY (ASan build)"
977     scripts/config.pl set MBEDTLS_TEST_NULL_ENTROPY
978     scripts/config.pl set MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
979     scripts/config.pl set MBEDTLS_ENTROPY_C
980     scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
981     scripts/config.pl unset MBEDTLS_ENTROPY_HARDWARE_ALT
982     scripts/config.pl unset MBEDTLS_HAVEGE_C
983     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan -D UNSAFE_BUILD=ON .
984     make
985
986     msg "test: MBEDTLS_TEST_NULL_ENTROPY - main suites (inc. selftests) (ASan build)"
987     make test
988 }
989
990 component_test_platform_calloc_macro () {
991     msg "build: MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO enabled (ASan build)"
992     scripts/config.pl set MBEDTLS_PLATFORM_MEMORY
993     scripts/config.pl set MBEDTLS_PLATFORM_CALLOC_MACRO calloc
994     scripts/config.pl set MBEDTLS_PLATFORM_FREE_MACRO   free
995     CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
996     make
997
998     msg "test: MBEDTLS_PLATFORM_{CALLOC/FREE}_MACRO enabled (ASan build)"
999     make test
1000 }
1001
1002 component_test_make_shared () {
1003     msg "build/test: make shared" # ~ 40s
1004     make SHARED=1 all check -j1
1005 }
1006
1007 component_test_m32_o0 () {
1008     # Build once with -O0, to compile out the i386 specific inline assembly
1009     msg "build: i386, make, gcc -O0 (ASan build)" # ~ 30s
1010     scripts/config.pl full
1011     make CC=gcc CFLAGS='-O0 -Werror -Wall -Wextra -m32 -fsanitize=address'
1012
1013     msg "test: i386, make, gcc -O0 (ASan build)"
1014     make test
1015 }
1016 support_test_m32_o0 () {
1017     case $(uname -m) in
1018         *64*) true;;
1019         *) false;;
1020     esac
1021 }
1022
1023 component_test_m32_o1 () {
1024     # Build again with -O1, to compile in the i386 specific inline assembly
1025     msg "build: i386, make, gcc -O1 (ASan build)" # ~ 30s
1026     scripts/config.pl full
1027     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE
1028     scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
1029     scripts/config.pl unset MBEDTLS_MEMORY_DEBUG
1030     make CC=gcc CFLAGS='-O1 -Werror -Wall -Wextra -m32 -fsanitize=address'
1031
1032     msg "test: i386, make, gcc -O1 (ASan build)"
1033     make test
1034
1035     msg "test ssl-opt.sh, i386, make, gcc-O1"
1036     if_build_succeeded tests/ssl-opt.sh
1037 }
1038 support_test_m32_o1 () {
1039     support_test_m32_o0 "$@"
1040 }
1041
1042 component_test_mx32 () {
1043     msg "build: 64-bit ILP32, make, gcc" # ~ 30s
1044     scripts/config.pl full
1045     make CC=gcc CFLAGS='-Werror -Wall -Wextra -mx32'
1046
1047     msg "test: 64-bit ILP32, make, gcc"
1048     make test
1049 }
1050 support_test_mx32 () {
1051     case $(uname -m) in
1052         amd64|x86_64) true;;
1053         *) false;;
1054     esac
1055 }
1056
1057 component_build_arm_none_eabi_gcc () {
1058     msg "build: arm-none-eabi-gcc, make" # ~ 10s
1059     scripts/config.pl full
1060     scripts/config.pl unset MBEDTLS_NET_C
1061     scripts/config.pl unset MBEDTLS_TIMING_C
1062     scripts/config.pl unset MBEDTLS_FS_IO
1063     scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
1064     scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY
1065     scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C
1066     scripts/config.pl unset MBEDTLS_PSA_ITS_FILE_C
1067     # following things are not in the default config
1068     scripts/config.pl unset MBEDTLS_HAVEGE_C # depends on timing.c
1069     scripts/config.pl unset MBEDTLS_THREADING_PTHREAD
1070     scripts/config.pl unset MBEDTLS_THREADING_C
1071     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # execinfo.h
1072     scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C # calls exit
1073     make CC=arm-none-eabi-gcc AR=arm-none-eabi-ar LD=arm-none-eabi-ld CFLAGS='-Werror -Wall -Wextra' lib
1074 }
1075
1076 component_build_arm_none_eabi_gcc_no_udbl_division () {
1077     msg "build: arm-none-eabi-gcc -DMBEDTLS_NO_UDBL_DIVISION, make" # ~ 10s
1078     scripts/config.pl full
1079     scripts/config.pl unset MBEDTLS_NET_C
1080     scripts/config.pl unset MBEDTLS_TIMING_C
1081     scripts/config.pl unset MBEDTLS_FS_IO
1082     scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
1083     scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY
1084     scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C
1085     scripts/config.pl unset MBEDTLS_PSA_ITS_FILE_C
1086     # following things are not in the default config
1087     scripts/config.pl unset MBEDTLS_HAVEGE_C # depends on timing.c
1088     scripts/config.pl unset MBEDTLS_THREADING_PTHREAD
1089     scripts/config.pl unset MBEDTLS_THREADING_C
1090     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # execinfo.h
1091     scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C # calls exit
1092     scripts/config.pl set MBEDTLS_NO_UDBL_DIVISION
1093     make CC=arm-none-eabi-gcc AR=arm-none-eabi-ar LD=arm-none-eabi-ld CFLAGS='-Werror -Wall -Wextra' lib
1094     echo "Checking that software 64-bit division is not required"
1095     if_build_succeeded not grep __aeabi_uldiv library/*.o
1096 }
1097
1098 component_build_arm_none_eabi_gcc_no_64bit_multiplication () {
1099     msg "build: arm-none-eabi-gcc MBEDTLS_NO_64BIT_MULTIPLICATION, make" # ~ 10s
1100     scripts/config.pl full
1101     scripts/config.pl unset MBEDTLS_NET_C
1102     scripts/config.pl unset MBEDTLS_TIMING_C
1103     scripts/config.pl unset MBEDTLS_FS_IO
1104     scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
1105     scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C
1106     scripts/config.pl unset MBEDTLS_PSA_ITS_FILE_C
1107     scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY
1108     # following things are not in the default config
1109     scripts/config.pl unset MBEDTLS_HAVEGE_C # depends on timing.c
1110     scripts/config.pl unset MBEDTLS_THREADING_PTHREAD
1111     scripts/config.pl unset MBEDTLS_THREADING_C
1112     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # execinfo.h
1113     scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C # calls exit
1114     scripts/config.pl set MBEDTLS_NO_64BIT_MULTIPLICATION
1115     make CC=arm-none-eabi-gcc AR=arm-none-eabi-ar LD=arm-none-eabi-ld CFLAGS='-Werror -O1 -march=armv6-m -mthumb' lib
1116     echo "Checking that software 64-bit multiplication is not required"
1117     if_build_succeeded not grep __aeabi_lmul library/*.o
1118 }
1119
1120 component_build_armcc () {
1121     msg "build: ARM Compiler 5, make"
1122     scripts/config.pl full
1123     scripts/config.pl unset MBEDTLS_NET_C
1124     scripts/config.pl unset MBEDTLS_TIMING_C
1125     scripts/config.pl unset MBEDTLS_FS_IO
1126     scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
1127     scripts/config.pl unset MBEDTLS_HAVE_TIME
1128     scripts/config.pl unset MBEDTLS_HAVE_TIME_DATE
1129     scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY
1130     scripts/config.pl unset MBEDTLS_PSA_CRYPTO_STORAGE_C
1131     scripts/config.pl unset MBEDTLS_PSA_ITS_FILE_C
1132     # following things are not in the default config
1133     scripts/config.pl unset MBEDTLS_DEPRECATED_WARNING
1134     scripts/config.pl unset MBEDTLS_HAVEGE_C # depends on timing.c
1135     scripts/config.pl unset MBEDTLS_THREADING_PTHREAD
1136     scripts/config.pl unset MBEDTLS_THREADING_C
1137     scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # execinfo.h
1138     scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C # calls exit
1139     scripts/config.pl unset MBEDTLS_PLATFORM_TIME_ALT # depends on MBEDTLS_HAVE_TIME
1140
1141     make CC="$ARMC5_CC" AR="$ARMC5_AR" WARNING_CFLAGS='--strict --c99' lib
1142     make clean
1143
1144     # ARM Compiler 6 - Target ARMv7-A
1145     armc6_build_test "--target=arm-arm-none-eabi -march=armv7-a"
1146
1147     # ARM Compiler 6 - Target ARMv7-M
1148     armc6_build_test "--target=arm-arm-none-eabi -march=armv7-m"
1149
1150     # ARM Compiler 6 - Target ARMv8-A - AArch32
1151     armc6_build_test "--target=arm-arm-none-eabi -march=armv8.2-a"
1152
1153     # ARM Compiler 6 - Target ARMv8-M
1154     armc6_build_test "--target=arm-arm-none-eabi -march=armv8-m.main"
1155
1156     # ARM Compiler 6 - Target ARMv8-A - AArch64
1157     armc6_build_test "--target=aarch64-arm-none-eabi -march=armv8.2-a"
1158 }
1159
1160 component_test_allow_sha1 () {
1161     msg "build: allow SHA1 in certificates by default"
1162     scripts/config.pl set MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES
1163     make CFLAGS='-Werror -Wall -Wextra'
1164     msg "test: allow SHA1 in certificates by default"
1165     make test
1166     if_build_succeeded tests/ssl-opt.sh -f SHA-1
1167 }
1168
1169 component_build_mingw () {
1170     msg "build: Windows cross build - mingw64, make (Link Library)" # ~ 30s
1171     make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 lib programs -j1
1172
1173     # note Make tests only builds the tests, but doesn't run them
1174     make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror' WINDOWS_BUILD=1 tests -j1
1175     make WINDOWS_BUILD=1 clean
1176
1177     msg "build: Windows cross build - mingw64, make (DLL)" # ~ 30s
1178     make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 SHARED=1 lib programs -j1
1179     make CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS='-Werror -Wall -Wextra' WINDOWS_BUILD=1 SHARED=1 tests -j1
1180     make WINDOWS_BUILD=1 clean
1181 }
1182 support_build_mingw() {
1183     case $(i686-w64-mingw32-gcc -dumpversion) in
1184         [0-5]*) false;;
1185         *) true;;
1186     esac
1187 }
1188
1189 component_test_memsan () {
1190     msg "build: MSan (clang)" # ~ 1 min 20s
1191     scripts/config.pl unset MBEDTLS_AESNI_C # memsan doesn't grok asm
1192     CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
1193     make
1194
1195     msg "test: main suites (MSan)" # ~ 10s
1196     make test
1197
1198     msg "test: ssl-opt.sh (MSan)" # ~ 1 min
1199     if_build_succeeded tests/ssl-opt.sh
1200
1201     # Optional part(s)
1202
1203     if [ "$MEMORY" -gt 0 ]; then
1204         msg "test: compat.sh (MSan)" # ~ 6 min 20s
1205         if_build_succeeded tests/compat.sh
1206     fi
1207 }
1208
1209 component_test_valgrind () {
1210     msg "build: Release (clang)"
1211     CC=clang cmake -D CMAKE_BUILD_TYPE:String=Release .
1212     make
1213
1214     msg "test: main suites valgrind (Release)"
1215     make memcheck
1216
1217     # Optional parts (slow; currently broken on OS X because programs don't
1218     # seem to receive signals under valgrind on OS X).
1219     if [ "$MEMORY" -gt 0 ]; then
1220         msg "test: ssl-opt.sh --memcheck (Release)"
1221         if_build_succeeded tests/ssl-opt.sh --memcheck
1222     fi
1223
1224     if [ "$MEMORY" -gt 1 ]; then
1225         msg "test: compat.sh --memcheck (Release)"
1226         if_build_succeeded tests/compat.sh --memcheck
1227     fi
1228 }
1229
1230 component_test_cmake_out_of_source () {
1231     msg "build: cmake 'out-of-source' build"
1232     MBEDTLS_ROOT_DIR="$PWD"
1233     mkdir "$OUT_OF_SOURCE_DIR"
1234     cd "$OUT_OF_SOURCE_DIR"
1235     cmake "$MBEDTLS_ROOT_DIR"
1236     make
1237
1238     msg "test: cmake 'out-of-source' build"
1239     make test
1240     # Test an SSL option that requires an auxiliary script in test/scripts/.
1241     # Also ensure that there are no error messages such as
1242     # "No such file or directory", which would indicate that some required
1243     # file is missing (ssl-opt.sh tolerates the absence of some files so
1244     # may exit with status 0 but emit errors).
1245     if_build_succeeded ./tests/ssl-opt.sh -f 'Fallback SCSV: beginning of list' 2>ssl-opt.err
1246     if [ -s ssl-opt.err ]; then
1247         cat ssl-opt.err >&2
1248         record_status [ ! -s ssl-opt.err ]
1249         rm ssl-opt.err
1250     fi
1251     cd "$MBEDTLS_ROOT_DIR"
1252     rm -rf "$OUT_OF_SOURCE_DIR"
1253     unset MBEDTLS_ROOT_DIR
1254 }
1255
1256 component_test_cmake_as_subdirectory () {
1257     msg "build: cmake 'as-subdirectory' build"
1258     MBEDTLS_ROOT_DIR="$PWD"
1259
1260     cd programs/test/cmake_subproject
1261     cmake .
1262     make
1263     if_build_succeeded ./cmake_subproject
1264
1265     cd "$MBEDTLS_ROOT_DIR"
1266     unset MBEDTLS_ROOT_DIR
1267 }
1268
1269 component_test_zeroize () {
1270     # Test that the function mbedtls_platform_zeroize() is not optimized away by
1271     # different combinations of compilers and optimization flags by using an
1272     # auxiliary GDB script. Unfortunately, GDB does not return error values to the
1273     # system in all cases that the script fails, so we must manually search the
1274     # output to check whether the pass string is present and no failure strings
1275     # were printed.
1276
1277     # Don't try to disable ASLR. We don't care about ASLR here. We do care
1278     # about a spurious message if Gdb tries and fails, so suppress that.
1279     gdb_disable_aslr=
1280     if [ -z "$(gdb -batch -nw -ex 'set disable-randomization off' 2>&1)" ]; then
1281         gdb_disable_aslr='set disable-randomization off'
1282     fi
1283
1284     for optimization_flag in -O2 -O3 -Ofast -Os; do
1285         for compiler in clang gcc; do
1286             msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()"
1287             make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag"
1288             if_build_succeeded gdb -ex "$gdb_disable_aslr" -x tests/scripts/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log
1289             if_build_succeeded grep "The buffer was correctly zeroized" test_zeroize.log
1290             if_build_succeeded not grep -i "error" test_zeroize.log
1291             rm -f test_zeroize.log
1292             make clean
1293         done
1294     done
1295
1296     unset gdb_disable_aslr
1297 }
1298
1299 support_check_python_files () {
1300     type pylint3 >/dev/null 2>/dev/null
1301 }
1302 component_check_python_files () {
1303     msg "Lint: Python scripts"
1304     record_status tests/scripts/check-python-files.sh
1305 }
1306
1307 component_check_generate_test_code () {
1308     msg "uint test: generate_test_code.py"
1309     record_status ./tests/scripts/test_generate_test_code.py
1310 }
1311
1312 ################################################################
1313 #### Termination
1314 ################################################################
1315
1316 post_report () {
1317     msg "Done, cleaning up"
1318     cleanup
1319
1320     final_report
1321 }
1322
1323
1324
1325 ################################################################
1326 #### Run all the things
1327 ################################################################
1328
1329 # Run one component and clean up afterwards.
1330 run_component () {
1331     # Back up the configuration in case the component modifies it.
1332     # The cleanup function will restore it.
1333     cp -p "$CONFIG_H" "$CONFIG_BAK"
1334     current_component="$1"
1335     "$@"
1336     cleanup
1337 }
1338
1339 # Preliminary setup
1340 pre_check_environment
1341 pre_initialize_variables
1342 pre_parse_command_line "$@"
1343
1344 pre_check_git
1345 pre_check_seedfile
1346
1347 build_status=0
1348 if [ $KEEP_GOING -eq 1 ]; then
1349     pre_setup_keep_going
1350 else
1351     record_status () {
1352         "$@"
1353     }
1354 fi
1355 pre_print_configuration
1356 pre_check_tools
1357 cleanup
1358
1359 # Run the requested tests.
1360 for component in $RUN_COMPONENTS; do
1361     run_component "component_$component"
1362 done
1363
1364 # We're done.
1365 post_report