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_multicast_service.h"
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
39 * Return value from 'main'.
44 * Handle for task for timeout termination.
46 static struct GNUNET_SCHEDULER_Task * end_badly_task;
48 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50 struct GNUNET_PeerIdentity this_peer;
52 struct GNUNET_MULTICAST_Origin *origin;
53 struct GNUNET_MULTICAST_Member *member;
55 struct GNUNET_CRYPTO_EddsaPrivateKey *group_key;
56 struct GNUNET_CRYPTO_EddsaPublicKey group_pub_key;
58 struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key;
59 struct GNUNET_CRYPTO_EcdsaPublicKey member_pub_key;
61 struct TransmitClosure {
62 struct GNUNET_MULTICAST_OriginTransmitHandle *orig_tmit;
63 struct GNUNET_MULTICAST_MemberTransmitHandle *mem_tmit;
65 uint8_t data_delay[16];
71 struct OriginClosure {
72 uint8_t msgs_expected;
76 struct MemberClosure {
77 uint8_t msgs_expected;
81 struct GNUNET_MessageHeader *join_req, *join_resp;
86 TEST_ORIGIN_START = 1,
87 TEST_MEMBER_JOIN_REFUSE = 2,
88 TEST_MEMBER_JOIN_ADMIT = 3,
89 TEST_ORIGIN_TO_ALL = 4,
90 TEST_ORIGIN_TO_ALL_RECV = 5,
91 TEST_MEMBER_TO_ORIGIN = 6,
92 TEST_MEMBER_REPLAY_ERROR = 7,
93 TEST_MEMBER_REPLAY_OK = 8,
95 TEST_ORIGIN_STOP = 10,
98 uint64_t replay_fragment_id;
99 uint64_t replay_flags;
106 * Clean up all resources used.
113 GNUNET_MULTICAST_member_part (member, NULL, NULL);
118 GNUNET_MULTICAST_origin_stop (origin, NULL, NULL);
125 * Terminate the test case (failure).
130 end_badly (void *cls)
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
139 * Terminate the test case (success).
144 end_normally (void *cls)
148 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test PASSED.\n");
153 * Finish the test case (successfully).
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
160 if (end_badly_task != NULL)
162 GNUNET_SCHEDULER_cancel (end_badly_task);
163 end_badly_task = NULL;
165 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
166 &end_normally, NULL);
171 tmit_resume (void *cls)
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
174 struct TransmitClosure *tmit = cls;
175 if (NULL != tmit->orig_tmit)
176 GNUNET_MULTICAST_origin_to_all_resume (tmit->orig_tmit);
177 else if (NULL != tmit->mem_tmit)
178 GNUNET_MULTICAST_member_to_origin_resume (tmit->mem_tmit);
183 tmit_notify (void *cls, size_t *data_size, void *data)
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Test #%u: origin_tmit_notify()\n", test);
187 struct TransmitClosure *tmit = cls;
189 if (0 == tmit->data_count)
195 uint16_t size = strlen (tmit->data[tmit->n]);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Transmit notify data: %u bytes available, processing fragment %u/%u (size %u).\n",
198 (unsigned int) *data_size,
202 if (*data_size < size)
206 return GNUNET_SYSERR;
209 if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
212 tmit->paused = GNUNET_YES;
213 GNUNET_SCHEDULER_add_delayed (
214 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
215 tmit->data_delay[tmit->n]),
220 tmit->paused = GNUNET_NO;
223 GNUNET_memcpy (data, tmit->data[tmit->n], size);
225 return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
230 member_recv_join_request (void *cls,
231 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
232 const struct GNUNET_MessageHeader *join_msg,
233 struct GNUNET_MULTICAST_JoinHandle *jh)
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 "Test #%u: member_recv_join_request()\n", test);
241 origin_stopped (void *cls)
243 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
244 "Test #%u: origin_stopped()\n", test);
250 schedule_origin_stop (void *cls)
252 test = TEST_ORIGIN_STOP;
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
254 "Test #%u: origin_stop()\n", test);
255 GNUNET_MULTICAST_origin_stop (origin, origin_stopped, NULL);
261 member_parted (void *cls)
263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
264 "Test #%u: member_parted()\n", test);
269 case TEST_MEMBER_JOIN_REFUSE:
270 // Test 3 starts here
271 member_join (TEST_MEMBER_JOIN_ADMIT);
274 case TEST_MEMBER_PART:
275 GNUNET_SCHEDULER_add_now (&schedule_origin_stop, NULL);
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280 "Invalid test #%d in member_parted()\n", test);
287 schedule_member_part (void *cls)
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
290 "Test #%u: schedule_member_part()\n", test);
291 GNUNET_MULTICAST_member_part (member, member_parted, NULL);
298 test = TEST_MEMBER_PART;
299 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
300 "Test #%u: member_part()\n", test);
301 // Test 10 starts here
302 GNUNET_SCHEDULER_add_now (&schedule_member_part, NULL);
309 // Execution of test 8 here
310 test = TEST_MEMBER_REPLAY_OK;
311 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
312 "Test #%u: member_replay_ok()\n", test);
313 replay_fragment_id = 1;
314 replay_flags = 1 | 1<<11;
315 GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
321 member_replay_error ()
323 test = TEST_MEMBER_REPLAY_ERROR;
324 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
325 "Test #%u: member_replay_error()\n", test);
326 replay_fragment_id = 1234;
327 replay_flags = 11 | 1<<11;
328 GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
334 origin_recv_replay_msg (void *cls,
335 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
337 uint64_t fragment_offset,
339 struct GNUNET_MULTICAST_ReplayHandle *rh)
341 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
342 "Test #%u: origin_recv_replay_msg()\n", test);
348 member_recv_replay_msg (void *cls,
349 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
351 uint64_t fragment_offset,
353 struct GNUNET_MULTICAST_ReplayHandle *rh)
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356 "Test #%u: member_recv_replay_msg()\n", test);
362 origin_recv_replay_frag (void *cls,
363 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
364 uint64_t fragment_id,
366 struct GNUNET_MULTICAST_ReplayHandle *rh)
368 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
369 "Test #%u: origin_recv_replay_frag()"
370 " - fragment_id=%" PRIu64 " flags=%" PRIu64 "\n",
371 test, fragment_id, flags);
372 GNUNET_assert (replay_fragment_id == fragment_id && replay_flags == flags);
375 case TEST_MEMBER_REPLAY_ERROR:
376 // Test 8 starts here
377 GNUNET_MULTICAST_replay_response (rh, NULL, GNUNET_SYSERR);
381 case TEST_MEMBER_REPLAY_OK:
383 struct GNUNET_MULTICAST_MessageHeader mmsg = {
385 .type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE),
386 .size = htons (sizeof (mmsg)),
388 .fragment_id = GNUNET_htonll (1),
389 .message_id = GNUNET_htonll (1),
390 .fragment_offset = 0,
391 .group_generation = GNUNET_htonll (1),
395 member_cls.msgs_expected = 1;
396 GNUNET_MULTICAST_replay_response (rh, &mmsg.header, GNUNET_MULTICAST_REC_OK);
397 GNUNET_MULTICAST_replay_response_end (rh);
402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
403 "Invalid test #%d in origin_recv_replay_frag()\n", test);
410 member_recv_replay_frag (void *cls,
411 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
412 uint64_t fragment_id,
414 struct GNUNET_MULTICAST_ReplayHandle *rh)
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
417 "Test #%u: member_recv_replay_frag()\n", test);
423 origin_recv_request (void *cls,
424 const struct GNUNET_MULTICAST_RequestHeader *req)
426 struct OriginClosure *ocls = cls;
427 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
428 "Test #%u: origin_recv_request()\n", test);
429 if (++ocls->n != ocls->msgs_expected)
432 GNUNET_assert (0 == memcmp (&req->member_pub_key,
433 &member_pub_key, sizeof (member_pub_key)));
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Test #%u: verify message content, take first 3 bytes: %.3s\n",
437 test, (char *)&req[1]);
438 GNUNET_assert (0 == memcmp (&req[1], "abc", 3));
440 // Test 7 starts here
441 member_replay_error ();
448 test = TEST_MEMBER_TO_ORIGIN;
449 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
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] = 2;
457 tmit->data[2] = "pqr stuw xyz";
458 tmit->data_count = 3;
461 origin_cls.msgs_expected = 1;
463 tmit->mem_tmit = GNUNET_MULTICAST_member_to_origin (member, 1,
469 member_recv_message (void *cls,
470 const struct GNUNET_MULTICAST_MessageHeader *msg)
472 struct MemberClosure *mcls = cls;
474 // Test 5 starts here after message has been received from origin
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Test #%u: member_recv_message() %u/%u\n",
478 (unsigned int) (mcls->n + 1),
479 mcls->msgs_expected);
480 if (++mcls->n != mcls->msgs_expected)
483 // FIXME: check message content
487 case TEST_ORIGIN_TO_ALL_RECV:
488 // Test 6 starts here
492 case TEST_MEMBER_REPLAY_OK:
493 // Test 9 starts here
494 GNUNET_assert (replay_fragment_id == GNUNET_ntohll (msg->fragment_id));
499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500 "Invalid test #%d in origin_recv_message()\n", test);
507 origin_recv_message (void *cls,
508 const struct GNUNET_MULTICAST_MessageHeader *msg)
510 struct OriginClosure *ocls = cls;
511 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
512 "Test #%u: origin_recv_message() %u/%u\n",
513 test, ocls->n + 1, ocls->msgs_expected);
514 if (++ocls->n != ocls->msgs_expected)
517 // FIXME: check message content
521 case TEST_ORIGIN_TO_ALL:
522 // Prepare to execute test 5
523 test = TEST_ORIGIN_TO_ALL_RECV;
527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
528 "Invalid test #%d in origin_recv_message()\n", test);
537 test = TEST_ORIGIN_TO_ALL;
538 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
539 "Test #%u: origin_to_all()\n", test);
541 struct TransmitClosure *tmit = &tmit_cls;
542 *tmit = (struct TransmitClosure) {};
543 tmit->data[0] = "ABC DEF";
544 tmit->data[1] = GNUNET_malloc (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD + 1);
546 for (i = 0; i < GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD; i++)
547 tmit->data[1][i] = (0 == i % 10000) ? '0' + i / 10000 : '_';
548 tmit->data[2] = "GHI JKL MNO";
549 tmit->data_delay[2] = 2;
550 tmit->data[3] = "PQR STUW XYZ";
551 tmit->data_count = 4;
553 origin_cls.n = member_cls.n = 0;
554 origin_cls.msgs_expected = member_cls.msgs_expected = tmit->data_count;
556 tmit->orig_tmit = GNUNET_MULTICAST_origin_to_all (origin, 1, 1,
562 member_recv_join_decision (void *cls,
564 const struct GNUNET_PeerIdentity *peer,
565 uint16_t relay_count,
566 const struct GNUNET_PeerIdentity *relays,
567 const struct GNUNET_MessageHeader *join_msg)
569 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
570 "Test #%u: member_recv_join_decision() - is_admitted: %d\n",
573 GNUNET_assert (join_msg->size == join_resp->size);
574 GNUNET_assert (join_msg->type == join_resp->type);
575 GNUNET_assert (0 == memcmp (join_msg, join_resp, ntohs (join_resp->size)));
579 case TEST_MEMBER_JOIN_REFUSE:
580 GNUNET_assert (0 == relay_count);
581 // Test 3 starts here
582 GNUNET_SCHEDULER_add_now (&schedule_member_part, NULL);
585 case TEST_MEMBER_JOIN_ADMIT:
586 GNUNET_assert (1 == relay_count);
587 GNUNET_assert (0 == memcmp (relays, &this_peer, sizeof (this_peer)));
588 // Test 4 starts here
593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
594 "Invalid test #%d in member_recv_join_decision()\n", test);
600 * Test: origin receives join request
603 origin_recv_join_request (void *cls,
604 const struct GNUNET_CRYPTO_EcdsaPublicKey *mem_key,
605 const struct GNUNET_MessageHeader *join_msg,
606 struct GNUNET_MULTICAST_JoinHandle *jh)
608 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
609 "Test #%u: origin_recv_join_request()\n", test);
611 GNUNET_assert (0 == memcmp (mem_key, &member_pub_key, sizeof (member_pub_key)));
612 GNUNET_assert (join_msg->size == join_req->size);
613 GNUNET_assert (join_msg->type == join_req->type);
614 GNUNET_assert (0 == memcmp (join_msg, join_req, ntohs (join_req->size)));
616 char data[] = "here's the decision";
617 uint8_t data_size = strlen (data) + 1;
618 join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
619 join_resp->size = htons (sizeof (join_resp) + data_size);
620 join_resp->type = htons (456);
621 GNUNET_memcpy (&join_resp[1], data, data_size);
625 case TEST_MEMBER_JOIN_REFUSE:
626 // Test 3 starts here
627 GNUNET_MULTICAST_join_decision (jh, GNUNET_NO, 0, NULL, join_resp);
630 case TEST_MEMBER_JOIN_ADMIT:
632 GNUNET_MULTICAST_join_decision (jh, GNUNET_YES, 1, &this_peer, join_resp);
636 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
637 "Invalid test #%d in origin_recv_join_request()\n", test);
644 * Test: member joins multicast group
650 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
651 "Test #%u: member_join()\n", test);
653 member_key = GNUNET_CRYPTO_ecdsa_key_create ();
654 GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
656 if (NULL != join_req)
657 GNUNET_free (join_req);
659 char data[] = "let me in!";
660 uint8_t data_size = strlen (data) + 1;
661 join_req = GNUNET_malloc (sizeof (join_req) + data_size);
662 join_req->size = htons (sizeof (join_req) + data_size);
663 join_req->type = htons (123);
664 GNUNET_memcpy (&join_req[1], data, data_size);
666 member = GNUNET_MULTICAST_member_join (cfg, &group_pub_key, member_key,
667 &this_peer, 1, &this_peer, join_req,
668 member_recv_join_request,
669 member_recv_join_decision,
670 member_recv_replay_frag,
671 member_recv_replay_msg,
677 * Test: Start a multicast group as origin
682 test = TEST_ORIGIN_START;
683 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
684 "Test #%u: origin_start()\n", test);
686 group_key = GNUNET_CRYPTO_eddsa_key_create ();
687 GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
689 origin = GNUNET_MULTICAST_origin_start (cfg, group_key, 0,
690 origin_recv_join_request,
691 origin_recv_replay_frag,
692 origin_recv_replay_msg,
696 // Test 2 starts here
697 member_join (TEST_MEMBER_JOIN_REFUSE);
702 * Main function of the test, run from scheduler.
705 * @param cfg configuration we use (also to connect to Multicast service)
706 * @param peer handle to access more of the peer (not used)
709 #if DEBUG_TEST_MULTICAST
713 const struct GNUNET_CONFIGURATION_Handle *c)
716 const struct GNUNET_CONFIGURATION_Handle *c,
717 struct GNUNET_TESTING_Peer *peer)
721 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
723 GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer);
725 // Test 1 starts here
731 main (int argc, char *argv[])
734 #if DEBUG_TEST_MULTICAST
735 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
736 GNUNET_GETOPT_OPTION_END
738 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-multicast",
739 "test-multicast [options]",
743 if (0 != GNUNET_TESTING_peer_run ("test-multicast", "test_multicast.conf", &run, NULL))
749 /* end of test_multicast.c */