cryptonas/tools/validate.sh

170 lines
5.3 KiB
Bash
Executable File

#!/bin/sh
#
# do a validation
#
# use "--help" for a list of possible actions
#
set -eu
# get the path of a configuration file - local configuration files
# supersede default files
# parameter: base name of the configuration file
function get_config_file()
{
[ -e "$LOCALCONF_DIR/$1" ] && echo "$LOCALCONF_DIR/$1" && return 0
[ -e "$DEFAULTCONF_DIR/$1" ] && echo "$DEFAULTCONF_DIR/$1" && return 0
echo "configuration file ($1) not found!" >&2
exit 1
}
# the base directory of your local development files
ROOT_DIR=$(dirname "$0")/..
# the template (default) configuration directory
DEFAULTCONF_DIR="$ROOT_DIR/etc-defaults.d"
# your local configuration directory (existing files supersede the defaults)
LOCALCONF_DIR="$ROOT_DIR/etc-local.d"
############# include local configuration ##############
if [ -e "$(get_config_file cbox-dev.conf)" ]
then source "$(get_config_file cbox-dev.conf)"
else echo "local cbox-dev.conf ($(get_config_file cbox-dev.conf)) does not exist!" >&2
exit 1
fi
#################### some functions ####################
function error_die()
{
echo "$2" >&2
exit $1
}
function do_single()
# Parameter: "test case dir" "output directory for results"
{
local TESTNAME=$(basename $1)
# replace IPs and ports in the curl-file by local settings
cat "$1/input.curl" | substitute_constants | curl --insecure --silent --output "${2}/${TESTNAME}.html" --config -
# there is no status in certain cases - e.g. for error 404
if [ -e "${2}/${TESTNAME}.html" ]
then sed "1,/CBOX-STATUS-begin/d; /CBOX-STATUS-end/,\$d" "${2}/${TESTNAME}.html" >"${2}/${TESTNAME}.status"
# the diff option "-B" is required, because the status output of
# the cryptobox.pl script contains some blank lines
cat "$1/output" | substitute_constants | diff -NB "${2}/${TESTNAME}.status" - >"${2}/${TESTNAME}.diff" || true
rm "${2}/${TESTNAME}.status"
fi
cp "$1/description" "${2}/${TESTNAME}.desc"
# sleep, if a file called 'delay' exists
[ -e "$1/delay" ] && sleep "$(<$1/delay)"
}
function do_series()
# parameter: name of the test case
{
[ -d "$VALIDATE_REPORT_DIR/$1" ] && rm -r "$VALIDATE_REPORT_DIR/$1"
mkdir -p "$VALIDATE_REPORT_DIR/$1"
find "$VALIDATE_TEST_CASES_DIR/$1" -type d -maxdepth 1 -mindepth 1 | grep -v "/\.[^/]*$" | sort | while read a
do do_single "$a" "$VALIDATE_REPORT_DIR/$1"
done
create_summary "$VALIDATE_REPORT_DIR/$1" >"$VALIDATE_REPORT_DIR/$1/summary.html"
tar czf "$VALIDATE_REPORT_DIR/${1}-results.tar.gz" -C "$VALIDATE_REPORT_DIR" "$1"
#echo "$VALIDATE_REPORT_DIR/${1}-results.tar.gz"
}
function create_summary()
# parameter: directory of results
{
cat "$VALIDATE_SUMMARY_TEMPLATE_DIR/header"
find "$1" -type f -name \*.desc -maxdepth 1 | sort | while read a
do TESTNAME=$(basename ${a%.desc})
TESTDESCRIPTION=$(cat $a)
sed "s#_TESTNAME_#$TESTNAME#g; s/_TESTDESCRIPTION_/$TESTDESCRIPTION/" "$VALIDATE_SUMMARY_TEMPLATE_DIR/single_header"
local DIFF_FILE=${a%.desc}.diff
if [ -s "$DIFF_FILE" ]
then cat "$VALIDATE_SUMMARY_TEMPLATE_DIR/result-error"
sed 's#$#<br/>#' "$DIFF_FILE"
else cat "$VALIDATE_SUMMARY_TEMPLATE_DIR/result-ok"
echo "no differences found"
# remove empty diff
[ -e "$DIFF_FILE" ] && rm "$DIFF_FILE"
fi
cat "$VALIDATE_SUMMARY_TEMPLATE_DIR/single_footer"
# remove description file
rm "$a"
done
cat "$VALIDATE_SUMMARY_TEMPLATE_DIR/footer"
}
function import_style()
# get the stylesheet file and images
# change the stylesheet link
{
[ -d "$VALIDATE_REPORT_DIR/cryptobox-img" ] && rm -r "$VALIDATE_REPORT_DIR/cryptobox-img"
mkdir -p "$VALIDATE_REPORT_DIR/cryptobox-img"
[ -e "$VALIDATE_REPORT_DIR/cryptobox.css" ] && rm "$VALIDATE_REPORT_DIR/cryptobox.css"
wget -q -O "$VALIDATE_REPORT_DIR/cryptobox.css" http://$VALIDATE_HOST_IP_DEFAULT/cryptobox.css
# extract image file names
grep "url(/cryptobox-img/" "$VALIDATE_REPORT_DIR/cryptobox.css" | sed 's#^.*url(/cryptobox-img/\(.*\)).*$#\1#' | while read a
do wget -q -O "$VALIDATE_REPORT_DIR/cryptobox-img/$a" "http://$VALIDATE_HOST_IP_DEFAULT/cryptobox-img/$a"
done
# change the stylesheet link in every html file in REPORT_DIR
find "$VALIDATE_REPORT_DIR" -type f -name \*.html | while read a
do sed -i '#link rel="stylesheet"#s#href="/cryptobox.css"#href="../cryptobox.css"#g' "$a"
done
}
function substitute_constants()
{
sed "s/_HOST_IP_DEFAULT_/$VALIDATE_HOST_IP_DEFAULT/g; s/_VALIDATE_HOST_IP_CHANGED_/$VALIDATE_HOST_IP_CHANGED/g"
}
##################### main ###########################
ACTION="--help"
[ $# -gt 0 ] && ACTION=$1
case "$ACTION" in
list )
find "$VALIDATE_TEST_CASES_DIR" -type d -maxdepth 1 -mindepth 1 | grep -v "/\.[^/]*$" | sort | while read a
do echo $(basename "$a")
done
;;
check )
[ $# -ne 2 ] && error_die 1 "Syntax: $(basename $0) check NAME"
case_dir="$VALIDATE_TEST_CASES_DIR/$2"
[ ! -d "$case_dir" ] && error_die 2 "the test case was not found ($case_dir)!"
do_series "$2"
import_style
;;
check_all )
"$0" list | sort | while read a
do echo -n "Validating $a ..."
"$0" check "$a"
echo
echo -n "Waiting 20 seconds ..."
sleep 20
echo
done
;;
* )
echo "Syntax of $(basename $0)"
echo -e "\t list \t\t - show a list of available test cases"
echo -e "\t check NAME \t - execute a test case"
echo -e "\t check_all \t - execute all test cases"
echo -e "\t help \t\t - this syntax information"
echo
;;
esac