2007-02-09 02:54:31 +03:00
#!/usr/bin/perl
# (C) 2007 Jelmer Vernooij <jelmer@samba.org>
# Published under the GNU General Public License
# test parsing wireshark conformance files
use strict ;
use warnings ;
2008-02-19 04:02:48 +03:00
use Test::More tests = > 49 ;
2007-02-09 02:54:31 +03:00
use FindBin qw( $RealBin ) ;
use lib "$RealBin" ;
use Util ;
use Parse::Pidl::Util qw( MyDumper ) ;
2007-02-27 15:46:19 +03:00
use Parse::Pidl::Wireshark::Conformance qw( ReadConformanceFH valid_ft_type valid_base_type ) ;
2007-02-09 02:54:31 +03:00
sub parse_conf ($)
{
my $ str = shift ;
2014-11-21 18:52:32 +03:00
open ( TMP , "+>" , undef ) or die ( "unable to open temp file" ) ;
2007-02-09 02:54:31 +03:00
print TMP $ str ;
seek ( TMP , 0 , 0 ) ;
my $ data = { } ;
ReadConformanceFH ( * TMP , $ data , "nofile" ) or return undef ;
close ( TMP ) ;
return $ data ;
}
ok ( parse_conf ( "\n" ) , undef ) ;
ok ( parse_conf ( " \n" ) , undef ) ;
ok ( parse_conf ( "CODE START\nCODE END\n" ) ) ;
test_warnings ( "nofile:1: Expecting CODE END\n" , sub { is ( parse_conf ( "CODE START\n" ) , undef ) ; } ) ;
ok ( parse_conf ( "#foobar\n" ) , undef ) ;
test_warnings ( "nofile:1: Unknown command `foobar'\n" ,
sub { ok ( parse_conf ( "foobar\n" ) , undef ) ; } ) ;
test_warnings ( "nofile:1: incomplete HF_RENAME command\n" ,
sub { parse_conf ( "HF_RENAME\n" ) ; } ) ;
is_deeply ( parse_conf ( "HF_RENAME foo bar\n" ) - > { hf_renames } - > { foo } ,
{ OLDNAME = > "foo" , NEWNAME = > "bar" , POS = > { FILE = > "nofile" , LINE = > 1 } , USED = > 0 } ) ;
is_deeply ( parse_conf ( "NOEMIT\n" ) , { "noemit_dissector" = > 1 } ) ;
is_deeply ( parse_conf ( "NOEMIT foo\n" ) , { "noemit" = > { "foo" = > 1 } } ) ;
test_warnings ( "nofile:1: incomplete MANUAL command\n" ,
sub { parse_conf ( "MANUAL\n" ) ; } ) ;
is_deeply ( parse_conf ( "MANUAL foo\n" ) , { manual = > { foo = > 1 } } ) ;
2008-02-19 04:02:48 +03:00
test_errors ( "nofile:1: incomplete INCLUDE command\n" ,
sub { parse_conf ( "INCLUDE\n" ) ; } ) ;
2007-02-09 02:54:31 +03:00
test_warnings ( "nofile:1: incomplete FIELD_DESCRIPTION command\n" ,
sub { parse_conf ( "FIELD_DESCRIPTION foo\n" ) ; } ) ;
is_deeply ( parse_conf ( "FIELD_DESCRIPTION foo \"my description\"\n" ) ,
{ fielddescription = > { foo = > { DESCRIPTION = > "\"my description\"" , POS = > { FILE = > "nofile" , LINE = > 1 } , USED = > 0 } } } ) ;
is_deeply ( parse_conf ( "FIELD_DESCRIPTION foo my description\n" ) ,
{ fielddescription = > { foo = > { DESCRIPTION = > "my" , POS = > { FILE = > "nofile" , LINE = > 1 } , USED = > 0 } } } ) ;
is_deeply ( parse_conf ( "CODE START\ndata\nCODE END\n" ) , { override = > "data\n" } ) ;
is_deeply ( parse_conf ( "CODE START\ndata\nmore data\nCODE END\n" ) , { override = > "data\nmore data\n" } ) ;
2016-05-18 15:23:56 +03:00
test_warnings ( "nofile:1: CODE END outside CODE section\n" ,
2007-02-09 02:54:31 +03:00
sub { parse_conf ( "CODE END\n" ) ; } ) ;
2007-02-27 15:46:19 +03:00
2007-04-25 20:10:54 +04:00
is_deeply ( parse_conf ( "TYPE winreg_String dissect_myminregstring(); FT_STRING BASE_DEC 0 0 2\n" ) , { types = > { winreg_String = > {
2007-02-27 15:46:19 +03:00
NAME = > "winreg_String" ,
POS = > { FILE = > "nofile" , LINE = > 1 } ,
USED = > 0 ,
2007-04-25 20:10:54 +04:00
DISSECTOR_NAME = > "dissect_myminregstring();" ,
2007-02-27 15:46:19 +03:00
FT_TYPE = > "FT_STRING" ,
BASE_TYPE = > "BASE_DEC" ,
MASK = > 0 ,
VALSSTRING = > 0 ,
ALIGNMENT = > 2 } } } ) ;
ok ( valid_ft_type ( "FT_UINT32" ) ) ;
ok ( not valid_ft_type ( "BLA" ) ) ;
ok ( not valid_ft_type ( "ft_uint32" ) ) ;
ok ( valid_ft_type ( "FT_BLA" ) ) ;
ok ( valid_base_type ( "BASE_DEC" ) ) ;
ok ( valid_base_type ( "BASE_HEX" ) ) ;
ok ( not valid_base_type ( "base_dec" ) ) ;
ok ( not valid_base_type ( "BLA" ) ) ;
ok ( not valid_base_type ( "BASEDEC" ) ) ;
test_errors ( "nofile:1: incomplete TYPE command\n" ,
sub { parse_conf ( "TYPE mytype dissector\n" ) ; } ) ;
test_warnings ( "nofile:1: dissector name does not contain `dissect'\n" ,
2007-04-25 20:10:54 +04:00
sub { parse_conf ( "TYPE winreg_String myminregstring; FT_STRING BASE_DEC 0 0 2\n" ) ; } ) ;
2007-02-27 15:46:19 +03:00
test_warnings ( "nofile:1: invalid FT_TYPE `BLA'\n" ,
2007-04-25 20:10:54 +04:00
sub { parse_conf ( "TYPE winreg_String dissect_myminregstring; BLA BASE_DEC 0 0 2\n" ) ; } ) ;
2007-02-27 15:46:19 +03:00
test_warnings ( "nofile:1: invalid BASE_TYPE `BLOE'\n" ,
2007-04-25 20:10:54 +04:00
sub { parse_conf ( "TYPE winreg_String dissect_myminregstring; FT_UINT32 BLOE 0 0 2\n" ) ; } ) ;
2007-02-27 16:41:56 +03:00
is_deeply ( parse_conf ( "TFS hf_bla \"True string\" \"False String\"\n" ) ,
{ tfs = > { hf_bla = > {
TRUE_STRING = > "\"True string\"" ,
FALSE_STRING = > "\"False String\"" } } } ) ;
test_errors ( "nofile:1: incomplete TFS command\n" ,
sub { parse_conf ( "TFS hf_bla \"Trues\"" ) ; } ) ;
test_errors ( "nofile:1: incomplete PARAM_VALUE command\n" ,
sub { parse_conf ( "PARAM_VALUE\n" ) ; } ) ;
is_deeply ( parse_conf ( "PARAM_VALUE Life 42\n" ) ,
{ dissectorparams = > {
Life = > {
DISSECTOR = > "Life" ,
POS = > { FILE = > "nofile" , LINE = > 1 } ,
PARAM = > 42 ,
USED = > 0
}
}
} ) ;
is_deeply ( parse_conf ( "STRIP_PREFIX bla_\n" ) ,
{ strip_prefixes = > [ "bla_" ] } ) ;
is_deeply ( parse_conf ( "STRIP_PREFIX bla_\nSTRIP_PREFIX bloe\n" ) ,
{ strip_prefixes = > [ "bla_" , "bloe" ] } ) ;
is_deeply ( parse_conf ( "PROTOCOL atsvc \"Scheduling jobs on remote machines\" \"at\" \"atsvc\"\n" ) ,
{ protocols = > {
atsvc = > {
LONGNAME = > "\"Scheduling jobs on remote machines\"" ,
SHORTNAME = > "\"at\"" ,
FILTERNAME = > "\"atsvc\""
}
}
}
) ;
is_deeply ( parse_conf ( "IMPORT bla\n" ) , {
imports = > {
bla = > {
NAME = > "bla" ,
DATA = > "" ,
USED = > 0 ,
POS = > { FILE = > "nofile" , LINE = > 1 }
}
}
}
) ;
is_deeply ( parse_conf ( "IMPORT bla fn1 fn2 fn3\n" ) , {
imports = > {
bla = > {
NAME = > "bla" ,
DATA = > "fn1 fn2 fn3" ,
USED = > 0 ,
POS = > { FILE = > "nofile" , LINE = > 1 }
}
}
}
) ;
test_errors ( "nofile:1: no dissectorname specified\n" ,
sub { parse_conf ( "IMPORT\n" ) ; } ) ;
test_errors ( "nofile:1: incomplete HF_FIELD command\n" ,
sub { parse_conf ( "HF_FIELD hf_idx\n" ) ; } ) ;
2007-04-25 20:10:54 +04:00
2008-02-13 04:28:41 +03:00
test_errors ( "nofile:1: incomplete ETT_FIELD command\n" ,
sub { parse_conf ( "ETT_FIELD\n" ) ; } ) ;
2007-04-25 20:10:54 +04:00
is_deeply ( parse_conf ( "TYPE winreg_String dissect_myminregstring(); FT_STRING BASE_DEC 0 0 0 2\n" ) , {
types = > {
winreg_String = > {
NAME = > "winreg_String" ,
POS = > { FILE = > "nofile" , LINE = > 1 } ,
USED = > 0 ,
DISSECTOR_NAME = > "dissect_myminregstring();" ,
FT_TYPE = > "FT_STRING" ,
BASE_TYPE = > "BASE_DEC" ,
MASK = > 0 ,
VALSSTRING = > 0 ,
ALIGNMENT = > 0
}
}
}
) ;
is_deeply ( parse_conf ( "TYPE winreg_String \"offset = dissect_myminregstring(\@HF\@);\" FT_STRING BASE_DEC 0 0 0 2\n" ) , {
types = > {
winreg_String = > {
NAME = > "winreg_String" ,
POS = > { FILE = > "nofile" , LINE = > 1 } ,
USED = > 0 ,
DISSECTOR_NAME = > "offset = dissect_myminregstring(\@HF\@);" ,
FT_TYPE = > "FT_STRING" ,
BASE_TYPE = > "BASE_DEC" ,
MASK = > 0 ,
VALSSTRING = > 0 ,
ALIGNMENT = > 0
}
}
}
) ;