minor cleanup
[obnox/wireshark/wip.git] / test / suite-clopts.sh
1 #!/bin/bash
2 #
3 # Test the command line options of the Ethereal tools
4 #
5 # $Id$
6 #
7 # Ethereal - Network traffic analyzer
8 # By Gerald Combs <gerald@ethereal.com>
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
32 # generic: check against a specific exit status with a single char option
33 # $1 command: tethereal
34 # $2 option: a
35 # $3 expected exit status: 0
36 test_single_char_options()
37 {
38         #echo "command: "$1" opt1: "$2" opt2: "$3" opt3: "$4" opt4: "$5" opt5: "$6
39         $1 -$2  > ./testout.txt 2>&1
40         RETURNVALUE=$?
41         if [ ! $RETURNVALUE -eq $3 ]; then
42                 test_step_failed "exit status: $RETURNVALUE"
43         else
44                 test_step_ok
45         fi
46         rm ./testout.txt
47 }
48
49
50 # check exit status when reading an existing file
51 clopts_step_existing_file() {
52         $TETHEREAL -r $CAPFILE > ./testout.txt 2>&1
53         RETURNVALUE=$?
54         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
55                 test_step_failed "exit status: $RETURNVALUE"
56         else
57                 test_step_ok
58         fi
59         rm ./testout.txt
60 }
61
62
63 # check exit status when reading a none existing file
64 clopts_step_nonexisting_file() {
65         $TETHEREAL -r ThisFileDontExist.pcap  > ./testout.txt 2>&1
66         RETURNVALUE=$?
67         if [ ! $RETURNVALUE -eq $EXIT_ERROR ]; then
68                 test_step_failed "exit status: $RETURNVALUE"
69         else
70                 test_step_ok
71         fi
72         rm  ./testout.txt
73 }
74
75
76 # check exit status of all single char option being invalid
77 clopts_suite_tethereal_invalid_chars() {
78         for index in A B C E F H I J K M N O P 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
79         do
80           test_step_add "Invalid Tethereal parameter -$index, exit status must be $EXIT_COMMAND_LINE" "test_single_char_options $TETHEREAL $index $EXIT_COMMAND_LINE"
81         done
82 }
83
84
85 # check exit status of all single char option being valid
86 clopts_suite_valid_chars() {
87         for index in D G L h v
88         do
89           test_step_add "Valid Tethereal parameter -$index, exit status must be $EXIT_OK" "test_single_char_options $TETHEREAL $index $EXIT_OK"
90         done
91 }
92
93
94 # S V l n p q x
95
96 # check exit status and grep output string of an invalid capture filter
97 clopts_step_invalid_capfilter() {
98         $TETHEREAL -f 'jkghg' -w './testout.pcap' > ./testout.txt 2>&1
99         RETURNVALUE=$?
100         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
101                 test_step_failed "exit status: $RETURNVALUE"
102         else
103                 grep -i 'Invalid capture filter: "jkghg"' ./testout.txt > /dev/null
104                 if [ $? -eq 0 ]; then
105                         test_step_ok
106                 else
107                         cat ./testout.txt
108                         test_step_failed "Infos"
109                 fi
110         fi
111 }
112
113 # check exit status and grep output string of an invalid interface
114 clopts_step_invalid_interface() {
115         $TETHEREAL -i invalid_interface -w './testout.pcap' > ./testout.txt 2>&1
116         RETURNVALUE=$?
117         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
118                 test_step_failed "exit status: $RETURNVALUE"
119         else
120                 grep -i 'The capture session could not be initiated!' ./testout.txt > /dev/null
121                 if [ $? -eq 0 ]; then
122                         test_step_ok
123                 else
124                         cat ./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 index
131 # (valid interface indexes start with 1)
132 clopts_step_invalid_interface_index() {
133         $TETHEREAL -i 0 -w './testout.pcap' > ./testout.txt 2>&1
134         RETURNVALUE=$?
135         if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ]; then
136                 test_step_failed "exit status: $RETURNVALUE"
137         else
138                 grep -i 'there is no interface with that adapter index' ./testout.txt > /dev/null
139                 if [ $? -eq 0 ]; then
140                         test_step_ok
141                 else
142                         cat ./testout.txt
143                         test_step_failed "Infos"
144                 fi
145         fi
146 }
147
148 # check exit status and grep output string of an invalid capture filter
149 # XXX - how to efficiently test the *invalid* flags?
150 clopts_step_valid_name_resolving() {
151         $TETHEREAL -N mntC -a duration:1 > ./testout.txt 2>&1
152         RETURNVALUE=$?
153         if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
154                 test_step_failed "exit status: $RETURNVALUE"
155         else
156                 test_step_ok
157         fi
158 }
159
160 # check exit status of some basic functions
161 clopts_suite_basic() {
162         test_step_add "Exit status for existing file: \""$CAPFILE"\" must be 0" clopts_step_existing_file
163         test_step_add "Exit status for none existing files must be 2" clopts_step_nonexisting_file
164 }
165
166
167 clopts_post_step() {
168         rm -f ./testout.txt
169 }
170
171 clopt_suite() {
172         test_step_set_post clopts_post_step
173         test_suite_add "Basic tests" clopts_suite_basic
174         test_suite_add "Invalid Tethereal single char options" clopts_suite_tethereal_invalid_chars
175         test_suite_add "Valid Tethereal single char options" clopts_suite_valid_chars
176         test_step_add  "Invalid capture filter -f" clopts_step_invalid_capfilter
177         test_step_add  "Invalid capture interface -i" clopts_step_invalid_interface
178         test_step_add  "Invalid capture interface index 0" clopts_step_invalid_interface_index
179         test_step_add  "Valid name resolution options -N (1s)" clopts_step_valid_name_resolving
180         #test_remark_add "Undocumented command line option: G"
181         #test_remark_add "Options currently unchecked: S, V, l, n, p, q and x"
182 }
183