summaryrefslogtreecommitdiff
path: root/source4/pidl/tests/samba-ndr.pl
blob: a14111961f58024bd731e0cccd8da3b13c3e2b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/perl
# (C) 2007 Jelmer Vernooij <jelmer@samba.org>
# Published under the GNU General Public License
use strict;
use warnings;

use Test::More tests => 30;
use FindBin qw($RealBin);
use lib "$RealBin";
use Util;
use strict;
use Parse::Pidl::Util qw(MyDumper);
use Parse::Pidl::Samba4::NDR::Parser qw(check_null_pointer 
	NeededFunction NeededElement NeededType
	NeededInterface TypeFunctionName ParseElementPrint); 

my $output;
sub print_fn($) { my $x = shift; $output.=$x; }

# Test case 1: Simple unique pointer dereference

$output = "";
my $fn = check_null_pointer({ 
	PARENT => {
		ELEMENTS => [
			{ 
				NAME => "bla",
				LEVELS => [
					{ TYPE => "POINTER",
					  POINTER_INDEX => 0,
					  POINTER_TYPE => "unique" },
					{ TYPE => "DATA" }
				],
			},
		]
	}
}, { bla => "r->in.bla" }, \&print_fn, "return;"); 


test_warnings("", sub { $fn->("r->in.bla"); });

is($output, "if (r->in.bla == NULL) return;");

# Test case 2: Simple ref pointer dereference

$output = "";
$fn = check_null_pointer({ 
	PARENT => {
		ELEMENTS => [
			{ 
				NAME => "bla",
				LEVELS => [
					{ TYPE => "POINTER",
					  POINTER_INDEX => 0,
					  POINTER_TYPE => "ref" },
					{ TYPE => "DATA" }
				],
			},
		]
	}
}, { bla => "r->in.bla" }, \&print_fn, undef); 

test_warnings("", sub { $fn->("r->in.bla"); });

is($output, "");

# Test case 3: Illegal dereference

$output = "";
$fn = check_null_pointer({ 
	FILE => "nofile",
	LINE => 1,
	PARENT => {
		ELEMENTS => [
			{ 
				NAME => "bla",
				LEVELS => [
					{ TYPE => "DATA" }
				],
			},
		]
	}
}, { bla => "r->in.bla" }, \&print_fn, undef); 

test_warnings("nofile:1: too much dereferences for `bla'\n", 
	          sub { $fn->("r->in.bla"); });

is($output, "");

# Test case 4: Double pointer dereference

$output = "";
$fn = check_null_pointer({ 
	PARENT => {
		ELEMENTS => [
			{ 
				NAME => "bla",
				LEVELS => [
					{ TYPE => "POINTER",
					  POINTER_INDEX => 0,
					  POINTER_TYPE => "unique" },
					{ TYPE => "POINTER",
					  POINTER_INDEX => 1,
					  POINTER_TYPE => "unique" },
					{ TYPE => "DATA" }
				],
			},
		]
	}
}, { bla => "r->in.bla" }, \&print_fn, "return;"); 

test_warnings("",
	          sub { $fn->("*r->in.bla"); });

is($output, "if (*r->in.bla == NULL) return;");

# Test case 5: Unknown variable

$output = "";
$fn = check_null_pointer({ 
	FILE => "nofile",
	LINE => 2,
	PARENT => {
		ELEMENTS => [
			{ 
				NAME => "bla",
				LEVELS => [
					{ TYPE => "DATA" }
				],
			},
		]
	}
}, { }, \&print_fn, "return;"); 

test_warnings("nofile:2: unknown dereferenced expression `r->in.bla'\n",
	          sub { $fn->("r->in.bla"); });

is($output, "if (r->in.bla == NULL) return;");

my $needed = {};
NeededElement({ TYPE => "foo", REPRESENTATION_TYPE => "foo" }, "pull", $needed); 
is_deeply($needed, { ndr_pull_foo => 1 });

# old settings should be kept
$needed = { ndr_pull_foo => 0 };
NeededElement({ TYPE => "foo", REPRESENTATION_TYPE => "foo" }, "pull", $needed); 
is_deeply($needed, { ndr_pull_foo => 0 });

# print/pull/push are independent of each other
$needed = { ndr_pull_foo => 0 };
NeededElement({ TYPE => "foo", REPRESENTATION_TYPE => "foo" }, "print", $needed); 
is_deeply($needed, { ndr_pull_foo => 0, ndr_print_foo => 1 });

$needed = { };
NeededFunction({ NAME => "foo", ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] }, $needed); 
is_deeply($needed, { ndr_pull_foo => 1, ndr_print_foo => 1, ndr_push_foo => 1,
	                 ndr_pull_bar => 1, ndr_print_bar => 1, ndr_push_bar => 1});

# push/pull/print are always set for functions
$needed = { ndr_pull_foo => 0 };
NeededFunction({ NAME => "foo", ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] }, $needed); 
is_deeply($needed, { ndr_pull_foo => 1, ndr_print_foo => 1, ndr_push_foo => 1,
	                 ndr_pull_bar => 1, ndr_push_bar => 1, ndr_print_bar => 1});

# public structs are always needed
$needed = {};
NeededType({ NAME => "bla", TYPE => "TYPEDEF",
		DATA => { TYPE => "STRUCT", ELEMENTS => [] } },
			  $needed, "pull");
is_deeply($needed, { });

$needed = {};
NeededInterface({ TYPES => [ { PROPERTIES => { public => 1 }, NAME => "bla", 
				TYPE => "TYPEDEF",
	            DATA => { TYPE => "STRUCT", ELEMENTS => [] } } ] },
			  $needed);
