basic-build-test.sh 11 KB

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