root/source4/lib/messaging/tests/irpc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. irpc_AddOne
  2. deferred_echodata
  3. irpc_EchoData
  4. test_addone
  5. test_echodata
  6. irpc_callback
  7. test_speed
  8. irpc_setup
  9. torture_local_irpc

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

/* [<][>][^][v][top][bottom][index][help] */