blob: f4f6f4e3592c2d69b94f730e07002748f2ec32e7 [file] [log] [blame]
SimonB21ab9d72016-03-12 20:37:32 +00001#!/bin/sh
2
3# basic-build-tests.sh
4#
5# Copyright (c) 2016, ARM Limited, All Rights Reserved
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#
Bence Szépkúti09b4f192020-05-26 01:54:15 +020047# This file is part of Mbed TLS (https://tls.mbed.org)
SimonB21ab9d72016-03-12 20:37:32 +000048#
49# Purpose
50#
51# Executes the basic test suites, captures the results, and generates a simple
52# test report and code coverage report.
53#
54# The tests include:
SimonB21ab9d72016-03-12 20:37:32 +000055# * Unit tests - executed using tests/scripts/run-test-suite.pl
Paul Bakker4b8bc522016-07-20 09:52:01 +010056# * Self-tests - executed using the test suites above
SimonB21ab9d72016-03-12 20:37:32 +000057# * System tests - executed using tests/ssl-opt.sh
58# * Interoperability tests - executed using tests/compat.sh
59#
60# The tests focus on functionality and do not consider performance.
61#
62# Note the tests self-adapt due to configurations in include/mbedtls/config.h
63# which can lead to some tests being skipped, and can cause the number of
Paul Bakker4b8bc522016-07-20 09:52:01 +010064# available tests to fluctuate.
SimonB21ab9d72016-03-12 20:37:32 +000065#
66# This script has been written to be generic and should work on any shell.
67#
68# Usage: basic-build-tests.sh
69#
70
71# Abort on errors (and uninitiliased variables)
72set -eu
73
74if [ -d library -a -d include -a -d tests ]; then :; else
75 echo "Must be run from mbed TLS root" >&2
76 exit 1
77fi
78
Andres AGb2fdd042016-09-22 14:17:46 +010079: ${OPENSSL:="openssl"}
80: ${OPENSSL_LEGACY:="$OPENSSL"}
81: ${GNUTLS_CLI:="gnutls-cli"}
82: ${GNUTLS_SERV:="gnutls-serv"}
83: ${GNUTLS_LEGACY_CLI:="$GNUTLS_CLI"}
84: ${GNUTLS_LEGACY_SERV:="$GNUTLS_SERV"}
85
Manuel Pégourié-Gonnard54d95b12020-06-08 12:59:27 +020086# Used to make ssl-opt.sh deterministic.
Manuel Pégourié-Gonnarde4a5c052020-06-22 10:11:47 +020087#
88# See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
89# in sync. If you change the value here because it breaks some tests, you'll
90# definitely want to change it in all.sh as well.
Manuel Pégourié-Gonnard54d95b12020-06-08 12:59:27 +020091: ${SEED:=1}
92export SEED
93
Andres AGb2fdd042016-09-22 14:17:46 +010094# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
95# we just export the variables they require
96export OPENSSL_CMD="$OPENSSL"
97export GNUTLS_CLI="$GNUTLS_CLI"
98export GNUTLS_SERV="$GNUTLS_SERV"
99
Janos Follath7ccac852016-06-06 13:18:39 +0100100CONFIG_H='include/mbedtls/config.h'
101CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +0000102
Janos Follathb72c6782016-07-19 14:54:17 +0100103# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +0100104OPENSSL="$OPENSSL" \
105 OPENSSL_LEGACY="$OPENSSL_LEGACY" \
106 GNUTLS_CLI="$GNUTLS_CLI" \
107 GNUTLS_SERV="$GNUTLS_SERV" \
108 GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
109 GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" \
110 scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +0100111echo
112
SimonB21ab9d72016-03-12 20:37:32 +0000113# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +0000114export CFLAGS=' --coverage -g3 -O0 '
SimonB098a3b52016-04-16 21:56:59 +0100115make clean
Janos Follath7ccac852016-06-06 13:18:39 +0100116cp "$CONFIG_H" "$CONFIG_BAK"
SimonB098a3b52016-04-16 21:56:59 +0100117scripts/config.pl full
Simon Butchercbb90752016-05-19 22:15:34 +0100118make -j
SimonB21ab9d72016-03-12 20:37:32 +0000119
120
121# Step 2 - Execute the tests
122TEST_OUTPUT=out_${PPID}
123cd tests
124
Gilles Peskine9cc203a2020-04-09 18:33:34 +0200125if [ ! -f "seedfile" ]; then
126 dd if=/dev/urandom of="seedfile" bs=64 count=1
127fi
128
Gilles Peskine27c36f02020-04-09 18:29:42 +0200129# Step 2a - Unit Tests (keep going even if some tests fail)
Jaeden Amero3ee55792018-12-07 13:06:24 +0000130perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000131echo
132
Gilles Peskine27c36f02020-04-09 18:29:42 +0200133# Step 2b - System Tests (keep going even if some tests fail)
SimonB21ab9d72016-03-12 20:37:32 +0000134sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
135echo
136
Gilles Peskine27c36f02020-04-09 18:29:42 +0200137# Step 2c - Compatibility tests (keep going even if some tests fail)
Andres AGb2fdd042016-09-22 14:17:46 +0100138sh compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2' | \
139 tee compat-test-$TEST_OUTPUT
140OPENSSL_CMD="$OPENSSL_LEGACY" \
141 sh compat.sh -m 'ssl3' |tee -a compat-test-$TEST_OUTPUT
142OPENSSL_CMD="$OPENSSL_LEGACY" \
143 GNUTLS_CLI="$GNUTLS_LEGACY_CLI" \
144 GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
Andres Amaya Garciafea3d0a2019-02-19 20:20:57 +0000145 sh compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR' | \
Andres AGb2fdd042016-09-22 14:17:46 +0100146 tee -a compat-test-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000147echo
148
149# Step 3 - Process the coverage report
150cd ..
Gilles Peskineb07541d2020-04-09 18:32:48 +0200151{
152 make lcov
153 echo SUCCESS
154} | tee tests/cov-$TEST_OUTPUT
155
156if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
157 echo >&2 "Fatal: 'make lcov' failed"
158 exit 2
159fi
SimonB21ab9d72016-03-12 20:37:32 +0000160
161
162# Step 4 - Summarise the test report
163echo
164echo "========================================================================="
165echo "Test Report Summary"
166echo
167
168cd tests
169
Paul Bakker4b8bc522016-07-20 09:52:01 +0100170# Step 4a - Unit tests
SimonB21ab9d72016-03-12 20:37:32 +0000171echo "Unit tests - tests/scripts/run-test-suites.pl"
172
173PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
174SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
175TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
176FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
177
178echo "No test suites : $TOTAL_SUITES"
179echo "Passed : $PASSED_TESTS"
180echo "Failed : $FAILED_TESTS"
181echo "Skipped : $SKIPPED_TESTS"
182echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
183echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
184echo
185
Paul Bakker4b8bc522016-07-20 09:52:01 +0100186TOTAL_PASS=$PASSED_TESTS
187TOTAL_FAIL=$FAILED_TESTS
188TOTAL_SKIP=$SKIPPED_TESTS
189TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
190TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000191
Paul Bakker4b8bc522016-07-20 09:52:01 +0100192# Step 4b - TLS Options tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000193echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000194
195PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
196SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
197TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
198FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
199
200echo "Passed : $PASSED_TESTS"
201echo "Failed : $FAILED_TESTS"
202echo "Skipped : $SKIPPED_TESTS"
203echo "Total exec'd tests : $TOTAL_TESTS"
204echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
205echo
206
207TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
208TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
209TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
210TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
211TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
212
213
Paul Bakker4b8bc522016-07-20 09:52:01 +0100214# Step 4c - System Compatibility tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000215echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000216
Andres AGb2fdd042016-09-22 14:17:46 +0100217PASSED_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 }')
218SKIPPED_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 }')
219EXED_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 +0000220FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
221
222echo "Passed : $PASSED_TESTS"
223echo "Failed : $FAILED_TESTS"
224echo "Skipped : $SKIPPED_TESTS"
225echo "Total exec'd tests : $EXED_TESTS"
226echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
227echo
228
229TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
230TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
231TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
232TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
233TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
234
235
Paul Bakker4b8bc522016-07-20 09:52:01 +0100236# Step 4d - Grand totals
SimonB21ab9d72016-03-12 20:37:32 +0000237echo "-------------------------------------------------------------------------"
238echo "Total tests"
239
240echo "Total Passed : $TOTAL_PASS"
241echo "Total Failed : $TOTAL_FAIL"
242echo "Total Skipped : $TOTAL_SKIP"
243echo "Total exec'd tests : $TOTAL_EXED"
244echo "Total avail tests : $TOTAL_AVAIL"
245echo
246
247
Paul Bakker4b8bc522016-07-20 09:52:01 +0100248# Step 4e - Coverage
SimonB21ab9d72016-03-12 20:37:32 +0000249echo "Coverage"
250
Dan Handleyaba9e222020-05-28 16:20:31 +0100251LINES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* lines)/\1/p')
252LINES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) lines)/\1/p')
253FUNCS_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* functions)$/\1/p')
254FUNCS_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) functions)$/\1/p')
255BRANCHES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* branches)$/\1/p')
256BRANCHES_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 +0000257
258LINES_PERCENT=$((1000*$LINES_TESTED/$LINES_TOTAL))
259LINES_PERCENT="$(($LINES_PERCENT/10)).$(($LINES_PERCENT-($LINES_PERCENT/10)*10))"
260
261FUNCS_PERCENT=$((1000*$FUNCS_TESTED/$FUNCS_TOTAL))
262FUNCS_PERCENT="$(($FUNCS_PERCENT/10)).$(($FUNCS_PERCENT-($FUNCS_PERCENT/10)*10))"
263
Dan Handleyaba9e222020-05-28 16:20:31 +0100264BRANCHES_PERCENT=$((1000*$BRANCHES_TESTED/$BRANCHES_TOTAL))
265BRANCHES_PERCENT="$(($BRANCHES_PERCENT/10)).$(($BRANCHES_PERCENT-($BRANCHES_PERCENT/10)*10))"
266
SimonB21ab9d72016-03-12 20:37:32 +0000267echo "Lines Tested : $LINES_TESTED of $LINES_TOTAL $LINES_PERCENT%"
268echo "Functions Tested : $FUNCS_TESTED of $FUNCS_TOTAL $FUNCS_PERCENT%"
Dan Handleyaba9e222020-05-28 16:20:31 +0100269echo "Branches Tested : $BRANCHES_TESTED of $BRANCHES_TOTAL $BRANCHES_PERCENT%"
SimonB21ab9d72016-03-12 20:37:32 +0000270echo
271
SimonB21ab9d72016-03-12 20:37:32 +0000272rm unit-test-$TEST_OUTPUT
273rm sys-test-$TEST_OUTPUT
274rm compat-test-$TEST_OUTPUT
275rm cov-$TEST_OUTPUT
276
277cd ..
Janos Follath7ccac852016-06-06 13:18:39 +0100278
279make clean
280
281if [ -f "$CONFIG_BAK" ]; then
282 mv "$CONFIG_BAK" "$CONFIG_H"
283fi
Gilles Peskinedd7ea382020-04-09 18:28:14 +0200284
285if [ $TOTAL_FAIL -ne 0 ]; then
286 exit 1
287fi