5 echo [STEP] Initialize test library
7 # Paths to compiled executables
9 # realpath on FreeBSD fails if the path does not exist.
11 [ -e "$1" ] || mkdir -p "$1"
12 if type realpath >/dev/null; then
19 tincd_path=$(realdir "../src/tincd@EXEEXT@")
20 tinc_path=$(realdir "../src/tinc@EXEEXT@")
22 # shellcheck disable=SC2034
23 SPTPS_TEST=$(realdir "../src/sptps_test@EXEEXT@")
24 # shellcheck disable=SC2034
25 SPTPS_KEYPAIR=$(realdir "../src/sptps_keypair@EXEEXT@")
28 # shellcheck disable=SC2034
30 # shellcheck disable=SC2034
33 # The list of the environment variables that tinc injects into the scripts it calls.
34 # shellcheck disable=SC2016
35 TINC_SCRIPT_VARS='$NETNAME,$NAME,$DEVICE,$IFACE,$NODE,$REMOTEADDRESS,$REMOTEPORT,$SUBNET,$WEIGHT,$INVITATION_FILE,$INVITATION_URL,$DEBUG'
39 # Reuse script name if it was passed in an env var (when imported from tinc scripts).
40 if [ -z "$SCRIPTNAME" ]; then
41 SCRIPTNAME=$(basename "$0")
44 # Network names for tincd daemons.
49 # Configuration/pidfile directories for tincd daemons.
50 DIR_FOO=$(realdir "$PWD/$net1")
51 DIR_BAR=$(realdir "$PWD/$net2")
52 DIR_BAZ=$(realdir "$PWD/$net3")
54 # Register helper functions
56 # Alias gtimeout to timeout if it exists.
57 if type gtimeout >/dev/null; then
58 timeout() { gtimeout "$@"; }
61 # As usual, BSD tools require special handling, as they do not support -i without a suffix.
62 # Note that there must be no space after -i, or it won't work on GNU sed.
67 # Are the shell tools provided by busybox?
69 timeout --help 2>&1 | grep -q -i busybox
72 # busybox timeout returns 128 + signal number (which is TERM by default)
74 # shellcheck disable=SC2034
75 EXIT_TIMEOUT=$((128 + 15))
77 # shellcheck disable=SC2034
83 test "$(uname -o)" = Msys
86 # Are we running on a CI server?
91 # Dump error message and exit with an error.
97 # Remove carriage returns to normalize strings on Windows for easier comparisons.
103 normalize_path() { cygpath --mixed -- "$@"; }
105 normalize_path() { echo "$@"; }
108 # Executes whatever is passed to it, checking that the resulting exit code is non-zero.
111 bail "expected a non-zero exit code"
115 # Executes the passed command and checks two conditions:
116 # 1. it must exit successfully (with code 0)
117 # 2. its output (stdout + stderr) must include the substring from the first argument (ignoring case)
118 # usage: expect_msg 'expected message' command --with --args
123 if ! output=$("$@" 2>&1); then
124 bail 'expected 0 exit code'
127 if ! echo "$output" | grep -q -i "$message"; then
128 bail "expected message '$message'"
132 # The reverse of expect_msg. We cannot simply wrap expect_msg with must_fail
133 # because there should be a separate check for tinc exit code.
138 if ! output=$("$@" 2>&1); then
139 bail 'expected 0 exit code'
142 if echo "$output" | grep -q -i "$message"; then
143 bail "unexpected message '$message'"
147 # Like expect_msg, but the command must fail with a non-zero exit code.
148 # usage: must_fail_with_msg 'expected message' command --with --args
149 must_fail_with_msg() {
153 if output=$("$@" 2>&1); then
154 bail "expected a non-zero exit code"
157 if ! echo "$output" | grep -i -q "$message"; then
158 bail "expected message '$message'"
162 # Is the legacy protocol enabled?
164 tincd foo --version | grep -q legacy_protocol
167 # Are we running with EUID 0?
172 # Executes whatever is passed to it, checking that the resulting exit code is equal to the first argument.
180 if [ $code != "$expected" ]; then
181 bail "wrong exit code $code, expected $expected"
185 # Runs its arguments with timeout(1) or gtimeout(1) if either are installed.
186 # Usage: try_limit_time 10 command --with --args
187 if type timeout >/dev/null; then
195 echo >&2 "timeout was not found, running without time limits!"
201 # wc -l on mac prints whitespace before the actual number.
202 # This is simplest cross-platform alternative without that behavior.
204 awk 'END{ print NR }'
207 # Calls compiled tinc, passing any supplied arguments.
208 # Usage: tinc { foo | bar | baz } --arg1 val1 "$args"
214 foo) try_limit_time 30 "$tinc_path" -n "$net1" --config="$DIR_FOO" --pidfile="$DIR_FOO/pid" "$@" ;;
215 bar) try_limit_time 30 "$tinc_path" -n "$net2" --config="$DIR_BAR" --pidfile="$DIR_BAR/pid" "$@" ;;
216 baz) try_limit_time 30 "$tinc_path" -n "$net3" --config="$DIR_BAZ" --pidfile="$DIR_BAZ/pid" "$@" ;;
217 *) bail "invalid command [[$peer $*]]" ;;
221 # Calls compiled tincd, passing any supplied arguments.
222 # Usage: tincd { foo | bar | baz } --arg1 val1 "$args"
228 foo) try_limit_time 30 "$tincd_path" -n "$net1" --config="$DIR_FOO" --pidfile="$DIR_FOO/pid" --logfile="$DIR_FOO/log" -d5 "$@" ;;
229 bar) try_limit_time 30 "$tincd_path" -n "$net2" --config="$DIR_BAR" --pidfile="$DIR_BAR/pid" --logfile="$DIR_BAR/log" -d5 "$@" ;;
230 baz) try_limit_time 30 "$tincd_path" -n "$net3" --config="$DIR_BAZ" --pidfile="$DIR_BAZ/pid" --logfile="$DIR_BAZ/log" -d5 "$@" ;;
231 *) bail "invalid command [[$peer $*]]" ;;
235 # Start the specified tinc daemon.
236 # usage: start_tinc { foo | bar | baz }
242 foo) tinc "$peer" start --logfile="$DIR_FOO/log" -d5 "$@" ;;
243 bar) tinc "$peer" start --logfile="$DIR_BAR/log" -d5 "$@" ;;
244 baz) tinc "$peer" start --logfile="$DIR_BAZ/log" -d5 "$@" ;;
245 *) bail "invalid peer $peer" ;;
249 # Stop all tinc clients.
252 # In case these pid files are mangled.
254 [ -f "$DIR_FOO/pid" ] && tinc foo stop
255 [ -f "$DIR_BAR/pid" ] && tinc bar stop
256 [ -f "$DIR_BAZ/pid" ] && tinc baz stop
261 # Checks that the number of reachable nodes matches what is expected.
262 # usage: require_nodes node_name expected_number
264 echo >&2 "Check that we're able to reach tincd"
265 test "$(tinc "$1" pid | count_lines)" = 1
267 echo >&2 "Check the number of reachable nodes for $1 (expecting $2)"
268 actual="$(tinc "$1" dump reachable nodes | count_lines)"
270 if [ "$actual" != "$2" ]; then
271 echo >&2 "tinc $1: expected $2 reachable nodes, got $actual"
279 foo) echo "$DIR_FOO" ;;
280 bar) echo "$DIR_BAR" ;;
281 baz) echo "$DIR_BAZ" ;;
282 *) bail "invalid peer $peer" ;;
286 # This is an append-only log of all scripts executed by all peers.
288 echo "$(peer_directory "$1")/script-runs.log"
291 # Create tincd script. If it fails, it kills the test script with SIGTERM.
292 # usage: create_script { foo | bar | baz } { tinc-up | host-down | ... } 'script content'
298 # This is the line that we should start from when reading the script execution log while waiting
299 # for $script from $peer. It is a poor man's hash map to avoid polluting tinc's home directory with
300 # "last seen" files. There seem to be no good solutions to this that are compatible with all shells.
301 line_var=$(next_line_var "$peer" "$script")
303 # We must reassign it here in case the script is recreated.
304 # shellcheck disable=SC2229
305 read -r "$line_var" <<EOF
309 # Full path to the script.
310 script_path=$(peer_directory "$peer")/$script
312 # Full path to the script execution log (one for each peer).
313 script_log=$(script_runs_log "$peer")
314 printf '' >"$script_log"
316 # Script output is redirected into /dev/null. Otherwise, it ends up
317 # in tinc's output and breaks things like 'tinc invite'.
318 cat >"$script_path" <<EOF
322 SCRIPTNAME="$SCRIPTNAME" . ./testlib.sh
324 echo "$script,\$$,$TINC_SCRIPT_VARS" >>"$script_log"
325 ) >/dev/null 2>&1 || kill -TERM $$
328 chmod u+x "$script_path"
331 echo "@$MINGW_SHELL '$script_path'" >"$script_path.cmd"
335 # Returns the name of the variable that contains the line number
336 # we should read next when waiting on $script from $peer.
337 # usage: next_line_var foo host-up
340 script=$(echo "$2" | sed 's/[^a-zA-Z0-9]/_/g')
341 printf "%s" "next_line_${peer}_${script}"
344 # Waits for `peer`'s script `script` to finish `count` number of times.
345 # usage: wait_script { foo | bar | baz } { tinc-up | host-up | ... } [count=1]
351 if [ -z "$count" ] || [ "$count" -lt 1 ]; then
355 # Find out the location of the log and how many lines we should skip
356 # (because we've already seen them in previous invocations of wait_script
357 # for current $peer and $script).
358 line_var=$(next_line_var "$peer" "$script")
360 # eval is the only solution supported by POSIX shells.
361 # https://github.com/koalaman/shellcheck/wiki/SC3053
362 # 1. $line_var expands into 'next_line_foo_hosts_bar_up'
363 # 2. the name is substituted and the command becomes 'echo "$next_line_foo_hosts_bar_up"'
364 # 3. the command is evaluated and the line number is assigned to $line
365 line=$(eval "echo \"\$$line_var\"")
367 # This is the file that we monitor for script execution records.
368 script_log=$(script_runs_log "$peer")
370 # Starting from $line, read until $count matches are found.
371 # Print the number of the last matching line and exit.
372 # GNU tail 2.82 and newer terminates by itself when the pipe breaks.
373 # To support other tails we do an explicit `kill`.
374 # FIFO is useful here because otherwise it's difficult to determine
375 # which tail process should be killed. We could stick them in a process
376 # group by enabling job control, but this results in weird behavior when
377 # running tests in parallel on some interactive shells
378 # (e.g. when /bin/sh is symlinked to dash).
383 # This weird thing is required to support old versions of ksh on NetBSD 8.2 and the like.
384 (tail -n +"$line" -f "$script_log" >"$fifo") &
387 try_limit_time 60 sh -c "
388 grep -n -m $count '^$script,' <'$fifo'
389 " | awk -F: 'END { print $1 }'
392 # Try to stop the background tail, ignoring possible failure (some tails
393 # detect EOF, some don't, so it may have already exited), but do wait on
394 # it (which is required at least by old ksh).
399 # Remember the next line number for future reference. We'll use it if
400 # wait_script is called again with same $peer and $script.
401 read -r "${line_var?}" <<EOF
406 # Are we running tests in parallel?
408 # Grep the make flags for any of: '-j', '-j5', '-j 42', but not 'n-j', '-junk'.
409 echo "$MAKEFLAGS" | grep -E -q '(^|[[:space:]])-j[[:digit:]]*([[:space:]]|$)'
412 # Cleanup after running each script.
417 if command -v cleanup_hook 2>/dev/null; then
418 echo >&2 "Cleanup hook found, calling..."
424 # Ask nicely, then kill anything that's left.
425 if is_ci && ! is_parallel; then
429 for process in "$@"; do
430 pkill -"SIG$signal" -x -u "$(id -u)" "$process"
433 echo >&2 "CI server detected, performing aggressive cleanup"
434 kill_processes TERM tinc tincd
435 kill_processes KILL tinc tincd
440 # If we're on a CI server, the test requires superuser privileges to run, and we're not
441 # currently a superuser, try running the test as one and fail if it doesn't work (the
442 # system must be configured to provide passwordless sudo for our user).
448 echo "root is required for test $SCRIPTNAME, but we're a regular user; elevating privileges..."
449 if ! command -v sudo 2>/dev/null; then
450 bail "please install sudo and configure passwordless auth for user $USER"
452 if ! sudo --preserve-env --non-interactive true; then
453 bail "sudo is not allowed or requires a password for user $USER"
455 exec sudo --preserve-env "$@"
457 # Avoid these kinds of surprises outside CI. Just skip the test.
458 echo "root is required for test $SCRIPTNAME, but we're a regular user; skipping"
463 # Generate path to current shell which can be used from Windows applications.
465 MINGW_SHELL=$(normalize_path "$SHELL")
468 # This was called from a tincd script. Skip executing commands with side effects.
469 [ -n "$NAME" ] && return
471 echo [STEP] Check for leftover tinc daemons and test directories
473 # Cleanup leftovers from previous runs.
476 if [ -d "$DIR_FOO" ]; then rm -rf "$DIR_FOO"; fi
477 if [ -d "$DIR_BAR" ]; then rm -rf "$DIR_BAR"; fi
478 if [ -d "$DIR_BAZ" ]; then rm -rf "$DIR_BAZ"; fi
480 # Register cleanup function so we don't have to call it everywhere
481 # (and failed scripts do not leave stray tincd running).
482 trap cleanup EXIT INT TERM