1#!/bin/sh
2
3# basic-build-test.sh
4#
5# Copyright The Mbed TLS Contributors
6# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7#
8# Purpose
9#
10# Executes the basic test suites, captures the results, and generates a simple
11# test report and code coverage report.
12#
13# The tests include:
14#   * Unit tests                - executed using tests/scripts/run-test-suite.pl
15#   * Self-tests                - executed using the test suites above
16#   * System tests              - executed using tests/ssl-opt.sh
17#   * Interoperability tests    - executed using tests/compat.sh
18#
19# The tests focus on functionality and do not consider performance.
20#
21# Note the tests self-adapt due to configurations in include/mbedtls/mbedtls_config.h
22# which can lead to some tests being skipped, and can cause the number of
23# available tests to fluctuate.
24#
25# This script has been written to be generic and should work on any shell.
26#
27# Usage: basic-build-test.sh
28#
29
30# Abort on errors (and uninitiliased variables)
31set -eu
32
33if [ -d library -a -d include -a -d tests ]; then :; else
34    echo "Must be run from Mbed TLS root" >&2
35    exit 1
36fi
37
38: ${OPENSSL:="openssl"}
39: ${GNUTLS_CLI:="gnutls-cli"}
40: ${GNUTLS_SERV:="gnutls-serv"}
41
42# Used to make ssl-opt.sh deterministic.
43#
44# See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
45# in sync. If you change the value here because it breaks some tests, you'll
46# definitely want to change it in all.sh as well.
47: ${SEED:=1}
48export SEED
49
50# if MAKEFLAGS is not set add the -j option to speed up invocations of make
51if [ -z "${MAKEFLAGS+set}" ]; then
52    export MAKEFLAGS="-j"
53fi
54
55# To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
56# we just export the variables they require
57export OPENSSL="$OPENSSL"
58export GNUTLS_CLI="$GNUTLS_CLI"
59export GNUTLS_SERV="$GNUTLS_SERV"
60
61CONFIG_H='include/mbedtls/mbedtls_config.h'
62CONFIG_BAK="$CONFIG_H.bak"
63
64# Step 0 - print build environment info
65OPENSSL="$OPENSSL"                           \
66    GNUTLS_CLI="$GNUTLS_CLI"                 \
67    GNUTLS_SERV="$GNUTLS_SERV"               \
68    scripts/output_env.sh
69echo
70
71# Step 1 - Make and instrumented build for code coverage
72export CFLAGS=' --coverage -g3 -O0 '
73export LDFLAGS=' --coverage'
74make clean
75cp "$CONFIG_H" "$CONFIG_BAK"
76scripts/config.py full
77make
78
79
80# Step 2 - Execute the tests
81TEST_OUTPUT=out_${PPID}
82cd tests
83if [ ! -f "seedfile" ]; then
84    dd if=/dev/urandom of="seedfile" bs=64 count=1
85fi
86echo
87
88# Step 2a - Unit Tests (keep going even if some tests fail)
89echo '################ Unit tests ################'
90perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
91echo '^^^^^^^^^^^^^^^^ Unit tests ^^^^^^^^^^^^^^^^'
92echo
93
94# Step 2b - System Tests (keep going even if some tests fail)
95echo
96echo '################ ssl-opt.sh ################'
97echo "ssl-opt.sh will use SEED=$SEED for udp_proxy"
98sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
99echo '^^^^^^^^^^^^^^^^ ssl-opt.sh ^^^^^^^^^^^^^^^^'
100echo
101
102# Step 2c - Compatibility tests (keep going even if some tests fail)
103echo '################ compat.sh ################'
104{
105    echo '#### compat.sh: Default versions'
106    sh compat.sh
107    echo
108
109    echo '#### compat.sh: null cipher'
110    sh compat.sh -e '^$' -f 'NULL'
111    echo
112
113    echo '#### compat.sh: next (ARIA, ChaCha)'
114    OPENSSL="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
115    echo
116} | tee compat-test-$TEST_OUTPUT
117echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
118echo
119
120# Step 3 - Process the coverage report
121cd ..
122{
123    make lcov
124    echo SUCCESS
125} | tee tests/cov-$TEST_OUTPUT
126
127if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
128    echo >&2 "Fatal: 'make lcov' failed"
129    exit 2
130fi
131
132
133# Step 4 - Summarise the test report
134echo
135echo "========================================================================="
136echo "Test Report Summary"
137echo
138
139# A failure of the left-hand side of a pipe is ignored (this is a limitation
140# of sh). We'll use the presence of this file as a marker that the generation
141# of the report succeeded.
142rm -f "tests/basic-build-test-$$.ok"
143
144{
145
146    cd tests
147
148    # Step 4a - Unit tests
149    echo "Unit tests - tests/scripts/run-test-suites.pl"
150
151    PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
152    SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
153    TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
154    FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
155
156    echo "No test suites     : $TOTAL_SUITES"
157    echo "Passed             : $PASSED_TESTS"
158    echo "Failed             : $FAILED_TESTS"
159    echo "Skipped            : $SKIPPED_TESTS"
160    echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
161    echo "Total avail tests  : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
162    echo
163
164    TOTAL_PASS=$PASSED_TESTS
165    TOTAL_FAIL=$FAILED_TESTS
166    TOTAL_SKIP=$SKIPPED_TESTS
167    TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
168    TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
169
170    # Step 4b - TLS Options tests
171    echo "TLS Options tests - tests/ssl-opt.sh"
172
173    PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
174    SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
175    TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
176    FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
177
178    echo "Passed             : $PASSED_TESTS"
179    echo "Failed             : $FAILED_TESTS"
180    echo "Skipped            : $SKIPPED_TESTS"
181    echo "Total exec'd tests : $TOTAL_TESTS"
182    echo "Total avail tests  : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
183    echo
184
185    TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
186    TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
187    TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
188    TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
189    TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
190
191
192    # Step 4c - System Compatibility tests
193    echo "System/Compatibility tests - tests/compat.sh"
194
195    PASSED_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 }')
196    SKIPPED_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 }')
197    EXED_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 }')
198    FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
199
200    echo "Passed             : $PASSED_TESTS"
201    echo "Failed             : $FAILED_TESTS"
202    echo "Skipped            : $SKIPPED_TESTS"
203    echo "Total exec'd tests : $EXED_TESTS"
204    echo "Total avail tests  : $(($EXED_TESTS + $SKIPPED_TESTS))"
205    echo
206
207    TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
208    TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
209    TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
210    TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
211    TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
212
213
214    # Step 4d - Grand totals
215    echo "-------------------------------------------------------------------------"
216    echo "Total tests"
217
218    echo "Total Passed       : $TOTAL_PASS"
219    echo "Total Failed       : $TOTAL_FAIL"
220    echo "Total Skipped      : $TOTAL_SKIP"
221    echo "Total exec'd tests : $TOTAL_EXED"
222    echo "Total avail tests  : $TOTAL_AVAIL"
223    echo
224
225
226    # Step 4e - Coverage report
227    echo "Coverage statistics:"
228    sed -n '1,/^Overall coverage/d; /%/p' cov-$TEST_OUTPUT
229    echo
230
231    rm unit-test-$TEST_OUTPUT
232    rm sys-test-$TEST_OUTPUT
233    rm compat-test-$TEST_OUTPUT
234    rm cov-$TEST_OUTPUT
235
236    # Mark the report generation as having succeeded. This must be the
237    # last thing in the report generation.
238    touch "basic-build-test-$$.ok"
239} | tee coverage-summary.txt
240
241make clean
242
243if [ -f "$CONFIG_BAK" ]; then
244    mv "$CONFIG_BAK" "$CONFIG_H"
245fi
246
247# The file must exist, otherwise it means something went wrong while generating
248# the coverage report. If something did go wrong, rm will complain so this
249# script will exit with a failure status.
250rm "tests/basic-build-test-$$.ok"
251