is_deeply($needed, { ndr_pull_bla => 1, ndr_push_bla => 1, ndr_print_bla => 1 });

# make sure types for elements are set too
$needed = {};
NeededInterface({ TYPES => [ { PROPERTIES => { public => 1 }, NAME => "bla", 
				TYPE => "TYPEDEF",
	            DATA => { TYPE => "STRUCT", 
						  ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] } } ] },
			  $needed);
is_deeply($needed, { ndr_pull_bla => 1, ndr_pull_bar => 1, ndr_push_bla => 1, ndr_push_bar => 1,
					 ndr_print_bla => 1, ndr_print_bar => 1});

$needed = {};
NeededInterface({ TYPES => [ { PROPERTIES => { gensize => 1}, NAME => "bla", 
				TYPE => "TYPEDEF",
	            DATA => { TYPE => "STRUCT", 
						  ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] } } ] },
			  $needed);
is_deeply($needed, { ndr_size_bla => 1 });
	                 
# make sure types for elements are set too
$needed = { ndr_pull_bla => 1 };
NeededType({ NAME => "bla", 
				TYPE => "TYPEDEF",
	            DATA => { TYPE => "STRUCT", 
						  ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "bar" } ] } },
			  $needed, "pull");
is_deeply($needed, { ndr_pull_bla => 1, ndr_pull_bar => 1 });

$needed = {};
NeededInterface({ TYPES => [ { PROPERTIES => { public => 1}, 
				NAME => "bla", 
				TYPE => "TYPEDEF",
	            DATA => { TYPE => "STRUCT", 
						  ELEMENTS => [ { TYPE => "bar", REPRESENTATION_TYPE => "rep" } ] } } ] }, $needed);
is_deeply($needed, { ndr_pull_bla => 1, ndr_push_bla => 1, ndr_print_bla => 1, 
					 ndr_print_rep => 1,
	                 ndr_pull_bar => 1, ndr_push_bar => 1, 
				     ndr_bar_to_rep => 1, ndr_rep_to_bar => 1});
	
my $generator = new Parse::Pidl::Samba4::NDR::Parser();
$generator->ParseStructPush({
			NAME => "mystruct",
			TYPE => "STRUCT",
			PROPERTIES => {},
			ALIGN => 4,
			ELEMENTS => [ ]}, "x");
is($generator->{res}, "if (ndr_flags & NDR_SCALARS) {
	NDR_CHECK(ndr_push_align(ndr, 4));
}
if (ndr_flags & NDR_BUFFERS) {
}
");

$generator = new Parse::Pidl::Samba4::NDR::Parser();
my $e = { 
	NAME => "el1", 
	TYPE => "mytype",
	REPRESENTATION_TYPE => "mytype",
	PROPERTIES => {},
	LEVELS => [ 
		{ LEVEL_INDEX => 0, TYPE => "DATA", DATA_TYPE => "mytype" } 
] };
$generator->ParseStructPush({
			NAME => "mystruct",
			TYPE => "STRUCT",
			PROPERTIES => {},
			ALIGN => 4,
			SURROUNDING_ELEMENT => $e,
			ELEMENTS => [ $e ]}, "x");
is($generator->{res}, "if (ndr_flags & NDR_SCALARS) {
	NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, ndr_string_array_size(ndr, x->el1)));
	NDR_CHECK(ndr_push_align(ndr, 4));
	NDR_CHECK(ndr_push_mytype(ndr, NDR_SCALARS, &x->el1));
}
if (ndr_flags & NDR_BUFFERS) {
}
");

is(TypeFunctionName("ndr_pull", "uint32"), "ndr_pull_uint32");
is(TypeFunctionName("ndr_pull", {TYPE => "ENUM", NAME => "bar"}), "ndr_pull_ENUM_bar");
is(TypeFunctionName("ndr_pull", {TYPE => "TYPEDEF", NAME => "bar", DATA => undef}), "ndr_pull_bar");
is(TypeFunctionName("ndr_push", {TYPE => "STRUCT", NAME => "bar"}), "ndr_push_STRUCT_bar");

# check noprint works
$generator = new Parse::Pidl::Samba4::NDR::Parser();
$generator->ParseElementPrint({ NAME => "x", TYPE => "rt", REPRESENTATION_TYPE => "rt", 
				    PROPERTIES => { noprint => 1},
				    LEVELS => [ { TYPE => "DATA", DATA_TYPE => "rt"} ]}, "var", { "x" => "r->foobar" } );
is($generator->{res}, "");

$generator = new Parse::Pidl::Samba4::NDR::Parser();
$generator->ParseElementPrint({ NAME => "x", TYPE => "rt", REPRESENTATION_TYPE => "rt", 
				    PROPERTIES => {},
				    LEVELS => [ { TYPE => "DATA", DATA_TYPE => "rt" }]}, "var", { "x" => "r->foobar" } );
is($generator->{res}, "ndr_print_rt(ndr, \"x\", &var);\n");

# make sure that a print function for an element with value() set works
$generator = new Parse::Pidl::Samba4::NDR::Parser();
$generator->ParseElementPrint({ NAME => "x", TYPE => "uint32", REPRESENTATION_TYPE => "uint32", 
				    PROPERTIES => { value => "23" },
				    LEVELS => [ { TYPE => "DATA", DATA_TYPE => "uint32"} ]}, "var", { "x" => "r->foobar" } );
is($generator->{res}, "ndr_print_uint32(ndr, \"x\", (ndr->flags & LIBNDR_PRINT_SET_VALUES)?23:var);\n");