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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/events/events.h"
25 #include "lib/messaging/irpc.h"
26 #include "librpc/gen_ndr/ndr_echo.h"
27 #include "torture/torture.h"
29 const uint32_t MSG_ID1 = 1, MSG_ID2 = 2;
31 static BOOL test_debug;
35 struct messaging_context *msg_ctx1, *msg_ctx2;
36 struct event_context *ev;
40 serve up AddOne over the irpc system
42 static NTSTATUS irpc_AddOne(struct irpc_message *irpc, struct echo_AddOne *r)
44 *r->out.out_data = r->in.in_data + 1;
46 printf("irpc_AddOne: in=%u in+1=%u out=%u\n",
47 r->in.in_data, r->in.in_data+1, *r->out.out_data);
53 a deferred reply to echodata
55 static void deferred_echodata(struct event_context *ev, struct timed_event *te,
56 struct timeval t, void *private)
58 struct irpc_message *irpc = talloc_get_type(private, struct irpc_message);
59 struct echo_EchoData *r = irpc->data;
60 r->out.out_data = talloc_memdup(r, r->in.in_data, r->in.len);
61 if (r->out.out_data == NULL) {
62 irpc_send_reply(irpc, NT_STATUS_NO_MEMORY);
64 printf("sending deferred reply\n");
65 irpc_send_reply(irpc, NT_STATUS_OK);
70 serve up EchoData over the irpc system
72 static NTSTATUS irpc_EchoData(struct irpc_message *irpc, struct echo_EchoData *r)
74 irpc->defer_reply = True;
75 event_add_timed(irpc->ev, irpc, timeval_zero(), deferred_echodata, irpc);
81 test a addone call over the internal messaging system
83 static BOOL test_addone(struct torture_context *test, const void *_data,
88 const struct irpc_test_data *data = _data;
89 uint32_t value = (uint32_t)value;
95 status = IRPC_CALL(data->msg_ctx1, MSG_ID2, rpcecho, ECHO_ADDONE, &r, test);
97 torture_assert_ntstatus_ok(test, status, "AddOne failed");
99 /* check the answer */
101 *r.out.out_data == r.in.in_data + 1,
102 "AddOne wrong answer");
104 torture_comment(test, "%u + 1 = %u", r.in.in_data, *r.out.out_data);
110 test a echodata call over the internal messaging system
112 static BOOL test_echodata(struct torture_context *test,
113 const void *_data, const void *_data2)
115 struct echo_EchoData r;
117 const struct irpc_test_data *data = _data;
120 r.in.in_data = (unsigned char *)talloc_strdup(test, "0123456789");
121 r.in.len = strlen((char *)r.in.in_data);
123 status = IRPC_CALL(data->msg_ctx1, MSG_ID2, rpcecho, ECHO_ECHODATA, &r,
125 torture_assert_ntstatus_ok(test, status, "EchoData failed");
127 /* check the answer */
128 if (memcmp(r.out.out_data, r.in.in_data, r.in.len) != 0) {
129 torture_fail(test, "EchoData wrong answer");
130 NDR_PRINT_OUT_DEBUG(echo_EchoData, &r);
134 torture_comment(test, "Echo '%*.*s' -> '%*.*s'",
144 static void irpc_callback(struct irpc_request *irpc)
146 struct echo_AddOne *r = irpc->r;
147 int *pong_count = (int *)irpc->async.private;
148 NTSTATUS status = irpc_call_recv(irpc);
149 if (!NT_STATUS_IS_OK(status)) {
150 printf("irpc call failed - %s\n", nt_errstr(status));
152 if (*r->out.out_data != r->in.in_data + 1) {
153 printf("AddOne wrong answer - %u + 1 = %u should be %u\n",
154 r->in.in_data, *r->out.out_data, r->in.in_data+1);
162 static BOOL test_speed(struct torture_context *test,
168 const struct irpc_test_data *data = _data;
170 struct echo_AddOne r;
171 int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
173 tv = timeval_current();
177 torture_comment(test, "Sending echo for %d seconds", timelimit);
178 while (timeval_elapsed(&tv) < timelimit) {
179 struct irpc_request *irpc;
181 irpc = IRPC_CALL_SEND(data->msg_ctx1, MSG_ID2, rpcecho, ECHO_ADDONE,
183 torture_assert(test, irpc != NULL, "AddOne send failed");
185 irpc->async.fn = irpc_callback;
186 irpc->async.private = &pong_count;
190 while (ping_count > pong_count + 20) {
191 event_loop_once(data->ev);
195 torture_comment(test, "waiting for %d remaining replies (done %d)",
196 ping_count - pong_count, pong_count);
197 while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
198 event_loop_once(data->ev);
201 if (ping_count != pong_count) {
202 torture_fail(test, "ping test failed! received %d, sent %d",
203 pong_count, ping_count);
206 torture_comment(test, "echo rate of %.0f messages/sec",
207 (ping_count+pong_count)/timeval_elapsed(&tv));
213 static BOOL irpc_setup(struct torture_context *test, void **_data)
215 struct irpc_test_data *data;
217 *_data = data = talloc(test, struct irpc_test_data);
219 lp_set_cmdline("lock dir", "lockdir.tmp");
221 data->ev = event_context_init(test);
223 data->msg_ctx1 = messaging_init(test, MSG_ID1, data->ev),
224 "Failed to init first messaging context");
227 data->msg_ctx2 = messaging_init(test, MSG_ID2, data->ev),
228 "Failed to init second messaging context");
230 /* register the server side function */
231 IRPC_REGISTER(data->msg_ctx1, rpcecho, ECHO_ADDONE, irpc_AddOne, NULL);
232 IRPC_REGISTER(data->msg_ctx2, rpcecho, ECHO_ADDONE, irpc_AddOne, NULL);
234 IRPC_REGISTER(data->msg_ctx1, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
235 IRPC_REGISTER(data->msg_ctx2, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
240 struct torture_suite *torture_local_irpc(TALLOC_CTX *mem_ctx)
242 struct torture_suite *suite = torture_suite_create(mem_ctx, "LOCAL-IRPC");
243 struct torture_tcase *tcase = torture_suite_add_tcase(suite, "irpc");
245 uint32_t values[] = {0, 0x7FFFFFFE, 0xFFFFFFFE, 0xFFFFFFFF,
246 random() & 0xFFFFFFFF};
248 tcase->setup = irpc_setup;
250 for (i = 0; i < ARRAY_SIZE(values); i++) {
251 torture_tcase_add_test(tcase, "addone", test_addone, (void *)values[i]);
254 torture_tcase_add_test(tcase, "echodata", test_echodata, NULL);
255 torture_tcase_add_test(tcase, "speed", test_speed, NULL);