2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
5 * GNUnet is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your
8 * option) any later version.
10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file multicast/test_multicast.c
23 * @brief Tests for the Multicast API.
24 * @author Gabor X Toth
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_common.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testing_lib.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_multicast_service.h"
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
40 * Return value from 'main'.
45 * Handle for task for timeout termination.
47 static struct GNUNET_SCHEDULER_Task * end_badly_task;
49 static const struct GNUNET_CONFIGURATION_Handle *cfg;
51 struct GNUNET_CORE_Handle *core;
52 struct GNUNET_PeerIdentity this_peer;
54 struct GNUNET_MULTICAST_Origin *origin;
55 struct GNUNET_MULTICAST_Member *member;
57 struct GNUNET_CRYPTO_EddsaPrivateKey *group_key;
58 struct GNUNET_CRYPTO_EddsaPublicKey group_pub_key;
60 struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key;
61 struct GNUNET_CRYPTO_EcdsaPublicKey member_pub_key;
63 struct TransmitClosure {
64 struct GNUNET_MULTICAST_OriginTransmitHandle *orig_tmit;
65 struct GNUNET_MULTICAST_MemberTransmitHandle *mem_tmit;
67 uint8_t data_delay[16];
73 struct OriginClosure {
74 uint8_t msgs_expected;
78 struct MemberClosure {
79 uint8_t msgs_expected;
83 struct GNUNET_MessageHeader *join_req, *join_resp;
88 TEST_ORIGIN_START = 1,
89 TEST_MEMBER_JOIN_REFUSE = 2,
90 TEST_MEMBER_JOIN_ADMIT = 3,
91 TEST_ORIGIN_TO_ALL = 4,
92 TEST_ORIGIN_TO_ALL_RECV = 5,
93 TEST_MEMBER_TO_ORIGIN = 6,
94 TEST_MEMBER_REPLAY_ERROR = 7,
95 TEST_MEMBER_REPLAY_OK = 8,
97 TEST_ORIGIN_STOP = 10,
100 uint64_t replay_fragment_id;
101 uint64_t replay_flags;
108 * Clean up all resources used.
115 GNUNET_CORE_disconnect (core);
120 GNUNET_MULTICAST_member_part (member, NULL, NULL);
125 GNUNET_MULTICAST_origin_stop (origin, NULL, NULL);
132 * Terminate the test case (failure).
137 end_badly (void *cls)
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
146 * Terminate the test case (success).
151 end_normally (void *cls)
155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
160 * Finish the test case (successfully).
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
167 if (end_badly_task != NULL)
169 GNUNET_SCHEDULER_cancel (end_badly_task);
170 end_badly_task = NULL;
172 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
173 &end_normally, NULL);
178 tmit_resume (void *cls)
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
181 struct TransmitClosure *tmit = cls;
182 if (NULL != tmit->orig_tmit)
183 GNUNET_MULTICAST_origin_to_all_resume (tmit->orig_tmit);
184 else if (NULL != tmit->mem_tmit)
185 GNUNET_MULTICAST_member_to_origin_resume (tmit->mem_tmit);
190 tmit_notify (void *cls, size_t *data_size, void *data)
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "Test #%u: origin_tmit_notify()\n", test);
194 struct TransmitClosure *tmit = cls;
196 if (0 == tmit->data_count)
202 uint16_t size = strlen (tmit->data[tmit->n]);
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Transmit notify data: %u bytes available, "
205 "processing fragment %u/%u (size %u).\n",
206 *data_size, tmit->n + 1, tmit->data_count, size);
207 if (*data_size < size)
211 return GNUNET_SYSERR;
214 if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
217 tmit->paused = GNUNET_YES;
218 GNUNET_SCHEDULER_add_delayed (
219 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
220 tmit->data_delay[tmit->n]),
225 tmit->paused = GNUNET_NO;
228 memcpy (data, tmit->data[tmit->n], size);
230 return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
235 member_recv_join_request (void *cls,
236 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
237 const struct GNUNET_MessageHeader *join_msg,
238 struct GNUNET_MULTICAST_JoinHandle *jh)
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Test #%u: member_recv_join_request()\n", test);
246 origin_stopped (void *cls)
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Test #%u: origin_stopped()\n", test);
255 schedule_origin_stop (void *cls)
257 test = TEST_ORIGIN_STOP;
258 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
259 "Test #%u: origin_stop()\n", test);
260 GNUNET_MULTICAST_origin_stop (origin, origin_stopped, NULL);
266 member_parted (void *cls)
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "Test #%u: member_parted()\n", test);
274 case TEST_MEMBER_JOIN_REFUSE:
275 member_join (TEST_MEMBER_JOIN_ADMIT);
278 case TEST_MEMBER_PART:
279 GNUNET_SCHEDULER_add_now (&schedule_origin_stop, NULL);
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 "Invalid test #%d in member_recv_join_decision()\n", test);
291 schedule_member_part (void *cls)
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Test #%u: schedule_member_part()\n", test);
295 GNUNET_MULTICAST_member_part (member, member_parted, NULL);
302 test = TEST_MEMBER_PART;
303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
304 "Test #%u: member_part()\n", test);
305 GNUNET_SCHEDULER_add_now (&schedule_member_part, NULL);
312 test = TEST_MEMBER_REPLAY_OK;
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Test #%u: member_replay_ok()\n", test);
315 replay_fragment_id = 1;
316 replay_flags = 1 | 1<<11;
317 GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
323 member_replay_error ()
325 test = TEST_MEMBER_REPLAY_ERROR;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Test #%u: member_replay_error()\n", test);
328 replay_fragment_id = 1234;
329 replay_flags = 11 | 1<<11;
330 GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
336 origin_recv_replay_msg (void *cls,
337 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
339 uint64_t fragment_offset,
341 struct GNUNET_MULTICAST_ReplayHandle *rh)
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "Test #%u: origin_recv_replay_msg()\n", test);
350 member_recv_replay_msg (void *cls,
351 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
353 uint64_t fragment_offset,
355 struct GNUNET_MULTICAST_ReplayHandle *rh)
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Test #%u: member_recv_replay_msg()\n", test);
364 origin_recv_replay_frag (void *cls,
365 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
366 uint64_t fragment_id,
368 struct GNUNET_MULTICAST_ReplayHandle *rh)
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Test #%u: origin_recv_replay_frag()"
372 " - fragment_id=%" PRIu64 " flags=%" PRIu64 "\n",
373 test, fragment_id, flags);
374 GNUNET_assert (replay_fragment_id == fragment_id && replay_flags == flags);
377 case TEST_MEMBER_REPLAY_ERROR:
378 GNUNET_MULTICAST_replay_response (rh, NULL, GNUNET_SYSERR);
382 case TEST_MEMBER_REPLAY_OK:
384 struct GNUNET_MULTICAST_MessageHeader mmsg = {
386 .type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE),
387 .size = htons (sizeof (mmsg)),
389 .fragment_id = GNUNET_htonll (1),
390 .message_id = GNUNET_htonll (1),
391 .fragment_offset = 0,
392 .group_generation = GNUNET_htonll (1),
396 member_cls.msgs_expected = 1;
397 GNUNET_MULTICAST_replay_response (rh, &mmsg.header, GNUNET_MULTICAST_REC_OK);
398 GNUNET_MULTICAST_replay_response_end (rh);
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404 "Invalid test #%d in origin_recv_replay_frag()\n", test);
411 member_recv_replay_frag (void *cls,
412 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
413 uint64_t fragment_id,
415 struct GNUNET_MULTICAST_ReplayHandle *rh)
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Test #%u: member_recv_replay_frag()\n", test);
424 origin_recv_request (void *cls,
425 const struct GNUNET_MULTICAST_RequestHeader *req)
427 struct OriginClosure *ocls = cls;
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Test #%u: origin_recv_request()\n", test);
430 if (++ocls->n != ocls->msgs_expected)
433 GNUNET_assert (0 == memcmp (&req->member_pub_key,
434 &member_pub_key, sizeof (member_pub_key)));
437 // FIXME: check message content
439 member_replay_error ();
446 test = TEST_MEMBER_TO_ORIGIN;
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Test #%u: member_to_origin()\n", test);
450 struct TransmitClosure *tmit = &tmit_cls;
451 *tmit = (struct TransmitClosure) {};
452 tmit->data[0] = "abc def";
453 tmit->data[1] = "ghi jkl mno";
454 tmit->data_delay[1] = 2;
455 tmit->data[2] = "pqr stuw xyz";
456 tmit->data_count = 3;
459 origin_cls.msgs_expected = 1;
461 tmit->mem_tmit = GNUNET_MULTICAST_member_to_origin (member, 1,
467 member_recv_message (void *cls,
468 const struct GNUNET_MULTICAST_MessageHeader *msg)
470 struct MemberClosure *mcls = cls;
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Test #%u: member_recv_message() %u/%u\n",
473 test, mcls->n + 1, mcls->msgs_expected);
474 if (++mcls->n != mcls->msgs_expected)
477 // FIXME: check message content
481 case TEST_ORIGIN_TO_ALL_RECV:
485 case TEST_MEMBER_REPLAY_OK:
486 GNUNET_assert (replay_fragment_id == GNUNET_ntohll (msg->fragment_id));
491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
492 "Invalid test #%d in origin_recv_message()\n", test);
499 origin_recv_message (void *cls,
500 const struct GNUNET_MULTICAST_MessageHeader *msg)
502 struct OriginClosure *ocls = cls;
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Test #%u: origin_recv_message() %u/%u\n",
505 test, ocls->n + 1, ocls->msgs_expected);
506 if (++ocls->n != ocls->msgs_expected)
509 // FIXME: check message content
513 case TEST_ORIGIN_TO_ALL:
514 test = TEST_ORIGIN_TO_ALL_RECV;
518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
519 "Invalid test #%d in origin_recv_message()\n", test);
528 test = TEST_ORIGIN_TO_ALL;
529 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
530 "Test #%u: origin_to_all()\n", test);
532 struct TransmitClosure *tmit = &tmit_cls;
533 *tmit = (struct TransmitClosure) {};
534 tmit->data[0] = "ABC DEF";
535 tmit->data[1] = GNUNET_malloc (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD + 1);
537 for (i = 0; i < GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD; i++)
538 tmit->data[1][i] = (0 == i % 10000) ? '0' + i / 10000 : '_';
539 tmit->data[2] = "GHI JKL MNO";
540 tmit->data_delay[2] = 2;
541 tmit->data[3] = "PQR STUW XYZ";
542 tmit->data_count = 4;
544 origin_cls.n = member_cls.n = 0;
545 origin_cls.msgs_expected = member_cls.msgs_expected = tmit->data_count;
547 tmit->orig_tmit = GNUNET_MULTICAST_origin_to_all (origin, 1, 1,
553 member_recv_join_decision (void *cls,
555 const struct GNUNET_PeerIdentity *peer,
556 uint16_t relay_count,
557 const struct GNUNET_PeerIdentity *relays,
558 const struct GNUNET_MessageHeader *join_msg)
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Test #%u: member_recv_join_decision() - is_admitted: %d\n",
564 GNUNET_assert (join_msg->size == join_resp->size);
565 GNUNET_assert (join_msg->type == join_resp->type);
566 GNUNET_assert (0 == memcmp (join_msg, join_resp, ntohs (join_resp->size)));
570 case TEST_MEMBER_JOIN_REFUSE:
571 GNUNET_assert (0 == relay_count);
572 GNUNET_SCHEDULER_add_now (&schedule_member_part, NULL);
575 case TEST_MEMBER_JOIN_ADMIT:
576 GNUNET_assert (1 == relay_count);
577 GNUNET_assert (0 == memcmp (relays, &this_peer, sizeof (this_peer)));
582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583 "Invalid test #%d in member_recv_join_decision()\n", test);
590 origin_recv_join_request (void *cls,
591 const struct GNUNET_CRYPTO_EcdsaPublicKey *mem_key,
592 const struct GNUNET_MessageHeader *join_msg,
593 struct GNUNET_MULTICAST_JoinHandle *jh)
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 "Test #%u: origin_recv_join_request()\n", test);
598 GNUNET_assert (0 == memcmp (mem_key, &member_pub_key, sizeof (member_pub_key)));
599 GNUNET_assert (join_msg->size == join_req->size);
600 GNUNET_assert (join_msg->type == join_req->type);
601 GNUNET_assert (0 == memcmp (join_msg, join_req, ntohs (join_req->size)));
603 char data[] = "here's the decision";
604 uint8_t data_size = strlen (data) + 1;
605 join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
606 join_resp->size = htons (sizeof (join_resp) + data_size);
607 join_resp->type = htons (456);
608 memcpy (&join_resp[1], data, data_size);
612 case TEST_MEMBER_JOIN_REFUSE:
613 GNUNET_MULTICAST_join_decision (jh, GNUNET_NO, 0, NULL, join_resp);
616 case TEST_MEMBER_JOIN_ADMIT:
617 GNUNET_MULTICAST_join_decision (jh, GNUNET_YES, 1, &this_peer, join_resp);
621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
622 "Invalid test #%d in origin_recv_join_request()\n", test);
633 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
634 "Test #%u: member_join()\n", test);
636 member_key = GNUNET_CRYPTO_ecdsa_key_create ();
637 GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
639 if (NULL != join_req)
640 GNUNET_free (join_req);
642 char data[] = "let me in!";
643 uint8_t data_size = strlen (data) + 1;
644 join_req = GNUNET_malloc (sizeof (join_req) + data_size);
645 join_req->size = htons (sizeof (join_req) + data_size);
646 join_req->type = htons (123);
647 memcpy (&join_req[1], data, data_size);
649 member = GNUNET_MULTICAST_member_join (cfg, &group_pub_key, member_key,
650 &this_peer, 1, &this_peer, join_req,
651 member_recv_join_request,
652 member_recv_join_decision,
653 member_recv_replay_frag,
654 member_recv_replay_msg,
663 test = TEST_ORIGIN_START;
664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
665 "Test #%u: origin_start()\n", test);
667 group_key = GNUNET_CRYPTO_eddsa_key_create ();
668 GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
670 origin = GNUNET_MULTICAST_origin_start (cfg, group_key, 0,
671 origin_recv_join_request,
672 origin_recv_replay_frag,
673 origin_recv_replay_msg,
677 member_join (TEST_MEMBER_JOIN_REFUSE);
682 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
684 this_peer = *my_identity;
690 * Main function of the test, run from scheduler.
693 * @param cfg configuration we use (also to connect to Multicast service)
694 * @param peer handle to access more of the peer (not used)
697 #if DEBUG_TEST_MULTICAST
701 const struct GNUNET_CONFIGURATION_Handle *c)
704 const struct GNUNET_CONFIGURATION_Handle *c,
705 struct GNUNET_TESTING_Peer *peer)
709 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
711 core = GNUNET_CORE_connect (cfg, NULL,
712 &core_connected, NULL, NULL,
720 main (int argc, char *argv[])
723 #if DEBUG_TEST_MULTICAST
724 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
725 GNUNET_GETOPT_OPTION_END
727 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-multicast",
728 "test-multicast [options]",
732 if (0 != GNUNET_TESTING_peer_run ("test-multicast", "test_multicast.conf", &run, NULL))
738 /* end of test_multicast.c */