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