# Common definitions for all test suites. # Copyright 2012 Serval Project Inc. # Copyright 2017 Flinders University # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. shopt -s extglob testdefs_sh=$(abspath "${BASH_SOURCE[0]}") servald_source_root="${testdefs_sh%/*}" if [ -r "$SERVAL_TEST_TARGETDIR/testconfig.sh" ]; then servald_build_root="$(abspath "$SERVAL_TEST_TARGETDIR")" elif [ -r "$PWD/testconfig.sh" ]; then servald_build_root="$PWD" elif [ -r "$servald_source_root/testconfig.sh" ]; then servald_build_root="$servald_source_root" else echo "$0: ERROR: cannot find testconfig.sh" >&2 echo "Set either the current working directory or the SERVAL_TEST_TARGETDIR" >&2 echo "environment variable to the directory that contains the configured and" >&2 echo "built servald." >&2 exit 1 fi source "$servald_build_root/testconfig.sh" || exit 1 export PATH="$servald_build_root:$PATH" export TFW_LOGDIR="${SERVAL_TEST_LOGDIR:-$servald_build_root/testlog}" servald_basename="servald" servald_build_executable="$servald_build_root/$servald_basename" addr_localhost="127.0.0.1" declare -a instance_stack=() # Some useful string constants. LF=' ' CR=' ' HT=' ' # Some useful regular expressions. These must work in grep(1) as basic # expressions, and also in sed(1). rexp_sid='[0-9a-fA-F]\{64\}' rexp_id='[0-9a-fA-F]\{64\}' rexp_did='[0-9+#]\{5,\}' # Utility function for extracting information from the output of servald # commands that return "key:value\n" pairs. # # extract_stdout_keyvalue_optional [] # # Examines the standard output of the last command executed using "execute" or # any of its variants. If there is a line matching # "\n" then assigns the part matched by # into the shell variable called and returns 0. # Otherwise, leaves unchanged and returns 1. # # The default is ':'. # extract_stdout_keyvalue_optional() { local _var="$1" local _label="$2" local _delim=':' local _rexp="$3" case $# in 3) ;; 4) _delim="$3"; _rexp="$4";; *) error "invalid number of args";; esac local _label_re=$(escape_grep_basic "$_label") local _delim_re=$(escape_grep_basic "$_delim") local _line=$($GREP "^$_label_re$_delim_re$_rexp" "$TFWSTDOUT") local _value= local _return=1 if [ -n "$_line" ]; then _value="${_line#*$_delim}" _return=0 if [ -n "$_var" ]; then eval $_var="\$_value" eval tfw_log "$_var=\$(shellarg "\${$_var}")" fi fi return $_return } # Utility function for extracting information from the output of servald # commands that return "key:value\n" pairs. extract_stdout_keyvalue() { local _label="$2" assert --message="stdout of ($TFWEXECUTED) contains valid '$_label:' line" --stdout extract_stdout_keyvalue_optional "$@" } # Assert function for the output of servald commands that return "key:value\n" # pairs. assert_stdout_keyvalue() { local _label="$1" local _value="$2" local _extracted extract_stdout_keyvalue _extracted "$_label" '.*' assert --message="stdout of ($executed) '$_label:' line does not equal '$_value'" --stdout test "$_extracted" = "$_value" } # Parse the standard result set output produced by the immediately preceding command # command into the following shell variables: # NCOLS the number of columns # NROWS the number of data rows (not counting headers) # HEADER[c] the C-th header label, 0 <= C <= NCOLS-1 #