r21459: Remove name argument, more refactoring.
[Samba.git] / source / pidl / tests / samba-ndr.pl
blob1167f77aeeee45a44c65e6c780e5d59706cce747
1 #!/usr/bin/perl
2 # (C) 2007 Jelmer Vernooij <jelmer@samba.org>
3 # Published under the GNU General Public License
4 use strict;
5 use warnings;
7 use Test::More tests => 34;
8 use FindBin qw($RealBin);
9 use lib "$RealBin";
10 use Util;
11 use Parse::Pidl::Util qw(MyDumper);
12 use Parse::Pidl::Samba4::NDR::Parser qw(check_null_pointer
13 GenerateFunctionInEnv GenerateFunctionOutEnv GenerateStructEnv
14 EnvSubstituteValue NeededFunction NeededElement NeededType $res);
16 my $output;
17 sub print_fn($) { my $x = shift; $output.=$x; }
19 # Test case 1: Simple unique pointer dereference
21 $output = "";
22 my $fn = check_null_pointer({
23 PARENT => {
24 ELEMENTS => [
26 NAME => "bla",
27 LEVELS => [
28 { TYPE => "POINTER",
29 POINTER_INDEX => 0,
30 POINTER_TYPE => "unique" },
31 { TYPE => "DATA" }
36 }, { bla => "r->in.bla" }, \&print_fn, "return;");
39 test_warnings("", sub { $fn->("r->in.bla"); });
41 is($output, "if (r->in.bla == NULL) return;");
43 # Test case 2: Simple ref pointer dereference
45 $output = "";
46 $fn = check_null_pointer({
47 PARENT => {
48 ELEMENTS => [
50 NAME => "bla",
51 LEVELS => [
52 { TYPE => "POINTER",
53 POINTER_INDEX => 0,
54 POINTER_TYPE => "ref" },
55 { TYPE => "DATA" }
60 }, { bla => "r->in.bla" }, \&print_fn, undef);
62 test_warnings("", sub { $fn->("r->in.bla"); });
64 is($output, "");
66 # Test case 3: Illegal dereference
68 $output = "";
69 $fn = check_null_pointer({
70 FILE => "nofile",
71 LINE => 1,
72 PARENT => {
73 ELEMENTS => [
75 NAME => "bla",
76 LEVELS => [
77 { TYPE => "DATA" }
82 }, { bla => "r->in.bla" }, \&print_fn, undef);
84 test_warnings("nofile:1: too much dereferences for `bla'\n",
85 sub { $fn->("r->in.bla"); });
87 is($output, "");
89 # Test case 4: Double pointer dereference
91 $output = "";
92 $fn = check_null_pointer({
93 PARENT => {
94 ELEMENTS => [
96 NAME => "bla",
97 LEVELS => [
98 { TYPE => "POINTER",
99 POINTER_INDEX => 0,
100 POINTER_TYPE => "unique" },
101 { TYPE => "POINTER",
102 POINTER_INDEX => 1,
103 POINTER_TYPE => "unique" },
104 { TYPE => "DATA" }
109 }, { bla => "r->in.bla" }, \&print_fn, "return;");
111 test_warnings("",
112 sub { $fn->("*r->in.bla"); });
114 is($output, "if (*r->in.bla == NULL) return;");
116 # Test case 5: Unknown variable
118 $output = "";
119 $fn = check_null_pointer({
120 FILE => "nofile",
121 LINE => 2,
122 PARENT => {
123 ELEMENTS => [
125 NAME => "bla",
126 LEVELS => [
127 { TYPE => "DATA" }
132 }, { }, \&print_fn, "return;");
134 test_warnings("nofile:2: unknown dereferenced expression `r->in.bla'\n",
135 sub { $fn->("r->in.bla"); });
137 is($output, "if (r->in.bla == NULL) return;");
139 # Make sure GenerateFunctionInEnv and GenerateFunctionOutEnv work
140 $fn = { ELEMENTS => [ { DIRECTION => ["in"], NAME => "foo" } ] };
141 is_deeply({ "foo" => "r->in.foo" }, GenerateFunctionInEnv($fn));
143 $fn = { ELEMENTS => [ { DIRECTION => ["out"], NAME => "foo" } ] };
144 is_deeply({ "foo" => "r->out.foo" }, GenerateFunctionOutEnv($fn));
146 $fn = { ELEMENTS => [ { DIRECTION => ["out", "in"], NAME => "foo" } ] };
147 is_deeply({ "foo" => "r->in.foo" }, GenerateFunctionInEnv($fn));
149 $fn = { ELEMENTS => [ { DIRECTION => ["out", "in"], NAME => "foo" } ] };
150 is_deeply({ "foo" => "r->out.foo" }, GenerateFunctionOutEnv($fn));
152 $fn = { ELEMENTS => [ { DIRECTION => ["in"], NAME => "foo" } ] };
153 is_deeply({ "foo" => "r->in.foo" }, GenerateFunctionOutEnv($fn));
155 $fn = { ELEMENTS => [ { DIRECTION => ["out"], NAME => "foo" } ] };
156 is_deeply({ }, GenerateFunctionInEnv($fn));
158 $fn = { ELEMENTS => [ { NAME => "foo" }, { NAME => "bar" } ] };
159 is_deeply({ foo => "r->foo", bar => "r->bar", this => "r" },
160 GenerateStructEnv($fn, "r"));
162 $fn = { ELEMENTS => [ { NAME => "foo" }, { NAME => "bar" } ] };
163 is_deeply({ foo => "some->complex.variable->foo",
164 bar => "some->complex.variable->bar",
165 this => "some->complex.variable" },
166 GenerateStructEnv($fn, "some->complex.variable"));
168 $fn = { ELEMENTS => [ { NAME => "foo", PROPERTIES => { value => 3 }} ] };
170 my $env = GenerateStructEnv($fn, "r");
171 EnvSubstituteValue($env, $fn);
172 is_deeply($env, { foo => 3, this => "r" });
174 $fn = { ELEMENTS => [ { NAME => "foo" }, { NAME => "bar" } ] };
175 $env = GenerateStructEnv($fn, "r");
176 EnvSubstituteValue($env, $fn);
177 is_deeply($env, { foo => 'r->foo', bar => 'r->bar', this => "r" });
179 $fn = { ELEMENTS => [ { NAME => "foo", PROPERTIES => { value => 0 }} ] };
181 $env = GenerateStructEnv($fn, "r");
182 EnvSubstituteValue($env, $fn);
183 is_deeply($env, { foo => 0, this => "r" });
185 my $needed = {};
186 NeededElement({ TYPE => "foo", REPRESENTATION_TYPE => "foo" }, "pull", $needed);
187 is_deeply($needed, { pull_foo => 1 });
189 # old settings should be kept
190 $needed = { pull_foo => 0 };
191 NeededElement({ TYPE => "foo", REPRESENTATION_TYPE => "foo" }, "pull", $needed);
192 is_deeply($needed, { pull_foo => 0 });
194 # print/pull/push are independent of each other
195 $needed = { pull_foo => 0 };
196 NeededElement({ TYPE => "foo", REPRESENTATION_TYPE => "foo" }, "print", $needed);
197 is_deeply($needed, { pull_foo => 0, print_foo => 1 });
199 $needed = { };
200 NeededFunction({ NAME => "foo", ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] }, $needed);
201 is_deeply($needed, { pull_foo => 1, print_foo => 1, push_foo => 1,
202 pull_bar => 1, print_bar => 1, push_bar => 1});
204 # push/pull/print are always set for functions
205 $needed = { pull_foo => 0 };
206 NeededFunction({ NAME => "foo", ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] }, $needed);
207 is_deeply($needed, { pull_foo => 1, print_foo => 1, push_foo => 1,
208 pull_bar => 1, push_bar => 1, print_bar => 1});
210 # public structs are always needed
211 $needed = {};
212 NeededType({ NAME => "bla", DATA => { TYPE => "STRUCT", ELEMENTS => [] } },
213 $needed);
214 is_deeply($needed, { });
216 $needed = {};
217 NeededType({ PROPERTIES => { public => 1 }, NAME => "bla",
218 DATA => { TYPE => "STRUCT", ELEMENTS => [] } },
219 $needed);
220 is_deeply($needed, { pull_bla => 1, print_bla => 1, push_bla => 1 });
222 # make sure types for elements are set too
223 $needed = {};
224 NeededType({ PROPERTIES => { public => 1 }, NAME => "bla",
225 DATA => { TYPE => "STRUCT",
226 ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] } },
227 $needed);
228 is_deeply($needed, { pull_bla => 1, print_bla => 1, push_bla => 1,
229 pull_bar => 1, print_bar => 1, push_bar => 1});
231 $needed = {};
232 NeededType({ PROPERTIES => { gensize => 1}, NAME => "bla",
233 DATA => { TYPE => "STRUCT",
234 ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] } },
235 $needed);
236 is_deeply($needed, { ndr_size_bla => 1 });
238 # make sure types for elements are set too
239 $needed = { pull_bla => 1 };
240 NeededType({ NAME => "bla",
241 DATA => { TYPE => "STRUCT",
242 ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] } },
243 $needed);
244 is_deeply($needed, { pull_bla => 1, pull_bar => 1 });
246 $needed = {};
247 NeededType({ PROPERTIES => { public => 1},
248 NAME => "bla",
249 DATA => { TYPE => "STRUCT",
250 ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "rep" } ] } },
251 $needed);
252 is_deeply($needed, { pull_bla => 1, push_bla => 1, print_bla => 1, print_rep => 1,
253 pull_bar => 1, push_bar => 1,
254 ndr_bar_to_rep => 1, ndr_rep_to_bar => 1});
256 $res = "";
257 Parse::Pidl::Samba4::NDR::Parser::ParseStructPush({
258 NAME => "mystruct",
259 TYPE => "STRUCT",
260 PROPERTIES => {},
261 ALIGN => 4,
262 ELEMENTS => [ ]}, "x");
263 is($res, "if (ndr_flags & NDR_SCALARS) {
264 NDR_CHECK(ndr_push_align(ndr, 4));
266 if (ndr_flags & NDR_BUFFERS) {
270 $res = "";
271 my $e = {
272 NAME => "el1",
273 TYPE => "mytype",
274 REPRESENTATION_TYPE => "mytype",
275 PROPERTIES => {},
276 LEVELS => [
277 { LEVEL_INDEX => 0, TYPE => "DATA", DATA_TYPE => "mytype" }
278 ] };
279 Parse::Pidl::Samba4::NDR::Parser::ParseStructPush({
280 NAME => "mystruct",
281 TYPE => "STRUCT",
282 PROPERTIES => {},
283 ALIGN => 4,
284 SURROUNDING_ELEMENT => $e,
285 ELEMENTS => [ $e ]}, "x");
286 is($res, "if (ndr_flags & NDR_SCALARS) {
287 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_string_array_size(ndr, x->el1)));
288 NDR_CHECK(ndr_push_align(ndr, 4));
289 NDR_CHECK(ndr_push_mytype(ndr, NDR_SCALARS, &x->el1));
291 if (ndr_flags & NDR_BUFFERS) {