r10490: - allow deferred irpc replies to set the status
[jra/samba/.git] / source4 / torture / local / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "lib/messaging/irpc.h"
26 #include "librpc/gen_ndr/ndr_echo.h"
27
28 const uint32_t MSG_ID1 = 1, MSG_ID2 = 2;
29
30 static BOOL test_debug;
31
32 /*
33   serve up AddOne over the irpc system
34 */
35 static NTSTATUS irpc_AddOne(struct irpc_message *irpc, struct echo_AddOne *r)
36 {
37         *r->out.out_data = r->in.in_data + 1;
38         if (test_debug) {
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);
41         }
42         return NT_STATUS_OK;
43 }
44
45 /*
46   a deferred reply to echodata
47 */
48 static void deferred_echodata(struct event_context *ev, struct timed_event *te, 
49                               struct timeval t, void *private)
50 {
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);
56         }
57         printf("sending deferred reply\n");
58         irpc_send_reply(irpc, NT_STATUS_OK);
59 }
60
61
62 /*
63   serve up EchoData over the irpc system
64 */
65 static NTSTATUS irpc_EchoData(struct irpc_message *irpc, struct echo_EchoData *r)
66 {
67         irpc->defer_reply = True;
68         event_add_timed(irpc->ev, irpc, timeval_zero(), deferred_echodata, irpc);
69         return NT_STATUS_OK;
70 }
71
72
73 /*
74   test a addone call over the internal messaging system
75 */
76 static BOOL test_addone(TALLOC_CTX *mem_ctx, 
77                         struct messaging_context *msg_ctx1,
78                         struct messaging_context *msg_ctx2,
79                         uint32_t value)
80 {
81         struct echo_AddOne r;
82         NTSTATUS status;
83
84         /* make the call */
85         r.in.in_data = value;
86
87         test_debug = True;
88         status = IRPC_CALL(msg_ctx1, MSG_ID2, rpcecho, ECHO_ADDONE, &r, mem_ctx);
89         test_debug = False;
90         if (!NT_STATUS_IS_OK(status)) {
91                 printf("AddOne failed - %s\n", nt_errstr(status));
92                 return False;
93         }
94
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);
99                 return False;
100         }
101
102         printf("%u + 1 = %u\n", r.in.in_data, *r.out.out_data);
103
104         return True;    
105 }
106
107 /*
108   test a echodata call over the internal messaging system
109 */
110 static BOOL test_echodata(TALLOC_CTX *mem_ctx, 
111                           struct messaging_context *msg_ctx1,
112                           struct messaging_context *msg_ctx2)
113 {
114         struct echo_EchoData r;
115         NTSTATUS status;
116
117         /* make the call */
118         r.in.in_data = (unsigned char *)talloc_strdup(mem_ctx, "0123456789");
119         r.in.len = strlen((char *)r.in.in_data);
120
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));
124                 return False;
125         }
126
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);
131                 return False;
132         }
133
134         printf("Echo '%*.*s' -> '%*.*s'\n", 
135                r.in.len, r.in.len,
136                r.in.in_data,
137                r.in.len, r.in.len,
138                r.out.out_data);
139
140         return True;    
141 }
142
143
144 static void irpc_callback(struct irpc_request *irpc)
145 {
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));
151         }
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);
155         }
156         (*pong_count)++;
157         talloc_free(irpc);
158 }
159
160 /*
161   test echo speed
162 */
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)
167 {
168         int ping_count = 0;
169         int pong_count = 0;
170         BOOL ret = True;
171         struct timeval tv;
172         struct echo_AddOne r;
173         int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
174
175         tv = timeval_current();
176
177         r.in.in_data = 0;
178
179         printf("Sending echo for %d seconds\n", timelimit);
180         while (timeval_elapsed(&tv) < timelimit) {
181                 struct irpc_request *irpc;
182
183                 irpc = IRPC_CALL_SEND(msg_ctx1, MSG_ID2, rpcecho, ECHO_ADDONE, &r, mem_ctx);
184                 if (irpc == NULL) {
185                         printf("AddOne send failed\n");
186                         return False;
187                 }
188
189                 irpc->async.fn = irpc_callback;
190                 irpc->async.private = &pong_count;
191
192                 ping_count++;
193
194                 while (ping_count > pong_count + 20) {
195                         event_loop_once(ev);
196                 }
197         }
198
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) {
202                 event_loop_once(ev);
203         }
204
205         if (ping_count != pong_count) {
206                 printf("ping test failed! received %d, sent %d\n", 
207                        pong_count, ping_count);
208                 ret = False;
209         }
210
211         printf("echo rate of %.0f messages/sec\n", 
212                (ping_count+pong_count)/timeval_elapsed(&tv));
213
214         return ret;
215 }
216
217
218 BOOL torture_local_irpc(void) 
219 {
220         TALLOC_CTX *mem_ctx = talloc_init("torture_local_irpc");
221         BOOL ret = True;
222         struct messaging_context *msg_ctx1, *msg_ctx2;
223         struct event_context *ev;
224
225         lp_set_cmdline("lock dir", "lockdir.tmp");
226
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);
230
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);
234
235         IRPC_REGISTER(msg_ctx1, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
236         IRPC_REGISTER(msg_ctx2, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
237
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);
245
246         talloc_free(mem_ctx);
247
248         return ret;
249 }