WHATSNEW: Update changes.
[Samba.git] / pidl / tests / samba3-cli.pl
blob0d283a2d5aae05c8d20666ed985db0bf948c7bc8
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 => 9;
8 use FindBin qw($RealBin);
9 use lib "$RealBin";
10 use Util;
11 use Parse::Pidl::Util qw(MyDumper);
12 use Parse::Pidl::Samba3::ClientNDR qw(ParseFunction ParseOutputArgument);
13 use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv);
15 # Make sure GenerateFunctionInEnv and GenerateFunctionOutEnv work
16 my $fn = { ELEMENTS => [ { DIRECTION => ["in"], NAME => "foo" } ] };
17 is_deeply({ "foo" => "r.in.foo" }, GenerateFunctionInEnv($fn, "r."));
18 is_deeply({ "foo" => "r.in.foo" }, GenerateFunctionOutEnv($fn, "r."));
20 $fn = { ELEMENTS => [ { DIRECTION => ["out", "in"], NAME => "foo" } ] };
21 is_deeply({ "foo" => "r.in.foo" }, GenerateFunctionInEnv($fn, "r."));
22 is_deeply({ "foo" => "r.out.foo" }, GenerateFunctionOutEnv($fn, "r."));
24 $fn = { ELEMENTS => [ { DIRECTION => ["out"], NAME => "foo" } ] };
25 is_deeply({ }, GenerateFunctionInEnv($fn, "r."));
26 is_deeply({ "foo" => "r.out.foo" }, GenerateFunctionOutEnv($fn, "r."));
28 my $x = new Parse::Pidl::Samba3::ClientNDR();
30 $fn = { NAME => "bar", ELEMENTS => [ ] };
31 $x->ParseFunction("foo", $fn);
32 is($x->{res},
33 "struct rpccli_bar_state {
34 struct bar orig;
35 struct bar tmp;
36 TALLOC_CTX *out_mem_ctx;
37 NTSTATUS (*dispatch_recv)(struct tevent_req *req, TALLOC_CTX *mem_ctx);
40 static void rpccli_bar_done(struct tevent_req *subreq);
42 struct tevent_req *rpccli_bar_send(TALLOC_CTX *mem_ctx,
43 struct tevent_context *ev,
44 struct rpc_pipe_client *cli)
46 struct tevent_req *req;
47 struct rpccli_bar_state *state;
48 struct tevent_req *subreq;
50 req = tevent_req_create(mem_ctx, &state,
51 struct rpccli_bar_state);
52 if (req == NULL) {
53 return NULL;
55 state->out_mem_ctx = NULL;
56 state->dispatch_recv = cli->dispatch_recv;
58 /* In parameters */
60 /* Out parameters */
62 if (DEBUGLEVEL >= 10) {
63 NDR_PRINT_IN_DEBUG(bar, &state->orig);
66 /* make a temporary copy, that we pass to the dispatch function */
67 state->tmp = state->orig;
69 subreq = cli->dispatch_send(state, ev, cli,
70 &ndr_table_foo,
71 NDR_BAR,
72 &state->tmp);
73 if (tevent_req_nomem(subreq, req)) {
74 return tevent_req_post(req, ev);
76 tevent_req_set_callback(subreq, rpccli_bar_done, req);
77 return req;
80 static void rpccli_bar_done(struct tevent_req *subreq)
82 struct tevent_req *req = tevent_req_callback_data(
83 subreq, struct tevent_req);
84 struct rpccli_bar_state *state = tevent_req_data(
85 req, struct rpccli_bar_state);
86 NTSTATUS status;
87 TALLOC_CTX *mem_ctx;
89 if (state->out_mem_ctx) {
90 mem_ctx = state->out_mem_ctx;
91 } else {
92 mem_ctx = state;
95 status = state->dispatch_recv(subreq, mem_ctx);
96 TALLOC_FREE(subreq);
97 if (!NT_STATUS_IS_OK(status)) {
98 tevent_req_nterror(req, status);
99 return;
102 /* Copy out parameters */
104 /* Reset temporary structure */
105 ZERO_STRUCT(state->tmp);
107 if (DEBUGLEVEL >= 10) {
108 NDR_PRINT_OUT_DEBUG(bar, &state->orig);
111 tevent_req_done(req);
114 NTSTATUS rpccli_bar_recv(struct tevent_req *req,
115 TALLOC_CTX *mem_ctx)
117 struct rpccli_bar_state *state = tevent_req_data(
118 req, struct rpccli_bar_state);
119 NTSTATUS status;
121 if (tevent_req_is_nterror(req, &status)) {
122 tevent_req_received(req);
123 return status;
126 /* Steal possbile out parameters to the callers context */
127 talloc_steal(mem_ctx, state->out_mem_ctx);
129 tevent_req_received(req);
130 return NT_STATUS_OK;
133 NTSTATUS rpccli_bar(struct rpc_pipe_client *cli,
134 TALLOC_CTX *mem_ctx)
136 \tstruct bar r;
137 \tNTSTATUS status;
139 \t/* In parameters */
141 \tif (DEBUGLEVEL >= 10) {
142 \t\tNDR_PRINT_IN_DEBUG(bar, &r);
145 status = cli->dispatch(cli,
146 mem_ctx,
147 &ndr_table_foo,
148 NDR_BAR,
149 &r);
151 \tif (!NT_STATUS_IS_OK(status)) {
152 \t\treturn status;
155 \tif (DEBUGLEVEL >= 10) {
156 \t\tNDR_PRINT_OUT_DEBUG(bar, &r);
159 \tif (NT_STATUS_IS_ERR(status)) {
160 \t\treturn status;
163 \t/* Return variables */
165 \t/* Return result */
166 \treturn NT_STATUS_OK;
171 $x = new Parse::Pidl::Samba3::ClientNDR();
173 $fn = { NAME => "bar", ELEMENTS => [ ], RETURN_TYPE => "WERROR" };
174 $x->ParseFunction("foo", $fn);
175 is($x->{res},
176 "struct rpccli_bar_state {
177 struct bar orig;
178 struct bar tmp;
179 TALLOC_CTX *out_mem_ctx;
180 NTSTATUS (*dispatch_recv)(struct tevent_req *req, TALLOC_CTX *mem_ctx);
183 static void rpccli_bar_done(struct tevent_req *subreq);
185 struct tevent_req *rpccli_bar_send(TALLOC_CTX *mem_ctx,
186 struct tevent_context *ev,
187 struct rpc_pipe_client *cli)
189 struct tevent_req *req;
190 struct rpccli_bar_state *state;
191 struct tevent_req *subreq;
193 req = tevent_req_create(mem_ctx, &state,
194 struct rpccli_bar_state);
195 if (req == NULL) {
196 return NULL;
198 state->out_mem_ctx = NULL;
199 state->dispatch_recv = cli->dispatch_recv;
201 /* In parameters */
203 /* Out parameters */
205 /* Result */
206 ZERO_STRUCT(state->orig.out.result);
208 if (DEBUGLEVEL >= 10) {
209 NDR_PRINT_IN_DEBUG(bar, &state->orig);
212 /* make a temporary copy, that we pass to the dispatch function */
213 state->tmp = state->orig;
215 subreq = cli->dispatch_send(state, ev, cli,
216 &ndr_table_foo,
217 NDR_BAR,
218 &state->tmp);
219 if (tevent_req_nomem(subreq, req)) {
220 return tevent_req_post(req, ev);
222 tevent_req_set_callback(subreq, rpccli_bar_done, req);
223 return req;
226 static void rpccli_bar_done(struct tevent_req *subreq)
228 struct tevent_req *req = tevent_req_callback_data(
229 subreq, struct tevent_req);
230 struct rpccli_bar_state *state = tevent_req_data(
231 req, struct rpccli_bar_state);
232 NTSTATUS status;
233 TALLOC_CTX *mem_ctx;
235 if (state->out_mem_ctx) {
236 mem_ctx = state->out_mem_ctx;
237 } else {
238 mem_ctx = state;
241 status = state->dispatch_recv(subreq, mem_ctx);
242 TALLOC_FREE(subreq);
243 if (!NT_STATUS_IS_OK(status)) {
244 tevent_req_nterror(req, status);
245 return;
248 /* Copy out parameters */
250 /* Copy result */
251 state->orig.out.result = state->tmp.out.result;
253 /* Reset temporary structure */
254 ZERO_STRUCT(state->tmp);
256 if (DEBUGLEVEL >= 10) {
257 NDR_PRINT_OUT_DEBUG(bar, &state->orig);
260 tevent_req_done(req);
263 NTSTATUS rpccli_bar_recv(struct tevent_req *req,
264 TALLOC_CTX *mem_ctx,
265 WERROR *result)
267 struct rpccli_bar_state *state = tevent_req_data(
268 req, struct rpccli_bar_state);
269 NTSTATUS status;
271 if (tevent_req_is_nterror(req, &status)) {
272 tevent_req_received(req);
273 return status;
276 /* Steal possbile out parameters to the callers context */
277 talloc_steal(mem_ctx, state->out_mem_ctx);
279 /* Return result */
280 *result = state->orig.out.result;
282 tevent_req_received(req);
283 return NT_STATUS_OK;
286 NTSTATUS rpccli_bar(struct rpc_pipe_client *cli,
287 TALLOC_CTX *mem_ctx,
288 WERROR *werror)
290 \tstruct bar r;
291 \tNTSTATUS status;
293 \t/* In parameters */
295 \tif (DEBUGLEVEL >= 10) {
296 \t\tNDR_PRINT_IN_DEBUG(bar, &r);
299 status = cli->dispatch(cli,
300 mem_ctx,
301 &ndr_table_foo,
302 NDR_BAR,
303 &r);
305 \tif (!NT_STATUS_IS_OK(status)) {
306 \t\treturn status;
309 \tif (DEBUGLEVEL >= 10) {
310 \t\tNDR_PRINT_OUT_DEBUG(bar, &r);
313 \tif (NT_STATUS_IS_ERR(status)) {
314 \t\treturn status;
317 \t/* Return variables */
319 \t/* Return result */
320 \tif (werror) {
321 \t\t*werror = r.out.result;
324 \treturn werror_to_ntstatus(r.out.result);
329 $x = new Parse::Pidl::Samba3::ClientNDR();
331 $fn = { NAME => "bar", ELEMENTS => [ ], RETURN_TYPE => "WERROR" };
332 my $e = { NAME => "foo", ORIGINAL => { FILE => "f", LINE => -1 },
333 LEVELS => [ { TYPE => "ARRAY", SIZE_IS => "mysize" }, { TYPE => "DATA", DATA_TYPE => "int" } ]};
335 $x->ParseOutputArgument($fn, $e);
336 is($x->{res}, "memcpy(foo, r.out.foo, (mysize) * sizeof(*foo));\n");