blob: 970abfa23155983c7094abbfde4f8799ba92a3c0 [file] [log] [blame]
SimonB21ab9d72016-03-12 20:37:32 +00001#!/bin/sh
2
Tom Cosgrove5f49b3c2022-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
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02006# SPDX-License-Identifier: Apache-2.0
7#
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
SimonB21ab9d72016-03-12 20:37:32 +000020# Purpose
21#
22# Executes the basic test suites, captures the results, and generates a simple
23# test report and code coverage report.
24#
25# The tests include:
SimonB21ab9d72016-03-12 20:37:32 +000026# * Unit tests - executed using tests/scripts/run-test-suite.pl
Paul Bakker4b8bc522016-07-20 09:52:01 +010027# * Self-tests - executed using the test suites above
SimonB21ab9d72016-03-12 20:37:32 +000028# * System tests - executed using tests/ssl-opt.sh
29# * Interoperability tests - executed using tests/compat.sh
30#
31# The tests focus on functionality and do not consider performance.
32#
33# Note the tests self-adapt due to configurations in include/mbedtls/config.h
34# which can lead to some tests being skipped, and can cause the number of
Paul Bakker4b8bc522016-07-20 09:52:01 +010035# available tests to fluctuate.
SimonB21ab9d72016-03-12 20:37:32 +000036#
37# This script has been written to be generic and should work on any shell.
38#
Tom Cosgrove5f49b3c2022-11-30 11:13:00 +000039# Usage: basic-build-test.sh
SimonB21ab9d72016-03-12 20:37:32 +000040#
41
42# Abort on errors (and uninitiliased variables)
43set -eu
44
45if [ -d library -a -d include -a -d tests ]; then :; else
Gilles Peskinef08ca832023-09-12 19:21:54 +020046 echo "Must be run from Mbed TLS root" >&2
SimonB21ab9d72016-03-12 20:37:32 +000047 exit 1
48fi
49
Andres AGb2fdd042016-09-22 14:17:46 +010050: ${OPENSSL:="openssl"}
51: ${OPENSSL_LEGACY:="$OPENSSL"}
52: ${GNUTLS_CLI:="gnutls-cli"}
53: ${GNUTLS_SERV:="gnutls-serv"}
Andres AGb2fdd042016-09-22 14:17:46 +010054
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020055# Used to make ssl-opt.sh deterministic.
Manuel Pégourié-Gonnard54304472020-06-22 10:11:47 +020056#
57# See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
58# in sync. If you change the value here because it breaks some tests, you'll
59# definitely want to change it in all.sh as well.
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020060: ${SEED:=1}
61export SEED
62
Gilles Peskine5a09a4a2021-07-13 23:23:23 +020063# if MAKEFLAGS is not set add the -j option to speed up invocations of make
64if [ -z "${MAKEFLAGS+set}" ]; then
65 export MAKEFLAGS="-j"
66fi
67
Andres AGb2fdd042016-09-22 14:17:46 +010068# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
69# we just export the variables they require
Manuel Pégourié-Gonnard89d40272022-12-19 11:42:12 +010070export OPENSSL="$OPENSSL"
Andres AGb2fdd042016-09-22 14:17:46 +010071export GNUTLS_CLI="$GNUTLS_CLI"
72export GNUTLS_SERV="$GNUTLS_SERV"
73
Janos Follath7ccac852016-06-06 13:18:39 +010074CONFIG_H='include/mbedtls/config.h'
75CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +000076
Janos Follathb72c6782016-07-19 14:54:17 +010077# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +010078OPENSSL="$OPENSSL" \
79 OPENSSL_LEGACY="$OPENSSL_LEGACY" \
80 GNUTLS_CLI="$GNUTLS_CLI" \
81 GNUTLS_SERV="$GNUTLS_SERV" \
Andres AGb2fdd042016-09-22 14:17:46 +010082 scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +010083echo
84
SimonB21ab9d72016-03-12 20:37:32 +000085# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +000086export CFLAGS=' --coverage -g3 -O0 '
Philippe Antoine702c6592019-07-09 17:44:53 +020087export LDFLAGS=' --coverage'
SimonB098a3b52016-04-16 21:56:59 +010088make clean
Janos Follath7ccac852016-06-06 13:18:39 +010089cp "$CONFIG_H" "$CONFIG_BAK"
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020090scripts/config.py full
Gilles Peskine5a09a4a2021-07-13 23:23:23 +020091make
SimonB21ab9d72016-03-12 20:37:32 +000092
93
94# Step 2 - Execute the tests
95TEST_OUTPUT=out_${PPID}
96cd tests
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020097if [ ! -f "seedfile" ]; then
Gilles Peskinebfcb6e12020-04-09 18:33:34 +020098 dd if=/dev/urandom of="seedfile" bs=64 count=1
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020099fi
Gilles Peskine40be51f2020-04-09 18:50:08 +0200100echo
SimonB21ab9d72016-03-12 20:37:32 +0000101
Gilles Peskineca51b472020-04-09 18:29:42 +0200102# Step 2a - Unit Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200103echo '################ Unit tests ################'
Jaeden Amero60ca6e52018-12-07 13:06:24 +0000104perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200105echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000106echo
107
Gilles Peskineca51b472020-04-09 18:29:42 +0200108# Step 2b - System Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200109echo
110echo '################ ssl-opt.sh ################'
Gilles Peskine8e741412021-07-13 23:26:00 +0200111echo "ssl-opt.sh will use SEED=$SEED for udp_proxy"
SimonB21ab9d72016-03-12 20:37:32 +0000112sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200113echo '^^^^^^^^^^^^^^^^ ssl-opt.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000114echo
115
Gilles Peskineca51b472020-04-09 18:29:42 +0200116# Step 2c - Compatibility tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200117echo '################ compat.sh ################'
118{
Gilles Peskinec67c3b32023-08-27 21:33:41 +0200119 echo '#### compat.sh: Default ciphers'
120 sh compat.sh -m 'ssl3 tls1 tls1_1 tls12 dtls1 dtls12'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200121 echo
122
123 echo '#### compat.sh: legacy (null, DES, RC4)'
Manuel Pégourié-Gonnard89d40272022-12-19 11:42:12 +0100124 OPENSSL="$OPENSSL_LEGACY" \
Gilles Peskine40be51f2020-04-09 18:50:08 +0200125 sh compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR'
126 echo
127
128 echo '#### compat.sh: next (ARIA, ChaCha)'
Manuel Pégourié-Gonnard89d40272022-12-19 11:42:12 +0100129 OPENSSL="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200130 echo
131} | tee compat-test-$TEST_OUTPUT
132echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000133echo
134
135# Step 3 - Process the coverage report
136cd ..
Gilles Peskine5757d542020-04-09 18:32:48 +0200137{
138 make lcov
139 echo SUCCESS
140} | tee tests/cov-$TEST_OUTPUT
141
142if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
143 echo >&2 "Fatal: 'make lcov' failed"
144 exit 2
145fi
SimonB21ab9d72016-03-12 20:37:32 +0000146
147
148# Step 4 - Summarise the test report
149echo
150echo "========================================================================="
151echo "Test Report Summary"
152echo
153
Gilles Peskine5cf753a2021-07-22 11:08:30 +0200154# A failure of the left-hand side of a pipe is ignored (this is a limitation
155# of sh). We'll use the presence of this file as a marker that the generation
156# of the report succeeded.
Gilles Peskineeadd8ee2021-07-22 12:29:27 +0200157rm -f "tests/basic-build-test-$$.ok"
Gilles Peskine5cf753a2021-07-22 11:08:30 +0200158
Gilles Peskine81786e42021-07-13 23:27:01 +0200159{
SimonB21ab9d72016-03-12 20:37:32 +0000160
Gilles Peskine81786e42021-07-13 23:27:01 +0200161 cd tests
SimonB21ab9d72016-03-12 20:37:32 +0000162
Gilles Peskine81786e42021-07-13 23:27:01 +0200163 # Step 4a - Unit tests
164 echo "Unit tests - tests/scripts/run-test-suites.pl"
SimonB21ab9d72016-03-12 20:37:32 +0000165
Gilles Peskine81786e42021-07-13 23:27:01 +0200166 PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
167 SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
168 TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
169 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 +0000170
Gilles Peskine81786e42021-07-13 23:27:01 +0200171 echo "No test suites : $TOTAL_SUITES"
172 echo "Passed : $PASSED_TESTS"
173 echo "Failed : $FAILED_TESTS"
174 echo "Skipped : $SKIPPED_TESTS"
175 echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
176 echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
177 echo
SimonB21ab9d72016-03-12 20:37:32 +0000178
Gilles Peskine81786e42021-07-13 23:27:01 +0200179 TOTAL_PASS=$PASSED_TESTS
180 TOTAL_FAIL=$FAILED_TESTS
181 TOTAL_SKIP=$SKIPPED_TESTS
182 TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
183 TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000184
Gilles Peskine81786e42021-07-13 23:27:01 +0200185 # Step 4b - TLS Options tests
186 echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000187
Gilles Peskine81786e42021-07-13 23:27:01 +0200188 PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
189 SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
190 TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
191 FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000192
Gilles Peskine81786e42021-07-13 23:27:01 +0200193 echo "Passed : $PASSED_TESTS"
194 echo "Failed : $FAILED_TESTS"
195 echo "Skipped : $SKIPPED_TESTS"
196 echo "Total exec'd tests : $TOTAL_TESTS"
197 echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
198 echo
199
200 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
201 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
202 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
203 TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
204 TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000205
206
Gilles Peskine81786e42021-07-13 23:27:01 +0200207 # Step 4c - System Compatibility tests
208 echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000209
Gilles Peskine81786e42021-07-13 23:27:01 +0200210 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 }')
211 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 }')
212 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 }')
213 FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000214
Gilles Peskine81786e42021-07-13 23:27:01 +0200215 echo "Passed : $PASSED_TESTS"
216 echo "Failed : $FAILED_TESTS"
217 echo "Skipped : $SKIPPED_TESTS"
218 echo "Total exec'd tests : $EXED_TESTS"
219 echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
220 echo
SimonB21ab9d72016-03-12 20:37:32 +0000221
Gilles Peskine81786e42021-07-13 23:27:01 +0200222 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
223 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
224 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
225 TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
226 TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000227
228
Gilles Peskine81786e42021-07-13 23:27:01 +0200229 # Step 4d - Grand totals
230 echo "-------------------------------------------------------------------------"
231 echo "Total tests"
SimonB21ab9d72016-03-12 20:37:32 +0000232
Gilles Peskine81786e42021-07-13 23:27:01 +0200233 echo "Total Passed : $TOTAL_PASS"
234 echo "Total Failed : $TOTAL_FAIL"
235 echo "Total Skipped : $TOTAL_SKIP"
236 echo "Total exec'd tests : $TOTAL_EXED"
237 echo "Total avail tests : $TOTAL_AVAIL"
238 echo
SimonB21ab9d72016-03-12 20:37:32 +0000239
240
Gilles Peskine38a49562022-12-16 12:03:39 +0100241 # Step 4e - Coverage report
242 echo "Coverage statistics:"
243 sed -n '1,/^Overall coverage/d; /%/p' cov-$TEST_OUTPUT
244 echo
Dan Handleya8b26c22020-05-28 16:20:31 +0100245
Gilles Peskine81786e42021-07-13 23:27:01 +0200246 rm unit-test-$TEST_OUTPUT
247 rm sys-test-$TEST_OUTPUT
248 rm compat-test-$TEST_OUTPUT
249 rm cov-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000250
Gilles Peskineeadd8ee2021-07-22 12:29:27 +0200251 # Mark the report generation as having succeeded. This must be the
252 # last thing in the report generation.
Gilles Peskine5cf753a2021-07-22 11:08:30 +0200253 touch "basic-build-test-$$.ok"
Gilles Peskine81786e42021-07-13 23:27:01 +0200254} | tee coverage-summary.txt
Janos Follath7ccac852016-06-06 13:18:39 +0100255
256make clean
257
258if [ -f "$CONFIG_BAK" ]; then
259 mv "$CONFIG_BAK" "$CONFIG_H"
260fi
Gilles Peskine6d6ee982020-04-09 18:28:14 +0200261
Gilles Peskine5cf753a2021-07-22 11:08:30 +0200262# The file must exist, otherwise it means something went wrong while generating
263# the coverage report. If something did go wrong, rm will complain so this
264# script will exit with a failure status.
Gilles Peskineeadd8ee2021-07-22 12:29:27 +0200265rm "tests/basic-build-test-$$.ok"