blob: a3ccd1fa2b5d8e2dd87f6aaf6c80aa8c677e8205 [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{
69 local output program error awk
70 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
85 awk=${AWK:-awk}
86
87 check_prog "$awk"
88
89 "$awk" -f "$program" "$@" < "$output" || {
90 cat < "$output"
91 fail_ "$error"
92 }
93}
94
95# Usage: [FILE_TO_CHECK [FILE_TO_COMPATE_WITH [ERROR_MESSAGE]]]
96# Check whether FILE_TO_CHECK differs from FILE_TO_COMPATE_WITH.
97# If it does, dump the difference and fail with ERROR_MESSAGE.
98match_diff()
99{
100 local output expected error
101 if [ $# -eq 0 ]; then
102 output="$LOG"
103 else
104 output="$1"; shift
105 fi
106 if [ $# -eq 0 ]; then
107 expected="$srcdir/${ME_%.test}.expected"
108 else
109 expected="$1"; shift
110 fi
111 if [ $# -eq 0 ]; then
112 error="$STRACE $args output mismatch"
113 else
114 error="$1"; shift
115 fi
116
117 check_prog diff
118
119 diff -- "$expected" "$output" ||
120 fail_ "$error"
121}
122
123# Usage: [FILE_TO_CHECK [FILE_WITH_PATTERNS [ERROR_MESSAGE]]]
124# Check whether all patterns listed in FILE_WITH_PATTERNS
125# match FILE_TO_CHECK using egrep.
126# If at least one of these patterns does not match,
127# dump both files and fail with ERROR_MESSAGE.
128match_grep()
129{
130 local output patterns error expected matched
131 if [ $# -eq 0 ]; then
132 output="$LOG"
133 else
134 output="$1"; shift
135 fi
136 if [ $# -eq 0 ]; then
137 patterns="$srcdir/${ME_%.test}.expected"
138 else
139 patterns="$1"; shift
140 fi
141 if [ $# -eq 0 ]; then
142 error="$STRACE $args output mismatch"
143 else
144 error="$1"; shift
145 fi
146
147 check_prog wc
148 check_prog grep
149
150 expected=$(wc -l < "$patterns") &&
151 matched=$(LC_ALL=C grep -c -E -x -f "$patterns" < "$output") &&
152 test "$expected" -eq "$matched" || {
153 echo 'Patterns of expected output:'
154 cat < "$patterns"
155 echo 'Actual output:'
156 cat < "$output"
157 fail_ "$error"
158 }
159}
160
Dmitry V. Levin3ec5c042014-09-23 01:51:05 +0000161check_prog cat
162check_prog rm
163
164LOG="$ME_.tmp"
165rm -f "$LOG"
166
Dmitry V. Levinf60347d2013-06-18 15:28:47 +0000167: "${STRACE:=../strace}"
168: "${TIMEOUT_DURATION:=60}"
Dmitry V. Levin1e0a2802013-06-18 20:51:49 +0000169: "${SLEEP_A_BIT:=sleep 1}"