blob: 72923f62c6b5a406b9dc123500671b5a537d32c8 [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
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#
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020033# Note the tests self-adapt due to configurations in include/mbedtls/mbedtls_config.h
SimonB21ab9d72016-03-12 20:37:32 +000034# 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 Cosgrovefc2ac752022-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 Peskinee820c0a2023-08-03 17:45:20 +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"}
Andres AGb2fdd042016-09-22 14:17:46 +010051: ${GNUTLS_CLI:="gnutls-cli"}
52: ${GNUTLS_SERV:="gnutls-serv"}
Andres AGb2fdd042016-09-22 14:17:46 +010053
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020054# Used to make ssl-opt.sh deterministic.
Manuel Pégourié-Gonnard54304472020-06-22 10:11:47 +020055#
56# See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
57# in sync. If you change the value here because it breaks some tests, you'll
58# definitely want to change it in all.sh as well.
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020059: ${SEED:=1}
60export SEED
61
Gilles Peskinea3d22642021-07-13 23:23:23 +020062# if MAKEFLAGS is not set add the -j option to speed up invocations of make
63if [ -z "${MAKEFLAGS+set}" ]; then
64 export MAKEFLAGS="-j"
65fi
66
Andres AGb2fdd042016-09-22 14:17:46 +010067# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
68# we just export the variables they require
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +010069export OPENSSL="$OPENSSL"
Andres AGb2fdd042016-09-22 14:17:46 +010070export GNUTLS_CLI="$GNUTLS_CLI"
71export GNUTLS_SERV="$GNUTLS_SERV"
72
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020073CONFIG_H='include/mbedtls/mbedtls_config.h'
Janos Follath7ccac852016-06-06 13:18:39 +010074CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +000075
Janos Follathb72c6782016-07-19 14:54:17 +010076# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +010077OPENSSL="$OPENSSL" \
Andres AGb2fdd042016-09-22 14:17:46 +010078 GNUTLS_CLI="$GNUTLS_CLI" \
79 GNUTLS_SERV="$GNUTLS_SERV" \
Andres AGb2fdd042016-09-22 14:17:46 +010080 scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +010081echo
82
SimonB21ab9d72016-03-12 20:37:32 +000083# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +000084export CFLAGS=' --coverage -g3 -O0 '
Philippe Antoine702c6592019-07-09 17:44:53 +020085export LDFLAGS=' --coverage'
SimonB098a3b52016-04-16 21:56:59 +010086make clean
Janos Follath7ccac852016-06-06 13:18:39 +010087cp "$CONFIG_H" "$CONFIG_BAK"
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020088scripts/config.py full
Gilles Peskinea3d22642021-07-13 23:23:23 +020089make
SimonB21ab9d72016-03-12 20:37:32 +000090
91
92# Step 2 - Execute the tests
93TEST_OUTPUT=out_${PPID}
94cd tests
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020095if [ ! -f "seedfile" ]; then
Gilles Peskinebfcb6e12020-04-09 18:33:34 +020096 dd if=/dev/urandom of="seedfile" bs=64 count=1
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020097fi
Gilles Peskine40be51f2020-04-09 18:50:08 +020098echo
SimonB21ab9d72016-03-12 20:37:32 +000099
Gilles Peskineca51b472020-04-09 18:29:42 +0200100# Step 2a - Unit Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200101echo '################ Unit tests ################'
Jaeden Amero60ca6e52018-12-07 13:06:24 +0000102perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200103echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000104echo
105
Gilles Peskineca51b472020-04-09 18:29:42 +0200106# Step 2b - System Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200107echo
108echo '################ ssl-opt.sh ################'
Gilles Peskinebbced872021-07-13 23:26:00 +0200109echo "ssl-opt.sh will use SEED=$SEED for udp_proxy"
SimonB21ab9d72016-03-12 20:37:32 +0000110sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200111echo '^^^^^^^^^^^^^^^^ ssl-opt.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000112echo
113
Gilles Peskineca51b472020-04-09 18:29:42 +0200114# Step 2c - Compatibility tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200115echo '################ compat.sh ################'
116{
117 echo '#### compat.sh: Default versions'
Manuel Pégourié-Gonnard94732292022-04-14 09:12:10 +0200118 sh compat.sh
Gilles Peskine40be51f2020-04-09 18:50:08 +0200119 echo
120
Gilles Peskinee29203b2023-08-27 21:43:00 +0200121 echo '#### compat.sh: null cipher'
Manuel Pégourié-Gonnard296787f2022-04-06 13:28:27 +0200122 sh compat.sh -e '^$' -f 'NULL'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200123 echo
124
125 echo '#### compat.sh: next (ARIA, ChaCha)'
Manuel Pégourié-Gonnardc5722462022-12-19 11:42:12 +0100126 OPENSSL="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200127 echo
128} | tee compat-test-$TEST_OUTPUT
129echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000130echo
131
132# Step 3 - Process the coverage report
133cd ..
Gilles Peskine5757d542020-04-09 18:32:48 +0200134{
135 make lcov
136 echo SUCCESS
137} | tee tests/cov-$TEST_OUTPUT
138
139if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
140 echo >&2 "Fatal: 'make lcov' failed"
141 exit 2
142fi
SimonB21ab9d72016-03-12 20:37:32 +0000143
144
145# Step 4 - Summarise the test report
146echo
147echo "========================================================================="
148echo "Test Report Summary"
149echo
150
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200151# A failure of the left-hand side of a pipe is ignored (this is a limitation
152# of sh). We'll use the presence of this file as a marker that the generation
153# of the report succeeded.
Gilles Peskined8d19322021-07-22 12:29:27 +0200154rm -f "tests/basic-build-test-$$.ok"
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200155
Gilles Peskinefc70b522021-07-13 23:27:01 +0200156{
SimonB21ab9d72016-03-12 20:37:32 +0000157
Gilles Peskinefc70b522021-07-13 23:27:01 +0200158 cd tests
SimonB21ab9d72016-03-12 20:37:32 +0000159
Gilles Peskinefc70b522021-07-13 23:27:01 +0200160 # Step 4a - Unit tests
161 echo "Unit tests - tests/scripts/run-test-suites.pl"
SimonB21ab9d72016-03-12 20:37:32 +0000162
Gilles Peskinefc70b522021-07-13 23:27:01 +0200163 PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
164 SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
165 TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
166 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 +0000167
Gilles Peskinefc70b522021-07-13 23:27:01 +0200168 echo "No test suites : $TOTAL_SUITES"
169 echo "Passed : $PASSED_TESTS"
170 echo "Failed : $FAILED_TESTS"
171 echo "Skipped : $SKIPPED_TESTS"
172 echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
173 echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
174 echo
SimonB21ab9d72016-03-12 20:37:32 +0000175
Gilles Peskinefc70b522021-07-13 23:27:01 +0200176 TOTAL_PASS=$PASSED_TESTS
177 TOTAL_FAIL=$FAILED_TESTS
178 TOTAL_SKIP=$SKIPPED_TESTS
179 TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
180 TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000181
Gilles Peskinefc70b522021-07-13 23:27:01 +0200182 # Step 4b - TLS Options tests
183 echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000184
Gilles Peskinefc70b522021-07-13 23:27:01 +0200185 PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
186 SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
187 TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
188 FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000189
Gilles Peskinefc70b522021-07-13 23:27:01 +0200190 echo "Passed : $PASSED_TESTS"
191 echo "Failed : $FAILED_TESTS"
192 echo "Skipped : $SKIPPED_TESTS"
193 echo "Total exec'd tests : $TOTAL_TESTS"
194 echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
195 echo
196
197 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
198 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
199 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
200 TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
201 TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000202
203
Gilles Peskinefc70b522021-07-13 23:27:01 +0200204 # Step 4c - System Compatibility tests
205 echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000206
Gilles Peskinefc70b522021-07-13 23:27:01 +0200207 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 }')
208 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 }')
209 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 }')
210 FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000211
Gilles Peskinefc70b522021-07-13 23:27:01 +0200212 echo "Passed : $PASSED_TESTS"
213 echo "Failed : $FAILED_TESTS"
214 echo "Skipped : $SKIPPED_TESTS"
215 echo "Total exec'd tests : $EXED_TESTS"
216 echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
217 echo
SimonB21ab9d72016-03-12 20:37:32 +0000218
Gilles Peskinefc70b522021-07-13 23:27:01 +0200219 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
220 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
221 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
222 TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
223 TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000224
225
Gilles Peskinefc70b522021-07-13 23:27:01 +0200226 # Step 4d - Grand totals
227 echo "-------------------------------------------------------------------------"
228 echo "Total tests"
SimonB21ab9d72016-03-12 20:37:32 +0000229
Gilles Peskinefc70b522021-07-13 23:27:01 +0200230 echo "Total Passed : $TOTAL_PASS"
231 echo "Total Failed : $TOTAL_FAIL"
232 echo "Total Skipped : $TOTAL_SKIP"
233 echo "Total exec'd tests : $TOTAL_EXED"
234 echo "Total avail tests : $TOTAL_AVAIL"
235 echo
SimonB21ab9d72016-03-12 20:37:32 +0000236
237
Gilles Peskine98d3a672022-12-16 12:03:39 +0100238 # Step 4e - Coverage report
239 echo "Coverage statistics:"
240 sed -n '1,/^Overall coverage/d; /%/p' cov-$TEST_OUTPUT
241 echo
Dan Handleya8b26c22020-05-28 16:20:31 +0100242
Gilles Peskinefc70b522021-07-13 23:27:01 +0200243 rm unit-test-$TEST_OUTPUT
244 rm sys-test-$TEST_OUTPUT
245 rm compat-test-$TEST_OUTPUT
246 rm cov-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000247
Gilles Peskined8d19322021-07-22 12:29:27 +0200248 # Mark the report generation as having succeeded. This must be the
249 # last thing in the report generation.
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200250 touch "basic-build-test-$$.ok"
Gilles Peskinefc70b522021-07-13 23:27:01 +0200251} | tee coverage-summary.txt
Janos Follath7ccac852016-06-06 13:18:39 +0100252
253make clean
254
255if [ -f "$CONFIG_BAK" ]; then
256 mv "$CONFIG_BAK" "$CONFIG_H"
257fi
Gilles Peskine6d6ee982020-04-09 18:28:14 +0200258
Gilles Peskine6ee3b7e2021-07-22 11:08:30 +0200259# The file must exist, otherwise it means something went wrong while generating
260# the coverage report. If something did go wrong, rm will complain so this
261# script will exit with a failure status.
Gilles Peskined8d19322021-07-22 12:29:27 +0200262rm "tests/basic-build-test-$$.ok"