Move the definition of MAX_NUM_COLOR_CONVERSATION_COLORS.
[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 source $TESTS_DIR/suite-mergecap.sh
112 source $TESTS_DIR/suite-text2pcap.sh
113
114 test_cleanup() {
115         if [ $TEST_OUTDIR_CLEAN = 1 ]; then
116                 # display contents of test outputs, ignore directory:
117                 # home (decryption suite)
118                 grep -r . --exclude-dir=home .
119                 rm -rf "$TEST_OUTDIR"
120         elif ! rmdir "$TEST_OUTDIR" 2>/dev/null; then
121                 # if directory is non-empty, print directory
122                 echo "Test results are available in $TEST_OUTDIR"
123         fi
124 }
125 trap test_cleanup EXIT
126
127 #check prerequisites
128 test_step_prerequisites() {
129
130         NOTFOUND=0
131         for i in "$WIRESHARK" "$WIRESHARK_GTK" "$TSHARK" "$CAPINFOS" "$MERGECAP" "$DUMPCAP" ; do
132                 if [ ! -x $i ]; then
133                         echo "Couldn't find $i"
134                         NOTFOUND=1
135                 fi
136         done
137         if [ $NOTFOUND -eq 1 ]; then
138                 test_step_failed "Tool not found"
139                 exit 1
140         else
141                 test_step_ok
142         fi
143 }
144
145 # Dump version information
146 test_step_tshark_version() {
147         test_remark_add "Printing TShark version"
148         $TESTS_DIR/run_and_catch_crashes $TSHARK -v
149         RETURNVALUE=$?
150         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
151                 test_step_failed "Failed to print version information"
152                 return
153         fi
154         test_step_ok
155 }
156
157
158 prerequisites_suite() {
159         test_step_add "Prerequisites settings" test_step_prerequisites
160         test_step_add "Version information" test_step_tshark_version
161 }
162
163 test_suite() {
164         test_suite_add "Prerequisites" prerequisites_suite
165         test_suite_add "Command line options" clopt_suite
166         test_suite_add "File I/O" io_suite
167         test_suite_add "Capture" capture_suite
168         test_suite_add "Unit tests" unittests_suite
169         test_suite_add "Decryption" decryption_suite
170         test_suite_add "Name Resolution" name_resolution_suite
171         test_suite_add "Lua API" wslua_suite
172         test_suite_add "Mergecap" mergecap_suite
173         test_suite_add "File formats" fileformats_suite
174         test_suite_add "Text2pcap" text2pcap_suite
175 }
176
177
178 #test_set_output OFF # doesn't work
179 #test_set_output DOTTED
180 test_set_output VERBOSE
181
182
183 #test_suite_run "TShark command line options" clopt_suite
184 #test_suite_run "TShark capture" capture_suite
185
186
187 # all
188 #test_suite_run "All" test_suite
189 #test_suite_show "All" test_suite
190
191 if [ -n "$RUN_SUITE" ] ; then
192         case $RUN_SUITE in
193                 "all")
194                         test_suite_run "All" test_suite
195                         exit $? ;;
196                 "capture")
197                         test_suite_run "Capture" capture_suite
198                         exit $? ;;
199                 "clopts")
200                         test_suite_run "Command line options" clopt_suite
201                         exit $? ;;
202                 "decryption")
203                         test_suite_run "Decryption" decryption_suite
204                         exit $? ;;
205                 "fileformats")
206                         test_suite_run "File formats" fileformats_suite
207                         exit $? ;;
208                 "io")
209                         test_suite_run "File I/O" io_suite
210                         exit $? ;;
211                 "nameres")
212                         test_suite_run "Name Resolution" name_resolution_suite
213                         exit $? ;;
214                 "prerequisites")
215                         test_suite_run "Prerequisites" prerequisites_suite
216                         exit $? ;;
217                 "unittests")
218                         test_suite_run "Unit tests" unittests_suite
219                         exit $? ;;
220                 "wslua")
221                         test_suite_run "Lua API" wslua_suite
222                         exit $? ;;
223                 "text2pcap")
224                         test_suite_run "Text2pcap" text2pcap_suite
225                         exit $? ;;
226         esac
227 fi
228
229 MENU_LEVEL=0
230
231 menu_title[0]="All"
232 menu_function[0]=test_suite
233
234 echo "----------------------------------------------------------------------"
235
236 for ((a=0; a <= 100000000000 ; a++))
237 do
238         TEST_STEPS[0]=0                 # number of steps of a specific nesting level
239
240         #echo $current_title $current_function
241         test_suite_show "${menu_title[MENU_LEVEL]}" "${menu_function[MENU_LEVEL]}"
242         if [ $MENU_LEVEL -gt 0 ]; then
243                 echo "T or Enter:  Run suite"
244         else
245         echo "1-$TEST_STEPS : Select suite"
246         fi
247
248         # DBG
249         #echo "Menu level: $MENU_LEVEL"
250         #echo "Menu Title: ${menu_title[MENU_LEVEL]}"
251         #echo "Menu Function: ${menu_function[MENU_LEVEL]}"
252         #echo "Test title size: ${#test_title[@]}"
253         # END DBG
254
255         if [[ ! $MENU_LEVEL -eq 0 ]]; then
256                 echo "U    : Up"
257         fi
258         echo "Q    : Quit"
259         echo ""
260         read key
261         newl=$'\x0d'
262         echo "$newl----------------------------------------------------------------------"
263
264         TEST_STEPS[0]=0                 # number of steps of a specific nesting level
265
266         #echo $key
267         case "$key" in
268                 "Q" | "q")
269                 exit 0
270         ;;
271                 "T" | "t" | "")
272 LIMIT_RUNS=1
273 for ((a_runs=1; a_runs <= LIMIT_RUNS ; a_runs++))  # Double parentheses, and "LIMIT" with no "$".
274 do
275                 test_suite_run "${menu_title[MENU_LEVEL]}" "${menu_function[MENU_LEVEL]}"
276 done
277                 echo "----------------------------------------------------------------------"
278         ;;
279                 "U" | "u")
280                 if [[ ! $MENU_LEVEL -eq 0 ]]; then
281                         let "MENU_LEVEL -= 1"
282                         #echo "----------------------------------------------------------------------"
283                 fi
284         ;;
285                 ## Now we're only interested in digits when the menu level is at the top (0)
286                 [0-9]*)
287                 if [ $MENU_LEVEL -eq 0 ]; then
288                         if [ $key -le ${#test_title[@]} ]; then
289                                 let "MENU_LEVEL += 1"
290                                 menu_title[MENU_LEVEL]=${test_title[$key]}
291                                 menu_function[MENU_LEVEL]=${test_function[$key]}
292                         fi
293                 fi
294         ;;
295         esac
296 done
297
298 # Editor modelines
299 #
300 # Local Variables:
301 # sh-basic-offset: 8
302 # tab-width: 8
303 # indent-tabs-mode: t
304 # End:
305 #
306 # ex: set shiftwidth=8 tabstop=8 noexpandtab:
307 # :indentSize=8:tabSize=8:noTabs=false: