2 * This file is part of GNUnet
3 * Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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).
135 * @param tc scheduler context
138 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
147 * Terminate the test case (success).
150 * @param tc scheduler context
153 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
162 * Finish the test case (successfully).
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
169 if (end_badly_task != NULL)
171 GNUNET_SCHEDULER_cancel (end_badly_task);
172 end_badly_task = NULL;
174 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
175 &end_normally, NULL);
180 tmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
183 struct TransmitClosure *tmit = cls;
184 if (NULL != tmit->orig_tmit)
185 GNUNET_MULTICAST_origin_to_all_resume (tmit->orig_tmit);
187 GNUNET_MULTICAST_member_to_origin_resume (tmit->mem_tmit);
192 tmit_notify (void *cls, size_t *data_size, void *data)
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "Test #%u: origin_tmit_notify()\n", test);
196 struct TransmitClosure *tmit = cls;
198 if (0 == tmit->data_count)
204 uint16_t size = strlen (tmit->data[tmit->n]);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Transmit notify data: %u bytes available, "
207 "processing fragment %u/%u (size %u).\n",
208 *data_size, tmit->n + 1, tmit->data_count, size);
209 if (*data_size < size)
213 return GNUNET_SYSERR;
216 if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
219 tmit->paused = GNUNET_YES;
220 GNUNET_SCHEDULER_add_delayed (
221 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
222 tmit->data_delay[tmit->n]),
227 tmit->paused = GNUNET_NO;
230 memcpy (data, tmit->data[tmit->n], size);
232 return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
237 member_recv_join_request (void *cls,
238 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
239 const struct GNUNET_MessageHeader *join_msg,
240 struct GNUNET_MULTICAST_JoinHandle *jh)
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Test #%u: member_recv_join_request()\n", test);
248 origin_stopped (void *cls)
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Test #%u: origin_stopped()\n", test);
257 schedule_origin_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 test = TEST_ORIGIN_STOP;
260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
261 "Test #%u: origin_stop()\n", test);
262 GNUNET_MULTICAST_origin_stop (origin, origin_stopped, NULL);
268 member_parted (void *cls)
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "Test #%u: member_parted()\n", test);
276 case TEST_MEMBER_JOIN_REFUSE:
277 member_join (TEST_MEMBER_JOIN_ADMIT);
280 case TEST_MEMBER_PART:
281 GNUNET_SCHEDULER_add_now (schedule_origin_stop, NULL);
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286 "Invalid test #%d in member_recv_join_decision()\n", test);
293 schedule_member_part (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Test #%u: schedule_member_part()\n", test);
297 GNUNET_MULTICAST_member_part (member, member_parted, NULL);
304 test = TEST_MEMBER_PART;
305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
306 "Test #%u: member_part()\n", test);
307 GNUNET_SCHEDULER_add_now (schedule_member_part, NULL);
314 test = TEST_MEMBER_REPLAY_OK;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Test #%u: member_replay_ok()\n", test);
317 replay_fragment_id = 1;
318 replay_flags = 1 | 1<<11;
319 GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
325 member_replay_error ()
327 test = TEST_MEMBER_REPLAY_ERROR;
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "Test #%u: member_replay_error()\n", test);
330 replay_fragment_id = 1234;
331 replay_flags = 11 | 1<<11;
332 GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
338 origin_recv_replay_msg (void *cls,
339 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
341 uint64_t fragment_offset,
343 struct GNUNET_MULTICAST_ReplayHandle *rh)
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Test #%u: origin_recv_replay_msg()\n", test);
352 member_recv_replay_msg (void *cls,
353 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
355 uint64_t fragment_offset,
357 struct GNUNET_MULTICAST_ReplayHandle *rh)
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Test #%u: member_recv_replay_msg()\n", test);
366 origin_recv_replay_frag (void *cls,
367 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
368 uint64_t fragment_id,
370 struct GNUNET_MULTICAST_ReplayHandle *rh)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Test #%u: origin_recv_replay_frag()"
374 " - fragment_id=%" PRIu64 " flags=%" PRIu64 "\n",
375 test, fragment_id, flags);
376 GNUNET_assert (replay_fragment_id == fragment_id && replay_flags == flags);
379 case TEST_MEMBER_REPLAY_ERROR:
380 GNUNET_MULTICAST_replay_response (rh, NULL, GNUNET_SYSERR);
384 case TEST_MEMBER_REPLAY_OK:
386 struct GNUNET_MULTICAST_MessageHeader mmsg = {
388 .type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE),
389 .size = htons (sizeof (mmsg)),
391 .fragment_id = GNUNET_htonll (1),
392 .message_id = GNUNET_htonll (1),
393 .fragment_offset = 0,
394 .group_generation = GNUNET_htonll (1),
398 member_cls.msgs_expected = 1;
399 GNUNET_MULTICAST_replay_response (rh, &mmsg.header, GNUNET_MULTICAST_REC_OK);
400 GNUNET_MULTICAST_replay_response_end (rh);
405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
406 "Invalid test #%d in origin_recv_replay_frag()\n", test);
413 member_recv_replay_frag (void *cls,
414 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
415 uint64_t fragment_id,
417 struct GNUNET_MULTICAST_ReplayHandle *rh)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Test #%u: member_recv_replay_frag()\n", test);
426 origin_recv_request (void *cls,
427 const struct GNUNET_MULTICAST_RequestHeader *req)
429 struct OriginClosure *ocls = cls;
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Test #%u: origin_recv_request()\n", test);
432 if (++ocls->n != ocls->msgs_expected)
435 GNUNET_assert (0 == memcmp (&req->member_key,
436 &member_pub_key, sizeof (member_pub_key)));
439 // FIXME: check message content
441 member_replay_error ();
448 test = TEST_MEMBER_TO_ORIGIN;
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "Test #%u: member_to_origin()\n", test);
452 struct TransmitClosure *tmit = &tmit_cls;
453 *tmit = (struct TransmitClosure) {};
454 tmit->data[0] = "abc def";
455 tmit->data[1] = "ghi jkl mno";
456 tmit->data_delay[1] = 1;
457 tmit->data[2] = "pqr stuw xyz";
458 tmit->data_count = 3;
461 origin_cls.msgs_expected = 1;
463 GNUNET_MULTICAST_member_to_origin (member, 1, tmit_notify, tmit);
468 member_recv_message (void *cls,
469 const struct GNUNET_MULTICAST_MessageHeader *msg)
471 struct MemberClosure *mcls = cls;
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Test #%u: member_recv_message() %u/%u\n",
474 test, mcls->n + 1, mcls->msgs_expected);
475 if (++mcls->n != mcls->msgs_expected)
478 // FIXME: check message content
482 case TEST_ORIGIN_TO_ALL_RECV:
486 case TEST_MEMBER_REPLAY_OK:
487 GNUNET_assert (replay_fragment_id == GNUNET_ntohll (msg->fragment_id));
492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
493 "Invalid test #%d in origin_recv_message()\n", test);
500 origin_recv_message (void *cls,
501 const struct GNUNET_MULTICAST_MessageHeader *msg)
503 struct OriginClosure *ocls = cls;
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Test #%u: origin_recv_message() %u/%u\n",
506 test, ocls->n + 1, ocls->msgs_expected);
507 if (++ocls->n != ocls->msgs_expected)
510 // FIXME: check message content
514 case TEST_ORIGIN_TO_ALL:
515 test = TEST_ORIGIN_TO_ALL_RECV;
519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
520 "Invalid test #%d in origin_recv_message()\n", test);
529 test = TEST_ORIGIN_TO_ALL;
530 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
531 "Test #%u: origin_to_all()\n", test);
533 struct TransmitClosure *tmit = &tmit_cls;
534 *tmit = (struct TransmitClosure) {};
535 tmit->data[0] = "ABC DEF";
536 tmit->data[1] = "GHI JKL MNO";
537 tmit->data_delay[1] = 1;
538 tmit->data[2] = "PQR STUW XYZ";
539 tmit->data_count = 3;
541 origin_cls.n = member_cls.n = 0;
542 origin_cls.msgs_expected = member_cls.msgs_expected = 1;
544 GNUNET_MULTICAST_origin_to_all (origin, 1, 1, tmit_notify, tmit);
549 member_recv_join_decision (void *cls,
551 const struct GNUNET_PeerIdentity *peer,
552 uint16_t relay_count,
553 const struct GNUNET_PeerIdentity *relays,
554 const struct GNUNET_MessageHeader *join_msg)
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Test #%u: member_recv_join_decision() - is_admitted: %d\n",
560 GNUNET_assert (join_msg->size == join_resp->size);
561 GNUNET_assert (join_msg->type == join_resp->type);
562 GNUNET_assert (0 == memcmp (join_msg, join_resp, ntohs (join_resp->size)));
566 case TEST_MEMBER_JOIN_REFUSE:
567 GNUNET_assert (0 == relay_count);
568 GNUNET_SCHEDULER_add_now (schedule_member_part, NULL);
571 case TEST_MEMBER_JOIN_ADMIT:
572 GNUNET_assert (1 == relay_count);
573 GNUNET_assert (0 == memcmp (relays, &this_peer, sizeof (this_peer)));
578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
579 "Invalid test #%d in member_recv_join_decision()\n", test);
586 origin_recv_join_request (void *cls,
587 const struct GNUNET_CRYPTO_EcdsaPublicKey *mem_key,
588 const struct GNUNET_MessageHeader *join_msg,
589 struct GNUNET_MULTICAST_JoinHandle *jh)
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "Test #%u: origin_recv_join_request()\n", test);
594 GNUNET_assert (0 == memcmp (mem_key, &member_pub_key, sizeof (member_pub_key)));
595 GNUNET_assert (join_msg->size == join_req->size);
596 GNUNET_assert (join_msg->type == join_req->type);
597 GNUNET_assert (0 == memcmp (join_msg, join_req, ntohs (join_req->size)));
599 char data[] = "here's the decision";
600 uint8_t data_size = strlen (data) + 1;
601 join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
602 join_resp->size = htons (sizeof (join_resp) + data_size);
603 join_resp->type = htons (456);
604 memcpy (&join_resp[1], data, data_size);
608 case TEST_MEMBER_JOIN_REFUSE:
609 GNUNET_MULTICAST_join_decision (jh, GNUNET_NO, 0, NULL, join_resp);
612 case TEST_MEMBER_JOIN_ADMIT:
613 GNUNET_MULTICAST_join_decision (jh, GNUNET_YES, 1, &this_peer, join_resp);
617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
618 "Invalid test #%d in origin_recv_join_request()\n", test);
629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
630 "Test #%u: member_join()\n", test);
632 member_key = GNUNET_CRYPTO_ecdsa_key_create ();
633 GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
635 if (NULL != join_req)
636 GNUNET_free (join_req);
638 char data[] = "let me in!";
639 uint8_t data_size = strlen (data) + 1;
640 join_req = GNUNET_malloc (sizeof (join_req) + data_size);
641 join_req->size = htons (sizeof (join_req) + data_size);
642 join_req->type = htons (123);
643 memcpy (&join_req[1], data, data_size);
645 member = GNUNET_MULTICAST_member_join (cfg, &group_pub_key, member_key,
646 &this_peer, 1, &this_peer, join_req,
647 member_recv_join_request,
648 member_recv_join_decision,
649 member_recv_replay_frag,
650 member_recv_replay_msg,
659 test = TEST_ORIGIN_START;
660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
661 "Test #%u: origin_start()\n", test);
663 group_key = GNUNET_CRYPTO_eddsa_key_create ();
664 GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
666 origin = GNUNET_MULTICAST_origin_start (cfg, group_key, 0,
667 origin_recv_join_request,
668 origin_recv_replay_frag,
669 origin_recv_replay_msg,
673 member_join (TEST_MEMBER_JOIN_REFUSE);
678 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
680 this_peer = *my_identity;
686 * Main function of the test, run from scheduler.
689 * @param cfg configuration we use (also to connect to Multicast service)
690 * @param peer handle to access more of the peer (not used)
693 #if DEBUG_TEST_MULTICAST
694 run (void *cls, char *const *args, const char *cfgfile,
695 const struct GNUNET_CONFIGURATION_Handle *c)
698 const struct GNUNET_CONFIGURATION_Handle *c,
699 struct GNUNET_TESTING_Peer *peer)
703 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
705 core = GNUNET_CORE_connect (cfg, NULL, &core_connected, NULL, NULL,
706 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
711 main (int argc, char *argv[])
714 #if DEBUG_TEST_MULTICAST
715 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
716 GNUNET_GETOPT_OPTION_END
718 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-multicast",
719 "test-multicast [options]",
723 if (0 != GNUNET_TESTING_peer_run ("test-multicast", "test_multicast.conf", &run, NULL))
729 /* end of test_multicast.c */