s4:irpc/tests: make use explicit use of the top level event context
[Samba.git] / source4 / lib / messaging / tests / irpc.c
blobd78dc784920ffb9b56b8a2d923c286a1d58648cf
1 /*
2 Unix SMB/CIFS implementation.
4 local test for irpc code
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
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 #include "torture/local/proto.h"
32 const uint32_t MSG_ID1 = 1, MSG_ID2 = 2;
34 static bool test_debug;
36 struct irpc_test_data
38 struct imessaging_context *msg_ctx1, *msg_ctx2;
39 struct tevent_context *ev;
43 serve up AddOne over the irpc system
45 static NTSTATUS irpc_AddOne(struct irpc_message *irpc, struct echo_AddOne *r)
47 *r->out.out_data = r->in.in_data + 1;
48 if (test_debug) {
49 printf("irpc_AddOne: in=%u in+1=%u out=%u\n",
50 r->in.in_data, r->in.in_data+1, *r->out.out_data);
52 return NT_STATUS_OK;
56 a deferred reply to echodata
58 static void deferred_echodata(struct tevent_context *ev, struct tevent_timer *te,
59 struct timeval t, void *private_data)
61 struct irpc_message *irpc = talloc_get_type(private_data, struct irpc_message);
62 struct echo_EchoData *r = (struct echo_EchoData *)irpc->data;
63 r->out.out_data = (uint8_t *)talloc_memdup(r, r->in.in_data, r->in.len);
64 if (r->out.out_data == NULL) {
65 irpc_send_reply(irpc, NT_STATUS_NO_MEMORY);
67 printf("sending deferred reply\n");
68 irpc_send_reply(irpc, NT_STATUS_OK);
73 serve up EchoData over the irpc system
75 static NTSTATUS irpc_EchoData(struct irpc_message *irpc, struct echo_EchoData *r)
77 struct irpc_test_data *data = talloc_get_type_abort(irpc->private_data, struct irpc_test_data);
78 irpc->defer_reply = true;
79 tevent_add_timer(data->ev, irpc, timeval_zero(), deferred_echodata, irpc);
80 return NT_STATUS_OK;
85 test a addone call over the internal messaging system
87 static bool test_addone(struct torture_context *test, const void *_data,
88 const void *_value)
90 struct echo_AddOne r;
91 NTSTATUS status;
92 const struct irpc_test_data *data = (const struct irpc_test_data *)_data;
93 uint32_t value = *(const uint32_t *)_value;
94 struct dcerpc_binding_handle *irpc_handle;
96 irpc_handle = irpc_binding_handle(test, data->msg_ctx1,
97 cluster_id(0, MSG_ID2),
98 &ndr_table_rpcecho);
99 torture_assert(test, irpc_handle, "no memory");
101 /* make the call */
102 r.in.in_data = value;
104 test_debug = true;
105 status = dcerpc_echo_AddOne_r(irpc_handle, test, &r);
106 test_debug = false;
107 torture_assert_ntstatus_ok(test, status, "AddOne failed");
109 /* check the answer */
110 torture_assert(test, *r.out.out_data == r.in.in_data + 1,
111 "AddOne wrong answer");
113 torture_comment(test, "%u + 1 = %u\n", r.in.in_data, *r.out.out_data);
114 return true;
118 test a echodata call over the internal messaging system
120 static bool test_echodata(struct torture_context *tctx,
121 const void *tcase_data,
122 const void *test_data)
124 struct echo_EchoData r;
125 NTSTATUS status;
126 const struct irpc_test_data *data = (const struct irpc_test_data *)tcase_data;
127 TALLOC_CTX *mem_ctx = tctx;
128 struct dcerpc_binding_handle *irpc_handle;
130 irpc_handle = irpc_binding_handle(mem_ctx, data->msg_ctx1,
131 cluster_id(0, MSG_ID2),
132 &ndr_table_rpcecho);
133 torture_assert(tctx, irpc_handle, "no memory");
135 /* make the call */
136 r.in.in_data = (unsigned char *)talloc_strdup(mem_ctx, "0123456789");
137 r.in.len = strlen((char *)r.in.in_data);
139 status = dcerpc_echo_EchoData_r(irpc_handle, mem_ctx, &r);
140 torture_assert_ntstatus_ok(tctx, status, "EchoData failed");
142 /* check the answer */
143 if (memcmp(r.out.out_data, r.in.in_data, r.in.len) != 0) {
144 NDR_PRINT_OUT_DEBUG(echo_EchoData, &r);
145 torture_fail(tctx, "EchoData wrong answer");
148 torture_comment(tctx, "Echo '%*.*s' -> '%*.*s'\n",
149 r.in.len, r.in.len,
150 r.in.in_data,
151 r.in.len, r.in.len,
152 r.out.out_data);
153 return true;
156 struct irpc_callback_state {
157 struct echo_AddOne r;
158 int *pong_count;
161 static void irpc_callback(struct tevent_req *subreq)
163 struct irpc_callback_state *s =
164 tevent_req_callback_data(subreq,
165 struct irpc_callback_state);
166 NTSTATUS status;
168 status = dcerpc_echo_AddOne_r_recv(subreq, s);
169 TALLOC_FREE(subreq);
170 if (!NT_STATUS_IS_OK(status)) {
171 printf("irpc call failed - %s\n", nt_errstr(status));
173 if (*s->r.out.out_data != s->r.in.in_data + 1) {
174 printf("AddOne wrong answer - %u + 1 = %u should be %u\n",
175 s->r.in.in_data, *s->r.out.out_data, s->r.in.in_data+1);
177 (*s->pong_count)++;
181 test echo speed
183 static bool test_speed(struct torture_context *tctx,
184 const void *tcase_data,
185 const void *test_data)
187 int ping_count = 0;
188 int pong_count = 0;
189 const struct irpc_test_data *data = (const struct irpc_test_data *)tcase_data;
190 struct timeval tv;
191 TALLOC_CTX *mem_ctx = tctx;
192 int timelimit = torture_setting_int(tctx, "timelimit", 10);
193 struct dcerpc_binding_handle *irpc_handle;
195 irpc_handle = irpc_binding_handle(mem_ctx, data->msg_ctx1,
196 cluster_id(0, MSG_ID2),
197 &ndr_table_rpcecho);
198 torture_assert(tctx, irpc_handle, "no memory");
200 tv = timeval_current();
202 torture_comment(tctx, "Sending echo for %d seconds\n", timelimit);
203 while (timeval_elapsed(&tv) < timelimit) {
204 struct tevent_req *subreq;
205 struct irpc_callback_state *s;
207 s = talloc_zero(mem_ctx, struct irpc_callback_state);
208 torture_assert(tctx, s != NULL, "no mem");
210 s->pong_count = &pong_count;
212 subreq = dcerpc_echo_AddOne_r_send(mem_ctx,
213 tctx->ev,
214 irpc_handle,
215 &s->r);
216 torture_assert(tctx, subreq != NULL, "AddOne send failed");
218 tevent_req_set_callback(subreq, irpc_callback, s);
220 ping_count++;
222 while (ping_count > pong_count + 20) {
223 tevent_loop_once(data->ev);
227 torture_comment(tctx, "waiting for %d remaining replies (done %d)\n",
228 ping_count - pong_count, pong_count);
229 while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
230 tevent_loop_once(data->ev);
233 torture_assert_int_equal(tctx, ping_count, pong_count, "ping test failed");
235 torture_comment(tctx, "echo rate of %.0f messages/sec\n",
236 (ping_count+pong_count)/timeval_elapsed(&tv));
237 return true;
241 static bool irpc_setup(struct torture_context *tctx, void **_data)
243 struct irpc_test_data *data;
245 *_data = data = talloc(tctx, struct irpc_test_data);
247 lpcfg_set_cmdline(tctx->lp_ctx, "pid directory", "piddir.tmp");
249 data->ev = tctx->ev;
250 torture_assert(tctx, data->msg_ctx1 =
251 imessaging_init(tctx,
252 tctx->lp_ctx,
253 cluster_id(0, MSG_ID1),
254 data->ev, true),
255 "Failed to init first messaging context");
257 torture_assert(tctx, data->msg_ctx2 =
258 imessaging_init(tctx,
259 tctx->lp_ctx,
260 cluster_id(0, MSG_ID2),
261 data->ev, true),
262 "Failed to init second messaging context");
264 /* register the server side function */
265 IRPC_REGISTER(data->msg_ctx1, rpcecho, ECHO_ADDONE, irpc_AddOne, data);
266 IRPC_REGISTER(data->msg_ctx2, rpcecho, ECHO_ADDONE, irpc_AddOne, data);
268 IRPC_REGISTER(data->msg_ctx1, rpcecho, ECHO_ECHODATA, irpc_EchoData, data);
269 IRPC_REGISTER(data->msg_ctx2, rpcecho, ECHO_ECHODATA, irpc_EchoData, data);
271 return true;
274 struct torture_suite *torture_local_irpc(TALLOC_CTX *mem_ctx)
276 struct torture_suite *suite = torture_suite_create(mem_ctx, "irpc");
277 struct torture_tcase *tcase = torture_suite_add_tcase(suite, "irpc");
278 int i;
279 uint32_t *values = talloc_array(tcase, uint32_t, 5);
281 values[0] = 0;
282 values[1] = 0x7FFFFFFE;
283 values[2] = 0xFFFFFFFE;
284 values[3] = 0xFFFFFFFF;
285 values[4] = random() & 0xFFFFFFFF;
287 tcase->setup = irpc_setup;
289 for (i = 0; i < 5; i++) {
290 torture_tcase_add_test_const(tcase, "addone", test_addone,
291 (void *)&values[i]);
294 torture_tcase_add_test_const(tcase, "echodata", test_echodata, NULL);
295 torture_tcase_add_test_const(tcase, "speed", test_speed, NULL);
297 return suite;