cryptonas/validation/validate.sh

137 lines
3.9 KiB
Bash
Executable File

#!/bin/sh
#
# do a validation
#
# use "--help" for a list of possible actions
#
set -eu
CB_ROOT_DIR=$(dirname $0)/..
TEST_CASES_DIR=$CB_ROOT_DIR/validation/test-cases
# TODO: take care, that this file exists! (it is created during "upload", "diff" or "merge")
HOST_IP=192.168.0.24
REPORT_DIR=/tmp/cryptobox-validation-$$
REPORT_DIR=$CB_ROOT_DIR/validation/report
SUMMARY_TEMPLATE_DIR=$CB_ROOT_DIR/validation/templates
function error_die()
{
echo "$2" >&2
exit $1
}
function do_single()
# Parameter: "test case dir" "output directory for results"
{
local TESTNAME=$(basename $1)
curl --insecure --silent --output "${2}/${TESTNAME}.html" --config "$1/input.curl"
# 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
diff -NB "${2}/${TESTNAME}.status" "$1/output" >"${2}/${TESTNAME}.diff" || true
rm "${2}/${TESTNAME}.status"
fi
cp "$1/description" "${2}/${TESTNAME}.desc"
}
function do_series()
# parameter: name of the test case
{
[ -d "$REPORT_DIR/$1" ] && rm -r "$REPORT_DIR/$1"
mkdir -p "$REPORT_DIR/$1"
find "$TEST_CASES_DIR/$1" -type d -maxdepth 1 -mindepth 1 | grep -v "/\.[^/]*$" | sort | while read a
do do_single "$a" "$REPORT_DIR/$1"
done
create_summary "$REPORT_DIR/$1" >"$REPORT_DIR/$1/summary.html"
tar czf "$REPORT_DIR/${1}-results.tar.gz" -C "$REPORT_DIR" "$1"
#echo "$REPORT_DIR/${1}-results.tar.gz"
}
create_summary()
# parameter: directory of results
{
cat "$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/" "$SUMMARY_TEMPLATE_DIR/single_header"
local DIFF_FILE=${a%.desc}.diff
if [ -s "$DIFF_FILE" ]
then cat "$SUMMARY_TEMPLATE_DIR/result-error"
sed 's#$#<br/>#' "$DIFF_FILE"
else cat "$SUMMARY_TEMPLATE_DIR/result-ok"
echo "no differences found"
# remove empty diff
[ -e "$DIFF_FILE" ] && rm "$DIFF_FILE"
fi
cat "$SUMMARY_TEMPLATE_DIR/single_footer"
# remove description file
rm "$a"
done
cat "$SUMMARY_TEMPLATE_DIR/footer"
}
import_style()
# get the stylesheet file and images
# change the stylesheet link
{
[ -d "REPORT_DIR/img" ] && rm -r "$REPORT_DIR/img"
mkdir -p "$REPORT_DIR/img"
[ -e "$REPORT_DIR/cryptobox.css" ] && rm "$REPORT_DIR/cryptobox.css"
wget -q -O "$REPORT_DIR/cryptobox.css" http://$HOST_IP/cryptobox.css
# extract image file names
grep "url(img/" "$REPORT_DIR/cryptobox.css" | sed 's#^.*url(img/\(.*\)).*$#\1#' | while read a
do wget -q -O "$REPORT_DIR/img/$a" "http://$HOST_IP/img/$a"
done
# change the stylesheet link in every html file in REPORT_DIR
find "$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
}
ACTION="--help"
[ $# -gt 0 ] && ACTION=$1
case "$ACTION" in
list )
find "$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="$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