1#!/bin/sh
2
3# all.sh
4#
5# This file is part of mbed TLS (https://tls.mbed.org)
6#
7# Copyright (c) 2014-2016, ARM Limited, All Rights Reserved
8#
9# Purpose
10#
11# To run all tests possible or available on the platform.
12#
13# Warning: the test is destructive. It includes various build modes and
14# configurations, and can and will arbitrarily change the current CMake
15# configuration. After this script has been run, the CMake cache will be lost
16# and CMake will no longer be initialised.
17#
18# The script assumes the presence of gcc and clang (recent enough for using
19# ASan with gcc and MemSan with clang, or valgrind) are available, as well as
20# cmake and a "good" find.
21
22# Abort on errors (and uninitialised variables)
23set -eu
24
25if [ -d library -a -d include -a -d tests ]; then :; else
26    err_msg "Must be run from mbed TLS root"
27    exit 1
28fi
29
30CONFIG_H='include/mbedtls/config.h'
31CONFIG_BAK="$CONFIG_H.bak"
32
33MEMORY=0
34FORCE=0
35RELEASE=0
36
37# Default commands, can be overriden by the environment
38: ${OPENSSL:="openssl"}
39: ${OPENSSL_LEGACY:="$OPENSSL"}
40: ${GNUTLS_CLI:="gnutls-cli"}
41: ${GNUTLS_SERV:="gnutls-serv"}
42: ${GNUTLS_LEGACY_CLI:="$GNUTLS_CLI"}
43: ${GNUTLS_LEGACY_SERV:="$GNUTLS_SERV"}
44: ${OUT_OF_SOURCE_DIR:=./mbedtls_out_of_source_build}
45
46usage()
47{
48    printf "Usage: $0\n"
49    printf "  -h|--help\t\tPrint this help.\n"
50    printf "  -m|--memory\t\tAdditional optional memory tests.\n"
51    printf "  -f|--force\t\tForce the tests to overwrite any modified files.\n"
52    printf "  -s|--seed\t\tInteger seed value to use for this test run.\n"
53    printf "  -r|--release-test\t\tRun this script in release mode. This fixes the seed value to 1.\n"
54    printf "     --out-of-source-dir=<path>\t\tDirectory used for CMake out-of-source build tests."
55    printf "     --openssl=<OpenSSL_path>\t\tPath to OpenSSL executable to use for most tests.\n"
56    printf "     --openssl-legacy=<OpenSSL_path>\t\tPath to OpenSSL executable to use for legacy tests e.g. SSLv3.\n"
57    printf "     --gnutls-cli=<GnuTLS_cli_path>\t\tPath to GnuTLS client executable to use for most tests.\n"
58    printf "     --gnutls-serv=<GnuTLS_serv_path>\t\tPath to GnuTLS server executable to use for most tests.\n"
59    printf "     --gnutls-legacy-cli=<GnuTLS_cli_path>\t\tPath to GnuTLS client executable to use for legacy tests.\n"
60    printf "     --gnutls-legacy-serv=<GnuTLS_serv_path>\t\tPath to GnuTLS server executable to use for legacy tests.\n"
61}
62
63# remove built files as well as the cmake cache/config
64cleanup()
65{
66    make clean
67
68    find . -name yotta -prune -o -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} \+
69    rm -f include/Makefile include/mbedtls/Makefile programs/*/Makefile
70    git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile
71    git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile
72
73    if [ -f "$CONFIG_BAK" ]; then
74        mv "$CONFIG_BAK" "$CONFIG_H"
75    fi
76}
77
78trap cleanup INT TERM HUP
79
80msg()
81{
82    echo ""
83    echo "******************************************************************"
84    echo "* $1 "
85    printf "* "; date
86    echo "******************************************************************"
87}
88
89err_msg()
90{
91    echo "$1" >&2
92}
93
94check_tools()
95{
96    for TOOL in "$@"; do
97        if ! `hash "$TOOL" >/dev/null 2>&1`; then
98            err_msg "$TOOL not found!"
99            exit 1
100        fi
101    done
102}
103
104while [ $# -gt 0 ]; do
105    case "$1" in
106        --memory|-m*)
107            MEMORY=${1#-m}
108            ;;
109        --force|-f)
110            FORCE=1
111            ;;
112        --seed|-s)
113            shift
114            SEED="$1"
115            ;;
116        --release-test|-r)
117            RELEASE=1
118            ;;
119        --out-of-source-dir)
120            shift
121            OUT_OF_SOURCE_DIR="$1"
122            ;;
123        --openssl)
124            shift
125            OPENSSL="$1"
126            ;;
127        --openssl-legacy)
128            shift
129            OPENSSL_LEGACY="$1"
130            ;;
131        --gnutls-cli)
132            shift
133            GNUTLS_CLI="$1"
134            ;;
135        --gnutls-serv)
136            shift
137            GNUTLS_SERV="$1"
138            ;;
139        --gnutls-legacy-cli)
140            shift
141            GNUTLS_LEGACY_CLI="$1"
142            ;;
143        --gnutls-legacy-serv)
144            shift
145            GNUTLS_LEGACY_SERV="$1"
146            ;;
147        --help|-h|*)
148            usage
149            exit 1
150            ;;
151    esac
152    shift
153done
154
155if [ $FORCE -eq 1 ]; then
156    rm -rf yotta/module "$OUT_OF_SOURCE_DIR"
157    git checkout-index -f -q $CONFIG_H
158    cleanup
159else
160
161    if [ -d yotta/module ]; then
162        err_msg "Warning - there is an existing yotta module in the directory 'yotta/module'"
163        echo "You can either delete your work and retry, or force the test to overwrite the"
164        echo "test by rerunning the script as: $0 --force"
165        exit 1
166    fi
167
168    if [ -d "$OUT_OF_SOURCE_DIR" ]; then
169        echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
170        echo "You can either delete this directory manually, or force the test by rerunning"
171        echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
172        exit 1
173    fi
174
175    if ! git diff-files --quiet include/mbedtls/config.h; then
176        echo $?
177        err_msg "Warning - the configuration file 'include/mbedtls/config.h' has been edited. "
178        echo "You can either delete or preserve your work, or force the test by rerunning the"
179        echo "script as: $0 --force"
180        exit 1
181    fi
182fi
183
184if [ $RELEASE -eq 1 ]; then
185    # Fix the seed value to 1 to ensure that the tests are deterministic.
186    SEED=1
187fi
188
189msg "info: $0 configuration"
190echo "MEMORY: $MEMORY"
191echo "FORCE: $FORCE"
192echo "SEED: ${SEED-"UNSET"}"
193echo "OPENSSL: $OPENSSL"
194echo "OPENSSL_LEGACY: $OPENSSL_LEGACY"
195echo "GNUTLS_CLI: $GNUTLS_CLI"
196echo "GNUTLS_SERV: $GNUTLS_SERV"
197echo "GNUTLS_LEGACY_CLI: $GNUTLS_LEGACY_CLI"
198echo "GNUTLS_LEGACY_SERV: $GNUTLS_LEGACY_SERV"
199
200# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
201# we just export the variables they require
202export OPENSSL_CMD="$OPENSSL"
203export GNUTLS_CLI="$GNUTLS_CLI"
204export GNUTLS_SERV="$GNUTLS_SERV"
205
206# Avoid passing --seed flag in every call to ssl-opt.sh
207[ ! -z ${SEED+set} ] && export SEED
208
209# Make sure the tools we need are available.
210check_tools "$OPENSSL" "$OPENSSL_LEGACY" "$GNUTLS_CLI" "$GNUTLS_SERV" \
211    "$GNUTLS_LEGACY_CLI" "$GNUTLS_LEGACY_SERV" "doxygen" "dot" \
212    "arm-none-eabi-gcc" "armcc"
213
214#
215# Test Suites to be executed
216#
217# The test ordering tries to optimize for the following criteria:
218# 1. Catch possible problems early, by running first tests that run quickly
219#    and/or are more likely to fail than others (eg I use Clang most of the
220#    time, so start with a GCC build).
221# 2. Minimize total running time, by avoiding useless rebuilds
222#
223# Indicative running times are given for reference.
224
225msg "info: output_env.sh"
226OPENSSL="$OPENSSL" OPENSSL_LEGACY="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_CLI" \
227    GNUTLS_SERV="$GNUTLS_SERV" GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
228    GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" scripts/output_env.sh
229
230msg "test: recursion.pl" # < 1s
231tests/scripts/recursion.pl library/*.c
232
233msg "test: freshness of generated source files" # < 1s
234tests/scripts/check-generated-files.sh
235
236msg "test: doxygen markup outside doxygen blocks" # < 1s
237tests/scripts/check-doxy-blocks.pl
238
239msg "test/build: declared and exported names" # < 3s
240cleanup
241tests/scripts/check-names.sh
242
243msg "test: doxygen warnings" # ~ 3s
244cleanup
245tests/scripts/doxygen.sh
246
247msg "build: create and build yotta module" # ~ 30s
248cleanup
249tests/scripts/yotta-build.sh
250
251msg "build: cmake, gcc, ASan" # ~ 1 min 50s
252cleanup
253CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
254make
255
256msg "test: main suites (inc. selftests) (ASan build)" # ~ 50s
257make test
258
259msg "test: ssl-opt.sh (ASan build)" # ~ 1 min
260tests/ssl-opt.sh
261
262msg "test/build: ref-configs (ASan build)" # ~ 6 min 20s
263tests/scripts/test-ref-configs.pl
264
265msg "build: with ASan (rebuild after ref-configs)" # ~ 1 min
266make
267
268msg "test: compat.sh (ASan build)" # ~ 6 min
269tests/compat.sh
270
271msg "build: Default + SSLv3 (ASan build)" # ~ 6 min
272cleanup
273cp "$CONFIG_H" "$CONFIG_BAK"
274scripts/config.pl set MBEDTLS_SSL_PROTO_SSL3
275CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
276make
277
278msg "test: SSLv3 - main suites (inc. selftests) (ASan build)" # ~ 50s
279make test
280
281msg "build: SSLv3 - compat.sh (ASan build)" # ~ 6 min
282tests/compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2'
283OPENSSL_CMD="$OPENSSL_LEGACY" tests/compat.sh -m 'ssl3'
284
285msg "build: SSLv3 - ssl-opt.sh (ASan build)" # ~ 6 min
286tests/ssl-opt.sh
287
288msg "build: cmake, full config, clang" # ~ 50s
289cleanup
290cp "$CONFIG_H" "$CONFIG_BAK"
291scripts/config.pl full
292scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # too slow for tests
293CC=clang cmake -D CMAKE_BUILD_TYPE:String=Check .
294make
295
296msg "test: main suites (full config)" # ~ 5s
297make test
298
299msg "test: ssl-opt.sh default (full config)" # ~ 1s
300tests/ssl-opt.sh -f Default
301
302msg "test: compat.sh RC4, DES & NULL (full config)" # ~ 2 min
303OPENSSL_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'
304
305msg "test/build: curves.pl (gcc)" # ~ 4 min
306cleanup
307cmake -D CMAKE_BUILD_TYPE:String=Debug .
308tests/scripts/curves.pl
309
310msg "test/build: key-exchanges (gcc)" # ~ 1 min
311cleanup
312cmake -D CMAKE_BUILD_TYPE:String=Check .
313tests/scripts/key-exchanges.pl
314
315msg "build: Unix make, -Os (gcc)" # ~ 30s
316cleanup
317CC=gcc CFLAGS='-Werror -Os' make
318
319# this is meant to cath missing #define mbedtls_printf etc
320# disable fsio to catch some more missing #include <stdio.h>
321msg "build: full config except platform/fsio, make, gcc" # ~ 30s
322cleanup
323cp "$CONFIG_H" "$CONFIG_BAK"
324scripts/config.pl full
325scripts/config.pl unset MBEDTLS_PLATFORM_C
326scripts/config.pl unset MBEDTLS_PLATFORM_MEMORY
327scripts/config.pl unset MBEDTLS_PLATFORM_PRINTF_ALT
328scripts/config.pl unset MBEDTLS_PLATFORM_FPRINTF_ALT
329scripts/config.pl unset MBEDTLS_PLATFORM_SNPRINTF_ALT
330scripts/config.pl unset MBEDTLS_PLATFORM_TIME_ALT
331scripts/config.pl unset MBEDTLS_PLATFORM_EXIT_ALT
332scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
333scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
334scripts/config.pl unset MBEDTLS_FS_IO
335CC=gcc CFLAGS='-Werror -O0' make
336
337# catch compile bugs in _uninit functions
338msg "build: full config with NO_STD_FUNCTION, make, gcc" # ~ 30s
339cleanup
340cp "$CONFIG_H" "$CONFIG_BAK"
341scripts/config.pl full
342scripts/config.pl set MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
343scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
344CC=gcc CFLAGS='-Werror -O0' make
345
346msg "build: full config except ssl_srv.c, make, gcc" # ~ 30s
347cleanup
348cp "$CONFIG_H" "$CONFIG_BAK"
349scripts/config.pl full
350scripts/config.pl unset MBEDTLS_SSL_SRV_C
351CC=gcc CFLAGS='-Werror -O0' make
352
353msg "build: full config except ssl_cli.c, make, gcc" # ~ 30s
354cleanup
355cp "$CONFIG_H" "$CONFIG_BAK"
356scripts/config.pl full
357scripts/config.pl unset MBEDTLS_SSL_CLI_C
358CC=gcc CFLAGS='-Werror -O0' make
359
360msg "build: full config except net_sockets.c, make, gcc -std=c99 -pedantic" # ~ 30s
361cleanup
362cp "$CONFIG_H" "$CONFIG_BAK"
363scripts/config.pl full
364scripts/config.pl unset MBEDTLS_NET_C # getaddrinfo() undeclared, etc.
365scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY # uses syscall() on GNU/Linux
366CC=gcc CFLAGS='-Werror -O0 -std=c99 -pedantic' make lib
367
368msg "build: default config with  MBEDTLS_TEST_NULL_ENTROPY (ASan build)"
369cleanup
370cp "$CONFIG_H" "$CONFIG_BAK"
371scripts/config.pl set MBEDTLS_TEST_NULL_ENTROPY
372scripts/config.pl set MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
373scripts/config.pl set MBEDTLS_ENTROPY_C
374scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
375scripts/config.pl unset MBEDTLS_ENTROPY_HARDWARE_ALT
376scripts/config.pl unset MBEDTLS_HAVEGE_C
377CC=gcc cmake  -D UNSAFE_BUILD=ON -D CMAKE_C_FLAGS:String="-fsanitize=address -fno-common -O3" .
378make
379
380msg "test: MBEDTLS_TEST_NULL_ENTROPY - main suites (inc. selftests) (ASan build)"
381make test
382
383if uname -a | grep -F Linux >/dev/null; then
384msg "build/test: make shared" # ~ 40s
385cleanup
386make SHARED=1 all check
387fi
388
389if uname -a | grep -F x86_64 >/dev/null; then
390msg "build: i386, make, gcc" # ~ 30s
391cleanup
392CC=gcc CFLAGS='-Werror -m32' make
393fi # x86_64
394
395msg "build: arm-none-eabi-gcc, make" # ~ 10s
396cleanup
397cp "$CONFIG_H" "$CONFIG_BAK"
398scripts/config.pl full
399scripts/config.pl unset MBEDTLS_NET_C
400scripts/config.pl unset MBEDTLS_TIMING_C
401scripts/config.pl unset MBEDTLS_FS_IO
402scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
403scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY
404# following things are not in the default config
405scripts/config.pl unset MBEDTLS_HAVEGE_C # depends on timing.c
406scripts/config.pl unset MBEDTLS_THREADING_PTHREAD
407scripts/config.pl unset MBEDTLS_THREADING_C
408scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # execinfo.h
409scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C # calls exit
410CC=arm-none-eabi-gcc AR=arm-none-eabi-ar LD=arm-none-eabi-ld CFLAGS=-Werror make lib
411
412msg "build: armcc, make"
413cleanup
414cp "$CONFIG_H" "$CONFIG_BAK"
415scripts/config.pl full
416scripts/config.pl unset MBEDTLS_NET_C
417scripts/config.pl unset MBEDTLS_TIMING_C
418scripts/config.pl unset MBEDTLS_FS_IO
419scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
420scripts/config.pl unset MBEDTLS_HAVE_TIME
421scripts/config.pl unset MBEDTLS_HAVE_TIME_DATE
422scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY
423# following things are not in the default config
424scripts/config.pl unset MBEDTLS_DEPRECATED_WARNING
425scripts/config.pl unset MBEDTLS_HAVEGE_C # depends on timing.c
426scripts/config.pl unset MBEDTLS_THREADING_PTHREAD
427scripts/config.pl unset MBEDTLS_THREADING_C
428scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # execinfo.h
429scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C # calls exit
430scripts/config.pl unset MBEDTLS_PLATFORM_TIME_ALT # depends on MBEDTLS_HAVE_TIME
431CC=armcc AR=armar WARNING_CFLAGS= make lib
432
433if which i686-w64-mingw32-gcc >/dev/null; then
434msg "build: cross-mingw64, make" # ~ 30s
435cleanup
436CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS=-Werror WINDOWS_BUILD=1 make
437WINDOWS_BUILD=1 make clean
438CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS=-Werror WINDOWS_BUILD=1 SHARED=1 make
439WINDOWS_BUILD=1 make clean
440fi
441
442# MemSan currently only available on Linux 64 bits
443if uname -a | grep 'Linux.*x86_64' >/dev/null; then
444
445msg "build: MSan (clang)" # ~ 1 min 20s
446cleanup
447cp "$CONFIG_H" "$CONFIG_BAK"
448scripts/config.pl unset MBEDTLS_AESNI_C # memsan doesn't grok asm
449CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
450make
451
452msg "test: main suites (MSan)" # ~ 10s
453make test
454
455msg "test: ssl-opt.sh (MSan)" # ~ 1 min
456tests/ssl-opt.sh
457
458# Optional part(s)
459
460if [ "$MEMORY" -gt 0 ]; then
461    msg "test: compat.sh (MSan)" # ~ 6 min 20s
462    tests/compat.sh
463fi
464
465else # no MemSan
466
467msg "build: Release (clang)"
468cleanup
469CC=clang cmake -D CMAKE_BUILD_TYPE:String=Release .
470make
471
472msg "test: main suites valgrind (Release)"
473make memcheck
474
475# Optional part(s)
476# Currently broken, programs don't seem to receive signals
477# under valgrind on OS X
478
479if [ "$MEMORY" -gt 0 ]; then
480    msg "test: ssl-opt.sh --memcheck (Release)"
481    tests/ssl-opt.sh --memcheck
482fi
483
484if [ "$MEMORY" -gt 1 ]; then
485    msg "test: compat.sh --memcheck (Release)"
486    tests/compat.sh --memcheck
487fi
488
489fi # MemSan
490
491msg "build: cmake 'out-of-source' build"
492cleanup
493MBEDTLS_ROOT_DIR="$PWD"
494mkdir "$OUT_OF_SOURCE_DIR"
495cd "$OUT_OF_SOURCE_DIR"
496cmake "$MBEDTLS_ROOT_DIR"
497make
498
499msg "test: cmake 'out-of-source' build"
500make test
501cd "$MBEDTLS_ROOT_DIR"
502rm -rf "$OUT_OF_SOURCE_DIR"
503
504msg "Done, cleaning up"
505cleanup
506
507