From Tom Brezinski:
[obnox/wireshark/wip.git] / test / suite-clopts.sh
1 #!/bin/bash
2 #
3 # Test the command line options of the Wireshark tools
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 # common exit status values
27 EXIT_OK=0
28 EXIT_COMMAND_LINE=1
29 EXIT_ERROR=2
30
31 # generic: check against a specific exit status with a single char option
32 # $1 command: tshark
33 # $2 option: a
34 # $3 expected exit status: 0
35 test_single_char_options()
36 {
37         #echo "command: "$1" opt1: "$2" opt2: "$3" opt3: "$4" opt4: "$5" opt5: "$6
38         $1 -$2  > ./testout.txt 2>&1
39         RETURNVALUE=$?
40         if [ ! $RETURNVALUE -eq $3 ]; then
41                 test_step_failed "exit status: $RETURNVALUE"
42         else
43                 test_step_ok
44         fi
45         rm ./testout.txt
46 }
47
48
49 # check exit status when reading an existing file
50 clopts_step_existing_file() {
51         $TSHARK -r $CAPFILE > ./testout.txt 2>&1
52         RETURNVALUE=$?
53         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
54                 test_step_failed "exit status: $RETURNVALUE"
55         else
56                 test_step_ok
57         fi
58         rm ./testout.txt
59 }
60
61
62 # check exit status when reading a non-existing file
63 clopts_step_nonexisting_file() {
64         $TSHARK -r ThisFileDontExist.pcap  > ./testout.txt 2>&1
65         RETURNVALUE=$?
66         if [ ! $RETURNVALUE -eq $EXIT_ERROR ]; then
67                 test_step_failed "exit status: $RETURNVALUE"
68         else
69                 test_step_ok
70         fi
71         rm  ./testout.txt
72 }
73
74
75 # check exit status of all single char option being invalid
76 clopts_suite_tshark_invalid_chars() {
77         for index in A B C E F H J K M N O Q R T U W X Y Z a b c d e f g i j k m o r s t u w y z
78         do
79           test_step_add "Invalid TShark parameter -$index, exit status must be $EXIT_COMMAND_LINE" "test_single_char_options $TSHARK $index $EXIT_COMMAND_LINE"
80         done
81 }
82
83
84 # check exit status of all single char option being valid
85 clopts_suite_valid_chars() {
86         for index in G h v
87         do
88           test_step_add "Valid TShark parameter -$index, exit status must be $EXIT_OK" "test_single_char_options $TSHARK $index $EXIT_OK"
89         done
90 }
91
92 # special case: interface-specific opts should work under Windows and fail as
93 # a regular user on other systems.
94 clopts_suite_interface_chars() {
95         for index in D L
96         do
97           if [ "$SKIP_CAPTURE" -eq 0 ] ; then
98             test_step_add "Valid TShark parameter -$index, exit status must be $EXIT_OK" "test_single_char_options $TSHARK $index $EXIT_OK"
99           else
100             test_step_add "Invalid permissions for TShark parameter -$index, exit status must be $EXIT_ERROR" "test_single_char_options $TSHARK $index $EXIT_ERROR"
101           fi
102         done
103 }
104
105 # S V l n p q x
106
107 # check exit status and grep output string of an invalid capture filter
108 clopts_step_invalid_capfilter() {
109         if [ "$WS_SYSTEM" != "Windows" ] ; then
110                 test_step_skipped
111                 return
112         fi
113
114         $TSHARK -f 'jkghg' -w './testout.pcap' > ./testout.txt 2>&1
115         RETURNVALUE=$?
116         if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ]; then
117                 test_step_failed "exit status: $RETURNVALUE"
118         else
119                 grep -i 'Invalid capture filter "jkghg" for interface' ./testout.txt > /dev/null
120                 if [ $? -eq 0 ]; then
121                         test_step_output_print ./testout.txt
122                         test_step_ok
123                 else
124                         test_step_output_print ./testout.txt
125                         test_step_failed "Infos"
126                 fi
127         fi
128 }
129
130 # check exit status and grep output string of an invalid interface
131 clopts_step_invalid_interface() {
132         $TSHARK -i invalid_interface -w './testout.pcap' > ./testout.txt 2>&1
133         RETURNVALUE=$?
134         if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ]; then
135                 test_step_failed "exit status: $RETURNVALUE"
136         else
137                 grep -i 'The capture session could not be initiated' ./testout.txt > /dev/null
138                 if [ $? -eq 0 ]; then
139                         test_step_output_print ./testout.txt
140                         test_step_ok
141                 else
142                         test_step_output_print ./testout.txt
143                         test_step_failed "Infos"
144                 fi
145         fi
146 }
147
148 # check exit status and grep output string of an invalid interface index
149 # (valid interface indexes start with 1)
150 clopts_step_invalid_interface_index() {
151         $TSHARK -i 0 -w './testout.pcap' > ./testout.txt 2>&1
152         RETURNVALUE=$?
153         if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ]; then
154                 test_step_failed "exit status: $RETURNVALUE"
155         else
156                 grep -i 'there is no interface with that adapter index' ./testout.txt > /dev/null
157                 if [ $? -eq 0 ]; then
158                         test_step_ok
159                 else
160                         test_step_output_print ./testout.txt
161                         test_step_failed "Infos"
162                 fi
163         fi
164 }
165
166 # check exit status and grep output string of an invalid capture filter
167 # XXX - how to efficiently test the *invalid* flags?
168 clopts_step_valid_name_resolving() {
169         if [ "$WS_SYSTEM" != "Windows" ] ; then
170                 test_step_skipped
171                 return
172         fi
173
174         $TSHARK -N mntC -a duration:1 > ./testout.txt 2>&1
175         RETURNVALUE=$?
176         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
177                 test_step_failed "exit status: $RETURNVALUE"
178         else
179                 test_step_ok
180         fi
181 }
182
183 # check exit status of some basic functions
184 clopts_suite_basic() {
185         test_step_add "Exit status for existing file: \""$CAPFILE"\" must be 0" clopts_step_existing_file
186         test_step_add "Exit status for none existing files must be 2" clopts_step_nonexisting_file
187 }
188
189 clopts_suite_capture_options() {
190         test_step_add  "Invalid capture filter -f" clopts_step_invalid_capfilter
191         test_step_add  "Invalid capture interface -i" clopts_step_invalid_interface
192         test_step_add  "Invalid capture interface index 0" clopts_step_invalid_interface_index
193 }
194
195 clopts_post_step() {
196         rm -f ./testout.txt ./testout2.txt
197 }
198
199 clopt_suite() {
200         test_step_set_post clopts_post_step
201         test_suite_add "Basic tests" clopts_suite_basic
202         test_suite_add "Invalid TShark single char options" clopts_suite_tshark_invalid_chars
203         test_suite_add "Valid TShark single char options" clopts_suite_valid_chars
204         test_suite_add "Interface-specific TShark single char options" clopts_suite_interface_chars
205         test_suite_add "Capture filter/interface options tests" clopts_suite_capture_options
206         test_step_add  "Valid name resolution options -N (1s)" clopts_step_valid_name_resolving
207         #test_remark_add "Undocumented command line option: G"
208         #test_remark_add "Options currently unchecked: S, V, l, n, p, q and x"
209 }
210
211 ## Emacs
212 ## Local Variables:
213 ## tab-width: 8
214 ## indent-tabs-mode: t
215 ## sh-basic-offset: 8
216 ## End: