blob: 78763b82e5882e1da4ad2883d5983e03b2f4a2ff [file] [log] [blame]
Gilles Peskined0a4dc82020-04-23 17:33:36 +02001## Common shell functions used by demo scripts programs/*/*.sh.
2
3## How to write a demo script
4## ==========================
5##
6## Include this file near the top of each demo script:
7## . "${0%/*}/../demo_common.sh"
8##
Gilles Peskineb63e79d2020-04-23 18:50:37 +02009## Start with a "msg" call that explains the purpose of the script.
10## Then call the "depends_on" function to ensure that all config
11## dependencies are met.
12##
Gilles Peskined0a4dc82020-04-23 17:33:36 +020013## As the last thing in the script, call the cleanup function.
14##
15## You can use the functions and variables described below.
16
17set -e -u
18
19## $root_dir is the root directory of the Mbed TLS source tree.
20root_dir="${0%/*}"
21n=4 # limit the search depth
22while ! [ -d "$root_dir/programs" ] || ! [ -d "$root_dir/library" ]; do
23 if [ $n -eq 0 ]; then
24 echo >&2 "This doesn't seem to be an Mbed TLS source tree."
25 exit 125
26 fi
27 n=$((n - 1))
28 case $root_dir in
29 .) root_dir="..";;
30 ..|?*/..) root_dir="$root_dir/..";;
31 ?*/*) root_dir="${root_dir%/*}";;
32 /*) root_dir="/";;
33 *) root_dir=".";;
34 esac
35done
36
37## $programs_dir is the directory containing the sample programs.
38programs_dir="$root_dir/programs"
39
40## msg LINE...
41## msg <TEXT_ORIGIN
42## Display an informational message.
43msg () {
44 if [ $# -eq 0 ]; then
45 sed 's/^/# /'
46 else
47 for x in "$@"; do
48 echo "# $x"
49 done
50 fi
51}
52
53## run "Message" COMMAND ARGUMENT...
54## Display the message, then run COMMAND with the specified arguments.
55run () {
56 echo
57 echo "# $1"
58 shift
59 echo "+ $*"
60 "$@"
61}
62
63## Like '!', but stop on failure with 'set -e'
64not () {
65 if "$@"; then false; fi
66}
67
68## run_bad "Message" COMMAND ARGUMENT...
69## Like run, but the command is expected to fail.
70run_bad () {
71 echo
72 echo "$1 This must fail."
73 shift
74 echo "+ ! $*"
75 not "$@"
76}
77
Gilles Peskine1e187752020-04-23 17:50:26 +020078## config_has SYMBOL...
79## Succeeds if the library configuration has all SYMBOLs set.
80config_has () {
81 for x in "$@"; do
82 "$programs_dir/test/query_compile_time_config" "$x"
83 done
84}
85
Gilles Peskineb63e79d2020-04-23 18:50:37 +020086## depends_on SYMBOL...
87## Exit if the library configuration does not have all SYMBOLs set.
88depends_on () {
89 if ! config_has "$@"; then
90 cat >&2 <<EOF
91This demo script requires the following configuration options to be enabled
92at compile time:
93 $@
94EOF
95 # Exit with a success status so that this counts as a pass for run_demos.py.
96 exit
97 fi
98}
99
Gilles Peskined0a4dc82020-04-23 17:33:36 +0200100## Add the names of files to clean up to this whitespace-separated variable.
101## The file names must not contain whitespace characters.
102files_to_clean=
103
104
105
106################################################################
107## End of the public interfaces. Code beyond this point is not
108## meant to be called directly from a demo script.
109
110cleanup () {
111 rm -f -- $files_to_clean
112}
113trap 'cleanup; trap - HUP; kill -HUP $$' HUP
114trap 'cleanup; trap - INT; kill -INT $$' INT
115trap 'cleanup; trap - TERM; kill -TERM $$' TERM
Gilles Peskine1e187752020-04-23 17:50:26 +0200116
117if config_has MBEDTLS_ENTROPY_NV_SEED; then
118 # Create a seedfile that's sufficiently long in all library configurations.
119 # This is necessary for programs that use randomness.
120 # Assume that the name of the seedfile is the default name.
121 files_to_clean="$files_to_clean seedfile"
122 dd if=/dev/urandom of=seedfile ibs=64 obs=64 count=1
123fi