Revisit https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=3161
[obnox/wireshark/wip.git] / test / test-backend.sh
1 #!/bin/bash
2 #
3 # Test backend
4 #
5 # $Id$
6 #
7 # Wireshark - Network traffic analyzer
8 # By Gerald Combs <gerald@wireshark.org>
9 # Copyright 2005 Ulf Lamping
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24 #
25
26
27 # References:
28 # http://www.gnu.org/software/bash/manual/bashref.html "Bash Reference Manual"
29 # http://www.tldp.org/LDP/abs/html/ "Advanced Bash-Scripting Guide"
30 # http://www.tldp.org/LDP/abs/html/colorizing.html "Colorizing" Scripts"
31 # http://www.junit.org/junit/javadoc/3.8.1/index.htm "JUnit javadoc"
32
33 # check undefined variables
34 # http://www.tldp.org/LDP/abs/html/options.html
35 # bash -u test.sh
36
37
38 # coloring the output
39 if [ $USE_COLOR -eq 1 ] ; then
40         color_reset="tput sgr0"
41         color_green='\E[32;40m'
42         color_red='\E[31;40m'
43         color_yellow='\E[33;40m'
44         color_blue='\E[36;40m'
45 else
46         color_reset="/bin/true"
47         color_green=''
48         color_red=''
49         color_yellow=''
50         color_blue=''
51 fi
52
53 # runtime flags
54 TEST_RUN="OFF"
55 TEST_OUTPUT="VERBOSE"   # "OFF", "DOTTED", "VERBOSE"
56
57 # runtime vars
58 TEST_NESTING_LEVEL=0    # nesting level of current test
59 TEST_STEPS[0]=0                 # number of steps of a specific nesting level
60
61 # output counters
62 TEST_OK=0                               # global count of succeeded steps
63 TEST_FAILED=0                   # global count of failed steps
64 TEST_SKIPPED=0                  # global count of failed steps
65
66 TEST_STEP_PRE_CB=
67 TEST_STEP_POST_CB=
68
69 # level number of this test item (suite or step)
70 test_level() {
71         LIMIT_LEVEL=100
72
73         for ((a=0; a <= LIMIT_LEVEL ; a++))
74         do
75                 if [ ! $a -eq 0 ]; then
76                         echo -n "."
77                 fi
78                 echo -n "${TEST_STEPS[a]}"
79                 if [ $a -eq $TEST_NESTING_LEVEL ]; then
80                         #echo "end"
81                         return
82                 fi
83         done
84 }
85
86 # set output format
87 # $1 - "OUT", "DOTTED", "VERBOSE"
88 test_set_output() {
89         TEST_OUTPUT=$1
90 }
91
92 # run a test suite
93 # $1 name
94 # $2 command
95 test_suite_run() {
96         # header
97         echo -n -e $color_blue
98         echo ""
99         echo "### $1 ###"
100         $color_reset
101
102         TEST_RUN="ON"
103
104         # run the actual test suite
105         $2
106
107         # results
108         if [ $TEST_RUN = "ON" ]; then
109                 echo ""
110                 if [ $TEST_FAILED -eq 0 ]; then
111                         echo -n -e $color_green
112                 else
113                         echo -n -e $color_red
114                 fi
115                 echo "### Test suite results ###"
116                 echo -n -e $color_green
117                 echo "Ok    : $TEST_OK"
118                 echo -n -e $color_red
119                 echo "Failed: $TEST_FAILED"
120                 echo -n -e $color_yellow
121                 echo "Skipped: $TEST_SKIPPED"
122                 $color_reset
123         fi
124
125         TEST_RUN="OFF"
126
127         # exit status
128         if [ $TEST_FAILED -eq 0 ]; then
129                 return 0
130         else
131                 return 1
132         fi
133 }
134
135
136 # show a test suite
137 # $1 name
138 # $2 command
139 test_suite_show() {
140
141         # header
142         echo -n -e $color_blue
143         echo ""
144         echo "### Test suite: $1 ###"
145         echo ""
146         echo "Subitems:"
147         echo "---------"
148         $color_reset
149
150         # show this test suite subitems
151         $2
152
153         echo ""
154 }
155
156
157 # add a test suite
158 # $1 name
159 # $2 function
160 test_suite_add() {
161         # increase step counter of this nesting level
162         let "TEST_STEPS[$TEST_NESTING_LEVEL] += 1"
163
164         if [ $TEST_RUN = "ON" ]; then
165                 echo ""
166         fi
167
168         # title output if we'll list the subitems
169         if [[ $TEST_RUN = "ON" ]]; then
170                 echo -n -e $color_blue
171                 test_level
172                 echo "  Suite: $1"
173                 $color_reset
174         fi
175
176         if [[ $TEST_NESTING_LEVEL -eq 0 ]]; then
177                 pos=${TEST_STEPS[$TEST_NESTING_LEVEL]}
178                 #echo "pos " $pos
179                 test_title[$pos]=$1
180                 test_function[$pos]=$2
181                 #echo ${test_title[1]}
182
183         fi
184
185         # reset test step counter back to zero
186         TEST_STEP=0
187
188         # call the suites function
189         let "TEST_NESTING_LEVEL += 1"
190         TEST_STEPS[$TEST_NESTING_LEVEL]=0
191         $2
192         let "TEST_NESTING_LEVEL -= 1"
193
194         # title output (with subitem counter) if we don't listed the subitems
195         if [[ ! $TEST_RUN = "ON" && $TEST_NESTING_LEVEL -eq 0 ]]; then
196                 echo -n -e $color_blue
197                 test_level
198                 echo "  Suite: $1 (${TEST_STEPS[TEST_NESTING_LEVEL+1]} subitems)"
199                 $color_reset
200         fi
201
202 }
203
204
205 # add a test step
206 # $1 name
207 # $2 function
208 test_step_add() {
209
210         let "TEST_STEPS[$TEST_NESTING_LEVEL] += 1"
211
212         if [[ ($TEST_RUN = "ON" && $TEST_OUTPUT = "DOTTED") && $TEST_NESTING_LEVEL -eq 0 ]]; then
213                 echo ""
214         fi
215
216         if [[ ( $TEST_RUN = "ON" && $TEST_OUTPUT = "VERBOSE" ) || $TEST_NESTING_LEVEL -eq 0 ]]; then
217                 echo -n -e $color_blue
218                 test_level
219                 echo -n " Step:" $1
220                 $color_reset
221         fi
222
223         if [ $TEST_RUN = "ON" ]; then
224                 # preprecessing step
225                 $TEST_STEP_PRE_CB
226                 #echo "command: "$2" opt1: "$3" opt2: "$4" opt3: "$5" opt4: "$6" opt5: "$7
227                 TEST_STEP_NAME=$1
228                 # actually run the command to test now
229                 $2
230                 #"$3" "$4" "$5" "$6" "$7"
231                 # post precessing step
232                 $TEST_STEP_POST_CB
233         else
234                 if [[ $TEST_NESTING_LEVEL -eq 0 ]]; then
235                         echo ""
236                 fi
237         fi
238 }
239
240
241 # set the preprocessing function
242 # $1 remark
243 test_step_set_pre() {
244         TEST_STEP_PRE_CB=$1
245 }
246
247 # set the post processing function
248 # $1 remark
249 test_step_set_post() {
250         TEST_STEP_POST_CB=$1
251 }
252
253 # add a test remark
254 # $1 remark
255 test_remark_add() {
256
257         # test is running or toplevel item? -> show remark
258         if [[ $TEST_RUN = "ON" || $TEST_NESTING_LEVEL -eq 0 ]]; then
259                 # test is running and output is dotted -> newline first
260                 if [[ $TEST_RUN = "ON" && $TEST_OUTPUT = "DOTTED" ]]; then
261                         echo ""
262                 fi
263
264                 # remark
265                 echo -n -e $color_blue
266                 echo "  Remark: $1"
267                 $color_reset
268         fi
269 }
270
271
272 # the test step succeeded
273 test_step_ok() {
274         # count appearance
275         let "TEST_OK += 1"
276
277         # output in green
278         echo -n -e $color_green
279
280         if [ $TEST_OUTPUT = "VERBOSE" ]; then
281                 echo " Ok"
282         else
283                 echo -n .
284         fi
285
286         $color_reset
287 }
288
289 # the test step failed
290 # $1 output text
291 test_step_failed() {
292         let "TEST_FAILED += 1"
293
294         # output in red
295         echo -n -e "$color_red"
296
297         echo ""
298         echo "\"$TEST_STEP_NAME\" Failed!"
299         echo $1
300
301         $color_reset
302
303         exit 1
304
305         # XXX - add a mechanism to optionally stop here
306 }
307
308 # the test step skipped (usually not suitable for this machine/platform)
309 test_step_skipped() {
310         # count appearance
311         let "TEST_SKIPPED += 1"
312
313         # output in green
314         echo -n -e $color_yellow
315
316         if [ $TEST_OUTPUT = "VERBOSE" ]; then
317                 echo " Skipped"
318         else
319                 echo -n .
320         fi
321
322         $color_reset
323 }
324
325 test_step_output_print() {
326         wait
327         printf "\n"
328         for f in "$@"; do
329                 if [[ -f "$f" ]]; then
330                         printf " --> $f\n"
331                         cat "$f"
332                         printf " <--\n"
333                 else
334                         printf " --> $f: doesn't exist (or isn't a file)\n"
335                 fi
336         done
337 }
338
339 ## Emacs
340 ## Local Variables:
341 ## tab-width: 8
342 ## indent-tabs-mode: t
343 ## sh-basic-offset: 8
344 ## End: