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_struct.h"
65 #include "event2/tag.h"
66 #include "log-internal.h"
68 #include "regress.gen.h"
71 #include "regress_testutils.h"
73 #ifndef NO_PYTHON_EXISTS
75 static struct evhttp *
76 http_setup(ev_uint16_t *pport)
78 struct evhttp *myhttp;
80 struct evhttp_bound_socket *sock;
82 myhttp = evhttp_new(NULL);
84 event_errx(1, "Could not start web server");
86 /* Try a few different ports */
87 sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", 0);
89 event_errx(1, "Couldn't open web port");
91 port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
97 EVRPC_HEADER(Message, msg, kill)
98 EVRPC_HEADER(NeverReply, msg, kill)
100 EVRPC_GENERATE(Message, msg, kill)
101 EVRPC_GENERATE(NeverReply, msg, kill)
103 static int need_input_hook = 0;
104 static int need_output_hook = 0;
107 MessageCb(EVRPC_STRUCT(Message)* rpc, void *arg)
109 struct kill* kill_reply = rpc->reply;
111 if (need_input_hook) {
112 struct evhttp_request* req = EVRPC_REQUEST_HTTP(rpc);
113 const char *header = evhttp_find_header(
114 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");
336 assert(strcmp(header, "ran") == 0);
339 if (status->error != EVRPC_STATUS_ERR_NONE)
342 if (EVTAG_GET(kill, weapon, &weapon) == -1) {
343 fprintf(stderr, "get weapon\n");
346 if (EVTAG_GET(kill, action, &action) == -1) {
347 fprintf(stderr, "get action\n");
351 if (strcmp(weapon, "dagger"))
354 if (strcmp(action, "wave around like an idiot"))
360 event_loopexit(NULL);
364 GotKillCbTwo(struct evrpc_status *status,
365 struct msg *msg, struct kill *kill, void *arg)
370 if (status->error != EVRPC_STATUS_ERR_NONE)
373 if (EVTAG_GET(kill, weapon, &weapon) == -1) {
374 fprintf(stderr, "get weapon\n");
377 if (EVTAG_GET(kill, action, &action) == -1) {
378 fprintf(stderr, "get action\n");
382 if (strcmp(weapon, "dagger"))
385 if (strcmp(action, "wave around like an idiot"))
392 event_loopexit(NULL);
396 rpc_hook_add_header(void *ctx, struct evhttp_request *req,
397 struct evbuffer *evbuf, void *arg)
399 const char *hook_type = arg;
400 if (strcmp("input", hook_type) == 0)
401 evhttp_add_header(req->input_headers, "X-Hook", hook_type);
403 evhttp_add_header(req->output_headers, "X-Hook", hook_type);
405 assert(evrpc_hook_get_connection(ctx) != NULL);
407 return (EVRPC_CONTINUE);
411 rpc_hook_add_meta(void *ctx, struct evhttp_request *req,
412 struct evbuffer *evbuf, void *arg)
414 evrpc_hook_add_meta(ctx, "meta", "test", 5);
416 assert(evrpc_hook_get_connection(ctx) != NULL);
418 return (EVRPC_CONTINUE);
422 rpc_hook_remove_header(void *ctx, struct evhttp_request *req,
423 struct evbuffer *evbuf, void *arg)
425 const char *header = evhttp_find_header(req->input_headers, "X-Hook");
429 assert(header != NULL);
430 assert(strcmp(header, arg) == 0);
432 evhttp_remove_header(req->input_headers, "X-Hook");
433 evhttp_add_header(req->input_headers, "X-Pool-Hook", "ran");
435 assert(evrpc_hook_find_meta(ctx, "meta", &data, &data_len) == 0);
436 assert(data != NULL);
437 assert(data_len == 5);
439 assert(evrpc_hook_get_connection(ctx) != NULL);
441 return (EVRPC_CONTINUE);
445 rpc_basic_client(void)
448 struct evhttp *http = NULL;
449 struct evrpc_base *base = NULL;
450 struct evrpc_pool *pool = NULL;
451 struct msg *msg = NULL;
452 struct kill *kill = NULL;
454 rpc_setup(&http, &port, &base);
457 need_output_hook = 1;
459 assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_add_header, (void*)"input")
461 assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_add_header, (void*)"output")
464 pool = rpc_pool_with_connection(port);
467 assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL));
468 assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output"));
470 /* set up the basic message */
473 EVTAG_ASSIGN(msg, from_name, "niels");
474 EVTAG_ASSIGN(msg, to_name, "tester");
478 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
484 tt_assert(test_ok == 1);
486 /* we do it twice to make sure that reuse works correctly */
489 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
493 tt_assert(test_ok == 2);
495 /* we do it trice to make sure other stuff works, too */
499 struct evrpc_request_wrapper *ctx =
500 EVRPC_MAKE_CTX(Message, msg, kill,
501 pool, msg, kill, GotKillCb, NULL);
502 evrpc_make_request(ctx);
509 tt_assert(test_ok == 3);
518 evrpc_pool_free(pool);
523 need_output_hook = 0;
527 * We are testing that the second requests gets send over the same
528 * connection after the first RPCs completes.
531 rpc_basic_queued_client(void)
534 struct evhttp *http = NULL;
535 struct evrpc_base *base = NULL;
536 struct evrpc_pool *pool = NULL;
537 struct msg *msg=NULL;
538 struct kill *kill_one=NULL, *kill_two=NULL;
540 rpc_setup(&http, &port, &base);
542 pool = rpc_pool_with_connection(port);
545 /* set up the basic message */
548 EVTAG_ASSIGN(msg, from_name, "niels");
549 EVTAG_ASSIGN(msg, to_name, "tester");
551 kill_one = kill_new();
552 kill_two = kill_new();
554 EVRPC_MAKE_REQUEST(Message, pool, msg, kill_one, GotKillCbTwo, NULL);
555 EVRPC_MAKE_REQUEST(Message, pool, msg, kill_two, GotKillCb, NULL);
563 tt_assert(test_ok == 2);
574 evrpc_pool_free(pool);
580 GotErrorCb(struct evrpc_status *status,
581 struct msg *msg, struct kill *kill, void *arg)
583 if (status->error != EVRPC_STATUS_ERR_TIMEOUT)
586 /* should never be complete but just to check */
587 if (kill_complete(kill) == 0)
593 event_loopexit(NULL);
596 /* we just pause the rpc and continue it in the next callback */
598 struct rpc_hook_ctx_ {
603 static int hook_pause_cb_called=0;
606 rpc_hook_pause_cb(evutil_socket_t fd, short what, void *arg)
608 struct rpc_hook_ctx_ *ctx = arg;
609 ++hook_pause_cb_called;
610 evrpc_resume_request(ctx->vbase, ctx->ctx, EVRPC_CONTINUE);
615 rpc_hook_pause(void *ctx, struct evhttp_request *req, struct evbuffer *evbuf,
618 struct rpc_hook_ctx_ *tmp = malloc(sizeof(*tmp));
625 memset(&tv, 0, sizeof(tv));
626 event_once(-1, EV_TIMEOUT, rpc_hook_pause_cb, tmp, &tv);
631 rpc_basic_client_with_pause(void)
634 struct evhttp *http = NULL;
635 struct evrpc_base *base = NULL;
636 struct evrpc_pool *pool = NULL;
637 struct msg *msg = NULL;
638 struct kill *kill= NULL;
640 rpc_setup(&http, &port, &base);
642 assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_pause, base));
643 assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base));
645 pool = rpc_pool_with_connection(port);
647 assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool));
648 assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool));
650 /* set up the basic message */
653 EVTAG_ASSIGN(msg, from_name, "niels");
654 EVTAG_ASSIGN(msg, to_name, "tester");
658 EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
664 tt_int_op(test_ok, ==, 1);
665 tt_int_op(hook_pause_cb_called, ==, 4);
677 evrpc_pool_free(pool);
683 rpc_client_timeout(void)
686 struct evhttp *http = NULL;
687 struct evrpc_base *base = NULL;
688 struct evrpc_pool *pool = NULL;
689 struct msg *msg = NULL;
690 struct kill *kill = NULL;
692 rpc_setup(&http, &port, &base);
694 pool = rpc_pool_with_connection(port);
697 /* set the timeout to 1 second. */
698 evrpc_pool_set_timeout(pool, 1);
700 /* set up the basic message */
703 EVTAG_ASSIGN(msg, from_name, "niels");
704 EVTAG_ASSIGN(msg, to_name, "tester");
708 EVRPC_MAKE_REQUEST(NeverReply, pool, msg, kill, GotErrorCb, NULL);
714 /* free the saved RPC structure up */
715 EVRPC_REQUEST_DONE(saved_rpc);
719 tt_assert(test_ok == 2);
728 evrpc_pool_free(pool);
736 struct msg *msg = NULL, *msg2 = NULL;
737 struct kill *attack = NULL;
738 struct run *run = NULL;
739 struct evbuffer *tmp = evbuffer_new();
740 struct timeval tv_start, tv_end;
748 EVTAG_ASSIGN(msg, from_name, "niels");
749 EVTAG_ASSIGN(msg, to_name, "phoenix");
751 if (EVTAG_GET(msg, attack, &attack) == -1) {
752 tt_abort_msg("Failed to set kill message.");
755 EVTAG_ASSIGN(attack, weapon, "feather");
756 EVTAG_ASSIGN(attack, action, "tickle");
757 for (i = 0; i < 3; ++i) {
758 if (EVTAG_ARRAY_ADD_VALUE(attack, how_often, i) == NULL) {
759 tt_abort_msg("Failed to add how_often.");
763 evutil_gettimeofday(&tv_start, NULL);
764 for (i = 0; i < 1000; ++i) {
765 run = EVTAG_ARRAY_ADD(msg, run);
767 tt_abort_msg("Failed to add run message.");
769 EVTAG_ASSIGN(run, how, "very fast but with some data in it");
770 EVTAG_ASSIGN(run, fixed_bytes,
771 (ev_uint8_t*)"012345678901234567890123");
773 if (EVTAG_ARRAY_ADD_VALUE(
774 run, notes, "this is my note") == NULL) {
775 tt_abort_msg("Failed to add note.");
777 if (EVTAG_ARRAY_ADD_VALUE(run, notes, "pps") == NULL) {
778 tt_abort_msg("Failed to add note");
781 EVTAG_ASSIGN(run, large_number, 0xdead0a0bcafebeefLL);
782 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xdead0a0b);
783 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xbeefcafe);
786 if (msg_complete(msg) == -1)
787 tt_abort_msg("Failed to make complete message.");
789 evtag_marshal_msg(tmp, 0xdeaf, msg);
791 if (evtag_peek(tmp, &tag) == -1)
792 tt_abort_msg("Failed to peak tag.");
795 TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag));
798 if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1)
799 tt_abort_msg("Failed to unmarshal message.");
801 evutil_gettimeofday(&tv_end, NULL);
802 evutil_timersub(&tv_end, &tv_start, &tv_end);
803 TT_BLATHER(("(%.1f us/add) ",
804 (float)tv_end.tv_sec/(float)i * 1000000.0 +
805 tv_end.tv_usec / (float)i));
807 if (!EVTAG_HAS(msg2, from_name) ||
808 !EVTAG_HAS(msg2, to_name) ||
809 !EVTAG_HAS(msg2, attack)) {
810 tt_abort_msg("Missing data structures.");
813 if (EVTAG_GET(msg2, attack, &attack) == -1) {
814 tt_abort_msg("Could not get attack.");
817 if (EVTAG_ARRAY_LEN(msg2, run) != i) {
818 tt_abort_msg("Wrong number of run messages.");
821 /* get the very first run message */
822 if (EVTAG_ARRAY_GET(msg2, run, 0, &run) == -1) {
823 tt_abort_msg("Failed to get run msg.");
825 /* verify the notes */
826 char *note_one, *note_two;
827 ev_uint64_t large_number;
828 ev_uint32_t short_number;
830 if (EVTAG_ARRAY_LEN(run, notes) != 2) {
831 tt_abort_msg("Wrong number of note strings.");
834 if (EVTAG_ARRAY_GET(run, notes, 0, ¬e_one) == -1 ||
835 EVTAG_ARRAY_GET(run, notes, 1, ¬e_two) == -1) {
836 tt_abort_msg("Could not get note strings.");
839 if (strcmp(note_one, "this is my note") ||
840 strcmp(note_two, "pps")) {
841 tt_abort_msg("Incorrect note strings encoded.");
844 if (EVTAG_GET(run, large_number, &large_number) == -1 ||
845 large_number != 0xdead0a0bcafebeefLL) {
846 tt_abort_msg("Incorrrect large_number.");
849 if (EVTAG_ARRAY_LEN(run, other_numbers) != 2) {
850 tt_abort_msg("Wrong number of other_numbers.");
854 run, other_numbers, 0, &short_number) == -1) {
855 tt_abort_msg("Could not get short number.");
857 tt_uint_op(short_number, ==, 0xdead0a0b);
860 tt_int_op(EVTAG_ARRAY_LEN(attack, how_often), ==, 3);
862 for (i = 0; i < 3; ++i) {
864 if (EVTAG_ARRAY_GET(attack, how_often, i, &res) == -1) {
865 TT_DIE(("Cannot get %dth how_often msg.", i));
868 TT_DIE(("Wrong message encoded %d != %d", i, res));
883 rpc_invalid_type(void)
886 struct evhttp *http = NULL;
887 struct evrpc_base *base = NULL;
888 struct evhttp_connection *evcon = NULL;
889 struct evhttp_request *req = NULL;
891 rpc_setup(&http, &port, &base);
893 evcon = evhttp_connection_new("127.0.0.1", port);
897 * At this point, we want to schedule an HTTP POST request
898 * server using our make request method.
901 req = evhttp_request_new(rpc_postrequest_failure, NULL);
904 /* Add the information that we care about */
905 evhttp_add_header(req->output_headers, "Host", "somehost");
906 evbuffer_add_printf(req->output_buffer, "Some Nonsense");
908 if (evhttp_make_request(evcon, req,
910 "/.rpc.Message") == -1) {
918 evhttp_connection_free(evcon);
922 tt_assert(test_ok == 1);
929 #define RPC_LEGACY(name) \
930 { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \
934 /* NO_PYTHON_EXISTS */
936 #define RPC_LEGACY(name) \
937 { #name, NULL, TT_SKIP, NULL, NULL }
941 struct testcase_t rpc_testcases[] = {
942 RPC_LEGACY(basic_test),
943 RPC_LEGACY(basic_message),
944 RPC_LEGACY(basic_client),
945 RPC_LEGACY(basic_queued_client),
946 RPC_LEGACY(basic_client_with_pause),
947 RPC_LEGACY(invalid_type),
948 RPC_LEGACY(client_timeout),