blob: 0c5b121f0405ae0e995c5761a3a8443dfbfd2757 [file] [log] [blame]
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +00001#!/bin/sh
2
3ME_="${0##*/}"
4
5warn_() { printf >&2 '%s\n' "$*"; }
6fail_() { warn_ "$ME_: failed test: $*"; exit 1; }
7skip_() { warn_ "$ME_: skipped test: $*"; exit 77; }
8framework_failure_() { warn_ "$ME_: framework failure: $*"; exit 99; }
Mike Frysingere689e042011-02-28 19:57:24 -05009framework_skip_() { warn_ "$ME_: framework skip: $*"; exit 77; }
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +000010
11check_prog()
12{
Maxin B. John066a7332013-03-18 11:35:06 +010013 type "$@" > /dev/null 2>&1 ||
Mike Frysingere689e042011-02-28 19:57:24 -050014 framework_skip_ "$* is not available"
Dmitry V. Levin4e4b5ad2011-02-27 00:28:50 +000015}
16
Dmitry V. Levin8f546642015-03-17 17:07:57 +000017dump_log_and_fail_with()
18{
19 cat < "$LOG"
20 fail_ "$*"
21}
22
23run_prog()
24{
25 if [ $# -eq 0 ]; then
26 set -- "./${ME_%.test}"
27 fi
28 args="$*"
29 "$@" || {
30 if [ $? -eq 77 ]; then
31 skip_ "$args exited with code 77"
32 else
33 fail_ "$args failed"
34 fi
35 }
36}
37
38
39run_prog_skip_if_failed()
40{
41 args="$*"
42 "$@" || framework_skip_ "$args failed"
43}
44
45run_strace()
46{
47 > "$LOG" || fail_ "failed to write $LOG"
48 args="$*"
49 $STRACE -o "$LOG" "$@" ||
50 dump_log_and_fail_with "$STRACE $args failed"
51}
52
53run_strace_merge()
54{
55 rm -f -- "$LOG".[0-9]*
56 run_strace -ff -tt "$@"
57 "$srcdir"/../strace-log-merge "$LOG" > "$LOG" ||
58 dump_log_and_fail_with 'strace-log-merge failed'
59 rm -f -- "$LOG".[0-9]*
60}
61
62# Usage: [FILE_TO_CHECK [AWK_PROGRAM [ERROR_MESSAGE [EXTRA_AWK_OPTIONS...]]]]
63# Check whether all patterns listed in AWK_PROGRAM
64# match FILE_TO_CHECK using egrep.
65# If at least one of these patterns does not match,
66# dump both files and fail with ERROR_MESSAGE.
67match_awk()
68{
Dmitry V. Levin226bf1c2015-03-18 19:14:02 +000069 local output program error
Dmitry V. Levin8f546642015-03-17 17:07:57 +000070 if [ $# -eq 0 ]; then
71 output="$LOG"
72 else
73 output="$1"; shift
74 fi
75 if [ $# -eq 0 ]; then
76 program="$srcdir/${ME_%.test}.awk"
77 else
78 program="$1"; shift
79 fi
80 if [ $# -eq 0 ]; then
81 error="$STRACE $args output mismatch"
82 else
83 error="$1"; shift
84 fi
Dmitry V. Levin8f546642015-03-17 17:07:57 +000085
Dmitry V. Levin226bf1c2015-03-18 19:14:02 +000086 check_prog gawk
Dmitry V. Levin8f546642015-03-17 17:07:57 +000087
Dmitry V. Levin226bf1c2015-03-18 19:14:02 +000088 AWKPATH="$srcdir" gawk -f "$program" "$@" < "$output" || {
Dmitry V. Levin8f546642015-03-17 17:07:57 +000089 cat < "$output"
90 fail_ "$error"
91 }
92}
93
94# Usage: [FILE_TO_CHECK [FILE_TO_COMPATE_WITH [ERROR_MESSAGE]]]
95# Check whether FILE_TO_CHECK differs from FILE_TO_COMPATE_WITH.
96# If it does, dump the difference and fail with ERROR_MESSAGE.
97match_diff()
98{
99 local output expected error
100 if [ $# -eq 0 ]; then
101 output="$LOG"
102 else
103 output="$1"; shift
104 fi
105 if [ $# -eq 0 ]; then
106 expected="$srcdir/${ME_%.test}.expected"
107 else
108 expected="$1"; shift
109 fi
110 if [ $# -eq 0 ]; then
111 error="$STRACE $args output mismatch"
112 else
113 error="$1"; shift
114 fi
115
116 check_prog diff
117
118 diff -- "$expected" "$output" ||
119 fail_ "$error"
120}
121
122# Usage: [FILE_TO_CHECK [FILE_WITH_PATTERNS [ERROR_MESSAGE]]]
123# Check whether all patterns listed in FILE_WITH_PATTERNS
124# match FILE_TO_CHECK using egrep.
125# If at least one of these patterns does not match,
126# dump both files and fail with ERROR_MESSAGE.
127match_grep()
128{
129 local output patterns error expected matched
130 if [ $# -eq 0 ]; then
131 output="$LOG"
132 else
133 output="$1"; shift
134 fi
135 if [ $# -eq 0 ]; then
136 patterns="$srcdir/${ME_%.test}.expected"
137 else
138 patterns="$1"; shift
139 fi
140 if [ $# -eq 0 ]; then
141 error="$STRACE $args output mismatch"
142 else
143 error="$1"; shift
144 fi
145
146 check_prog wc
147 check_prog grep
148
149 expected=$(wc -l < "$patterns") &&
150 matched=$(LC_ALL=C grep -c -E -x -f "$patterns" < "$output") &&
151 test "$expected" -eq "$matched" || {
152 echo 'Patterns of expected output:'
153 cat < "$patterns"
154 echo 'Actual output:'
155 cat < "$output"
156 fail_ "$error"
157 }
158}
159
Dmitry V. Levin3ec5c042014-09-23 01:51:05 +0000160check_prog cat
161check_prog rm
162
163LOG="$ME_.tmp"
164rm -f "$LOG"
165
Dmitry V. Levinf60347d2013-06-18 15:28:47 +0000166: "${STRACE:=../strace}"
167: "${TIMEOUT_DURATION:=60}"
Dmitry V. Levin1e0a2802013-06-18 20:51:49 +0000168: "${SLEEP_A_BIT:=sleep 1}"