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_multipeers.c
23 * @brief Tests for the Multicast API with multiple peers.
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_common.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testbed_service.h"
34 #include "gnunet_multicast_service.h"
38 static struct GNUNET_TESTBED_Operation *op0;
39 static struct GNUNET_TESTBED_Operation *op1;
40 static struct GNUNET_TESTBED_Operation *pi_op0;
41 static struct GNUNET_TESTBED_Operation *pi_op1;
43 static struct GNUNET_TESTBED_Peer **peers;
44 const struct GNUNET_PeerIdentity *peer_id[2];
46 static struct GNUNET_SCHEDULER_Task *timeout_tid;
48 static struct GNUNET_MULTICAST_Origin *origin;
49 static struct GNUNET_MULTICAST_Member *member;
51 struct GNUNET_CRYPTO_EddsaPrivateKey *group_key;
52 struct GNUNET_CRYPTO_EddsaPublicKey group_pub_key;
54 struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key;
55 struct GNUNET_CRYPTO_EcdsaPublicKey member_pub_key;
61 TEST_ORIGIN_START = 1,
67 * Global result for testcase.
73 * Function run on CTRL-C or shutdown (i.e. success/timeout/etc.).
77 shutdown_task (void *cls)
81 GNUNET_TESTBED_operation_done (op0);
86 GNUNET_TESTBED_operation_done (op1);
91 GNUNET_TESTBED_operation_done (pi_op0);
96 GNUNET_TESTBED_operation_done (pi_op1);
99 if (NULL != timeout_tid)
101 GNUNET_SCHEDULER_cancel (timeout_tid);
108 timeout_task (void *cls)
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 result = GNUNET_SYSERR;
113 GNUNET_SCHEDULER_shutdown ();
118 member_join_request (void *cls,
119 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
120 const struct GNUNET_MessageHeader *join_msg,
121 struct GNUNET_MULTICAST_JoinHandle *jh)
123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
124 "Member sent a join request.\n");
128 int notify (void *cls,
133 char text[] = "ping";
134 *data_size = strlen(text)+1;
135 GNUNET_memcpy(data, text, *data_size);
137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
138 "Member sents message to origin: %s\n", text);
145 member_join_decision (void *cls,
147 const struct GNUNET_PeerIdentity *peer,
148 uint16_t relay_count,
149 const struct GNUNET_PeerIdentity *relays,
150 const struct GNUNET_MessageHeader *join_msg)
152 struct GNUNET_MULTICAST_MemberTransmitHandle *req;
154 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
155 "Member received a decision from origin: %s\n", (GNUNET_YES == is_admitted)?"accepted":"rejected");
157 if (GNUNET_YES == is_admitted)
159 req = GNUNET_MULTICAST_member_to_origin (member,
168 member_replay_frag ()
170 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
171 "member replay frag...\n");
177 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
178 "member replay msg...\n");
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
185 "member message...\n");
187 // FIXME: not finished here
189 GNUNET_SCHEDULER_shutdown ();
193 origin_join_request (void *cls,
194 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
195 const struct GNUNET_MessageHeader *join_msg,
196 struct GNUNET_MULTICAST_JoinHandle *jh)
198 struct GNUNET_MessageHeader *join_resp;
200 uint8_t data_size = ntohs (join_msg->size);
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
203 "origin got a join request...\n");
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205 "origin receives: '%s'\n", (char *)&join_msg[1]);
207 char data[] = "Come in!";
208 data_size = strlen (data) + 1;
209 join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
210 join_resp->size = htons (sizeof (join_resp) + data_size);
211 join_resp->type = htons (123);
212 GNUNET_memcpy (&join_resp[1], data, data_size);
214 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
215 "origin sends: '%s'\n", data);
217 GNUNET_MULTICAST_join_decision (jh,
227 origin_replay_frag (void *cls,
228 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
229 uint64_t fragment_id,
231 struct GNUNET_MULTICAST_ReplayHandle *rh)
233 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin replay fraq msg\n");
237 origin_replay_msg (void *cls,
238 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
240 uint64_t fragment_offset,
242 struct GNUNET_MULTICAST_ReplayHandle *rh)
245 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin replay msg\n");
250 origin_notify (void *cls,
254 char text[] = "pong";
255 *data_size = strlen(text)+1;
256 memcpy(data, text, *data_size);
258 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin sends (to all): %s\n", text);
265 origin_request (void *cls,
266 const struct GNUNET_MULTICAST_RequestHeader *req)
268 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin receives: %s\n", (char *)&req[1]);
270 if (0 != strncmp ("ping", (char *)&req[1], 4)) {
271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "origin didn't reveice a correct request");
274 GNUNET_MULTICAST_origin_to_all (origin,
282 origin_message (void *cls,
283 const struct GNUNET_MULTICAST_MessageHeader *msg)
285 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin message msg\n");
290 service_connect1 (void *cls,
291 struct GNUNET_TESTBED_Operation *op,
298 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to multicast service of member\n");
300 result = GNUNET_SYSERR;
304 multicast_da1 (void *cls,
307 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
308 "Member parting from multicast group\n");
310 GNUNET_MULTICAST_member_part (member, NULL, NULL);
315 multicast_ca1 (void *cls,
316 const struct GNUNET_CONFIGURATION_Handle *cfg)
318 struct GNUNET_MessageHeader *join_msg;
321 member_key = GNUNET_CRYPTO_ecdsa_key_create ();
322 GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
324 char data[] = "Hi, can I enter?";
325 uint8_t data_size = strlen (data) + 1;
326 join_msg = GNUNET_malloc (sizeof (join_msg) + data_size);
327 join_msg->size = htons (sizeof (join_msg) + data_size);
328 join_msg->type = htons (123);
329 GNUNET_memcpy (&join_msg[1], data, data_size);
331 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
332 "Members tries to join multicast group\n");
334 return GNUNET_MULTICAST_member_join (cfg,
340 join_msg, /* join message */
342 member_join_decision,
351 peer_information_cb (void *cls,
352 struct GNUNET_TESTBED_Operation *op,
353 const struct GNUNET_TESTBED_PeerInformation *pinfo,
356 int i = (int) (long) cls;
359 result = GNUNET_SYSERR;
360 GNUNET_SCHEDULER_shutdown ();
363 peer_id[i] = pinfo->result.id;
365 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
366 "Got peer information of %s (%s)\n", (0==i)?"origin":"member" ,GNUNET_i2s(pinfo->result.id));
368 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
369 "Create member peer\n");
372 /* connect to multicast service of member */
373 op1 = GNUNET_TESTBED_service_connect (NULL, /* Closure for operation */
374 peers[1], /* The peer whose service to connect to */
375 "multicast", /* The name of the service */
376 service_connect1, /* callback to call after a handle to service
378 NULL, /* closure for the above callback */
379 multicast_ca1, /* callback to call with peer's configuration;
380 this should open the needed service connection */
381 multicast_da1, /* callback to be called when closing the
382 opened service connection */
383 NULL); /* closure for the above two callbacks */
388 * Test logic of peer "0" being origin starts here.
390 * @param cls closure, for the example: NULL
391 * @param op should be equal to "dht_op"
392 * @param ca_result result of the connect operation, the
393 * connection to the DHT service
394 * @param emsg error message, if testbed somehow failed to
395 * connect to the DHT.
398 service_connect0 (void *cls,
399 struct GNUNET_TESTBED_Operation *op,
405 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
406 "Connected to multicast service of origin\n");
408 // Get GNUnet identity of origin
409 pi_op0 = GNUNET_TESTBED_peer_get_information (peers[0],
410 GNUNET_TESTBED_PIT_IDENTITY,
413 // Get GNUnet identity of member
414 pi_op1 = GNUNET_TESTBED_peer_get_information (peers[1],
415 GNUNET_TESTBED_PIT_IDENTITY,
419 /* Connection to service successful. Here we'd usually do something with
422 //GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
428 * Function run when service multicast has started and is providing us
429 * with a configuration file.
432 multicast_ca0 (void *cls,
433 const struct GNUNET_CONFIGURATION_Handle *cfg)
435 group_key = GNUNET_CRYPTO_eddsa_key_create ();
436 GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
438 return GNUNET_MULTICAST_origin_start (cfg,
450 multicast_da0 (void *cls,
453 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
454 "Origin closes multicast group\n");
456 GNUNET_MULTICAST_origin_stop (origin, NULL, NULL);
461 * Main function inovked from TESTBED once all of the
462 * peers are up and running. This one then connects
463 * just to the multicast service of peer 0 and 1.
464 * Peer 0 is going to be origin.
465 * Peer 1 is going to be one member.
466 * Origin will start a multicast group and the member will try to join it.
467 * After that we execute some multicast test.
470 * @param h the run handle
471 * @param peers started peers for the test
472 * @param num_peers size of the 'peers' array
473 * @param links_succeeded number of links between peers that were created
474 * @param links_failed number of links testbed was unable to establish
477 testbed_master (void *cls,
478 struct GNUNET_TESTBED_RunHandle *h,
479 unsigned int num_peers,
480 struct GNUNET_TESTBED_Peer **p,
481 unsigned int links_succeeded,
482 unsigned int links_failed)
484 /* Testbed is ready with peers running and connected in a pre-defined overlay
486 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
487 "Connected to testbed_master()\n");
491 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
492 "Create origin peer\n");
493 op0 = GNUNET_TESTBED_service_connect (NULL, /* Closure for operation */
494 peers[0], /* The peer whose service to connect to */
495 "multicast", /* The name of the service */
496 service_connect0, /* callback to call after a handle to service
498 NULL, /* closure for the above callback */
499 multicast_ca0, /* callback to call with peer's configuration;
500 this should open the needed service connection */
501 multicast_da0, /* callback to be called when closing the
502 opened service connection */
503 NULL); /* closure for the above two callbacks */
505 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); /* Schedule a new task on shutdown */
507 /* Schedule the shutdown task with a delay of a few Seconds */
508 timeout_tid = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 48),
509 &timeout_task, NULL);
514 main (int argc, char *argv[])
518 result = GNUNET_SYSERR;
519 ret = GNUNET_TESTBED_test_run
520 ("test-multicast-multipeer", /* test case name */
521 "test_multicast.conf", /* template configuration */
522 NUM_PEERS, /* number of peers to start */
523 0LL, /* Event mask - set to 0 for no event notifications */
524 NULL, /* Controller event callback */
525 NULL, /* Closure for controller event callback */
526 testbed_master, /* continuation callback to be called when testbed setup is complete */
527 NULL); /* Closure for the test_master callback */
528 if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
533 /* end of test_multicast_multipeer.c */