Fix typo
[samba.git] / pidl / tests / wireshark-conf.pl
1 #!/usr/bin/perl
2 # (C) 2007 Jelmer Vernooij <jelmer@samba.org>
3 # Published under the GNU General Public License
4 # test parsing wireshark conformance files
5 use strict;
6 use warnings;
7
8 use Test::More tests => 49;
9 use FindBin qw($RealBin);
10 use lib "$RealBin";
11 use Util;
12 use Parse::Pidl::Util qw(MyDumper);
13 use Parse::Pidl::Wireshark::Conformance qw(ReadConformanceFH valid_ft_type valid_base_type);
14
15 sub parse_conf($)
16 {
17         my $str = shift;
18         open(TMP, "+>", undef) or die("unable to open temp file");
19         print TMP $str;
20         seek(TMP, 0, 0);
21         my $data = {};
22         ReadConformanceFH(*TMP, $data, "nofile") or return undef;
23         close(TMP);
24         return $data;
25 }
26
27 ok(parse_conf("\n"), undef);
28 ok(parse_conf(" \n"), undef);
29 ok(parse_conf("CODE START\nCODE END\n"));
30 test_warnings("nofile:1: Expecting CODE END\n", sub { is(parse_conf("CODE START\n"), undef); });
31 ok(parse_conf("#foobar\n"), undef);
32 test_warnings("nofile:1: Unknown command `foobar'\n",
33         sub { ok(parse_conf("foobar\n"), undef); });
34
35 test_warnings("nofile:1: incomplete HF_RENAME command\n",
36         sub { parse_conf("HF_RENAME\n"); });
37
38 is_deeply(parse_conf("HF_RENAME foo bar\n")->{hf_renames}->{foo}, 
39         { OLDNAME => "foo", NEWNAME => "bar", POS => {FILE => "nofile", LINE => 1}, USED => 0});
40
41 is_deeply(parse_conf("NOEMIT\n"), { "noemit_dissector" => 1 });
42 is_deeply(parse_conf("NOEMIT foo\n"), { "noemit" => { "foo" => 1 } });
43
44 test_warnings("nofile:1: incomplete MANUAL command\n",
45         sub { parse_conf("MANUAL\n"); } );
46
47 is_deeply(parse_conf("MANUAL foo\n"), { manual => {foo => 1}});
48
49 test_errors("nofile:1: incomplete INCLUDE command\n",
50         sub { parse_conf("INCLUDE\n"); } );
51
52 test_warnings("nofile:1: incomplete FIELD_DESCRIPTION command\n",
53         sub { parse_conf("FIELD_DESCRIPTION foo\n"); });
54
55 is_deeply(parse_conf("FIELD_DESCRIPTION foo \"my description\"\n"),
56         { fielddescription => { foo => { DESCRIPTION => "\"my description\"", POS => { FILE => "nofile", LINE => 1}, USED => 0 }}});
57
58 is_deeply(parse_conf("FIELD_DESCRIPTION foo my description\n"),
59         { fielddescription => { foo => { DESCRIPTION => "my", POS => { FILE => "nofile", LINE => 1}, USED => 0 }}});
60
61 is_deeply(parse_conf("CODE START\ndata\nCODE END\n"), { override => "data\n" });
62 is_deeply(parse_conf("CODE START\ndata\nmore data\nCODE END\n"), { override => "data\nmore data\n" });
63 test_warnings("nofile:1: CODE END outside CODE section\n",
64         sub { parse_conf("CODE END\n"); } );
65
66 is_deeply(parse_conf("TYPE winreg_String dissect_myminregstring(); FT_STRING BASE_DEC 0 0 2\n"), { types => { winreg_String => { 
67                                 NAME => "winreg_String",
68                                 POS => { FILE => "nofile", LINE => 1 },
69                                 USED => 0,
70                                 DISSECTOR_NAME => "dissect_myminregstring();",
71                                 FT_TYPE => "FT_STRING",
72                                 BASE_TYPE => "BASE_DEC",
73                                 MASK => 0,
74                                 VALSSTRING => 0,
75                                 ALIGNMENT => 2}}});
76
77 ok(valid_ft_type("FT_UINT32"));
78 ok(not valid_ft_type("BLA"));
79 ok(not valid_ft_type("ft_uint32"));
80 ok(valid_ft_type("FT_BLA"));
81
82 ok(valid_base_type("BASE_DEC"));
83 ok(valid_base_type("BASE_HEX"));
84 ok(not valid_base_type("base_dec"));
85 ok(not valid_base_type("BLA"));
86 ok(not valid_base_type("BASEDEC"));
87
88 test_errors("nofile:1: incomplete TYPE command\n",
89         sub { parse_conf("TYPE mytype dissector\n"); });
90
91 test_warnings("nofile:1: dissector name does not contain `dissect'\n",
92         sub { parse_conf("TYPE winreg_String myminregstring; FT_STRING BASE_DEC 0 0 2\n"); });
93
94 test_warnings("nofile:1: invalid FT_TYPE `BLA'\n",
95         sub { parse_conf("TYPE winreg_String dissect_myminregstring; BLA BASE_DEC 0 0 2\n"); });
96
97 test_warnings("nofile:1: invalid BASE_TYPE `BLOE'\n",
98         sub { parse_conf("TYPE winreg_String dissect_myminregstring; FT_UINT32 BLOE 0 0 2\n"); });
99
100 is_deeply(parse_conf("TFS hf_bla \"True string\" \"False String\"\n"),
101                 { tfs => { hf_bla => {
102                                         TRUE_STRING => "\"True string\"",
103                                    FALSE_STRING => "\"False String\"" } } });
104
105 test_errors("nofile:1: incomplete TFS command\n",
106         sub { parse_conf("TFS hf_bla \"Trues\""); } );
107
108 test_errors("nofile:1: incomplete PARAM_VALUE command\n",
109         sub { parse_conf("PARAM_VALUE\n"); });
110
111 is_deeply(parse_conf("PARAM_VALUE Life 42\n"),
112         { dissectorparams => {
113                         Life => {
114                                 DISSECTOR => "Life",
115                                 POS => { FILE => "nofile", LINE => 1 },
116                                 PARAM => 42,
117                                 USED => 0
118                         }
119                 }
120         });
121
122 is_deeply(parse_conf("STRIP_PREFIX bla_\n"),
123         { strip_prefixes => [ "bla_" ] });
124
125 is_deeply(parse_conf("STRIP_PREFIX bla_\nSTRIP_PREFIX bloe\n"),
126         { strip_prefixes => [ "bla_", "bloe" ] });
127
128 is_deeply(parse_conf("PROTOCOL atsvc \"Scheduling jobs on remote machines\" \"at\" \"atsvc\"\n"), 
129         { protocols => {
130                         atsvc => {
131                                 LONGNAME => "\"Scheduling jobs on remote machines\"",
132                                 SHORTNAME => "\"at\"",
133                                 FILTERNAME => "\"atsvc\""
134                         }
135                 }
136         }
137 );
138
139 is_deeply(parse_conf("IMPORT bla\n"), {
140                 imports => {
141                         bla => {
142                                 NAME => "bla",
143                                 DATA => "",
144                                 USED => 0,
145                                 POS => { FILE => "nofile", LINE => 1 }
146                         }
147                 }
148         }
149 );
150
151 is_deeply(parse_conf("IMPORT bla fn1 fn2 fn3\n"), {
152                 imports => {
153                         bla => {
154                                 NAME => "bla",
155                                 DATA => "fn1 fn2 fn3",
156                                 USED => 0,
157                                 POS => { FILE => "nofile", LINE => 1 }
158                         }
159                 }
160         }
161 );
162
163 test_errors("nofile:1: no dissectorname specified\n",
164         sub { parse_conf("IMPORT\n"); } );
165
166 test_errors("nofile:1: incomplete HF_FIELD command\n",
167         sub { parse_conf("HF_FIELD hf_idx\n"); });
168
169 test_errors("nofile:1: incomplete ETT_FIELD command\n",
170         sub { parse_conf("ETT_FIELD\n"); });
171
172 is_deeply(parse_conf("TYPE winreg_String dissect_myminregstring(); FT_STRING BASE_DEC 0 0 0 2\n"), {
173                 types => {
174                         winreg_String => {
175                                 NAME => "winreg_String",
176                                 POS => { FILE => "nofile", LINE => 1 },
177                                 USED => 0,
178                                 DISSECTOR_NAME => "dissect_myminregstring();",
179                                 FT_TYPE => "FT_STRING",
180                                 BASE_TYPE => "BASE_DEC",
181                                 MASK => 0,
182                                 VALSSTRING => 0,
183                                 ALIGNMENT => 0
184                         }
185                 }
186         }
187 );
188
189
190 is_deeply(parse_conf("TYPE winreg_String \"offset = dissect_myminregstring(\@HF\@);\" FT_STRING BASE_DEC 0 0 0 2\n"), {
191                 types => {
192                         winreg_String => {
193                                 NAME => "winreg_String",
194                                 POS => { FILE => "nofile", LINE => 1 },
195                                 USED => 0,
196                                 DISSECTOR_NAME => "offset = dissect_myminregstring(\@HF\@);",
197                                 FT_TYPE => "FT_STRING",
198                                 BASE_TYPE => "BASE_DEC",
199                                 MASK => 0,
200                                 VALSSTRING => 0,
201                                 ALIGNMENT => 0
202                         }
203                 }
204         }
205 );