blob: 003c03aabd8dd923b401c75ca3b22c2cb7945560 [file] [log] [blame]
SimonB21ab9d72016-03-12 20:37:32 +00001#!/bin/sh
2
3# basic-build-tests.sh
4#
Bence Szépkútia2947ac2020-08-19 16:37:36 +02005# Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02006# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7#
8# This file is provided under the Apache License 2.0, or the
9# GNU General Public License v2.0 or later.
10#
11# **********
12# Apache License 2.0:
Bence Szépkúti51b41d52020-05-26 01:54:15 +020013#
14# Licensed under the Apache License, Version 2.0 (the "License"); you may
15# not use this file except in compliance with the License.
16# You may obtain a copy of the License at
17#
18# http://www.apache.org/licenses/LICENSE-2.0
19#
20# Unless required by applicable law or agreed to in writing, software
21# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23# See the License for the specific language governing permissions and
24# limitations under the License.
25#
Bence Szépkútif744bd72020-06-05 13:02:18 +020026# **********
27#
28# **********
29# GNU General Public License v2.0 or later:
30#
31# This program is free software; you can redistribute it and/or modify
32# it under the terms of the GNU General Public License as published by
33# the Free Software Foundation; either version 2 of the License, or
34# (at your option) any later version.
35#
36# This program is distributed in the hope that it will be useful,
37# but WITHOUT ANY WARRANTY; without even the implied warranty of
38# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39# GNU General Public License for more details.
40#
41# You should have received a copy of the GNU General Public License along
42# with this program; if not, write to the Free Software Foundation, Inc.,
43# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
44#
45# **********
46#
SimonB21ab9d72016-03-12 20:37:32 +000047# Purpose
48#
49# Executes the basic test suites, captures the results, and generates a simple
50# test report and code coverage report.
51#
52# The tests include:
SimonB21ab9d72016-03-12 20:37:32 +000053# * Unit tests - executed using tests/scripts/run-test-suite.pl
Paul Bakker4b8bc522016-07-20 09:52:01 +010054# * Self-tests - executed using the test suites above
SimonB21ab9d72016-03-12 20:37:32 +000055# * System tests - executed using tests/ssl-opt.sh
56# * Interoperability tests - executed using tests/compat.sh
57#
58# The tests focus on functionality and do not consider performance.
59#
60# Note the tests self-adapt due to configurations in include/mbedtls/config.h
61# which can lead to some tests being skipped, and can cause the number of
Paul Bakker4b8bc522016-07-20 09:52:01 +010062# available tests to fluctuate.
SimonB21ab9d72016-03-12 20:37:32 +000063#
64# This script has been written to be generic and should work on any shell.
65#
66# Usage: basic-build-tests.sh
67#
68
69# Abort on errors (and uninitiliased variables)
70set -eu
71
72if [ -d library -a -d include -a -d tests ]; then :; else
73 echo "Must be run from mbed TLS root" >&2
74 exit 1
75fi
76
Andres AGb2fdd042016-09-22 14:17:46 +010077: ${OPENSSL:="openssl"}
78: ${OPENSSL_LEGACY:="$OPENSSL"}
79: ${GNUTLS_CLI:="gnutls-cli"}
80: ${GNUTLS_SERV:="gnutls-serv"}
81: ${GNUTLS_LEGACY_CLI:="$GNUTLS_CLI"}
82: ${GNUTLS_LEGACY_SERV:="$GNUTLS_SERV"}
83
Manuel Pégourié-Gonnardc2400d32020-06-08 12:59:27 +020084# Used to make ssl-opt.sh deterministic.
Manuel Pégourié-Gonnard1bff6842020-06-22 10:11:47 +020085#
86# See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
87# in sync. If you change the value here because it breaks some tests, you'll
88# definitely want to change it in all.sh as well.
Manuel Pégourié-Gonnardc2400d32020-06-08 12:59:27 +020089: ${SEED:=1}
90export SEED
91
Andres AGb2fdd042016-09-22 14:17:46 +010092# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
93# we just export the variables they require
94export OPENSSL_CMD="$OPENSSL"
95export GNUTLS_CLI="$GNUTLS_CLI"
96export GNUTLS_SERV="$GNUTLS_SERV"
97
Janos Follath7ccac852016-06-06 13:18:39 +010098CONFIG_H='include/mbedtls/config.h'
99CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +0000100
Janos Follathb72c6782016-07-19 14:54:17 +0100101# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +0100102OPENSSL="$OPENSSL" \
103 OPENSSL_LEGACY="$OPENSSL_LEGACY" \
104 GNUTLS_CLI="$GNUTLS_CLI" \
105 GNUTLS_SERV="$GNUTLS_SERV" \
106 GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
107 GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" \
108 scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +0100109echo
110
SimonB21ab9d72016-03-12 20:37:32 +0000111# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +0000112export CFLAGS=' --coverage -g3 -O0 '
SimonB098a3b52016-04-16 21:56:59 +0100113make clean
Janos Follath7ccac852016-06-06 13:18:39 +0100114cp "$CONFIG_H" "$CONFIG_BAK"
SimonB098a3b52016-04-16 21:56:59 +0100115scripts/config.pl full
Simon Butchercbb90752016-05-19 22:15:34 +0100116make -j
SimonB21ab9d72016-03-12 20:37:32 +0000117
118
119# Step 2 - Execute the tests
120TEST_OUTPUT=out_${PPID}
121cd tests
122
Gilles Peskined9701ae2020-04-09 18:33:34 +0200123if [ ! -f "seedfile" ]; then
124 dd if=/dev/urandom of="seedfile" bs=64 count=1
125fi
Gilles Peskine78c8e822020-04-09 18:50:08 +0200126echo
Gilles Peskined9701ae2020-04-09 18:33:34 +0200127
Gilles Peskine8bfe12b2020-04-09 18:29:42 +0200128# Step 2a - Unit Tests (keep going even if some tests fail)
Gilles Peskine78c8e822020-04-09 18:50:08 +0200129echo '################ Unit tests ################'
Jaeden Amero60ca6e52018-12-07 13:06:24 +0000130perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
Gilles Peskine78c8e822020-04-09 18:50:08 +0200131echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000132echo
133
Gilles Peskine8bfe12b2020-04-09 18:29:42 +0200134# Step 2b - System Tests (keep going even if some tests fail)
Gilles Peskine78c8e822020-04-09 18:50:08 +0200135echo
136echo '################ ssl-opt.sh ################'
SimonB21ab9d72016-03-12 20:37:32 +0000137sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
Gilles Peskine78c8e822020-04-09 18:50:08 +0200138echo '^^^^^^^^^^^^^^^^ ssl-opt.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000139echo
140
Gilles Peskine8bfe12b2020-04-09 18:29:42 +0200141# Step 2c - Compatibility tests (keep going even if some tests fail)
Gilles Peskine78c8e822020-04-09 18:50:08 +0200142echo '################ compat.sh ################'
143{
144 echo '#### compat.sh: Default versions'
145 sh compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2'
146 echo
147
148 echo '#### compat.sh: legacy (SSLv3)'
149 OPENSSL_CMD="$OPENSSL_LEGACY" sh compat.sh -m 'ssl3'
150 echo
151
152 echo '#### compat.sh: legacy (null, DES, RC4)'
153 OPENSSL_CMD="$OPENSSL_LEGACY" \
154 GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
155 sh compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR'
156 echo
157
158 echo '#### compat.sh: next (ARIA, ChaCha)'
159 OPENSSL_CMD="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
160 echo
161} | tee compat-test-$TEST_OUTPUT
162echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
SimonB21ab9d72016-03-12 20:37:32 +0000163echo
164
165# Step 3 - Process the coverage report
166cd ..
Gilles Peskineed1f6732020-04-09 18:32:48 +0200167{
168 make lcov
169 echo SUCCESS
170} | tee tests/cov-$TEST_OUTPUT
171
172if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
173 echo >&2 "Fatal: 'make lcov' failed"
174 exit 2
175fi
SimonB21ab9d72016-03-12 20:37:32 +0000176
177
178# Step 4 - Summarise the test report
179echo
180echo "========================================================================="
181echo "Test Report Summary"
182echo
183
184cd tests
185
Paul Bakker4b8bc522016-07-20 09:52:01 +0100186# Step 4a - Unit tests
SimonB21ab9d72016-03-12 20:37:32 +0000187echo "Unit tests - tests/scripts/run-test-suites.pl"
188
189PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
190SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
191TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
192FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
193
194echo "No test suites : $TOTAL_SUITES"
195echo "Passed : $PASSED_TESTS"
196echo "Failed : $FAILED_TESTS"
197echo "Skipped : $SKIPPED_TESTS"
198echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
199echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
200echo
201
Paul Bakker4b8bc522016-07-20 09:52:01 +0100202TOTAL_PASS=$PASSED_TESTS
203TOTAL_FAIL=$FAILED_TESTS
204TOTAL_SKIP=$SKIPPED_TESTS
205TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
206TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000207
Paul Bakker4b8bc522016-07-20 09:52:01 +0100208# Step 4b - TLS Options tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000209echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000210
211PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
212SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
213TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
214FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
215
216echo "Passed : $PASSED_TESTS"
217echo "Failed : $FAILED_TESTS"
218echo "Skipped : $SKIPPED_TESTS"
219echo "Total exec'd tests : $TOTAL_TESTS"
220echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
221echo
222
223TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
224TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
225TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
226TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
227TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
228
229
Paul Bakker4b8bc522016-07-20 09:52:01 +0100230# Step 4c - System Compatibility tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000231echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000232
Andres AGb2fdd042016-09-22 14:17:46 +0100233PASSED_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 }')
234SKIPPED_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 }')
235EXED_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 +0000236FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
237
238echo "Passed : $PASSED_TESTS"
239echo "Failed : $FAILED_TESTS"
240echo "Skipped : $SKIPPED_TESTS"
241echo "Total exec'd tests : $EXED_TESTS"
242echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
243echo
244
245TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
246TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
247TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
248TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
249TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
250
251
Paul Bakker4b8bc522016-07-20 09:52:01 +0100252# Step 4d - Grand totals
SimonB21ab9d72016-03-12 20:37:32 +0000253echo "-------------------------------------------------------------------------"
254echo "Total tests"
255
256echo "Total Passed : $TOTAL_PASS"
257echo "Total Failed : $TOTAL_FAIL"
258echo "Total Skipped : $TOTAL_SKIP"
259echo "Total exec'd tests : $TOTAL_EXED"
260echo "Total avail tests : $TOTAL_AVAIL"
261echo
262
263
Paul Bakker4b8bc522016-07-20 09:52:01 +0100264# Step 4e - Coverage
SimonB21ab9d72016-03-12 20:37:32 +0000265echo "Coverage"
266
Dan Handley8cb19812020-05-28 16:20:31 +0100267LINES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* lines)/\1/p')
268LINES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) lines)/\1/p')
269FUNCS_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* functions)$/\1/p')
270FUNCS_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) functions)$/\1/p')
271BRANCHES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* branches)$/\1/p')
272BRANCHES_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 +0000273
274LINES_PERCENT=$((1000*$LINES_TESTED/$LINES_TOTAL))
275LINES_PERCENT="$(($LINES_PERCENT/10)).$(($LINES_PERCENT-($LINES_PERCENT/10)*10))"
276
277FUNCS_PERCENT=$((1000*$FUNCS_TESTED/$FUNCS_TOTAL))
278FUNCS_PERCENT="$(($FUNCS_PERCENT/10)).$(($FUNCS_PERCENT-($FUNCS_PERCENT/10)*10))"
279
Dan Handley8cb19812020-05-28 16:20:31 +0100280BRANCHES_PERCENT=$((1000*$BRANCHES_TESTED/$BRANCHES_TOTAL))
281BRANCHES_PERCENT="$(($BRANCHES_PERCENT/10)).$(($BRANCHES_PERCENT-($BRANCHES_PERCENT/10)*10))"
282
SimonB21ab9d72016-03-12 20:37:32 +0000283echo "Lines Tested : $LINES_TESTED of $LINES_TOTAL $LINES_PERCENT%"
284echo "Functions Tested : $FUNCS_TESTED of $FUNCS_TOTAL $FUNCS_PERCENT%"
Dan Handley8cb19812020-05-28 16:20:31 +0100285echo "Branches Tested : $BRANCHES_TESTED of $BRANCHES_TOTAL $BRANCHES_PERCENT%"
SimonB21ab9d72016-03-12 20:37:32 +0000286echo
287
SimonB21ab9d72016-03-12 20:37:32 +0000288rm unit-test-$TEST_OUTPUT
289rm sys-test-$TEST_OUTPUT
290rm compat-test-$TEST_OUTPUT
291rm cov-$TEST_OUTPUT
292
293cd ..
Janos Follath7ccac852016-06-06 13:18:39 +0100294
295make clean
296
297if [ -f "$CONFIG_BAK" ]; then
298 mv "$CONFIG_BAK" "$CONFIG_H"
299fi
Gilles Peskinef54a5de2020-04-09 18:28:14 +0200300
301if [ $TOTAL_FAIL -ne 0 ]; then
302 exit 1
303fi