blob: ac9cb93cd5013c90eb8e78a25d31472566c69bf4 [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úti09b4f192020-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#
20# This file is part of Mbed TLS (https://tls.mbed.org)
SimonB21ab9d72016-03-12 20:37:32 +000021#
22# Purpose
23#
24# Executes the basic test suites, captures the results, and generates a simple
25# test report and code coverage report.
26#
27# The tests include:
SimonB21ab9d72016-03-12 20:37:32 +000028# * Unit tests - executed using tests/scripts/run-test-suite.pl
Paul Bakker4b8bc522016-07-20 09:52:01 +010029# * Self-tests - executed using the test suites above
SimonB21ab9d72016-03-12 20:37:32 +000030# * System tests - executed using tests/ssl-opt.sh
31# * Interoperability tests - executed using tests/compat.sh
32#
33# The tests focus on functionality and do not consider performance.
34#
35# Note the tests self-adapt due to configurations in include/mbedtls/config.h
36# which can lead to some tests being skipped, and can cause the number of
Paul Bakker4b8bc522016-07-20 09:52:01 +010037# available tests to fluctuate.
SimonB21ab9d72016-03-12 20:37:32 +000038#
39# This script has been written to be generic and should work on any shell.
40#
41# Usage: basic-build-tests.sh
42#
43
44# Abort on errors (and uninitiliased variables)
45set -eu
46
47if [ -d library -a -d include -a -d tests ]; then :; else
48 echo "Must be run from mbed TLS root" >&2
49 exit 1
50fi
51
Andres AGb2fdd042016-09-22 14:17:46 +010052: ${OPENSSL:="openssl"}
53: ${OPENSSL_LEGACY:="$OPENSSL"}
54: ${GNUTLS_CLI:="gnutls-cli"}
55: ${GNUTLS_SERV:="gnutls-serv"}
56: ${GNUTLS_LEGACY_CLI:="$GNUTLS_CLI"}
57: ${GNUTLS_LEGACY_SERV:="$GNUTLS_SERV"}
58
59# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
60# we just export the variables they require
61export OPENSSL_CMD="$OPENSSL"
62export GNUTLS_CLI="$GNUTLS_CLI"
63export GNUTLS_SERV="$GNUTLS_SERV"
64
Janos Follath7ccac852016-06-06 13:18:39 +010065CONFIG_H='include/mbedtls/config.h'
66CONFIG_BAK="$CONFIG_H.bak"
SimonB21ab9d72016-03-12 20:37:32 +000067
Janos Follathb72c6782016-07-19 14:54:17 +010068# Step 0 - print build environment info
Andres AGb2fdd042016-09-22 14:17:46 +010069OPENSSL="$OPENSSL" \
70 OPENSSL_LEGACY="$OPENSSL_LEGACY" \
71 GNUTLS_CLI="$GNUTLS_CLI" \
72 GNUTLS_SERV="$GNUTLS_SERV" \
73 GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
74 GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" \
75 scripts/output_env.sh
Janos Follathb72c6782016-07-19 14:54:17 +010076echo
77
SimonB21ab9d72016-03-12 20:37:32 +000078# Step 1 - Make and instrumented build for code coverage
Simon Butcherc2b0efc2016-03-18 18:28:43 +000079export CFLAGS=' --coverage -g3 -O0 '
SimonB098a3b52016-04-16 21:56:59 +010080make clean
Janos Follath7ccac852016-06-06 13:18:39 +010081cp "$CONFIG_H" "$CONFIG_BAK"
SimonB098a3b52016-04-16 21:56:59 +010082scripts/config.pl full
Simon Butchercbb90752016-05-19 22:15:34 +010083make -j
SimonB21ab9d72016-03-12 20:37:32 +000084
85
86# Step 2 - Execute the tests
87TEST_OUTPUT=out_${PPID}
88cd tests
89
Gilles Peskine9cc203a2020-04-09 18:33:34 +020090if [ ! -f "seedfile" ]; then
91 dd if=/dev/urandom of="seedfile" bs=64 count=1
92fi
93
Gilles Peskine27c36f02020-04-09 18:29:42 +020094# Step 2a - Unit Tests (keep going even if some tests fail)
Jaeden Amero3ee55792018-12-07 13:06:24 +000095perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +000096echo
97
Gilles Peskine27c36f02020-04-09 18:29:42 +020098# Step 2b - System Tests (keep going even if some tests fail)
SimonB21ab9d72016-03-12 20:37:32 +000099sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
100echo
101
Gilles Peskine27c36f02020-04-09 18:29:42 +0200102# Step 2c - Compatibility tests (keep going even if some tests fail)
Andres AGb2fdd042016-09-22 14:17:46 +0100103sh compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2' | \
104 tee compat-test-$TEST_OUTPUT
105OPENSSL_CMD="$OPENSSL_LEGACY" \
106 sh compat.sh -m 'ssl3' |tee -a compat-test-$TEST_OUTPUT
107OPENSSL_CMD="$OPENSSL_LEGACY" \
108 GNUTLS_CLI="$GNUTLS_LEGACY_CLI" \
109 GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
Andres Amaya Garciafea3d0a2019-02-19 20:20:57 +0000110 sh compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR' | \
Andres AGb2fdd042016-09-22 14:17:46 +0100111 tee -a compat-test-$TEST_OUTPUT
SimonB21ab9d72016-03-12 20:37:32 +0000112echo
113
114# Step 3 - Process the coverage report
115cd ..
Gilles Peskineb07541d2020-04-09 18:32:48 +0200116{
117 make lcov
118 echo SUCCESS
119} | tee tests/cov-$TEST_OUTPUT
120
121if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
122 echo >&2 "Fatal: 'make lcov' failed"
123 exit 2
124fi
SimonB21ab9d72016-03-12 20:37:32 +0000125
126
127# Step 4 - Summarise the test report
128echo
129echo "========================================================================="
130echo "Test Report Summary"
131echo
132
133cd tests
134
Paul Bakker4b8bc522016-07-20 09:52:01 +0100135# Step 4a - Unit tests
SimonB21ab9d72016-03-12 20:37:32 +0000136echo "Unit tests - tests/scripts/run-test-suites.pl"
137
138PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
139SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
140TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
141FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
142
143echo "No test suites : $TOTAL_SUITES"
144echo "Passed : $PASSED_TESTS"
145echo "Failed : $FAILED_TESTS"
146echo "Skipped : $SKIPPED_TESTS"
147echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
148echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
149echo
150
Paul Bakker4b8bc522016-07-20 09:52:01 +0100151TOTAL_PASS=$PASSED_TESTS
152TOTAL_FAIL=$FAILED_TESTS
153TOTAL_SKIP=$SKIPPED_TESTS
154TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
155TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
SimonB21ab9d72016-03-12 20:37:32 +0000156
Paul Bakker4b8bc522016-07-20 09:52:01 +0100157# Step 4b - TLS Options tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000158echo "TLS Options tests - tests/ssl-opt.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000159
160PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
161SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
162TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
163FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
164
165echo "Passed : $PASSED_TESTS"
166echo "Failed : $FAILED_TESTS"
167echo "Skipped : $SKIPPED_TESTS"
168echo "Total exec'd tests : $TOTAL_TESTS"
169echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
170echo
171
172TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
173TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
174TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
175TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
176TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
177
178
Paul Bakker4b8bc522016-07-20 09:52:01 +0100179# Step 4c - System Compatibility tests
Simon Butcherab0c51d2016-03-13 01:23:34 +0000180echo "System/Compatibility tests - tests/compat.sh"
SimonB21ab9d72016-03-12 20:37:32 +0000181
Andres AGb2fdd042016-09-22 14:17:46 +0100182PASSED_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 }')
183SKIPPED_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 }')
184EXED_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 +0000185FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
186
187echo "Passed : $PASSED_TESTS"
188echo "Failed : $FAILED_TESTS"
189echo "Skipped : $SKIPPED_TESTS"
190echo "Total exec'd tests : $EXED_TESTS"
191echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
192echo
193
194TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
195TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
196TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
197TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
198TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
199
200
Paul Bakker4b8bc522016-07-20 09:52:01 +0100201# Step 4d - Grand totals
SimonB21ab9d72016-03-12 20:37:32 +0000202echo "-------------------------------------------------------------------------"
203echo "Total tests"
204
205echo "Total Passed : $TOTAL_PASS"
206echo "Total Failed : $TOTAL_FAIL"
207echo "Total Skipped : $TOTAL_SKIP"
208echo "Total exec'd tests : $TOTAL_EXED"
209echo "Total avail tests : $TOTAL_AVAIL"
210echo
211
212
Paul Bakker4b8bc522016-07-20 09:52:01 +0100213# Step 4e - Coverage
SimonB21ab9d72016-03-12 20:37:32 +0000214echo "Coverage"
215
Dan Handleyaba9e222020-05-28 16:20:31 +0100216LINES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* lines)/\1/p')
217LINES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) lines)/\1/p')
218FUNCS_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* functions)$/\1/p')
219FUNCS_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) functions)$/\1/p')
220BRANCHES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* branches)$/\1/p')
221BRANCHES_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 +0000222
223LINES_PERCENT=$((1000*$LINES_TESTED/$LINES_TOTAL))
224LINES_PERCENT="$(($LINES_PERCENT/10)).$(($LINES_PERCENT-($LINES_PERCENT/10)*10))"
225
226FUNCS_PERCENT=$((1000*$FUNCS_TESTED/$FUNCS_TOTAL))
227FUNCS_PERCENT="$(($FUNCS_PERCENT/10)).$(($FUNCS_PERCENT-($FUNCS_PERCENT/10)*10))"
228
Dan Handleyaba9e222020-05-28 16:20:31 +0100229BRANCHES_PERCENT=$((1000*$BRANCHES_TESTED/$BRANCHES_TOTAL))
230BRANCHES_PERCENT="$(($BRANCHES_PERCENT/10)).$(($BRANCHES_PERCENT-($BRANCHES_PERCENT/10)*10))"
231
SimonB21ab9d72016-03-12 20:37:32 +0000232echo "Lines Tested : $LINES_TESTED of $LINES_TOTAL $LINES_PERCENT%"
233echo "Functions Tested : $FUNCS_TESTED of $FUNCS_TOTAL $FUNCS_PERCENT%"
Dan Handleyaba9e222020-05-28 16:20:31 +0100234echo "Branches Tested : $BRANCHES_TESTED of $BRANCHES_TOTAL $BRANCHES_PERCENT%"
SimonB21ab9d72016-03-12 20:37:32 +0000235echo
236
SimonB21ab9d72016-03-12 20:37:32 +0000237rm unit-test-$TEST_OUTPUT
238rm sys-test-$TEST_OUTPUT
239rm compat-test-$TEST_OUTPUT
240rm cov-$TEST_OUTPUT
241
242cd ..
Janos Follath7ccac852016-06-06 13:18:39 +0100243
244make clean
245
246if [ -f "$CONFIG_BAK" ]; then
247 mv "$CONFIG_BAK" "$CONFIG_H"
248fi
Gilles Peskinedd7ea382020-04-09 18:28:14 +0200249
250if [ $TOTAL_FAIL -ne 0 ]; then
251 exit 1
252fi