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
5use strict;
6use warnings;
7
8use Test::More tests => 49;
9use FindBin qw($RealBin);
10use lib "$RealBin";
11use Util;
12use Parse::Pidl::Util qw(MyDumper);
13use Parse::Pidl::Wireshark::Conformance qw(ReadConformanceFH valid_ft_type valid_base_type);
14
15sub 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
27ok(parse_conf("\n"), undef);
28ok(parse_conf(" \n"), undef);
29ok(parse_conf("CODE START\nCODE END\n"));
30test_warnings("nofile:1: Expecting CODE END\n", sub { is(parse_conf("CODE START\n"), undef); });
31ok(parse_conf("#foobar\n"), undef);
32test_warnings("nofile:1: Unknown command `foobar'\n",
33	sub { ok(parse_conf("foobar\n"), undef); });
34
35test_warnings("nofile:1: incomplete HF_RENAME command\n",
36	sub { parse_conf("HF_RENAME\n"); });
37
38is_deeply(parse_conf("HF_RENAME foo bar\n")->{hf_renames}->{foo},
39	{ OLDNAME => "foo", NEWNAME => "bar", POS => {FILE => "nofile", LINE => 1}, USED => 0});
40
41is_deeply(parse_conf("NOEMIT\n"), { "noemit_dissector" => 1 });
42is_deeply(parse_conf("NOEMIT foo\n"), { "noemit" => { "foo" => 1 } });
43
44test_warnings("nofile:1: incomplete MANUAL command\n",
45	sub { parse_conf("MANUAL\n"); } );
46
47is_deeply(parse_conf("MANUAL foo\n"), { manual => {foo => 1}});
48
49test_errors("nofile:1: incomplete INCLUDE command\n",
50	sub { parse_conf("INCLUDE\n"); } );
51
52test_warnings("nofile:1: incomplete FIELD_DESCRIPTION command\n",
53	sub { parse_conf("FIELD_DESCRIPTION foo\n"); });
54
55is_deeply(parse_conf("FIELD_DESCRIPTION foo \"my description\"\n"),
56	{ fielddescription => { foo => { DESCRIPTION => "\"my description\"", POS => { FILE => "nofile", LINE => 1}, USED => 0 }}});
57
58is_deeply(parse_conf("FIELD_DESCRIPTION foo my description\n"),
59	{ fielddescription => { foo => { DESCRIPTION => "my", POS => { FILE => "nofile", LINE => 1}, USED => 0 }}});
60
61is_deeply(parse_conf("CODE START\ndata\nCODE END\n"), { override => "data\n" });
62is_deeply(parse_conf("CODE START\ndata\nmore data\nCODE END\n"), { override => "data\nmore data\n" });
63test_warnings("nofile:1: CODE END outside CODE section\n",
64	sub { parse_conf("CODE END\n"); } );
65
66is_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
77ok(valid_ft_type("FT_UINT32"));
78ok(not valid_ft_type("BLA"));
79ok(not valid_ft_type("ft_uint32"));
80ok(valid_ft_type("FT_BLA"));
81
82ok(valid_base_type("BASE_DEC"));
83ok(valid_base_type("BASE_HEX"));
84ok(not valid_base_type("base_dec"));
85ok(not valid_base_type("BLA"));
86ok(not valid_base_type("BASEDEC"));
87
88test_errors("nofile:1: incomplete TYPE command\n",
89	sub { parse_conf("TYPE mytype dissector\n"); });
90
91test_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
94test_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
97test_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
100is_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
105test_errors("nofile:1: incomplete TFS command\n",
106	sub { parse_conf("TFS hf_bla \"Trues\""); } );
107
108test_errors("nofile:1: incomplete PARAM_VALUE command\n",
109	sub { parse_conf("PARAM_VALUE\n"); });
110
111is_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
122is_deeply(parse_conf("STRIP_PREFIX bla_\n"),
123	{ strip_prefixes => [ "bla_" ] });
124
125is_deeply(parse_conf("STRIP_PREFIX bla_\nSTRIP_PREFIX bloe\n"),
126	{ strip_prefixes => [ "bla_", "bloe" ] });
127
128is_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
139is_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
151is_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
163test_errors("nofile:1: no dissectorname specified\n",
164	sub { parse_conf("IMPORT\n"); } );
165
166test_errors("nofile:1: incomplete HF_FIELD command\n",
167	sub { parse_conf("HF_FIELD hf_idx\n"); });
168
169test_errors("nofile:1: incomplete ETT_FIELD command\n",
170	sub { parse_conf("ETT_FIELD\n"); });
171
172is_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
190is_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);
206