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");
117 assert(strcmp(header, "input") == 0);
120 /* we just want to fill in some non-sense */
121 EVTAG_ASSIGN(kill_reply, weapon, "dagger");
122 EVTAG_ASSIGN(kill_reply, action, "wave around like an idiot");
124 /* no reply to the RPC */
125 EVRPC_REQUEST_DONE(rpc);
128 static EVRPC_STRUCT(NeverReply) *saved_rpc;
131 NeverReplyCb(EVRPC_STRUCT(NeverReply)* rpc, void *arg)
138 rpc_setup(struct evhttp **phttp, ev_uint16_t *pport, struct evrpc_base **pbase)
141 struct evhttp *http = NULL;
142 struct evrpc_base *base = NULL;
144 http = http_setup(&port);
145 base = evrpc_init(http);
147 EVRPC_REGISTER(base, Message, msg, kill, MessageCb, NULL);
148 EVRPC_REGISTER(base, NeverReply, msg, kill, NeverReplyCb, NULL);
155 need_output_hook = 0;
159 rpc_teardown(struct evrpc_base *base)
161 assert(EVRPC_UNREGISTER(base, Message) == 0);
162 assert(EVRPC_UNREGISTER(base, NeverReply) == 0);
168 rpc_postrequest_failure(struct evhttp_request *req, void *arg)
170 if (req->response_code != HTTP_SERVUNAVAIL) {
172 fprintf(stderr, "FAILED (response code)\n");
177 event_loopexit(NULL);
181 * Test a malformed payload submitted as an RPC
188 struct evhttp *http = NULL;
189 struct evrpc_base *base = NULL;
190 struct evhttp_connection *evcon = NULL;
191 struct evhttp_request *req = NULL;
193 rpc_setup(&http, &port, &base);
195 evcon = evhttp_connection_new("127.0.0.1", port);
199 * At this point, we want to schedule an HTTP POST request
200 * server using our make request method.
203 req = evhttp_request_new(rpc_postrequest_failure, NULL);
206 /* Add the information that we care about */
207 evhttp_add_header(req->output_headers, "Host", "somehost");
208 evbuffer_add_printf(req->output_buffer, "Some Nonsense");
210 if (evhttp_make_request(evcon, req,
212 "/.rpc.Message") == -1) {
220 evhttp_connection_free(evcon);
224 tt_assert(test_ok == 1);
231 rpc_postrequest_done(struct evhttp_request *req, void *arg)
233 struct kill* kill_reply = NULL;
235 if (req->response_code != HTTP_OK) {
236 fprintf(stderr, "FAILED (response code)\n");
240 kill_reply = kill_new();
242 if ((kill_unmarshal(kill_reply, req->input_buffer)) == -1) {
243 fprintf(stderr, "FAILED (unmarshal)\n");
247 kill_free(kill_reply);
250 event_loopexit(NULL);
254 rpc_basic_message(void)
257 struct evhttp *http = NULL;
258 struct evrpc_base *base = NULL;
259 struct evhttp_connection *evcon = NULL;
260 struct evhttp_request *req = NULL;
263 rpc_setup(&http, &port, &base);
265 evcon = evhttp_connection_new("127.0.0.1", port);
269 * At this point, we want to schedule an HTTP POST request
270 * server using our make request method.
273 req = evhttp_request_new(rpc_postrequest_done, NULL);
275 fprintf(stdout, "FAILED\n");
279 /* Add the information that we care about */
280 evhttp_add_header(req->output_headers, "Host", "somehost");
282 /* set up the basic message */
284 EVTAG_ASSIGN(msg, from_name, "niels");
285 EVTAG_ASSIGN(msg, to_name, "tester");
286 msg_marshal(req->output_buffer, msg);
289 if (evhttp_make_request(evcon, req,
291 "/.rpc.Message") == -1) {
292 fprintf(stdout, "FAILED\n");
300 evhttp_connection_free(evcon);
308 static struct evrpc_pool *
309 rpc_pool_with_connection(ev_uint16_t port)
311 struct evhttp_connection *evcon;
312 struct evrpc_pool *pool;
314 pool = evrpc_pool_new(NULL);
315 assert(pool != NULL);
317 evcon = evhttp_connection_new("127.0.0.1", port);
318 assert(evcon != NULL);
320 evrpc_pool_add_connection(pool, evcon);
326 GotKillCb(struct evrpc_status *status,
327 struct msg *msg, struct kill *kill, void *arg)
332 if (need_output_hook) {
333 struct evhttp_request *req = status->http_req;
334 const char *header = evhttp_find_header(
335 req->input_headers, "X-Pool-Hook");
337 assert(strcmp(header, "ran") == 0);
340 if (status->error != EVRPC_STATUS_ERR_NONE)
343 if (EVTAG_GET(kill, weapon, &weapon) == -1) {
344 fprintf(stderr, "get weapon\n");
347 if (EVTAG_GET(kill, action, &action) == -1) {
348 fprintf(stderr, "get action\n");
352 if (strcmp(weapon, "dagger"))
355 if (strcmp(action, "wave around like an idiot"))
361 event_loopexit(NULL);
365 GotKillCbTwo(struct evrpc_status *status,
366 struct msg *msg, struct kill *kill, void *arg)
371 if (status->error != EVRPC_STATUS_ERR_NONE)
374 if (EVTAG_GET(kill, weapon, &weapon) == -1) {
375 fprintf(stderr, "get weapon\n");
378 if (EVTAG_GET(kill, action, &action) == -1) {
379 fprintf(stderr, "get action\n");
383 if (strcmp(weapon, "dagger"))
386 if (strcmp(action, "wave around like an idiot"))
393 event_loopexit(NULL);
397 rpc_hook_add_header(void *ctx, struct evhttp_request *req,
398 struct evbuffer *evbuf, void *arg)
400 const char *hook_type = arg;
401 if (strcmp("input", hook_type) == 0)
402 evhttp_add_header(req->input_headers, "X-Hook", hook_type);
404 evhttp_add_header(req->output_headers, "X-Hook", hook_type);
406 assert(evrpc_hook_get_connection(ctx) != NULL);
408 return (EVRPC_CONTINUE);
412 rpc_hook_add_meta(void *ctx, struct evhttp_request *req,
413 struct evbuffer *evbuf, void *arg)
415 evrpc_hook_add_meta(ctx, "meta", "test", 5);
417 assert(evrpc_hook_get_connection(ctx) != NULL);
419 return (EVRPC_CONTINUE);
423 rpc_hook_remove_header(void *ctx, struct evhttp_request *req,
424 struct evbuffer *evbuf, void *arg)
426 const char *header = evhttp_find_header(req->input_headers, "X-Hook");
430 assert(header != NULL);
431 assert(strcmp(header, arg) == 0);
433 evhttp_remove_header(req->input_headers, "X-Hook");
434 evhttp_add_header(req->input_headers, "X-Pool-Hook", "ran");
436 assert(evrpc_hook_find_meta(ctx, "meta", &data, &data_len) == 0);
437 assert(data != NULL);
438 assert(data_len == 5);
440 assert(evrpc_hook_get_connection(ctx) != NULL);
442 return (EVRPC_CONTINUE);
446 rpc_basic_client(void)
449 struct evhttp *http = NULL;
450 struct evrpc_base *base = NULL;
451 struct evrpc_pool *pool = NULL;
452 struct msg *msg = NULL;
453 struct kill *kill = NULL;
455 rpc_setup(&http, &port, &base);
458 need_output_hook = 1;
460 assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_add_header, (void*)"input")
462 assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_add_header, (void*)"output")
465 pool = rpc_pool_with_connection(port);
468 assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL));
469 assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output"));
471 /* set up the basic message */
474 EVTAG_ASSIGN(msg, from_name, "niels");
475 EVTAG_ASSIGN(msg, to_name, "tester");
479 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
485 tt_assert(test_ok == 1);
487 /* we do it twice to make sure that reuse works correctly */
490 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
494 tt_assert(test_ok == 2);
496 /* we do it trice to make sure other stuff works, too */
500 struct evrpc_request_wrapper *ctx =
501 EVRPC_MAKE_CTX(Message, msg, kill,
502 pool, msg, kill, GotKillCb, NULL);
503 evrpc_make_request(ctx);
510 tt_assert(test_ok == 3);
519 evrpc_pool_free(pool);
524 need_output_hook = 0;
528 * We are testing that the second requests gets send over the same
529 * connection after the first RPCs completes.
532 rpc_basic_queued_client(void)
535 struct evhttp *http = NULL;
536 struct evrpc_base *base = NULL;
537 struct evrpc_pool *pool = NULL;
538 struct msg *msg=NULL;
539 struct kill *kill_one=NULL, *kill_two=NULL;
541 rpc_setup(&http, &port, &base);
543 pool = rpc_pool_with_connection(port);
546 /* set up the basic message */
549 EVTAG_ASSIGN(msg, from_name, "niels");
550 EVTAG_ASSIGN(msg, to_name, "tester");
552 kill_one = kill_new();
553 kill_two = kill_new();
555 EVRPC_MAKE_REQUEST(Message, pool, msg, kill_one, GotKillCbTwo, NULL);
556 EVRPC_MAKE_REQUEST(Message, pool, msg, kill_two, GotKillCb, NULL);
564 tt_assert(test_ok == 2);
575 evrpc_pool_free(pool);
581 GotErrorCb(struct evrpc_status *status,
582 struct msg *msg, struct kill *kill, void *arg)
584 if (status->error != EVRPC_STATUS_ERR_TIMEOUT)
587 /* should never be complete but just to check */
588 if (kill_complete(kill) == 0)
594 event_loopexit(NULL);
597 /* we just pause the rpc and continue it in the next callback */
599 struct rpc_hook_ctx_ {
604 static int hook_pause_cb_called=0;
607 rpc_hook_pause_cb(evutil_socket_t fd, short what, void *arg)
609 struct rpc_hook_ctx_ *ctx = arg;
610 ++hook_pause_cb_called;
611 evrpc_resume_request(ctx->vbase, ctx->ctx, EVRPC_CONTINUE);
616 rpc_hook_pause(void *ctx, struct evhttp_request *req, struct evbuffer *evbuf,
619 struct rpc_hook_ctx_ *tmp = malloc(sizeof(*tmp));
626 memset(&tv, 0, sizeof(tv));
627 event_once(-1, EV_TIMEOUT, rpc_hook_pause_cb, tmp, &tv);
632 rpc_basic_client_with_pause(void)
635 struct evhttp *http = NULL;
636 struct evrpc_base *base = NULL;
637 struct evrpc_pool *pool = NULL;
638 struct msg *msg = NULL;
639 struct kill *kill= NULL;
641 rpc_setup(&http, &port, &base);
643 assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_pause, base));
644 assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base));
646 pool = rpc_pool_with_connection(port);
648 assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool));
649 assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool));
651 /* set up the basic message */
654 EVTAG_ASSIGN(msg, from_name, "niels");
655 EVTAG_ASSIGN(msg, to_name, "tester");
659 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
665 tt_int_op(test_ok, ==, 1);
666 tt_int_op(hook_pause_cb_called, ==, 4);
678 evrpc_pool_free(pool);
684 rpc_client_timeout(void)
687 struct evhttp *http = NULL;
688 struct evrpc_base *base = NULL;
689 struct evrpc_pool *pool = NULL;
690 struct msg *msg = NULL;
691 struct kill *kill = NULL;
693 rpc_setup(&http, &port, &base);
695 pool = rpc_pool_with_connection(port);
698 /* set the timeout to 1 second. */
699 evrpc_pool_set_timeout(pool, 1);
701 /* set up the basic message */
704 EVTAG_ASSIGN(msg, from_name, "niels");
705 EVTAG_ASSIGN(msg, to_name, "tester");
709 EVRPC_MAKE_REQUEST(NeverReply, pool, msg, kill, GotErrorCb, NULL);
715 /* free the saved RPC structure up */
716 EVRPC_REQUEST_DONE(saved_rpc);
720 tt_assert(test_ok == 2);
729 evrpc_pool_free(pool);
737 struct msg *msg = NULL, *msg2 = NULL;
738 struct kill *attack = NULL;
739 struct run *run = NULL;
740 struct evbuffer *tmp = evbuffer_new();
741 struct timeval tv_start, tv_end;
749 EVTAG_ASSIGN(msg, from_name, "niels");
750 EVTAG_ASSIGN(msg, to_name, "phoenix");
752 if (EVTAG_GET(msg, attack, &attack) == -1) {
753 tt_abort_msg("Failed to set kill message.");
756 EVTAG_ASSIGN(attack, weapon, "feather");
757 EVTAG_ASSIGN(attack, action, "tickle");
758 for (i = 0; i < 3; ++i) {
759 if (EVTAG_ARRAY_ADD_VALUE(attack, how_often, i) == NULL) {
760 tt_abort_msg("Failed to add how_often.");
764 evutil_gettimeofday(&tv_start, NULL);
765 for (i = 0; i < 1000; ++i) {
766 run = EVTAG_ARRAY_ADD(msg, run);
768 tt_abort_msg("Failed to add run message.");
770 EVTAG_ASSIGN(run, how, "very fast but with some data in it");
771 EVTAG_ASSIGN(run, fixed_bytes,
772 (ev_uint8_t*)"012345678901234567890123");
774 if (EVTAG_ARRAY_ADD_VALUE(
775 run, notes, "this is my note") == NULL) {
776 tt_abort_msg("Failed to add note.");
778 if (EVTAG_ARRAY_ADD_VALUE(run, notes, "pps") == NULL) {
779 tt_abort_msg("Failed to add note");
782 EVTAG_ASSIGN(run, large_number, 0xdead0a0bcafebeefLL);
783 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xdead0a0b);
784 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xbeefcafe);
787 if (msg_complete(msg) == -1)
788 tt_abort_msg("Failed to make complete message.");
790 evtag_marshal_msg(tmp, 0xdeaf, msg);
792 if (evtag_peek(tmp, &tag) == -1)
793 tt_abort_msg("Failed to peak tag.");
796 TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag));
799 if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1)
800 tt_abort_msg("Failed to unmarshal message.");
802 evutil_gettimeofday(&tv_end, NULL);
803 evutil_timersub(&tv_end, &tv_start, &tv_end);
804 TT_BLATHER(("(%.1f us/add) ",
805 (float)tv_end.tv_sec/(float)i * 1000000.0 +
806 tv_end.tv_usec / (float)i));
808 if (!EVTAG_HAS(msg2, from_name) ||
809 !EVTAG_HAS(msg2, to_name) ||
810 !EVTAG_HAS(msg2, attack)) {
811 tt_abort_msg("Missing data structures.");
814 if (EVTAG_GET(msg2, attack, &attack) == -1) {
815 tt_abort_msg("Could not get attack.");
818 if (EVTAG_ARRAY_LEN(msg2, run) != i) {
819 tt_abort_msg("Wrong number of run messages.");
822 /* get the very first run message */
823 if (EVTAG_ARRAY_GET(msg2, run, 0, &run) == -1) {
824 tt_abort_msg("Failed to get run msg.");
826 /* verify the notes */
827 char *note_one, *note_two;
828 ev_uint64_t large_number;
829 ev_uint32_t short_number;
831 if (EVTAG_ARRAY_LEN(run, notes) != 2) {
832 tt_abort_msg("Wrong number of note strings.");
835 if (EVTAG_ARRAY_GET(run, notes, 0, ¬e_one) == -1 ||
836 EVTAG_ARRAY_GET(run, notes, 1, ¬e_two) == -1) {
837 tt_abort_msg("Could not get note strings.");
840 if (strcmp(note_one, "this is my note") ||
841 strcmp(note_two, "pps")) {
842 tt_abort_msg("Incorrect note strings encoded.");
845 if (EVTAG_GET(run, large_number, &large_number) == -1 ||
846 large_number != 0xdead0a0bcafebeefLL) {
847 tt_abort_msg("Incorrrect large_number.");
850 if (EVTAG_ARRAY_LEN(run, other_numbers) != 2) {
851 tt_abort_msg("Wrong number of other_numbers.");
855 run, other_numbers, 0, &short_number) == -1) {
856 tt_abort_msg("Could not get short number.");
858 tt_uint_op(short_number, ==, 0xdead0a0b);
861 tt_int_op(EVTAG_ARRAY_LEN(attack, how_often), ==, 3);
863 for (i = 0; i < 3; ++i) {
865 if (EVTAG_ARRAY_GET(attack, how_often, i, &res) == -1) {
866 TT_DIE(("Cannot get %dth how_often msg.", i));
869 TT_DIE(("Wrong message encoded %d != %d", i, res));
883 #define RPC_LEGACY(name) \
884 { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \
888 /* NO_PYTHON_EXISTS */
890 #define RPC_LEGACY(name) \
891 { #name, NULL, TT_SKIP, NULL, NULL }
895 struct testcase_t rpc_testcases[] = {
896 RPC_LEGACY(basic_test),
897 RPC_LEGACY(basic_message),
898 RPC_LEGACY(basic_client),
899 RPC_LEGACY(basic_queued_client),
900 RPC_LEGACY(basic_client_with_pause),
901 RPC_LEGACY(client_timeout),