2011-02-27 03:28:50 +03:00
#!/bin/sh
2015-12-17 20:56:48 +03:00
#
2016-03-30 03:13:37 +03:00
# Copyright (c) 2011-2016 Dmitry V. Levin <ldv@altlinux.org>
2015-12-17 20:56:48 +03:00
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2011-02-27 03:28:50 +03:00
ME_ = " ${ 0 ##*/ } "
2016-03-30 03:13:37 +03:00
LOG = " $ME_ .tmp "
OUT = " $LOG .out "
EXP = " $LOG .exp "
2016-03-30 03:13:56 +03:00
NAME = " ${ ME_ %.test } "
2011-02-27 03:28:50 +03:00
warn_( ) { printf >& 2 '%s\n' " $* " ; }
fail_( ) { warn_ " $ME_ : failed test: $* " ; exit 1; }
skip_( ) { warn_ " $ME_ : skipped test: $* " ; exit 77; }
framework_failure_( ) { warn_ " $ME_ : framework failure: $* " ; exit 99; }
2011-03-01 03:57:24 +03:00
framework_skip_( ) { warn_ " $ME_ : framework skip: $* " ; exit 77; }
2011-02-27 03:28:50 +03:00
check_prog( )
{
2013-03-18 14:35:06 +04:00
type " $@ " > /dev/null 2>& 1 ||
2011-03-01 03:57:24 +03:00
framework_skip_ " $* is not available "
2011-02-27 03:28:50 +03:00
}
2015-03-17 20:07:57 +03:00
dump_log_and_fail_with( )
{
cat < " $LOG "
fail_ " $* "
}
run_prog( )
{
if [ $# -eq 0 ] ; then
2016-03-30 03:13:56 +03:00
set -- " ./ $NAME "
2015-03-17 20:07:57 +03:00
fi
args = " $* "
" $@ " || {
if [ $? -eq 77 ] ; then
skip_ " $args exited with code 77 "
else
fail_ " $args failed "
fi
}
}
run_prog_skip_if_failed( )
{
args = " $* "
" $@ " || framework_skip_ " $args failed "
}
run_strace( )
{
> " $LOG " || fail_ " failed to write $LOG "
args = " $* "
$STRACE -o " $LOG " " $@ " ||
dump_log_and_fail_with " $STRACE $args failed "
}
run_strace_merge( )
{
rm -f -- " $LOG " .[ 0-9] *
run_strace -ff -tt " $@ "
" $srcdir " /../strace-log-merge " $LOG " > " $LOG " ||
dump_log_and_fail_with 'strace-log-merge failed'
rm -f -- " $LOG " .[ 0-9] *
}
2015-04-07 02:40:13 +03:00
check_gawk( )
{
check_prog gawk
check_prog grep
local program = " $1 " ; shift
if grep '^@include[[:space:]]' < " $program " > /dev/null; then
gawk '@include "/dev/null"' < /dev/null ||
framework_skip_ 'gawk does not support @include'
fi
}
2015-03-17 20:07:57 +03:00
# Usage: [FILE_TO_CHECK [AWK_PROGRAM [ERROR_MESSAGE [EXTRA_AWK_OPTIONS...]]]]
2015-09-19 03:01:35 +03:00
# Check whether AWK_PROGRAM matches FILE_TO_CHECK using gawk.
# If it doesn't, dump FILE_TO_CHECK and fail with ERROR_MESSAGE.
2015-03-17 20:07:57 +03:00
match_awk( )
{
2015-03-18 22:14:02 +03:00
local output program error
2015-03-17 20:07:57 +03:00
if [ $# -eq 0 ] ; then
output = " $LOG "
else
output = " $1 " ; shift
fi
if [ $# -eq 0 ] ; then
2016-03-30 03:13:56 +03:00
program = " $srcdir / $NAME .awk "
2015-03-17 20:07:57 +03:00
else
program = " $1 " ; shift
fi
if [ $# -eq 0 ] ; then
error = " $STRACE $args output mismatch "
else
error = " $1 " ; shift
fi
2015-04-07 02:40:13 +03:00
check_gawk " $program "
2015-03-17 20:07:57 +03:00
2015-03-18 22:14:02 +03:00
AWKPATH = " $srcdir " gawk -f " $program " " $@ " < " $output " || {
2015-03-17 20:07:57 +03:00
cat < " $output "
fail_ " $error "
}
}
# Usage: [FILE_TO_CHECK [FILE_TO_COMPATE_WITH [ERROR_MESSAGE]]]
# Check whether FILE_TO_CHECK differs from FILE_TO_COMPATE_WITH.
# If it does, dump the difference and fail with ERROR_MESSAGE.
match_diff( )
{
local output expected error
if [ $# -eq 0 ] ; then
output = " $LOG "
else
output = " $1 " ; shift
fi
if [ $# -eq 0 ] ; then
2016-03-30 03:13:56 +03:00
expected = " $srcdir / $NAME .expected "
2015-03-17 20:07:57 +03:00
else
expected = " $1 " ; shift
fi
if [ $# -eq 0 ] ; then
error = " $STRACE $args output mismatch "
else
error = " $1 " ; shift
fi
check_prog diff
diff -- " $expected " " $output " ||
fail_ " $error "
}
# Usage: [FILE_TO_CHECK [FILE_WITH_PATTERNS [ERROR_MESSAGE]]]
# Check whether all patterns listed in FILE_WITH_PATTERNS
# match FILE_TO_CHECK using egrep.
# If at least one of these patterns does not match,
# dump both files and fail with ERROR_MESSAGE.
match_grep( )
{
2015-09-19 03:11:07 +03:00
local output patterns error pattern failed =
2015-03-17 20:07:57 +03:00
if [ $# -eq 0 ] ; then
output = " $LOG "
else
output = " $1 " ; shift
fi
if [ $# -eq 0 ] ; then
2016-03-30 03:13:56 +03:00
patterns = " $srcdir / $NAME .expected "
2015-03-17 20:07:57 +03:00
else
patterns = " $1 " ; shift
fi
if [ $# -eq 0 ] ; then
error = " $STRACE $args output mismatch "
else
error = " $1 " ; shift
fi
check_prog wc
check_prog grep
2015-09-19 03:11:07 +03:00
while read -r pattern; do
LC_ALL = C grep -E -x -e " $pattern " < " $output " > /dev/null || {
test -n " $failed " || {
echo 'Failed patterns of expected output:'
failed = 1
}
printf '%s\n' " $pattern "
}
done < " $patterns "
test -z " $failed " || {
2015-03-17 20:07:57 +03:00
echo 'Actual output:'
cat < " $output "
fail_ " $error "
}
}
2016-03-30 03:16:24 +03:00
# Usage: run_strace_match_diff [args to run_strace]
run_strace_match_diff( )
{
args = " $* "
[ -n " $args " -a -z " ${ args ##*-e trace=* } " ] ||
set -- -e trace = " $NAME " " $@ "
run_prog > /dev/null
run_strace " $@ " $args > " $EXP "
match_diff " $LOG " " $EXP "
rm -f " $EXP "
}
2014-09-23 05:51:05 +04:00
check_prog cat
check_prog rm
rm -f " $LOG "
2013-06-18 19:28:47 +04:00
: " ${ STRACE : =../strace } "
: " ${ TIMEOUT_DURATION : =60 } "
2013-06-19 00:51:49 +04:00
: " ${ SLEEP_A_BIT : =sleep 1 } "
2015-11-27 00:25:31 +03:00
[ -z " ${ VERBOSE - } " ] ||
set -x