all.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #!/bin/sh
  2. # all.sh
  3. #
  4. # This file is part of mbed TLS (https://tls.mbed.org)
  5. #
  6. # Copyright (c) 2014-2016, ARM Limited, All Rights Reserved
  7. #
  8. # Purpose
  9. #
  10. # To run all tests possible or available on the platform.
  11. #
  12. # Warning: the test is destructive. It includes various build modes and
  13. # configurations, and can and will arbitrarily change the current CMake
  14. # configuration. After this script has been run, the CMake cache will be lost
  15. # and CMake will no longer be initialised.
  16. #
  17. # The script assumes the presence of gcc and clang (recent enough for using
  18. # ASan with gcc and MemSan with clang, or valgrind) are available, as well as
  19. # cmake and a "good" find.
  20. # Abort on errors (and uninitialised variables)
  21. set -eu
  22. if [ -d library -a -d include -a -d tests ]; then :; else
  23. err_msg "Must be run from mbed TLS root"
  24. exit 1
  25. fi
  26. CONFIG_H='include/mbedtls/config.h'
  27. CONFIG_BAK="$CONFIG_H.bak"
  28. MEMORY=0
  29. FORCE=0
  30. RELEASE=0
  31. # Default commands, can be overriden by the environment
  32. : ${OPENSSL:="openssl"}
  33. : ${OPENSSL_LEGACY:="$OPENSSL"}
  34. : ${GNUTLS_CLI:="gnutls-cli"}
  35. : ${GNUTLS_SERV:="gnutls-serv"}
  36. : ${GNUTLS_LEGACY_CLI:="$GNUTLS_CLI"}
  37. : ${GNUTLS_LEGACY_SERV:="$GNUTLS_SERV"}
  38. : ${OUT_OF_SOURCE_DIR:=./mbedtls_out_of_source_build}
  39. usage()
  40. {
  41. printf "Usage: $0\n"
  42. printf " -h|--help\t\tPrint this help.\n"
  43. printf " -m|--memory\t\tAdditional optional memory tests.\n"
  44. printf " -f|--force\t\tForce the tests to overwrite any modified files.\n"
  45. printf " -s|--seed\t\tInteger seed value to use for this test run.\n"
  46. printf " -r|--release-test\t\tRun this script in release mode. This fixes the seed value to 1.\n"
  47. printf " --out-of-source-dir=<path>\t\tDirectory used for CMake out-of-source build tests."
  48. printf " --openssl=<OpenSSL_path>\t\tPath to OpenSSL executable to use for most tests.\n"
  49. printf " --openssl-legacy=<OpenSSL_path>\t\tPath to OpenSSL executable to use for legacy tests e.g. SSLv3.\n"
  50. printf " --gnutls-cli=<GnuTLS_cli_path>\t\tPath to GnuTLS client executable to use for most tests.\n"
  51. printf " --gnutls-serv=<GnuTLS_serv_path>\t\tPath to GnuTLS server executable to use for most tests.\n"
  52. printf " --gnutls-legacy-cli=<GnuTLS_cli_path>\t\tPath to GnuTLS client executable to use for legacy tests.\n"
  53. printf " --gnutls-legacy-serv=<GnuTLS_serv_path>\t\tPath to GnuTLS server executable to use for legacy tests.\n"
  54. }
  55. # remove built files as well as the cmake cache/config
  56. cleanup()
  57. {
  58. make clean
  59. find . -name yotta -prune -o -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} \+
  60. rm -f include/Makefile include/mbedtls/Makefile programs/*/Makefile
  61. git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile
  62. git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile
  63. if [ -f "$CONFIG_BAK" ]; then
  64. mv "$CONFIG_BAK" "$CONFIG_H"
  65. fi
  66. }
  67. trap cleanup INT TERM HUP
  68. msg()
  69. {
  70. echo ""
  71. echo "******************************************************************"
  72. echo "* $1 "
  73. printf "* "; date
  74. echo "******************************************************************"
  75. }
  76. err_msg()
  77. {
  78. echo "$1" >&2
  79. }
  80. check_tools()
  81. {
  82. for TOOL in "$@"; do
  83. if ! `hash "$TOOL" >/dev/null 2>&1`; then
  84. err_msg "$TOOL not found!"
  85. exit 1
  86. fi
  87. done
  88. }
  89. while [ $# -gt 0 ]; do
  90. case "$1" in
  91. --memory|-m*)
  92. MEMORY=${1#-m}
  93. ;;
  94. --force|-f)
  95. FORCE=1
  96. ;;
  97. --seed|-s)
  98. shift
  99. SEED="$1"
  100. ;;
  101. --release-test|-r)
  102. RELEASE=1
  103. ;;
  104. --out-of-source-dir)
  105. shift
  106. OUT_OF_SOURCE_DIR="$1"
  107. ;;
  108. --openssl)
  109. shift
  110. OPENSSL="$1"
  111. ;;
  112. --openssl-legacy)
  113. shift
  114. OPENSSL_LEGACY="$1"
  115. ;;
  116. --gnutls-cli)
  117. shift
  118. GNUTLS_CLI="$1"
  119. ;;
  120. --gnutls-serv)
  121. shift
  122. GNUTLS_SERV="$1"
  123. ;;
  124. --gnutls-legacy-cli)
  125. shift
  126. GNUTLS_LEGACY_CLI="$1"
  127. ;;
  128. --gnutls-legacy-serv)
  129. shift
  130. GNUTLS_LEGACY_SERV="$1"
  131. ;;
  132. --help|-h|*)
  133. usage
  134. exit 1
  135. ;;
  136. esac
  137. shift
  138. done
  139. if [ $FORCE -eq 1 ]; then
  140. rm -rf yotta/module "$OUT_OF_SOURCE_DIR"
  141. git checkout-index -f -q $CONFIG_H
  142. cleanup
  143. else
  144. if [ -d yotta/module ]; then
  145. err_msg "Warning - there is an existing yotta module in the directory 'yotta/module'"
  146. echo "You can either delete your work and retry, or force the test to overwrite the"
  147. echo "test by rerunning the script as: $0 --force"
  148. exit 1
  149. fi
  150. if [ -d "$OUT_OF_SOURCE_DIR" ]; then
  151. echo "Warning - there is an existing directory at '$OUT_OF_SOURCE_DIR'" >&2
  152. echo "You can either delete this directory manually, or force the test by rerunning"
  153. echo "the script as: $0 --force --out-of-source-dir $OUT_OF_SOURCE_DIR"
  154. exit 1
  155. fi
  156. if ! git diff-files --quiet include/mbedtls/config.h; then
  157. echo $?
  158. err_msg "Warning - the configuration file 'include/mbedtls/config.h' has been edited. "
  159. echo "You can either delete or preserve your work, or force the test by rerunning the"
  160. echo "script as: $0 --force"
  161. exit 1
  162. fi
  163. fi
  164. if [ $RELEASE -eq 1 ]; then
  165. # Fix the seed value to 1 to ensure that the tests are deterministic.
  166. SEED=1
  167. fi
  168. msg "info: $0 configuration"
  169. echo "MEMORY: $MEMORY"
  170. echo "FORCE: $FORCE"
  171. echo "SEED: ${SEED-"UNSET"}"
  172. echo "OPENSSL: $OPENSSL"
  173. echo "OPENSSL_LEGACY: $OPENSSL_LEGACY"
  174. echo "GNUTLS_CLI: $GNUTLS_CLI"
  175. echo "GNUTLS_SERV: $GNUTLS_SERV"
  176. echo "GNUTLS_LEGACY_CLI: $GNUTLS_LEGACY_CLI"
  177. echo "GNUTLS_LEGACY_SERV: $GNUTLS_LEGACY_SERV"
  178. # To avoid setting OpenSSL and GnuTLS for each call to compat.sh and ssl-opt.sh
  179. # we just export the variables they require
  180. export OPENSSL_CMD="$OPENSSL"
  181. export GNUTLS_CLI="$GNUTLS_CLI"
  182. export GNUTLS_SERV="$GNUTLS_SERV"
  183. # Avoid passing --seed flag in every call to ssl-opt.sh
  184. [ ! -z ${SEED+set} ] && export SEED
  185. # Make sure the tools we need are available.
  186. check_tools "$OPENSSL" "$OPENSSL_LEGACY" "$GNUTLS_CLI" "$GNUTLS_SERV" \
  187. "$GNUTLS_LEGACY_CLI" "$GNUTLS_LEGACY_SERV" "doxygen" "dot" \
  188. "arm-none-eabi-gcc" "armcc"
  189. #
  190. # Test Suites to be executed
  191. #
  192. # The test ordering tries to optimize for the following criteria:
  193. # 1. Catch possible problems early, by running first tests that run quickly
  194. # and/or are more likely to fail than others (eg I use Clang most of the
  195. # time, so start with a GCC build).
  196. # 2. Minimize total running time, by avoiding useless rebuilds
  197. #
  198. # Indicative running times are given for reference.
  199. msg "info: output_env.sh"
  200. OPENSSL="$OPENSSL" OPENSSL_LEGACY="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_CLI" \
  201. GNUTLS_SERV="$GNUTLS_SERV" GNUTLS_LEGACY_CLI="$GNUTLS_LEGACY_CLI" \
  202. GNUTLS_LEGACY_SERV="$GNUTLS_LEGACY_SERV" scripts/output_env.sh
  203. msg "test: recursion.pl" # < 1s
  204. tests/scripts/recursion.pl library/*.c
  205. msg "test: freshness of generated source files" # < 1s
  206. tests/scripts/check-generated-files.sh
  207. msg "test: doxygen markup outside doxygen blocks" # < 1s
  208. tests/scripts/check-doxy-blocks.pl
  209. msg "test/build: declared and exported names" # < 3s
  210. cleanup
  211. tests/scripts/check-names.sh
  212. msg "test: doxygen warnings" # ~ 3s
  213. cleanup
  214. tests/scripts/doxygen.sh
  215. msg "build: create and build yotta module" # ~ 30s
  216. cleanup
  217. tests/scripts/yotta-build.sh
  218. msg "build: cmake, gcc, ASan" # ~ 1 min 50s
  219. cleanup
  220. CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
  221. make
  222. msg "test: main suites (inc. selftests) (ASan build)" # ~ 50s
  223. make test
  224. msg "test: ssl-opt.sh (ASan build)" # ~ 1 min
  225. tests/ssl-opt.sh
  226. msg "test/build: ref-configs (ASan build)" # ~ 6 min 20s
  227. tests/scripts/test-ref-configs.pl
  228. msg "build: with ASan (rebuild after ref-configs)" # ~ 1 min
  229. make
  230. msg "test: compat.sh (ASan build)" # ~ 6 min
  231. tests/compat.sh
  232. msg "build: Default + SSLv3 (ASan build)" # ~ 6 min
  233. cleanup
  234. cp "$CONFIG_H" "$CONFIG_BAK"
  235. scripts/config.pl set MBEDTLS_SSL_PROTO_SSL3
  236. CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
  237. make
  238. msg "test: SSLv3 - main suites (inc. selftests) (ASan build)" # ~ 50s
  239. make test
  240. msg "build: SSLv3 - compat.sh (ASan build)" # ~ 6 min
  241. tests/compat.sh -m 'tls1 tls1_1 tls1_2 dtls1 dtls1_2'
  242. OPENSSL_CMD="$OPENSSL_LEGACY" tests/compat.sh -m 'ssl3'
  243. msg "build: SSLv3 - ssl-opt.sh (ASan build)" # ~ 6 min
  244. tests/ssl-opt.sh
  245. msg "build: cmake, full config, clang" # ~ 50s
  246. cleanup
  247. cp "$CONFIG_H" "$CONFIG_BAK"
  248. scripts/config.pl full
  249. scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # too slow for tests
  250. CC=clang cmake -D CMAKE_BUILD_TYPE:String=Check .
  251. make
  252. msg "test: main suites (full config)" # ~ 5s
  253. make test
  254. msg "test: ssl-opt.sh default (full config)" # ~ 1s
  255. tests/ssl-opt.sh -f Default
  256. msg "test: compat.sh RC4, DES & NULL (full config)" # ~ 2 min
  257. OPENSSL_CMD="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" tests/compat.sh -e '3DES\|DES-CBC3' -f 'NULL\|DES\|RC4\|ARCFOUR'
  258. msg "test/build: curves.pl (gcc)" # ~ 4 min
  259. cleanup
  260. cmake -D CMAKE_BUILD_TYPE:String=Debug .
  261. tests/scripts/curves.pl
  262. msg "test/build: key-exchanges (gcc)" # ~ 1 min
  263. cleanup
  264. cmake -D CMAKE_BUILD_TYPE:String=Check .
  265. tests/scripts/key-exchanges.pl
  266. msg "build: Unix make, -Os (gcc)" # ~ 30s
  267. cleanup
  268. CC=gcc CFLAGS='-Werror -Os' make
  269. # this is meant to cath missing #define mbedtls_printf etc
  270. # disable fsio to catch some more missing #include <stdio.h>
  271. msg "build: full config except platform/fsio, make, gcc" # ~ 30s
  272. cleanup
  273. cp "$CONFIG_H" "$CONFIG_BAK"
  274. scripts/config.pl full
  275. scripts/config.pl unset MBEDTLS_PLATFORM_C
  276. scripts/config.pl unset MBEDTLS_PLATFORM_MEMORY
  277. scripts/config.pl unset MBEDTLS_PLATFORM_PRINTF_ALT
  278. scripts/config.pl unset MBEDTLS_PLATFORM_FPRINTF_ALT
  279. scripts/config.pl unset MBEDTLS_PLATFORM_SNPRINTF_ALT
  280. scripts/config.pl unset MBEDTLS_PLATFORM_TIME_ALT
  281. scripts/config.pl unset MBEDTLS_PLATFORM_EXIT_ALT
  282. scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
  283. scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C
  284. scripts/config.pl unset MBEDTLS_FS_IO
  285. CC=gcc CFLAGS='-Werror -O0' make
  286. # catch compile bugs in _uninit functions
  287. msg "build: full config with NO_STD_FUNCTION, make, gcc" # ~ 30s
  288. cleanup
  289. cp "$CONFIG_H" "$CONFIG_BAK"
  290. scripts/config.pl full
  291. scripts/config.pl set MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
  292. scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
  293. CC=gcc CFLAGS='-Werror -O0' make
  294. msg "build: full config except ssl_srv.c, make, gcc" # ~ 30s
  295. cleanup
  296. cp "$CONFIG_H" "$CONFIG_BAK"
  297. scripts/config.pl full
  298. scripts/config.pl unset MBEDTLS_SSL_SRV_C
  299. CC=gcc CFLAGS='-Werror -O0' make
  300. msg "build: full config except ssl_cli.c, make, gcc" # ~ 30s
  301. cleanup
  302. cp "$CONFIG_H" "$CONFIG_BAK"
  303. scripts/config.pl full
  304. scripts/config.pl unset MBEDTLS_SSL_CLI_C
  305. CC=gcc CFLAGS='-Werror -O0' make
  306. msg "build: full config except net_sockets.c, make, gcc -std=c99 -pedantic" # ~ 30s
  307. cleanup
  308. cp "$CONFIG_H" "$CONFIG_BAK"
  309. scripts/config.pl full
  310. scripts/config.pl unset MBEDTLS_NET_C # getaddrinfo() undeclared, etc.
  311. scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY # uses syscall() on GNU/Linux
  312. CC=gcc CFLAGS='-Werror -O0 -std=c99 -pedantic' make lib
  313. msg "build: default config with MBEDTLS_TEST_NULL_ENTROPY (ASan build)"
  314. cleanup
  315. cp "$CONFIG_H" "$CONFIG_BAK"
  316. scripts/config.pl set MBEDTLS_TEST_NULL_ENTROPY
  317. scripts/config.pl set MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
  318. scripts/config.pl set MBEDTLS_ENTROPY_C
  319. scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
  320. scripts/config.pl unset MBEDTLS_ENTROPY_HARDWARE_ALT
  321. scripts/config.pl unset MBEDTLS_HAVEGE_C
  322. CC=gcc cmake -D UNSAFE_BUILD=ON -D CMAKE_C_FLAGS:String="-fsanitize=address -fno-common -O3" .
  323. make
  324. msg "test: MBEDTLS_TEST_NULL_ENTROPY - main suites (inc. selftests) (ASan build)"
  325. make test
  326. if uname -a | grep -F Linux >/dev/null; then
  327. msg "build/test: make shared" # ~ 40s
  328. cleanup
  329. make SHARED=1 all check
  330. fi
  331. if uname -a | grep -F x86_64 >/dev/null; then
  332. msg "build: i386, make, gcc" # ~ 30s
  333. cleanup
  334. CC=gcc CFLAGS='-Werror -m32' make
  335. fi # x86_64
  336. msg "build: arm-none-eabi-gcc, make" # ~ 10s
  337. cleanup
  338. cp "$CONFIG_H" "$CONFIG_BAK"
  339. scripts/config.pl full
  340. scripts/config.pl unset MBEDTLS_NET_C
  341. scripts/config.pl unset MBEDTLS_TIMING_C
  342. scripts/config.pl unset MBEDTLS_FS_IO
  343. scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
  344. scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY
  345. # following things are not in the default config
  346. scripts/config.pl unset MBEDTLS_HAVEGE_C # depends on timing.c
  347. scripts/config.pl unset MBEDTLS_THREADING_PTHREAD
  348. scripts/config.pl unset MBEDTLS_THREADING_C
  349. scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # execinfo.h
  350. scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C # calls exit
  351. CC=arm-none-eabi-gcc AR=arm-none-eabi-ar LD=arm-none-eabi-ld CFLAGS=-Werror make lib
  352. msg "build: armcc, make"
  353. cleanup
  354. cp "$CONFIG_H" "$CONFIG_BAK"
  355. scripts/config.pl full
  356. scripts/config.pl unset MBEDTLS_NET_C
  357. scripts/config.pl unset MBEDTLS_TIMING_C
  358. scripts/config.pl unset MBEDTLS_FS_IO
  359. scripts/config.pl unset MBEDTLS_ENTROPY_NV_SEED
  360. scripts/config.pl unset MBEDTLS_HAVE_TIME
  361. scripts/config.pl unset MBEDTLS_HAVE_TIME_DATE
  362. scripts/config.pl set MBEDTLS_NO_PLATFORM_ENTROPY
  363. # following things are not in the default config
  364. scripts/config.pl unset MBEDTLS_DEPRECATED_WARNING
  365. scripts/config.pl unset MBEDTLS_HAVEGE_C # depends on timing.c
  366. scripts/config.pl unset MBEDTLS_THREADING_PTHREAD
  367. scripts/config.pl unset MBEDTLS_THREADING_C
  368. scripts/config.pl unset MBEDTLS_MEMORY_BACKTRACE # execinfo.h
  369. scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C # calls exit
  370. scripts/config.pl unset MBEDTLS_PLATFORM_TIME_ALT # depends on MBEDTLS_HAVE_TIME
  371. CC=armcc AR=armar WARNING_CFLAGS= make lib
  372. if which i686-w64-mingw32-gcc >/dev/null; then
  373. msg "build: cross-mingw64, make" # ~ 30s
  374. cleanup
  375. CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS=-Werror WINDOWS_BUILD=1 make
  376. WINDOWS_BUILD=1 make clean
  377. CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar LD=i686-w64-minggw32-ld CFLAGS=-Werror WINDOWS_BUILD=1 SHARED=1 make
  378. WINDOWS_BUILD=1 make clean
  379. fi
  380. # MemSan currently only available on Linux 64 bits
  381. if uname -a | grep 'Linux.*x86_64' >/dev/null; then
  382. msg "build: MSan (clang)" # ~ 1 min 20s
  383. cleanup
  384. cp "$CONFIG_H" "$CONFIG_BAK"
  385. scripts/config.pl unset MBEDTLS_AESNI_C # memsan doesn't grok asm
  386. CC=clang cmake -D CMAKE_BUILD_TYPE:String=MemSan .
  387. make
  388. msg "test: main suites (MSan)" # ~ 10s
  389. make test
  390. msg "test: ssl-opt.sh (MSan)" # ~ 1 min
  391. tests/ssl-opt.sh
  392. # Optional part(s)
  393. if [ "$MEMORY" -gt 0 ]; then
  394. msg "test: compat.sh (MSan)" # ~ 6 min 20s
  395. tests/compat.sh
  396. fi
  397. else # no MemSan
  398. msg "build: Release (clang)"
  399. cleanup
  400. CC=clang cmake -D CMAKE_BUILD_TYPE:String=Release .
  401. make
  402. msg "test: main suites valgrind (Release)"
  403. make memcheck
  404. # Optional part(s)
  405. # Currently broken, programs don't seem to receive signals
  406. # under valgrind on OS X
  407. if [ "$MEMORY" -gt 0 ]; then
  408. msg "test: ssl-opt.sh --memcheck (Release)"
  409. tests/ssl-opt.sh --memcheck
  410. fi
  411. if [ "$MEMORY" -gt 1 ]; then
  412. msg "test: compat.sh --memcheck (Release)"
  413. tests/compat.sh --memcheck
  414. fi
  415. fi # MemSan
  416. msg "build: cmake 'out-of-source' build"
  417. cleanup
  418. MBEDTLS_ROOT_DIR="$PWD"
  419. mkdir "$OUT_OF_SOURCE_DIR"
  420. cd "$OUT_OF_SOURCE_DIR"
  421. cmake "$MBEDTLS_ROOT_DIR"
  422. make
  423. msg "test: cmake 'out-of-source' build"
  424. make test
  425. cd "$MBEDTLS_ROOT_DIR"
  426. rm -rf "$OUT_OF_SOURCE_DIR"
  427. msg "Done, cleaning up"
  428. cleanup