]> arthur.barton.de Git - netatalk.git/blob - libevent/test/regress_rpc.c
volume name must not contain ":"
[netatalk.git] / libevent / test / regress_rpc.c
1 /*
2  * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
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.
26  */
27
28 /* The old tests here need assertions to work. */
29 #undef NDEBUG
30
31 #ifdef WIN32
32 #include <winsock2.h>
33 #include <windows.h>
34 #endif
35
36 #include "event2/event-config.h"
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #ifdef _EVENT_HAVE_SYS_TIME_H
41 #include <sys/time.h>
42 #endif
43 #include <sys/queue.h>
44 #ifndef WIN32
45 #include <sys/socket.h>
46 #include <signal.h>
47 #include <unistd.h>
48 #include <netdb.h>
49 #endif
50 #include <fcntl.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <errno.h>
55 #include <assert.h>
56
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"
68
69 #include "regress.gen.h"
70
71 #include "regress.h"
72 #include "regress_testutils.h"
73
74 #ifndef NO_PYTHON_EXISTS
75
76 static struct evhttp *
77 http_setup(ev_uint16_t *pport)
78 {
79         struct evhttp *myhttp;
80         ev_uint16_t port;
81         struct evhttp_bound_socket *sock;
82
83         myhttp = evhttp_new(NULL);
84         if (!myhttp)
85                 event_errx(1, "Could not start web server");
86
87         /* Try a few different ports */
88         sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", 0);
89         if (!sock)
90                 event_errx(1, "Couldn't open web port");
91
92         port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
93
94         *pport = port;
95         return (myhttp);
96 }
97
98 EVRPC_HEADER(Message, msg, kill)
99 EVRPC_HEADER(NeverReply, msg, kill)
100
101 EVRPC_GENERATE(Message, msg, kill)
102 EVRPC_GENERATE(NeverReply, msg, kill)
103
104 static int need_input_hook = 0;
105 static int need_output_hook = 0;
106
107 static void
108 MessageCb(EVRPC_STRUCT(Message)* rpc, void *arg)
109 {
110         struct kill* kill_reply = rpc->reply;
111
112         if (need_input_hook) {
113                 struct evhttp_request* req = EVRPC_REQUEST_HTTP(rpc);
114                 const char *header = evhttp_find_header(
115                         req->input_headers, "X-Hook");
116                 assert(strcmp(header, "input") == 0);
117         }
118
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");
122
123         /* no reply to the RPC */
124         EVRPC_REQUEST_DONE(rpc);
125 }
126
127 static EVRPC_STRUCT(NeverReply) *saved_rpc;
128
129 static void
130 NeverReplyCb(EVRPC_STRUCT(NeverReply)* rpc, void *arg)
131 {
132         test_ok += 1;
133         saved_rpc = rpc;
134 }
135
136 static void
137 rpc_setup(struct evhttp **phttp, ev_uint16_t *pport, struct evrpc_base **pbase)
138 {
139         ev_uint16_t port;
140         struct evhttp *http = NULL;
141         struct evrpc_base *base = NULL;
142
143         http = http_setup(&port);
144         base = evrpc_init(http);
145
146         EVRPC_REGISTER(base, Message, msg, kill, MessageCb, NULL);
147         EVRPC_REGISTER(base, NeverReply, msg, kill, NeverReplyCb, NULL);
148
149         *phttp = http;
150         *pport = port;
151         *pbase = base;
152
153         need_input_hook = 0;
154         need_output_hook = 0;
155 }
156
157 static void
158 rpc_teardown(struct evrpc_base *base)
159 {
160         assert(EVRPC_UNREGISTER(base, Message) == 0);
161         assert(EVRPC_UNREGISTER(base, NeverReply) == 0);
162
163         evrpc_free(base);
164 }
165
166 static void
167 rpc_postrequest_failure(struct evhttp_request *req, void *arg)
168 {
169         if (req->response_code != HTTP_SERVUNAVAIL) {
170
171                 fprintf(stderr, "FAILED (response code)\n");
172                 exit(1);
173         }
174
175         test_ok = 1;
176         event_loopexit(NULL);
177 }
178
179 /*
180  * Test a malformed payload submitted as an RPC
181  */
182
183 static void
184 rpc_basic_test(void)
185 {
186         ev_uint16_t port;
187         struct evhttp *http = NULL;
188         struct evrpc_base *base = NULL;
189         struct evhttp_connection *evcon = NULL;
190         struct evhttp_request *req = NULL;
191
192         rpc_setup(&http, &port, &base);
193
194         evcon = evhttp_connection_new("127.0.0.1", port);
195         tt_assert(evcon);
196
197         /*
198          * At this point, we want to schedule an HTTP POST request
199          * server using our make request method.
200          */
201
202         req = evhttp_request_new(rpc_postrequest_failure, NULL);
203         tt_assert(req);
204
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");
208
209         if (evhttp_make_request(evcon, req,
210                 EVHTTP_REQ_POST,
211                 "/.rpc.Message") == -1) {
212                 tt_abort();
213         }
214
215         test_ok = 0;
216
217         event_dispatch();
218
219         evhttp_connection_free(evcon);
220
221         rpc_teardown(base);
222
223         tt_assert(test_ok == 1);
224
225 end:
226         evhttp_free(http);
227 }
228
229 static void
230 rpc_postrequest_done(struct evhttp_request *req, void *arg)
231 {
232         struct kill* kill_reply = NULL;
233
234         if (req->response_code != HTTP_OK) {
235                 fprintf(stderr, "FAILED (response code)\n");
236                 exit(1);
237         }
238
239         kill_reply = kill_new();
240
241         if ((kill_unmarshal(kill_reply, req->input_buffer)) == -1) {
242                 fprintf(stderr, "FAILED (unmarshal)\n");
243                 exit(1);
244         }
245
246         kill_free(kill_reply);
247
248         test_ok = 1;
249         event_loopexit(NULL);
250 }
251
252 static void
253 rpc_basic_message(void)
254 {
255         ev_uint16_t port;
256         struct evhttp *http = NULL;
257         struct evrpc_base *base = NULL;
258         struct evhttp_connection *evcon = NULL;
259         struct evhttp_request *req = NULL;
260         struct msg *msg;
261
262         rpc_setup(&http, &port, &base);
263
264         evcon = evhttp_connection_new("127.0.0.1", port);
265         tt_assert(evcon);
266
267         /*
268          * At this point, we want to schedule an HTTP POST request
269          * server using our make request method.
270          */
271
272         req = evhttp_request_new(rpc_postrequest_done, NULL);
273         if (req == NULL) {
274                 fprintf(stdout, "FAILED\n");
275                 exit(1);
276         }
277
278         /* Add the information that we care about */
279         evhttp_add_header(req->output_headers, "Host", "somehost");
280
281         /* set up the basic message */
282         msg = msg_new();
283         EVTAG_ASSIGN(msg, from_name, "niels");
284         EVTAG_ASSIGN(msg, to_name, "tester");
285         msg_marshal(req->output_buffer, msg);
286         msg_free(msg);
287
288         if (evhttp_make_request(evcon, req,
289                 EVHTTP_REQ_POST,
290                 "/.rpc.Message") == -1) {
291                 fprintf(stdout, "FAILED\n");
292                 exit(1);
293         }
294
295         test_ok = 0;
296
297         event_dispatch();
298
299         evhttp_connection_free(evcon);
300
301         rpc_teardown(base);
302
303 end:
304         evhttp_free(http);
305 }
306
307 static struct evrpc_pool *
308 rpc_pool_with_connection(ev_uint16_t port)
309 {
310         struct evhttp_connection *evcon;
311         struct evrpc_pool *pool;
312
313         pool = evrpc_pool_new(NULL);
314         assert(pool != NULL);
315
316         evcon = evhttp_connection_new("127.0.0.1", port);
317         assert(evcon != NULL);
318
319         evrpc_pool_add_connection(pool, evcon);
320
321         return (pool);
322 }
323
324 static void
325 GotKillCb(struct evrpc_status *status,
326     struct msg *msg, struct kill *kill, void *arg)
327 {
328         char *weapon;
329         char *action;
330
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");
335                 assert(strcmp(header, "ran") == 0);
336         }
337
338         if (status->error != EVRPC_STATUS_ERR_NONE)
339                 goto done;
340
341         if (EVTAG_GET(kill, weapon, &weapon) == -1) {
342                 fprintf(stderr, "get weapon\n");
343                 goto done;
344         }
345         if (EVTAG_GET(kill, action, &action) == -1) {
346                 fprintf(stderr, "get action\n");
347                 goto done;
348         }
349
350         if (strcmp(weapon, "dagger"))
351                 goto done;
352
353         if (strcmp(action, "wave around like an idiot"))
354                 goto done;
355
356         test_ok += 1;
357
358 done:
359         event_loopexit(NULL);
360 }
361
362 static void
363 GotKillCbTwo(struct evrpc_status *status,
364     struct msg *msg, struct kill *kill, void *arg)
365 {
366         char *weapon;
367         char *action;
368
369         if (status->error != EVRPC_STATUS_ERR_NONE)
370                 goto done;
371
372         if (EVTAG_GET(kill, weapon, &weapon) == -1) {
373                 fprintf(stderr, "get weapon\n");
374                 goto done;
375         }
376         if (EVTAG_GET(kill, action, &action) == -1) {
377                 fprintf(stderr, "get action\n");
378                 goto done;
379         }
380
381         if (strcmp(weapon, "dagger"))
382                 goto done;
383
384         if (strcmp(action, "wave around like an idiot"))
385                 goto done;
386
387         test_ok += 1;
388
389 done:
390         if (test_ok == 2)
391                 event_loopexit(NULL);
392 }
393
394 static int
395 rpc_hook_add_header(void *ctx, struct evhttp_request *req,
396     struct evbuffer *evbuf, void *arg)
397 {
398         const char *hook_type = arg;
399         if (strcmp("input", hook_type) == 0)
400                 evhttp_add_header(req->input_headers, "X-Hook", hook_type);
401         else
402                 evhttp_add_header(req->output_headers, "X-Hook", hook_type);
403
404         assert(evrpc_hook_get_connection(ctx) != NULL);
405
406         return (EVRPC_CONTINUE);
407 }
408
409 static int
410 rpc_hook_add_meta(void *ctx, struct evhttp_request *req,
411     struct evbuffer *evbuf, void *arg)
412 {
413         evrpc_hook_add_meta(ctx, "meta", "test", 5);
414
415         assert(evrpc_hook_get_connection(ctx) != NULL);
416
417         return (EVRPC_CONTINUE);
418 }
419
420 static int
421 rpc_hook_remove_header(void *ctx, struct evhttp_request *req,
422     struct evbuffer *evbuf, void *arg)
423 {
424         const char *header = evhttp_find_header(req->input_headers, "X-Hook");
425         void *data = NULL;
426         size_t data_len = 0;
427
428         assert(header != NULL);
429         assert(strcmp(header, arg) == 0);
430
431         evhttp_remove_header(req->input_headers, "X-Hook");
432         evhttp_add_header(req->input_headers, "X-Pool-Hook", "ran");
433
434         assert(evrpc_hook_find_meta(ctx, "meta", &data, &data_len) == 0);
435         assert(data != NULL);
436         assert(data_len == 5);
437
438         assert(evrpc_hook_get_connection(ctx) != NULL);
439
440         return (EVRPC_CONTINUE);
441 }
442
443 static void
444 rpc_basic_client(void)
445 {
446         ev_uint16_t port;
447         struct evhttp *http = NULL;
448         struct evrpc_base *base = NULL;
449         struct evrpc_pool *pool = NULL;
450         struct msg *msg = NULL;
451         struct kill *kill = NULL;
452
453         rpc_setup(&http, &port, &base);
454
455         need_input_hook = 1;
456         need_output_hook = 1;
457
458         assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_add_header, (void*)"input")
459             != NULL);
460         assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_add_header, (void*)"output")
461             != NULL);
462
463         pool = rpc_pool_with_connection(port);
464
465         assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_add_meta, NULL));
466         assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_remove_header, (void*)"output"));
467
468         /* set up the basic message */
469         msg = msg_new();
470         EVTAG_ASSIGN(msg, from_name, "niels");
471         EVTAG_ASSIGN(msg, to_name, "tester");
472
473         kill = kill_new();
474
475         EVRPC_MAKE_REQUEST(Message, pool, msg, kill,  GotKillCb, NULL);
476
477         test_ok = 0;
478
479         event_dispatch();
480
481         tt_assert(test_ok == 1);
482
483         /* we do it twice to make sure that reuse works correctly */
484         kill_clear(kill);
485
486         EVRPC_MAKE_REQUEST(Message, pool, msg, kill,  GotKillCb, NULL);
487
488         event_dispatch();
489
490         tt_assert(test_ok == 2);
491
492         /* we do it trice to make sure other stuff works, too */
493         kill_clear(kill);
494
495         {
496                 struct evrpc_request_wrapper *ctx =
497                     EVRPC_MAKE_CTX(Message, msg, kill,
498                         pool, msg, kill, GotKillCb, NULL);
499                 evrpc_make_request(ctx);
500         }
501
502         event_dispatch();
503
504         rpc_teardown(base);
505
506         tt_assert(test_ok == 3);
507
508 end:
509         if (msg)
510                 msg_free(msg);
511         if (kill)
512                 kill_free(kill);
513
514         if (pool)
515                 evrpc_pool_free(pool);
516         if (http)
517                 evhttp_free(http);
518
519         need_input_hook = 0;
520         need_output_hook = 0;
521 }
522
523 /*
524  * We are testing that the second requests gets send over the same
525  * connection after the first RPCs completes.
526  */
527 static void
528 rpc_basic_queued_client(void)
529 {
530         ev_uint16_t port;
531         struct evhttp *http = NULL;
532         struct evrpc_base *base = NULL;
533         struct evrpc_pool *pool = NULL;
534         struct msg *msg=NULL;
535         struct kill *kill_one=NULL, *kill_two=NULL;
536
537         rpc_setup(&http, &port, &base);
538
539         pool = rpc_pool_with_connection(port);
540
541         /* set up the basic message */
542         msg = msg_new();
543         EVTAG_ASSIGN(msg, from_name, "niels");
544         EVTAG_ASSIGN(msg, to_name, "tester");
545
546         kill_one = kill_new();
547         kill_two = kill_new();
548
549         EVRPC_MAKE_REQUEST(Message, pool, msg, kill_one,  GotKillCbTwo, NULL);
550         EVRPC_MAKE_REQUEST(Message, pool, msg, kill_two,  GotKillCb, NULL);
551
552         test_ok = 0;
553
554         event_dispatch();
555
556         rpc_teardown(base);
557
558         tt_assert(test_ok == 2);
559
560 end:
561         if (msg)
562                 msg_free(msg);
563         if (kill_one)
564                 kill_free(kill_one);
565         if (kill_two)
566                 kill_free(kill_two);
567
568         if (pool)
569                 evrpc_pool_free(pool);
570         if (http)
571                 evhttp_free(http);
572 }
573
574 static void
575 GotErrorCb(struct evrpc_status *status,
576     struct msg *msg, struct kill *kill, void *arg)
577 {
578         if (status->error != EVRPC_STATUS_ERR_TIMEOUT)
579                 goto done;
580
581         /* should never be complete but just to check */
582         if (kill_complete(kill) == 0)
583                 goto done;
584
585         test_ok += 1;
586
587 done:
588         event_loopexit(NULL);
589 }
590
591 /* we just pause the rpc and continue it in the next callback */
592
593 struct _rpc_hook_ctx {
594         void *vbase;
595         void *ctx;
596 };
597
598 static int hook_pause_cb_called=0;
599
600 static void
601 rpc_hook_pause_cb(evutil_socket_t fd, short what, void *arg)
602 {
603         struct _rpc_hook_ctx *ctx = arg;
604         ++hook_pause_cb_called;
605         evrpc_resume_request(ctx->vbase, ctx->ctx, EVRPC_CONTINUE);
606         free(arg);
607 }
608
609 static int
610 rpc_hook_pause(void *ctx, struct evhttp_request *req, struct evbuffer *evbuf,
611     void *arg)
612 {
613         struct _rpc_hook_ctx *tmp = malloc(sizeof(*tmp));
614         struct timeval tv;
615
616         assert(tmp != NULL);
617         tmp->vbase = arg;
618         tmp->ctx = ctx;
619
620         memset(&tv, 0, sizeof(tv));
621         event_once(-1, EV_TIMEOUT, rpc_hook_pause_cb, tmp, &tv);
622         return EVRPC_PAUSE;
623 }
624
625 static void
626 rpc_basic_client_with_pause(void)
627 {
628         ev_uint16_t port;
629         struct evhttp *http = NULL;
630         struct evrpc_base *base = NULL;
631         struct evrpc_pool *pool = NULL;
632         struct msg *msg = NULL;
633         struct kill *kill= NULL;
634
635         rpc_setup(&http, &port, &base);
636
637         assert(evrpc_add_hook(base, EVRPC_INPUT, rpc_hook_pause, base));
638         assert(evrpc_add_hook(base, EVRPC_OUTPUT, rpc_hook_pause, base));
639
640         pool = rpc_pool_with_connection(port);
641
642         assert(evrpc_add_hook(pool, EVRPC_INPUT, rpc_hook_pause, pool));
643         assert(evrpc_add_hook(pool, EVRPC_OUTPUT, rpc_hook_pause, pool));
644
645         /* set up the basic message */
646         msg = msg_new();
647         EVTAG_ASSIGN(msg, from_name, "niels");
648         EVTAG_ASSIGN(msg, to_name, "tester");
649
650         kill = kill_new();
651
652         EVRPC_MAKE_REQUEST(Message, pool, msg, kill, GotKillCb, NULL);
653
654         test_ok = 0;
655
656         event_dispatch();
657
658         tt_int_op(test_ok, ==, 1);
659         tt_int_op(hook_pause_cb_called, ==, 4);
660
661 end:
662         if (base)
663                 rpc_teardown(base);
664
665         if (msg)
666                 msg_free(msg);
667         if (kill)
668                 kill_free(kill);
669
670         if (pool)
671                 evrpc_pool_free(pool);
672         if (http)
673                 evhttp_free(http);
674 }
675
676 static void
677 rpc_client_timeout(void)
678 {
679         ev_uint16_t port;
680         struct evhttp *http = NULL;
681         struct evrpc_base *base = NULL;
682         struct evrpc_pool *pool = NULL;
683         struct msg *msg = NULL;
684         struct kill *kill = NULL;
685
686         rpc_setup(&http, &port, &base);
687
688         pool = rpc_pool_with_connection(port);
689
690         /* set the timeout to 5 seconds */
691         evrpc_pool_set_timeout(pool, 5);
692
693         /* set up the basic message */
694         msg = msg_new();
695         EVTAG_ASSIGN(msg, from_name, "niels");
696         EVTAG_ASSIGN(msg, to_name, "tester");
697
698         kill = kill_new();
699
700         EVRPC_MAKE_REQUEST(NeverReply, pool, msg, kill, GotErrorCb, NULL);
701
702         test_ok = 0;
703
704         event_dispatch();
705
706         /* free the saved RPC structure up */
707         EVRPC_REQUEST_DONE(saved_rpc);
708
709         rpc_teardown(base);
710
711         tt_assert(test_ok == 2);
712
713 end:
714         if (msg)
715                 msg_free(msg);
716         if (kill)
717                 kill_free(kill);
718
719         if (pool)
720                 evrpc_pool_free(pool);
721         if (http)
722                 evhttp_free(http);
723 }
724
725 static void
726 rpc_test(void)
727 {
728         struct msg *msg = NULL, *msg2 = NULL;
729         struct kill *attack = NULL;
730         struct run *run = NULL;
731         struct evbuffer *tmp = evbuffer_new();
732         struct timeval tv_start, tv_end;
733         ev_uint32_t tag;
734         int i;
735
736         msg = msg_new();
737         EVTAG_ASSIGN(msg, from_name, "niels");
738         EVTAG_ASSIGN(msg, to_name, "phoenix");
739
740         if (EVTAG_GET(msg, attack, &attack) == -1) {
741                 tt_abort_msg("Failed to set kill message.");
742         }
743
744         EVTAG_ASSIGN(attack, weapon, "feather");
745         EVTAG_ASSIGN(attack, action, "tickle");
746         for (i = 0; i < 3; ++i) {
747                 if (EVTAG_ARRAY_ADD_VALUE(attack, how_often, i) == NULL) {
748                         tt_abort_msg("Failed to add how_often.");
749                 }
750         }
751
752         evutil_gettimeofday(&tv_start, NULL);
753         for (i = 0; i < 1000; ++i) {
754                 run = EVTAG_ARRAY_ADD(msg, run);
755                 if (run == NULL) {
756                         tt_abort_msg("Failed to add run message.");
757                 }
758                 EVTAG_ASSIGN(run, how, "very fast but with some data in it");
759                 EVTAG_ASSIGN(run, fixed_bytes,
760                     (ev_uint8_t*)"012345678901234567890123");
761
762                 if (EVTAG_ARRAY_ADD_VALUE(
763                             run, notes, "this is my note") == NULL) {
764                         tt_abort_msg("Failed to add note.");
765                 }
766                 if (EVTAG_ARRAY_ADD_VALUE(run, notes, "pps") == NULL) {
767                         tt_abort_msg("Failed to add note");
768                 }
769
770                 EVTAG_ASSIGN(run, large_number, 0xdead0a0bcafebeefLL);
771                 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xdead0a0b);
772                 EVTAG_ARRAY_ADD_VALUE(run, other_numbers, 0xbeefcafe);
773         }
774
775         if (msg_complete(msg) == -1)
776                 tt_abort_msg("Failed to make complete message.");
777
778         evtag_marshal_msg(tmp, 0xdeaf, msg);
779
780         if (evtag_peek(tmp, &tag) == -1)
781                 tt_abort_msg("Failed to peak tag.");
782
783         if (tag != 0xdeaf)
784                 TT_DIE(("Got incorrect tag: %0x.", (unsigned)tag));
785
786         msg2 = msg_new();
787         if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1)
788                 tt_abort_msg("Failed to unmarshal message.");
789
790         evutil_gettimeofday(&tv_end, NULL);
791         evutil_timersub(&tv_end, &tv_start, &tv_end);
792         TT_BLATHER(("(%.1f us/add) ",
793                 (float)tv_end.tv_sec/(float)i * 1000000.0 +
794                 tv_end.tv_usec / (float)i));
795
796         if (!EVTAG_HAS(msg2, from_name) ||
797             !EVTAG_HAS(msg2, to_name) ||
798             !EVTAG_HAS(msg2, attack)) {
799                 tt_abort_msg("Missing data structures.");
800         }
801
802         if (EVTAG_GET(msg2, attack, &attack) == -1) {
803                 tt_abort_msg("Could not get attack.");
804         }
805
806         if (EVTAG_ARRAY_LEN(msg2, run) != i) {
807                 tt_abort_msg("Wrong number of run messages.");
808         }
809
810         /* get the very first run message */
811         if (EVTAG_ARRAY_GET(msg2, run, 0, &run) == -1) {
812                 tt_abort_msg("Failed to get run msg.");
813         } else {
814                 /* verify the notes */
815                 char *note_one, *note_two;
816                 ev_uint64_t large_number;
817                 ev_uint32_t short_number;
818
819                 if (EVTAG_ARRAY_LEN(run, notes) != 2) {
820                         tt_abort_msg("Wrong number of note strings.");
821                 }
822
823                 if (EVTAG_ARRAY_GET(run, notes, 0, &note_one) == -1 ||
824                     EVTAG_ARRAY_GET(run, notes, 1, &note_two) == -1) {
825                         tt_abort_msg("Could not get note strings.");
826                 }
827
828                 if (strcmp(note_one, "this is my note") ||
829                     strcmp(note_two, "pps")) {
830                         tt_abort_msg("Incorrect note strings encoded.");
831                 }
832
833                 if (EVTAG_GET(run, large_number, &large_number) == -1 ||
834                     large_number != 0xdead0a0bcafebeefLL) {
835                         tt_abort_msg("Incorrrect large_number.");
836                 }
837
838                 if (EVTAG_ARRAY_LEN(run, other_numbers) != 2) {
839                         tt_abort_msg("Wrong number of other_numbers.");
840                 }
841
842                 if (EVTAG_ARRAY_GET(
843                             run, other_numbers, 0, &short_number) == -1) {
844                         tt_abort_msg("Could not get short number.");
845                 }
846                 tt_uint_op(short_number, ==, 0xdead0a0b);
847
848         }
849         tt_int_op(EVTAG_ARRAY_LEN(attack, how_often), ==, 3);
850
851         for (i = 0; i < 3; ++i) {
852                 ev_uint32_t res;
853                 if (EVTAG_ARRAY_GET(attack, how_often, i, &res) == -1) {
854                         TT_DIE(("Cannot get %dth how_often msg.", i));
855                 }
856                 if ((int)res != i) {
857                         TT_DIE(("Wrong message encoded %d != %d", i, res));
858                 }
859         }
860
861         test_ok = 1;
862 end:
863         if (msg)
864                 msg_free(msg);
865         if (msg2)
866                 msg_free(msg2);
867         if (tmp)
868                 evbuffer_free(tmp);
869 }
870
871 #define RPC_LEGACY(name)                                                \
872         { #name, run_legacy_test_fn, TT_FORK|TT_NEED_BASE|TT_LEGACY,    \
873                     &legacy_setup,                                      \
874                     rpc_##name }
875 #else
876 /* NO_PYTHON_EXISTS */
877
878 #define RPC_LEGACY(name) \
879         { #name, NULL, TT_SKIP, NULL, NULL }
880
881 #endif
882
883 struct testcase_t rpc_testcases[] = {
884         RPC_LEGACY(basic_test),
885         RPC_LEGACY(basic_message),
886         RPC_LEGACY(basic_client),
887         RPC_LEGACY(basic_queued_client),
888         RPC_LEGACY(basic_client_with_pause),
889         RPC_LEGACY(client_timeout),
890         RPC_LEGACY(test),
891
892         END_OF_TESTCASES,
893 };