2005-12-13 23:15:20 +00:00
#!/bin/bash
#
2006-05-22 08:21:22 +00:00
# Test the command line options of the Wireshark tools
2005-12-13 23:15:20 +00:00
#
2006-05-21 05:12:17 +00:00
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
2005-12-13 23:15:20 +00:00
# Copyright 2005 Ulf Lamping
#
2018-03-06 14:31:02 +00:00
# SPDX-License-Identifier: GPL-2.0-or-later
2006-10-09 18:47:42 +00:00
#
2005-12-13 23:15:20 +00:00
# common exit status values
EXIT_OK = 0
EXIT_COMMAND_LINE = 1
EXIT_ERROR = 2
# generic: check against a specific exit status with a single char option
2012-11-21 09:01:39 +00:00
# $1 command: tshark or dumpcap
2005-12-13 23:15:20 +00:00
# $2 option: a
# $3 expected exit status: 0
test_single_char_options( )
{
#echo "command: "$1" opt1: "$2" opt2: "$3" opt3: "$4" opt4: "$5" opt5: "$6
2014-12-24 09:25:32 +00:00
$1 -$2 > ./testout.txt 2>& 1
2005-12-13 23:15:20 +00:00
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $3 ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
test_step_ok
fi
rm ./testout.txt
}
2016-02-15 20:17:26 +00:00
# check against $EXIT_OK or $EXIT_ERROR with a single char option
# $1 command: tshark or dumpcap
# $2 option: a
test_D_or_L_option( )
{
#echo "command: "$1" opt1: "$2" opt2: "$3" opt3: "$4" opt4: "$5" opt5: "$6
$1 -$2 > ./testout.txt 2>& 1
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_OK -a ! $RETURNVALUE -eq $EXIT_ERROR ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
test_step_ok
fi
rm ./testout.txt
}
2012-11-21 09:01:39 +00:00
# dumpcap
# Only with remote capture: A:ru
# Only with WinPcap: m:
# Only with WinPcap or 1.0.0-or-later libpcap: B:
# Only with 1.0.0-or-later libpcap: I
# Only with libpcap/WinPcap with bpf_image(): d
# check exit status of all invalid single char dumpcap options (must be 1)
clopts_suite_dumpcap_invalid_chars( ) {
for index in C E F G H J K N O Q R T U V W X Y e j l o x z
do
2013-03-01 16:13:44 +00:00
test_step_add " Invalid dumpcap parameter - $index , exit status must be $EXIT_COMMAND_LINE " " test_single_char_options $DUMPCAP $index $EXIT_COMMAND_LINE "
2012-11-21 09:01:39 +00:00
done
}
# check exit status of all valid single char dumpcap options being (must be 0)
# tests only those options that cause dumpcap to do something other than
# capture
clopts_suite_dumpcap_valid_chars( ) {
2012-12-12 14:11:53 +00:00
for index in h v
2012-11-21 09:01:39 +00:00
do
2013-03-01 16:13:44 +00:00
test_step_add " Valid dumpcap parameter - $index , exit status must be $EXIT_OK " " test_single_char_options $DUMPCAP $index $EXIT_OK "
2012-11-21 09:01:39 +00:00
done
}
2016-02-15 20:17:26 +00:00
# special case: interface-specific opts might work as a regular user
# (exit code 0) or might fail due to lack of permissions (exit code 2)
# but shouldn't fail due to a syntax error (exit code 1). We cannot
# predict whether it'll be a success or failure based on the machine,
# so allow either one.
2012-11-21 09:01:39 +00:00
clopts_suite_dumpcap_interface_chars( ) {
for index in D L
do
2016-02-15 20:17:26 +00:00
test_step_add " Valid dumpcap parameter - $index requiring capture permissions, exit status must be $EXIT_OK or $EXIT_ERROR " " test_D_or_L_option $DUMPCAP $index "
2013-03-01 16:13:44 +00:00
done
2012-11-21 09:01:39 +00:00
}
# check exit status and grep output string of an invalid capture filter
clopts_step_dumpcap_invalid_capfilter( ) {
2013-03-01 16:13:44 +00:00
if [ " $WS_SYSTEM " != "Windows" ] ; then
test_step_skipped
return
fi
2012-11-21 09:01:39 +00:00
$DUMPCAP -f 'jkghg' -w './testout.pcap' > ./testout.txt 2>& 1
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
grep -i 'Invalid capture filter "jkghg" for interface' ./testout.txt > /dev/null
if [ $? -eq 0 ] ; then
test_step_output_print ./testout.txt
test_step_ok
else
test_step_output_print ./testout.txt
2012-11-21 19:04:21 +00:00
test_step_failed "Error message wasn't what we expected"
2012-11-21 09:01:39 +00:00
fi
fi
}
# check exit status and grep output string of an invalid interface
clopts_step_dumpcap_invalid_interfaces( ) {
$DUMPCAP -i invalid_interface -w './testout.pcap' > ./testout.txt 2>& 1
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
2012-11-21 19:01:16 +00:00
grep -i 'The capture session could not be initiated' ./testout.txt > /dev/null
2012-11-21 09:01:39 +00:00
if [ $? -eq 0 ] ; then
test_step_output_print ./testout.txt
test_step_ok
else
test_step_output_print ./testout.txt
2012-11-21 19:04:21 +00:00
test_step_failed "Error message wasn't what we expected"
2012-11-21 09:01:39 +00:00
fi
fi
}
# check exit status and grep output string of an invalid interface index
# (valid interface indexes start with 1)
clopts_step_dumpcap_invalid_interfaces_index( ) {
$DUMPCAP -i 0 -w './testout.pcap' > ./testout.txt 2>& 1
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
grep -i 'There is no interface with that adapter index' ./testout.txt > /dev/null
if [ $? -eq 0 ] ; then
test_step_ok
else
test_step_output_print ./testout.txt
2012-11-21 19:04:21 +00:00
test_step_failed "Error message wasn't what we expected"
2012-11-21 09:01:39 +00:00
fi
fi
}
# TShark
2005-12-13 23:15:20 +00:00
# check exit status when reading an existing file
clopts_step_existing_file( ) {
2012-03-30 18:19:35 +00:00
$TSHARK -r " ${ CAPTURE_DIR } dhcp.pcap " > ./testout.txt 2>& 1
2005-12-13 23:15:20 +00:00
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_OK ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
test_step_ok
fi
rm ./testout.txt
}
2008-02-23 21:19:44 +00:00
# check exit status when reading a non-existing file
2005-12-13 23:15:20 +00:00
clopts_step_nonexisting_file( ) {
2014-12-24 09:25:32 +00:00
$TSHARK -r ThisFileDontExist.pcap > ./testout.txt 2>& 1
2005-12-13 23:15:20 +00:00
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_ERROR ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
test_step_ok
fi
2014-12-24 09:25:32 +00:00
rm ./testout.txt
2005-12-13 23:15:20 +00:00
}
2012-11-21 09:01:39 +00:00
# check exit status of all invalid single char TShark options (must be 1)
2006-05-31 17:38:42 +00:00
clopts_suite_tshark_invalid_chars( ) {
2012-12-20 14:36:06 +00:00
for index in A B C E F H J K M N O R T U W X Y Z a b c d e f i j k m o r s t u w y z
2005-12-13 23:15:20 +00:00
do
2013-03-01 16:13:44 +00:00
test_step_add " Invalid TShark parameter - $index , exit status must be $EXIT_COMMAND_LINE " " test_single_char_options $TSHARK $index $EXIT_COMMAND_LINE "
2005-12-13 23:15:20 +00:00
done
}
2012-11-21 09:01:39 +00:00
# check exit status of all valid single char TShark options being (must be 0)
clopts_suite_tshark_valid_chars( ) {
2006-10-09 18:47:42 +00:00
for index in G h v
2005-12-13 23:15:20 +00:00
do
2013-03-01 16:13:44 +00:00
test_step_add " Valid TShark parameter - $index , exit status must be $EXIT_OK " " test_single_char_options $TSHARK $index $EXIT_OK "
2005-12-13 23:15:20 +00:00
done
}
2016-02-15 20:17:26 +00:00
# special case: interface-specific opts might work as a regular user
# (exit code 0) or might fail due to lack of permissions (exit code 2)
# but shouldn't fail due to a syntax error (exit code 1). We cannot
# predict whether it'll be a success or failure based on the machine,
# so allow either one.
2012-11-21 09:01:39 +00:00
clopts_suite_tshark_interface_chars( ) {
2006-10-09 18:47:42 +00:00
for index in D L
do
2016-02-15 20:17:26 +00:00
test_step_add " Valid TShark parameter - $index requiring capture permissions, exit status must be $EXIT_OK or $EXIT_ERROR " " test_D_or_L_option $TSHARK $index "
2013-03-01 16:13:44 +00:00
done
2006-10-09 18:47:42 +00:00
}
2005-12-13 23:15:20 +00:00
# S V l n p q x
# check exit status and grep output string of an invalid capture filter
2012-11-21 09:01:39 +00:00
clopts_step_tshark_invalid_capfilter( ) {
2013-03-01 16:13:44 +00:00
if [ " $WS_SYSTEM " != "Windows" ] ; then
test_step_skipped
return
fi
2006-10-09 18:47:42 +00:00
2006-05-31 17:38:42 +00:00
$TSHARK -f 'jkghg' -w './testout.pcap' > ./testout.txt 2>& 1
2005-12-13 23:15:20 +00:00
RETURNVALUE = $?
2010-07-01 04:01:13 +00:00
if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ] ; then
2005-12-13 23:15:20 +00:00
test_step_failed " exit status: $RETURNVALUE "
else
2011-06-27 16:09:49 +00:00
grep -i 'Invalid capture filter "jkghg" for interface' ./testout.txt > /dev/null
2005-12-13 23:15:20 +00:00
if [ $? -eq 0 ] ; then
2008-02-23 21:19:44 +00:00
test_step_output_print ./testout.txt
2005-12-13 23:15:20 +00:00
test_step_ok
else
2008-02-23 21:19:44 +00:00
test_step_output_print ./testout.txt
2012-11-21 19:04:21 +00:00
test_step_failed "Error message wasn't what we expected"
2005-12-13 23:15:20 +00:00
fi
fi
}
2006-01-05 00:29:32 +00:00
# check exit status and grep output string of an invalid interface
2012-11-21 09:01:39 +00:00
clopts_step_tshark_invalid_interfaces( ) {
2006-05-31 17:38:42 +00:00
$TSHARK -i invalid_interface -w './testout.pcap' > ./testout.txt 2>& 1
2005-12-13 23:15:20 +00:00
RETURNVALUE = $?
2010-07-01 04:01:13 +00:00
if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ] ; then
2005-12-13 23:15:20 +00:00
test_step_failed " exit status: $RETURNVALUE "
else
2012-11-21 19:01:16 +00:00
grep -i 'The capture session could not be initiated' ./testout.txt > /dev/null
2005-12-13 23:15:20 +00:00
if [ $? -eq 0 ] ; then
2008-02-23 21:19:44 +00:00
test_step_output_print ./testout.txt
2005-12-13 23:15:20 +00:00
test_step_ok
else
2008-02-23 21:19:44 +00:00
test_step_output_print ./testout.txt
2012-11-21 19:04:21 +00:00
test_step_failed "Error message wasn't what we expected"
2005-12-13 23:15:20 +00:00
fi
fi
}
2006-01-05 00:29:32 +00:00
# check exit status and grep output string of an invalid interface index
# (valid interface indexes start with 1)
2012-11-21 09:01:39 +00:00
clopts_step_tshark_invalid_interfaces_index( ) {
2006-05-31 17:38:42 +00:00
$TSHARK -i 0 -w './testout.pcap' > ./testout.txt 2>& 1
2006-01-05 00:29:32 +00:00
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_COMMAND_LINE ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
2012-11-21 01:11:19 +00:00
grep -i 'There is no interface with that adapter index' ./testout.txt > /dev/null
2006-01-05 00:29:32 +00:00
if [ $? -eq 0 ] ; then
test_step_ok
else
2008-02-23 21:19:44 +00:00
test_step_output_print ./testout.txt
2012-11-21 19:04:21 +00:00
test_step_failed "Error message wasn't what we expected"
2006-01-05 00:29:32 +00:00
fi
fi
}
2005-12-13 23:15:20 +00:00
# check exit status and grep output string of an invalid capture filter
# XXX - how to efficiently test the *invalid* flags?
clopts_step_valid_name_resolving( ) {
2013-03-01 16:13:44 +00:00
if [ " $WS_SYSTEM " != "Windows" ] ; then
test_step_skipped
return
fi
2006-10-09 18:47:42 +00:00
2006-05-31 17:38:42 +00:00
$TSHARK -N mntC -a duration:1 > ./testout.txt 2>& 1
2005-12-13 23:15:20 +00:00
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_OK ] ; then
test_step_failed " exit status: $RETURNVALUE "
else
test_step_ok
fi
}
2014-03-05 18:14:26 +00:00
test_dump_glossary( ) {
2014-06-27 03:43:51 +00:00
$TSHARK -G $1 > /dev/null 2> ./testout.txt
2014-03-05 18:14:26 +00:00
RETURNVALUE = $?
if [ ! $RETURNVALUE -eq $EXIT_OK ] ; then
test_step_failed " exit status: $RETURNVALUE "
2014-06-27 03:43:51 +00:00
elif [ -s ./testout.txt ] ; then
test_step_output_print ./testout.txt
test_step_failed "Error messages on stderr"
2014-03-05 18:14:26 +00:00
else
test_step_ok
fi
}
2014-12-30 20:38:52 +00:00
test_dump_glossary_utf8( ) {
if [ " $HAVE_ICONV " != "True" ] ; then
test_step_skipped
return
fi
2014-12-31 16:53:39 +00:00
SAVE_LANG = $LANG
LANG = en_US.UTF-8
export LANG
2014-12-30 20:38:52 +00:00
$TSHARK -G $1 | iconv -f UTF-8 > /dev/null 2> ./testout.txt
RETURNVALUE = $?
2014-12-31 16:53:39 +00:00
LANG = $SAVE_LANG
export LANG
2014-12-30 20:38:52 +00:00
if [ ! $RETURNVALUE -eq $EXIT_OK ] ; then
if [ -s ./testout.txt ] ; then
test_step_output_print ./testout.txt
fi
test_step_failed " exit status: $RETURNVALUE "
else
test_step_ok
fi
}
2018-01-10 23:31:03 +00:00
test_dump_glossary_plugins( ) {
# We do a similar test in fuzz-test.sh.
2018-01-11 16:27:52 +00:00
$TSHARK -G plugins > ./testout.txt 2> /dev/null
PLUGIN_COUNT = $( grep dissector ./testout.txt | wc -l )
2018-01-10 23:31:03 +00:00
if [ $PLUGIN_COUNT -lt 10 ] ; then
test_step_output_print ./testout.txt
test_step_failed "Fewer than 10 dissector plugins found"
else
test_step_ok
fi
}
2014-03-05 18:14:26 +00:00
# check that dumping the glossaries succeeds (at least doesn't crash)
# this catches extended value strings without the BASE_EXT_STRING flag
# among other problems
clopts_suite_dump_glossaries( ) {
for glossary in fields protocols values decodes defaultprefs currentprefs; do
test_step_add " Dumping $glossary glossary " " test_dump_glossary $glossary "
2014-12-30 20:38:52 +00:00
test_step_add " Testing $glossary output encoding " " test_dump_glossary_utf8 $glossary "
2014-03-05 18:14:26 +00:00
done
2018-01-10 23:31:03 +00:00
test_step_add "Testing plugins" test_dump_glossary_plugins
2014-03-05 18:14:26 +00:00
}
2005-12-13 23:15:20 +00:00
# check exit status of some basic functions
clopts_suite_basic( ) {
2012-03-30 18:19:35 +00:00
test_step_add "Exit status for existing file: \"" " ${ CAPTURE_DIR } dhcp.pcap " "\" must be 0" clopts_step_existing_file
2005-12-13 23:15:20 +00:00
test_step_add "Exit status for none existing files must be 2" clopts_step_nonexisting_file
}
2012-11-21 17:54:14 +00:00
clopts_suite_dumpcap_capture_options( ) {
test_step_add "Invalid dumpcap capture filter -f" clopts_step_dumpcap_invalid_capfilter
test_step_add "Invalid dumpcap capture interface -i" clopts_step_dumpcap_invalid_interfaces
test_step_add "Invalid dumpcap capture interface index 0" clopts_step_dumpcap_invalid_interfaces_index
2012-11-21 09:01:39 +00:00
}
2012-11-21 17:54:14 +00:00
clopts_suite_tshark_capture_options( ) {
test_step_add "Invalid TShark capture filter -f" clopts_step_tshark_invalid_capfilter
test_step_add "Invalid TShark capture interface -i" clopts_step_tshark_invalid_interfaces
test_step_add "Invalid TShark capture interface index 0" clopts_step_tshark_invalid_interfaces_index
2008-02-23 21:19:44 +00:00
}
2005-12-13 23:15:20 +00:00
clopts_post_step( ) {
2008-02-23 21:19:44 +00:00
rm -f ./testout.txt ./testout2.txt
2005-12-13 23:15:20 +00:00
}
clopt_suite( ) {
test_step_set_post clopts_post_step
test_suite_add "Basic tests" clopts_suite_basic
2012-11-21 09:01:39 +00:00
test_suite_add "Invalid dumpcap single char options" clopts_suite_dumpcap_invalid_chars
test_suite_add "Valid dumpcap single char options" clopts_suite_dumpcap_valid_chars
test_suite_add "Interface-specific dumpcap single char options" clopts_suite_dumpcap_interface_chars
test_suite_add "Capture filter/interface options tests" clopts_suite_dumpcap_capture_options
2006-05-31 17:38:42 +00:00
test_suite_add "Invalid TShark single char options" clopts_suite_tshark_invalid_chars
2012-11-21 09:01:39 +00:00
test_suite_add "Valid TShark single char options" clopts_suite_tshark_valid_chars
test_suite_add "Interface-specific TShark single char options" clopts_suite_tshark_interface_chars
test_suite_add "Capture filter/interface options tests" clopts_suite_tshark_capture_options
2014-03-05 18:14:26 +00:00
test_suite_add "Dump glossaries" clopts_suite_dump_glossaries
2005-12-13 23:15:20 +00:00
test_step_add "Valid name resolution options -N (1s)" clopts_step_valid_name_resolving
#test_remark_add "Options currently unchecked: S, V, l, n, p, q and x"
}
2013-03-01 16:13:44 +00:00
#
# Editor modelines - http://www.wireshark.org/tools/modelines.html
#
# Local variables:
2016-02-03 09:27:09 +00:00
# sh-basic-offset: 8
2013-03-01 16:13:44 +00:00
# tab-width: 8
# indent-tabs-mode: t
# End:
#
# vi: set shiftwidth=8 tabstop=8 noexpandtab:
# :indentSize=8:tabSize=8:noTabs=false:
#