Ethereal->Wireshark
[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 color_reset="tput sgr0"
40 color_green='\E[32;40m'
41 color_red='\E[31;40m'
42 color_blue='\E[36;40m'
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
56 TEST_STEP_PRE_CB=
57 TEST_STEP_POST_CB=
58
59 # level number of this test item (suite or step)
60 test_level() {
61         LIMIT=100
62         
63         for ((a=0; a <= LIMIT ; a++)) 
64         do
65                 if [ ! $a -eq 0 ]; then
66                         echo -n "."
67                 fi
68                 echo -n "${TEST_STEPS[a]}"
69                 if [ $a -eq $TEST_NESTING_LEVEL ]; then
70                         #echo "end"
71                         return
72                 fi
73         done                           
74 }
75
76 # set output format
77 # $1 - "OUT", "DOTTED", "VERBOSE"
78 test_set_output() {
79         TEST_OUTPUT=$1
80 }
81
82 # run a test suite
83 # $1 name
84 # $2 command
85 test_suite_run() {
86         # header
87         echo -n -e $color_blue
88         echo ""
89         echo "### $1 ###"
90         $color_reset
91
92         TEST_RUN="ON"
93
94         # run the actual test suite
95         $2
96         
97         # results
98         if [ $TEST_RUN = "ON" ]; then
99                 echo ""
100                 if [ $TEST_FAILED -eq 0 ]; then
101                         echo -n -e $color_green
102                 else
103                         echo -n -e $color_red
104                 fi
105                 echo "### Test suite results ###"
106                 echo -n -e $color_green
107                 echo "Ok    : $TEST_OK"
108                 echo -n -e $color_red
109                 echo "Failed: $TEST_FAILED"
110                 $color_reset
111         fi
112
113         # exit status
114         #if [ $TEST_FAILED -eq 0 ]; then
115         #       exit 0
116         #else
117         #       exit 1
118         #fi
119
120         TEST_RUN="OFF"
121 }
122
123
124 # show a test suite
125 # $1 name
126 # $2 command
127 test_suite_show() {
128
129         # header
130         echo -n -e $color_blue
131         echo ""
132         echo "### Test suite: $1 ###"
133         echo ""
134         echo "Subitems:"
135         echo "---------"
136         $color_reset
137
138         # show this test suite subitems
139         $2
140         
141         echo ""
142 }
143
144
145 # add a test suite
146 # $1 name
147 # $2 function
148 test_suite_add() {
149         # increase step counter of this nesting level
150         let "TEST_STEPS[$TEST_NESTING_LEVEL] += 1"
151         
152         if [ $TEST_RUN = "ON" ]; then
153                 echo ""
154         fi
155         
156         # title output if we'll list the subitems 
157         if [[ $TEST_RUN = "ON" ]]; then
158                 echo -n -e $color_blue
159                 test_level
160                 echo "  Suite: $1"
161                 $color_reset
162         fi
163
164         if [[ $TEST_NESTING_LEVEL -eq 0 ]]; then
165                 pos=${TEST_STEPS[$TEST_NESTING_LEVEL]}
166                 #echo "pos " $pos
167                 test_title[$pos]=$1
168                 test_function[$pos]=$2
169                 #echo ${test_title[1]}
170
171         fi
172
173         # reset test step counter back to zero
174         TEST_STEP=0
175         
176         # call the suites function
177         let "TEST_NESTING_LEVEL += 1"
178         TEST_STEPS[$TEST_NESTING_LEVEL]=0
179         $2
180         let "TEST_NESTING_LEVEL -= 1"
181
182         # title output (with subitem counter) if we don't listed the subitems
183         if [[ ! $TEST_RUN = "ON" && $TEST_NESTING_LEVEL -eq 0 ]]; then
184                 echo -n -e $color_blue
185                 test_level
186                 echo "  Suite: $1 (${TEST_STEPS[TEST_NESTING_LEVEL+1]} subitems)"
187                 $color_reset
188         fi      
189
190 }
191
192
193 # add a test step
194 # $1 name
195 # $2 function
196 test_step_add() {
197
198         let "TEST_STEPS[$TEST_NESTING_LEVEL] += 1"
199
200         if [[ ($TEST_RUN = "ON" && $TEST_OUTPUT = "DOTTED") && $TEST_NESTING_LEVEL -eq 0 ]]; then
201                 echo ""
202         fi
203         
204         if [[ ( $TEST_RUN = "ON" && $TEST_OUTPUT = "VERBOSE" ) || $TEST_NESTING_LEVEL -eq 0 ]]; then
205                 echo -n -e $color_blue
206                 test_level
207                 echo -n " Step:" $1
208                 $color_reset
209         fi
210
211         if [ $TEST_RUN = "ON" ]; then           
212                 # preprecessing step
213                 $TEST_STEP_PRE_CB
214                 #echo "command: "$2" opt1: "$3" opt2: "$4" opt3: "$5" opt4: "$6" opt5: "$7
215                 TEST_STEP_NAME=$1
216                 # actually run the command to test now
217                 $2 
218                 #"$3" "$4" "$5" "$6" "$7"
219                 # post precessing step
220                 $TEST_STEP_POST_CB
221         else
222                 if [[ $TEST_NESTING_LEVEL -eq 0 ]]; then
223                         echo ""
224                 fi
225         fi
226 }
227
228
229 # set the preprocessing function
230 # $1 remark
231 test_step_set_pre() {
232         TEST_STEP_PRE_CB=$1
233 }
234
235 # set the post processing function
236 # $1 remark
237 test_step_set_post() {
238         TEST_STEP_POST_CB=$1
239 }
240
241 # add a test remark
242 # $1 remark
243 test_remark_add() {
244
245         # test is running or toplevel item? -> show remark
246         if [[ $TEST_RUN = "ON" || $TEST_NESTING_LEVEL -eq 0 ]]; then
247                 # test is running and output is dotted -> newline first
248                 if [[ $TEST_RUN = "ON" && $TEST_OUTPUT = "DOTTED" ]]; then
249                         echo ""
250                 fi
251                 
252                 # remark
253                 echo -n -e $color_blue
254                 echo "  Remark: $1"
255                 $color_reset
256         fi
257 }
258
259
260 # the test step succeeded
261 test_step_ok() {
262         # count appearance
263         let "TEST_OK += 1"
264         
265         # output in green
266         echo -n -e $color_green
267         
268         if [ $TEST_OUTPUT = "VERBOSE" ]; then
269                 echo " Ok"
270         else
271                 echo -n .
272         fi
273         
274         $color_reset
275 }
276
277 # the test step failed
278 # $1 output text
279 test_step_failed() {
280         let "TEST_FAILED += 1"
281         
282         # output in red
283         echo -n -e "$color_red"
284         
285         echo ""
286         echo "\"$TEST_STEP_NAME\" Failed!"
287         echo $1
288         
289         $color_reset
290         
291         exit 1
292         
293         # XXX - add a mechanism to optionally stop here
294 }
295