basic-build-test.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/bin/sh
  2. # basic-build-test.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/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-test.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="$OPENSSL"
  66. export GNUTLS_CLI="$GNUTLS_CLI"
  67. export GNUTLS_SERV="$GNUTLS_SERV"
  68. CONFIG_H='include/mbedtls/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
  110. echo
  111. echo '#### compat.sh: legacy (null)'
  112. OPENSSL="$OPENSSL_LEGACY" \
  113. GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" \
  114. sh compat.sh -e '^$' -f 'NULL'
  115. echo
  116. echo '#### compat.sh: next (ARIA, ChaCha)'
  117. OPENSSL="$OPENSSL_NEXT" sh compat.sh -e '^$' -f 'ARIA\|CHACHA'
  118. echo
  119. } | tee compat-test-$TEST_OUTPUT
  120. echo '^^^^^^^^^^^^^^^^ compat.sh ^^^^^^^^^^^^^^^^'
  121. echo
  122. # Step 3 - Process the coverage report
  123. cd ..
  124. {
  125. make lcov
  126. echo SUCCESS
  127. } | tee tests/cov-$TEST_OUTPUT
  128. if [ "$(tail -n1 tests/cov-$TEST_OUTPUT)" != "SUCCESS" ]; then
  129. echo >&2 "Fatal: 'make lcov' failed"
  130. exit 2
  131. fi
  132. # Step 4 - Summarise the test report
  133. echo
  134. echo "========================================================================="
  135. echo "Test Report Summary"
  136. echo
  137. # A failure of the left-hand side of a pipe is ignored (this is a limitation
  138. # of sh). We'll use the presence of this file as a marker that the generation
  139. # of the report succeeded.
  140. rm -f "tests/basic-build-test-$$.ok"
  141. {
  142. cd tests
  143. # Step 4a - Unit tests
  144. echo "Unit tests - tests/scripts/run-test-suites.pl"
  145. PASSED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/test cases passed :[\t]*\([0-9]*\)/\1/p'| tr -d ' ')
  146. SKIPPED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/skipped :[ \t]*\([0-9]*\)/\1/p'| tr -d ' ')
  147. TOTAL_SUITES=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) .*, [0-9]* tests run)/\1/p'| tr -d ' ')
  148. FAILED_TESTS=$(tail -n6 unit-test-$TEST_OUTPUT|sed -n -e 's/failed :[\t]*\([0-9]*\)/\1/p' |tr -d ' ')
  149. echo "No test suites : $TOTAL_SUITES"
  150. echo "Passed : $PASSED_TESTS"
  151. echo "Failed : $FAILED_TESTS"
  152. echo "Skipped : $SKIPPED_TESTS"
  153. echo "Total exec'd tests : $(($PASSED_TESTS + $FAILED_TESTS))"
  154. echo "Total avail tests : $(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))"
  155. echo
  156. TOTAL_PASS=$PASSED_TESTS
  157. TOTAL_FAIL=$FAILED_TESTS
  158. TOTAL_SKIP=$SKIPPED_TESTS
  159. TOTAL_AVAIL=$(($PASSED_TESTS + $FAILED_TESTS + $SKIPPED_TESTS))
  160. TOTAL_EXED=$(($PASSED_TESTS + $FAILED_TESTS))
  161. # Step 4b - TLS Options tests
  162. echo "TLS Options tests - tests/ssl-opt.sh"
  163. PASSED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* (\([0-9]*\) \/ [0-9]* tests ([0-9]* skipped))$/\1/p')
  164. SKIPPED_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ [0-9]* tests (\([0-9]*\) skipped))$/\1/p')
  165. TOTAL_TESTS=$(tail -n5 sys-test-$TEST_OUTPUT|sed -n -e 's/.* ([0-9]* \/ \([0-9]*\) tests ([0-9]* skipped))$/\1/p')
  166. FAILED_TESTS=$(($TOTAL_TESTS - $PASSED_TESTS))
  167. echo "Passed : $PASSED_TESTS"
  168. echo "Failed : $FAILED_TESTS"
  169. echo "Skipped : $SKIPPED_TESTS"
  170. echo "Total exec'd tests : $TOTAL_TESTS"
  171. echo "Total avail tests : $(($TOTAL_TESTS + $SKIPPED_TESTS))"
  172. echo
  173. TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
  174. TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
  175. TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
  176. TOTAL_AVAIL=$(($TOTAL_AVAIL + $TOTAL_TESTS + $SKIPPED_TESTS))
  177. TOTAL_EXED=$(($TOTAL_EXED + $TOTAL_TESTS))
  178. # Step 4c - System Compatibility tests
  179. echo "System/Compatibility tests - tests/compat.sh"
  180. 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 }')
  181. 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 }')
  182. 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 }')
  183. FAILED_TESTS=$(($EXED_TESTS - $PASSED_TESTS))
  184. echo "Passed : $PASSED_TESTS"
  185. echo "Failed : $FAILED_TESTS"
  186. echo "Skipped : $SKIPPED_TESTS"
  187. echo "Total exec'd tests : $EXED_TESTS"
  188. echo "Total avail tests : $(($EXED_TESTS + $SKIPPED_TESTS))"
  189. echo
  190. TOTAL_PASS=$(($TOTAL_PASS+$PASSED_TESTS))
  191. TOTAL_FAIL=$(($TOTAL_FAIL+$FAILED_TESTS))
  192. TOTAL_SKIP=$(($TOTAL_SKIP+$SKIPPED_TESTS))
  193. TOTAL_AVAIL=$(($TOTAL_AVAIL + $EXED_TESTS + $SKIPPED_TESTS))
  194. TOTAL_EXED=$(($TOTAL_EXED + $EXED_TESTS))
  195. # Step 4d - Grand totals
  196. echo "-------------------------------------------------------------------------"
  197. echo "Total tests"
  198. echo "Total Passed : $TOTAL_PASS"
  199. echo "Total Failed : $TOTAL_FAIL"
  200. echo "Total Skipped : $TOTAL_SKIP"
  201. echo "Total exec'd tests : $TOTAL_EXED"
  202. echo "Total avail tests : $TOTAL_AVAIL"
  203. echo
  204. # Step 4e - Coverage report
  205. echo "Coverage statistics:"
  206. sed -n '1,/^Overall coverage/d; /%/p' cov-$TEST_OUTPUT
  207. echo
  208. rm unit-test-$TEST_OUTPUT
  209. rm sys-test-$TEST_OUTPUT
  210. rm compat-test-$TEST_OUTPUT
  211. rm cov-$TEST_OUTPUT
  212. # Mark the report generation as having succeeded. This must be the
  213. # last thing in the report generation.
  214. touch "basic-build-test-$$.ok"
  215. } | tee coverage-summary.txt
  216. make clean
  217. if [ -f "$CONFIG_BAK" ]; then
  218. mv "$CONFIG_BAK" "$CONFIG_H"
  219. fi
  220. # The file must exist, otherwise it means something went wrong while generating
  221. # the coverage report. If something did go wrong, rm will complain so this
  222. # script will exit with a failure status.
  223. rm "tests/basic-build-test-$$.ok"