blob: 46a08f95ea6aeca84f12627b6ed30cfabadcd636 [file] [log] [blame]
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +00001#!/bin/sh
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00002#
Dmitry V. Levin2e8a7872016-03-30 00:13:37 +00003# Copyright (c) 2011-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00004# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# 3. The name of the author may not be used to endorse or promote products
15# derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +000027
28ME_="${0##*/}"
Dmitry V. Levin2e8a7872016-03-30 00:13:37 +000029LOG="$ME_.tmp"
30OUT="$LOG.out"
31EXP="$LOG.exp"
Dmitry V. Levin579a4aa2016-03-30 00:13:56 +000032NAME="${ME_%.test}"
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +000033
34warn_() { printf >&2 '%s\n' "$*"; }
35fail_() { warn_ "$ME_: failed test: $*"; exit 1; }
36skip_() { warn_ "$ME_: skipped test: $*"; exit 77; }
37framework_failure_() { warn_ "$ME_: framework failure: $*"; exit 99; }
Mike Frysingere689e042011-02-28 19:57:24 -050038framework_skip_() { warn_ "$ME_: framework skip: $*"; exit 77; }
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +000039
40check_prog()
41{
Maxin B. John066a7332013-03-18 11:35:06 +010042 type "$@" > /dev/null 2>&1 ||
Mike Frysingere689e042011-02-28 19:57:24 -050043 framework_skip_ "$* is not available"
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +000044}
45
Dmitry V. Levin8f546642015-03-17 17:07:57 +000046dump_log_and_fail_with()
47{
48 cat < "$LOG"
49 fail_ "$*"
50}
51
52run_prog()
53{
54 if [ $# -eq 0 ]; then
Dmitry V. Levin579a4aa2016-03-30 00:13:56 +000055 set -- "./$NAME"
Dmitry V. Levin8f546642015-03-17 17:07:57 +000056 fi
57 args="$*"
58 "$@" || {
Dmitry V. Levine4e9df22016-08-13 22:05:20 +000059 rc=$?
60 if [ $rc -eq 77 ]; then
Dmitry V. Levin8f546642015-03-17 17:07:57 +000061 skip_ "$args exited with code 77"
62 else
Dmitry V. Levine4e9df22016-08-13 22:05:20 +000063 fail_ "$args failed with code $rc"
Dmitry V. Levin8f546642015-03-17 17:07:57 +000064 fi
65 }
66}
67
68
69run_prog_skip_if_failed()
70{
71 args="$*"
Dmitry V. Levine4e9df22016-08-13 22:05:20 +000072 "$@" || framework_skip_ "$args failed with code $?"
Dmitry V. Levin8f546642015-03-17 17:07:57 +000073}
74
75run_strace()
76{
77 > "$LOG" || fail_ "failed to write $LOG"
78 args="$*"
79 $STRACE -o "$LOG" "$@" ||
Dmitry V. Levine4e9df22016-08-13 22:05:20 +000080 dump_log_and_fail_with "$STRACE $args failed with code $?"
Dmitry V. Levin8f546642015-03-17 17:07:57 +000081}
82
83run_strace_merge()
84{
85 rm -f -- "$LOG".[0-9]*
86 run_strace -ff -tt "$@"
87 "$srcdir"/../strace-log-merge "$LOG" > "$LOG" ||
Dmitry V. Levine4e9df22016-08-13 22:05:20 +000088 dump_log_and_fail_with 'strace-log-merge failed with code $?'
Dmitry V. Levin8f546642015-03-17 17:07:57 +000089 rm -f -- "$LOG".[0-9]*
90}
91
Dmitry V. Levin79c5c5d2015-04-06 23:40:13 +000092check_gawk()
93{
94 check_prog gawk
95 check_prog grep
96
97 local program="$1"; shift
98 if grep '^@include[[:space:]]' < "$program" > /dev/null; then
99 gawk '@include "/dev/null"' < /dev/null ||
100 framework_skip_ 'gawk does not support @include'
101 fi
102}
103
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000104# Usage: [FILE_TO_CHECK [AWK_PROGRAM [ERROR_MESSAGE [EXTRA_AWK_OPTIONS...]]]]
Dmitry V. Levina69854a2015-09-19 00:01:35 +0000105# Check whether AWK_PROGRAM matches FILE_TO_CHECK using gawk.
106# If it doesn't, dump FILE_TO_CHECK and fail with ERROR_MESSAGE.
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000107match_awk()
108{
Dmitry V. Levin226bf1c2015-03-18 19:14:02 +0000109 local output program error
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000110 if [ $# -eq 0 ]; then
111 output="$LOG"
112 else
113 output="$1"; shift
114 fi
115 if [ $# -eq 0 ]; then
Dmitry V. Levin579a4aa2016-03-30 00:13:56 +0000116 program="$srcdir/$NAME.awk"
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000117 else
118 program="$1"; shift
119 fi
120 if [ $# -eq 0 ]; then
121 error="$STRACE $args output mismatch"
122 else
123 error="$1"; shift
124 fi
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000125
Dmitry V. Levin79c5c5d2015-04-06 23:40:13 +0000126 check_gawk "$program"
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000127
Dmitry V. Levin226bf1c2015-03-18 19:14:02 +0000128 AWKPATH="$srcdir" gawk -f "$program" "$@" < "$output" || {
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000129 cat < "$output"
130 fail_ "$error"
131 }
132}
133
134# Usage: [FILE_TO_CHECK [FILE_TO_COMPATE_WITH [ERROR_MESSAGE]]]
135# Check whether FILE_TO_CHECK differs from FILE_TO_COMPATE_WITH.
136# If it does, dump the difference and fail with ERROR_MESSAGE.
137match_diff()
138{
139 local output expected error
140 if [ $# -eq 0 ]; then
141 output="$LOG"
142 else
143 output="$1"; shift
144 fi
145 if [ $# -eq 0 ]; then
Dmitry V. Levin579a4aa2016-03-30 00:13:56 +0000146 expected="$srcdir/$NAME.expected"
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000147 else
148 expected="$1"; shift
149 fi
150 if [ $# -eq 0 ]; then
151 error="$STRACE $args output mismatch"
152 else
153 error="$1"; shift
154 fi
155
156 check_prog diff
157
158 diff -- "$expected" "$output" ||
159 fail_ "$error"
160}
161
162# Usage: [FILE_TO_CHECK [FILE_WITH_PATTERNS [ERROR_MESSAGE]]]
163# Check whether all patterns listed in FILE_WITH_PATTERNS
164# match FILE_TO_CHECK using egrep.
165# If at least one of these patterns does not match,
166# dump both files and fail with ERROR_MESSAGE.
167match_grep()
168{
Dmitry V. Levin3b731942015-09-19 00:11:07 +0000169 local output patterns error pattern failed=
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000170 if [ $# -eq 0 ]; then
171 output="$LOG"
172 else
173 output="$1"; shift
174 fi
175 if [ $# -eq 0 ]; then
Dmitry V. Levin579a4aa2016-03-30 00:13:56 +0000176 patterns="$srcdir/$NAME.expected"
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000177 else
178 patterns="$1"; shift
179 fi
180 if [ $# -eq 0 ]; then
181 error="$STRACE $args output mismatch"
182 else
183 error="$1"; shift
184 fi
185
186 check_prog wc
187 check_prog grep
188
Dmitry V. Levin3b731942015-09-19 00:11:07 +0000189 while read -r pattern; do
190 LC_ALL=C grep -E -x -e "$pattern" < "$output" > /dev/null || {
191 test -n "$failed" || {
192 echo 'Failed patterns of expected output:'
193 failed=1
194 }
195 printf '%s\n' "$pattern"
196 }
197 done < "$patterns"
198 test -z "$failed" || {
Dmitry V. Levin8f546642015-03-17 17:07:57 +0000199 echo 'Actual output:'
200 cat < "$output"
201 fail_ "$error"
202 }
203}
204
Dmitry V. Levin5dde5672016-03-30 00:16:24 +0000205# Usage: run_strace_match_diff [args to run_strace]
206run_strace_match_diff()
207{
208 args="$*"
209 [ -n "$args" -a -z "${args##*-e trace=*}" ] ||
210 set -- -e trace="$NAME" "$@"
211 run_prog > /dev/null
212 run_strace "$@" $args > "$EXP"
213 match_diff "$LOG" "$EXP"
214 rm -f "$EXP"
215}
216
Dmitry V. Levin3ec5c042014-09-23 01:51:05 +0000217check_prog cat
218check_prog rm
219
Dmitry V. Levin3ec5c042014-09-23 01:51:05 +0000220rm -f "$LOG"
221
Dmitry V. Levinf60347d2013-06-18 15:28:47 +0000222: "${STRACE:=../strace}"
223: "${TIMEOUT_DURATION:=60}"
Dmitry V. Levin1e0a2802013-06-18 20:51:49 +0000224: "${SLEEP_A_BIT:=sleep 1}"
Dmitry V. Levin4f7db9a2015-11-26 21:25:31 +0000225
226[ -z "${VERBOSE-}" ] ||
227 set -x