blob: 298422687f9a405cf293bdc2e8dffb06c24f2f1c [file] [log] [blame]
SimonB21ab9d72016-03-12 20:37:32 +00001#!/bin/sh
2
Tom Cosgrovefc2ac752022-11-30 11:13:00 +00003# basic-build-test.sh
SimonB21ab9d72016-03-12 20:37:32 +00004#
Bence Szépkúti1e148272020-08-07 13:07:28 +02005# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02007#
SimonB21ab9d72016-03-12 20:37:32 +00008# Purpose
9#
10# Executes the basic test suites, captures the results, and generates a simple
11# test report and code coverage report.
12#
13# The tests include:
SimonB21ab9d72016-03-12 20:37:32 +000014# * Unit tests - executed using tests/scripts/run-test-suite.pl
Paul Bakker4b8bc522016-07-20 09:52:01 +010015# * Self-tests - executed using the test suites above
SimonB21ab9d72016-03-12 20:37:32 +000016# * System tests - executed using tests/ssl-opt.sh
17# * Interoperability tests - executed using tests/compat.sh
18#
19# The tests focus on functionality and do not consider performance.
20#
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020021# Note the tests self-adapt due to configurations in include/mbedtls/mbedtls_config.h
SimonB21ab9d72016-03-12 20:37:32 +000022# which can lead to some tests being skipped, and can cause the number of
Paul Bakker4b8bc522016-07-20 09:52:01 +010023# available tests to fluctuate.
SimonB21ab9d72016-03-12 20:37:32 +000024#
25# This script has been written to be generic and should work on any shell.
26#
Tom Cosgrovefc2ac752022-11-30 11:13:00 +000027# Usage: basic-build-test.sh
SimonB21ab9d72016-03-12 20:37:32 +000028#
29
30# Abort on errors (and uninitiliased variables)
31set -eu
32
33if [ -d library -a -d include -a -d tests ]; then :; else
Gilles Peskinee820c0a2023-08-03 17:45:20 +020034 echo "Must be run from Mbed TLS root" >&2
SimonB21ab9d72016-03-12 20:37:32 +000035 exit 1
36fi
37
Andres AGb2fdd042016-09-22 14:17:46 +010038: ${OPENSSL:="openssl"}
Andres AGb2fdd042016-09-22 14:17:46 +010039: ${GNUTLS_CLI:="gnutls-cli"}
40: ${GNUTLS_SERV:="gnutls-serv"}
Andres AGb2fdd042016-09-22 14:17:46 +010041
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020042# Used to make ssl-opt.sh deterministic.
Manuel Pégourié-Gonnard54304472020-06-22 10:11:47 +020043#
44# See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
45# in sync. If you change the value here because it breaks some tests, you'll
46# definitely want to change it in all.sh as well.
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020047: ${SEED:=1}
48export SEED
49
Gilles Peskinea3d22642021-07-13 23:23:23 +020050# if MAKEFLAGS is not set add the -j option to speed up invocations of make
51if [ -z "${MAKEFLAGS+set}" ]; then
52 export MAKEFLAGS="-j"
53fi
54
Andres AGb2fdd042016-09-22 14:17:46 +010055# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
56# we just export the variables they require
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +010057export OPENSSL="$OPENSSL"
Andres AGb2fdd042016-09-22 14:17:46 +010058export GNUTLS_CLI="$GNUTLS_CLI"
59export GNUTLS_SERV="$GNUTLS_SERV"
60
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020061CONFIG_H='include/mbedtls/mbedtls_config.h'
Janos Follath7ccac852016-06-06 13:18:39 +010062CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +000063
Janos Follathb72c6782016-07-19 14:54:17 +010064# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +010065OPENSSL="$OPENSSL" \
Andres AGb2fdd042016-09-22 14:17:46 +010066 GNUTLS_CLI="$GNUTLS_CLI" \
67 GNUTLS_SERV="$GNUTLS_SERV" \
Elena Uziunaited92e24e2024-11-19 16:22:33 +000068 framework/scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +010069echo
70
SimonB21ab9d72016-03-12 20:37:32 +000071# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +000072export CFLAGS=' --coverage -g3 -O0 '
Philippe Antoine702c6592019-07-09 17:44:53 +020073export LDFLAGS=' --coverage'
Ronald Crone5bae0d2025-09-24 09:50:06 +020074make -f scripts/legacy.make clean
Janos Follath7ccac852016-06-06 13:18:39 +010075cp "$CONFIG_H" "$CONFIG_BAK"
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020076scripts/config.py full
Ronald Crone5bae0d2025-09-24 09:50:06 +020077make -f scripts/legacy.make
SimonB21ab9d72016-03-12 20:37:32 +000078
79# Step 2 - Execute the tests
80TEST_OUTPUT=out_${PPID}
Gilles Peskine24515f92024-09-11 12:46:08 +020081cd tests
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020082if [ ! -f "seedfile" ]; then
Gilles Peskinebfcb6e12020-04-09 18:33:34 +020083 dd if=/dev/urandom of="seedfile" bs=64 count=1
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020084fi
Gilles Peskine282abb52024-09-11 12:49:50 +020085if [ ! -f "../tf-psa-crypto/tests/seedfile" ]; then
86 cp "seedfile" "../tf-psa-crypto/tests/seedfile"
87fi
Gilles Peskine40be51f2020-04-09 18:50:08 +020088echo
SimonB21ab9d72016-03-12 20:37:32 +000089
Gilles Peskineca51b472020-04-09 18:29:42 +020090# Step 2a - Unit Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +020091echo '################ Unit tests ################'
Gilles Peskine24515f92024-09-11 12:46:08 +020092perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +020093echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +000094echo
95
Gilles Peskineca51b472020-04-09 18:29:42 +020096# Step 2b - System Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +020097echo
98echo '################ ssl-opt.sh ################'
Gilles Peskinebbced872021-07-13 23:26:00 +020099echo "ssl-opt.sh will use SEED=$SEED for udp_proxy"
SimonB21ab9d72016-03-12 20:37:32 +0000100sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200101echo '^^^^^^^^^^^^^^^^ ssl-opt.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000102echo
103
Gilles Peskineca51b472020-04-09 18:29:42 +0200104# Step 2c - Compatibility tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200105echo '################ compat.sh ################'
106{
Manuel Pégourié-Gonnardeb86b902024-04-22 10:25:09 +0200107 echo '#### compat.sh: Default versions'
108 sh compat.sh -e 'ARIA\|CHACHA'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200109 echo
110
Manuel Pégourié-Gonnardeb86b902024-04-22 10:25:09 +0200111 echo '#### compat.sh: next (ARIA, ChaCha)'
112 OPENSSL="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200113 echo
114} | tee compat-test-$TEST_OUTPUT
115echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000116echo
117
118# Step 3 - Process the coverage report
119cd ..
Gilles Peskine5757d542020-04-09 18:32:48 +0200120{
Ronald Crone5bae0d2025-09-24 09:50:06 +0200121 make -f scripts/legacy.make lcov
Gilles Peskine5757d542020-04-09 18:32:48 +0200122 echo SUCCESS
123} | tee tests/cov-$TEST_OUTPUT
124
125if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
126 echo >&2 "Fatal: 'make lcov' failed"
127 exit 2
128fi
SimonB21ab9d72016-03-12 20:37:32 +0000129
130
131# Step 4 - Summarise the test report
132echo
133echo "========================================================================="
134echo "Test Report Summary"
135echo
136
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200137# A failure of the left-hand side of a pipe is ignored (this is a limitation
138# of sh). We'll use the presence of this file as a marker that the generation
139# of the report succeeded.
Gilles Peskined8d19322021-07-22 12:29:27 +0200140rm -f "tests/basic-build-test-$$.ok"
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200141
Gilles Peskinefc70b522021-07-13 23:27:01 +0200142{
SimonB21ab9d72016-03-12 20:37:32 +0000143
Gilles Peskinefc70b522021-07-13 23:27:01 +0200144 cd tests
SimonB21ab9d72016-03-12 20:37:32 +0000145
Gilles Peskine24515f92024-09-11 12:46:08 +0200146 # Step 4a - Unit tests
147 echo "Unit tests - tests/scripts/run-test-suites.pl"
SimonB21ab9d72016-03-12 20:37:32 +0000148
Gilles Peskine24515f92024-09-11 12:46:08 +0200149 PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
150 SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
151 TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
152 FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
SimonB21ab9d72016-03-12 20:37:32 +0000153
Gilles Peskinefc70b522021-07-13 23:27:01 +0200154 echo "No test suites : $TOTAL_SUITES"
155 echo "Passed : $PASSED_TESTS"
156 echo "Failed : $FAILED_TESTS"
157 echo "Skipped : $SKIPPED_TESTS"
158 echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
159 echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
160 echo
SimonB21ab9d72016-03-12 20:37:32 +0000161
Gilles Peskinefc70b522021-07-13 23:27:01 +0200162 TOTAL_PASS=$PASSED_TESTS
163 TOTAL_FAIL=$FAILED_TESTS
164 TOTAL_SKIP=$SKIPPED_TESTS
165 TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
166 TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000167
Gilles Peskine24515f92024-09-11 12:46:08 +0200168 # Step 4b - TLS Options tests
Gilles Peskinefc70b522021-07-13 23:27:01 +0200169 echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000170
Gilles Peskinefc70b522021-07-13 23:27:01 +0200171 PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
172 SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
173 TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
174 FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000175
Gilles Peskinefc70b522021-07-13 23:27:01 +0200176 echo "Passed : $PASSED_TESTS"
177 echo "Failed : $FAILED_TESTS"
178 echo "Skipped : $SKIPPED_TESTS"
179 echo "Total exec'd tests : $TOTAL_TESTS"
180 echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
181 echo
182
183 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
184 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
185 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
186 TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
187 TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000188
189
Gilles Peskine24515f92024-09-11 12:46:08 +0200190 # Step 4c - System Compatibility tests
Gilles Peskinefc70b522021-07-13 23:27:01 +0200191 echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000192
Gilles Peskinefc70b522021-07-13 23:27:01 +0200193 PASSED_TESTS=$(cat compat-test-$TEST_OUTPUT | sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p' | awk 'BEGIN{ s = 0 } { s += $1 } END{ print s }')
194 SKIPPED_TESTS=$(cat compat-test-$TEST_OUTPUT | sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p' | awk 'BEGIN{ s = 0 } { s += $1 } END{ print s }')
195 EXED_TESTS=$(cat compat-test-$TEST_OUTPUT | sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p' | awk 'BEGIN{ s = 0 } { s += $1 } END{ print s }')
196 FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000197
Gilles Peskinefc70b522021-07-13 23:27:01 +0200198 echo "Passed : $PASSED_TESTS"
199 echo "Failed : $FAILED_TESTS"
200 echo "Skipped : $SKIPPED_TESTS"
201 echo "Total exec'd tests : $EXED_TESTS"
202 echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
203 echo
SimonB21ab9d72016-03-12 20:37:32 +0000204
Gilles Peskinefc70b522021-07-13 23:27:01 +0200205 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
206 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
207 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
208 TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
209 TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000210
211
Gilles Peskine24515f92024-09-11 12:46:08 +0200212 # Step 4d - Grand totals
Gilles Peskinefc70b522021-07-13 23:27:01 +0200213 echo "-------------------------------------------------------------------------"
214 echo "Total tests"
SimonB21ab9d72016-03-12 20:37:32 +0000215
Gilles Peskinefc70b522021-07-13 23:27:01 +0200216 echo "Total Passed : $TOTAL_PASS"
217 echo "Total Failed : $TOTAL_FAIL"
218 echo "Total Skipped : $TOTAL_SKIP"
219 echo "Total exec'd tests : $TOTAL_EXED"
220 echo "Total avail tests : $TOTAL_AVAIL"
221 echo
SimonB21ab9d72016-03-12 20:37:32 +0000222
223
Gilles Peskine24515f92024-09-11 12:46:08 +0200224 # Step 4e - Coverage report
Gilles Peskine98d3a672022-12-16 12:03:39 +0100225 echo "Coverage statistics:"
226 sed -n '1,/^Overall coverage/d; /%/p' cov-$TEST_OUTPUT
227 echo
Dan Handleya8b26c22020-05-28 16:20:31 +0100228
Gilles Peskine24515f92024-09-11 12:46:08 +0200229 rm unit-test-$TEST_OUTPUT
Gilles Peskinefc70b522021-07-13 23:27:01 +0200230 rm sys-test-$TEST_OUTPUT
231 rm compat-test-$TEST_OUTPUT
232 rm cov-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000233
Gilles Peskined8d19322021-07-22 12:29:27 +0200234 # Mark the report generation as having succeeded. This must be the
235 # last thing in the report generation.
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200236 touch "basic-build-test-$$.ok"
Gilles Peskinefc70b522021-07-13 23:27:01 +0200237} | tee coverage-summary.txt
Janos Follath7ccac852016-06-06 13:18:39 +0100238
Ronald Crone5bae0d2025-09-24 09:50:06 +0200239make -f scripts/legacy.make clean
Janos Follath7ccac852016-06-06 13:18:39 +0100240
241if [ -f "$CONFIG_BAK" ]; then
242 mv "$CONFIG_BAK" "$CONFIG_H"
243fi
Gilles Peskine6d6ee982020-04-09 18:28:14 +0200244
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200245# The file must exist, otherwise it means something went wrong while generating
246# the coverage report. If something did go wrong, rm will complain so this
247# script will exit with a failure status.
Gilles Peskined8d19322021-07-22 12:29:27 +0200248rm "tests/basic-build-test-$$.ok"