Merge commit 'release-4-0-0alpha15' into master4-tmp
[kai/samba-autobuild/.git] / source4 / lib / messaging / tests / irpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local test for irpc code
5
6    Copyright (C) Andrew Tridgell 2004
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "lib/messaging/irpc.h"
25 #include "librpc/gen_ndr/ndr_echo.h"
26 #include "librpc/gen_ndr/ndr_echo_c.h"
27 #include "torture/torture.h"
28 #include "cluster/cluster.h"
29 #include "param/param.h"
30
31 const uint32_t MSG_ID1 = 1, MSG_ID2 = 2;
32
33 static bool test_debug;
34
35 struct irpc_test_data
36 {
37         struct imessaging_context *msg_ctx1, *msg_ctx2;
38         struct tevent_context *ev;
39 };
40
41 /*
42   serve up AddOne over the irpc system
43 */
44 static NTSTATUS irpc_AddOne(struct irpc_message *irpc, struct echo_AddOne *r)
45 {
46         *r->out.out_data = r->in.in_data + 1;
47         if (test_debug) {
48                 printf("irpc_AddOne: in=%u in+1=%u out=%u\n", 
49                         r->in.in_data, r->in.in_data+1, *r->out.out_data);
50         }
51         return NT_STATUS_OK;
52 }
53
54 /*
55   a deferred reply to echodata
56 */
57 static void deferred_echodata(struct tevent_context *ev, struct tevent_timer *te, 
58                               struct timeval t, void *private_data)
59 {
60         struct irpc_message *irpc = talloc_get_type(private_data, struct irpc_message);
61         struct echo_EchoData *r = (struct echo_EchoData *)irpc->data;
62         r->out.out_data = (uint8_t *)talloc_memdup(r, r->in.in_data, r->in.len);
63         if (r->out.out_data == NULL) {
64                 irpc_send_reply(irpc, NT_STATUS_NO_MEMORY);
65         }
66         printf("sending deferred reply\n");
67         irpc_send_reply(irpc, NT_STATUS_OK);
68 }
69
70
71 /*
72   serve up EchoData over the irpc system
73 */
74 static NTSTATUS irpc_EchoData(struct irpc_message *irpc, struct echo_EchoData *r)
75 {
76         irpc->defer_reply = true;
77         event_add_timed(irpc->ev, irpc, timeval_zero(), deferred_echodata, irpc);
78         return NT_STATUS_OK;
79 }
80
81
82 /*
83   test a addone call over the internal messaging system
84 */
85 static bool test_addone(struct torture_context *test, const void *_data,
86                         const void *_value)
87 {
88         struct echo_AddOne r;
89         NTSTATUS status;
90         const struct irpc_test_data *data = (const struct irpc_test_data *)_data;
91         uint32_t value = *(const uint32_t *)_value;
92         struct dcerpc_binding_handle *irpc_handle;
93
94         irpc_handle = irpc_binding_handle(test, data->msg_ctx1,
95                                           cluster_id(0, MSG_ID2),
96                                           &ndr_table_rpcecho);
97         torture_assert(test, irpc_handle, "no memory");
98
99         /* make the call */
100         r.in.in_data = value;
101
102         test_debug = true;
103         status = dcerpc_echo_AddOne_r(irpc_handle, test, &r);
104         test_debug = false;
105         torture_assert_ntstatus_ok(test, status, "AddOne failed");
106
107         /* check the answer */
108         torture_assert(test, *r.out.out_data == r.in.in_data + 1, 
109                                    "AddOne wrong answer");
110
111         torture_comment(test, "%u + 1 = %u\n", r.in.in_data, *r.out.out_data);
112         return true;
113 }
114
115 /*
116   test a echodata call over the internal messaging system
117 */
118 static bool test_echodata(struct torture_context *tctx,
119                                                   const void *tcase_data,
120                                                   const void *test_data)
121 {
122         struct echo_EchoData r;
123         NTSTATUS status;
124         const struct irpc_test_data *data = (const struct irpc_test_data *)tcase_data;
125         TALLOC_CTX *mem_ctx = tctx;
126         struct dcerpc_binding_handle *irpc_handle;
127
128         irpc_handle = irpc_binding_handle(mem_ctx, data->msg_ctx1,
129                                           cluster_id(0, MSG_ID2),
130                                           &ndr_table_rpcecho);
131         torture_assert(tctx, irpc_handle, "no memory");
132
133         /* make the call */
134         r.in.in_data = (unsigned char *)talloc_strdup(mem_ctx, "0123456789");
135         r.in.len = strlen((char *)r.in.in_data);
136
137         status = dcerpc_echo_EchoData_r(irpc_handle, mem_ctx, &r);
138         torture_assert_ntstatus_ok(tctx, status, "EchoData failed");
139
140         /* check the answer */
141         if (memcmp(r.out.out_data, r.in.in_data, r.in.len) != 0) {
142                 NDR_PRINT_OUT_DEBUG(echo_EchoData, &r);
143                 torture_fail(tctx, "EchoData wrong answer");
144         }
145
146         torture_comment(tctx, "Echo '%*.*s' -> '%*.*s'\n", 
147                r.in.len, r.in.len,
148                r.in.in_data,
149                r.in.len, r.in.len,
150                r.out.out_data);
151         return true;
152 }
153
154 struct irpc_callback_state {
155         struct echo_AddOne r;
156         int *pong_count;
157 };
158
159 static void irpc_callback(struct tevent_req *subreq)
160 {
161         struct irpc_callback_state *s =
162                 tevent_req_callback_data(subreq,
163                 struct irpc_callback_state);
164         NTSTATUS status;
165
166         status = dcerpc_echo_AddOne_r_recv(subreq, s);
167         TALLOC_FREE(subreq);
168         if (!NT_STATUS_IS_OK(status)) {
169                 printf("irpc call failed - %s\n", nt_errstr(status));
170         }
171         if (*s->r.out.out_data != s->r.in.in_data + 1) {
172                 printf("AddOne wrong answer - %u + 1 = %u should be %u\n", 
173                        s->r.in.in_data, *s->r.out.out_data, s->r.in.in_data+1);
174         }
175         (*s->pong_count)++;
176 }
177
178 /*
179   test echo speed
180 */
181 static bool test_speed(struct torture_context *tctx,
182                                            const void *tcase_data,
183                                            const void *test_data)
184 {
185         int ping_count = 0;
186         int pong_count = 0;
187         const struct irpc_test_data *data = (const struct irpc_test_data *)tcase_data;
188         struct timeval tv;
189         TALLOC_CTX *mem_ctx = tctx;
190         int timelimit = torture_setting_int(tctx, "timelimit", 10);
191         struct dcerpc_binding_handle *irpc_handle;
192
193         irpc_handle = irpc_binding_handle(mem_ctx, data->msg_ctx1,
194                                           cluster_id(0, MSG_ID2),
195                                           &ndr_table_rpcecho);
196         torture_assert(tctx, irpc_handle, "no memory");
197
198         tv = timeval_current();
199
200         torture_comment(tctx, "Sending echo for %d seconds\n", timelimit);
201         while (timeval_elapsed(&tv) < timelimit) {
202                 struct tevent_req *subreq;
203                 struct irpc_callback_state *s;
204
205                 s = talloc_zero(mem_ctx, struct irpc_callback_state);
206                 torture_assert(tctx, s != NULL, "no mem");
207
208                 s->pong_count = &pong_count;
209
210                 subreq = dcerpc_echo_AddOne_r_send(mem_ctx,
211                                                    tctx->ev,
212                                                    irpc_handle,
213                                                    &s->r);
214                 torture_assert(tctx, subreq != NULL, "AddOne send failed");
215
216                 tevent_req_set_callback(subreq, irpc_callback, s);
217
218                 ping_count++;
219
220                 while (ping_count > pong_count + 20) {
221                         event_loop_once(data->ev);
222                 }
223         }
224
225         torture_comment(tctx, "waiting for %d remaining replies (done %d)\n", 
226                ping_count - pong_count, pong_count);
227         while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
228                 event_loop_once(data->ev);
229         }
230
231         torture_assert_int_equal(tctx, ping_count, pong_count, "ping test failed");
232
233         torture_comment(tctx, "echo rate of %.0f messages/sec\n", 
234                (ping_count+pong_count)/timeval_elapsed(&tv));
235         return true;
236 }
237
238
239 static bool irpc_setup(struct torture_context *tctx, void **_data)
240 {
241         struct irpc_test_data *data;
242
243         *_data = data = talloc(tctx, struct irpc_test_data);
244
245         lpcfg_set_cmdline(tctx->lp_ctx, "pid directory", "piddir.tmp");
246
247         data->ev = tctx->ev;
248         torture_assert(tctx, data->msg_ctx1 = 
249                        imessaging_init(tctx,
250                                       lpcfg_imessaging_path(tctx, tctx->lp_ctx),
251                                       cluster_id(0, MSG_ID1),
252                                       data->ev),
253                        "Failed to init first messaging context");
254
255         torture_assert(tctx, data->msg_ctx2 = 
256                        imessaging_init(tctx,
257                                       lpcfg_imessaging_path(tctx, tctx->lp_ctx),
258                                       cluster_id(0, MSG_ID2), 
259                                       data->ev),
260                        "Failed to init second messaging context");
261
262         /* register the server side function */
263         IRPC_REGISTER(data->msg_ctx1, rpcecho, ECHO_ADDONE, irpc_AddOne, NULL);
264         IRPC_REGISTER(data->msg_ctx2, rpcecho, ECHO_ADDONE, irpc_AddOne, NULL);
265
266         IRPC_REGISTER(data->msg_ctx1, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
267         IRPC_REGISTER(data->msg_ctx2, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
268
269         return true;
270 }
271
272 struct torture_suite *torture_local_irpc(TALLOC_CTX *mem_ctx)
273 {
274         struct torture_suite *suite = torture_suite_create(mem_ctx, "irpc");
275         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "irpc");
276         int i;
277         uint32_t *values = talloc_array(tcase, uint32_t, 5);
278
279         values[0] = 0;
280         values[1] = 0x7FFFFFFE;
281         values[2] = 0xFFFFFFFE;
282         values[3] = 0xFFFFFFFF;
283         values[4] = random() & 0xFFFFFFFF;
284
285         tcase->setup = irpc_setup;
286
287         for (i = 0; i < 5; i++) {
288                 torture_tcase_add_test_const(tcase, "addone", test_addone,
289                                 (void *)&values[i]);
290         }
291
292         torture_tcase_add_test_const(tcase, "echodata", test_echodata, NULL);
293         torture_tcase_add_test_const(tcase, "speed", test_speed, NULL);
294
295         return suite;
296 }