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