Dmitry V. Levin | 4e4b5ad | 2011-02-27 00:28:50 +0000 | [diff] [blame] | 1 | #!/bin/sh |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 2 | # |
Dmitry V. Levin | 2e8a787 | 2016-03-30 00:13:37 +0000 | [diff] [blame] | 3 | # Copyright (c) 2011-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 4 | # 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. Levin | 4e4b5ad | 2011-02-27 00:28:50 +0000 | [diff] [blame] | 27 | |
| 28 | ME_="${0##*/}" |
Dmitry V. Levin | 2e8a787 | 2016-03-30 00:13:37 +0000 | [diff] [blame] | 29 | LOG="$ME_.tmp" |
| 30 | OUT="$LOG.out" |
| 31 | EXP="$LOG.exp" |
Dmitry V. Levin | 579a4aa | 2016-03-30 00:13:56 +0000 | [diff] [blame] | 32 | NAME="${ME_%.test}" |
Dmitry V. Levin | 4e4b5ad | 2011-02-27 00:28:50 +0000 | [diff] [blame] | 33 | |
| 34 | warn_() { printf >&2 '%s\n' "$*"; } |
| 35 | fail_() { warn_ "$ME_: failed test: $*"; exit 1; } |
| 36 | skip_() { warn_ "$ME_: skipped test: $*"; exit 77; } |
| 37 | framework_failure_() { warn_ "$ME_: framework failure: $*"; exit 99; } |
Mike Frysinger | e689e04 | 2011-02-28 19:57:24 -0500 | [diff] [blame] | 38 | framework_skip_() { warn_ "$ME_: framework skip: $*"; exit 77; } |
Dmitry V. Levin | 4e4b5ad | 2011-02-27 00:28:50 +0000 | [diff] [blame] | 39 | |
| 40 | check_prog() |
| 41 | { |
Maxin B. John | 066a733 | 2013-03-18 11:35:06 +0100 | [diff] [blame] | 42 | type "$@" > /dev/null 2>&1 || |
Mike Frysinger | e689e04 | 2011-02-28 19:57:24 -0500 | [diff] [blame] | 43 | framework_skip_ "$* is not available" |
Dmitry V. Levin | 4e4b5ad | 2011-02-27 00:28:50 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 46 | dump_log_and_fail_with() |
| 47 | { |
| 48 | cat < "$LOG" |
| 49 | fail_ "$*" |
| 50 | } |
| 51 | |
| 52 | run_prog() |
| 53 | { |
| 54 | if [ $# -eq 0 ]; then |
Dmitry V. Levin | 579a4aa | 2016-03-30 00:13:56 +0000 | [diff] [blame] | 55 | set -- "./$NAME" |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 56 | fi |
| 57 | args="$*" |
| 58 | "$@" || { |
| 59 | if [ $? -eq 77 ]; then |
| 60 | skip_ "$args exited with code 77" |
| 61 | else |
| 62 | fail_ "$args failed" |
| 63 | fi |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | run_prog_skip_if_failed() |
| 69 | { |
| 70 | args="$*" |
| 71 | "$@" || framework_skip_ "$args failed" |
| 72 | } |
| 73 | |
| 74 | run_strace() |
| 75 | { |
| 76 | > "$LOG" || fail_ "failed to write $LOG" |
| 77 | args="$*" |
| 78 | $STRACE -o "$LOG" "$@" || |
| 79 | dump_log_and_fail_with "$STRACE $args failed" |
| 80 | } |
| 81 | |
| 82 | run_strace_merge() |
| 83 | { |
| 84 | rm -f -- "$LOG".[0-9]* |
| 85 | run_strace -ff -tt "$@" |
| 86 | "$srcdir"/../strace-log-merge "$LOG" > "$LOG" || |
| 87 | dump_log_and_fail_with 'strace-log-merge failed' |
| 88 | rm -f -- "$LOG".[0-9]* |
| 89 | } |
| 90 | |
Dmitry V. Levin | 79c5c5d | 2015-04-06 23:40:13 +0000 | [diff] [blame] | 91 | check_gawk() |
| 92 | { |
| 93 | check_prog gawk |
| 94 | check_prog grep |
| 95 | |
| 96 | local program="$1"; shift |
| 97 | if grep '^@include[[:space:]]' < "$program" > /dev/null; then |
| 98 | gawk '@include "/dev/null"' < /dev/null || |
| 99 | framework_skip_ 'gawk does not support @include' |
| 100 | fi |
| 101 | } |
| 102 | |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 103 | # Usage: [FILE_TO_CHECK [AWK_PROGRAM [ERROR_MESSAGE [EXTRA_AWK_OPTIONS...]]]] |
Dmitry V. Levin | a69854a | 2015-09-19 00:01:35 +0000 | [diff] [blame] | 104 | # Check whether AWK_PROGRAM matches FILE_TO_CHECK using gawk. |
| 105 | # If it doesn't, dump FILE_TO_CHECK and fail with ERROR_MESSAGE. |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 106 | match_awk() |
| 107 | { |
Dmitry V. Levin | 226bf1c | 2015-03-18 19:14:02 +0000 | [diff] [blame] | 108 | local output program error |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 109 | if [ $# -eq 0 ]; then |
| 110 | output="$LOG" |
| 111 | else |
| 112 | output="$1"; shift |
| 113 | fi |
| 114 | if [ $# -eq 0 ]; then |
Dmitry V. Levin | 579a4aa | 2016-03-30 00:13:56 +0000 | [diff] [blame] | 115 | program="$srcdir/$NAME.awk" |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 116 | else |
| 117 | program="$1"; shift |
| 118 | fi |
| 119 | if [ $# -eq 0 ]; then |
| 120 | error="$STRACE $args output mismatch" |
| 121 | else |
| 122 | error="$1"; shift |
| 123 | fi |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 124 | |
Dmitry V. Levin | 79c5c5d | 2015-04-06 23:40:13 +0000 | [diff] [blame] | 125 | check_gawk "$program" |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 126 | |
Dmitry V. Levin | 226bf1c | 2015-03-18 19:14:02 +0000 | [diff] [blame] | 127 | AWKPATH="$srcdir" gawk -f "$program" "$@" < "$output" || { |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 128 | cat < "$output" |
| 129 | fail_ "$error" |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | # Usage: [FILE_TO_CHECK [FILE_TO_COMPATE_WITH [ERROR_MESSAGE]]] |
| 134 | # Check whether FILE_TO_CHECK differs from FILE_TO_COMPATE_WITH. |
| 135 | # If it does, dump the difference and fail with ERROR_MESSAGE. |
| 136 | match_diff() |
| 137 | { |
| 138 | local output expected error |
| 139 | if [ $# -eq 0 ]; then |
| 140 | output="$LOG" |
| 141 | else |
| 142 | output="$1"; shift |
| 143 | fi |
| 144 | if [ $# -eq 0 ]; then |
Dmitry V. Levin | 579a4aa | 2016-03-30 00:13:56 +0000 | [diff] [blame] | 145 | expected="$srcdir/$NAME.expected" |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 146 | else |
| 147 | expected="$1"; shift |
| 148 | fi |
| 149 | if [ $# -eq 0 ]; then |
| 150 | error="$STRACE $args output mismatch" |
| 151 | else |
| 152 | error="$1"; shift |
| 153 | fi |
| 154 | |
| 155 | check_prog diff |
| 156 | |
| 157 | diff -- "$expected" "$output" || |
| 158 | fail_ "$error" |
| 159 | } |
| 160 | |
| 161 | # Usage: [FILE_TO_CHECK [FILE_WITH_PATTERNS [ERROR_MESSAGE]]] |
| 162 | # Check whether all patterns listed in FILE_WITH_PATTERNS |
| 163 | # match FILE_TO_CHECK using egrep. |
| 164 | # If at least one of these patterns does not match, |
| 165 | # dump both files and fail with ERROR_MESSAGE. |
| 166 | match_grep() |
| 167 | { |
Dmitry V. Levin | 3b73194 | 2015-09-19 00:11:07 +0000 | [diff] [blame] | 168 | local output patterns error pattern failed= |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 169 | if [ $# -eq 0 ]; then |
| 170 | output="$LOG" |
| 171 | else |
| 172 | output="$1"; shift |
| 173 | fi |
| 174 | if [ $# -eq 0 ]; then |
Dmitry V. Levin | 579a4aa | 2016-03-30 00:13:56 +0000 | [diff] [blame] | 175 | patterns="$srcdir/$NAME.expected" |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 176 | else |
| 177 | patterns="$1"; shift |
| 178 | fi |
| 179 | if [ $# -eq 0 ]; then |
| 180 | error="$STRACE $args output mismatch" |
| 181 | else |
| 182 | error="$1"; shift |
| 183 | fi |
| 184 | |
| 185 | check_prog wc |
| 186 | check_prog grep |
| 187 | |
Dmitry V. Levin | 3b73194 | 2015-09-19 00:11:07 +0000 | [diff] [blame] | 188 | while read -r pattern; do |
| 189 | LC_ALL=C grep -E -x -e "$pattern" < "$output" > /dev/null || { |
| 190 | test -n "$failed" || { |
| 191 | echo 'Failed patterns of expected output:' |
| 192 | failed=1 |
| 193 | } |
| 194 | printf '%s\n' "$pattern" |
| 195 | } |
| 196 | done < "$patterns" |
| 197 | test -z "$failed" || { |
Dmitry V. Levin | 8f54664 | 2015-03-17 17:07:57 +0000 | [diff] [blame] | 198 | echo 'Actual output:' |
| 199 | cat < "$output" |
| 200 | fail_ "$error" |
| 201 | } |
| 202 | } |
| 203 | |
Dmitry V. Levin | 5dde567 | 2016-03-30 00:16:24 +0000 | [diff] [blame] | 204 | # Usage: run_strace_match_diff [args to run_strace] |
| 205 | run_strace_match_diff() |
| 206 | { |
| 207 | args="$*" |
| 208 | [ -n "$args" -a -z "${args##*-e trace=*}" ] || |
| 209 | set -- -e trace="$NAME" "$@" |
| 210 | run_prog > /dev/null |
| 211 | run_strace "$@" $args > "$EXP" |
| 212 | match_diff "$LOG" "$EXP" |
| 213 | rm -f "$EXP" |
| 214 | } |
| 215 | |
Dmitry V. Levin | 3ec5c04 | 2014-09-23 01:51:05 +0000 | [diff] [blame] | 216 | check_prog cat |
| 217 | check_prog rm |
| 218 | |
Dmitry V. Levin | 3ec5c04 | 2014-09-23 01:51:05 +0000 | [diff] [blame] | 219 | rm -f "$LOG" |
| 220 | |
Dmitry V. Levin | f60347d | 2013-06-18 15:28:47 +0000 | [diff] [blame] | 221 | : "${STRACE:=../strace}" |
| 222 | : "${TIMEOUT_DURATION:=60}" |
Dmitry V. Levin | 1e0a280 | 2013-06-18 20:51:49 +0000 | [diff] [blame] | 223 | : "${SLEEP_A_BIT:=sleep 1}" |
Dmitry V. Levin | 4f7db9a | 2015-11-26 21:25:31 +0000 | [diff] [blame] | 224 | |
| 225 | [ -z "${VERBOSE-}" ] || |
| 226 | set -x |