blob: 64ed145f396eafbbbaee1c1df5f6b55417172938 [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
Andres AGb2fdd042016-09-22 14:17:46 +010065# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
66# we just export the variables they require
67export OPENSSL_CMD="$OPENSSL"
68export GNUTLS_CLI="$GNUTLS_CLI"
69export GNUTLS_SERV="$GNUTLS_SERV"
70
Janos Follath7ccac852016-06-06 13:18:39 +010071CONFIG_H='include/mbedtls/config.h'
72CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +000073
Janos Follathb72c6782016-07-19 14:54:17 +010074# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +010075OPENSSL="$OPENSSL" \
76 OPENSSL_LEGACY="$OPENSSL_LEGACY" \
77 GNUTLS_CLI="$GNUTLS_CLI" \
78 GNUTLS_SERV="$GNUTLS_SERV" \
79 GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
80 GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" \
81 scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +010082echo
83
SimonB21ab9d72016-03-12 20:37:32 +000084# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +000085export CFLAGS=' --coverage -g3 -O0 '
Philippe Antoine702c6592019-07-09 17:44:53 +020086export LDFLAGS=' --coverage'
SimonB098a3b52016-04-16 21:56:59 +010087make clean
Janos Follath7ccac852016-06-06 13:18:39 +010088cp "$CONFIG_H" "$CONFIG_BAK"
Gilles Peskine5d46f6a2019-07-27 23:52:53 +020089scripts/config.py full
Simon Butchercbb90752016-05-19 22:15:34 +010090make -j
SimonB21ab9d72016-03-12 20:37:32 +000091
92
93# Step 2 - Execute the tests
94TEST_OUTPUT=out_${PPID}
95cd tests
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020096if [ ! -f "seedfile" ]; then
Gilles Peskinebfcb6e12020-04-09 18:33:34 +020097 dd if=/dev/urandom of="seedfile" bs=64 count=1
Gilles Peskine3c8ccc02019-05-20 11:39:18 +020098fi
Gilles Peskine40be51f2020-04-09 18:50:08 +020099echo
SimonB21ab9d72016-03-12 20:37:32 +0000100
Gilles Peskineca51b472020-04-09 18:29:42 +0200101# Step 2a - Unit Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200102echo '################ Unit tests ################'
Jaeden Amero60ca6e52018-12-07 13:06:24 +0000103perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
Gilles Peskine40be51f2020-04-09 18:50:08 +0200104echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000105echo
106
Gilles Peskineca51b472020-04-09 18:29:42 +0200107# Step 2b - System Tests (keep going even if some tests fail)
Gilles Peskine40be51f2020-04-09 18:50:08 +0200108echo
109echo '################ ssl-opt.sh ################'
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'
118 sh compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2'
119 echo
120
121 echo '#### compat.sh: legacy (SSLv3)'
122 OPENSSL_CMD="$OPENSSL_LEGACY" sh compat.sh -m 'ssl3'
123 echo
124
125 echo '#### compat.sh: legacy (null, DES, RC4)'
126 OPENSSL_CMD="$OPENSSL_LEGACY" \
127 GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
128 sh compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR'
129 echo
130
131 echo '#### compat.sh: next (ARIA, ChaCha)'
132 OPENSSL_CMD="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
133 echo
134} | tee compat-test-$TEST_OUTPUT
135echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000136echo
137
138# Step 3 - Process the coverage report
139cd ..
Gilles Peskine5757d542020-04-09 18:32:48 +0200140{
141 make lcov
142 echo SUCCESS
143} | tee tests/cov-$TEST_OUTPUT
144
145if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
146 echo >&2 "Fatal: 'make lcov' failed"
147 exit 2
148fi
SimonB21ab9d72016-03-12 20:37:32 +0000149
150
151# Step 4 - Summarise the test report
152echo
153echo "========================================================================="
154echo "Test Report Summary"
155echo
156
157cd tests
158
Paul Bakker4b8bc522016-07-20 09:52:01 +0100159# Step 4a - Unit tests
SimonB21ab9d72016-03-12 20:37:32 +0000160echo "Unit tests - tests/scripts/run-test-suites.pl"
161
162PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
163SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
164TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
165FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
166
167echo "No test suites : $TOTAL_SUITES"
168echo "Passed : $PASSED_TESTS"
169echo "Failed : $FAILED_TESTS"
170echo "Skipped : $SKIPPED_TESTS"
171echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
172echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
173echo
174
Paul Bakker4b8bc522016-07-20 09:52:01 +0100175TOTAL_PASS=$PASSED_TESTS
176TOTAL_FAIL=$FAILED_TESTS
177TOTAL_SKIP=$SKIPPED_TESTS
178TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
179TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000180
Paul Bakker4b8bc522016-07-20 09:52:01 +0100181# Step 4b - TLS Options tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000182echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000183
184PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
185SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
186TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
187FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
188
189echo "Passed : $PASSED_TESTS"
190echo "Failed : $FAILED_TESTS"
191echo "Skipped : $SKIPPED_TESTS"
192echo "Total exec'd tests : $TOTAL_TESTS"
193echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
194echo
195
196TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
197TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
198TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
199TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
200TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
201
202
Paul Bakker4b8bc522016-07-20 09:52:01 +0100203# Step 4c - System Compatibility tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000204echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000205
Andres AGb2fdd042016-09-22 14:17:46 +0100206PASSED_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 }')
207SKIPPED_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 }')
208EXED_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 }')
SimonB21ab9d72016-03-12 20:37:32 +0000209FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
210
211echo "Passed : $PASSED_TESTS"
212echo "Failed : $FAILED_TESTS"
213echo "Skipped : $SKIPPED_TESTS"
214echo "Total exec'd tests : $EXED_TESTS"
215echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
216echo
217
218TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
219TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
220TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
221TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
222TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
223
224
Paul Bakker4b8bc522016-07-20 09:52:01 +0100225# Step 4d - Grand totals
SimonB21ab9d72016-03-12 20:37:32 +0000226echo "-------------------------------------------------------------------------"
227echo "Total tests"
228
229echo "Total Passed : $TOTAL_PASS"
230echo "Total Failed : $TOTAL_FAIL"
231echo "Total Skipped : $TOTAL_SKIP"
232echo "Total exec'd tests : $TOTAL_EXED"
233echo "Total avail tests : $TOTAL_AVAIL"
234echo
235
236
Paul Bakker4b8bc522016-07-20 09:52:01 +0100237# Step 4e - Coverage
SimonB21ab9d72016-03-12 20:37:32 +0000238echo "Coverage"
239
Dan Handleya8b26c22020-05-28 16:20:31 +0100240LINES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* lines)/\1/p')
241LINES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) lines)/\1/p')
242FUNCS_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* functions)$/\1/p')
243FUNCS_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) functions)$/\1/p')
244BRANCHES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* branches)$/\1/p')
245BRANCHES_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 +0000246
247LINES_PERCENT=$((1000*$LINES_TESTED/$LINES_TOTAL))
248LINES_PERCENT="$(($LINES_PERCENT/10)).$(($LINES_PERCENT-($LINES_PERCENT/10)*10))"
249
250FUNCS_PERCENT=$((1000*$FUNCS_TESTED/$FUNCS_TOTAL))
251FUNCS_PERCENT="$(($FUNCS_PERCENT/10)).$(($FUNCS_PERCENT-($FUNCS_PERCENT/10)*10))"
252
Dan Handleya8b26c22020-05-28 16:20:31 +0100253BRANCHES_PERCENT=$((1000*$BRANCHES_TESTED/$BRANCHES_TOTAL))
254BRANCHES_PERCENT="$(($BRANCHES_PERCENT/10)).$(($BRANCHES_PERCENT-($BRANCHES_PERCENT/10)*10))"
255
SimonB21ab9d72016-03-12 20:37:32 +0000256echo "Lines Tested : $LINES_TESTED of $LINES_TOTAL $LINES_PERCENT%"
257echo "Functions Tested : $FUNCS_TESTED of $FUNCS_TOTAL $FUNCS_PERCENT%"
Dan Handleya8b26c22020-05-28 16:20:31 +0100258echo "Branches Tested : $BRANCHES_TESTED of $BRANCHES_TOTAL $BRANCHES_PERCENT%"
SimonB21ab9d72016-03-12 20:37:32 +0000259echo
260
SimonB21ab9d72016-03-12 20:37:32 +0000261rm unit-test-$TEST_OUTPUT
262rm sys-test-$TEST_OUTPUT
263rm compat-test-$TEST_OUTPUT
264rm cov-$TEST_OUTPUT
265
266cd ..
Janos Follath7ccac852016-06-06 13:18:39 +0100267
268make clean
269
270if [ -f "$CONFIG_BAK" ]; then
271 mv "$CONFIG_BAK" "$CONFIG_H"
272fi
Gilles Peskine6d6ee982020-04-09 18:28:14 +0200273
274if [ $TOTAL_FAIL -ne 0 ]; then
275 exit 1
276fi