1 /* $NetBSD: regress_rpc.c,v 1.3 2013/04/12 21:49:54 christos Exp $ */
3 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /* The old tests here need assertions to work. */
37 #include "event2/event-config.h"
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: regress_rpc.c,v 1.3 2013/04/12 21:49:54 christos Exp $");
41 #include <sys/types.h>
43 #ifdef _EVENT_HAVE_SYS_TIME_H
46 #include <sys/queue.h>
48 #include <sys/socket.h>
60 #include "event2/buffer.h"
61 #include "event2/event.h"
62 #include "event2/event_compat.h"
63 #include "event2/http.h"
64 #include "event2/http_compat.h"
65 #include "event2/http_struct.h"
66 #include "event2/rpc.h"
67 #include "event2/rpc.h"
68 #include "event2/rpc_struct.h"
69 #include "event2/tag.h"
70 #include "log-internal.h"
72 #include "regress.gen.h"
75 #include "regress_testutils.h"
77 #ifndef NO_PYTHON_EXISTS
79 static struct evhttp
*
80 http_setup(ev_uint16_t
*pport
)
82 struct evhttp
*myhttp
;
84 struct evhttp_bound_socket
*sock
;
86 myhttp
= evhttp_new(NULL
);
88 event_errx(1, "Could not start web server");
90 /* Try a few different ports */
91 sock
= evhttp_bind_socket_with_handle(myhttp
, "127.0.0.1", 0);
93 event_errx(1, "Couldn't open web port");
95 port
= regress_get_socket_port(evhttp_bound_socket_get_fd(sock
));
101 EVRPC_HEADER(Message
, msg
, kill
)
102 EVRPC_HEADER(NeverReply
, msg
, kill
)
104 EVRPC_GENERATE(Message
, msg
, kill
)
105 EVRPC_GENERATE(NeverReply
, msg
, kill
)
107 static int need_input_hook
= 0;
108 static int need_output_hook
= 0;
111 MessageCb(EVRPC_STRUCT(Message
)* rpc
, void *arg
)
113 struct kill
* kill_reply
= rpc
->reply
;
115 if (need_input_hook
) {
116 struct evhttp_request
* req
= EVRPC_REQUEST_HTTP(rpc
);
117 const char *header
= evhttp_find_header(
118 req
->input_headers
, "X-Hook");
120 assert(strcmp(header
, "input") == 0);
123 /* we just want to fill in some non-sense */
124 EVTAG_ASSIGN(kill_reply
, weapon
, "dagger");
125 EVTAG_ASSIGN(kill_reply
, action
, "wave around like an idiot");
127 /* no reply to the RPC */
128 EVRPC_REQUEST_DONE(rpc
);
131 static EVRPC_STRUCT(NeverReply
) *saved_rpc
;
134 NeverReplyCb(EVRPC_STRUCT(NeverReply
)* rpc
, void *arg
)
141 rpc_setup(struct evhttp
**phttp
, ev_uint16_t
*pport
, struct evrpc_base
**pbase
)
144 struct evhttp
*http
= NULL
;
145 struct evrpc_base
*base
= NULL
;
147 http
= http_setup(&port
);
148 base
= evrpc_init(http
);
150 EVRPC_REGISTER(base
, Message
, msg
, kill
, MessageCb
, NULL
);
151 EVRPC_REGISTER(base
, NeverReply
, msg
, kill
, NeverReplyCb
, NULL
);
158 need_output_hook
= 0;
162 rpc_teardown(struct evrpc_base
*base
)
164 assert(EVRPC_UNREGISTER(base
, Message
) == 0);
165 assert(EVRPC_UNREGISTER(base
, NeverReply
) == 0);
171 rpc_postrequest_failure(struct evhttp_request
*req
, void *arg
)
173 if (req
->response_code
!= HTTP_SERVUNAVAIL
) {
175 fprintf(stderr
, "FAILED (response code)\n");
180 event_loopexit(NULL
);
184 * Test a malformed payload submitted as an RPC
191 struct evhttp
*http
= NULL
;
192 struct evrpc_base
*base
= NULL
;
193 struct evhttp_connection
*evcon
= NULL
;
194 struct evhttp_request
*req
= NULL
;
196 rpc_setup(&http
, &port
, &base
);
198 evcon
= evhttp_connection_new("127.0.0.1", port
);
202 * At this point, we want to schedule an HTTP POST request
203 * server using our make request method.
206 req
= evhttp_request_new(rpc_postrequest_failure
, NULL
);
209 /* Add the information that we care about */
210 evhttp_add_header(req
->output_headers
, "Host", "somehost");
211 evbuffer_add_printf(req
->output_buffer
, "Some Nonsense");
213 if (evhttp_make_request(evcon
, req
,
215 "/.rpc.Message") == -1) {
223 evhttp_connection_free(evcon
);
227 tt_assert(test_ok
== 1);
234 rpc_postrequest_done(struct evhttp_request
*req
, void *arg
)
236 struct kill
* kill_reply
= NULL
;
238 if (req
->response_code
!= HTTP_OK
) {
239 fprintf(stderr
, "FAILED (response code)\n");
243 kill_reply
= kill_new();
245 if ((kill_unmarshal(kill_reply
, req
->input_buffer
)) == -1) {
246 fprintf(stderr
, "FAILED (unmarshal)\n");
250 kill_free(kill_reply
);
253 event_loopexit(NULL
);
257 rpc_basic_message(void)
260 struct evhttp
*http
= NULL
;
261 struct evrpc_base
*base
= NULL
;
262 struct evhttp_connection
*evcon
= NULL
;
263 struct evhttp_request
*req
= NULL
;
266 rpc_setup(&http
, &port
, &base
);
268 evcon
= evhttp_connection_new("127.0.0.1", port
);
272 * At this point, we want to schedule an HTTP POST request
273 * server using our make request method.
276 req
= evhttp_request_new(rpc_postrequest_done
, NULL
);
278 fprintf(stdout
, "FAILED\n");
282 /* Add the information that we care about */
283 evhttp_add_header(req
->output_headers
, "Host", "somehost");
285 /* set up the basic message */
287 EVTAG_ASSIGN(msg
, from_name
, "niels");
288 EVTAG_ASSIGN(msg
, to_name
, "tester");
289 msg_marshal(req
->output_buffer
, msg
);
292 if (evhttp_make_request(evcon
, req
,
294 "/.rpc.Message") == -1) {
295 fprintf(stdout
, "FAILED\n");
303 evhttp_connection_free(evcon
);
311 static struct evrpc_pool
*
312 rpc_pool_with_connection(ev_uint16_t port
)
314 struct evhttp_connection
*evcon
;
315 struct evrpc_pool
*pool
;
317 pool
= evrpc_pool_new(NULL
);
318 assert(pool
!= NULL
);
320 evcon
= evhttp_connection_new("127.0.0.1", port
);
321 assert(evcon
!= NULL
);
323 evrpc_pool_add_connection(pool
, evcon
);
329 GotKillCb(struct evrpc_status
*status
,
330 struct msg
*msg
, struct kill
*xkill
, void *arg
)
335 if (need_output_hook
) {
336 struct evhttp_request
*req
= status
->http_req
;
337 const char *header
= evhttp_find_header(
338 req
->input_headers
, "X-Pool-Hook");
340 assert(strcmp(header
, "ran") == 0);
343 if (status
->error
!= EVRPC_STATUS_ERR_NONE
)
346 if (EVTAG_GET(xkill
, weapon
, &weapon
) == -1) {
347 fprintf(stderr
, "get weapon\n");
350 if (EVTAG_GET(xkill
, action
, &action
) == -1) {
351 fprintf(stderr
, "get action\n");
355 if (strcmp(weapon
, "dagger"))
358 if (strcmp(action
, "wave around like an idiot"))
364 event_loopexit(NULL
);
368 GotKillCbTwo(struct evrpc_status
*status
,
369 struct msg
*msg
, struct kill
*xkill
, void *arg
)
374 if (status
->error
!= EVRPC_STATUS_ERR_NONE
)
377 if (EVTAG_GET(xkill
, weapon
, &weapon
) == -1) {
378 fprintf(stderr
, "get weapon\n");
381 if (EVTAG_GET(xkill
, action
, &action
) == -1) {
382 fprintf(stderr
, "get action\n");
386 if (strcmp(weapon
, "dagger"))
389 if (strcmp(action
, "wave around like an idiot"))
396 event_loopexit(NULL
);
400 rpc_hook_add_header(void *ctx
, struct evhttp_request
*req
,
401 struct evbuffer
*evbuf
, void *arg
)
403 const char *hook_type
= arg
;
404 if (strcmp("input", hook_type
) == 0)
405 evhttp_add_header(req
->input_headers
, "X-Hook", hook_type
);
407 evhttp_add_header(req
->output_headers
, "X-Hook", hook_type
);
409 assert(evrpc_hook_get_connection(ctx
) != NULL
);
411 return (EVRPC_CONTINUE
);
415 rpc_hook_add_meta(void *ctx
, struct evhttp_request
*req
,
416 struct evbuffer
*evbuf
, void *arg
)
418 evrpc_hook_add_meta(ctx
, "meta", "test", 5);
420 assert(evrpc_hook_get_connection(ctx
) != NULL
);
422 return (EVRPC_CONTINUE
);
426 rpc_hook_remove_header(void *ctx
, struct evhttp_request
*req
,
427 struct evbuffer
*evbuf
, void *arg
)
429 const char *header
= evhttp_find_header(req
->input_headers
, "X-Hook");
433 assert(header
!= NULL
);
434 assert(strcmp(header
, arg
) == 0);
436 evhttp_remove_header(req
->input_headers
, "X-Hook");
437 evhttp_add_header(req
->input_headers
, "X-Pool-Hook", "ran");
439 assert(evrpc_hook_find_meta(ctx
, "meta", &data
, &data_len
) == 0);
440 assert(data
!= NULL
);
441 assert(data_len
== 5);
443 assert(evrpc_hook_get_connection(ctx
) != NULL
);
445 return (EVRPC_CONTINUE
);
449 rpc_basic_client(void)
452 struct evhttp
*http
= NULL
;
453 struct evrpc_base
*base
= NULL
;
454 struct evrpc_pool
*pool
= NULL
;
455 struct msg
*msg
= NULL
;
456 struct kill
*xkill
= NULL
;
458 rpc_setup(&http
, &port
, &base
);
461 need_output_hook
= 1;
463 assert(evrpc_add_hook(base
, EVRPC_INPUT
, rpc_hook_add_header
, __UNCONST("input"))
465 assert(evrpc_add_hook(base
, EVRPC_OUTPUT
, rpc_hook_add_header
, __UNCONST("output"))
468 pool
= rpc_pool_with_connection(port
);
470 assert(evrpc_add_hook(pool
, EVRPC_OUTPUT
, rpc_hook_add_meta
, NULL
));
471 assert(evrpc_add_hook(pool
, EVRPC_INPUT
, rpc_hook_remove_header
, __UNCONST("output")));
473 /* set up the basic message */
475 EVTAG_ASSIGN(msg
, from_name
, "niels");
476 EVTAG_ASSIGN(msg
, to_name
, "tester");
480 EVRPC_MAKE_REQUEST(Message
, pool
, msg
, xkill
, GotKillCb
, NULL
);
486 tt_assert(test_ok
== 1);
488 /* we do it twice to make sure that reuse works correctly */
491 EVRPC_MAKE_REQUEST(Message
, pool
, msg
, xkill
, GotKillCb
, NULL
);
495 tt_assert(test_ok
== 2);
497 /* we do it trice to make sure other stuff works, too */
501 struct evrpc_request_wrapper
*ctx
=
502 EVRPC_MAKE_CTX(Message
, msg
, kill
,
503 pool
, msg
, xkill
, GotKillCb
, NULL
);
504 evrpc_make_request(ctx
);
511 tt_assert(test_ok
== 3);
520 evrpc_pool_free(pool
);
525 need_output_hook
= 0;
529 * We are testing that the second requests gets send over the same
530 * connection after the first RPCs completes.
533 rpc_basic_queued_client(void)
536 struct evhttp
*http
= NULL
;
537 struct evrpc_base
*base
= NULL
;
538 struct evrpc_pool
*pool
= NULL
;
539 struct msg
*msg
=NULL
;
540 struct kill
*kill_one
=NULL
, *kill_two
=NULL
;
542 rpc_setup(&http
, &port
, &base
);
544 pool
= rpc_pool_with_connection(port
);
546 /* 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
*xkill
, void *arg
)
583 if (status
->error
!= EVRPC_STATUS_ERR_TIMEOUT
)
586 /* should never be complete but just to check */
587 if (kill_complete(xkill
) == 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
*xkill
= 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 */
652 EVTAG_ASSIGN(msg
, from_name
, "niels");
653 EVTAG_ASSIGN(msg
, to_name
, "tester");
657 EVRPC_MAKE_REQUEST(Message
, pool
, msg
, xkill
, GotKillCb
, NULL
);
663 tt_int_op(test_ok
, ==, 1);
664 tt_int_op(hook_pause_cb_called
, ==, 4);
676 evrpc_pool_free(pool
);
682 rpc_client_timeout(void)
685 struct evhttp
*http
= NULL
;
686 struct evrpc_base
*base
= NULL
;
687 struct evrpc_pool
*pool
= NULL
;
688 struct msg
*msg
= NULL
;
689 struct kill
*xkill
= NULL
;
691 rpc_setup(&http
, &port
, &base
);
693 pool
= rpc_pool_with_connection(port
);
695 /* set the timeout to 5 seconds */
696 evrpc_pool_set_timeout(pool
, 5);
698 /* set up the basic message */
700 EVTAG_ASSIGN(msg
, from_name
, "niels");
701 EVTAG_ASSIGN(msg
, to_name
, "tester");
705 EVRPC_MAKE_REQUEST(NeverReply
, pool
, msg
, xkill
, GotErrorCb
, NULL
);
711 /* free the saved RPC structure up */
712 EVRPC_REQUEST_DONE(saved_rpc
);
716 tt_assert(test_ok
== 2);
725 evrpc_pool_free(pool
);
733 struct msg
*msg
= NULL
, *msg2
= NULL
;
734 struct kill
*attack
= NULL
;
735 struct run
*run
= NULL
;
736 struct evbuffer
*tmp
= evbuffer_new();
737 struct timeval tv_start
, tv_end
;
745 EVTAG_ASSIGN(msg
, from_name
, "niels");
746 EVTAG_ASSIGN(msg
, to_name
, "phoenix");
748 if (EVTAG_GET(msg
, attack
, &attack
) == -1) {
749 tt_abort_msg("Failed to set kill message.");
752 EVTAG_ASSIGN(attack
, weapon
, "feather");
753 EVTAG_ASSIGN(attack
, action
, "tickle");
754 for (i
= 0; i
< 3; ++i
) {
755 if (EVTAG_ARRAY_ADD_VALUE(attack
, how_often
, i
) == NULL
) {
756 tt_abort_msg("Failed to add how_often.");
760 evutil_gettimeofday(&tv_start
, NULL
);
761 for (i
= 0; i
< 1000; ++i
) {
762 run
= EVTAG_ARRAY_ADD(msg
, run
);
764 tt_abort_msg("Failed to add run message.");
766 EVTAG_ASSIGN(run
, how
, "very fast but with some data in it");
767 EVTAG_ASSIGN(run
, fixed_bytes
,
768 __UNCONST("012345678901234567890123"));
770 if (EVTAG_ARRAY_ADD_VALUE(
771 run
, notes
, "this is my note") == NULL
) {
772 tt_abort_msg("Failed to add note.");
774 if (EVTAG_ARRAY_ADD_VALUE(run
, notes
, "pps") == NULL
) {
775 tt_abort_msg("Failed to add note");
778 EVTAG_ASSIGN(run
, large_number
, 0xdead0a0bcafebeefLL
);
779 EVTAG_ARRAY_ADD_VALUE(run
, other_numbers
, 0xdead0a0b);
780 EVTAG_ARRAY_ADD_VALUE(run
, other_numbers
, 0xbeefcafe);
783 if (msg_complete(msg
) == -1)
784 tt_abort_msg("Failed to make complete message.");
786 evtag_marshal_msg(tmp
, 0xdeaf, msg
);
788 if (evtag_peek(tmp
, &tag
) == -1)
789 tt_abort_msg("Failed to peak tag.");
792 TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag
));
795 if (evtag_unmarshal_msg(tmp
, 0xdeaf, msg2
) == -1)
796 tt_abort_msg("Failed to unmarshal message.");
798 evutil_gettimeofday(&tv_end
, NULL
);
799 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
800 TT_BLATHER(("(%.1f us/add) ",
801 (float)tv_end
.tv_sec
/(float)i
* 1000000.0 +
802 tv_end
.tv_usec
/ (float)i
));
804 if (!EVTAG_HAS(msg2
, from_name
) ||
805 !EVTAG_HAS(msg2
, to_name
) ||
806 !EVTAG_HAS(msg2
, attack
)) {
807 tt_abort_msg("Missing data structures.");
810 if (EVTAG_GET(msg2
, attack
, &attack
) == -1) {
811 tt_abort_msg("Could not get attack.");
814 if (EVTAG_ARRAY_LEN(msg2
, run
) != i
) {
815 tt_abort_msg("Wrong number of run messages.");
818 /* get the very first run message */
819 if (EVTAG_ARRAY_GET(msg2
, run
, 0, &run
) == -1) {
820 tt_abort_msg("Failed to get run msg.");
822 /* verify the notes */
823 char *note_one
, *note_two
;
824 ev_uint64_t large_number
;
825 ev_uint32_t short_number
;
827 if (EVTAG_ARRAY_LEN(run
, notes
) != 2) {
828 tt_abort_msg("Wrong number of note strings.");
831 if (EVTAG_ARRAY_GET(run
, notes
, 0, ¬e_one
) == -1 ||
832 EVTAG_ARRAY_GET(run
, notes
, 1, ¬e_two
) == -1) {
833 tt_abort_msg("Could not get note strings.");
836 if (strcmp(note_one
, "this is my note") ||
837 strcmp(note_two
, "pps")) {
838 tt_abort_msg("Incorrect note strings encoded.");
841 if (EVTAG_GET(run
, large_number
, &large_number
) == -1 ||
842 large_number
!= 0xdead0a0bcafebeefLL
) {
843 tt_abort_msg("Incorrrect large_number.");
846 if (EVTAG_ARRAY_LEN(run
, other_numbers
) != 2) {
847 tt_abort_msg("Wrong number of other_numbers.");
851 run
, other_numbers
, 0, &short_number
) == -1) {
852 tt_abort_msg("Could not get short number.");
854 tt_uint_op(short_number
, ==, 0xdead0a0b);
857 tt_int_op(EVTAG_ARRAY_LEN(attack
, how_often
), ==, 3);
859 for (i
= 0; i
< 3; ++i
) {
861 if (EVTAG_ARRAY_GET(attack
, how_often
, i
, &res
) == -1) {
862 TT_DIE(("Cannot get %dth how_often msg.", i
));
865 TT_DIE(("Wrong message encoded %d != %d", i
, res
));
879 #define RPC_LEGACY(name) \
880 { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \
884 /* NO_PYTHON_EXISTS */
886 #define RPC_LEGACY(name) \
887 { #name, NULL, TT_SKIP, NULL, NULL }
891 struct testcase_t rpc_testcases
[] = {
892 RPC_LEGACY(basic_test
),
893 RPC_LEGACY(basic_message
),
894 RPC_LEGACY(basic_client
),
895 RPC_LEGACY(basic_queued_client
),
896 RPC_LEGACY(basic_client_with_pause
),
897 RPC_LEGACY(client_timeout
),