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