blob: 56fd5b981c7b6e3ee00a1d9d309912fd93d82245 [file] [log] [blame]
SimonB21ab9d72016-03-12 20:37:32 +00001#!/bin/sh
2
3# basic-build-tests.sh
4#
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#
39# Usage: basic-build-tests.sh
40#
41
42# Abort on errors (and uninitiliased variables)
43set -eu
44
45if [ -d library -a -d include -a -d tests ]; then :; else
46 echo "Must be run from mbed TLS root" >&2
47 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"}
54: ${GNUTLS_LEGACY_CLI:="$GNUTLS_CLI"}
55: ${GNUTLS_LEGACY_SERV:="$GNUTLS_SERV"}
56
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020057# Used to make ssl-opt.sh deterministic.
Manuel Pégourié-Gonnard54304472020-06-22 10:11:47 +020058#
59# See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
60# in sync. If you change the value here because it breaks some tests, you'll
61# definitely want to change it in all.sh as well.
Manuel Pégourié-Gonnarde0501912020-06-08 12:59:27 +020062: ${SEED:=1}
63export SEED
64
Gilles Peskine5a09a4a2021-07-13 23:23:23 +020065# if MAKEFLAGS is not set add the -j option to speed up invocations of make
66if [ -z "${MAKEFLAGS+set}" ]; then
67 export MAKEFLAGS="-j"
68fi
69
Andres AGb2fdd042016-09-22 14:17:46 +010070# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
71# we just export the variables they require
72export OPENSSL_CMD="$OPENSSL"
73export GNUTLS_CLI="$GNUTLS_CLI"
74export GNUTLS_SERV="$GNUTLS_SERV"
75
Janos Follath7ccac852016-06-06 13:18:39 +010076CONFIG_H='include/mbedtls/config.h'
77CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +000078
Janos Follathb72c6782016-07-19 14:54:17 +010079# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +010080OPENSSL="$OPENSSL" \
81 OPENSSL_LEGACY="$OPENSSL_LEGACY" \
82 GNUTLS_CLI="$GNUTLS_CLI" \
83 GNUTLS_SERV="$GNUTLS_SERV" \
84 GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
85 GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" \
86 scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +010087echo
88
SimonB21ab9d72016-03-12 20:37:32 +000089# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +000090export CFLAGS=' --coverage -g3 -O0 '
Philippe Antoine702c6592019-07-09 17:44:53 +020091export LDFLAGS=' --coverage'
SimonB098a3b52016-04-16 21:56:59 +010092make clean
Janos Follath7ccac852016-06-06 13:18:39 +010093cp "$CONFIG_H" "$CONFIG_BAK"
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020094scripts/config.py full
Gilles Peskine5a09a4a2021-07-13 23:23:23 +020095make
SimonB21ab9d72016-03-12 20:37:32 +000096
97
98# Step 2 - Execute the tests
99TEST_OUTPUT=out_${PPID}
100cd tests
Gilles Peskine3c8ccc02019-05-20 11:39:18 +0200101if [ ! -f "seedfile" ]; then
Gilles Peskinebfcb6e12020-04-09 18:33:34 +0200102 dd if=/dev/urandom of="seedfile" bs=64 count=1
Gilles Peskine3c8ccc02019-05-20 11:39:18 +0200103fi
Gilles Peskine40be51f2020-04-09 18:50:08 +0200104echo
SimonB21ab9d72016-03-12 20:37:32 +0000105
Gilles Peskineca51b472020-04-09 18:29:42 +0200106# Step 2a - Unit Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200107echo '################ Unit tests ################'
Jaeden Amero60ca6e52018-12-07 13:06:24 +0000108perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200109echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000110echo
111
Gilles Peskineca51b472020-04-09 18:29:42 +0200112# Step 2b - System Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200113echo
114echo '################ ssl-opt.sh ################'
Gilles Peskine8e741412021-07-13 23:26:00 +0200115echo "ssl-opt.sh will use SEED=$SEED for udp_proxy"
SimonB21ab9d72016-03-12 20:37:32 +0000116sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200117echo '^^^^^^^^^^^^^^^^ ssl-opt.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000118echo
119
Gilles Peskineca51b472020-04-09 18:29:42 +0200120# Step 2c - Compatibility tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200121echo '################ compat.sh ################'
122{
123 echo '#### compat.sh: Default versions'
Manuel Pégourié-Gonnardb6238322022-04-14 09:12:10 +0200124 sh compat.sh -m 'tls1 tls1_1 tls12 dtls1 dtls12'
Gilles Peskine40be51f2020-04-09 18:50:08 +0200125 echo
126
127 echo '#### compat.sh: legacy (SSLv3)'
128 OPENSSL_CMD="$OPENSSL_LEGACY" sh compat.sh -m 'ssl3'
129 echo
130
131 echo '#### compat.sh: legacy (null, DES, RC4)'
132 OPENSSL_CMD="$OPENSSL_LEGACY" \
133 GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
134 sh compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR'
135 echo
136
137 echo '#### compat.sh: next (ARIA, ChaCha)'
138 OPENSSL_CMD="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
139 echo
140} | tee compat-test-$TEST_OUTPUT
141echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000142echo
143
144# Step 3 - Process the coverage report
145cd ..
Gilles Peskine5757d542020-04-09 18:32:48 +0200146{
147 make lcov
148 echo SUCCESS
149} | tee tests/cov-$TEST_OUTPUT
150
151if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
152 echo >&2 "Fatal: 'make lcov' failed"
153 exit 2
154fi
SimonB21ab9d72016-03-12 20:37:32 +0000155
156
157# Step 4 - Summarise the test report
158echo
159echo "========================================================================="
160echo "Test Report Summary"
161echo
162
Gilles Peskine5cf753a2021-07-22 11:08:30 +0200163# A failure of the left-hand side of a pipe is ignored (this is a limitation
164# of sh). We'll use the presence of this file as a marker that the generation
165# of the report succeeded.
Gilles Peskineeadd8ee2021-07-22 12:29:27 +0200166rm -f "tests/basic-build-test-$$.ok"
Gilles Peskine5cf753a2021-07-22 11:08:30 +0200167
Gilles Peskine81786e42021-07-13 23:27:01 +0200168{
SimonB21ab9d72016-03-12 20:37:32 +0000169
Gilles Peskine81786e42021-07-13 23:27:01 +0200170 cd tests
SimonB21ab9d72016-03-12 20:37:32 +0000171
Gilles Peskine81786e42021-07-13 23:27:01 +0200172 # Step 4a - Unit tests
173 echo "Unit tests - tests/scripts/run-test-suites.pl"
SimonB21ab9d72016-03-12 20:37:32 +0000174
Gilles Peskine81786e42021-07-13 23:27:01 +0200175 PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
176 SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
177 TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
178 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 +0000179
Gilles Peskine81786e42021-07-13 23:27:01 +0200180 echo "No test suites : $TOTAL_SUITES"
181 echo "Passed : $PASSED_TESTS"
182 echo "Failed : $FAILED_TESTS"
183 echo "Skipped : $SKIPPED_TESTS"
184 echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
185 echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
186 echo
SimonB21ab9d72016-03-12 20:37:32 +0000187
Gilles Peskine81786e42021-07-13 23:27:01 +0200188 TOTAL_PASS=$PASSED_TESTS
189 TOTAL_FAIL=$FAILED_TESTS
190 TOTAL_SKIP=$SKIPPED_TESTS
191 TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
192 TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000193
Gilles Peskine81786e42021-07-13 23:27:01 +0200194 # Step 4b - TLS Options tests
195 echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000196
Gilles Peskine81786e42021-07-13 23:27:01 +0200197 PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
198 SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
199 TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
200 FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000201
Gilles Peskine81786e42021-07-13 23:27:01 +0200202 echo "Passed : $PASSED_TESTS"
203 echo "Failed : $FAILED_TESTS"
204 echo "Skipped : $SKIPPED_TESTS"
205 echo "Total exec'd tests : $TOTAL_TESTS"
206 echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
207 echo
208
209 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
210 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
211 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
212 TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
213 TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000214
215
Gilles Peskine81786e42021-07-13 23:27:01 +0200216 # Step 4c - System Compatibility tests
217 echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000218
Gilles Peskine81786e42021-07-13 23:27:01 +0200219 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 }')
220 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 }')
221 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 }')
222 FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000223
Gilles Peskine81786e42021-07-13 23:27:01 +0200224 echo "Passed : $PASSED_TESTS"
225 echo "Failed : $FAILED_TESTS"
226 echo "Skipped : $SKIPPED_TESTS"
227 echo "Total exec'd tests : $EXED_TESTS"
228 echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
229 echo
SimonB21ab9d72016-03-12 20:37:32 +0000230
Gilles Peskine81786e42021-07-13 23:27:01 +0200231 TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
232 TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
233 TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
234 TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
235 TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000236
237
Gilles Peskine81786e42021-07-13 23:27:01 +0200238 # Step 4d - Grand totals
239 echo "-------------------------------------------------------------------------"
240 echo "Total tests"
SimonB21ab9d72016-03-12 20:37:32 +0000241
Gilles Peskine81786e42021-07-13 23:27:01 +0200242 echo "Total Passed : $TOTAL_PASS"
243 echo "Total Failed : $TOTAL_FAIL"
244 echo "Total Skipped : $TOTAL_SKIP"
245 echo "Total exec'd tests : $TOTAL_EXED"
246 echo "Total avail tests : $TOTAL_AVAIL"
247 echo
SimonB21ab9d72016-03-12 20:37:32 +0000248
249
Gilles Peskine81786e42021-07-13 23:27:01 +0200250 # Step 4e - Coverage
251 echo "Coverage"
SimonB21ab9d72016-03-12 20:37:32 +0000252
Gilles Peskine81786e42021-07-13 23:27:01 +0200253 LINES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* lines)/\1/p')
254 LINES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) lines)/\1/p')
255 FUNCS_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* functions)$/\1/p')
256 FUNCS_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) functions)$/\1/p')
257 BRANCHES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* branches)$/\1/p')
258 BRANCHES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) branches)$/\1/p')
SimonB21ab9d72016-03-12 20:37:32 +0000259
Gilles Peskine81786e42021-07-13 23:27:01 +0200260 LINES_PERCENT=$((1000*$LINES_TESTED/$LINES_TOTAL))
261 LINES_PERCENT="$(($LINES_PERCENT/10)).$(($LINES_PERCENT-($LINES_PERCENT/10)*10))"
SimonB21ab9d72016-03-12 20:37:32 +0000262
Gilles Peskine81786e42021-07-13 23:27:01 +0200263 FUNCS_PERCENT=$((1000*$FUNCS_TESTED/$FUNCS_TOTAL))
264 FUNCS_PERCENT="$(($FUNCS_PERCENT/10)).$(($FUNCS_PERCENT-($FUNCS_PERCENT/10)*10))"
SimonB21ab9d72016-03-12 20:37:32 +0000265
Gilles Peskine81786e42021-07-13 23:27:01 +0200266 BRANCHES_PERCENT=$((1000*$BRANCHES_TESTED/$BRANCHES_TOTAL))
267 BRANCHES_PERCENT="$(($BRANCHES_PERCENT/10)).$(($BRANCHES_PERCENT-($BRANCHES_PERCENT/10)*10))"
Dan Handleya8b26c22020-05-28 16:20:31 +0100268
Gilles Peskine81786e42021-07-13 23:27:01 +0200269 rm unit-test-$TEST_OUTPUT
270 rm sys-test-$TEST_OUTPUT
271 rm compat-test-$TEST_OUTPUT
272 rm cov-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000273
Gilles Peskine81786e42021-07-13 23:27:01 +0200274 echo "Lines Tested : $LINES_TESTED of $LINES_TOTAL $LINES_PERCENT%"
275 echo "Functions Tested : $FUNCS_TESTED of $FUNCS_TOTAL $FUNCS_PERCENT%"
276 echo "Branches Tested : $BRANCHES_TESTED of $BRANCHES_TOTAL $BRANCHES_PERCENT%"
277 echo
SimonB21ab9d72016-03-12 20:37:32 +0000278
Gilles Peskineeadd8ee2021-07-22 12:29:27 +0200279 # Mark the report generation as having succeeded. This must be the
280 # last thing in the report generation.
Gilles Peskine5cf753a2021-07-22 11:08:30 +0200281 touch "basic-build-test-$$.ok"
Gilles Peskine81786e42021-07-13 23:27:01 +0200282} | tee coverage-summary.txt
Janos Follath7ccac852016-06-06 13:18:39 +0100283
284make clean
285
286if [ -f "$CONFIG_BAK" ]; then
287 mv "$CONFIG_BAK" "$CONFIG_H"
288fi
Gilles Peskine6d6ee982020-04-09 18:28:14 +0200289
Gilles Peskine5cf753a2021-07-22 11:08:30 +0200290# The file must exist, otherwise it means something went wrong while generating
291# the coverage report. If something did go wrong, rm will complain so this
292# script will exit with a failure status.
Gilles Peskineeadd8ee2021-07-22 12:29:27 +0200293rm "tests/basic-build-test-$$.ok"