2 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2010 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 static struct evhttp
*
75 http_setup(ev_uint16_t
*pport
)
77 struct evhttp
*myhttp
;
79 struct evhttp_bound_socket
*sock
;
81 myhttp
= evhttp_new(NULL
);
83 event_errx(1, "Could not start web server");
85 /* Try a few different ports */
86 sock
= evhttp_bind_socket_with_handle(myhttp
, "127.0.0.1", 0);
88 event_errx(1, "Couldn't open web port");
90 port
= regress_get_socket_port(evhttp_bound_socket_get_fd(sock
));
96 EVRPC_HEADER(Message
, msg
, kill
)
97 EVRPC_HEADER(NeverReply
, msg
, kill
)
99 EVRPC_GENERATE(Message
, msg
, kill
)
100 EVRPC_GENERATE(NeverReply
, msg
, kill
)
102 static int need_input_hook
= 0;
103 static int need_output_hook
= 0;
106 MessageCb(EVRPC_STRUCT(Message
)* rpc
, void *arg
)
108 struct kill
* kill_reply
= rpc
->reply
;
110 if (need_input_hook
) {
111 struct evhttp_request
* req
= EVRPC_REQUEST_HTTP(rpc
);
112 const char *header
= evhttp_find_header(
113 req
->input_headers
, "X-Hook");
114 assert(strcmp(header
, "input") == 0);
117 /* we just want to fill in some non-sense */
118 EVTAG_ASSIGN(kill_reply
, weapon
, "dagger");
119 EVTAG_ASSIGN(kill_reply
, action
, "wave around like an idiot");
121 /* no reply to the RPC */
122 EVRPC_REQUEST_DONE(rpc
);
125 static EVRPC_STRUCT(NeverReply
) *saved_rpc
;
128 NeverReplyCb(EVRPC_STRUCT(NeverReply
)* rpc
, void *arg
)
135 rpc_setup(struct evhttp
**phttp
, ev_uint16_t
*pport
, struct evrpc_base
**pbase
)
138 struct evhttp
*http
= NULL
;
139 struct evrpc_base
*base
= NULL
;
141 http
= http_setup(&port
);
142 base
= evrpc_init(http
);
144 EVRPC_REGISTER(base
, Message
, msg
, kill
, MessageCb
, NULL
);
145 EVRPC_REGISTER(base
, NeverReply
, msg
, kill
, NeverReplyCb
, NULL
);
152 need_output_hook
= 0;
156 rpc_teardown(struct evrpc_base
*base
)
158 assert(EVRPC_UNREGISTER(base
, Message
) == 0);
159 assert(EVRPC_UNREGISTER(base
, NeverReply
) == 0);
165 rpc_postrequest_failure(struct evhttp_request
*req
, void *arg
)
167 if (req
->response_code
!= HTTP_SERVUNAVAIL
) {
169 fprintf(stderr
, "FAILED (response code)\n");
174 event_loopexit(NULL
);
178 * Test a malformed payload submitted as an RPC
185 struct evhttp
*http
= NULL
;
186 struct evrpc_base
*base
= NULL
;
187 struct evhttp_connection
*evcon
= NULL
;
188 struct evhttp_request
*req
= NULL
;
190 rpc_setup(&http
, &port
, &base
);
192 evcon
= evhttp_connection_new("127.0.0.1", port
);
196 * At this point, we want to schedule an HTTP POST request
197 * server using our make request method.
200 req
= evhttp_request_new(rpc_postrequest_failure
, NULL
);
203 /* Add the information that we care about */
204 evhttp_add_header(req
->output_headers
, "Host", "somehost");
205 evbuffer_add_printf(req
->output_buffer
, "Some Nonsense");
207 if (evhttp_make_request(evcon
, req
,
209 "/.rpc.Message") == -1) {
217 evhttp_connection_free(evcon
);
221 tt_assert(test_ok
== 1);
228 rpc_postrequest_done(struct evhttp_request
*req
, void *arg
)
230 struct kill
* kill_reply
= NULL
;
232 if (req
->response_code
!= HTTP_OK
) {
233 fprintf(stderr
, "FAILED (response code)\n");
237 kill_reply
= kill_new();
239 if ((kill_unmarshal(kill_reply
, req
->input_buffer
)) == -1) {
240 fprintf(stderr
, "FAILED (unmarshal)\n");
244 kill_free(kill_reply
);
247 event_loopexit(NULL
);
251 rpc_basic_message(void)
254 struct evhttp
*http
= NULL
;
255 struct evrpc_base
*base
= NULL
;
256 struct evhttp_connection
*evcon
= NULL
;
257 struct evhttp_request
*req
= NULL
;
260 rpc_setup(&http
, &port
, &base
);
262 evcon
= evhttp_connection_new("127.0.0.1", port
);
266 * At this point, we want to schedule an HTTP POST request
267 * server using our make request method.
270 req
= evhttp_request_new(rpc_postrequest_done
, NULL
);
272 fprintf(stdout
, "FAILED\n");
276 /* Add the information that we care about */
277 evhttp_add_header(req
->output_headers
, "Host", "somehost");
279 /* set up the basic message */
281 EVTAG_ASSIGN(msg
, from_name
, "niels");
282 EVTAG_ASSIGN(msg
, to_name
, "tester");
283 msg_marshal(req
->output_buffer
, msg
);
286 if (evhttp_make_request(evcon
, req
,
288 "/.rpc.Message") == -1) {
289 fprintf(stdout
, "FAILED\n");
297 evhttp_connection_free(evcon
);
305 static struct evrpc_pool
*
306 rpc_pool_with_connection(ev_uint16_t port
)
308 struct evhttp_connection
*evcon
;
309 struct evrpc_pool
*pool
;
311 pool
= evrpc_pool_new(NULL
);
312 assert(pool
!= NULL
);
314 evcon
= evhttp_connection_new("127.0.0.1", port
);
315 assert(evcon
!= NULL
);
317 evrpc_pool_add_connection(pool
, evcon
);
323 GotKillCb(struct evrpc_status
*status
,
324 struct msg
*msg
, struct kill
*kill
, void *arg
)
329 if (need_output_hook
) {
330 struct evhttp_request
*req
= status
->http_req
;
331 const char *header
= evhttp_find_header(
332 req
->input_headers
, "X-Pool-Hook");
333 assert(strcmp(header
, "ran") == 0);
336 if (status
->error
!= EVRPC_STATUS_ERR_NONE
)
339 if (EVTAG_GET(kill
, weapon
, &weapon
) == -1) {
340 fprintf(stderr
, "get weapon\n");
343 if (EVTAG_GET(kill
, action
, &action
) == -1) {
344 fprintf(stderr
, "get action\n");
348 if (strcmp(weapon
, "dagger"))
351 if (strcmp(action
, "wave around like an idiot"))
357 event_loopexit(NULL
);
361 GotKillCbTwo(struct evrpc_status
*status
,
362 struct msg
*msg
, struct kill
*kill
, void *arg
)
367 if (status
->error
!= EVRPC_STATUS_ERR_NONE
)
370 if (EVTAG_GET(kill
, weapon
, &weapon
) == -1) {
371 fprintf(stderr
, "get weapon\n");
374 if (EVTAG_GET(kill
, action
, &action
) == -1) {
375 fprintf(stderr
, "get action\n");
379 if (strcmp(weapon
, "dagger"))
382 if (strcmp(action
, "wave around like an idiot"))
389 event_loopexit(NULL
);
393 rpc_hook_add_header(void *ctx
, struct evhttp_request
*req
,
394 struct evbuffer
*evbuf
, void *arg
)
396 const char *hook_type
= arg
;
397 if (strcmp("input", hook_type
) == 0)
398 evhttp_add_header(req
->input_headers
, "X-Hook", hook_type
);
400 evhttp_add_header(req
->output_headers
, "X-Hook", hook_type
);
402 assert(evrpc_hook_get_connection(ctx
) != NULL
);
404 return (EVRPC_CONTINUE
);
408 rpc_hook_add_meta(void *ctx
, struct evhttp_request
*req
,
409 struct evbuffer
*evbuf
, void *arg
)
411 evrpc_hook_add_meta(ctx
, "meta", "test", 5);
413 assert(evrpc_hook_get_connection(ctx
) != NULL
);
415 return (EVRPC_CONTINUE
);
419 rpc_hook_remove_header(void *ctx
, struct evhttp_request
*req
,
420 struct evbuffer
*evbuf
, void *arg
)
422 const char *header
= evhttp_find_header(req
->input_headers
, "X-Hook");
426 assert(header
!= NULL
);
427 assert(strcmp(header
, arg
) == 0);
429 evhttp_remove_header(req
->input_headers
, "X-Hook");
430 evhttp_add_header(req
->input_headers
, "X-Pool-Hook", "ran");
432 assert(evrpc_hook_find_meta(ctx
, "meta", &data
, &data_len
) == 0);
433 assert(data
!= NULL
);
434 assert(data_len
== 5);
436 assert(evrpc_hook_get_connection(ctx
) != NULL
);
438 return (EVRPC_CONTINUE
);
442 rpc_basic_client(void)
445 struct evhttp
*http
= NULL
;
446 struct evrpc_base
*base
= NULL
;
447 struct evrpc_pool
*pool
= NULL
;
448 struct msg
*msg
= NULL
;
449 struct kill
*kill
= NULL
;
451 rpc_setup(&http
, &port
, &base
);
454 need_output_hook
= 1;
456 assert(evrpc_add_hook(base
, EVRPC_INPUT
, rpc_hook_add_header
, (void*)"input")
458 assert(evrpc_add_hook(base
, EVRPC_OUTPUT
, rpc_hook_add_header
, (void*)"output")
461 pool
= rpc_pool_with_connection(port
);
463 assert(evrpc_add_hook(pool
, EVRPC_OUTPUT
, rpc_hook_add_meta
, NULL
));
464 assert(evrpc_add_hook(pool
, EVRPC_INPUT
, rpc_hook_remove_header
, (void*)"output"));
466 /* set up the basic message */
468 EVTAG_ASSIGN(msg
, from_name
, "niels");
469 EVTAG_ASSIGN(msg
, to_name
, "tester");
473 EVRPC_MAKE_REQUEST(Message
, pool
, msg
, kill
, GotKillCb
, NULL
);
479 tt_assert(test_ok
== 1);
481 /* we do it twice to make sure that reuse works correctly */
484 EVRPC_MAKE_REQUEST(Message
, pool
, msg
, kill
, GotKillCb
, NULL
);
488 tt_assert(test_ok
== 2);
490 /* we do it trice to make sure other stuff works, too */
494 struct evrpc_request_wrapper
*ctx
=
495 EVRPC_MAKE_CTX(Message
, msg
, kill
,
496 pool
, msg
, kill
, GotKillCb
, NULL
);
497 evrpc_make_request(ctx
);
504 tt_assert(test_ok
== 3);
513 evrpc_pool_free(pool
);
518 need_output_hook
= 0;
522 * We are testing that the second requests gets send over the same
523 * connection after the first RPCs completes.
526 rpc_basic_queued_client(void)
529 struct evhttp
*http
= NULL
;
530 struct evrpc_base
*base
= NULL
;
531 struct evrpc_pool
*pool
= NULL
;
532 struct msg
*msg
=NULL
;
533 struct kill
*kill_one
=NULL
, *kill_two
=NULL
;
535 rpc_setup(&http
, &port
, &base
);
537 pool
= rpc_pool_with_connection(port
);
539 /* set up the basic message */
541 EVTAG_ASSIGN(msg
, from_name
, "niels");
542 EVTAG_ASSIGN(msg
, to_name
, "tester");
544 kill_one
= kill_new();
545 kill_two
= kill_new();
547 EVRPC_MAKE_REQUEST(Message
, pool
, msg
, kill_one
, GotKillCbTwo
, NULL
);
548 EVRPC_MAKE_REQUEST(Message
, pool
, msg
, kill_two
, GotKillCb
, NULL
);
556 tt_assert(test_ok
== 2);
567 evrpc_pool_free(pool
);
573 GotErrorCb(struct evrpc_status
*status
,
574 struct msg
*msg
, struct kill
*kill
, void *arg
)
576 if (status
->error
!= EVRPC_STATUS_ERR_TIMEOUT
)
579 /* should never be complete but just to check */
580 if (kill_complete(kill
) == 0)
586 event_loopexit(NULL
);
589 /* we just pause the rpc and continue it in the next callback */
591 struct _rpc_hook_ctx
{
596 static int hook_pause_cb_called
=0;
599 rpc_hook_pause_cb(evutil_socket_t fd
, short what
, void *arg
)
601 struct _rpc_hook_ctx
*ctx
= arg
;
602 ++hook_pause_cb_called
;
603 evrpc_resume_request(ctx
->vbase
, ctx
->ctx
, EVRPC_CONTINUE
);
608 rpc_hook_pause(void *ctx
, struct evhttp_request
*req
, struct evbuffer
*evbuf
,
611 struct _rpc_hook_ctx
*tmp
= malloc(sizeof(*tmp
));
618 memset(&tv
, 0, sizeof(tv
));
619 event_once(-1, EV_TIMEOUT
, rpc_hook_pause_cb
, tmp
, &tv
);
624 rpc_basic_client_with_pause(void)
627 struct evhttp
*http
= NULL
;
628 struct evrpc_base
*base
= NULL
;
629 struct evrpc_pool
*pool
= NULL
;
630 struct msg
*msg
= NULL
;
631 struct kill
*kill
= NULL
;
633 rpc_setup(&http
, &port
, &base
);
635 assert(evrpc_add_hook(base
, EVRPC_INPUT
, rpc_hook_pause
, base
));
636 assert(evrpc_add_hook(base
, EVRPC_OUTPUT
, rpc_hook_pause
, base
));
638 pool
= rpc_pool_with_connection(port
);
640 assert(evrpc_add_hook(pool
, EVRPC_INPUT
, rpc_hook_pause
, pool
));
641 assert(evrpc_add_hook(pool
, EVRPC_OUTPUT
, rpc_hook_pause
, pool
));
643 /* set up the basic message */
645 EVTAG_ASSIGN(msg
, from_name
, "niels");
646 EVTAG_ASSIGN(msg
, to_name
, "tester");
650 EVRPC_MAKE_REQUEST(Message
, pool
, msg
, kill
, GotKillCb
, NULL
);
656 tt_int_op(test_ok
, ==, 1);
657 tt_int_op(hook_pause_cb_called
, ==, 4);
669 evrpc_pool_free(pool
);
675 rpc_client_timeout(void)
678 struct evhttp
*http
= NULL
;
679 struct evrpc_base
*base
= NULL
;
680 struct evrpc_pool
*pool
= NULL
;
681 struct msg
*msg
= NULL
;
682 struct kill
*kill
= NULL
;
684 rpc_setup(&http
, &port
, &base
);
686 pool
= rpc_pool_with_connection(port
);
688 /* set the timeout to 5 seconds */
689 evrpc_pool_set_timeout(pool
, 5);
691 /* set up the basic message */
693 EVTAG_ASSIGN(msg
, from_name
, "niels");
694 EVTAG_ASSIGN(msg
, to_name
, "tester");
698 EVRPC_MAKE_REQUEST(NeverReply
, pool
, msg
, kill
, GotErrorCb
, NULL
);
704 /* free the saved RPC structure up */
705 EVRPC_REQUEST_DONE(saved_rpc
);
709 tt_assert(test_ok
== 2);
718 evrpc_pool_free(pool
);
726 struct msg
*msg
= NULL
, *msg2
= NULL
;
727 struct kill
*attack
= NULL
;
728 struct run
*run
= NULL
;
729 struct evbuffer
*tmp
= evbuffer_new();
730 struct timeval tv_start
, tv_end
;
735 EVTAG_ASSIGN(msg
, from_name
, "niels");
736 EVTAG_ASSIGN(msg
, to_name
, "phoenix");
738 if (EVTAG_GET(msg
, attack
, &attack
) == -1) {
739 tt_abort_msg("Failed to set kill message.");
742 EVTAG_ASSIGN(attack
, weapon
, "feather");
743 EVTAG_ASSIGN(attack
, action
, "tickle");
744 for (i
= 0; i
< 3; ++i
) {
745 if (EVTAG_ARRAY_ADD_VALUE(attack
, how_often
, i
) == NULL
) {
746 tt_abort_msg("Failed to add how_often.");
750 evutil_gettimeofday(&tv_start
, NULL
);
751 for (i
= 0; i
< 1000; ++i
) {
752 run
= EVTAG_ARRAY_ADD(msg
, run
);
754 tt_abort_msg("Failed to add run message.");
756 EVTAG_ASSIGN(run
, how
, "very fast but with some data in it");
757 EVTAG_ASSIGN(run
, fixed_bytes
,
758 (ev_uint8_t
*)"012345678901234567890123");
760 if (EVTAG_ARRAY_ADD_VALUE(
761 run
, notes
, "this is my note") == NULL
) {
762 tt_abort_msg("Failed to add note.");
764 if (EVTAG_ARRAY_ADD_VALUE(run
, notes
, "pps") == NULL
) {
765 tt_abort_msg("Failed to add note");
768 EVTAG_ASSIGN(run
, large_number
, 0xdead0a0bcafebeefLL
);
769 EVTAG_ARRAY_ADD_VALUE(run
, other_numbers
, 0xdead0a0b);
770 EVTAG_ARRAY_ADD_VALUE(run
, other_numbers
, 0xbeefcafe);
773 if (msg_complete(msg
) == -1)
774 tt_abort_msg("Failed to make complete message.");
776 evtag_marshal_msg(tmp
, 0xdeaf, msg
);
778 if (evtag_peek(tmp
, &tag
) == -1)
779 tt_abort_msg("Failed to peak tag.");
782 TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag
));
785 if (evtag_unmarshal_msg(tmp
, 0xdeaf, msg2
) == -1)
786 tt_abort_msg("Failed to unmarshal message.");
788 evutil_gettimeofday(&tv_end
, NULL
);
789 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
790 TT_BLATHER(("(%.1f us/add) ",
791 (float)tv_end
.tv_sec
/(float)i
* 1000000.0 +
792 tv_end
.tv_usec
/ (float)i
));
794 if (!EVTAG_HAS(msg2
, from_name
) ||
795 !EVTAG_HAS(msg2
, to_name
) ||
796 !EVTAG_HAS(msg2
, attack
)) {
797 tt_abort_msg("Missing data structures.");
800 if (EVTAG_GET(msg2
, attack
, &attack
) == -1) {
801 tt_abort_msg("Could not get attack.");
804 if (EVTAG_ARRAY_LEN(msg2
, run
) != i
) {
805 tt_abort_msg("Wrong number of run messages.");
808 /* get the very first run message */
809 if (EVTAG_ARRAY_GET(msg2
, run
, 0, &run
) == -1) {
810 tt_abort_msg("Failed to get run msg.");
812 /* verify the notes */
813 char *note_one
, *note_two
;
814 ev_uint64_t large_number
;
815 ev_uint32_t short_number
;
817 if (EVTAG_ARRAY_LEN(run
, notes
) != 2) {
818 tt_abort_msg("Wrong number of note strings.");
821 if (EVTAG_ARRAY_GET(run
, notes
, 0, ¬e_one
) == -1 ||
822 EVTAG_ARRAY_GET(run
, notes
, 1, ¬e_two
) == -1) {
823 tt_abort_msg("Could not get note strings.");
826 if (strcmp(note_one
, "this is my note") ||
827 strcmp(note_two
, "pps")) {
828 tt_abort_msg("Incorrect note strings encoded.");
831 if (EVTAG_GET(run
, large_number
, &large_number
) == -1 ||
832 large_number
!= 0xdead0a0bcafebeefLL
) {
833 tt_abort_msg("Incorrrect large_number.");
836 if (EVTAG_ARRAY_LEN(run
, other_numbers
) != 2) {
837 tt_abort_msg("Wrong number of other_numbers.");
841 run
, other_numbers
, 0, &short_number
) == -1) {
842 tt_abort_msg("Could not get short number.");
844 tt_uint_op(short_number
, ==, 0xdead0a0b);
847 tt_int_op(EVTAG_ARRAY_LEN(attack
, how_often
), ==, 3);
849 for (i
= 0; i
< 3; ++i
) {
851 if (EVTAG_ARRAY_GET(attack
, how_often
, i
, &res
) == -1) {
852 TT_DIE(("Cannot get %dth how_often msg.", i
));
855 TT_DIE(("Wrong message encoded %d != %d", i
, res
));
869 #define RPC_LEGACY(name) \
870 { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \
874 struct testcase_t rpc_testcases
[] = {
875 RPC_LEGACY(basic_test
),
876 RPC_LEGACY(basic_message
),
877 RPC_LEGACY(basic_client
),
878 RPC_LEGACY(basic_queued_client
),
879 RPC_LEGACY(basic_client_with_pause
),
880 RPC_LEGACY(client_timeout
),