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"
28 const uint32_t MSG_ID1 = 1, MSG_ID2 = 2;
30 static BOOL test_debug;
33 serve up AddOne over the irpc system
35 static NTSTATUS irpc_AddOne(struct irpc_message *irpc, struct echo_AddOne *r)
37 *r->out.out_data = r->in.in_data + 1;
39 printf("irpc_AddOne: in=%u in+1=%u out=%u\n",
40 r->in.in_data, r->in.in_data+1, *r->out.out_data);
46 a deferred reply to echodata
48 static void deferred_echodata(struct event_context *ev, struct timed_event *te,
49 struct timeval t, void *private)
51 struct irpc_message *irpc = talloc_get_type(private, struct irpc_message);
52 struct echo_EchoData *r = irpc->data;
53 r->out.out_data = talloc_memdup(r, r->in.in_data, r->in.len);
54 if (r->out.out_data == NULL) {
55 irpc_send_reply(irpc, NT_STATUS_NO_MEMORY);
57 printf("sending deferred reply\n");
58 irpc_send_reply(irpc, NT_STATUS_OK);
63 serve up EchoData over the irpc system
65 static NTSTATUS irpc_EchoData(struct irpc_message *irpc, struct echo_EchoData *r)
67 irpc->defer_reply = True;
68 event_add_timed(irpc->ev, irpc, timeval_zero(), deferred_echodata, irpc);
74 test a addone call over the internal messaging system
76 static BOOL test_addone(TALLOC_CTX *mem_ctx,
77 struct messaging_context *msg_ctx1,
78 struct messaging_context *msg_ctx2,
88 status = IRPC_CALL(msg_ctx1, MSG_ID2, rpcecho, ECHO_ADDONE, &r, mem_ctx);
90 if (!NT_STATUS_IS_OK(status)) {
91 printf("AddOne failed - %s\n", nt_errstr(status));
95 /* check the answer */
96 if (*r.out.out_data != r.in.in_data + 1) {
97 printf("AddOne wrong answer - %u + 1 = %u should be %u\n",
98 r.in.in_data, *r.out.out_data, r.in.in_data+1);
102 printf("%u + 1 = %u\n", r.in.in_data, *r.out.out_data);
108 test a echodata call over the internal messaging system
110 static BOOL test_echodata(TALLOC_CTX *mem_ctx,
111 struct messaging_context *msg_ctx1,
112 struct messaging_context *msg_ctx2)
114 struct echo_EchoData r;
118 r.in.in_data = (unsigned char *)talloc_strdup(mem_ctx, "0123456789");
119 r.in.len = strlen((char *)r.in.in_data);
121 status = IRPC_CALL(msg_ctx1, MSG_ID2, rpcecho, ECHO_ECHODATA, &r, mem_ctx);
122 if (!NT_STATUS_IS_OK(status)) {
123 printf("EchoData failed - %s\n", nt_errstr(status));
127 /* check the answer */
128 if (memcmp(r.out.out_data, r.in.in_data, r.in.len) != 0) {
129 printf("EchoData wrong answer\n");
130 NDR_PRINT_OUT_DEBUG(echo_EchoData, &r);
134 printf("Echo '%*.*s' -> '%*.*s'\n",
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);
163 static BOOL test_speed(TALLOC_CTX *mem_ctx,
164 struct messaging_context *msg_ctx1,
165 struct messaging_context *msg_ctx2,
166 struct event_context *ev)
172 struct echo_AddOne r;
173 int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
175 tv = timeval_current();
179 printf("Sending echo for %d seconds\n", timelimit);
180 while (timeval_elapsed(&tv) < timelimit) {
181 struct irpc_request *irpc;
183 irpc = IRPC_CALL_SEND(msg_ctx1, MSG_ID2, rpcecho, ECHO_ADDONE, &r, mem_ctx);
185 printf("AddOne send failed\n");
189 irpc->async.fn = irpc_callback;
190 irpc->async.private = &pong_count;
194 while (ping_count > pong_count + 20) {
199 printf("waiting for %d remaining replies (done %d)\n",
200 ping_count - pong_count, pong_count);
201 while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
205 if (ping_count != pong_count) {
206 printf("ping test failed! received %d, sent %d\n",
207 pong_count, ping_count);
211 printf("echo rate of %.0f messages/sec\n",
212 (ping_count+pong_count)/timeval_elapsed(&tv));
218 BOOL torture_local_irpc(void)
220 TALLOC_CTX *mem_ctx = talloc_init("torture_local_irpc");
222 struct messaging_context *msg_ctx1, *msg_ctx2;
223 struct event_context *ev;
225 lp_set_cmdline("lock dir", "lockdir.tmp");
227 ev = event_context_init(mem_ctx);
228 msg_ctx1 = messaging_init(mem_ctx, MSG_ID1, ev);
229 msg_ctx2 = messaging_init(mem_ctx, MSG_ID2, ev);
231 /* register the server side function */
232 IRPC_REGISTER(msg_ctx1, rpcecho, ECHO_ADDONE, irpc_AddOne, NULL);
233 IRPC_REGISTER(msg_ctx2, rpcecho, ECHO_ADDONE, irpc_AddOne, NULL);
235 IRPC_REGISTER(msg_ctx1, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
236 IRPC_REGISTER(msg_ctx2, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
238 ret &= test_addone(mem_ctx, msg_ctx1, msg_ctx2, 0);
239 ret &= test_addone(mem_ctx, msg_ctx1, msg_ctx2, 0x7FFFFFFE);
240 ret &= test_addone(mem_ctx, msg_ctx1, msg_ctx2, 0xFFFFFFFE);
241 ret &= test_addone(mem_ctx, msg_ctx1, msg_ctx2, 0xFFFFFFFF);
242 ret &= test_addone(mem_ctx, msg_ctx1, msg_ctx2, random() & 0xFFFFFFFF);
243 ret &= test_echodata(mem_ctx, msg_ctx1, msg_ctx2);
244 ret &= test_speed(mem_ctx, msg_ctx1, msg_ctx2, ev);
246 talloc_free(mem_ctx);