ipc/chromium/src/third_party/libevent/test/regress_rpc.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
michael@0 3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
michael@0 4 *
michael@0 5 * Redistribution and use in source and binary forms, with or without
michael@0 6 * modification, are permitted provided that the following conditions
michael@0 7 * are met:
michael@0 8 * 1. Redistributions of source code must retain the above copyright
michael@0 9 * notice, this list of conditions and the following disclaimer.
michael@0 10 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 11 * notice, this list of conditions and the following disclaimer in the
michael@0 12 * documentation and/or other materials provided with the distribution.
michael@0 13 * 3. The name of the author may not be used to endorse or promote products
michael@0 14 * derived from this software without specific prior written permission.
michael@0 15 *
michael@0 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
michael@0 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
michael@0 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@0 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
michael@0 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
michael@0 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
michael@0 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 26 */
michael@0 27
michael@0 28 /* The old tests here need assertions to work. */
michael@0 29 #undef NDEBUG
michael@0 30
michael@0 31 #ifdef WIN32
michael@0 32 #include <winsock2.h>
michael@0 33 #include <windows.h>
michael@0 34 #endif
michael@0 35
michael@0 36 #include "event2/event-config.h"
michael@0 37
michael@0 38 #include <sys/types.h>
michael@0 39 #include <sys/stat.h>
michael@0 40 #ifdef _EVENT_HAVE_SYS_TIME_H
michael@0 41 #include <sys/time.h>
michael@0 42 #endif
michael@0 43 #include <sys/queue.h>
michael@0 44 #ifndef WIN32
michael@0 45 #include <sys/socket.h>
michael@0 46 #include <signal.h>
michael@0 47 #include <unistd.h>
michael@0 48 #include <netdb.h>
michael@0 49 #endif
michael@0 50 #include <fcntl.h>
michael@0 51 #include <stdlib.h>
michael@0 52 #include <stdio.h>
michael@0 53 #include <string.h>
michael@0 54 #include <errno.h>
michael@0 55 #include <assert.h>
michael@0 56
michael@0 57 #include "event2/buffer.h"
michael@0 58 #include "event2/event.h"
michael@0 59 #include "event2/event_compat.h"
michael@0 60 #include "event2/http.h"
michael@0 61 #include "event2/http_compat.h"
michael@0 62 #include "event2/http_struct.h"
michael@0 63 #include "event2/rpc.h"
michael@0 64 #include "event2/rpc.h"
michael@0 65 #include "event2/rpc_struct.h"
michael@0 66 #include "event2/tag.h"
michael@0 67 #include "log-internal.h"
michael@0 68
michael@0 69 #include "regress.gen.h"
michael@0 70
michael@0 71 #include "regress.h"
michael@0 72 #include "regress_testutils.h"
michael@0 73
michael@0 74 #ifndef NO_PYTHON_EXISTS
michael@0 75
michael@0 76 static struct evhttp *
michael@0 77 http_setup(ev_uint16_t *pport)
michael@0 78 {
michael@0 79 struct evhttp *myhttp;
michael@0 80 ev_uint16_t port;
michael@0 81 struct evhttp_bound_socket *sock;
michael@0 82
michael@0 83 myhttp = evhttp_new(NULL);
michael@0 84 if (!myhttp)
michael@0 85 event_errx(1, "Could not start web server");
michael@0 86
michael@0 87 /* Try a few different ports */
michael@0 88 sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", 0);
michael@0 89 if (!sock)
michael@0 90 event_errx(1, "Couldn't open web port");
michael@0 91
michael@0 92 port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
michael@0 93
michael@0 94 *pport = port;
michael@0 95 return (myhttp);
michael@0 96 }
michael@0 97
michael@0 98 EVRPC_HEADER(Message, msg, kill)
michael@0 99 EVRPC_HEADER(NeverReply, msg, kill)
michael@0 100
michael@0 101 EVRPC_GENERATE(Message, msg, kill)
michael@0 102 EVRPC_GENERATE(NeverReply, msg, kill)
michael@0 103
michael@0 104 static int need_input_hook = 0;
michael@0 105 static int need_output_hook = 0;
michael@0 106
michael@0 107 static void
michael@0 108 MessageCb(EVRPC_STRUCT(Message)* rpc, void *arg)
michael@0 109 {
michael@0 110 struct kill* kill_reply = rpc->reply;
michael@0 111
michael@0 112 if (need_input_hook) {
michael@0 113 struct evhttp_request* req = EVRPC_REQUEST_HTTP(rpc);
michael@0 114 const char *header = evhttp_find_header(
michael@0 115 req->input_headers, "X-Hook");
michael@0 116 assert(header);
michael@0 117 assert(strcmp(header, "input") == 0);
michael@0 118 }
michael@0 119
michael@0 120 /* we just want to fill in some non-sense */
michael@0 121 EVTAG_ASSIGN(kill_reply, weapon, "dagger");
michael@0 122 EVTAG_ASSIGN(kill_reply, action, "wave around like an idiot");
michael@0 123
michael@0 124 /* no reply to the RPC */
michael@0 125 EVRPC_REQUEST_DONE(rpc);
michael@0 126 }
michael@0 127
michael@0 128 static EVRPC_STRUCT(NeverReply) *saved_rpc;
michael@0 129
michael@0 130 static void
michael@0 131 NeverReplyCb(EVRPC_STRUCT(NeverReply)* rpc, void *arg)
michael@0 132 {
michael@0 133 test_ok += 1;
michael@0 134 saved_rpc = rpc;
michael@0 135 }
michael@0 136
michael@0 137 static void
michael@0 138 rpc_setup(struct evhttp **phttp, ev_uint16_t *pport, struct evrpc_base **pbase)
michael@0 139 {
michael@0 140 ev_uint16_t port;
michael@0 141 struct evhttp *http = NULL;
michael@0 142 struct evrpc_base *base = NULL;
michael@0 143
michael@0 144 http = http_setup(&port);
michael@0 145 base = evrpc_init(http);
michael@0 146
michael@0 147 EVRPC_REGISTER(base, Message, msg, kill, MessageCb, NULL);
michael@0 148 EVRPC_REGISTER(base, NeverReply, msg, kill, NeverReplyCb, NULL);
michael@0 149
michael@0 150 *phttp = http;
michael@0 151 *pport = port;
michael@0 152 *pbase = base;
michael@0 153
michael@0 154 need_input_hook = 0;
michael@0 155 need_output_hook = 0;
michael@0 156 }
michael@0 157
michael@0 158 static void
michael@0 159 rpc_teardown(struct evrpc_base *base)
michael@0 160 {
michael@0 161 assert(EVRPC_UNREGISTER(base, Message) == 0);
michael@0 162 assert(EVRPC_UNREGISTER(base, NeverReply) == 0);
michael@0 163
michael@0 164 evrpc_free(base);
michael@0 165 }
michael@0 166
michael@0 167 static void
michael@0 168 rpc_postrequest_failure(struct evhttp_request *req, void *arg)
michael@0 169 {
michael@0 170 if (req->response_code != HTTP_SERVUNAVAIL) {
michael@0 171
michael@0 172 fprintf(stderr, "FAILED (response code)\n");
michael@0 173 exit(1);
michael@0 174 }
michael@0 175
michael@0 176 test_ok = 1;
michael@0 177 event_loopexit(NULL);
michael@0 178 }
michael@0 179
michael@0 180 /*
michael@0 181 * Test a malformed payload submitted as an RPC
michael@0 182 */
michael@0 183
michael@0 184 static void
michael@0 185 rpc_basic_test(void)
michael@0 186 {
michael@0 187 ev_uint16_t port;
michael@0 188 struct evhttp *http = NULL;
michael@0 189 struct evrpc_base *base = NULL;
michael@0 190 struct evhttp_connection *evcon = NULL;
michael@0 191 struct evhttp_request *req = NULL;
michael@0 192
michael@0 193 rpc_setup(&http, &port, &base);
michael@0 194
michael@0 195 evcon = evhttp_connection_new("127.0.0.1", port);
michael@0 196 tt_assert(evcon);
michael@0 197
michael@0 198 /*
michael@0 199 * At this point, we want to schedule an HTTP POST request
michael@0 200 * server using our make request method.
michael@0 201 */
michael@0 202
michael@0 203 req = evhttp_request_new(rpc_postrequest_failure, NULL);
michael@0 204 tt_assert(req);
michael@0 205
michael@0 206 /* Add the information that we care about */
michael@0 207 evhttp_add_header(req->output_headers, "Host", "somehost");
michael@0 208 evbuffer_add_printf(req->output_buffer, "Some Nonsense");
michael@0 209
michael@0 210 if (evhttp_make_request(evcon, req,
michael@0 211 EVHTTP_REQ_POST,
michael@0 212 "/.rpc.Message") == -1) {
michael@0 213 tt_abort();
michael@0 214 }
michael@0 215
michael@0 216 test_ok = 0;
michael@0 217
michael@0 218 event_dispatch();
michael@0 219
michael@0 220 evhttp_connection_free(evcon);
michael@0 221
michael@0 222 rpc_teardown(base);
michael@0 223
michael@0 224 tt_assert(test_ok == 1);
michael@0 225
michael@0 226 end:
michael@0 227 evhttp_free(http);
michael@0 228 }
michael@0 229
michael@0 230 static void
michael@0 231 rpc_postrequest_done(struct evhttp_request *req, void *arg)
michael@0 232 {
michael@0 233 struct kill* kill_reply = NULL;
michael@0 234
michael@0 235 if (req->response_code != HTTP_OK) {
michael@0 236 fprintf(stderr, "FAILED (response code)\n");
michael@0 237 exit(1);
michael@0 238 }
michael@0 239
michael@0 240 kill_reply = kill_new();
michael@0 241
michael@0 242 if ((kill_unmarshal(kill_reply, req->input_buffer)) == -1) {
michael@0 243 fprintf(stderr, "FAILED (unmarshal)\n");
michael@0 244 exit(1);
michael@0 245 }
michael@0 246
michael@0 247 kill_free(kill_reply);
michael@0 248
michael@0 249 test_ok = 1;
michael@0 250 event_loopexit(NULL);
michael@0 251 }
michael@0 252
michael@0 253 static void
michael@0 254 rpc_basic_message(void)
michael@0 255 {
michael@0 256 ev_uint16_t port;
michael@0 257 struct evhttp *http = NULL;
michael@0 258 struct evrpc_base *base = NULL;
michael@0 259 struct evhttp_connection *evcon = NULL;
michael@0 260 struct evhttp_request *req = NULL;
michael@0 261 struct msg *msg;
michael@0 262
michael@0 263 rpc_setup(&http, &port, &base);
michael@0 264
michael@0 265 evcon = evhttp_connection_new("127.0.0.1", port);
michael@0 266 tt_assert(evcon);
michael@0 267
michael@0 268 /*
michael@0 269 * At this point, we want to schedule an HTTP POST request
michael@0 270 * server using our make request method.
michael@0 271 */
michael@0 272
michael@0 273 req = evhttp_request_new(rpc_postrequest_done, NULL);
michael@0 274 if (req == NULL) {
michael@0 275 fprintf(stdout, "FAILED\n");
michael@0 276 exit(1);
michael@0 277 }
michael@0 278
michael@0 279 /* Add the information that we care about */
michael@0 280 evhttp_add_header(req->output_headers, "Host", "somehost");
michael@0 281
michael@0 282 /* set up the basic message */
michael@0 283 msg = msg_new();
michael@0 284 EVTAG_ASSIGN(msg, from_name, "niels");
michael@0 285 EVTAG_ASSIGN(msg, to_name, "tester");
michael@0 286 msg_marshal(req->output_buffer, msg);
michael@0 287 msg_free(msg);
michael@0 288
michael@0 289 if (evhttp_make_request(evcon, req,
michael@0 290 EVHTTP_REQ_POST,
michael@0 291 "/.rpc.Message") == -1) {
michael@0 292 fprintf(stdout, "FAILED\n");
michael@0 293 exit(1);
michael@0 294 }
michael@0 295
michael@0 296 test_ok = 0;
michael@0 297
michael@0 298 event_dispatch();
michael@0 299
michael@0 300 evhttp_connection_free(evcon);
michael@0 301
michael@0 302 rpc_teardown(base);
michael@0 303
michael@0 304 end:
michael@0 305 evhttp_free(http);
michael@0 306 }
michael@0 307
michael@0 308 static struct evrpc_pool *
michael@0 309 rpc_pool_with_connection(ev_uint16_t port)
michael@0 310 {
michael@0 311 struct evhttp_connection *evcon;
michael@0 312 struct evrpc_pool *pool;
michael@0 313
michael@0 314 pool = evrpc_pool_new(NULL);
michael@0 315 assert(pool != NULL);
michael@0 316
michael@0 317 evcon = evhttp_connection_new("127.0.0.1", port);
michael@0 318 assert(evcon != NULL);
michael@0 319
michael@0 320 evrpc_pool_add_connection(pool, evcon);
michael@0 321
michael@0 322 return (pool);
michael@0 323 }
michael@0 324
michael@0 325 static void
michael@0 326 GotKillCb(struct evrpc_status *status,
michael@0 327 struct msg *msg, struct kill *kill, void *arg)
michael@0 328 {
michael@0 329 char *weapon;
michael@0 330 char *action;
michael@0 331
michael@0 332 if (need_output_hook) {
michael@0 333 struct evhttp_request *req = status->http_req;
michael@0 334 const char *header = evhttp_find_header(
michael@0 335 req->input_headers, "X-Pool-Hook");
michael@0 336 assert(header);
michael@0 337 assert(strcmp(header, "ran") == 0);
michael@0 338 }
michael@0 339
michael@0 340 if (status->error != EVRPC_STATUS_ERR_NONE)
michael@0 341 goto done;
michael@0 342
michael@0 343 if (EVTAG_GET(kill, weapon, &weapon) == -1) {
michael@0 344 fprintf(stderr, "get weapon\n");
michael@0 345 goto done;
michael@0 346 }
michael@0 347 if (EVTAG_GET(kill, action, &action) == -1) {
michael@0 348 fprintf(stderr, "get action\n");
michael@0 349 goto done;
michael@0 350 }
michael@0 351
michael@0 352 if (strcmp(weapon, "dagger"))
michael@0 353 goto done;
michael@0 354
michael@0 355 if (strcmp(action, "wave around like an idiot"))
michael@0 356 goto done;
michael@0 357
michael@0 358 test_ok += 1;
michael@0 359
michael@0 360 done:
michael@0 361 event_loopexit(NULL);
michael@0 362 }
michael@0 363
michael@0 364 static void
michael@0 365 GotKillCbTwo(struct evrpc_status *status,
michael@0 366 struct msg *msg, struct kill *kill, void *arg)
michael@0 367 {
michael@0 368 char *weapon;
michael@0 369 char *action;
michael@0 370
michael@0 371 if (status->error != EVRPC_STATUS_ERR_NONE)
michael@0 372 goto done;
michael@0 373
michael@0 374 if (EVTAG_GET(kill, weapon, &weapon) == -1) {
michael@0 375 fprintf(stderr, "get weapon\n");
michael@0 376 goto done;
michael@0 377 }
michael@0 378 if (EVTAG_GET(kill, action, &action) == -1) {
michael@0 379 fprintf(stderr, "get action\n");
michael@0 380 goto done;
michael@0 381 }
michael@0 382
michael@0 383 if (strcmp(weapon, "dagger"))
michael@0 384 goto done;
michael@0 385
michael@0 386 if (strcmp(action, "wave around like an idiot"))
michael@0 387 goto done;
michael@0 388
michael@0 389 test_ok += 1;
michael@0 390
michael@0 391 done:
michael@0 392 if (test_ok == 2)
michael@0 393 event_loopexit(NULL);
michael@0 394 }
michael@0 395
michael@0 396 static int
michael@0 397 rpc_hook_add_header(void *ctx, struct evhttp_request *req,
michael@0 398 struct evbuffer *evbuf, void *arg)
michael@0 399 {
michael@0 400 const char *hook_type = arg;
michael@0 401 if (strcmp("input", hook_type) == 0)
michael@0 402 evhttp_add_header(req->input_headers, "X-Hook", hook_type);
michael@0 403 else
michael@0 404 evhttp_add_header(req->output_headers, "X-Hook", hook_type);
michael@0 405
michael@0 406 assert(evrpc_hook_get_connection(ctx) != NULL);
michael@0 407
michael@0 408 return (EVRPC_CONTINUE);
michael@0 409 }
michael@0 410
michael@0 411 static int
michael@0 412 rpc_hook_add_meta(void *ctx, struct evhttp_request *req,
michael@0 413 struct evbuffer *evbuf, void *arg)
michael@0 414 {
michael@0 415 evrpc_hook_add_meta(ctx, "meta", "test", 5);
michael@0 416
michael@0 417 assert(evrpc_hook_get_connection(ctx) != NULL);
michael@0 418
michael@0 419 return (EVRPC_CONTINUE);
michael@0 420 }
michael@0 421
michael@0 422 static int
michael@0 423 rpc_hook_remove_header(void *ctx, struct evhttp_request *req,
michael@0 424 struct evbuffer *evbuf, void *arg)
michael@0 425 {
michael@0 426 const char *header = evhttp_find_header(req->input_headers, "X-Hook");
michael@0 427 void *data = NULL;
michael@0 428 size_t data_len = 0;
michael@0 429
michael@0 430 assert(header != NULL);
michael@0 431 assert(strcmp(header, arg) == 0);
michael@0 432
michael@0 433 evhttp_remove_header(req->input_headers, "X-Hook");
michael@0 434 evhttp_add_header(req->input_headers, "X-Pool-Hook", "ran");
michael@0 435
michael@0 436 assert(evrpc_hook_find_meta(ctx, "meta", &data, &data_len) == 0);
michael@0 437 assert(data != NULL);
michael@0 438 assert(data_len == 5);
michael@0 439
michael@0 440 assert(evrpc_hook_get_connection(ctx) != NULL);
michael@0 441
michael@0 442 return (EVRPC_CONTINUE);
michael@0 443 }
michael@0 444
michael@0 445 static void
michael@0 446 rpc_basic_client(void)
michael@0 447 {
michael@0 448 ev_uint16_t port;
michael@0 449 struct evhttp *http = NULL;
michael@0 450 struct evrpc_base *base = NULL;
michael@0 451 struct evrpc_pool *pool = NULL;
michael@0 452 struct msg *msg = NULL;
michael@0 453 struct kill *kill = NULL;
michael@0 454
michael@0 455 rpc_setup(&http, &port, &base);
michael@0 456
michael@0 457 need_input_hook = 1;
michael@0 458 need_output_hook = 1;
michael@0 459
michael@0 460 assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_add_header, (void*)"input")
michael@0 461 != NULL);
michael@0 462 assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_add_header, (void*)"output")
michael@0 463 != NULL);
michael@0 464
michael@0 465 pool = rpc_pool_with_connection(port);
michael@0 466
michael@0 467 assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL));
michael@0 468 assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output"));
michael@0 469
michael@0 470 /* set up the basic message */
michael@0 471 msg = msg_new();
michael@0 472 EVTAG_ASSIGN(msg, from_name, "niels");
michael@0 473 EVTAG_ASSIGN(msg, to_name, "tester");
michael@0 474
michael@0 475 kill = kill_new();
michael@0 476
michael@0 477 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
michael@0 478
michael@0 479 test_ok = 0;
michael@0 480
michael@0 481 event_dispatch();
michael@0 482
michael@0 483 tt_assert(test_ok == 1);
michael@0 484
michael@0 485 /* we do it twice to make sure that reuse works correctly */
michael@0 486 kill_clear(kill);
michael@0 487
michael@0 488 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
michael@0 489
michael@0 490 event_dispatch();
michael@0 491
michael@0 492 tt_assert(test_ok == 2);
michael@0 493
michael@0 494 /* we do it trice to make sure other stuff works, too */
michael@0 495 kill_clear(kill);
michael@0 496
michael@0 497 {
michael@0 498 struct evrpc_request_wrapper *ctx =
michael@0 499 EVRPC_MAKE_CTX(Message, msg, kill,
michael@0 500 pool, msg, kill, GotKillCb, NULL);
michael@0 501 evrpc_make_request(ctx);
michael@0 502 }
michael@0 503
michael@0 504 event_dispatch();
michael@0 505
michael@0 506 rpc_teardown(base);
michael@0 507
michael@0 508 tt_assert(test_ok == 3);
michael@0 509
michael@0 510 end:
michael@0 511 if (msg)
michael@0 512 msg_free(msg);
michael@0 513 if (kill)
michael@0 514 kill_free(kill);
michael@0 515
michael@0 516 if (pool)
michael@0 517 evrpc_pool_free(pool);
michael@0 518 if (http)
michael@0 519 evhttp_free(http);
michael@0 520
michael@0 521 need_input_hook = 0;
michael@0 522 need_output_hook = 0;
michael@0 523 }
michael@0 524
michael@0 525 /*
michael@0 526 * We are testing that the second requests gets send over the same
michael@0 527 * connection after the first RPCs completes.
michael@0 528 */
michael@0 529 static void
michael@0 530 rpc_basic_queued_client(void)
michael@0 531 {
michael@0 532 ev_uint16_t port;
michael@0 533 struct evhttp *http = NULL;
michael@0 534 struct evrpc_base *base = NULL;
michael@0 535 struct evrpc_pool *pool = NULL;
michael@0 536 struct msg *msg=NULL;
michael@0 537 struct kill *kill_one=NULL, *kill_two=NULL;
michael@0 538
michael@0 539 rpc_setup(&http, &port, &base);
michael@0 540
michael@0 541 pool = rpc_pool_with_connection(port);
michael@0 542
michael@0 543 /* set up the basic message */
michael@0 544 msg = msg_new();
michael@0 545 EVTAG_ASSIGN(msg, from_name, "niels");
michael@0 546 EVTAG_ASSIGN(msg, to_name, "tester");
michael@0 547
michael@0 548 kill_one = kill_new();
michael@0 549 kill_two = kill_new();
michael@0 550
michael@0 551 EVRPC_MAKE_REQUEST(Message, pool, msg, kill_one, GotKillCbTwo, NULL);
michael@0 552 EVRPC_MAKE_REQUEST(Message, pool, msg, kill_two, GotKillCb, NULL);
michael@0 553
michael@0 554 test_ok = 0;
michael@0 555
michael@0 556 event_dispatch();
michael@0 557
michael@0 558 rpc_teardown(base);
michael@0 559
michael@0 560 tt_assert(test_ok == 2);
michael@0 561
michael@0 562 end:
michael@0 563 if (msg)
michael@0 564 msg_free(msg);
michael@0 565 if (kill_one)
michael@0 566 kill_free(kill_one);
michael@0 567 if (kill_two)
michael@0 568 kill_free(kill_two);
michael@0 569
michael@0 570 if (pool)
michael@0 571 evrpc_pool_free(pool);
michael@0 572 if (http)
michael@0 573 evhttp_free(http);
michael@0 574 }
michael@0 575
michael@0 576 static void
michael@0 577 GotErrorCb(struct evrpc_status *status,
michael@0 578 struct msg *msg, struct kill *kill, void *arg)
michael@0 579 {
michael@0 580 if (status->error != EVRPC_STATUS_ERR_TIMEOUT)
michael@0 581 goto done;
michael@0 582
michael@0 583 /* should never be complete but just to check */
michael@0 584 if (kill_complete(kill) == 0)
michael@0 585 goto done;
michael@0 586
michael@0 587 test_ok += 1;
michael@0 588
michael@0 589 done:
michael@0 590 event_loopexit(NULL);
michael@0 591 }
michael@0 592
michael@0 593 /* we just pause the rpc and continue it in the next callback */
michael@0 594
michael@0 595 struct _rpc_hook_ctx {
michael@0 596 void *vbase;
michael@0 597 void *ctx;
michael@0 598 };
michael@0 599
michael@0 600 static int hook_pause_cb_called=0;
michael@0 601
michael@0 602 static void
michael@0 603 rpc_hook_pause_cb(evutil_socket_t fd, short what, void *arg)
michael@0 604 {
michael@0 605 struct _rpc_hook_ctx *ctx = arg;
michael@0 606 ++hook_pause_cb_called;
michael@0 607 evrpc_resume_request(ctx->vbase, ctx->ctx, EVRPC_CONTINUE);
michael@0 608 free(arg);
michael@0 609 }
michael@0 610
michael@0 611 static int
michael@0 612 rpc_hook_pause(void *ctx, struct evhttp_request *req, struct evbuffer *evbuf,
michael@0 613 void *arg)
michael@0 614 {
michael@0 615 struct _rpc_hook_ctx *tmp = malloc(sizeof(*tmp));
michael@0 616 struct timeval tv;
michael@0 617
michael@0 618 assert(tmp != NULL);
michael@0 619 tmp->vbase = arg;
michael@0 620 tmp->ctx = ctx;
michael@0 621
michael@0 622 memset(&tv, 0, sizeof(tv));
michael@0 623 event_once(-1, EV_TIMEOUT, rpc_hook_pause_cb, tmp, &tv);
michael@0 624 return EVRPC_PAUSE;
michael@0 625 }
michael@0 626
michael@0 627 static void
michael@0 628 rpc_basic_client_with_pause(void)
michael@0 629 {
michael@0 630 ev_uint16_t port;
michael@0 631 struct evhttp *http = NULL;
michael@0 632 struct evrpc_base *base = NULL;
michael@0 633 struct evrpc_pool *pool = NULL;
michael@0 634 struct msg *msg = NULL;
michael@0 635 struct kill *kill= NULL;
michael@0 636
michael@0 637 rpc_setup(&http, &port, &base);
michael@0 638
michael@0 639 assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_pause, base));
michael@0 640 assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base));
michael@0 641
michael@0 642 pool = rpc_pool_with_connection(port);
michael@0 643
michael@0 644 assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool));
michael@0 645 assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool));
michael@0 646
michael@0 647 /* set up the basic message */
michael@0 648 msg = msg_new();
michael@0 649 EVTAG_ASSIGN(msg, from_name, "niels");
michael@0 650 EVTAG_ASSIGN(msg, to_name, "tester");
michael@0 651
michael@0 652 kill = kill_new();
michael@0 653
michael@0 654 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
michael@0 655
michael@0 656 test_ok = 0;
michael@0 657
michael@0 658 event_dispatch();
michael@0 659
michael@0 660 tt_int_op(test_ok, ==, 1);
michael@0 661 tt_int_op(hook_pause_cb_called, ==, 4);
michael@0 662
michael@0 663 end:
michael@0 664 if (base)
michael@0 665 rpc_teardown(base);
michael@0 666
michael@0 667 if (msg)
michael@0 668 msg_free(msg);
michael@0 669 if (kill)
michael@0 670 kill_free(kill);
michael@0 671
michael@0 672 if (pool)
michael@0 673 evrpc_pool_free(pool);
michael@0 674 if (http)
michael@0 675 evhttp_free(http);
michael@0 676 }
michael@0 677
michael@0 678 static void
michael@0 679 rpc_client_timeout(void)
michael@0 680 {
michael@0 681 ev_uint16_t port;
michael@0 682 struct evhttp *http = NULL;
michael@0 683 struct evrpc_base *base = NULL;
michael@0 684 struct evrpc_pool *pool = NULL;
michael@0 685 struct msg *msg = NULL;
michael@0 686 struct kill *kill = NULL;
michael@0 687
michael@0 688 rpc_setup(&http, &port, &base);
michael@0 689
michael@0 690 pool = rpc_pool_with_connection(port);
michael@0 691
michael@0 692 /* set the timeout to 5 seconds */
michael@0 693 evrpc_pool_set_timeout(pool, 5);
michael@0 694
michael@0 695 /* set up the basic message */
michael@0 696 msg = msg_new();
michael@0 697 EVTAG_ASSIGN(msg, from_name, "niels");
michael@0 698 EVTAG_ASSIGN(msg, to_name, "tester");
michael@0 699
michael@0 700 kill = kill_new();
michael@0 701
michael@0 702 EVRPC_MAKE_REQUEST(NeverReply, pool, msg, kill, GotErrorCb, NULL);
michael@0 703
michael@0 704 test_ok = 0;
michael@0 705
michael@0 706 event_dispatch();
michael@0 707
michael@0 708 /* free the saved RPC structure up */
michael@0 709 EVRPC_REQUEST_DONE(saved_rpc);
michael@0 710
michael@0 711 rpc_teardown(base);
michael@0 712
michael@0 713 tt_assert(test_ok == 2);
michael@0 714
michael@0 715 end:
michael@0 716 if (msg)
michael@0 717 msg_free(msg);
michael@0 718 if (kill)
michael@0 719 kill_free(kill);
michael@0 720
michael@0 721 if (pool)
michael@0 722 evrpc_pool_free(pool);
michael@0 723 if (http)
michael@0 724 evhttp_free(http);
michael@0 725 }
michael@0 726
michael@0 727 static void
michael@0 728 rpc_test(void)
michael@0 729 {
michael@0 730 struct msg *msg = NULL, *msg2 = NULL;
michael@0 731 struct kill *attack = NULL;
michael@0 732 struct run *run = NULL;
michael@0 733 struct evbuffer *tmp = evbuffer_new();
michael@0 734 struct timeval tv_start, tv_end;
michael@0 735 ev_uint32_t tag;
michael@0 736 int i;
michael@0 737
michael@0 738 msg = msg_new();
michael@0 739
michael@0 740 tt_assert(msg);
michael@0 741
michael@0 742 EVTAG_ASSIGN(msg, from_name, "niels");
michael@0 743 EVTAG_ASSIGN(msg, to_name, "phoenix");
michael@0 744
michael@0 745 if (EVTAG_GET(msg, attack, &attack) == -1) {
michael@0 746 tt_abort_msg("Failed to set kill message.");
michael@0 747 }
michael@0 748
michael@0 749 EVTAG_ASSIGN(attack, weapon, "feather");
michael@0 750 EVTAG_ASSIGN(attack, action, "tickle");
michael@0 751 for (i = 0; i < 3; ++i) {
michael@0 752 if (EVTAG_ARRAY_ADD_VALUE(attack, how_often, i) == NULL) {
michael@0 753 tt_abort_msg("Failed to add how_often.");
michael@0 754 }
michael@0 755 }
michael@0 756
michael@0 757 evutil_gettimeofday(&tv_start, NULL);
michael@0 758 for (i = 0; i < 1000; ++i) {
michael@0 759 run = EVTAG_ARRAY_ADD(msg, run);
michael@0 760 if (run == NULL) {
michael@0 761 tt_abort_msg("Failed to add run message.");
michael@0 762 }
michael@0 763 EVTAG_ASSIGN(run, how, "very fast but with some data in it");
michael@0 764 EVTAG_ASSIGN(run, fixed_bytes,
michael@0 765 (ev_uint8_t*)"012345678901234567890123");
michael@0 766
michael@0 767 if (EVTAG_ARRAY_ADD_VALUE(
michael@0 768 run, notes, "this is my note") == NULL) {
michael@0 769 tt_abort_msg("Failed to add note.");
michael@0 770 }
michael@0 771 if (EVTAG_ARRAY_ADD_VALUE(run, notes, "pps") == NULL) {
michael@0 772 tt_abort_msg("Failed to add note");
michael@0 773 }
michael@0 774
michael@0 775 EVTAG_ASSIGN(run, large_number, 0xdead0a0bcafebeefLL);
michael@0 776 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xdead0a0b);
michael@0 777 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xbeefcafe);
michael@0 778 }
michael@0 779
michael@0 780 if (msg_complete(msg) == -1)
michael@0 781 tt_abort_msg("Failed to make complete message.");
michael@0 782
michael@0 783 evtag_marshal_msg(tmp, 0xdeaf, msg);
michael@0 784
michael@0 785 if (evtag_peek(tmp, &tag) == -1)
michael@0 786 tt_abort_msg("Failed to peak tag.");
michael@0 787
michael@0 788 if (tag != 0xdeaf)
michael@0 789 TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag));
michael@0 790
michael@0 791 msg2 = msg_new();
michael@0 792 if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1)
michael@0 793 tt_abort_msg("Failed to unmarshal message.");
michael@0 794
michael@0 795 evutil_gettimeofday(&tv_end, NULL);
michael@0 796 evutil_timersub(&tv_end, &tv_start, &tv_end);
michael@0 797 TT_BLATHER(("(%.1f us/add) ",
michael@0 798 (float)tv_end.tv_sec/(float)i * 1000000.0 +
michael@0 799 tv_end.tv_usec / (float)i));
michael@0 800
michael@0 801 if (!EVTAG_HAS(msg2, from_name) ||
michael@0 802 !EVTAG_HAS(msg2, to_name) ||
michael@0 803 !EVTAG_HAS(msg2, attack)) {
michael@0 804 tt_abort_msg("Missing data structures.");
michael@0 805 }
michael@0 806
michael@0 807 if (EVTAG_GET(msg2, attack, &attack) == -1) {
michael@0 808 tt_abort_msg("Could not get attack.");
michael@0 809 }
michael@0 810
michael@0 811 if (EVTAG_ARRAY_LEN(msg2, run) != i) {
michael@0 812 tt_abort_msg("Wrong number of run messages.");
michael@0 813 }
michael@0 814
michael@0 815 /* get the very first run message */
michael@0 816 if (EVTAG_ARRAY_GET(msg2, run, 0, &run) == -1) {
michael@0 817 tt_abort_msg("Failed to get run msg.");
michael@0 818 } else {
michael@0 819 /* verify the notes */
michael@0 820 char *note_one, *note_two;
michael@0 821 ev_uint64_t large_number;
michael@0 822 ev_uint32_t short_number;
michael@0 823
michael@0 824 if (EVTAG_ARRAY_LEN(run, notes) != 2) {
michael@0 825 tt_abort_msg("Wrong number of note strings.");
michael@0 826 }
michael@0 827
michael@0 828 if (EVTAG_ARRAY_GET(run, notes, 0, &note_one) == -1 ||
michael@0 829 EVTAG_ARRAY_GET(run, notes, 1, &note_two) == -1) {
michael@0 830 tt_abort_msg("Could not get note strings.");
michael@0 831 }
michael@0 832
michael@0 833 if (strcmp(note_one, "this is my note") ||
michael@0 834 strcmp(note_two, "pps")) {
michael@0 835 tt_abort_msg("Incorrect note strings encoded.");
michael@0 836 }
michael@0 837
michael@0 838 if (EVTAG_GET(run, large_number, &large_number) == -1 ||
michael@0 839 large_number != 0xdead0a0bcafebeefLL) {
michael@0 840 tt_abort_msg("Incorrrect large_number.");
michael@0 841 }
michael@0 842
michael@0 843 if (EVTAG_ARRAY_LEN(run, other_numbers) != 2) {
michael@0 844 tt_abort_msg("Wrong number of other_numbers.");
michael@0 845 }
michael@0 846
michael@0 847 if (EVTAG_ARRAY_GET(
michael@0 848 run, other_numbers, 0, &short_number) == -1) {
michael@0 849 tt_abort_msg("Could not get short number.");
michael@0 850 }
michael@0 851 tt_uint_op(short_number, ==, 0xdead0a0b);
michael@0 852
michael@0 853 }
michael@0 854 tt_int_op(EVTAG_ARRAY_LEN(attack, how_often), ==, 3);
michael@0 855
michael@0 856 for (i = 0; i < 3; ++i) {
michael@0 857 ev_uint32_t res;
michael@0 858 if (EVTAG_ARRAY_GET(attack, how_often, i, &res) == -1) {
michael@0 859 TT_DIE(("Cannot get %dth how_often msg.", i));
michael@0 860 }
michael@0 861 if ((int)res != i) {
michael@0 862 TT_DIE(("Wrong message encoded %d != %d", i, res));
michael@0 863 }
michael@0 864 }
michael@0 865
michael@0 866 test_ok = 1;
michael@0 867 end:
michael@0 868 if (msg)
michael@0 869 msg_free(msg);
michael@0 870 if (msg2)
michael@0 871 msg_free(msg2);
michael@0 872 if (tmp)
michael@0 873 evbuffer_free(tmp);
michael@0 874 }
michael@0 875
michael@0 876 #define RPC_LEGACY(name) \
michael@0 877 { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \
michael@0 878 &legacy_setup, \
michael@0 879 rpc_##name }
michael@0 880 #else
michael@0 881 /* NO_PYTHON_EXISTS */
michael@0 882
michael@0 883 #define RPC_LEGACY(name) \
michael@0 884 { #name, NULL, TT_SKIP, NULL, NULL }
michael@0 885
michael@0 886 #endif
michael@0 887
michael@0 888 struct testcase_t rpc_testcases[] = {
michael@0 889 RPC_LEGACY(basic_test),
michael@0 890 RPC_LEGACY(basic_message),
michael@0 891 RPC_LEGACY(basic_client),
michael@0 892 RPC_LEGACY(basic_queued_client),
michael@0 893 RPC_LEGACY(basic_client_with_pause),
michael@0 894 RPC_LEGACY(client_timeout),
michael@0 895 RPC_LEGACY(test),
michael@0 896
michael@0 897 END_OF_TESTCASES,
michael@0 898 };

mercurial