r19339: Merge my 4.0-unittest branch. This adds an API for more fine-grained
[kai/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 #include "torture/torture.h"
28
29 const uint32_t MSG_ID1 = 1, MSG_ID2 = 2;
30
31 static BOOL test_debug;
32
33 struct irpc_test_data
34 {
35         struct messaging_context *msg_ctx1, *msg_ctx2;
36         struct event_context *ev;
37 };
38
39 /*
40   serve up AddOne over the irpc system
41 */
42 static NTSTATUS irpc_AddOne(struct irpc_message *irpc, struct echo_AddOne *r)
43 {
44         *r->out.out_data = r->in.in_data + 1;
45         if (test_debug) {
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);
48         }
49         return NT_STATUS_OK;
50 }
51
52 /*
53   a deferred reply to echodata
54 */
55 static void deferred_echodata(struct event_context *ev, struct timed_event *te, 
56                               struct timeval t, void *private)
57 {
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);
63         }
64         printf("sending deferred reply\n");
65         irpc_send_reply(irpc, NT_STATUS_OK);
66 }
67
68
69 /*
70   serve up EchoData over the irpc system
71 */
72 static NTSTATUS irpc_EchoData(struct irpc_message *irpc, struct echo_EchoData *r)
73 {
74         irpc->defer_reply = True;
75         event_add_timed(irpc->ev, irpc, timeval_zero(), deferred_echodata, irpc);
76         return NT_STATUS_OK;
77 }
78
79
80 /*
81   test a addone call over the internal messaging system
82 */
83 static bool test_addone(struct torture_context *test, const void *_data,
84                         const void *_value)
85 {
86         struct echo_AddOne r;
87         NTSTATUS status;
88         const struct irpc_test_data *data = _data;
89         uint32_t value = (uint32_t)_value;
90
91         /* make the call */
92         r.in.in_data = value;
93
94         test_debug = True;
95         status = IRPC_CALL(data->msg_ctx1, MSG_ID2, rpcecho, ECHO_ADDONE, &r, test);
96         test_debug = False;
97         torture_assert_ntstatus_ok(test, status, "AddOne failed");
98
99         /* check the answer */
100         torture_assert(test, *r.out.out_data == r.in.in_data + 1, 
101                                    "AddOne wrong answer");
102
103         torture_comment(test, "%u + 1 = %u\n", r.in.in_data, *r.out.out_data);
104         return true;
105 }
106
107 /*
108   test a echodata call over the internal messaging system
109 */
110 static bool test_echodata(struct torture_context *tctx,
111                                                   const void *tcase_data,
112                                                   const void *test_data)
113 {
114         struct echo_EchoData r;
115         NTSTATUS status;
116         const struct irpc_test_data *data = tcase_data;
117         TALLOC_CTX *mem_ctx = tctx;
118
119         /* make the call */
120         r.in.in_data = (unsigned char *)talloc_strdup(mem_ctx, "0123456789");
121         r.in.len = strlen((char *)r.in.in_data);
122
123         status = IRPC_CALL(data->msg_ctx1, MSG_ID2, rpcecho, ECHO_ECHODATA, &r, 
124                                            mem_ctx);
125         torture_assert_ntstatus_ok(tctx, status, "EchoData failed");
126
127         /* check the answer */
128         if (memcmp(r.out.out_data, r.in.in_data, r.in.len) != 0) {
129                 NDR_PRINT_OUT_DEBUG(echo_EchoData, &r);
130                 torture_fail(tctx, "EchoData wrong answer");
131         }
132
133         torture_comment(tctx, "Echo '%*.*s' -> '%*.*s'\n", 
134                r.in.len, r.in.len,
135                r.in.in_data,
136                r.in.len, r.in.len,
137                r.out.out_data);
138         return true;
139 }
140
141
142 static void irpc_callback(struct irpc_request *irpc)
143 {
144         struct echo_AddOne *r = irpc->r;
145         int *pong_count = (int *)irpc->async.private;
146         NTSTATUS status = irpc_call_recv(irpc);
147         if (!NT_STATUS_IS_OK(status)) {
148                 printf("irpc call failed - %s\n", nt_errstr(status));
149         }
150         if (*r->out.out_data != r->in.in_data + 1) {
151                 printf("AddOne wrong answer - %u + 1 = %u should be %u\n", 
152                        r->in.in_data, *r->out.out_data, r->in.in_data+1);
153         }
154         (*pong_count)++;
155 }
156
157 /*
158   test echo speed
159 */
160 static bool test_speed(struct torture_context *tctx,
161                                            const void *tcase_data,
162                                            const void *test_data)
163 {
164         int ping_count = 0;
165         int pong_count = 0;
166         const struct irpc_test_data *data = tcase_data;
167         struct timeval tv;
168         struct echo_AddOne r;
169         TALLOC_CTX *mem_ctx = tctx;
170         int timelimit = torture_setting_int(tctx, "timelimit", 10);
171
172         tv = timeval_current();
173
174         r.in.in_data = 0;
175
176         torture_comment(tctx, "Sending echo for %d seconds\n", timelimit);
177         while (timeval_elapsed(&tv) < timelimit) {
178                 struct irpc_request *irpc;
179
180                 irpc = IRPC_CALL_SEND(data->msg_ctx1, MSG_ID2, rpcecho, ECHO_ADDONE, 
181                                                           &r, mem_ctx);
182                 torture_assert(tctx, irpc != NULL, "AddOne send failed");
183
184                 irpc->async.fn = irpc_callback;
185                 irpc->async.private = &pong_count;
186
187                 ping_count++;
188
189                 while (ping_count > pong_count + 20) {
190                         event_loop_once(data->ev);
191                 }
192         }
193
194         torture_comment(tctx, "waiting for %d remaining replies (done %d)\n", 
195                ping_count - pong_count, pong_count);
196         while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
197                 event_loop_once(data->ev);
198         }
199
200         torture_assert_int_equal(tctx, ping_count, pong_count, "ping test failed");
201
202         torture_comment(tctx, "echo rate of %.0f messages/sec\n", 
203                (ping_count+pong_count)/timeval_elapsed(&tv));
204         return true;
205 }
206
207
208 static BOOL irpc_setup(struct torture_context *tctx, void **_data)
209 {
210         struct irpc_test_data *data;
211
212         *_data = data = talloc(tctx, struct irpc_test_data);
213
214         lp_set_cmdline("lock dir", "lockdir.tmp");
215
216         data->ev = event_context_init(tctx);
217         torture_assert(tctx, data->msg_ctx1 = messaging_init(tctx, MSG_ID1, data->ev),
218                                    "Failed to init first messaging context");
219
220         torture_assert(tctx, data->msg_ctx2 = messaging_init(tctx, MSG_ID2, data->ev),
221                                    "Failed to init second messaging context");
222
223         /* register the server side function */
224         IRPC_REGISTER(data->msg_ctx1, rpcecho, ECHO_ADDONE, irpc_AddOne, NULL);
225         IRPC_REGISTER(data->msg_ctx2, rpcecho, ECHO_ADDONE, irpc_AddOne, NULL);
226
227         IRPC_REGISTER(data->msg_ctx1, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
228         IRPC_REGISTER(data->msg_ctx2, rpcecho, ECHO_ECHODATA, irpc_EchoData, NULL);
229
230         return True;
231 }
232
233 struct torture_suite *torture_local_irpc(TALLOC_CTX *mem_ctx)
234 {
235         struct torture_suite *suite = torture_suite_create(mem_ctx, "IRPC");
236         struct torture_tcase *tcase = torture_suite_add_tcase(suite, "irpc");
237         int i;
238         uint32_t *values = talloc_array(tcase, uint32_t, 5);
239
240         values[0] = 0;
241         values[1] = 0x7FFFFFFE;
242         values[2] = 0xFFFFFFFE;
243         values[3] = 0xFFFFFFFF;
244         values[4] = random() & 0xFFFFFFFF;
245
246         tcase->setup = irpc_setup;
247
248         for (i = 0; i < 5; i++) {
249                 torture_tcase_add_test(tcase, "addone", test_addone, (void *)values[i]);
250         }
251                                                    
252         torture_tcase_add_test(tcase, "echodata", test_echodata, NULL);
253         torture_tcase_add_test(tcase, "speed", test_speed, NULL);
254
255         return suite;
256 }