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 it
6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your 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 * Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file multicast/test_multicast.c
21 * @brief Tests for the Multicast API.
22 * @author Gabor X Toth
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_common.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_multicast_service.h"
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37 * Return value from 'main'.
42 * Handle for task for timeout termination.
44 static struct GNUNET_SCHEDULER_Task * end_badly_task;
46 static const struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_PeerIdentity this_peer;
50 struct GNUNET_MULTICAST_Origin *origin;
51 struct GNUNET_MULTICAST_Member *member;
53 struct GNUNET_CRYPTO_EddsaPrivateKey *group_key;
54 struct GNUNET_CRYPTO_EddsaPublicKey group_pub_key;
56 struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key;
57 struct GNUNET_CRYPTO_EcdsaPublicKey member_pub_key;
59 struct TransmitClosure {
60 struct GNUNET_MULTICAST_OriginTransmitHandle *orig_tmit;
61 struct GNUNET_MULTICAST_MemberTransmitHandle *mem_tmit;
63 uint8_t data_delay[16];
69 struct OriginClosure {
70 uint8_t msgs_expected;
74 struct MemberClosure {
75 uint8_t msgs_expected;
79 struct GNUNET_MessageHeader *join_req, *join_resp;
84 TEST_ORIGIN_START = 1,
85 TEST_MEMBER_JOIN_REFUSE = 2,
86 TEST_MEMBER_JOIN_ADMIT = 3,
87 TEST_ORIGIN_TO_ALL = 4,
88 TEST_ORIGIN_TO_ALL_RECV = 5,
89 TEST_MEMBER_TO_ORIGIN = 6,
90 TEST_MEMBER_REPLAY_ERROR = 7,
91 TEST_MEMBER_REPLAY_OK = 8,
93 TEST_ORIGIN_STOP = 10,
96 uint64_t replay_fragment_id;
97 uint64_t replay_flags;
104 * Clean up all resources used.
111 GNUNET_MULTICAST_member_part (member, NULL, NULL);
116 GNUNET_MULTICAST_origin_stop (origin, NULL, NULL);
123 * Terminate the test case (failure).
128 end_badly (void *cls)
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
137 * Terminate the test case (success).
142 end_normally (void *cls)
146 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Test PASSED.\n");
151 * Finish the test case (successfully).
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
158 if (end_badly_task != NULL)
160 GNUNET_SCHEDULER_cancel (end_badly_task);
161 end_badly_task = NULL;
163 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
164 &end_normally, NULL);
169 tmit_resume (void *cls)
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
172 struct TransmitClosure *tmit = cls;
173 if (NULL != tmit->orig_tmit)
174 GNUNET_MULTICAST_origin_to_all_resume (tmit->orig_tmit);
175 else if (NULL != tmit->mem_tmit)
176 GNUNET_MULTICAST_member_to_origin_resume (tmit->mem_tmit);
181 tmit_notify (void *cls, size_t *data_size, void *data)
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Test #%u: origin_tmit_notify()\n", test);
185 struct TransmitClosure *tmit = cls;
187 if (0 == tmit->data_count)
193 uint16_t size = strlen (tmit->data[tmit->n]);
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "Transmit notify data: %u bytes available, processing fragment %u/%u (size %u).\n",
196 (unsigned int) *data_size,
200 if (*data_size < size)
204 return GNUNET_SYSERR;
207 if (GNUNET_YES != tmit->paused && 0 < tmit->data_delay[tmit->n])
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission paused.\n");
210 tmit->paused = GNUNET_YES;
211 GNUNET_SCHEDULER_add_delayed (
212 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
213 tmit->data_delay[tmit->n]),
218 tmit->paused = GNUNET_NO;
221 GNUNET_memcpy (data, tmit->data[tmit->n], size);
223 return ++tmit->n < tmit->data_count ? GNUNET_NO : GNUNET_YES;
228 member_recv_join_request (void *cls,
229 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
230 const struct GNUNET_MessageHeader *join_msg,
231 struct GNUNET_MULTICAST_JoinHandle *jh)
233 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
234 "Test #%u: member_recv_join_request()\n", test);
239 origin_stopped (void *cls)
241 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
242 "Test #%u: origin_stopped()\n", test);
248 schedule_origin_stop (void *cls)
250 test = TEST_ORIGIN_STOP;
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252 "Test #%u: origin_stop()\n", test);
253 GNUNET_MULTICAST_origin_stop (origin, origin_stopped, NULL);
259 member_parted (void *cls)
261 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
262 "Test #%u: member_parted()\n", test);
267 case TEST_MEMBER_JOIN_REFUSE:
268 // Test 3 starts here
269 member_join (TEST_MEMBER_JOIN_ADMIT);
272 case TEST_MEMBER_PART:
273 GNUNET_SCHEDULER_add_now (&schedule_origin_stop, NULL);
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
278 "Invalid test #%d in member_parted()\n", test);
285 schedule_member_part (void *cls)
287 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
288 "Test #%u: schedule_member_part()\n", test);
289 GNUNET_MULTICAST_member_part (member, member_parted, NULL);
296 test = TEST_MEMBER_PART;
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
298 "Test #%u: member_part()\n", test);
299 // Test 10 starts here
300 GNUNET_SCHEDULER_add_now (&schedule_member_part, NULL);
307 // Execution of test 8 here
308 test = TEST_MEMBER_REPLAY_OK;
309 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
310 "Test #%u: member_replay_ok()\n", test);
311 replay_fragment_id = 1;
312 replay_flags = 1 | 1<<11;
313 GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
319 member_replay_error ()
321 test = TEST_MEMBER_REPLAY_ERROR;
322 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
323 "Test #%u: member_replay_error()\n", test);
324 replay_fragment_id = 1234;
325 replay_flags = 11 | 1<<11;
326 GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
332 origin_recv_replay_msg (void *cls,
333 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
335 uint64_t fragment_offset,
337 struct GNUNET_MULTICAST_ReplayHandle *rh)
339 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
340 "Test #%u: origin_recv_replay_msg()\n", test);
346 member_recv_replay_msg (void *cls,
347 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
349 uint64_t fragment_offset,
351 struct GNUNET_MULTICAST_ReplayHandle *rh)
353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354 "Test #%u: member_recv_replay_msg()\n", test);
360 origin_recv_replay_frag (void *cls,
361 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
362 uint64_t fragment_id,
364 struct GNUNET_MULTICAST_ReplayHandle *rh)
366 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
367 "Test #%u: origin_recv_replay_frag()"
368 " - fragment_id=%" PRIu64 " flags=%" PRIu64 "\n",
369 test, fragment_id, flags);
370 GNUNET_assert (replay_fragment_id == fragment_id && replay_flags == flags);
373 case TEST_MEMBER_REPLAY_ERROR:
374 // Test 8 starts here
375 GNUNET_MULTICAST_replay_response (rh, NULL, GNUNET_SYSERR);
379 case TEST_MEMBER_REPLAY_OK:
381 struct GNUNET_MULTICAST_MessageHeader mmsg = {
383 .type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE),
384 .size = htons (sizeof (mmsg)),
386 .fragment_id = GNUNET_htonll (1),
387 .message_id = GNUNET_htonll (1),
388 .fragment_offset = 0,
389 .group_generation = GNUNET_htonll (1),
393 member_cls.msgs_expected = 1;
394 GNUNET_MULTICAST_replay_response (rh, &mmsg.header, GNUNET_MULTICAST_REC_OK);
395 GNUNET_MULTICAST_replay_response_end (rh);
400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
401 "Invalid test #%d in origin_recv_replay_frag()\n", test);
408 member_recv_replay_frag (void *cls,
409 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
410 uint64_t fragment_id,
412 struct GNUNET_MULTICAST_ReplayHandle *rh)
414 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
415 "Test #%u: member_recv_replay_frag()\n", test);
421 origin_recv_request (void *cls,
422 const struct GNUNET_MULTICAST_RequestHeader *req)
424 struct OriginClosure *ocls = cls;
425 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
426 "Test #%u: origin_recv_request()\n", test);
427 if (++ocls->n != ocls->msgs_expected)
430 GNUNET_assert (0 == memcmp (&req->member_pub_key,
431 &member_pub_key, sizeof (member_pub_key)));
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Test #%u: verify message content, take first 3 bytes: %.3s\n",
435 test, (char *)&req[1]);
436 GNUNET_assert (0 == memcmp (&req[1], "abc", 3));
438 // Test 7 starts here
439 member_replay_error ();
446 test = TEST_MEMBER_TO_ORIGIN;
447 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
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;
472 // Test 5 starts here after message has been received from origin
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Test #%u: member_recv_message() %u/%u\n",
476 (unsigned int) (mcls->n + 1),
477 mcls->msgs_expected);
478 if (++mcls->n != mcls->msgs_expected)
481 // FIXME: check message content
485 case TEST_ORIGIN_TO_ALL:
486 test = TEST_ORIGIN_TO_ALL_RECV;
489 case TEST_ORIGIN_TO_ALL_RECV:
490 // Test 6 starts here
494 case TEST_MEMBER_REPLAY_OK:
495 // Test 9 starts here
496 GNUNET_assert (replay_fragment_id == GNUNET_ntohll (msg->fragment_id));
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 "Invalid test #%d in origin_recv_message()\n", test);
509 origin_recv_message (void *cls,
510 const struct GNUNET_MULTICAST_MessageHeader *msg)
512 struct OriginClosure *ocls = cls;
513 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
514 "Test #%u: origin_recv_message() %u/%u\n",
515 test, ocls->n + 1, ocls->msgs_expected);
516 if (++ocls->n != ocls->msgs_expected)
519 // FIXME: check message content
523 case TEST_ORIGIN_TO_ALL:
524 // Prepare to execute test 5
525 test = TEST_ORIGIN_TO_ALL_RECV;
528 case TEST_ORIGIN_TO_ALL_RECV:
529 // Test 6 starts here
534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
535 "Invalid test #%d in origin_recv_message()\n", test);
544 test = TEST_ORIGIN_TO_ALL;
545 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
546 "Test #%u: origin_to_all()\n", test);
548 struct TransmitClosure *tmit = &tmit_cls;
549 *tmit = (struct TransmitClosure) {};
550 tmit->data[0] = "ABC DEF";
551 tmit->data[1] = GNUNET_malloc (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD + 1);
553 for (i = 0; i < GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD; i++)
554 tmit->data[1][i] = (0 == i % 10000) ? '0' + i / 10000 : '_';
555 tmit->data[2] = "GHI JKL MNO";
556 tmit->data_delay[2] = 2;
557 tmit->data[3] = "PQR STUW XYZ";
558 tmit->data_count = 4;
560 origin_cls.n = member_cls.n = 0;
561 origin_cls.msgs_expected = member_cls.msgs_expected = tmit->data_count;
563 tmit->orig_tmit = GNUNET_MULTICAST_origin_to_all (origin, 1, 1,
569 member_recv_join_decision (void *cls,
571 const struct GNUNET_PeerIdentity *peer,
572 uint16_t relay_count,
573 const struct GNUNET_PeerIdentity *relays,
574 const struct GNUNET_MessageHeader *join_msg)
576 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
577 "Test #%u: member_recv_join_decision() - is_admitted: %d\n",
580 GNUNET_assert (join_msg->size == join_resp->size);
581 GNUNET_assert (join_msg->type == join_resp->type);
582 GNUNET_assert (0 == memcmp (join_msg, join_resp, ntohs (join_resp->size)));
586 case TEST_MEMBER_JOIN_REFUSE:
587 GNUNET_assert (0 == relay_count);
588 // Test 3 starts here
589 GNUNET_SCHEDULER_add_now (&schedule_member_part, NULL);
592 case TEST_MEMBER_JOIN_ADMIT:
593 GNUNET_assert (1 == relay_count);
594 GNUNET_assert (0 == memcmp (relays, &this_peer, sizeof (this_peer)));
595 // Test 4 starts here
600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
601 "Invalid test #%d in member_recv_join_decision()\n", test);
607 * Test: origin receives join request
610 origin_recv_join_request (void *cls,
611 const struct GNUNET_CRYPTO_EcdsaPublicKey *mem_key,
612 const struct GNUNET_MessageHeader *join_msg,
613 struct GNUNET_MULTICAST_JoinHandle *jh)
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
616 "Test #%u: origin_recv_join_request()\n", test);
618 GNUNET_assert (0 == memcmp (mem_key, &member_pub_key, sizeof (member_pub_key)));
619 GNUNET_assert (join_msg->size == join_req->size);
620 GNUNET_assert (join_msg->type == join_req->type);
621 GNUNET_assert (0 == memcmp (join_msg, join_req, ntohs (join_req->size)));
623 char data[] = "here's the decision";
624 uint8_t data_size = strlen (data) + 1;
625 join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
626 join_resp->size = htons (sizeof (join_resp) + data_size);
627 join_resp->type = htons (456);
628 GNUNET_memcpy (&join_resp[1], data, data_size);
632 case TEST_MEMBER_JOIN_REFUSE:
633 // Test 3 starts here
634 GNUNET_MULTICAST_join_decision (jh, GNUNET_NO, 0, NULL, join_resp);
637 case TEST_MEMBER_JOIN_ADMIT:
639 GNUNET_MULTICAST_join_decision (jh, GNUNET_YES, 1, &this_peer, join_resp);
643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
644 "Invalid test #%d in origin_recv_join_request()\n", test);
651 * Test: member joins multicast group
657 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
658 "Test #%u: member_join()\n", test);
660 member_key = GNUNET_CRYPTO_ecdsa_key_create ();
661 GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
663 if (NULL != join_req)
664 GNUNET_free (join_req);
666 char data[] = "let me in!";
667 uint8_t data_size = strlen (data) + 1;
668 join_req = GNUNET_malloc (sizeof (join_req) + data_size);
669 join_req->size = htons (sizeof (join_req) + data_size);
670 join_req->type = htons (123);
671 GNUNET_memcpy (&join_req[1], data, data_size);
673 member = GNUNET_MULTICAST_member_join (cfg, &group_pub_key, member_key,
674 &this_peer, 1, &this_peer, join_req,
675 member_recv_join_request,
676 member_recv_join_decision,
677 member_recv_replay_frag,
678 member_recv_replay_msg,
684 * Test: Start a multicast group as origin
689 test = TEST_ORIGIN_START;
690 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
691 "Test #%u: origin_start()\n", test);
693 group_key = GNUNET_CRYPTO_eddsa_key_create ();
694 GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
696 origin = GNUNET_MULTICAST_origin_start (cfg, group_key, 0,
697 origin_recv_join_request,
698 origin_recv_replay_frag,
699 origin_recv_replay_msg,
703 // Test 2 starts here
704 member_join (TEST_MEMBER_JOIN_REFUSE);
709 * Main function of the test, run from scheduler.
712 * @param cfg configuration we use (also to connect to Multicast service)
713 * @param peer handle to access more of the peer (not used)
716 #if DEBUG_TEST_MULTICAST
720 const struct GNUNET_CONFIGURATION_Handle *c)
723 const struct GNUNET_CONFIGURATION_Handle *c,
724 struct GNUNET_TESTING_Peer *peer)
728 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
730 GNUNET_CRYPTO_get_peer_identity (cfg, &this_peer);
732 // Test 1 starts here
738 main (int argc, char *argv[])
741 #if DEBUG_TEST_MULTICAST
742 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
743 GNUNET_GETOPT_OPTION_END
745 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-multicast",
746 "test-multicast [options]",
750 if (0 != GNUNET_TESTING_peer_run ("test-multicast", "test_multicast.conf", &run, NULL))
756 /* end of test_multicast.c */