Remove remnants of WS_QT_BIN_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 # SPDX-License-Identifier: GPL-2.0-or-later
13 #
14
15 # an existing capture file
16 USE_COLOR=1
17 RUN_SUITE=""
18 PRINT_USAGE=0
19
20 # Ensure cygwin bin dir is on the path if running under it
21 if [[ $OSTYPE == "cygwin" ]]; then
22         PATH="$PATH:/usr/bin"
23 fi
24
25 while getopts "chs:" OPTION ; do
26         case $OPTION in
27                 c) USE_COLOR=0 ;;
28                 h) PRINT_USAGE=1 ;;
29                 s) RUN_SUITE="$OPTARG" ;;
30                 *) echo "Unknown option: " $OPTION $OPTARG
31         esac
32 done
33
34 shift $(( $OPTIND - 1 ))
35
36 if [ $PRINT_USAGE -ne 0 ] ; then
37         THIS=`basename $0`
38         cat <<FIN
39 Usage: $THIS [-c] [-h] [-s <suite>]
40   -c: Disable color output
41   -h: Print this message and exit
42   -s: Run a suite.  Must be one of:
43       all
44       capture
45       clopts
46       decryption
47       fileformats
48       io
49       nameres
50       prerequisites
51       unittests
52       wslua
53       dissection
54 FIN
55         exit 0
56 fi
57
58 MYDIR=$(dirname $0)
59 if [ -d run ]; then
60         if [ -e run/tshark -o -e run/dumpcap -o -e run/rawshark ]; then
61                 WS_BIN_PATH=${WS_BIN_PATH:-$(cd run && pwd)}
62         fi
63 fi
64 source $MYDIR/test-backend.sh
65 source $MYDIR/config.sh
66
67 # needed by some tests
68 TEST_OUTDIR="$(mktemp -d 2>/dev/null || mktemp -d ${TMPDIR:-/tmp}/wstest.XXXXXXXXXX)"
69 if [ $? -ne 0 ] || [ ! -d "$TEST_OUTDIR" ] || ! cd "$TEST_OUTDIR"; then
70         # Error out if TEST_OUTDIR cannot be created
71         echo "Failed to create directory '$TEST_OUTDIR'"
72         exit 1
73 fi
74 TEST_OUTDIR_CLEAN=${TEST_OUTDIR_CLEAN:-1}
75
76 # Configuration paths
77 HOME_ENV="HOME"
78 HOME_PATH="$TEST_OUTDIR/home"
79 CONF_PATH="$HOME_PATH/.wireshark"
80
81 if [ "$WS_SYSTEM" == "Windows" ] ; then
82         HOME_ENV="APPDATA"
83         HOME_PATH="`cygpath -w $HOME_PATH`"
84         CONF_PATH="$HOME_PATH/Wireshark"
85         CAPTURE_DIR="`cygpath -w $CAPTURE_DIR`"
86         TESTS_DIR="`cygpath -w $TESTS_DIR`"
87 fi
88
89 mkdir -p $CONF_PATH
90
91 source $TESTS_DIR/suite-clopts.sh
92 source $TESTS_DIR/suite-io.sh
93 source $TESTS_DIR/suite-capture.sh
94 source $TESTS_DIR/suite-unittests.sh
95 source $TESTS_DIR/suite-fileformats.sh
96 source $TESTS_DIR/suite-decryption.sh
97 source $TESTS_DIR/suite-nameres.sh
98 source $TESTS_DIR/suite-wslua.sh
99 source $TESTS_DIR/suite-mergecap.sh
100 source $TESTS_DIR/suite-text2pcap.sh
101 source $TESTS_DIR/suite-dissection.sh
102
103 test_cleanup() {
104         if [ $TEST_OUTDIR_CLEAN = 1 ]; then
105                 # display contents of test outputs, ignore directory:
106                 # home (decryption suite)
107                 grep -r . --exclude-dir=home .
108                 rm -rf "$TEST_OUTDIR"
109         elif ! rmdir "$TEST_OUTDIR" 2>/dev/null; then
110                 # if directory is non-empty, print directory
111                 echo "Test results are available in $TEST_OUTDIR"
112         fi
113 }
114 trap test_cleanup EXIT
115
116 #check prerequisites
117 test_step_prerequisites() {
118
119         NOTFOUND=0
120         for i in "$WIRESHARK" "$TSHARK" "$CAPINFOS" "$MERGECAP" "$DUMPCAP" ; do
121                 if [ ! -x $i ]; then
122                         echo "Couldn't find $i"
123                         NOTFOUND=1
124                 fi
125         done
126         if [ $NOTFOUND -eq 1 ]; then
127                 test_step_failed "Tool not found"
128                 exit 1
129         else
130                 test_step_ok
131         fi
132 }
133
134 # Dump version information
135 test_step_tshark_version() {
136         test_remark_add "Printing TShark version"
137         $TESTS_DIR/run_and_catch_crashes $TSHARK -v
138         RETURNVALUE=$?
139         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
140                 test_step_failed "Failed to print version information"
141                 return
142         fi
143         test_step_ok
144 }
145
146
147 prerequisites_suite() {
148         test_step_add "Prerequisites settings" test_step_prerequisites
149         test_step_add "Version information" test_step_tshark_version
150 }
151
152 test_suite() {
153         test_suite_add "Prerequisites" prerequisites_suite
154         test_suite_add "Command line options" clopt_suite
155         test_suite_add "File I/O" io_suite
156         test_suite_add "Capture" capture_suite
157         test_suite_add "Unit tests" unittests_suite
158         test_suite_add "Decryption" decryption_suite
159         test_suite_add "Name Resolution" name_resolution_suite
160         test_suite_add "Lua API" wslua_suite
161         test_suite_add "Mergecap" mergecap_suite
162         test_suite_add "File formats" fileformats_suite
163         test_suite_add "Text2pcap" text2pcap_suite
164         test_suite_add "Dissection" dissection_suite
165 }
166
167
168 #test_set_output OFF # doesn't work
169 #test_set_output DOTTED
170 test_set_output VERBOSE
171
172
173 #test_suite_run "TShark command line options" clopt_suite
174 #test_suite_run "TShark capture" capture_suite
175
176
177 # all
178 #test_suite_run "All" test_suite
179 #test_suite_show "All" test_suite
180
181 if [ -n "$RUN_SUITE" ] ; then
182         case $RUN_SUITE in
183                 "all")
184                         test_suite_run "All" test_suite
185                         exit $? ;;
186                 "capture")
187                         test_suite_run "Capture" capture_suite
188                         exit $? ;;
189                 "clopts")
190                         test_suite_run "Command line options" clopt_suite
191                         exit $? ;;
192                 "decryption")
193                         test_suite_run "Decryption" decryption_suite
194                         exit $? ;;
195                 "fileformats")
196                         test_suite_run "File formats" fileformats_suite
197                         exit $? ;;
198                 "io")
199                         test_suite_run "File I/O" io_suite
200                         exit $? ;;
201                 "nameres")
202                         test_suite_run "Name Resolution" name_resolution_suite
203                         exit $? ;;
204                 "prerequisites")
205                         test_suite_run "Prerequisites" prerequisites_suite
206                         exit $? ;;
207                 "unittests")
208                         test_suite_run "Unit tests" unittests_suite
209                         exit $? ;;
210                 "wslua")
211                         test_suite_run "Lua API" wslua_suite
212                         exit $? ;;
213                 "text2pcap")
214                         test_suite_run "Text2pcap" text2pcap_suite
215                         exit $? ;;
216                 "dissection")
217                         test_suite_run "Dissection" dissection_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         if [ $MENU_LEVEL -gt 0 ]; then
236                 echo "T or Enter:  Run suite"
237         else
238         echo "1-$TEST_STEPS : Select suite"
239         fi
240
241         # DBG
242         #echo "Menu level: $MENU_LEVEL"
243         #echo "Menu Title: ${menu_title[MENU_LEVEL]}"
244         #echo "Menu Function: ${menu_function[MENU_LEVEL]}"
245         #echo "Test title size: ${#test_title[@]}"
246         # END DBG
247
248         if [[ ! $MENU_LEVEL -eq 0 ]]; then
249                 echo "U    : Up"
250         fi
251         echo "Q    : Quit"
252         echo ""
253         read key
254         newl=$'\x0d'
255         echo "$newl----------------------------------------------------------------------"
256
257         TEST_STEPS[0]=0                 # number of steps of a specific nesting level
258
259         #echo $key
260         case "$key" in
261                 "Q" | "q")
262                 exit 0
263         ;;
264                 "T" | "t" | "")
265 LIMIT_RUNS=1
266 for ((a_runs=1; a_runs <= LIMIT_RUNS ; a_runs++))  # Double parentheses, and "LIMIT" with no "$".
267 do
268                 test_suite_run "${menu_title[MENU_LEVEL]}" "${menu_function[MENU_LEVEL]}"
269 done
270                 echo "----------------------------------------------------------------------"
271         ;;
272                 "U" | "u")
273                 if [[ ! $MENU_LEVEL -eq 0 ]]; then
274                         let "MENU_LEVEL -= 1"
275                         #echo "----------------------------------------------------------------------"
276                 fi
277         ;;
278                 ## Now we're only interested in digits when the menu level is at the top (0)
279                 [0-9]*)
280                 if [ $MENU_LEVEL -eq 0 ]; then
281                         if [ $key -le ${#test_title[@]} ]; then
282                                 let "MENU_LEVEL += 1"
283                                 menu_title[MENU_LEVEL]=${test_title[$key]}
284                                 menu_function[MENU_LEVEL]=${test_function[$key]}
285                         fi
286                 fi
287         ;;
288         esac
289 done
290
291 # Editor modelines
292 #
293 # Local Variables:
294 # sh-basic-offset: 8
295 # tab-width: 8
296 # indent-tabs-mode: t
297 # End:
298 #
299 # ex: set shiftwidth=8 tabstop=8 noexpandtab:
300 # :indentSize=8:tabSize=8:noTabs=false: