basic-build-test.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #!/bin/sh
  2. # basic-build-tests.sh
  3. #
  4. # Copyright The Mbed TLS Contributors
  5. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. #
  7. # This file is provided under the Apache License 2.0, or the
  8. # GNU General Public License v2.0 or later.
  9. #
  10. # **********
  11. # Apache License 2.0:
  12. #
  13. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. # not use this file except in compliance with the License.
  15. # You may obtain a copy of the License at
  16. #
  17. # http://www.apache.org/licenses/LICENSE-2.0
  18. #
  19. # Unless required by applicable law or agreed to in writing, software
  20. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. # See the License for the specific language governing permissions and
  23. # limitations under the License.
  24. #
  25. # **********
  26. #
  27. # **********
  28. # GNU General Public License v2.0 or later:
  29. #
  30. # This program is free software; you can redistribute it and/or modify
  31. # it under the terms of the GNU General Public License as published by
  32. # the Free Software Foundation; either version 2 of the License, or
  33. # (at your option) any later version.
  34. #
  35. # This program is distributed in the hope that it will be useful,
  36. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. # GNU General Public License for more details.
  39. #
  40. # You should have received a copy of the GNU General Public License along
  41. # with this program; if not, write to the Free Software Foundation, Inc.,
  42. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. #
  44. # **********
  45. #
  46. # Purpose
  47. #
  48. # Executes the basic test suites, captures the results, and generates a simple
  49. # test report and code coverage report.
  50. #
  51. # The tests include:
  52. # * Unit tests - executed using tests/scripts/run-test-suite.pl
  53. # * Self-tests - executed using the test suites above
  54. # * System tests - executed using tests/ssl-opt.sh
  55. # * Interoperability tests - executed using tests/compat.sh
  56. #
  57. # The tests focus on functionality and do not consider performance.
  58. #
  59. # Note the tests self-adapt due to configurations in include/mbedtls/config.h
  60. # which can lead to some tests being skipped, and can cause the number of
  61. # available tests to fluctuate.
  62. #
  63. # This script has been written to be generic and should work on any shell.
  64. #
  65. # Usage: basic-build-tests.sh
  66. #
  67. # Abort on errors (and uninitiliased variables)
  68. set -eu
  69. if [ -d library -a -d include -a -d tests ]; then :; else
  70. echo "Must be run from mbed TLS root" >&2
  71. exit 1
  72. fi
  73. : ${OPENSSL:="openssl"}
  74. : ${OPENSSL_LEGACY:="$OPENSSL"}
  75. : ${GNUTLS_CLI:="gnutls-cli"}
  76. : ${GNUTLS_SERV:="gnutls-serv"}
  77. : ${GNUTLS_LEGACY_CLI:="$GNUTLS_CLI"}
  78. : ${GNUTLS_LEGACY_SERV:="$GNUTLS_SERV"}
  79. # Used to make ssl-opt.sh deterministic.
  80. #
  81. # See also RELEASE_SEED in all.sh. Debugging is easier if both values are kept
  82. # in sync. If you change the value here because it breaks some tests, you'll
  83. # definitely want to change it in all.sh as well.
  84. : ${SEED:=1}
  85. export SEED
  86. # To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
  87. # we just export the variables they require
  88. export OPENSSL_CMD="$OPENSSL"
  89. export GNUTLS_CLI="$GNUTLS_CLI"
  90. export GNUTLS_SERV="$GNUTLS_SERV"
  91. CONFIG_H='include/mbedtls/config.h'
  92. CONFIG_BAK="$CONFIG_H.bak"
  93. # Step 0 - print build environment info
  94. OPENSSL="$OPENSSL" \
  95. OPENSSL_LEGACY="$OPENSSL_LEGACY" \
  96. GNUTLS_CLI="$GNUTLS_CLI" \
  97. GNUTLS_SERV="$GNUTLS_SERV" \
  98. GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
  99. GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" \
  100. scripts/output_env.sh
  101. echo
  102. # Step 1 - Make and instrumented build for code coverage
  103. export CFLAGS=' --coverage -g3 -O0 '
  104. make clean
  105. cp "$CONFIG_H" "$CONFIG_BAK"
  106. scripts/config.pl full
  107. make -j
  108. # Step 2 - Execute the tests
  109. TEST_OUTPUT=out_${PPID}
  110. cd tests
  111. if [ ! -f "seedfile" ]; then
  112. dd if=/dev/urandom of="seedfile" bs=64 count=1
  113. fi
  114. # Step 2a - Unit Tests (keep going even if some tests fail)
  115. perl scripts/run-test-suites.pl -v 2 |tee unit-test-$TEST_OUTPUT
  116. echo
  117. # Step 2b - System Tests (keep going even if some tests fail)
  118. sh ssl-opt.sh |tee sys-test-$TEST_OUTPUT
  119. echo
  120. # Step 2c - Compatibility tests (keep going even if some tests fail)
  121. sh compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2' | \
  122. tee compat-test-$TEST_OUTPUT
  123. OPENSSL_CMD="$OPENSSL_LEGACY" \
  124. sh compat.sh -m 'ssl3' |tee -a compat-test-$TEST_OUTPUT
  125. OPENSSL_CMD="$OPENSSL_LEGACY" \
  126. GNUTLS_CLI="$GNUTLS_LEGACY_CLI" \
  127. GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
  128. sh compat.sh -e '^$' -f 'NULL\|DES\|RC4\|ARCFOUR' | \
  129. tee -a compat-test-$TEST_OUTPUT
  130. OPENSSL_CMD="$OPENSSL_NEXT" \
  131. sh compat.sh -e '^$' -f 'ARIA\|CHACHA' | \
  132. tee -a compat-test-$TEST_OUTPUT
  133. echo
  134. # Step 3 - Process the coverage report
  135. cd ..
  136. {
  137. make lcov
  138. echo SUCCESS
  139. } | tee tests/cov-$TEST_OUTPUT
  140. if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
  141. echo >&2 "Fatal: 'make lcov' failed"
  142. exit 2
  143. fi
  144. # Step 4 - Summarise the test report
  145. echo
  146. echo "========================================================================="
  147. echo "Test Report Summary"
  148. echo
  149. cd tests
  150. # Step 4a - Unit tests
  151. echo "Unit tests - tests/scripts/run-test-suites.pl"
  152. PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
  153. SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
  154. TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
  155. FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
  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. TOTAL_PASS=$PASSED_TESTS
  164. TOTAL_FAIL=$FAILED_TESTS
  165. TOTAL_SKIP=$SKIPPED_TESTS
  166. TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
  167. TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
  168. # Step 4b - TLS Options tests
  169. echo "TLS Options tests - tests/ssl-opt.sh"
  170. PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
  171. SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
  172. TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
  173. FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
  174. echo "Passed : $PASSED_TESTS"
  175. echo "Failed : $FAILED_TESTS"
  176. echo "Skipped : $SKIPPED_TESTS"
  177. echo "Total exec'd tests : $TOTAL_TESTS"
  178. echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
  179. echo
  180. TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
  181. TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
  182. TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
  183. TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
  184. TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
  185. # Step 4c - System Compatibility tests
  186. echo "System/Compatibility tests - tests/compat.sh"
  187. 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 }')
  188. 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 }')
  189. 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 }')
  190. FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
  191. echo "Passed : $PASSED_TESTS"
  192. echo "Failed : $FAILED_TESTS"
  193. echo "Skipped : $SKIPPED_TESTS"
  194. echo "Total exec'd tests : $EXED_TESTS"
  195. echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
  196. echo
  197. TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
  198. TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
  199. TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
  200. TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
  201. TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
  202. # Step 4d - Grand totals
  203. echo "-------------------------------------------------------------------------"
  204. echo "Total tests"
  205. echo "Total Passed : $TOTAL_PASS"
  206. echo "Total Failed : $TOTAL_FAIL"
  207. echo "Total Skipped : $TOTAL_SKIP"
  208. echo "Total exec'd tests : $TOTAL_EXED"
  209. echo "Total avail tests : $TOTAL_AVAIL"
  210. echo
  211. # Step 4e - Coverage
  212. echo "Coverage"
  213. LINES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* lines)/\1/p')
  214. LINES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ lines......: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) lines)/\1/p')
  215. FUNCS_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* functions)$/\1/p')
  216. FUNCS_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ functions..: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) functions)$/\1/p')
  217. BRANCHES_TESTED=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% (\([0-9]*\) of [0-9]* branches)$/\1/p')
  218. BRANCHES_TOTAL=$(tail -n4 cov-$TEST_OUTPUT|sed -n -e 's/ branches...: [0-9]*.[0-9]% ([0-9]* of \([0-9]*\) branches)$/\1/p')
  219. LINES_PERCENT=$((1000*$LINES_TESTED/$LINES_TOTAL))
  220. LINES_PERCENT="$(($LINES_PERCENT/10)).$(($LINES_PERCENT-($LINES_PERCENT/10)*10))"
  221. FUNCS_PERCENT=$((1000*$FUNCS_TESTED/$FUNCS_TOTAL))
  222. FUNCS_PERCENT="$(($FUNCS_PERCENT/10)).$(($FUNCS_PERCENT-($FUNCS_PERCENT/10)*10))"
  223. BRANCHES_PERCENT=$((1000*$BRANCHES_TESTED/$BRANCHES_TOTAL))
  224. BRANCHES_PERCENT="$(($BRANCHES_PERCENT/10)).$(($BRANCHES_PERCENT-($BRANCHES_PERCENT/10)*10))"
  225. echo "Lines Tested : $LINES_TESTED of $LINES_TOTAL $LINES_PERCENT%"
  226. echo "Functions Tested : $FUNCS_TESTED of $FUNCS_TOTAL $FUNCS_PERCENT%"
  227. echo "Branches Tested : $BRANCHES_TESTED of $BRANCHES_TOTAL $BRANCHES_PERCENT%"
  228. echo
  229. rm unit-test-$TEST_OUTPUT
  230. rm sys-test-$TEST_OUTPUT
  231. rm compat-test-$TEST_OUTPUT
  232. rm cov-$TEST_OUTPUT
  233. cd ..
  234. make clean
  235. if [ -f "$CONFIG_BAK" ]; then
  236. mv "$CONFIG_BAK" "$CONFIG_H"
  237. fi
  238. if [ $TOTAL_FAIL -ne 0 ]; then
  239. exit 1
  240. fi