blob: e0eee031bee464136ae302f83274b9a0a972ea42 [file] [log] [blame]
SimonB21ab9d72016-03-12 20:37:32 +00001#!/bin/sh
2
3# basic-build-tests.sh
4#
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02005# Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti09b4f192020-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úti4e9f7122020-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é-Gonnard54d95b12020-06-08 12:59:27 +020084# Used to make ssl-opt.sh deterministic.
Manuel Pégourié-Gonnarde4a5c052020-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é-Gonnard54d95b12020-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 Peskine9cc203a2020-04-09 18:33:34 +0200123if [ ! -f "seedfile" ]; then
124 dd if=/dev/urandom of="seedfile" bs=64 count=1
125fi
126
Gilles Peskine27c36f02020-04-09 18:29:42 +0200127# Step 2a - Unit Tests (keep going even if some tests fail)
Jaeden Amero3ee55792018-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 Peskine27c36f02020-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 Peskine27c36f02020-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 Garciafea3d0a2019-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
SimonB21ab9d72016-03-12 20:37:32 +0000145echo
146
147# Step 3 - Process the coverage report
148cd ..
Gilles Peskineb07541d2020-04-09 18:32:48 +0200149{
150 make lcov
151 echo SUCCESS
152} | tee tests/cov-$TEST_OUTPUT
153
154if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
155 echo >&2 "Fatal: 'make lcov' failed"
156 exit 2
157fi
SimonB21ab9d72016-03-12 20:37:32 +0000158
159
160# Step 4 - Summarise the test report
161echo
162echo "========================================================================="
163echo "Test Report Summary"
164echo
165
166cd tests
167
Paul Bakker4b8bc522016-07-20 09:52:01 +0100168# Step 4a - Unit tests
SimonB21ab9d72016-03-12 20:37:32 +0000169echo "Unit tests - tests/scripts/run-test-suites.pl"
170
171PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
172SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
173TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
174FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
175
176echo "No test suites : $TOTAL_SUITES"
177echo "Passed : $PASSED_TESTS"
178echo "Failed : $FAILED_TESTS"
179echo "Skipped : $SKIPPED_TESTS"
180echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
181echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
182echo
183
Paul Bakker4b8bc522016-07-20 09:52:01 +0100184TOTAL_PASS=$PASSED_TESTS
185TOTAL_FAIL=$FAILED_TESTS
186TOTAL_SKIP=$SKIPPED_TESTS
187TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
188TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000189
Paul Bakker4b8bc522016-07-20 09:52:01 +0100190# Step 4b - TLS Options tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000191echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000192
193PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
194SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
195TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
196FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
197
198echo "Passed : $PASSED_TESTS"
199echo "Failed : $FAILED_TESTS"
200echo "Skipped : $SKIPPED_TESTS"
201echo "Total exec'd tests : $TOTAL_TESTS"
202echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
203echo
204
205TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
206TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
207TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
208TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
209TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
210
211
Paul Bakker4b8bc522016-07-20 09:52:01 +0100212# Step 4c - System Compatibility tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000213echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000214
Andres AGb2fdd042016-09-22 14:17:46 +0100215PASSED_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 }')
216SKIPPED_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 }')
217EXED_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 +0000218FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
219
220echo "Passed : $PASSED_TESTS"
221echo "Failed : $FAILED_TESTS"
222echo "Skipped : $SKIPPED_TESTS"
223echo "Total exec'd tests : $EXED_TESTS"
224echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
225echo
226
227TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
228TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
229TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
230TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
231TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
232
233
Paul Bakker4b8bc522016-07-20 09:52:01 +0100234# Step 4d - Grand totals
SimonB21ab9d72016-03-12 20:37:32 +0000235echo "-------------------------------------------------------------------------"
236echo "Total tests"
237
238echo "Total Passed : $TOTAL_PASS"
239echo "Total Failed : $TOTAL_FAIL"
240echo "Total Skipped : $TOTAL_SKIP"
241echo "Total exec'd tests : $TOTAL_EXED"
242echo "Total avail tests : $TOTAL_AVAIL"
243echo
244
245
Paul Bakker4b8bc522016-07-20 09:52:01 +0100246# Step 4e - Coverage
SimonB21ab9d72016-03-12 20:37:32 +0000247echo "Coverage"
248
Dan Handleyaba9e222020-05-28 16:20:31 +0100249LINES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* lines)/\1/p')
250LINES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) lines)/\1/p')
251FUNCS_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* functions)$/\1/p')
252FUNCS_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) functions)$/\1/p')
253BRANCHES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* branches)$/\1/p')
254BRANCHES_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 +0000255
256LINES_PERCENT=$((1000*$LINES_TESTED/$LINES_TOTAL))
257LINES_PERCENT="$(($LINES_PERCENT/10)).$(($LINES_PERCENT-($LINES_PERCENT/10)*10))"
258
259FUNCS_PERCENT=$((1000*$FUNCS_TESTED/$FUNCS_TOTAL))
260FUNCS_PERCENT="$(($FUNCS_PERCENT/10)).$(($FUNCS_PERCENT-($FUNCS_PERCENT/10)*10))"
261
Dan Handleyaba9e222020-05-28 16:20:31 +0100262BRANCHES_PERCENT=$((1000*$BRANCHES_TESTED/$BRANCHES_TOTAL))
263BRANCHES_PERCENT="$(($BRANCHES_PERCENT/10)).$(($BRANCHES_PERCENT-($BRANCHES_PERCENT/10)*10))"
264
SimonB21ab9d72016-03-12 20:37:32 +0000265echo "Lines Tested : $LINES_TESTED of $LINES_TOTAL $LINES_PERCENT%"
266echo "Functions Tested : $FUNCS_TESTED of $FUNCS_TOTAL $FUNCS_PERCENT%"
Dan Handleyaba9e222020-05-28 16:20:31 +0100267echo "Branches Tested : $BRANCHES_TESTED of $BRANCHES_TOTAL $BRANCHES_PERCENT%"
SimonB21ab9d72016-03-12 20:37:32 +0000268echo
269
SimonB21ab9d72016-03-12 20:37:32 +0000270rm unit-test-$TEST_OUTPUT
271rm sys-test-$TEST_OUTPUT
272rm compat-test-$TEST_OUTPUT
273rm cov-$TEST_OUTPUT
274
275cd ..
Janos Follath7ccac852016-06-06 13:18:39 +0100276
277make clean
278
279if [ -f "$CONFIG_BAK" ]; then
280 mv "$CONFIG_BAK" "$CONFIG_H"
281fi
Gilles Peskinedd7ea382020-04-09 18:28:14 +0200282
283if [ $TOTAL_FAIL -ne 0 ]; then
284 exit 1
285fi