blob: b8f624256a6aac4552f91390d3b8b16fccd8a65f [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
126
Gilles Peskine8bfe12b2020-04-09 18:29:42 +0200127# Step 2a - Unit Tests (keep going even if some tests fail)
Jaeden Amero60ca6e52018-12-07 13:06:24 +0000128perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000129echo
130
Gilles Peskine8bfe12b2020-04-09 18:29:42 +0200131# Step 2b - System Tests (keep going even if some tests fail)
SimonB21ab9d72016-03-12 20:37:32 +0000132sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
133echo
134
Gilles Peskine8bfe12b2020-04-09 18:29:42 +0200135# Step 2c - Compatibility tests (keep going even if some tests fail)
Andres AGb2fdd042016-09-22 14:17:46 +0100136sh compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2' | \
137 tee compat-test-$TEST_OUTPUT
138OPENSSL_CMD="$OPENSSL_LEGACY" \
139 sh compat.sh -m 'ssl3' |tee -a compat-test-$TEST_OUTPUT
140OPENSSL_CMD="$OPENSSL_LEGACY" \
141 GNUTLS_CLI="$GNUTLS_LEGACY_CLI" \
142 GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
Andres Amaya Garcia37e0a8c2019-02-19 20:20:57 +0000143 sh compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR' | \
Andres AGb2fdd042016-09-22 14:17:46 +0100144 tee -a compat-test-$TEST_OUTPUT
Manuel Pégourié-Gonnard79bf3272018-11-05 11:57:03 +0100145OPENSSL_CMD="$OPENSSL_NEXT" \
146 sh compat.sh -e '^$' -f 'ARIA\|CHACHA' | \
147 tee -a compat-test-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000148echo
149
150# Step 3 - Process the coverage report
151cd ..
Gilles Peskineed1f6732020-04-09 18:32:48 +0200152{
153 make lcov
154 echo SUCCESS
155} | tee tests/cov-$TEST_OUTPUT
156
157if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
158 echo >&2 "Fatal: 'make lcov' failed"
159 exit 2
160fi
SimonB21ab9d72016-03-12 20:37:32 +0000161
162
163# Step 4 - Summarise the test report
164echo
165echo "========================================================================="
166echo "Test Report Summary"
167echo
168
169cd tests
170
Paul Bakker4b8bc522016-07-20 09:52:01 +0100171# Step 4a - Unit tests
SimonB21ab9d72016-03-12 20:37:32 +0000172echo "Unit tests - tests/scripts/run-test-suites.pl"
173
174PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
175SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
176TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
177FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
178
179echo "No test suites : $TOTAL_SUITES"
180echo "Passed : $PASSED_TESTS"
181echo "Failed : $FAILED_TESTS"
182echo "Skipped : $SKIPPED_TESTS"
183echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
184echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
185echo
186
Paul Bakker4b8bc522016-07-20 09:52:01 +0100187TOTAL_PASS=$PASSED_TESTS
188TOTAL_FAIL=$FAILED_TESTS
189TOTAL_SKIP=$SKIPPED_TESTS
190TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
191TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000192
Paul Bakker4b8bc522016-07-20 09:52:01 +0100193# Step 4b - TLS Options tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000194echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000195
196PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
197SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
198TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
199FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
200
201echo "Passed : $PASSED_TESTS"
202echo "Failed : $FAILED_TESTS"
203echo "Skipped : $SKIPPED_TESTS"
204echo "Total exec'd tests : $TOTAL_TESTS"
205echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
206echo
207
208TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
209TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
210TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
211TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
212TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
213
214
Paul Bakker4b8bc522016-07-20 09:52:01 +0100215# Step 4c - System Compatibility tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000216echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000217
Andres AGb2fdd042016-09-22 14:17:46 +0100218PASSED_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 }')
219SKIPPED_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 }')
220EXED_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 +0000221FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
222
223echo "Passed : $PASSED_TESTS"
224echo "Failed : $FAILED_TESTS"
225echo "Skipped : $SKIPPED_TESTS"
226echo "Total exec'd tests : $EXED_TESTS"
227echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
228echo
229
230TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
231TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
232TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
233TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
234TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
235
236
Paul Bakker4b8bc522016-07-20 09:52:01 +0100237# Step 4d - Grand totals
SimonB21ab9d72016-03-12 20:37:32 +0000238echo "-------------------------------------------------------------------------"
239echo "Total tests"
240
241echo "Total Passed : $TOTAL_PASS"
242echo "Total Failed : $TOTAL_FAIL"
243echo "Total Skipped : $TOTAL_SKIP"
244echo "Total exec'd tests : $TOTAL_EXED"
245echo "Total avail tests : $TOTAL_AVAIL"
246echo
247
248
Paul Bakker4b8bc522016-07-20 09:52:01 +0100249# Step 4e - Coverage
SimonB21ab9d72016-03-12 20:37:32 +0000250echo "Coverage"
251
Dan Handley8cb19812020-05-28 16:20:31 +0100252LINES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* lines)/\1/p')
253LINES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) lines)/\1/p')
254FUNCS_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* functions)$/\1/p')
255FUNCS_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) functions)$/\1/p')
256BRANCHES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* branches)$/\1/p')
257BRANCHES_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 +0000258
259LINES_PERCENT=$((1000*$LINES_TESTED/$LINES_TOTAL))
260LINES_PERCENT="$(($LINES_PERCENT/10)).$(($LINES_PERCENT-($LINES_PERCENT/10)*10))"
261
262FUNCS_PERCENT=$((1000*$FUNCS_TESTED/$FUNCS_TOTAL))
263FUNCS_PERCENT="$(($FUNCS_PERCENT/10)).$(($FUNCS_PERCENT-($FUNCS_PERCENT/10)*10))"
264
Dan Handley8cb19812020-05-28 16:20:31 +0100265BRANCHES_PERCENT=$((1000*$BRANCHES_TESTED/$BRANCHES_TOTAL))
266BRANCHES_PERCENT="$(($BRANCHES_PERCENT/10)).$(($BRANCHES_PERCENT-($BRANCHES_PERCENT/10)*10))"
267
SimonB21ab9d72016-03-12 20:37:32 +0000268echo "Lines Tested : $LINES_TESTED of $LINES_TOTAL $LINES_PERCENT%"
269echo "Functions Tested : $FUNCS_TESTED of $FUNCS_TOTAL $FUNCS_PERCENT%"
Dan Handley8cb19812020-05-28 16:20:31 +0100270echo "Branches Tested : $BRANCHES_TESTED of $BRANCHES_TOTAL $BRANCHES_PERCENT%"
SimonB21ab9d72016-03-12 20:37:32 +0000271echo
272
SimonB21ab9d72016-03-12 20:37:32 +0000273rm unit-test-$TEST_OUTPUT
274rm sys-test-$TEST_OUTPUT
275rm compat-test-$TEST_OUTPUT
276rm cov-$TEST_OUTPUT
277
278cd ..
Janos Follath7ccac852016-06-06 13:18:39 +0100279
280make clean
281
282if [ -f "$CONFIG_BAK" ]; then
283 mv "$CONFIG_BAK" "$CONFIG_H"
284fi
Gilles Peskinef54a5de2020-04-09 18:28:14 +0200285
286if [ $TOTAL_FAIL -ne 0 ]; then
287 exit 1
288fi