blob: 1f8ade6636eb4c78285e129a942d29120e32cf63 [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 Rodgmane3c05852023-11-03 12:21:36 +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"}
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
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020045# Used to make ssl-opt.sh deterministic.
Manuel Pégourié-Gonnard54304472020-06-22 10:11:47 +020046#
47# See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
48# in sync. If you change the value here because it breaks some tests, you'll
49# definitely want to change it in all.sh as well.
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020050: ${SEED:=1}
51export SEED
52
Gilles Peskinea3d22642021-07-13 23:23:23 +020053# if MAKEFLAGS is not set add the -j option to speed up invocations of make
54if [ -z "${MAKEFLAGS+set}" ]; then
55 export MAKEFLAGS="-j"
56fi
57
Andres AGb2fdd042016-09-22 14:17:46 +010058# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
59# we just export the variables they require
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +010060export OPENSSL="$OPENSSL"
Andres AGb2fdd042016-09-22 14:17:46 +010061export GNUTLS_CLI="$GNUTLS_CLI"
62export GNUTLS_SERV="$GNUTLS_SERV"
63
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020064CONFIG_H='include/mbedtls/mbedtls_config.h'
Janos Follath7ccac852016-06-06 13:18:39 +010065CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +000066
Janos Follathb72c6782016-07-19 14:54:17 +010067# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +010068OPENSSL="$OPENSSL" \
69 OPENSSL_LEGACY="$OPENSSL_LEGACY" \
70 GNUTLS_CLI="$GNUTLS_CLI" \
71 GNUTLS_SERV="$GNUTLS_SERV" \
72 GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
73 GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" \
74 scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +010075echo
76
SimonB21ab9d72016-03-12 20:37:32 +000077# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +000078export CFLAGS=' --coverage -g3 -O0 '
Philippe Antoine702c6592019-07-09 17:44:53 +020079export LDFLAGS=' --coverage'
SimonB098a3b52016-04-16 21:56:59 +010080make clean
Janos Follath7ccac852016-06-06 13:18:39 +010081cp "$CONFIG_H" "$CONFIG_BAK"
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020082scripts/config.py full
Gilles Peskinea3d22642021-07-13 23:23:23 +020083make
SimonB21ab9d72016-03-12 20:37:32 +000084
85
86# Step 2 - Execute the tests
87TEST_OUTPUT=out_${PPID}
88cd tests
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020089if [ ! -f "seedfile" ]; then
Gilles Peskinebfcb6e12020-04-09 18:33:34 +020090 dd if=/dev/urandom of="seedfile" bs=64 count=1
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020091fi
Gilles Peskine40be51f2020-04-09 18:50:08 +020092echo
SimonB21ab9d72016-03-12 20:37:32 +000093
Gilles Peskineca51b472020-04-09 18:29:42 +020094# Step 2a - Unit Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +020095echo '################ Unit tests ################'
Jaeden Amero60ca6e52018-12-07 13:06:24 +000096perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +020097echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +000098echo
99
Gilles Peskineca51b472020-04-09 18:29:42 +0200100# Step 2b - System Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200101echo
102echo '################ ssl-opt.sh ################'
Gilles Peskinebbced872021-07-13 23:26:00 +0200103echo "ssl-opt.sh will use SEED=$SEED for udp_proxy"
SimonB21ab9d72016-03-12 20:37:32 +0000104sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200105echo '^^^^^^^^^^^^^^^^ ssl-opt.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000106echo
107
Gilles Peskineca51b472020-04-09 18:29:42 +0200108# Step 2c - Compatibility tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200109echo '################ compat.sh ################'
110{
111 echo '#### compat.sh: Default versions'
Manuel Pégourié-Gonnard94732292022-04-14 09:12:10 +0200112 sh compat.sh
Gilles Peskine40be51f2020-04-09 18:50:08 +0200113 echo
114
Manuel Pégourié-Gonnard296787f2022-04-06 13:28:27 +0200115 echo '#### compat.sh: legacy (null)'
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +0100116 OPENSSL="$OPENSSL_LEGACY" \
Gilles Peskine40be51f2020-04-09 18:50:08 +0200117 GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
Manuel Pégourié-Gonnard296787f2022-04-06 13:28:27 +0200118 sh compat.sh -e '^$' -f 'NULL'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200119 echo
120
121 echo '#### compat.sh: next (ARIA, ChaCha)'
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +0100122 OPENSSL="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200123 echo
124} | tee compat-test-$TEST_OUTPUT
125echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000126echo
127
128# Step 3 - Process the coverage report
129cd ..
Gilles Peskine5757d542020-04-09 18:32:48 +0200130{
131 make lcov
132 echo SUCCESS
133} | tee tests/cov-$TEST_OUTPUT
134
135if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
136 echo >&2 "Fatal: 'make lcov' failed"
137 exit 2
138fi
SimonB21ab9d72016-03-12 20:37:32 +0000139
140
141# Step 4 - Summarise the test report
142echo
143echo "========================================================================="
144echo "Test Report Summary"
145echo
146
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200147# A failure of the left-hand side of a pipe is ignored (this is a limitation
148# of sh). We'll use the presence of this file as a marker that the generation
149# of the report succeeded.
Gilles Peskined8d19322021-07-22 12:29:27 +0200150rm -f "tests/basic-build-test-$$.ok"
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200151
Gilles Peskinefc70b522021-07-13 23:27:01 +0200152{
SimonB21ab9d72016-03-12 20:37:32 +0000153
Gilles Peskinefc70b522021-07-13 23:27:01 +0200154 cd tests
SimonB21ab9d72016-03-12 20:37:32 +0000155
Gilles Peskinefc70b522021-07-13 23:27:01 +0200156 # Step 4a - Unit tests
157 echo "Unit tests - tests/scripts/run-test-suites.pl"
SimonB21ab9d72016-03-12 20:37:32 +0000158
Gilles Peskinefc70b522021-07-13 23:27:01 +0200159 PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
160 SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
161 TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
162 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 +0000163
Gilles Peskinefc70b522021-07-13 23:27:01 +0200164 echo "No test suites : $TOTAL_SUITES"
165 echo "Passed : $PASSED_TESTS"
166 echo "Failed : $FAILED_TESTS"
167 echo "Skipped : $SKIPPED_TESTS"
168 echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
169 echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
170 echo
SimonB21ab9d72016-03-12 20:37:32 +0000171
Gilles Peskinefc70b522021-07-13 23:27:01 +0200172 TOTAL_PASS=$PASSED_TESTS
173 TOTAL_FAIL=$FAILED_TESTS
174 TOTAL_SKIP=$SKIPPED_TESTS
175 TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
176 TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000177
Gilles Peskinefc70b522021-07-13 23:27:01 +0200178 # Step 4b - TLS Options tests
179 echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000180
Gilles Peskinefc70b522021-07-13 23:27:01 +0200181 PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
182 SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
183 TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
184 FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000185
Gilles Peskinefc70b522021-07-13 23:27:01 +0200186 echo "Passed : $PASSED_TESTS"
187 echo "Failed : $FAILED_TESTS"
188 echo "Skipped : $SKIPPED_TESTS"
189 echo "Total exec'd tests : $TOTAL_TESTS"
190 echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
191 echo
192
193 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
194 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
195 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
196 TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
197 TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000198
199
Gilles Peskinefc70b522021-07-13 23:27:01 +0200200 # Step 4c - System Compatibility tests
201 echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000202
Gilles Peskinefc70b522021-07-13 23:27:01 +0200203 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 }')
204 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 }')
205 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 }')
206 FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000207
Gilles Peskinefc70b522021-07-13 23:27:01 +0200208 echo "Passed : $PASSED_TESTS"
209 echo "Failed : $FAILED_TESTS"
210 echo "Skipped : $SKIPPED_TESTS"
211 echo "Total exec'd tests : $EXED_TESTS"
212 echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
213 echo
SimonB21ab9d72016-03-12 20:37:32 +0000214
Gilles Peskinefc70b522021-07-13 23:27:01 +0200215 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
216 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
217 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
218 TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
219 TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000220
221
Gilles Peskinefc70b522021-07-13 23:27:01 +0200222 # Step 4d - Grand totals
223 echo "-------------------------------------------------------------------------"
224 echo "Total tests"
SimonB21ab9d72016-03-12 20:37:32 +0000225
Gilles Peskinefc70b522021-07-13 23:27:01 +0200226 echo "Total Passed : $TOTAL_PASS"
227 echo "Total Failed : $TOTAL_FAIL"
228 echo "Total Skipped : $TOTAL_SKIP"
229 echo "Total exec'd tests : $TOTAL_EXED"
230 echo "Total avail tests : $TOTAL_AVAIL"
231 echo
SimonB21ab9d72016-03-12 20:37:32 +0000232
233
Gilles Peskine98d3a672022-12-16 12:03:39 +0100234 # Step 4e - Coverage report
235 echo "Coverage statistics:"
236 sed -n '1,/^Overall coverage/d; /%/p' cov-$TEST_OUTPUT
237 echo
Dan Handleya8b26c22020-05-28 16:20:31 +0100238
Gilles Peskinefc70b522021-07-13 23:27:01 +0200239 rm unit-test-$TEST_OUTPUT
240 rm sys-test-$TEST_OUTPUT
241 rm compat-test-$TEST_OUTPUT
242 rm cov-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000243
Gilles Peskined8d19322021-07-22 12:29:27 +0200244 # Mark the report generation as having succeeded. This must be the
245 # last thing in the report generation.
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200246 touch "basic-build-test-$$.ok"
Gilles Peskinefc70b522021-07-13 23:27:01 +0200247} | tee coverage-summary.txt
Janos Follath7ccac852016-06-06 13:18:39 +0100248
249make clean
250
251if [ -f "$CONFIG_BAK" ]; then
252 mv "$CONFIG_BAK" "$CONFIG_H"
253fi
Gilles Peskine6d6ee982020-04-09 18:28:14 +0200254
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200255# The file must exist, otherwise it means something went wrong while generating
256# the coverage report. If something did go wrong, rm will complain so this
257# script will exit with a failure status.
Gilles Peskined8d19322021-07-22 12:29:27 +0200258rm "tests/basic-build-test-$$.ok"