2 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* The old tests here need assertions to work. */
36 #include "event2/event-config.h"
38 #include <sys/types.h>
40 #ifdef _EVENT_HAVE_SYS_TIME_H
43 #include <sys/queue.h>
45 #include <sys/socket.h>
57 #include "event2/buffer.h"
58 #include "event2/event.h"
59 #include "event2/event_compat.h"
60 #include "event2/http.h"
61 #include "event2/http_compat.h"
62 #include "event2/http_struct.h"
63 #include "event2/rpc.h"
64 #include "event2/rpc.h"
65 #include "event2/rpc_struct.h"
66 #include "event2/tag.h"
67 #include "log-internal.h"
69 #include "regress.gen.h"
72 #include "regress_testutils.h"
74 #ifndef NO_PYTHON_EXISTS
76 static struct evhttp *
77 http_setup(ev_uint16_t *pport)
79 struct evhttp *myhttp;
81 struct evhttp_bound_socket *sock;
83 myhttp = evhttp_new(NULL);
85 event_errx(1, "Could not start web server");
87 /* Try a few different ports */
88 sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", 0);
90 event_errx(1, "Couldn't open web port");
92 port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
98 EVRPC_HEADER(Message, msg, kill)
99 EVRPC_HEADER(NeverReply, msg, kill)
101 EVRPC_GENERATE(Message, msg, kill)
102 EVRPC_GENERATE(NeverReply, msg, kill)
104 static int need_input_hook = 0;
105 static int need_output_hook = 0;
108 MessageCb(EVRPC_STRUCT(Message)* rpc, void *arg)
110 struct kill* kill_reply = rpc->reply;
112 if (need_input_hook) {
113 struct evhttp_request* req = EVRPC_REQUEST_HTTP(rpc);
114 const char *header = evhttp_find_header(
115 req->input_headers, "X-Hook");
116 assert(strcmp(header, "input") == 0);
119 /* we just want to fill in some non-sense */
120 EVTAG_ASSIGN(kill_reply, weapon, "dagger");
121 EVTAG_ASSIGN(kill_reply, action, "wave around like an idiot");
123 /* no reply to the RPC */
124 EVRPC_REQUEST_DONE(rpc);
127 static EVRPC_STRUCT(NeverReply) *saved_rpc;
130 NeverReplyCb(EVRPC_STRUCT(NeverReply)* rpc, void *arg)
137 rpc_setup(struct evhttp **phttp, ev_uint16_t *pport, struct evrpc_base **pbase)
140 struct evhttp *http = NULL;
141 struct evrpc_base *base = NULL;
143 http = http_setup(&port);
144 base = evrpc_init(http);
146 EVRPC_REGISTER(base, Message, msg, kill, MessageCb, NULL);
147 EVRPC_REGISTER(base, NeverReply, msg, kill, NeverReplyCb, NULL);
154 need_output_hook = 0;
158 rpc_teardown(struct evrpc_base *base)
160 assert(EVRPC_UNREGISTER(base, Message) == 0);
161 assert(EVRPC_UNREGISTER(base, NeverReply) == 0);
167 rpc_postrequest_failure(struct evhttp_request *req, void *arg)
169 if (req->response_code != HTTP_SERVUNAVAIL) {
171 fprintf(stderr, "FAILED (response code)\n");
176 event_loopexit(NULL);
180 * Test a malformed payload submitted as an RPC
187 struct evhttp *http = NULL;
188 struct evrpc_base *base = NULL;
189 struct evhttp_connection *evcon = NULL;
190 struct evhttp_request *req = NULL;
192 rpc_setup(&http, &port, &base);
194 evcon = evhttp_connection_new("127.0.0.1", port);
198 * At this point, we want to schedule an HTTP POST request
199 * server using our make request method.
202 req = evhttp_request_new(rpc_postrequest_failure, NULL);
205 /* Add the information that we care about */
206 evhttp_add_header(req->output_headers, "Host", "somehost");
207 evbuffer_add_printf(req->output_buffer, "Some Nonsense");
209 if (evhttp_make_request(evcon, req,
211 "/.rpc.Message") == -1) {
219 evhttp_connection_free(evcon);
223 tt_assert(test_ok == 1);
230 rpc_postrequest_done(struct evhttp_request *req, void *arg)
232 struct kill* kill_reply = NULL;
234 if (req->response_code != HTTP_OK) {
235 fprintf(stderr, "FAILED (response code)\n");
239 kill_reply = kill_new();
241 if ((kill_unmarshal(kill_reply, req->input_buffer)) == -1) {
242 fprintf(stderr, "FAILED (unmarshal)\n");
246 kill_free(kill_reply);
249 event_loopexit(NULL);
253 rpc_basic_message(void)
256 struct evhttp *http = NULL;
257 struct evrpc_base *base = NULL;
258 struct evhttp_connection *evcon = NULL;
259 struct evhttp_request *req = NULL;
262 rpc_setup(&http, &port, &base);
264 evcon = evhttp_connection_new("127.0.0.1", port);
268 * At this point, we want to schedule an HTTP POST request
269 * server using our make request method.
272 req = evhttp_request_new(rpc_postrequest_done, NULL);
274 fprintf(stdout, "FAILED\n");
278 /* Add the information that we care about */
279 evhttp_add_header(req->output_headers, "Host", "somehost");
281 /* set up the basic message */
283 EVTAG_ASSIGN(msg, from_name, "niels");
284 EVTAG_ASSIGN(msg, to_name, "tester");
285 msg_marshal(req->output_buffer, msg);
288 if (evhttp_make_request(evcon, req,
290 "/.rpc.Message") == -1) {
291 fprintf(stdout, "FAILED\n");
299 evhttp_connection_free(evcon);
307 static struct evrpc_pool *
308 rpc_pool_with_connection(ev_uint16_t port)
310 struct evhttp_connection *evcon;
311 struct evrpc_pool *pool;
313 pool = evrpc_pool_new(NULL);
314 assert(pool != NULL);
316 evcon = evhttp_connection_new("127.0.0.1", port);
317 assert(evcon != NULL);
319 evrpc_pool_add_connection(pool, evcon);
325 GotKillCb(struct evrpc_status *status,
326 struct msg *msg, struct kill *kill, void *arg)
331 if (need_output_hook) {
332 struct evhttp_request *req = status->http_req;
333 const char *header = evhttp_find_header(
334 req->input_headers, "X-Pool-Hook");
335 assert(strcmp(header, "ran") == 0);
338 if (status->error != EVRPC_STATUS_ERR_NONE)
341 if (EVTAG_GET(kill, weapon, &weapon) == -1) {
342 fprintf(stderr, "get weapon\n");
345 if (EVTAG_GET(kill, action, &action) == -1) {
346 fprintf(stderr, "get action\n");
350 if (strcmp(weapon, "dagger"))
353 if (strcmp(action, "wave around like an idiot"))
359 event_loopexit(NULL);
363 GotKillCbTwo(struct evrpc_status *status,
364 struct msg *msg, struct kill *kill, void *arg)
369 if (status->error != EVRPC_STATUS_ERR_NONE)
372 if (EVTAG_GET(kill, weapon, &weapon) == -1) {
373 fprintf(stderr, "get weapon\n");
376 if (EVTAG_GET(kill, action, &action) == -1) {
377 fprintf(stderr, "get action\n");
381 if (strcmp(weapon, "dagger"))
384 if (strcmp(action, "wave around like an idiot"))
391 event_loopexit(NULL);
395 rpc_hook_add_header(void *ctx, struct evhttp_request *req,
396 struct evbuffer *evbuf, void *arg)
398 const char *hook_type = arg;
399 if (strcmp("input", hook_type) == 0)
400 evhttp_add_header(req->input_headers, "X-Hook", hook_type);
402 evhttp_add_header(req->output_headers, "X-Hook", hook_type);
404 assert(evrpc_hook_get_connection(ctx) != NULL);
406 return (EVRPC_CONTINUE);
410 rpc_hook_add_meta(void *ctx, struct evhttp_request *req,
411 struct evbuffer *evbuf, void *arg)
413 evrpc_hook_add_meta(ctx, "meta", "test", 5);
415 assert(evrpc_hook_get_connection(ctx) != NULL);
417 return (EVRPC_CONTINUE);
421 rpc_hook_remove_header(void *ctx, struct evhttp_request *req,
422 struct evbuffer *evbuf, void *arg)
424 const char *header = evhttp_find_header(req->input_headers, "X-Hook");
428 assert(header != NULL);
429 assert(strcmp(header, arg) == 0);
431 evhttp_remove_header(req->input_headers, "X-Hook");
432 evhttp_add_header(req->input_headers, "X-Pool-Hook", "ran");
434 assert(evrpc_hook_find_meta(ctx, "meta", &data, &data_len) == 0);
435 assert(data != NULL);
436 assert(data_len == 5);
438 assert(evrpc_hook_get_connection(ctx) != NULL);
440 return (EVRPC_CONTINUE);
444 rpc_basic_client(void)
447 struct evhttp *http = NULL;
448 struct evrpc_base *base = NULL;
449 struct evrpc_pool *pool = NULL;
450 struct msg *msg = NULL;
451 struct kill *kill = NULL;
453 rpc_setup(&http, &port, &base);
456 need_output_hook = 1;
458 assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_add_header, (void*)"input")
460 assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_add_header, (void*)"output")
463 pool = rpc_pool_with_connection(port);
465 assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL));
466 assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output"));
468 /* set up the basic message */
470 EVTAG_ASSIGN(msg, from_name, "niels");
471 EVTAG_ASSIGN(msg, to_name, "tester");
475 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
481 tt_assert(test_ok == 1);
483 /* we do it twice to make sure that reuse works correctly */
486 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
490 tt_assert(test_ok == 2);
492 /* we do it trice to make sure other stuff works, too */
496 struct evrpc_request_wrapper *ctx =
497 EVRPC_MAKE_CTX(Message, msg, kill,
498 pool, msg, kill, GotKillCb, NULL);
499 evrpc_make_request(ctx);
506 tt_assert(test_ok == 3);
515 evrpc_pool_free(pool);
520 need_output_hook = 0;
524 * We are testing that the second requests gets send over the same
525 * connection after the first RPCs completes.
528 rpc_basic_queued_client(void)
531 struct evhttp *http = NULL;
532 struct evrpc_base *base = NULL;
533 struct evrpc_pool *pool = NULL;
534 struct msg *msg=NULL;
535 struct kill *kill_one=NULL, *kill_two=NULL;
537 rpc_setup(&http, &port, &base);
539 pool = rpc_pool_with_connection(port);
541 /* set up the basic message */
543 EVTAG_ASSIGN(msg, from_name, "niels");
544 EVTAG_ASSIGN(msg, to_name, "tester");
546 kill_one = kill_new();
547 kill_two = kill_new();
549 EVRPC_MAKE_REQUEST(Message, pool, msg, kill_one, GotKillCbTwo, NULL);
550 EVRPC_MAKE_REQUEST(Message, pool, msg, kill_two, GotKillCb, NULL);
558 tt_assert(test_ok == 2);
569 evrpc_pool_free(pool);
575 GotErrorCb(struct evrpc_status *status,
576 struct msg *msg, struct kill *kill, void *arg)
578 if (status->error != EVRPC_STATUS_ERR_TIMEOUT)
581 /* should never be complete but just to check */
582 if (kill_complete(kill) == 0)
588 event_loopexit(NULL);
591 /* we just pause the rpc and continue it in the next callback */
593 struct _rpc_hook_ctx {
598 static int hook_pause_cb_called=0;
601 rpc_hook_pause_cb(evutil_socket_t fd, short what, void *arg)
603 struct _rpc_hook_ctx *ctx = arg;
604 ++hook_pause_cb_called;
605 evrpc_resume_request(ctx->vbase, ctx->ctx, EVRPC_CONTINUE);
610 rpc_hook_pause(void *ctx, struct evhttp_request *req, struct evbuffer *evbuf,
613 struct _rpc_hook_ctx *tmp = malloc(sizeof(*tmp));
620 memset(&tv, 0, sizeof(tv));
621 event_once(-1, EV_TIMEOUT, rpc_hook_pause_cb, tmp, &tv);
626 rpc_basic_client_with_pause(void)
629 struct evhttp *http = NULL;
630 struct evrpc_base *base = NULL;
631 struct evrpc_pool *pool = NULL;
632 struct msg *msg = NULL;
633 struct kill *kill= NULL;
635 rpc_setup(&http, &port, &base);
637 assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_pause, base));
638 assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base));
640 pool = rpc_pool_with_connection(port);
642 assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool));
643 assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool));
645 /* set up the basic message */
647 EVTAG_ASSIGN(msg, from_name, "niels");
648 EVTAG_ASSIGN(msg, to_name, "tester");
652 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
658 tt_int_op(test_ok, ==, 1);
659 tt_int_op(hook_pause_cb_called, ==, 4);
671 evrpc_pool_free(pool);
677 rpc_client_timeout(void)
680 struct evhttp *http = NULL;
681 struct evrpc_base *base = NULL;
682 struct evrpc_pool *pool = NULL;
683 struct msg *msg = NULL;
684 struct kill *kill = NULL;
686 rpc_setup(&http, &port, &base);
688 pool = rpc_pool_with_connection(port);
690 /* set the timeout to 5 seconds */
691 evrpc_pool_set_timeout(pool, 5);
693 /* set up the basic message */
695 EVTAG_ASSIGN(msg, from_name, "niels");
696 EVTAG_ASSIGN(msg, to_name, "tester");
700 EVRPC_MAKE_REQUEST(NeverReply, pool, msg, kill, GotErrorCb, NULL);
706 /* free the saved RPC structure up */
707 EVRPC_REQUEST_DONE(saved_rpc);
711 tt_assert(test_ok == 2);
720 evrpc_pool_free(pool);
728 struct msg *msg = NULL, *msg2 = NULL;
729 struct kill *attack = NULL;
730 struct run *run = NULL;
731 struct evbuffer *tmp = evbuffer_new();
732 struct timeval tv_start, tv_end;
737 EVTAG_ASSIGN(msg, from_name, "niels");
738 EVTAG_ASSIGN(msg, to_name, "phoenix");
740 if (EVTAG_GET(msg, attack, &attack) == -1) {
741 tt_abort_msg("Failed to set kill message.");
744 EVTAG_ASSIGN(attack, weapon, "feather");
745 EVTAG_ASSIGN(attack, action, "tickle");
746 for (i = 0; i < 3; ++i) {
747 if (EVTAG_ARRAY_ADD_VALUE(attack, how_often, i) == NULL) {
748 tt_abort_msg("Failed to add how_often.");
752 evutil_gettimeofday(&tv_start, NULL);
753 for (i = 0; i < 1000; ++i) {
754 run = EVTAG_ARRAY_ADD(msg, run);
756 tt_abort_msg("Failed to add run message.");
758 EVTAG_ASSIGN(run, how, "very fast but with some data in it");
759 EVTAG_ASSIGN(run, fixed_bytes,
760 (ev_uint8_t*)"012345678901234567890123");
762 if (EVTAG_ARRAY_ADD_VALUE(
763 run, notes, "this is my note") == NULL) {
764 tt_abort_msg("Failed to add note.");
766 if (EVTAG_ARRAY_ADD_VALUE(run, notes, "pps") == NULL) {
767 tt_abort_msg("Failed to add note");
770 EVTAG_ASSIGN(run, large_number, 0xdead0a0bcafebeefLL);
771 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xdead0a0b);
772 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xbeefcafe);
775 if (msg_complete(msg) == -1)
776 tt_abort_msg("Failed to make complete message.");
778 evtag_marshal_msg(tmp, 0xdeaf, msg);
780 if (evtag_peek(tmp, &tag) == -1)
781 tt_abort_msg("Failed to peak tag.");
784 TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag));
787 if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1)
788 tt_abort_msg("Failed to unmarshal message.");
790 evutil_gettimeofday(&tv_end, NULL);
791 evutil_timersub(&tv_end, &tv_start, &tv_end);
792 TT_BLATHER(("(%.1f us/add) ",
793 (float)tv_end.tv_sec/(float)i * 1000000.0 +
794 tv_end.tv_usec / (float)i));
796 if (!EVTAG_HAS(msg2, from_name) ||
797 !EVTAG_HAS(msg2, to_name) ||
798 !EVTAG_HAS(msg2, attack)) {
799 tt_abort_msg("Missing data structures.");
802 if (EVTAG_GET(msg2, attack, &attack) == -1) {
803 tt_abort_msg("Could not get attack.");
806 if (EVTAG_ARRAY_LEN(msg2, run) != i) {
807 tt_abort_msg("Wrong number of run messages.");
810 /* get the very first run message */
811 if (EVTAG_ARRAY_GET(msg2, run, 0, &run) == -1) {
812 tt_abort_msg("Failed to get run msg.");
814 /* verify the notes */
815 char *note_one, *note_two;
816 ev_uint64_t large_number;
817 ev_uint32_t short_number;
819 if (EVTAG_ARRAY_LEN(run, notes) != 2) {
820 tt_abort_msg("Wrong number of note strings.");
823 if (EVTAG_ARRAY_GET(run, notes, 0, ¬e_one) == -1 ||
824 EVTAG_ARRAY_GET(run, notes, 1, ¬e_two) == -1) {
825 tt_abort_msg("Could not get note strings.");
828 if (strcmp(note_one, "this is my note") ||
829 strcmp(note_two, "pps")) {
830 tt_abort_msg("Incorrect note strings encoded.");
833 if (EVTAG_GET(run, large_number, &large_number) == -1 ||
834 large_number != 0xdead0a0bcafebeefLL) {
835 tt_abort_msg("Incorrrect large_number.");
838 if (EVTAG_ARRAY_LEN(run, other_numbers) != 2) {
839 tt_abort_msg("Wrong number of other_numbers.");
843 run, other_numbers, 0, &short_number) == -1) {
844 tt_abort_msg("Could not get short number.");
846 tt_uint_op(short_number, ==, 0xdead0a0b);
849 tt_int_op(EVTAG_ARRAY_LEN(attack, how_often), ==, 3);
851 for (i = 0; i < 3; ++i) {
853 if (EVTAG_ARRAY_GET(attack, how_often, i, &res) == -1) {
854 TT_DIE(("Cannot get %dth how_often msg.", i));
857 TT_DIE(("Wrong message encoded %d != %d", i, res));
871 #define RPC_LEGACY(name) \
872 { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \
876 /* NO_PYTHON_EXISTS */
878 #define RPC_LEGACY(name) \
879 { #name, NULL, TT_SKIP, NULL, NULL }
883 struct testcase_t rpc_testcases[] = {
884 RPC_LEGACY(basic_test),
885 RPC_LEGACY(basic_message),
886 RPC_LEGACY(basic_client),
887 RPC_LEGACY(basic_queued_client),
888 RPC_LEGACY(basic_client_with_pause),
889 RPC_LEGACY(client_timeout),