12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert *
52b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert * are met:
82b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert * derived from this software without specific prior written permission.
152b15cb3dSCy Schubert *
162b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert */
272b15cb3dSCy Schubert
282b15cb3dSCy Schubert /* The old tests here need assertions to work. */
292b15cb3dSCy Schubert #undef NDEBUG
302b15cb3dSCy Schubert
312b15cb3dSCy Schubert #ifdef _WIN32
322b15cb3dSCy Schubert #include <winsock2.h>
332b15cb3dSCy Schubert #include <windows.h>
342b15cb3dSCy Schubert #endif
352b15cb3dSCy Schubert
362b15cb3dSCy Schubert #include "event2/event-config.h"
372b15cb3dSCy Schubert
382b15cb3dSCy Schubert #include <sys/types.h>
392b15cb3dSCy Schubert #include <sys/stat.h>
402b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
412b15cb3dSCy Schubert #include <sys/time.h>
422b15cb3dSCy Schubert #endif
432b15cb3dSCy Schubert #include <sys/queue.h>
442b15cb3dSCy Schubert #ifndef _WIN32
452b15cb3dSCy Schubert #include <sys/socket.h>
462b15cb3dSCy Schubert #include <signal.h>
472b15cb3dSCy Schubert #include <unistd.h>
482b15cb3dSCy Schubert #include <netdb.h>
492b15cb3dSCy Schubert #endif
502b15cb3dSCy Schubert #include <fcntl.h>
512b15cb3dSCy Schubert #include <stdlib.h>
522b15cb3dSCy Schubert #include <stdio.h>
532b15cb3dSCy Schubert #include <string.h>
542b15cb3dSCy Schubert #include <errno.h>
552b15cb3dSCy Schubert #include <assert.h>
562b15cb3dSCy Schubert
572b15cb3dSCy Schubert #include "event2/buffer.h"
582b15cb3dSCy Schubert #include "event2/event.h"
592b15cb3dSCy Schubert #include "event2/event_compat.h"
602b15cb3dSCy Schubert #include "event2/http.h"
612b15cb3dSCy Schubert #include "event2/http_compat.h"
622b15cb3dSCy Schubert #include "event2/http_struct.h"
632b15cb3dSCy Schubert #include "event2/rpc.h"
642b15cb3dSCy Schubert #include "event2/rpc_struct.h"
652b15cb3dSCy Schubert #include "event2/tag.h"
662b15cb3dSCy Schubert #include "log-internal.h"
672b15cb3dSCy Schubert
682b15cb3dSCy Schubert #include "regress.gen.h"
692b15cb3dSCy Schubert
702b15cb3dSCy Schubert #include "regress.h"
712b15cb3dSCy Schubert #include "regress_testutils.h"
722b15cb3dSCy Schubert
732b15cb3dSCy Schubert #ifndef NO_PYTHON_EXISTS
742b15cb3dSCy Schubert
752b15cb3dSCy Schubert static struct evhttp *
http_setup(ev_uint16_t * pport)762b15cb3dSCy Schubert http_setup(ev_uint16_t *pport)
772b15cb3dSCy Schubert {
782b15cb3dSCy Schubert struct evhttp *myhttp;
792b15cb3dSCy Schubert ev_uint16_t port;
802b15cb3dSCy Schubert struct evhttp_bound_socket *sock;
812b15cb3dSCy Schubert
822b15cb3dSCy Schubert myhttp = evhttp_new(NULL);
832b15cb3dSCy Schubert if (!myhttp)
842b15cb3dSCy Schubert event_errx(1, "Could not start web server");
852b15cb3dSCy Schubert
862b15cb3dSCy Schubert /* Try a few different ports */
872b15cb3dSCy Schubert sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", 0);
882b15cb3dSCy Schubert if (!sock)
892b15cb3dSCy Schubert event_errx(1, "Couldn't open web port");
902b15cb3dSCy Schubert
912b15cb3dSCy Schubert port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
922b15cb3dSCy Schubert
932b15cb3dSCy Schubert *pport = port;
942b15cb3dSCy Schubert return (myhttp);
952b15cb3dSCy Schubert }
962b15cb3dSCy Schubert
972b15cb3dSCy Schubert EVRPC_HEADER(Message, msg, kill)
982b15cb3dSCy Schubert EVRPC_HEADER(NeverReply, msg, kill)
992b15cb3dSCy Schubert
1002b15cb3dSCy Schubert EVRPC_GENERATE(Message, msg, kill)
1012b15cb3dSCy Schubert EVRPC_GENERATE(NeverReply, msg, kill)
1022b15cb3dSCy Schubert
1032b15cb3dSCy Schubert static int need_input_hook = 0;
1042b15cb3dSCy Schubert static int need_output_hook = 0;
1052b15cb3dSCy Schubert
1062b15cb3dSCy Schubert static void
MessageCb(EVRPC_STRUCT (Message)* rpc,void * arg)1072b15cb3dSCy Schubert MessageCb(EVRPC_STRUCT(Message)* rpc, void *arg)
1082b15cb3dSCy Schubert {
1092b15cb3dSCy Schubert struct kill* kill_reply = rpc->reply;
1102b15cb3dSCy Schubert
1112b15cb3dSCy Schubert if (need_input_hook) {
1122b15cb3dSCy Schubert struct evhttp_request* req = EVRPC_REQUEST_HTTP(rpc);
1132b15cb3dSCy Schubert const char *header = evhttp_find_header(
1142b15cb3dSCy Schubert req->input_headers, "X-Hook");
1152b15cb3dSCy Schubert assert(header);
1162b15cb3dSCy Schubert assert(strcmp(header, "input") == 0);
1172b15cb3dSCy Schubert }
1182b15cb3dSCy Schubert
1192b15cb3dSCy Schubert /* we just want to fill in some non-sense */
1202b15cb3dSCy Schubert EVTAG_ASSIGN(kill_reply, weapon, "dagger");
1212b15cb3dSCy Schubert EVTAG_ASSIGN(kill_reply, action, "wave around like an idiot");
1222b15cb3dSCy Schubert
1232b15cb3dSCy Schubert /* no reply to the RPC */
1242b15cb3dSCy Schubert EVRPC_REQUEST_DONE(rpc);
1252b15cb3dSCy Schubert }
1262b15cb3dSCy Schubert
EVRPC_STRUCT(NeverReply)1272b15cb3dSCy Schubert static EVRPC_STRUCT(NeverReply) *saved_rpc;
1282b15cb3dSCy Schubert
1292b15cb3dSCy Schubert static void
1302b15cb3dSCy Schubert NeverReplyCb(EVRPC_STRUCT(NeverReply)* rpc, void *arg)
1312b15cb3dSCy Schubert {
1322b15cb3dSCy Schubert test_ok += 1;
1332b15cb3dSCy Schubert saved_rpc = rpc;
1342b15cb3dSCy Schubert }
1352b15cb3dSCy Schubert
1362b15cb3dSCy Schubert static void
rpc_setup(struct evhttp ** phttp,ev_uint16_t * pport,struct evrpc_base ** pbase)1372b15cb3dSCy Schubert rpc_setup(struct evhttp **phttp, ev_uint16_t *pport, struct evrpc_base **pbase)
1382b15cb3dSCy Schubert {
1392b15cb3dSCy Schubert ev_uint16_t port;
1402b15cb3dSCy Schubert struct evhttp *http = NULL;
1412b15cb3dSCy Schubert struct evrpc_base *base = NULL;
1422b15cb3dSCy Schubert
1432b15cb3dSCy Schubert http = http_setup(&port);
1442b15cb3dSCy Schubert base = evrpc_init(http);
1452b15cb3dSCy Schubert
1462b15cb3dSCy Schubert EVRPC_REGISTER(base, Message, msg, kill, MessageCb, NULL);
1472b15cb3dSCy Schubert EVRPC_REGISTER(base, NeverReply, msg, kill, NeverReplyCb, NULL);
1482b15cb3dSCy Schubert
1492b15cb3dSCy Schubert *phttp = http;
1502b15cb3dSCy Schubert *pport = port;
1512b15cb3dSCy Schubert *pbase = base;
1522b15cb3dSCy Schubert
1532b15cb3dSCy Schubert need_input_hook = 0;
1542b15cb3dSCy Schubert need_output_hook = 0;
1552b15cb3dSCy Schubert }
1562b15cb3dSCy Schubert
1572b15cb3dSCy Schubert static void
rpc_teardown(struct evrpc_base * base)1582b15cb3dSCy Schubert rpc_teardown(struct evrpc_base *base)
1592b15cb3dSCy Schubert {
1602b15cb3dSCy Schubert assert(EVRPC_UNREGISTER(base, Message) == 0);
1612b15cb3dSCy Schubert assert(EVRPC_UNREGISTER(base, NeverReply) == 0);
1622b15cb3dSCy Schubert
1632b15cb3dSCy Schubert evrpc_free(base);
1642b15cb3dSCy Schubert }
1652b15cb3dSCy Schubert
1662b15cb3dSCy Schubert static void
rpc_postrequest_failure(struct evhttp_request * req,void * arg)1672b15cb3dSCy Schubert rpc_postrequest_failure(struct evhttp_request *req, void *arg)
1682b15cb3dSCy Schubert {
1692b15cb3dSCy Schubert if (req->response_code != HTTP_SERVUNAVAIL) {
1702b15cb3dSCy Schubert
1712b15cb3dSCy Schubert fprintf(stderr, "FAILED (response code)\n");
1722b15cb3dSCy Schubert exit(1);
1732b15cb3dSCy Schubert }
1742b15cb3dSCy Schubert
1752b15cb3dSCy Schubert test_ok = 1;
1762b15cb3dSCy Schubert event_loopexit(NULL);
1772b15cb3dSCy Schubert }
1782b15cb3dSCy Schubert
1792b15cb3dSCy Schubert /*
1802b15cb3dSCy Schubert * Test a malformed payload submitted as an RPC
1812b15cb3dSCy Schubert */
1822b15cb3dSCy Schubert
1832b15cb3dSCy Schubert static void
rpc_basic_test(void)1842b15cb3dSCy Schubert rpc_basic_test(void)
1852b15cb3dSCy Schubert {
1862b15cb3dSCy Schubert ev_uint16_t port;
1872b15cb3dSCy Schubert struct evhttp *http = NULL;
1882b15cb3dSCy Schubert struct evrpc_base *base = NULL;
1892b15cb3dSCy Schubert struct evhttp_connection *evcon = NULL;
1902b15cb3dSCy Schubert struct evhttp_request *req = NULL;
1912b15cb3dSCy Schubert
1922b15cb3dSCy Schubert rpc_setup(&http, &port, &base);
1932b15cb3dSCy Schubert
1942b15cb3dSCy Schubert evcon = evhttp_connection_new("127.0.0.1", port);
1952b15cb3dSCy Schubert tt_assert(evcon);
1962b15cb3dSCy Schubert
1972b15cb3dSCy Schubert /*
1982b15cb3dSCy Schubert * At this point, we want to schedule an HTTP POST request
1992b15cb3dSCy Schubert * server using our make request method.
2002b15cb3dSCy Schubert */
2012b15cb3dSCy Schubert
2022b15cb3dSCy Schubert req = evhttp_request_new(rpc_postrequest_failure, NULL);
2032b15cb3dSCy Schubert tt_assert(req);
2042b15cb3dSCy Schubert
2052b15cb3dSCy Schubert /* Add the information that we care about */
2062b15cb3dSCy Schubert evhttp_add_header(req->output_headers, "Host", "somehost");
2072b15cb3dSCy Schubert evbuffer_add_printf(req->output_buffer, "Some Nonsense");
2082b15cb3dSCy Schubert
2092b15cb3dSCy Schubert if (evhttp_make_request(evcon, req,
2102b15cb3dSCy Schubert EVHTTP_REQ_POST,
2112b15cb3dSCy Schubert "/.rpc.Message") == -1) {
2122b15cb3dSCy Schubert tt_abort();
2132b15cb3dSCy Schubert }
2142b15cb3dSCy Schubert
2152b15cb3dSCy Schubert test_ok = 0;
2162b15cb3dSCy Schubert
2172b15cb3dSCy Schubert event_dispatch();
2182b15cb3dSCy Schubert
2192b15cb3dSCy Schubert evhttp_connection_free(evcon);
2202b15cb3dSCy Schubert
2212b15cb3dSCy Schubert rpc_teardown(base);
2222b15cb3dSCy Schubert
2232b15cb3dSCy Schubert tt_assert(test_ok == 1);
2242b15cb3dSCy Schubert
2252b15cb3dSCy Schubert end:
2262b15cb3dSCy Schubert evhttp_free(http);
2272b15cb3dSCy Schubert }
2282b15cb3dSCy Schubert
2292b15cb3dSCy Schubert static void
rpc_postrequest_done(struct evhttp_request * req,void * arg)2302b15cb3dSCy Schubert rpc_postrequest_done(struct evhttp_request *req, void *arg)
2312b15cb3dSCy Schubert {
2322b15cb3dSCy Schubert struct kill* kill_reply = NULL;
2332b15cb3dSCy Schubert
2342b15cb3dSCy Schubert if (req->response_code != HTTP_OK) {
2352b15cb3dSCy Schubert fprintf(stderr, "FAILED (response code)\n");
2362b15cb3dSCy Schubert exit(1);
2372b15cb3dSCy Schubert }
2382b15cb3dSCy Schubert
2392b15cb3dSCy Schubert kill_reply = kill_new();
2402b15cb3dSCy Schubert
2412b15cb3dSCy Schubert if ((kill_unmarshal(kill_reply, req->input_buffer)) == -1) {
2422b15cb3dSCy Schubert fprintf(stderr, "FAILED (unmarshal)\n");
2432b15cb3dSCy Schubert exit(1);
2442b15cb3dSCy Schubert }
2452b15cb3dSCy Schubert
2462b15cb3dSCy Schubert kill_free(kill_reply);
2472b15cb3dSCy Schubert
2482b15cb3dSCy Schubert test_ok = 1;
2492b15cb3dSCy Schubert event_loopexit(NULL);
2502b15cb3dSCy Schubert }
2512b15cb3dSCy Schubert
2522b15cb3dSCy Schubert static void
rpc_basic_message(void)2532b15cb3dSCy Schubert rpc_basic_message(void)
2542b15cb3dSCy Schubert {
2552b15cb3dSCy Schubert ev_uint16_t port;
2562b15cb3dSCy Schubert struct evhttp *http = NULL;
2572b15cb3dSCy Schubert struct evrpc_base *base = NULL;
2582b15cb3dSCy Schubert struct evhttp_connection *evcon = NULL;
2592b15cb3dSCy Schubert struct evhttp_request *req = NULL;
2602b15cb3dSCy Schubert struct msg *msg;
2612b15cb3dSCy Schubert
2622b15cb3dSCy Schubert rpc_setup(&http, &port, &base);
2632b15cb3dSCy Schubert
2642b15cb3dSCy Schubert evcon = evhttp_connection_new("127.0.0.1", port);
2652b15cb3dSCy Schubert tt_assert(evcon);
2662b15cb3dSCy Schubert
2672b15cb3dSCy Schubert /*
2682b15cb3dSCy Schubert * At this point, we want to schedule an HTTP POST request
2692b15cb3dSCy Schubert * server using our make request method.
2702b15cb3dSCy Schubert */
2712b15cb3dSCy Schubert
2722b15cb3dSCy Schubert req = evhttp_request_new(rpc_postrequest_done, NULL);
2732b15cb3dSCy Schubert if (req == NULL) {
2742b15cb3dSCy Schubert fprintf(stdout, "FAILED\n");
2752b15cb3dSCy Schubert exit(1);
2762b15cb3dSCy Schubert }
2772b15cb3dSCy Schubert
2782b15cb3dSCy Schubert /* Add the information that we care about */
2792b15cb3dSCy Schubert evhttp_add_header(req->output_headers, "Host", "somehost");
2802b15cb3dSCy Schubert
2812b15cb3dSCy Schubert /* set up the basic message */
2822b15cb3dSCy Schubert msg = msg_new();
2832b15cb3dSCy Schubert EVTAG_ASSIGN(msg, from_name, "niels");
2842b15cb3dSCy Schubert EVTAG_ASSIGN(msg, to_name, "tester");
2852b15cb3dSCy Schubert msg_marshal(req->output_buffer, msg);
2862b15cb3dSCy Schubert msg_free(msg);
2872b15cb3dSCy Schubert
2882b15cb3dSCy Schubert if (evhttp_make_request(evcon, req,
2892b15cb3dSCy Schubert EVHTTP_REQ_POST,
2902b15cb3dSCy Schubert "/.rpc.Message") == -1) {
2912b15cb3dSCy Schubert fprintf(stdout, "FAILED\n");
2922b15cb3dSCy Schubert exit(1);
2932b15cb3dSCy Schubert }
2942b15cb3dSCy Schubert
2952b15cb3dSCy Schubert test_ok = 0;
2962b15cb3dSCy Schubert
2972b15cb3dSCy Schubert event_dispatch();
2982b15cb3dSCy Schubert
2992b15cb3dSCy Schubert evhttp_connection_free(evcon);
3002b15cb3dSCy Schubert
3012b15cb3dSCy Schubert rpc_teardown(base);
3022b15cb3dSCy Schubert
3032b15cb3dSCy Schubert end:
3042b15cb3dSCy Schubert evhttp_free(http);
3052b15cb3dSCy Schubert }
3062b15cb3dSCy Schubert
3072b15cb3dSCy Schubert static struct evrpc_pool *
rpc_pool_with_connection(ev_uint16_t port)3082b15cb3dSCy Schubert rpc_pool_with_connection(ev_uint16_t port)
3092b15cb3dSCy Schubert {
3102b15cb3dSCy Schubert struct evhttp_connection *evcon;
3112b15cb3dSCy Schubert struct evrpc_pool *pool;
3122b15cb3dSCy Schubert
3132b15cb3dSCy Schubert pool = evrpc_pool_new(NULL);
3142b15cb3dSCy Schubert assert(pool != NULL);
3152b15cb3dSCy Schubert
3162b15cb3dSCy Schubert evcon = evhttp_connection_new("127.0.0.1", port);
3172b15cb3dSCy Schubert assert(evcon != NULL);
3182b15cb3dSCy Schubert
3192b15cb3dSCy Schubert evrpc_pool_add_connection(pool, evcon);
3202b15cb3dSCy Schubert
3212b15cb3dSCy Schubert return (pool);
3222b15cb3dSCy Schubert }
3232b15cb3dSCy Schubert
3242b15cb3dSCy Schubert static void
GotKillCb(struct evrpc_status * status,struct msg * msg,struct kill * kill,void * arg)3252b15cb3dSCy Schubert GotKillCb(struct evrpc_status *status,
3262b15cb3dSCy Schubert struct msg *msg, struct kill *kill, void *arg)
3272b15cb3dSCy Schubert {
3282b15cb3dSCy Schubert char *weapon;
3292b15cb3dSCy Schubert char *action;
3302b15cb3dSCy Schubert
3312b15cb3dSCy Schubert if (need_output_hook) {
3322b15cb3dSCy Schubert struct evhttp_request *req = status->http_req;
3332b15cb3dSCy Schubert const char *header = evhttp_find_header(
3342b15cb3dSCy Schubert req->input_headers, "X-Pool-Hook");
3352b15cb3dSCy Schubert assert(header);
3362b15cb3dSCy Schubert assert(strcmp(header, "ran") == 0);
3372b15cb3dSCy Schubert }
3382b15cb3dSCy Schubert
3392b15cb3dSCy Schubert if (status->error != EVRPC_STATUS_ERR_NONE)
3402b15cb3dSCy Schubert goto done;
3412b15cb3dSCy Schubert
3422b15cb3dSCy Schubert if (EVTAG_GET(kill, weapon, &weapon) == -1) {
3432b15cb3dSCy Schubert fprintf(stderr, "get weapon\n");
3442b15cb3dSCy Schubert goto done;
3452b15cb3dSCy Schubert }
3462b15cb3dSCy Schubert if (EVTAG_GET(kill, action, &action) == -1) {
3472b15cb3dSCy Schubert fprintf(stderr, "get action\n");
3482b15cb3dSCy Schubert goto done;
3492b15cb3dSCy Schubert }
3502b15cb3dSCy Schubert
3512b15cb3dSCy Schubert if (strcmp(weapon, "dagger"))
3522b15cb3dSCy Schubert goto done;
3532b15cb3dSCy Schubert
3542b15cb3dSCy Schubert if (strcmp(action, "wave around like an idiot"))
3552b15cb3dSCy Schubert goto done;
3562b15cb3dSCy Schubert
3572b15cb3dSCy Schubert test_ok += 1;
3582b15cb3dSCy Schubert
3592b15cb3dSCy Schubert done:
3602b15cb3dSCy Schubert event_loopexit(NULL);
3612b15cb3dSCy Schubert }
3622b15cb3dSCy Schubert
3632b15cb3dSCy Schubert static void
GotKillCbTwo(struct evrpc_status * status,struct msg * msg,struct kill * kill,void * arg)3642b15cb3dSCy Schubert GotKillCbTwo(struct evrpc_status *status,
3652b15cb3dSCy Schubert struct msg *msg, struct kill *kill, void *arg)
3662b15cb3dSCy Schubert {
3672b15cb3dSCy Schubert char *weapon;
3682b15cb3dSCy Schubert char *action;
3692b15cb3dSCy Schubert
3702b15cb3dSCy Schubert if (status->error != EVRPC_STATUS_ERR_NONE)
3712b15cb3dSCy Schubert goto done;
3722b15cb3dSCy Schubert
3732b15cb3dSCy Schubert if (EVTAG_GET(kill, weapon, &weapon) == -1) {
3742b15cb3dSCy Schubert fprintf(stderr, "get weapon\n");
3752b15cb3dSCy Schubert goto done;
3762b15cb3dSCy Schubert }
3772b15cb3dSCy Schubert if (EVTAG_GET(kill, action, &action) == -1) {
3782b15cb3dSCy Schubert fprintf(stderr, "get action\n");
3792b15cb3dSCy Schubert goto done;
3802b15cb3dSCy Schubert }
3812b15cb3dSCy Schubert
3822b15cb3dSCy Schubert if (strcmp(weapon, "dagger"))
3832b15cb3dSCy Schubert goto done;
3842b15cb3dSCy Schubert
3852b15cb3dSCy Schubert if (strcmp(action, "wave around like an idiot"))
3862b15cb3dSCy Schubert goto done;
3872b15cb3dSCy Schubert
3882b15cb3dSCy Schubert test_ok += 1;
3892b15cb3dSCy Schubert
3902b15cb3dSCy Schubert done:
3912b15cb3dSCy Schubert if (test_ok == 2)
3922b15cb3dSCy Schubert event_loopexit(NULL);
3932b15cb3dSCy Schubert }
3942b15cb3dSCy Schubert
3952b15cb3dSCy Schubert static int
rpc_hook_add_header(void * ctx,struct evhttp_request * req,struct evbuffer * evbuf,void * arg)3962b15cb3dSCy Schubert rpc_hook_add_header(void *ctx, struct evhttp_request *req,
3972b15cb3dSCy Schubert struct evbuffer *evbuf, void *arg)
3982b15cb3dSCy Schubert {
3992b15cb3dSCy Schubert const char *hook_type = arg;
4002b15cb3dSCy Schubert if (strcmp("input", hook_type) == 0)
4012b15cb3dSCy Schubert evhttp_add_header(req->input_headers, "X-Hook", hook_type);
4022b15cb3dSCy Schubert else
4032b15cb3dSCy Schubert evhttp_add_header(req->output_headers, "X-Hook", hook_type);
4042b15cb3dSCy Schubert
4052b15cb3dSCy Schubert assert(evrpc_hook_get_connection(ctx) != NULL);
4062b15cb3dSCy Schubert
4072b15cb3dSCy Schubert return (EVRPC_CONTINUE);
4082b15cb3dSCy Schubert }
4092b15cb3dSCy Schubert
4102b15cb3dSCy Schubert static int
rpc_hook_add_meta(void * ctx,struct evhttp_request * req,struct evbuffer * evbuf,void * arg)4112b15cb3dSCy Schubert rpc_hook_add_meta(void *ctx, struct evhttp_request *req,
4122b15cb3dSCy Schubert struct evbuffer *evbuf, void *arg)
4132b15cb3dSCy Schubert {
4142b15cb3dSCy Schubert evrpc_hook_add_meta(ctx, "meta", "test", 5);
4152b15cb3dSCy Schubert
4162b15cb3dSCy Schubert assert(evrpc_hook_get_connection(ctx) != NULL);
4172b15cb3dSCy Schubert
4182b15cb3dSCy Schubert return (EVRPC_CONTINUE);
4192b15cb3dSCy Schubert }
4202b15cb3dSCy Schubert
4212b15cb3dSCy Schubert static int
rpc_hook_remove_header(void * ctx,struct evhttp_request * req,struct evbuffer * evbuf,void * arg)4222b15cb3dSCy Schubert rpc_hook_remove_header(void *ctx, struct evhttp_request *req,
4232b15cb3dSCy Schubert struct evbuffer *evbuf, void *arg)
4242b15cb3dSCy Schubert {
4252b15cb3dSCy Schubert const char *header = evhttp_find_header(req->input_headers, "X-Hook");
4262b15cb3dSCy Schubert void *data = NULL;
4272b15cb3dSCy Schubert size_t data_len = 0;
4282b15cb3dSCy Schubert
4292b15cb3dSCy Schubert assert(header != NULL);
4302b15cb3dSCy Schubert assert(strcmp(header, arg) == 0);
4312b15cb3dSCy Schubert
4322b15cb3dSCy Schubert evhttp_remove_header(req->input_headers, "X-Hook");
4332b15cb3dSCy Schubert evhttp_add_header(req->input_headers, "X-Pool-Hook", "ran");
4342b15cb3dSCy Schubert
4352b15cb3dSCy Schubert assert(evrpc_hook_find_meta(ctx, "meta", &data, &data_len) == 0);
4362b15cb3dSCy Schubert assert(data != NULL);
4372b15cb3dSCy Schubert assert(data_len == 5);
4382b15cb3dSCy Schubert
4392b15cb3dSCy Schubert assert(evrpc_hook_get_connection(ctx) != NULL);
4402b15cb3dSCy Schubert
4412b15cb3dSCy Schubert return (EVRPC_CONTINUE);
4422b15cb3dSCy Schubert }
4432b15cb3dSCy Schubert
4442b15cb3dSCy Schubert static void
rpc_basic_client(void)4452b15cb3dSCy Schubert rpc_basic_client(void)
4462b15cb3dSCy Schubert {
4472b15cb3dSCy Schubert ev_uint16_t port;
4482b15cb3dSCy Schubert struct evhttp *http = NULL;
4492b15cb3dSCy Schubert struct evrpc_base *base = NULL;
4502b15cb3dSCy Schubert struct evrpc_pool *pool = NULL;
4512b15cb3dSCy Schubert struct msg *msg = NULL;
4522b15cb3dSCy Schubert struct kill *kill = NULL;
4532b15cb3dSCy Schubert
4542b15cb3dSCy Schubert rpc_setup(&http, &port, &base);
4552b15cb3dSCy Schubert
4562b15cb3dSCy Schubert need_input_hook = 1;
4572b15cb3dSCy Schubert need_output_hook = 1;
4582b15cb3dSCy Schubert
4592b15cb3dSCy Schubert assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_add_header, (void*)"input")
4602b15cb3dSCy Schubert != NULL);
4612b15cb3dSCy Schubert assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_add_header, (void*)"output")
4622b15cb3dSCy Schubert != NULL);
4632b15cb3dSCy Schubert
4642b15cb3dSCy Schubert pool = rpc_pool_with_connection(port);
4652b15cb3dSCy Schubert tt_assert(pool);
4662b15cb3dSCy Schubert
4672b15cb3dSCy Schubert assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL));
4682b15cb3dSCy Schubert assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output"));
4692b15cb3dSCy Schubert
4702b15cb3dSCy Schubert /* set up the basic message */
4712b15cb3dSCy Schubert msg = msg_new();
4722b15cb3dSCy Schubert tt_assert(msg);
4732b15cb3dSCy Schubert EVTAG_ASSIGN(msg, from_name, "niels");
4742b15cb3dSCy Schubert EVTAG_ASSIGN(msg, to_name, "tester");
4752b15cb3dSCy Schubert
4762b15cb3dSCy Schubert kill = kill_new();
4772b15cb3dSCy Schubert
4782b15cb3dSCy Schubert EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
4792b15cb3dSCy Schubert
4802b15cb3dSCy Schubert test_ok = 0;
4812b15cb3dSCy Schubert
4822b15cb3dSCy Schubert event_dispatch();
4832b15cb3dSCy Schubert
4842b15cb3dSCy Schubert tt_assert(test_ok == 1);
4852b15cb3dSCy Schubert
4862b15cb3dSCy Schubert /* we do it twice to make sure that reuse works correctly */
4872b15cb3dSCy Schubert kill_clear(kill);
4882b15cb3dSCy Schubert
4892b15cb3dSCy Schubert EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
4902b15cb3dSCy Schubert
4912b15cb3dSCy Schubert event_dispatch();
4922b15cb3dSCy Schubert
4932b15cb3dSCy Schubert tt_assert(test_ok == 2);
4942b15cb3dSCy Schubert
4952b15cb3dSCy Schubert /* we do it trice to make sure other stuff works, too */
4962b15cb3dSCy Schubert kill_clear(kill);
4972b15cb3dSCy Schubert
4982b15cb3dSCy Schubert {
4992b15cb3dSCy Schubert struct evrpc_request_wrapper *ctx =
5002b15cb3dSCy Schubert EVRPC_MAKE_CTX(Message, msg, kill,
5012b15cb3dSCy Schubert pool, msg, kill, GotKillCb, NULL);
5022b15cb3dSCy Schubert evrpc_make_request(ctx);
5032b15cb3dSCy Schubert }
5042b15cb3dSCy Schubert
5052b15cb3dSCy Schubert event_dispatch();
5062b15cb3dSCy Schubert
5072b15cb3dSCy Schubert rpc_teardown(base);
5082b15cb3dSCy Schubert
5092b15cb3dSCy Schubert tt_assert(test_ok == 3);
5102b15cb3dSCy Schubert
5112b15cb3dSCy Schubert end:
5122b15cb3dSCy Schubert if (msg)
5132b15cb3dSCy Schubert msg_free(msg);
5142b15cb3dSCy Schubert if (kill)
5152b15cb3dSCy Schubert kill_free(kill);
5162b15cb3dSCy Schubert
5172b15cb3dSCy Schubert if (pool)
5182b15cb3dSCy Schubert evrpc_pool_free(pool);
5192b15cb3dSCy Schubert if (http)
5202b15cb3dSCy Schubert evhttp_free(http);
5212b15cb3dSCy Schubert
5222b15cb3dSCy Schubert need_input_hook = 0;
5232b15cb3dSCy Schubert need_output_hook = 0;
5242b15cb3dSCy Schubert }
5252b15cb3dSCy Schubert
5262b15cb3dSCy Schubert /*
5272b15cb3dSCy Schubert * We are testing that the second requests gets send over the same
5282b15cb3dSCy Schubert * connection after the first RPCs completes.
5292b15cb3dSCy Schubert */
5302b15cb3dSCy Schubert static void
rpc_basic_queued_client(void)5312b15cb3dSCy Schubert rpc_basic_queued_client(void)
5322b15cb3dSCy Schubert {
5332b15cb3dSCy Schubert ev_uint16_t port;
5342b15cb3dSCy Schubert struct evhttp *http = NULL;
5352b15cb3dSCy Schubert struct evrpc_base *base = NULL;
5362b15cb3dSCy Schubert struct evrpc_pool *pool = NULL;
5372b15cb3dSCy Schubert struct msg *msg=NULL;
5382b15cb3dSCy Schubert struct kill *kill_one=NULL, *kill_two=NULL;
5392b15cb3dSCy Schubert
5402b15cb3dSCy Schubert rpc_setup(&http, &port, &base);
5412b15cb3dSCy Schubert
5422b15cb3dSCy Schubert pool = rpc_pool_with_connection(port);
5432b15cb3dSCy Schubert tt_assert(pool);
5442b15cb3dSCy Schubert
5452b15cb3dSCy Schubert /* set up the basic message */
5462b15cb3dSCy Schubert msg = msg_new();
5472b15cb3dSCy Schubert tt_assert(msg);
5482b15cb3dSCy Schubert EVTAG_ASSIGN(msg, from_name, "niels");
5492b15cb3dSCy Schubert EVTAG_ASSIGN(msg, to_name, "tester");
5502b15cb3dSCy Schubert
5512b15cb3dSCy Schubert kill_one = kill_new();
5522b15cb3dSCy Schubert kill_two = kill_new();
5532b15cb3dSCy Schubert
5542b15cb3dSCy Schubert EVRPC_MAKE_REQUEST(Message, pool, msg, kill_one, GotKillCbTwo, NULL);
5552b15cb3dSCy Schubert EVRPC_MAKE_REQUEST(Message, pool, msg, kill_two, GotKillCb, NULL);
5562b15cb3dSCy Schubert
5572b15cb3dSCy Schubert test_ok = 0;
5582b15cb3dSCy Schubert
5592b15cb3dSCy Schubert event_dispatch();
5602b15cb3dSCy Schubert
5612b15cb3dSCy Schubert rpc_teardown(base);
5622b15cb3dSCy Schubert
5632b15cb3dSCy Schubert tt_assert(test_ok == 2);
5642b15cb3dSCy Schubert
5652b15cb3dSCy Schubert end:
5662b15cb3dSCy Schubert if (msg)
5672b15cb3dSCy Schubert msg_free(msg);
5682b15cb3dSCy Schubert if (kill_one)
5692b15cb3dSCy Schubert kill_free(kill_one);
5702b15cb3dSCy Schubert if (kill_two)
5712b15cb3dSCy Schubert kill_free(kill_two);
5722b15cb3dSCy Schubert
5732b15cb3dSCy Schubert if (pool)
5742b15cb3dSCy Schubert evrpc_pool_free(pool);
5752b15cb3dSCy Schubert if (http)
5762b15cb3dSCy Schubert evhttp_free(http);
5772b15cb3dSCy Schubert }
5782b15cb3dSCy Schubert
5792b15cb3dSCy Schubert static void
GotErrorCb(struct evrpc_status * status,struct msg * msg,struct kill * kill,void * arg)5802b15cb3dSCy Schubert GotErrorCb(struct evrpc_status *status,
5812b15cb3dSCy Schubert struct msg *msg, struct kill *kill, void *arg)
5822b15cb3dSCy Schubert {
5832b15cb3dSCy Schubert if (status->error != EVRPC_STATUS_ERR_TIMEOUT)
5842b15cb3dSCy Schubert goto done;
5852b15cb3dSCy Schubert
5862b15cb3dSCy Schubert /* should never be complete but just to check */
5872b15cb3dSCy Schubert if (kill_complete(kill) == 0)
5882b15cb3dSCy Schubert goto done;
5892b15cb3dSCy Schubert
5902b15cb3dSCy Schubert test_ok += 1;
5912b15cb3dSCy Schubert
5922b15cb3dSCy Schubert done:
5932b15cb3dSCy Schubert event_loopexit(NULL);
5942b15cb3dSCy Schubert }
5952b15cb3dSCy Schubert
5962b15cb3dSCy Schubert /* we just pause the rpc and continue it in the next callback */
5972b15cb3dSCy Schubert
5982b15cb3dSCy Schubert struct rpc_hook_ctx_ {
5992b15cb3dSCy Schubert void *vbase;
6002b15cb3dSCy Schubert void *ctx;
6012b15cb3dSCy Schubert };
6022b15cb3dSCy Schubert
6032b15cb3dSCy Schubert static int hook_pause_cb_called=0;
6042b15cb3dSCy Schubert
6052b15cb3dSCy Schubert static void
rpc_hook_pause_cb(evutil_socket_t fd,short what,void * arg)6062b15cb3dSCy Schubert rpc_hook_pause_cb(evutil_socket_t fd, short what, void *arg)
6072b15cb3dSCy Schubert {
6082b15cb3dSCy Schubert struct rpc_hook_ctx_ *ctx = arg;
6092b15cb3dSCy Schubert ++hook_pause_cb_called;
6102b15cb3dSCy Schubert evrpc_resume_request(ctx->vbase, ctx->ctx, EVRPC_CONTINUE);
6112b15cb3dSCy Schubert free(arg);
6122b15cb3dSCy Schubert }
6132b15cb3dSCy Schubert
6142b15cb3dSCy Schubert static int
rpc_hook_pause(void * ctx,struct evhttp_request * req,struct evbuffer * evbuf,void * arg)6152b15cb3dSCy Schubert rpc_hook_pause(void *ctx, struct evhttp_request *req, struct evbuffer *evbuf,
6162b15cb3dSCy Schubert void *arg)
6172b15cb3dSCy Schubert {
6182b15cb3dSCy Schubert struct rpc_hook_ctx_ *tmp = malloc(sizeof(*tmp));
6192b15cb3dSCy Schubert struct timeval tv;
6202b15cb3dSCy Schubert
6212b15cb3dSCy Schubert assert(tmp != NULL);
6222b15cb3dSCy Schubert tmp->vbase = arg;
6232b15cb3dSCy Schubert tmp->ctx = ctx;
6242b15cb3dSCy Schubert
6252b15cb3dSCy Schubert memset(&tv, 0, sizeof(tv));
6262b15cb3dSCy Schubert event_once(-1, EV_TIMEOUT, rpc_hook_pause_cb, tmp, &tv);
6272b15cb3dSCy Schubert return EVRPC_PAUSE;
6282b15cb3dSCy Schubert }
6292b15cb3dSCy Schubert
6302b15cb3dSCy Schubert static void
rpc_basic_client_with_pause(void)6312b15cb3dSCy Schubert rpc_basic_client_with_pause(void)
6322b15cb3dSCy Schubert {
6332b15cb3dSCy Schubert ev_uint16_t port;
6342b15cb3dSCy Schubert struct evhttp *http = NULL;
6352b15cb3dSCy Schubert struct evrpc_base *base = NULL;
6362b15cb3dSCy Schubert struct evrpc_pool *pool = NULL;
6372b15cb3dSCy Schubert struct msg *msg = NULL;
6382b15cb3dSCy Schubert struct kill *kill= NULL;
6392b15cb3dSCy Schubert
6402b15cb3dSCy Schubert rpc_setup(&http, &port, &base);
6412b15cb3dSCy Schubert
6422b15cb3dSCy Schubert assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_pause, base));
6432b15cb3dSCy Schubert assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base));
6442b15cb3dSCy Schubert
6452b15cb3dSCy Schubert pool = rpc_pool_with_connection(port);
6462b15cb3dSCy Schubert tt_assert(pool);
6472b15cb3dSCy Schubert assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool));
6482b15cb3dSCy Schubert assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool));
6492b15cb3dSCy Schubert
6502b15cb3dSCy Schubert /* set up the basic message */
6512b15cb3dSCy Schubert msg = msg_new();
6522b15cb3dSCy Schubert tt_assert(msg);
6532b15cb3dSCy Schubert EVTAG_ASSIGN(msg, from_name, "niels");
6542b15cb3dSCy Schubert EVTAG_ASSIGN(msg, to_name, "tester");
6552b15cb3dSCy Schubert
6562b15cb3dSCy Schubert kill = kill_new();
6572b15cb3dSCy Schubert
6582b15cb3dSCy Schubert EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
6592b15cb3dSCy Schubert
6602b15cb3dSCy Schubert test_ok = 0;
6612b15cb3dSCy Schubert
6622b15cb3dSCy Schubert event_dispatch();
6632b15cb3dSCy Schubert
6642b15cb3dSCy Schubert tt_int_op(test_ok, ==, 1);
6652b15cb3dSCy Schubert tt_int_op(hook_pause_cb_called, ==, 4);
6662b15cb3dSCy Schubert
6672b15cb3dSCy Schubert end:
6682b15cb3dSCy Schubert if (base)
6692b15cb3dSCy Schubert rpc_teardown(base);
6702b15cb3dSCy Schubert
6712b15cb3dSCy Schubert if (msg)
6722b15cb3dSCy Schubert msg_free(msg);
6732b15cb3dSCy Schubert if (kill)
6742b15cb3dSCy Schubert kill_free(kill);
6752b15cb3dSCy Schubert
6762b15cb3dSCy Schubert if (pool)
6772b15cb3dSCy Schubert evrpc_pool_free(pool);
6782b15cb3dSCy Schubert if (http)
6792b15cb3dSCy Schubert evhttp_free(http);
6802b15cb3dSCy Schubert }
6812b15cb3dSCy Schubert
6822b15cb3dSCy Schubert static void
rpc_client_timeout(void)6832b15cb3dSCy Schubert rpc_client_timeout(void)
6842b15cb3dSCy Schubert {
6852b15cb3dSCy Schubert ev_uint16_t port;
6862b15cb3dSCy Schubert struct evhttp *http = NULL;
6872b15cb3dSCy Schubert struct evrpc_base *base = NULL;
6882b15cb3dSCy Schubert struct evrpc_pool *pool = NULL;
6892b15cb3dSCy Schubert struct msg *msg = NULL;
6902b15cb3dSCy Schubert struct kill *kill = NULL;
6912b15cb3dSCy Schubert
6922b15cb3dSCy Schubert rpc_setup(&http, &port, &base);
6932b15cb3dSCy Schubert
6942b15cb3dSCy Schubert pool = rpc_pool_with_connection(port);
6952b15cb3dSCy Schubert tt_assert(pool);
6962b15cb3dSCy Schubert
6972b15cb3dSCy Schubert /* set the timeout to 1 second. */
6982b15cb3dSCy Schubert evrpc_pool_set_timeout(pool, 1);
6992b15cb3dSCy Schubert
7002b15cb3dSCy Schubert /* set up the basic message */
7012b15cb3dSCy Schubert msg = msg_new();
7022b15cb3dSCy Schubert tt_assert(msg);
7032b15cb3dSCy Schubert EVTAG_ASSIGN(msg, from_name, "niels");
7042b15cb3dSCy Schubert EVTAG_ASSIGN(msg, to_name, "tester");
7052b15cb3dSCy Schubert
7062b15cb3dSCy Schubert kill = kill_new();
7072b15cb3dSCy Schubert
7082b15cb3dSCy Schubert EVRPC_MAKE_REQUEST(NeverReply, pool, msg, kill, GotErrorCb, NULL);
7092b15cb3dSCy Schubert
7102b15cb3dSCy Schubert test_ok = 0;
7112b15cb3dSCy Schubert
7122b15cb3dSCy Schubert event_dispatch();
7132b15cb3dSCy Schubert
7142b15cb3dSCy Schubert /* free the saved RPC structure up */
7152b15cb3dSCy Schubert EVRPC_REQUEST_DONE(saved_rpc);
7162b15cb3dSCy Schubert
7172b15cb3dSCy Schubert rpc_teardown(base);
7182b15cb3dSCy Schubert
7192b15cb3dSCy Schubert tt_assert(test_ok == 2);
7202b15cb3dSCy Schubert
7212b15cb3dSCy Schubert end:
7222b15cb3dSCy Schubert if (msg)
7232b15cb3dSCy Schubert msg_free(msg);
7242b15cb3dSCy Schubert if (kill)
7252b15cb3dSCy Schubert kill_free(kill);
7262b15cb3dSCy Schubert
7272b15cb3dSCy Schubert if (pool)
7282b15cb3dSCy Schubert evrpc_pool_free(pool);
7292b15cb3dSCy Schubert if (http)
7302b15cb3dSCy Schubert evhttp_free(http);
7312b15cb3dSCy Schubert }
7322b15cb3dSCy Schubert
7332b15cb3dSCy Schubert static void
rpc_test(void)7342b15cb3dSCy Schubert rpc_test(void)
7352b15cb3dSCy Schubert {
7362b15cb3dSCy Schubert struct msg *msg = NULL, *msg2 = NULL;
7372b15cb3dSCy Schubert struct kill *attack = NULL;
7382b15cb3dSCy Schubert struct run *run = NULL;
7392b15cb3dSCy Schubert struct evbuffer *tmp = evbuffer_new();
7402b15cb3dSCy Schubert struct timeval tv_start, tv_end;
7412b15cb3dSCy Schubert ev_uint32_t tag;
7422b15cb3dSCy Schubert int i;
7432b15cb3dSCy Schubert
7442b15cb3dSCy Schubert msg = msg_new();
7452b15cb3dSCy Schubert
7462b15cb3dSCy Schubert tt_assert(msg);
7472b15cb3dSCy Schubert
7482b15cb3dSCy Schubert EVTAG_ASSIGN(msg, from_name, "niels");
7492b15cb3dSCy Schubert EVTAG_ASSIGN(msg, to_name, "phoenix");
7502b15cb3dSCy Schubert
7512b15cb3dSCy Schubert if (EVTAG_GET(msg, attack, &attack) == -1) {
7522b15cb3dSCy Schubert tt_abort_msg("Failed to set kill message.");
7532b15cb3dSCy Schubert }
7542b15cb3dSCy Schubert
7552b15cb3dSCy Schubert EVTAG_ASSIGN(attack, weapon, "feather");
7562b15cb3dSCy Schubert EVTAG_ASSIGN(attack, action, "tickle");
7572b15cb3dSCy Schubert for (i = 0; i < 3; ++i) {
7582b15cb3dSCy Schubert if (EVTAG_ARRAY_ADD_VALUE(attack, how_often, i) == NULL) {
7592b15cb3dSCy Schubert tt_abort_msg("Failed to add how_often.");
7602b15cb3dSCy Schubert }
7612b15cb3dSCy Schubert }
7622b15cb3dSCy Schubert
7632b15cb3dSCy Schubert evutil_gettimeofday(&tv_start, NULL);
7642b15cb3dSCy Schubert for (i = 0; i < 1000; ++i) {
7652b15cb3dSCy Schubert run = EVTAG_ARRAY_ADD(msg, run);
7662b15cb3dSCy Schubert if (run == NULL) {
7672b15cb3dSCy Schubert tt_abort_msg("Failed to add run message.");
7682b15cb3dSCy Schubert }
7692b15cb3dSCy Schubert EVTAG_ASSIGN(run, how, "very fast but with some data in it");
7702b15cb3dSCy Schubert EVTAG_ASSIGN(run, fixed_bytes,
7712b15cb3dSCy Schubert (ev_uint8_t*)"012345678901234567890123");
7722b15cb3dSCy Schubert
7732b15cb3dSCy Schubert if (EVTAG_ARRAY_ADD_VALUE(
7742b15cb3dSCy Schubert run, notes, "this is my note") == NULL) {
7752b15cb3dSCy Schubert tt_abort_msg("Failed to add note.");
7762b15cb3dSCy Schubert }
7772b15cb3dSCy Schubert if (EVTAG_ARRAY_ADD_VALUE(run, notes, "pps") == NULL) {
7782b15cb3dSCy Schubert tt_abort_msg("Failed to add note");
7792b15cb3dSCy Schubert }
7802b15cb3dSCy Schubert
7812b15cb3dSCy Schubert EVTAG_ASSIGN(run, large_number, 0xdead0a0bcafebeefLL);
7822b15cb3dSCy Schubert EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xdead0a0b);
7832b15cb3dSCy Schubert EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xbeefcafe);
7842b15cb3dSCy Schubert }
7852b15cb3dSCy Schubert
7862b15cb3dSCy Schubert if (msg_complete(msg) == -1)
7872b15cb3dSCy Schubert tt_abort_msg("Failed to make complete message.");
7882b15cb3dSCy Schubert
7892b15cb3dSCy Schubert evtag_marshal_msg(tmp, 0xdeaf, msg);
7902b15cb3dSCy Schubert
7912b15cb3dSCy Schubert if (evtag_peek(tmp, &tag) == -1)
7922b15cb3dSCy Schubert tt_abort_msg("Failed to peak tag.");
7932b15cb3dSCy Schubert
7942b15cb3dSCy Schubert if (tag != 0xdeaf)
7952b15cb3dSCy Schubert TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag));
7962b15cb3dSCy Schubert
7972b15cb3dSCy Schubert msg2 = msg_new();
7982b15cb3dSCy Schubert if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1)
7992b15cb3dSCy Schubert tt_abort_msg("Failed to unmarshal message.");
8002b15cb3dSCy Schubert
8012b15cb3dSCy Schubert evutil_gettimeofday(&tv_end, NULL);
8022b15cb3dSCy Schubert evutil_timersub(&tv_end, &tv_start, &tv_end);
8032b15cb3dSCy Schubert TT_BLATHER(("(%.1f us/add) ",
8042b15cb3dSCy Schubert (float)tv_end.tv_sec/(float)i * 1000000.0 +
8052b15cb3dSCy Schubert tv_end.tv_usec / (float)i));
8062b15cb3dSCy Schubert
8072b15cb3dSCy Schubert if (!EVTAG_HAS(msg2, from_name) ||
8082b15cb3dSCy Schubert !EVTAG_HAS(msg2, to_name) ||
8092b15cb3dSCy Schubert !EVTAG_HAS(msg2, attack)) {
8102b15cb3dSCy Schubert tt_abort_msg("Missing data structures.");
8112b15cb3dSCy Schubert }
8122b15cb3dSCy Schubert
8132b15cb3dSCy Schubert if (EVTAG_GET(msg2, attack, &attack) == -1) {
8142b15cb3dSCy Schubert tt_abort_msg("Could not get attack.");
8152b15cb3dSCy Schubert }
8162b15cb3dSCy Schubert
8172b15cb3dSCy Schubert if (EVTAG_ARRAY_LEN(msg2, run) != i) {
8182b15cb3dSCy Schubert tt_abort_msg("Wrong number of run messages.");
8192b15cb3dSCy Schubert }
8202b15cb3dSCy Schubert
8212b15cb3dSCy Schubert /* get the very first run message */
8222b15cb3dSCy Schubert if (EVTAG_ARRAY_GET(msg2, run, 0, &run) == -1) {
8232b15cb3dSCy Schubert tt_abort_msg("Failed to get run msg.");
8242b15cb3dSCy Schubert } else {
8252b15cb3dSCy Schubert /* verify the notes */
8262b15cb3dSCy Schubert char *note_one, *note_two;
8272b15cb3dSCy Schubert ev_uint64_t large_number;
8282b15cb3dSCy Schubert ev_uint32_t short_number;
8292b15cb3dSCy Schubert
8302b15cb3dSCy Schubert if (EVTAG_ARRAY_LEN(run, notes) != 2) {
8312b15cb3dSCy Schubert tt_abort_msg("Wrong number of note strings.");
8322b15cb3dSCy Schubert }
8332b15cb3dSCy Schubert
8342b15cb3dSCy Schubert if (EVTAG_ARRAY_GET(run, notes, 0, ¬e_one) == -1 ||
8352b15cb3dSCy Schubert EVTAG_ARRAY_GET(run, notes, 1, ¬e_two) == -1) {
8362b15cb3dSCy Schubert tt_abort_msg("Could not get note strings.");
8372b15cb3dSCy Schubert }
8382b15cb3dSCy Schubert
8392b15cb3dSCy Schubert if (strcmp(note_one, "this is my note") ||
8402b15cb3dSCy Schubert strcmp(note_two, "pps")) {
8412b15cb3dSCy Schubert tt_abort_msg("Incorrect note strings encoded.");
8422b15cb3dSCy Schubert }
8432b15cb3dSCy Schubert
8442b15cb3dSCy Schubert if (EVTAG_GET(run, large_number, &large_number) == -1 ||
8452b15cb3dSCy Schubert large_number != 0xdead0a0bcafebeefLL) {
8462b15cb3dSCy Schubert tt_abort_msg("Incorrrect large_number.");
8472b15cb3dSCy Schubert }
8482b15cb3dSCy Schubert
8492b15cb3dSCy Schubert if (EVTAG_ARRAY_LEN(run, other_numbers) != 2) {
8502b15cb3dSCy Schubert tt_abort_msg("Wrong number of other_numbers.");
8512b15cb3dSCy Schubert }
8522b15cb3dSCy Schubert
8532b15cb3dSCy Schubert if (EVTAG_ARRAY_GET(
8542b15cb3dSCy Schubert run, other_numbers, 0, &short_number) == -1) {
8552b15cb3dSCy Schubert tt_abort_msg("Could not get short number.");
8562b15cb3dSCy Schubert }
8572b15cb3dSCy Schubert tt_uint_op(short_number, ==, 0xdead0a0b);
8582b15cb3dSCy Schubert
8592b15cb3dSCy Schubert }
8602b15cb3dSCy Schubert tt_int_op(EVTAG_ARRAY_LEN(attack, how_often), ==, 3);
8612b15cb3dSCy Schubert
8622b15cb3dSCy Schubert for (i = 0; i < 3; ++i) {
8632b15cb3dSCy Schubert ev_uint32_t res;
8642b15cb3dSCy Schubert if (EVTAG_ARRAY_GET(attack, how_often, i, &res) == -1) {
8652b15cb3dSCy Schubert TT_DIE(("Cannot get %dth how_often msg.", i));
8662b15cb3dSCy Schubert }
8672b15cb3dSCy Schubert if ((int)res != i) {
8682b15cb3dSCy Schubert TT_DIE(("Wrong message encoded %d != %d", i, res));
8692b15cb3dSCy Schubert }
8702b15cb3dSCy Schubert }
8712b15cb3dSCy Schubert
8722b15cb3dSCy Schubert test_ok = 1;
8732b15cb3dSCy Schubert end:
8742b15cb3dSCy Schubert if (msg)
8752b15cb3dSCy Schubert msg_free(msg);
8762b15cb3dSCy Schubert if (msg2)
8772b15cb3dSCy Schubert msg_free(msg2);
8782b15cb3dSCy Schubert if (tmp)
8792b15cb3dSCy Schubert evbuffer_free(tmp);
8802b15cb3dSCy Schubert }
8812b15cb3dSCy Schubert
882*a466cc55SCy Schubert static void
rpc_invalid_type(void)883*a466cc55SCy Schubert rpc_invalid_type(void)
884*a466cc55SCy Schubert {
885*a466cc55SCy Schubert ev_uint16_t port;
886*a466cc55SCy Schubert struct evhttp *http = NULL;
887*a466cc55SCy Schubert struct evrpc_base *base = NULL;
888*a466cc55SCy Schubert struct evhttp_connection *evcon = NULL;
889*a466cc55SCy Schubert struct evhttp_request *req = NULL;
890*a466cc55SCy Schubert
891*a466cc55SCy Schubert rpc_setup(&http, &port, &base);
892*a466cc55SCy Schubert
893*a466cc55SCy Schubert evcon = evhttp_connection_new("127.0.0.1", port);
894*a466cc55SCy Schubert tt_assert(evcon);
895*a466cc55SCy Schubert
896*a466cc55SCy Schubert /*
897*a466cc55SCy Schubert * At this point, we want to schedule an HTTP POST request
898*a466cc55SCy Schubert * server using our make request method.
899*a466cc55SCy Schubert */
900*a466cc55SCy Schubert
901*a466cc55SCy Schubert req = evhttp_request_new(rpc_postrequest_failure, NULL);
902*a466cc55SCy Schubert tt_assert(req);
903*a466cc55SCy Schubert
904*a466cc55SCy Schubert /* Add the information that we care about */
905*a466cc55SCy Schubert evhttp_add_header(req->output_headers, "Host", "somehost");
906*a466cc55SCy Schubert evbuffer_add_printf(req->output_buffer, "Some Nonsense");
907*a466cc55SCy Schubert
908*a466cc55SCy Schubert if (evhttp_make_request(evcon, req,
909*a466cc55SCy Schubert EVHTTP_REQ_GET,
910*a466cc55SCy Schubert "/.rpc.Message") == -1) {
911*a466cc55SCy Schubert tt_abort();
912*a466cc55SCy Schubert }
913*a466cc55SCy Schubert
914*a466cc55SCy Schubert test_ok = 0;
915*a466cc55SCy Schubert
916*a466cc55SCy Schubert event_dispatch();
917*a466cc55SCy Schubert
918*a466cc55SCy Schubert evhttp_connection_free(evcon);
919*a466cc55SCy Schubert
920*a466cc55SCy Schubert rpc_teardown(base);
921*a466cc55SCy Schubert
922*a466cc55SCy Schubert tt_assert(test_ok == 1);
923*a466cc55SCy Schubert
924*a466cc55SCy Schubert end:
925*a466cc55SCy Schubert evhttp_free(http);
926*a466cc55SCy Schubert }
927*a466cc55SCy Schubert
928*a466cc55SCy Schubert
9292b15cb3dSCy Schubert #define RPC_LEGACY(name) \
9302b15cb3dSCy Schubert { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY, \
9312b15cb3dSCy Schubert &legacy_setup, \
9322b15cb3dSCy Schubert rpc_##name }
9332b15cb3dSCy Schubert #else
9342b15cb3dSCy Schubert /* NO_PYTHON_EXISTS */
9352b15cb3dSCy Schubert
9362b15cb3dSCy Schubert #define RPC_LEGACY(name) \
9372b15cb3dSCy Schubert { #name, NULL, TT_SKIP, NULL, NULL }
9382b15cb3dSCy Schubert
9392b15cb3dSCy Schubert #endif
9402b15cb3dSCy Schubert
9412b15cb3dSCy Schubert struct testcase_t rpc_testcases[] = {
9422b15cb3dSCy Schubert RPC_LEGACY(basic_test),
9432b15cb3dSCy Schubert RPC_LEGACY(basic_message),
9442b15cb3dSCy Schubert RPC_LEGACY(basic_client),
9452b15cb3dSCy Schubert RPC_LEGACY(basic_queued_client),
9462b15cb3dSCy Schubert RPC_LEGACY(basic_client_with_pause),
947*a466cc55SCy Schubert RPC_LEGACY(invalid_type),
9482b15cb3dSCy Schubert RPC_LEGACY(client_timeout),
9492b15cb3dSCy Schubert RPC_LEGACY(test),
9502b15cb3dSCy Schubert
9512b15cb3dSCy Schubert END_OF_TESTCASES,
9522b15cb3dSCy Schubert };
953