Fix Cygwin path
[metze/wireshark/wip.git] / test / test.sh
1 #!/bin/bash
2 (set -o igncr) 2>/dev/null && set -o igncr; # comment is needed
3 #                                           # hack for cygwin bash
4 #                                           #  no-op for other
5 #
6 # Test various command line testable aspects of the Wireshark tools
7 #
8 # Wireshark - Network traffic analyzer
9 # By Gerald Combs <gerald@wireshark.org>
10 # Copyright 2005 Ulf Lamping
11 #
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #
26
27 # an existing capture file
28 USE_COLOR=1
29 RUN_SUITE=""
30 PRINT_USAGE=0
31
32 # Ensure cygwin bin dir is on the path if running under it
33 if [[ $OSTYPE == "cygwin" ]]; then
34         PATH="$PATH:/usr/bin"
35 fi
36
37 while getopts "chs:" OPTION ; do
38         case $OPTION in
39                 c) USE_COLOR=0 ;;
40                 h) PRINT_USAGE=1 ;;
41                 s) RUN_SUITE="$OPTARG" ;;
42                 *) echo "Unknown option: " $OPTION $OPTARG
43         esac
44 done
45
46 shift $(( $OPTIND - 1 ))
47
48 if [ $PRINT_USAGE -ne 0 ] ; then
49         THIS=`basename $0`
50         cat <<FIN
51 Usage: $THIS [-c] [-h] [-s <suite>]
52   -c: Disable color output
53   -h: Print this message and exit
54   -s: Run a suite.  Must be one of:
55       all
56       capture
57       clopts
58       decryption
59       fileformats
60       io
61       nameres
62       prerequisites
63       unittests
64       wslua
65 FIN
66         exit 0
67 fi
68
69 MYDIR=$(dirname $0)
70 if [ -d run ]; then
71         if [ -e run/tshark -o -e run/dumpcap -o -e run/rawshark ]; then
72                 WS_BIN_PATH=${WS_BIN_PATH:-$(cd run && pwd)}
73                 WS_QT_BIN_PATH=${WS_QT_BIN_PATH:-$WS_BIN_PATH}
74         fi
75 fi
76 source $MYDIR/test-backend.sh
77 source $MYDIR/config.sh
78
79 # needed by some tests
80 TEST_OUTDIR=$(mktemp -d)
81 TEST_OUTDIR_CLEAN=${TEST_OUTDIR_CLEAN:-1}
82 if [ -z "$TEST_OUTDIR" ] || ! cd "$TEST_OUTDIR"; then
83         # If for any reason the temporary tests output directory cannot be created...
84         TEST_OUTDIR=.
85         TEST_OUTDIR_CLEAN=0
86 fi
87
88 # Configuration paths
89 HOME_ENV="HOME"
90 HOME_PATH="$TEST_OUTDIR/home"
91 CONF_PATH="$HOME_PATH/.wireshark"
92
93 if [ "$WS_SYSTEM" == "Windows" ] ; then
94         HOME_ENV="APPDATA"
95         HOME_PATH="`cygpath -w $HOME_PATH`"
96         CONF_PATH="$HOME_PATH/Wireshark"
97         CAPTURE_DIR="`cygpath -w $CAPTURE_DIR`"
98         TESTS_DIR="`cygpath -w $TESTS_DIR`"
99 fi
100
101 mkdir -p $CONF_PATH
102
103 source $TESTS_DIR/suite-clopts.sh
104 source $TESTS_DIR/suite-io.sh
105 source $TESTS_DIR/suite-capture.sh
106 source $TESTS_DIR/suite-unittests.sh
107 source $TESTS_DIR/suite-fileformats.sh
108 source $TESTS_DIR/suite-decryption.sh
109 source $TESTS_DIR/suite-nameres.sh
110 source $TESTS_DIR/suite-wslua.sh
111
112 test_cleanup() {
113         if [ $TEST_OUTDIR_CLEAN = 1 ]; then
114                 # display contents of test outputs, ignore directory:
115                 # home (decryption suite)
116                 grep -r . --exclude-dir=home .
117                 rm -rf "$TEST_OUTDIR"
118         elif ! rmdir "$TEST_OUTDIR" 2>/dev/null; then
119                 # if directory is non-empty, print directory
120                 echo "Test results are available in $TEST_OUTDIR"
121         fi
122 }
123 trap test_cleanup EXIT
124
125 #check prerequisites
126 test_step_prerequisites() {
127
128         NOTFOUND=0
129         for i in "$WIRESHARK" "$WIRESHARK_GTK" "$TSHARK" "$CAPINFOS" "$DUMPCAP" ; do
130                 if [ ! -x $i ]; then
131                         echo "Couldn't find $i"
132                         NOTFOUND=1
133                 fi
134         done
135         if [ $NOTFOUND -eq 1 ]; then
136                 test_step_failed "Tool not found"
137                 exit 1
138         else
139                 test_step_ok
140         fi
141 }
142
143 # Dump version information
144 test_step_tshark_version() {
145         test_remark_add "Printing TShark version"
146         $TSHARK -v
147         RETURNVALUE=$?
148         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
149                 test_step_failed "Failed to print version information"
150                 return
151         fi
152         test_step_ok
153 }
154
155
156 prerequisites_suite() {
157         test_step_add "Prerequisites settings" test_step_prerequisites
158         test_step_add "Version information" test_step_tshark_version
159 }
160
161 test_suite() {
162         test_suite_add "Prerequisites" prerequisites_suite
163         test_suite_add "Command line options" clopt_suite
164         test_suite_add "File I/O" io_suite
165         test_suite_add "Capture" capture_suite
166         test_suite_add "Unit tests" unittests_suite
167         test_suite_add "File formats" fileformats_suite
168         test_suite_add "Decryption" decryption_suite
169         test_suite_add "Name Resolution" name_resolution_suite
170         test_suite_add "Lua API" wslua_suite
171 }
172
173
174 #test_set_output OFF # doesn't work
175 #test_set_output DOTTED
176 test_set_output VERBOSE
177
178
179 #test_suite_run "TShark command line options" clopt_suite
180 #test_suite_run "TShark capture" capture_suite
181
182
183 # all
184 #test_suite_run "All" test_suite
185 #test_suite_show "All" test_suite
186
187 if [ -n "$RUN_SUITE" ] ; then
188         case $RUN_SUITE in
189                 "all")
190                         test_suite_run "All" test_suite
191                         exit $? ;;
192                 "capture")
193                         test_suite_run "Capture" capture_suite
194                         exit $? ;;
195                 "clopts")
196                         test_suite_run "Command line options" clopt_suite
197                         exit $? ;;
198                 "decryption")
199                         test_suite_run "Decryption" decryption_suite
200                         exit $? ;;
201                 "fileformats")
202                         test_suite_run "File formats" fileformats_suite
203                         exit $? ;;
204                 "io")
205                         test_suite_run "File I/O" io_suite
206                         exit $? ;;
207                 "nameres")
208                         test_suite_run "Name Resolution" name_resolution_suite
209                         exit $? ;;
210                 "prerequisites")
211                         test_suite_run "Prerequisites" prerequisites_suite
212                         exit $? ;;
213                 "unittests")
214                         test_suite_run "Unit tests" unittests_suite
215                         exit $? ;;
216                 "wslua")
217                         test_suite_run "Lua API" wslua_suite
218                         exit $? ;;
219         esac
220 fi
221
222 MENU_LEVEL=0
223
224 menu_title[0]="All"
225 menu_function[0]=test_suite
226
227 echo "----------------------------------------------------------------------"
228
229 for ((a=0; a <= 100000000000 ; a++))
230 do
231         TEST_STEPS[0]=0                 # number of steps of a specific nesting level
232
233         #echo $current_title $current_function
234         test_suite_show "${menu_title[MENU_LEVEL]}" "${menu_function[MENU_LEVEL]}"
235         echo "1-$TEST_STEPS  : Select item"
236         echo "Enter: Test All"
237         if [[ ! $MENU_LEVEL -eq 0 ]]; then
238                 echo "U    : Up"
239         fi
240         echo "Q    : Quit"
241         echo ""
242         read -n1 key
243         newl=$'\x0d'
244         echo "$newl----------------------------------------------------------------------"
245
246         TEST_STEPS[0]=0                 # number of steps of a specific nesting level
247
248         #echo $key
249         case "$key" in
250                 "Q" | "q")
251                 exit 0
252         ;;
253                 "T" | "t" | "")
254 LIMIT_RUNS=1
255 for ((a_runs=1; a_runs <= LIMIT_RUNS ; a_runs++))  # Double parentheses, and "LIMIT" with no "$".
256 do
257                 test_suite_run "${menu_title[MENU_LEVEL]}" "${menu_function[MENU_LEVEL]}"
258 done
259                 echo "----------------------------------------------------------------------"
260         ;;
261                 "U" | "u")
262                 if [[ ! $MENU_LEVEL -eq 0 ]]; then
263                         let "MENU_LEVEL -= 1"
264                         #echo "----------------------------------------------------------------------"
265                 fi
266         ;;
267                 "1")
268                 let "MENU_LEVEL += 1"
269                 menu_title[MENU_LEVEL]=${test_title[1]}
270                 menu_function[MENU_LEVEL]=${test_function[1]}
271         ;;
272                 "2")
273                 let "MENU_LEVEL += 1"
274                 menu_title[MENU_LEVEL]=${test_title[2]}
275                 menu_function[MENU_LEVEL]=${test_function[2]}
276         ;;
277                 "3")
278                 let "MENU_LEVEL += 1"
279                 menu_title[MENU_LEVEL]=${test_title[3]}
280                 menu_function[MENU_LEVEL]=${test_function[3]}
281         ;;
282                 "4")
283                 let "MENU_LEVEL += 1"
284                 menu_title[MENU_LEVEL]=${test_title[4]}
285                 menu_function[MENU_LEVEL]=${test_function[4]}
286         ;;
287                 "5")
288                 let "MENU_LEVEL += 1"
289                 menu_title[MENU_LEVEL]=${test_title[5]}
290                 menu_function[MENU_LEVEL]=${test_function[5]}
291         ;;
292                 "6")
293                 let "MENU_LEVEL += 1"
294                 menu_title[MENU_LEVEL]=${test_title[6]}
295                 menu_function[MENU_LEVEL]=${test_function[6]}
296         ;;
297                 "7")
298                 let "MENU_LEVEL += 1"
299                 menu_title[MENU_LEVEL]=${test_title[7]}
300                 menu_function[MENU_LEVEL]=${test_function[7]}
301         ;;
302                 "8")
303                 let "MENU_LEVEL += 1"
304                 menu_title[MENU_LEVEL]=${test_title[8]}
305                 menu_function[MENU_LEVEL]=${test_function[8]}
306         ;;
307                 "9")
308                 let "MENU_LEVEL += 1"
309                 menu_title[MENU_LEVEL]=${test_title[9]}
310                 menu_function[MENU_LEVEL]=${test_function[9]}
311         ;;
312
313         esac
314 done
315
316 # Editor modelines
317 #
318 # Local Variables:
319 # sh-basic-offset: 8
320 # tab-width: 8
321 # indent-tabs-mode: t
322 # End:
323 #
324 # ex: set shiftwidth=8 tabstop=8 noexpandtab:
325 # :indentSize=8:tabSize=8:noTabs=false: