michael@0: /* michael@0: * Copyright (c) 2003-2007 Niels Provos michael@0: * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: /* The old tests here need assertions to work. */ michael@0: #undef NDEBUG michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include "event2/event-config.h" michael@0: michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_TIME_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifndef WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "event2/buffer.h" michael@0: #include "event2/event.h" michael@0: #include "event2/event_compat.h" michael@0: #include "event2/http.h" michael@0: #include "event2/http_compat.h" michael@0: #include "event2/http_struct.h" michael@0: #include "event2/rpc.h" michael@0: #include "event2/rpc.h" michael@0: #include "event2/rpc_struct.h" michael@0: #include "event2/tag.h" michael@0: #include "log-internal.h" michael@0: michael@0: #include "regress.gen.h" michael@0: michael@0: #include "regress.h" michael@0: #include "regress_testutils.h" michael@0: michael@0: #ifndef NO_PYTHON_EXISTS michael@0: michael@0: static struct evhttp * michael@0: http_setup(ev_uint16_t *pport) michael@0: { michael@0: struct evhttp *myhttp; michael@0: ev_uint16_t port; michael@0: struct evhttp_bound_socket *sock; michael@0: michael@0: myhttp = evhttp_new(NULL); michael@0: if (!myhttp) michael@0: event_errx(1, "Could not start web server"); michael@0: michael@0: /* Try a few different ports */ michael@0: sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", 0); michael@0: if (!sock) michael@0: event_errx(1, "Couldn't open web port"); michael@0: michael@0: port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock)); michael@0: michael@0: *pport = port; michael@0: return (myhttp); michael@0: } michael@0: michael@0: EVRPC_HEADER(Message, msg, kill) michael@0: EVRPC_HEADER(NeverReply, msg, kill) michael@0: michael@0: EVRPC_GENERATE(Message, msg, kill) michael@0: EVRPC_GENERATE(NeverReply, msg, kill) michael@0: michael@0: static int need_input_hook = 0; michael@0: static int need_output_hook = 0; michael@0: michael@0: static void michael@0: MessageCb(EVRPC_STRUCT(Message)* rpc, void *arg) michael@0: { michael@0: struct kill* kill_reply = rpc->reply; michael@0: michael@0: if (need_input_hook) { michael@0: struct evhttp_request* req = EVRPC_REQUEST_HTTP(rpc); michael@0: const char *header = evhttp_find_header( michael@0: req->input_headers, "X-Hook"); michael@0: assert(header); michael@0: assert(strcmp(header, "input") == 0); michael@0: } michael@0: michael@0: /* we just want to fill in some non-sense */ michael@0: EVTAG_ASSIGN(kill_reply, weapon, "dagger"); michael@0: EVTAG_ASSIGN(kill_reply, action, "wave around like an idiot"); michael@0: michael@0: /* no reply to the RPC */ michael@0: EVRPC_REQUEST_DONE(rpc); michael@0: } michael@0: michael@0: static EVRPC_STRUCT(NeverReply) *saved_rpc; michael@0: michael@0: static void michael@0: NeverReplyCb(EVRPC_STRUCT(NeverReply)* rpc, void *arg) michael@0: { michael@0: test_ok += 1; michael@0: saved_rpc = rpc; michael@0: } michael@0: michael@0: static void michael@0: rpc_setup(struct evhttp **phttp, ev_uint16_t *pport, struct evrpc_base **pbase) michael@0: { michael@0: ev_uint16_t port; michael@0: struct evhttp *http = NULL; michael@0: struct evrpc_base *base = NULL; michael@0: michael@0: http = http_setup(&port); michael@0: base = evrpc_init(http); michael@0: michael@0: EVRPC_REGISTER(base, Message, msg, kill, MessageCb, NULL); michael@0: EVRPC_REGISTER(base, NeverReply, msg, kill, NeverReplyCb, NULL); michael@0: michael@0: *phttp = http; michael@0: *pport = port; michael@0: *pbase = base; michael@0: michael@0: need_input_hook = 0; michael@0: need_output_hook = 0; michael@0: } michael@0: michael@0: static void michael@0: rpc_teardown(struct evrpc_base *base) michael@0: { michael@0: assert(EVRPC_UNREGISTER(base, Message) == 0); michael@0: assert(EVRPC_UNREGISTER(base, NeverReply) == 0); michael@0: michael@0: evrpc_free(base); michael@0: } michael@0: michael@0: static void michael@0: rpc_postrequest_failure(struct evhttp_request *req, void *arg) michael@0: { michael@0: if (req->response_code != HTTP_SERVUNAVAIL) { michael@0: michael@0: fprintf(stderr, "FAILED (response code)\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: test_ok = 1; michael@0: event_loopexit(NULL); michael@0: } michael@0: michael@0: /* michael@0: * Test a malformed payload submitted as an RPC michael@0: */ michael@0: michael@0: static void michael@0: rpc_basic_test(void) michael@0: { michael@0: ev_uint16_t port; michael@0: struct evhttp *http = NULL; michael@0: struct evrpc_base *base = NULL; michael@0: struct evhttp_connection *evcon = NULL; michael@0: struct evhttp_request *req = NULL; michael@0: michael@0: rpc_setup(&http, &port, &base); michael@0: michael@0: evcon = evhttp_connection_new("127.0.0.1", port); michael@0: tt_assert(evcon); michael@0: michael@0: /* michael@0: * At this point, we want to schedule an HTTP POST request michael@0: * server using our make request method. michael@0: */ michael@0: michael@0: req = evhttp_request_new(rpc_postrequest_failure, NULL); michael@0: tt_assert(req); michael@0: michael@0: /* Add the information that we care about */ michael@0: evhttp_add_header(req->output_headers, "Host", "somehost"); michael@0: evbuffer_add_printf(req->output_buffer, "Some Nonsense"); michael@0: michael@0: if (evhttp_make_request(evcon, req, michael@0: EVHTTP_REQ_POST, michael@0: "/.rpc.Message") == -1) { michael@0: tt_abort(); michael@0: } michael@0: michael@0: test_ok = 0; michael@0: michael@0: event_dispatch(); michael@0: michael@0: evhttp_connection_free(evcon); michael@0: michael@0: rpc_teardown(base); michael@0: michael@0: tt_assert(test_ok == 1); michael@0: michael@0: end: michael@0: evhttp_free(http); michael@0: } michael@0: michael@0: static void michael@0: rpc_postrequest_done(struct evhttp_request *req, void *arg) michael@0: { michael@0: struct kill* kill_reply = NULL; michael@0: michael@0: if (req->response_code != HTTP_OK) { michael@0: fprintf(stderr, "FAILED (response code)\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: kill_reply = kill_new(); michael@0: michael@0: if ((kill_unmarshal(kill_reply, req->input_buffer)) == -1) { michael@0: fprintf(stderr, "FAILED (unmarshal)\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: kill_free(kill_reply); michael@0: michael@0: test_ok = 1; michael@0: event_loopexit(NULL); michael@0: } michael@0: michael@0: static void michael@0: rpc_basic_message(void) michael@0: { michael@0: ev_uint16_t port; michael@0: struct evhttp *http = NULL; michael@0: struct evrpc_base *base = NULL; michael@0: struct evhttp_connection *evcon = NULL; michael@0: struct evhttp_request *req = NULL; michael@0: struct msg *msg; michael@0: michael@0: rpc_setup(&http, &port, &base); michael@0: michael@0: evcon = evhttp_connection_new("127.0.0.1", port); michael@0: tt_assert(evcon); michael@0: michael@0: /* michael@0: * At this point, we want to schedule an HTTP POST request michael@0: * server using our make request method. michael@0: */ michael@0: michael@0: req = evhttp_request_new(rpc_postrequest_done, NULL); michael@0: if (req == NULL) { michael@0: fprintf(stdout, "FAILED\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: /* Add the information that we care about */ michael@0: evhttp_add_header(req->output_headers, "Host", "somehost"); michael@0: michael@0: /* set up the basic message */ michael@0: msg = msg_new(); michael@0: EVTAG_ASSIGN(msg, from_name, "niels"); michael@0: EVTAG_ASSIGN(msg, to_name, "tester"); michael@0: msg_marshal(req->output_buffer, msg); michael@0: msg_free(msg); michael@0: michael@0: if (evhttp_make_request(evcon, req, michael@0: EVHTTP_REQ_POST, michael@0: "/.rpc.Message") == -1) { michael@0: fprintf(stdout, "FAILED\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: test_ok = 0; michael@0: michael@0: event_dispatch(); michael@0: michael@0: evhttp_connection_free(evcon); michael@0: michael@0: rpc_teardown(base); michael@0: michael@0: end: michael@0: evhttp_free(http); michael@0: } michael@0: michael@0: static struct evrpc_pool * michael@0: rpc_pool_with_connection(ev_uint16_t port) michael@0: { michael@0: struct evhttp_connection *evcon; michael@0: struct evrpc_pool *pool; michael@0: michael@0: pool = evrpc_pool_new(NULL); michael@0: assert(pool != NULL); michael@0: michael@0: evcon = evhttp_connection_new("127.0.0.1", port); michael@0: assert(evcon != NULL); michael@0: michael@0: evrpc_pool_add_connection(pool, evcon); michael@0: michael@0: return (pool); michael@0: } michael@0: michael@0: static void michael@0: GotKillCb(struct evrpc_status *status, michael@0: struct msg *msg, struct kill *kill, void *arg) michael@0: { michael@0: char *weapon; michael@0: char *action; michael@0: michael@0: if (need_output_hook) { michael@0: struct evhttp_request *req = status->http_req; michael@0: const char *header = evhttp_find_header( michael@0: req->input_headers, "X-Pool-Hook"); michael@0: assert(header); michael@0: assert(strcmp(header, "ran") == 0); michael@0: } michael@0: michael@0: if (status->error != EVRPC_STATUS_ERR_NONE) michael@0: goto done; michael@0: michael@0: if (EVTAG_GET(kill, weapon, &weapon) == -1) { michael@0: fprintf(stderr, "get weapon\n"); michael@0: goto done; michael@0: } michael@0: if (EVTAG_GET(kill, action, &action) == -1) { michael@0: fprintf(stderr, "get action\n"); michael@0: goto done; michael@0: } michael@0: michael@0: if (strcmp(weapon, "dagger")) michael@0: goto done; michael@0: michael@0: if (strcmp(action, "wave around like an idiot")) michael@0: goto done; michael@0: michael@0: test_ok += 1; michael@0: michael@0: done: michael@0: event_loopexit(NULL); michael@0: } michael@0: michael@0: static void michael@0: GotKillCbTwo(struct evrpc_status *status, michael@0: struct msg *msg, struct kill *kill, void *arg) michael@0: { michael@0: char *weapon; michael@0: char *action; michael@0: michael@0: if (status->error != EVRPC_STATUS_ERR_NONE) michael@0: goto done; michael@0: michael@0: if (EVTAG_GET(kill, weapon, &weapon) == -1) { michael@0: fprintf(stderr, "get weapon\n"); michael@0: goto done; michael@0: } michael@0: if (EVTAG_GET(kill, action, &action) == -1) { michael@0: fprintf(stderr, "get action\n"); michael@0: goto done; michael@0: } michael@0: michael@0: if (strcmp(weapon, "dagger")) michael@0: goto done; michael@0: michael@0: if (strcmp(action, "wave around like an idiot")) michael@0: goto done; michael@0: michael@0: test_ok += 1; michael@0: michael@0: done: michael@0: if (test_ok == 2) michael@0: event_loopexit(NULL); michael@0: } michael@0: michael@0: static int michael@0: rpc_hook_add_header(void *ctx, struct evhttp_request *req, michael@0: struct evbuffer *evbuf, void *arg) michael@0: { michael@0: const char *hook_type = arg; michael@0: if (strcmp("input", hook_type) == 0) michael@0: evhttp_add_header(req->input_headers, "X-Hook", hook_type); michael@0: else michael@0: evhttp_add_header(req->output_headers, "X-Hook", hook_type); michael@0: michael@0: assert(evrpc_hook_get_connection(ctx) != NULL); michael@0: michael@0: return (EVRPC_CONTINUE); michael@0: } michael@0: michael@0: static int michael@0: rpc_hook_add_meta(void *ctx, struct evhttp_request *req, michael@0: struct evbuffer *evbuf, void *arg) michael@0: { michael@0: evrpc_hook_add_meta(ctx, "meta", "test", 5); michael@0: michael@0: assert(evrpc_hook_get_connection(ctx) != NULL); michael@0: michael@0: return (EVRPC_CONTINUE); michael@0: } michael@0: michael@0: static int michael@0: rpc_hook_remove_header(void *ctx, struct evhttp_request *req, michael@0: struct evbuffer *evbuf, void *arg) michael@0: { michael@0: const char *header = evhttp_find_header(req->input_headers, "X-Hook"); michael@0: void *data = NULL; michael@0: size_t data_len = 0; michael@0: michael@0: assert(header != NULL); michael@0: assert(strcmp(header, arg) == 0); michael@0: michael@0: evhttp_remove_header(req->input_headers, "X-Hook"); michael@0: evhttp_add_header(req->input_headers, "X-Pool-Hook", "ran"); michael@0: michael@0: assert(evrpc_hook_find_meta(ctx, "meta", &data, &data_len) == 0); michael@0: assert(data != NULL); michael@0: assert(data_len == 5); michael@0: michael@0: assert(evrpc_hook_get_connection(ctx) != NULL); michael@0: michael@0: return (EVRPC_CONTINUE); michael@0: } michael@0: michael@0: static void michael@0: rpc_basic_client(void) michael@0: { michael@0: ev_uint16_t port; michael@0: struct evhttp *http = NULL; michael@0: struct evrpc_base *base = NULL; michael@0: struct evrpc_pool *pool = NULL; michael@0: struct msg *msg = NULL; michael@0: struct kill *kill = NULL; michael@0: michael@0: rpc_setup(&http, &port, &base); michael@0: michael@0: need_input_hook = 1; michael@0: need_output_hook = 1; michael@0: michael@0: assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_add_header, (void*)"input") michael@0: != NULL); michael@0: assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_add_header, (void*)"output") michael@0: != NULL); michael@0: michael@0: pool = rpc_pool_with_connection(port); michael@0: michael@0: assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL)); michael@0: assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output")); michael@0: michael@0: /* set up the basic message */ michael@0: msg = msg_new(); michael@0: EVTAG_ASSIGN(msg, from_name, "niels"); michael@0: EVTAG_ASSIGN(msg, to_name, "tester"); michael@0: michael@0: kill = kill_new(); michael@0: michael@0: EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL); michael@0: michael@0: test_ok = 0; michael@0: michael@0: event_dispatch(); michael@0: michael@0: tt_assert(test_ok == 1); michael@0: michael@0: /* we do it twice to make sure that reuse works correctly */ michael@0: kill_clear(kill); michael@0: michael@0: EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL); michael@0: michael@0: event_dispatch(); michael@0: michael@0: tt_assert(test_ok == 2); michael@0: michael@0: /* we do it trice to make sure other stuff works, too */ michael@0: kill_clear(kill); michael@0: michael@0: { michael@0: struct evrpc_request_wrapper *ctx = michael@0: EVRPC_MAKE_CTX(Message, msg, kill, michael@0: pool, msg, kill, GotKillCb, NULL); michael@0: evrpc_make_request(ctx); michael@0: } michael@0: michael@0: event_dispatch(); michael@0: michael@0: rpc_teardown(base); michael@0: michael@0: tt_assert(test_ok == 3); michael@0: michael@0: end: michael@0: if (msg) michael@0: msg_free(msg); michael@0: if (kill) michael@0: kill_free(kill); michael@0: michael@0: if (pool) michael@0: evrpc_pool_free(pool); michael@0: if (http) michael@0: evhttp_free(http); michael@0: michael@0: need_input_hook = 0; michael@0: need_output_hook = 0; michael@0: } michael@0: michael@0: /* michael@0: * We are testing that the second requests gets send over the same michael@0: * connection after the first RPCs completes. michael@0: */ michael@0: static void michael@0: rpc_basic_queued_client(void) michael@0: { michael@0: ev_uint16_t port; michael@0: struct evhttp *http = NULL; michael@0: struct evrpc_base *base = NULL; michael@0: struct evrpc_pool *pool = NULL; michael@0: struct msg *msg=NULL; michael@0: struct kill *kill_one=NULL, *kill_two=NULL; michael@0: michael@0: rpc_setup(&http, &port, &base); michael@0: michael@0: pool = rpc_pool_with_connection(port); michael@0: michael@0: /* set up the basic message */ michael@0: msg = msg_new(); michael@0: EVTAG_ASSIGN(msg, from_name, "niels"); michael@0: EVTAG_ASSIGN(msg, to_name, "tester"); michael@0: michael@0: kill_one = kill_new(); michael@0: kill_two = kill_new(); michael@0: michael@0: EVRPC_MAKE_REQUEST(Message, pool, msg, kill_one, GotKillCbTwo, NULL); michael@0: EVRPC_MAKE_REQUEST(Message, pool, msg, kill_two, GotKillCb, NULL); michael@0: michael@0: test_ok = 0; michael@0: michael@0: event_dispatch(); michael@0: michael@0: rpc_teardown(base); michael@0: michael@0: tt_assert(test_ok == 2); michael@0: michael@0: end: michael@0: if (msg) michael@0: msg_free(msg); michael@0: if (kill_one) michael@0: kill_free(kill_one); michael@0: if (kill_two) michael@0: kill_free(kill_two); michael@0: michael@0: if (pool) michael@0: evrpc_pool_free(pool); michael@0: if (http) michael@0: evhttp_free(http); michael@0: } michael@0: michael@0: static void michael@0: GotErrorCb(struct evrpc_status *status, michael@0: struct msg *msg, struct kill *kill, void *arg) michael@0: { michael@0: if (status->error != EVRPC_STATUS_ERR_TIMEOUT) michael@0: goto done; michael@0: michael@0: /* should never be complete but just to check */ michael@0: if (kill_complete(kill) == 0) michael@0: goto done; michael@0: michael@0: test_ok += 1; michael@0: michael@0: done: michael@0: event_loopexit(NULL); michael@0: } michael@0: michael@0: /* we just pause the rpc and continue it in the next callback */ michael@0: michael@0: struct _rpc_hook_ctx { michael@0: void *vbase; michael@0: void *ctx; michael@0: }; michael@0: michael@0: static int hook_pause_cb_called=0; michael@0: michael@0: static void michael@0: rpc_hook_pause_cb(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: struct _rpc_hook_ctx *ctx = arg; michael@0: ++hook_pause_cb_called; michael@0: evrpc_resume_request(ctx->vbase, ctx->ctx, EVRPC_CONTINUE); michael@0: free(arg); michael@0: } michael@0: michael@0: static int michael@0: rpc_hook_pause(void *ctx, struct evhttp_request *req, struct evbuffer *evbuf, michael@0: void *arg) michael@0: { michael@0: struct _rpc_hook_ctx *tmp = malloc(sizeof(*tmp)); michael@0: struct timeval tv; michael@0: michael@0: assert(tmp != NULL); michael@0: tmp->vbase = arg; michael@0: tmp->ctx = ctx; michael@0: michael@0: memset(&tv, 0, sizeof(tv)); michael@0: event_once(-1, EV_TIMEOUT, rpc_hook_pause_cb, tmp, &tv); michael@0: return EVRPC_PAUSE; michael@0: } michael@0: michael@0: static void michael@0: rpc_basic_client_with_pause(void) michael@0: { michael@0: ev_uint16_t port; michael@0: struct evhttp *http = NULL; michael@0: struct evrpc_base *base = NULL; michael@0: struct evrpc_pool *pool = NULL; michael@0: struct msg *msg = NULL; michael@0: struct kill *kill= NULL; michael@0: michael@0: rpc_setup(&http, &port, &base); michael@0: michael@0: assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_pause, base)); michael@0: assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base)); michael@0: michael@0: pool = rpc_pool_with_connection(port); michael@0: michael@0: assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool)); michael@0: assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool)); michael@0: michael@0: /* set up the basic message */ michael@0: msg = msg_new(); michael@0: EVTAG_ASSIGN(msg, from_name, "niels"); michael@0: EVTAG_ASSIGN(msg, to_name, "tester"); michael@0: michael@0: kill = kill_new(); michael@0: michael@0: EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL); michael@0: michael@0: test_ok = 0; michael@0: michael@0: event_dispatch(); michael@0: michael@0: tt_int_op(test_ok, ==, 1); michael@0: tt_int_op(hook_pause_cb_called, ==, 4); michael@0: michael@0: end: michael@0: if (base) michael@0: rpc_teardown(base); michael@0: michael@0: if (msg) michael@0: msg_free(msg); michael@0: if (kill) michael@0: kill_free(kill); michael@0: michael@0: if (pool) michael@0: evrpc_pool_free(pool); michael@0: if (http) michael@0: evhttp_free(http); michael@0: } michael@0: michael@0: static void michael@0: rpc_client_timeout(void) michael@0: { michael@0: ev_uint16_t port; michael@0: struct evhttp *http = NULL; michael@0: struct evrpc_base *base = NULL; michael@0: struct evrpc_pool *pool = NULL; michael@0: struct msg *msg = NULL; michael@0: struct kill *kill = NULL; michael@0: michael@0: rpc_setup(&http, &port, &base); michael@0: michael@0: pool = rpc_pool_with_connection(port); michael@0: michael@0: /* set the timeout to 5 seconds */ michael@0: evrpc_pool_set_timeout(pool, 5); michael@0: michael@0: /* set up the basic message */ michael@0: msg = msg_new(); michael@0: EVTAG_ASSIGN(msg, from_name, "niels"); michael@0: EVTAG_ASSIGN(msg, to_name, "tester"); michael@0: michael@0: kill = kill_new(); michael@0: michael@0: EVRPC_MAKE_REQUEST(NeverReply, pool, msg, kill, GotErrorCb, NULL); michael@0: michael@0: test_ok = 0; michael@0: michael@0: event_dispatch(); michael@0: michael@0: /* free the saved RPC structure up */ michael@0: EVRPC_REQUEST_DONE(saved_rpc); michael@0: michael@0: rpc_teardown(base); michael@0: michael@0: tt_assert(test_ok == 2); michael@0: michael@0: end: michael@0: if (msg) michael@0: msg_free(msg); michael@0: if (kill) michael@0: kill_free(kill); michael@0: michael@0: if (pool) michael@0: evrpc_pool_free(pool); michael@0: if (http) michael@0: evhttp_free(http); michael@0: } michael@0: michael@0: static void michael@0: rpc_test(void) michael@0: { michael@0: struct msg *msg = NULL, *msg2 = NULL; michael@0: struct kill *attack = NULL; michael@0: struct run *run = NULL; michael@0: struct evbuffer *tmp = evbuffer_new(); michael@0: struct timeval tv_start, tv_end; michael@0: ev_uint32_t tag; michael@0: int i; michael@0: michael@0: msg = msg_new(); michael@0: michael@0: tt_assert(msg); michael@0: michael@0: EVTAG_ASSIGN(msg, from_name, "niels"); michael@0: EVTAG_ASSIGN(msg, to_name, "phoenix"); michael@0: michael@0: if (EVTAG_GET(msg, attack, &attack) == -1) { michael@0: tt_abort_msg("Failed to set kill message."); michael@0: } michael@0: michael@0: EVTAG_ASSIGN(attack, weapon, "feather"); michael@0: EVTAG_ASSIGN(attack, action, "tickle"); michael@0: for (i = 0; i < 3; ++i) { michael@0: if (EVTAG_ARRAY_ADD_VALUE(attack, how_often, i) == NULL) { michael@0: tt_abort_msg("Failed to add how_often."); michael@0: } michael@0: } michael@0: michael@0: evutil_gettimeofday(&tv_start, NULL); michael@0: for (i = 0; i < 1000; ++i) { michael@0: run = EVTAG_ARRAY_ADD(msg, run); michael@0: if (run == NULL) { michael@0: tt_abort_msg("Failed to add run message."); michael@0: } michael@0: EVTAG_ASSIGN(run, how, "very fast but with some data in it"); michael@0: EVTAG_ASSIGN(run, fixed_bytes, michael@0: (ev_uint8_t*)"012345678901234567890123"); michael@0: michael@0: if (EVTAG_ARRAY_ADD_VALUE( michael@0: run, notes, "this is my note") == NULL) { michael@0: tt_abort_msg("Failed to add note."); michael@0: } michael@0: if (EVTAG_ARRAY_ADD_VALUE(run, notes, "pps") == NULL) { michael@0: tt_abort_msg("Failed to add note"); michael@0: } michael@0: michael@0: EVTAG_ASSIGN(run, large_number, 0xdead0a0bcafebeefLL); michael@0: EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xdead0a0b); michael@0: EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xbeefcafe); michael@0: } michael@0: michael@0: if (msg_complete(msg) == -1) michael@0: tt_abort_msg("Failed to make complete message."); michael@0: michael@0: evtag_marshal_msg(tmp, 0xdeaf, msg); michael@0: michael@0: if (evtag_peek(tmp, &tag) == -1) michael@0: tt_abort_msg("Failed to peak tag."); michael@0: michael@0: if (tag != 0xdeaf) michael@0: TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag)); michael@0: michael@0: msg2 = msg_new(); michael@0: if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1) michael@0: tt_abort_msg("Failed to unmarshal message."); michael@0: michael@0: evutil_gettimeofday(&tv_end, NULL); michael@0: evutil_timersub(&tv_end, &tv_start, &tv_end); michael@0: TT_BLATHER(("(%.1f us/add) ", michael@0: (float)tv_end.tv_sec/(float)i * 1000000.0 + michael@0: tv_end.tv_usec / (float)i)); michael@0: michael@0: if (!EVTAG_HAS(msg2, from_name) || michael@0: !EVTAG_HAS(msg2, to_name) || michael@0: !EVTAG_HAS(msg2, attack)) { michael@0: tt_abort_msg("Missing data structures."); michael@0: } michael@0: michael@0: if (EVTAG_GET(msg2, attack, &attack) == -1) { michael@0: tt_abort_msg("Could not get attack."); michael@0: } michael@0: michael@0: if (EVTAG_ARRAY_LEN(msg2, run) != i) { michael@0: tt_abort_msg("Wrong number of run messages."); michael@0: } michael@0: michael@0: /* get the very first run message */ michael@0: if (EVTAG_ARRAY_GET(msg2, run, 0, &run) == -1) { michael@0: tt_abort_msg("Failed to get run msg."); michael@0: } else { michael@0: /* verify the notes */ michael@0: char *note_one, *note_two; michael@0: ev_uint64_t large_number; michael@0: ev_uint32_t short_number; michael@0: michael@0: if (EVTAG_ARRAY_LEN(run, notes) != 2) { michael@0: tt_abort_msg("Wrong number of note strings."); michael@0: } michael@0: michael@0: if (EVTAG_ARRAY_GET(run, notes, 0, ¬e_one) == -1 || michael@0: EVTAG_ARRAY_GET(run, notes, 1, ¬e_two) == -1) { michael@0: tt_abort_msg("Could not get note strings."); michael@0: } michael@0: michael@0: if (strcmp(note_one, "this is my note") || michael@0: strcmp(note_two, "pps")) { michael@0: tt_abort_msg("Incorrect note strings encoded."); michael@0: } michael@0: michael@0: if (EVTAG_GET(run, large_number, &large_number) == -1 || michael@0: large_number != 0xdead0a0bcafebeefLL) { michael@0: tt_abort_msg("Incorrrect large_number."); michael@0: } michael@0: michael@0: if (EVTAG_ARRAY_LEN(run, other_numbers) != 2) { michael@0: tt_abort_msg("Wrong number of other_numbers."); michael@0: } michael@0: michael@0: if (EVTAG_ARRAY_GET( michael@0: run, other_numbers, 0, &short_number) == -1) { michael@0: tt_abort_msg("Could not get short number."); michael@0: } michael@0: tt_uint_op(short_number, ==, 0xdead0a0b); michael@0: michael@0: } michael@0: tt_int_op(EVTAG_ARRAY_LEN(attack, how_often), ==, 3); michael@0: michael@0: for (i = 0; i < 3; ++i) { michael@0: ev_uint32_t res; michael@0: if (EVTAG_ARRAY_GET(attack, how_often, i, &res) == -1) { michael@0: TT_DIE(("Cannot get %dth how_often msg.", i)); michael@0: } michael@0: if ((int)res != i) { michael@0: TT_DIE(("Wrong message encoded %d != %d", i, res)); michael@0: } michael@0: } michael@0: michael@0: test_ok = 1; michael@0: end: michael@0: if (msg) michael@0: msg_free(msg); michael@0: if (msg2) michael@0: msg_free(msg2); michael@0: if (tmp) michael@0: evbuffer_free(tmp); michael@0: } michael@0: michael@0: #define RPC_LEGACY(name) \ michael@0: { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \ michael@0: &legacy_setup, \ michael@0: rpc_##name } michael@0: #else michael@0: /* NO_PYTHON_EXISTS */ michael@0: michael@0: #define RPC_LEGACY(name) \ michael@0: { #name, NULL, TT_SKIP, NULL, NULL } michael@0: michael@0: #endif michael@0: michael@0: struct testcase_t rpc_testcases[] = { michael@0: RPC_LEGACY(basic_test), michael@0: RPC_LEGACY(basic_message), michael@0: RPC_LEGACY(basic_client), michael@0: RPC_LEGACY(basic_queued_client), michael@0: RPC_LEGACY(basic_client_with_pause), michael@0: RPC_LEGACY(client_timeout), michael@0: RPC_LEGACY(test), michael@0: michael@0: END_OF_TESTCASES, michael@0: };