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;
58 * Global result for testcase.
64 * Function run on CTRL-C or shutdown (i.e. success/timeout/etc.).
68 shutdown_task (void *cls)
72 GNUNET_TESTBED_operation_done (op0);
77 GNUNET_TESTBED_operation_done (op1);
82 GNUNET_TESTBED_operation_done (pi_op0);
87 GNUNET_TESTBED_operation_done (pi_op1);
90 if (NULL != timeout_tid)
92 GNUNET_SCHEDULER_cancel (timeout_tid);
99 timeout_task (void *cls)
101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103 result = GNUNET_SYSERR;
104 GNUNET_SCHEDULER_shutdown ();
109 member_join_request (void *cls,
110 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
111 const struct GNUNET_MessageHeader *join_msg,
112 struct GNUNET_MULTICAST_JoinHandle *jh)
114 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
115 "Member sent a join request.\n");
119 int notify (void *cls,
124 char text[] = "ping";
125 *data_size = strlen(text)+1;
126 GNUNET_memcpy(data, text, *data_size);
128 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
129 "Member sents message to origin: %s\n", text);
136 member_join_decision (void *cls,
138 const struct GNUNET_PeerIdentity *peer,
139 uint16_t relay_count,
140 const struct GNUNET_PeerIdentity *relays,
141 const struct GNUNET_MessageHeader *join_msg)
143 struct GNUNET_MULTICAST_MemberTransmitHandle *req;
145 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
146 "Member received a decision from origin: %s\n", (GNUNET_YES == is_admitted)?"accepted":"rejected");
148 if (GNUNET_YES == is_admitted)
150 req = GNUNET_MULTICAST_member_to_origin (member,
159 member_message (void *cls,
160 const struct GNUNET_MULTICAST_MessageHeader *msg)
162 if (0 != strncmp ("pong", (char *)&msg[1], 4)) {
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "member did not receive pong\n");
164 result = GNUNET_SYSERR;
165 GNUNET_SCHEDULER_shutdown ();
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169 "member receives: %s\n", (char *)&msg[1]);
171 // Testcase ends here.
173 GNUNET_SCHEDULER_shutdown ();
177 origin_join_request (void *cls,
178 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
179 const struct GNUNET_MessageHeader *join_msg,
180 struct GNUNET_MULTICAST_JoinHandle *jh)
182 struct GNUNET_MessageHeader *join_resp;
184 uint8_t data_size = ntohs (join_msg->size);
186 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
187 "origin got a join request...\n");
188 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
189 "origin receives: '%s'\n", (char *)&join_msg[1]);
191 char data[] = "Come in!";
192 data_size = strlen (data) + 1;
193 join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
194 join_resp->size = htons (sizeof (join_resp) + data_size);
195 join_resp->type = htons (123);
196 GNUNET_memcpy (&join_resp[1], data, data_size);
198 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
199 "origin sends: '%s'\n", data);
201 GNUNET_MULTICAST_join_decision (jh,
211 origin_notify (void *cls,
215 char text[] = "pong";
216 *data_size = strlen(text)+1;
217 memcpy(data, text, *data_size);
219 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin sends (to all): %s\n", text);
226 origin_request (void *cls,
227 const struct GNUNET_MULTICAST_RequestHeader *req)
229 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin receives: %s\n", (char *)&req[1]);
231 if (0 != strncmp ("ping", (char *)&req[1], 4)) {
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "origin didn't reveice a correct request");
235 GNUNET_MULTICAST_origin_to_all (origin,
243 origin_message (void *cls,
244 const struct GNUNET_MULTICAST_MessageHeader *msg)
246 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin message msg\n");
251 service_connect1 (void *cls,
252 struct GNUNET_TESTBED_Operation *op,
259 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to multicast service of member\n");
261 result = GNUNET_SYSERR;
265 multicast_da1 (void *cls,
268 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
269 "Member parting from multicast group\n");
271 GNUNET_MULTICAST_member_part (member, NULL, NULL);
276 multicast_ca1 (void *cls,
277 const struct GNUNET_CONFIGURATION_Handle *cfg)
279 struct GNUNET_MessageHeader *join_msg;
282 member_key = GNUNET_CRYPTO_ecdsa_key_create ();
283 GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
285 char data[] = "Hi, can I enter?";
286 uint8_t data_size = strlen (data) + 1;
287 join_msg = GNUNET_malloc (sizeof (join_msg) + data_size);
288 join_msg->size = htons (sizeof (join_msg) + data_size);
289 join_msg->type = htons (123);
290 GNUNET_memcpy (&join_msg[1], data, data_size);
292 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
293 "Members tries to join multicast group\n");
295 return GNUNET_MULTICAST_member_join (cfg,
301 join_msg, /* join message */
303 member_join_decision,
304 NULL, /* no test for member_replay_frag */
305 NULL, /* no test for member_replay_msg */
312 peer_information_cb (void *cls,
313 struct GNUNET_TESTBED_Operation *op,
314 const struct GNUNET_TESTBED_PeerInformation *pinfo,
317 int i = (int) (long) cls;
320 result = GNUNET_SYSERR;
321 GNUNET_SCHEDULER_shutdown ();
324 peer_id[i] = pinfo->result.id;
326 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
327 "Got peer information of %s (%s)\n", (0==i)?"origin":"member" ,GNUNET_i2s(pinfo->result.id));
329 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
330 "Create member peer\n");
333 /* connect to multicast service of member */
334 op1 = GNUNET_TESTBED_service_connect (NULL, /* Closure for operation */
335 peers[1], /* The peer whose service to connect to */
336 "multicast", /* The name of the service */
337 service_connect1, /* callback to call after a handle to service
339 NULL, /* closure for the above callback */
340 multicast_ca1, /* callback to call with peer's configuration;
341 this should open the needed service connection */
342 multicast_da1, /* callback to be called when closing the
343 opened service connection */
344 NULL); /* closure for the above two callbacks */
349 * Test logic of peer "0" being origin starts here.
351 * @param cls closure, for the example: NULL
352 * @param op should be equal to "dht_op"
353 * @param ca_result result of the connect operation, the
354 * connection to the DHT service
355 * @param emsg error message, if testbed somehow failed to
356 * connect to the DHT.
359 service_connect0 (void *cls,
360 struct GNUNET_TESTBED_Operation *op,
366 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
367 "Connected to multicast service of origin\n");
369 // Get GNUnet identity of origin
370 pi_op0 = GNUNET_TESTBED_peer_get_information (peers[0],
371 GNUNET_TESTBED_PIT_IDENTITY,
374 // Get GNUnet identity of member
375 pi_op1 = GNUNET_TESTBED_peer_get_information (peers[1],
376 GNUNET_TESTBED_PIT_IDENTITY,
380 /* Connection to service successful. Here we'd usually do something with
383 //GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
389 * Function run when service multicast has started and is providing us
390 * with a configuration file.
393 multicast_ca0 (void *cls,
394 const struct GNUNET_CONFIGURATION_Handle *cfg)
396 group_key = GNUNET_CRYPTO_eddsa_key_create ();
397 GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
399 return GNUNET_MULTICAST_origin_start (cfg,
403 NULL, /* no test for origin_replay_frag */
404 NULL, /* no test for origin_replay_msg */
411 multicast_da0 (void *cls,
414 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
415 "Origin closes multicast group\n");
417 GNUNET_MULTICAST_origin_stop (origin, NULL, NULL);
422 * Main function inovked from TESTBED once all of the
423 * peers are up and running. This one then connects
424 * just to the multicast service of peer 0 and 1.
425 * Peer 0 is going to be origin.
426 * Peer 1 is going to be one member.
427 * Origin will start a multicast group and the member will try to join it.
428 * After that we execute some multicast test.
431 * @param h the run handle
432 * @param peers started peers for the test
433 * @param num_peers size of the 'peers' array
434 * @param links_succeeded number of links between peers that were created
435 * @param links_failed number of links testbed was unable to establish
438 testbed_master (void *cls,
439 struct GNUNET_TESTBED_RunHandle *h,
440 unsigned int num_peers,
441 struct GNUNET_TESTBED_Peer **p,
442 unsigned int links_succeeded,
443 unsigned int links_failed)
445 /* Testbed is ready with peers running and connected in a pre-defined overlay
447 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
448 "Connected to testbed_master()\n");
452 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
453 "Create origin peer\n");
454 op0 = GNUNET_TESTBED_service_connect (NULL, /* Closure for operation */
455 peers[0], /* The peer whose service to connect to */
456 "multicast", /* The name of the service */
457 service_connect0, /* callback to call after a handle to service
459 NULL, /* closure for the above callback */
460 multicast_ca0, /* callback to call with peer's configuration;
461 this should open the needed service connection */
462 multicast_da0, /* callback to be called when closing the
463 opened service connection */
464 NULL); /* closure for the above two callbacks */
466 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); /* Schedule a new task on shutdown */
468 /* Schedule the shutdown task with a delay of a few Seconds */
469 timeout_tid = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 50),
470 &timeout_task, NULL);
475 main (int argc, char *argv[])
479 result = GNUNET_SYSERR;
480 ret = GNUNET_TESTBED_test_run
481 ("test-multicast-multipeer", /* test case name */
482 "test_multicast.conf", /* template configuration */
483 NUM_PEERS, /* number of peers to start */
484 0LL, /* Event mask - set to 0 for no event notifications */
485 NULL, /* Controller event callback */
486 NULL, /* Closure for controller event callback */
487 testbed_master, /* continuation callback to be called when testbed setup is complete */
488 NULL); /* Closure for the test_master callback */
489 if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
494 /* end of test_multicast_multipeer.c */