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_2peers.c
23 * @brief Tests for the Multicast API with two peers doing the ping
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_common.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_testbed_service.h"
35 #include "gnunet_multicast_service.h"
39 static struct GNUNET_TESTBED_Operation *op0;
40 static struct GNUNET_TESTBED_Operation *op1;
41 static struct GNUNET_TESTBED_Operation *pi_op0;
42 static struct GNUNET_TESTBED_Operation *pi_op1;
44 static struct GNUNET_TESTBED_Peer **peers;
45 const struct GNUNET_PeerIdentity *peer_id[2];
47 static struct GNUNET_SCHEDULER_Task *timeout_tid;
49 static struct GNUNET_MULTICAST_Origin *origin;
50 static struct GNUNET_MULTICAST_Member *member;
52 struct GNUNET_CRYPTO_EddsaPrivateKey *group_key;
53 struct GNUNET_CRYPTO_EddsaPublicKey group_pub_key;
55 struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key;
56 struct GNUNET_CRYPTO_EcdsaPublicKey member_pub_key;
59 * Global result for testcase.
65 * Function run on CTRL-C or shutdown (i.e. success/timeout/etc.).
69 shutdown_task (void *cls)
73 GNUNET_TESTBED_operation_done (op0);
78 GNUNET_TESTBED_operation_done (op1);
83 GNUNET_TESTBED_operation_done (pi_op0);
88 GNUNET_TESTBED_operation_done (pi_op1);
91 if (NULL != timeout_tid)
93 GNUNET_SCHEDULER_cancel (timeout_tid);
100 timeout_task (void *cls)
102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
104 result = GNUNET_SYSERR;
105 GNUNET_SCHEDULER_shutdown ();
110 member_join_request (void *cls,
111 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
112 const struct GNUNET_MessageHeader *join_msg,
113 struct GNUNET_MULTICAST_JoinHandle *jh)
115 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
116 "Member sent a join request.\n");
127 char text[] = "ping";
128 *data_size = strlen(text)+1;
129 GNUNET_memcpy(data, text, *data_size);
131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
132 "Member sents message to origin: %s\n", text);
139 member_join_decision (void *cls,
141 const struct GNUNET_PeerIdentity *peer,
142 uint16_t relay_count,
143 const struct GNUNET_PeerIdentity *relays,
144 const struct GNUNET_MessageHeader *join_msg)
146 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
147 "Member received a decision from origin: %s\n",
148 (GNUNET_YES == is_admitted)
152 if (GNUNET_YES == is_admitted)
154 struct GNUNET_MULTICAST_MemberTransmitHandle *req;
156 // FIXME: move to MQ-style API!
157 req = GNUNET_MULTICAST_member_to_origin (member,
166 member_message (void *cls,
167 const struct GNUNET_MULTICAST_MessageHeader *msg)
169 if (0 != strncmp ("pong", (char *)&msg[1], 4))
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "member did not receive pong\n");
172 result = GNUNET_SYSERR;
173 GNUNET_SCHEDULER_shutdown ();
176 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
177 "member receives: %s\n", (char *)&msg[1]);
179 // Testcase ends here.
181 GNUNET_SCHEDULER_shutdown ();
185 origin_join_request (void *cls,
186 const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
187 const struct GNUNET_MessageHeader *join_msg,
188 struct GNUNET_MULTICAST_JoinHandle *jh)
190 struct GNUNET_MessageHeader *join_resp;
192 uint8_t data_size = ntohs (join_msg->size);
194 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
195 "origin got a join request...\n");
196 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
197 "origin receives: '%s'\n", (char *)&join_msg[1]);
199 const char data[] = "Come in!";
200 data_size = strlen (data) + 1;
201 join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
202 join_resp->size = htons (sizeof (join_resp) + data_size);
203 join_resp->type = htons (123);
204 GNUNET_memcpy (&join_resp[1], data, data_size);
206 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
207 "origin sends: '%s'\n", data);
209 GNUNET_MULTICAST_join_decision (jh,
214 GNUNET_free (join_resp);
219 origin_notify (void *cls,
223 char text[] = "pong";
224 *data_size = strlen(text)+1;
225 memcpy(data, text, *data_size);
227 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin sends (to all): %s\n", text);
234 origin_request (void *cls,
235 const struct GNUNET_MULTICAST_RequestHeader *req)
237 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin receives: %s\n", (char *)&req[1]);
239 if (0 != strncmp ("ping", (char *)&req[1], 4))
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "origin didn't reveice a correct request");
242 GNUNET_MULTICAST_origin_to_all (origin,
250 origin_message (void *cls,
251 const struct GNUNET_MULTICAST_MessageHeader *msg)
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin message msg\n");
258 service_connect1 (void *cls,
259 struct GNUNET_TESTBED_Operation *op,
267 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to multicast service of member\n");
271 result = GNUNET_SYSERR;
272 GNUNET_SCHEDULER_shutdown ();
277 multicast_da1 (void *cls,
280 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
281 "Member parting from multicast group\n");
283 GNUNET_MULTICAST_member_part (member, NULL, NULL);
288 multicast_ca1 (void *cls,
289 const struct GNUNET_CONFIGURATION_Handle *cfg)
291 struct GNUNET_MessageHeader *join_msg;
295 member_key = GNUNET_CRYPTO_ecdsa_key_create ();
296 GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
298 char data[] = "Hi, can I enter?";
299 uint8_t data_size = strlen (data) + 1;
300 join_msg = GNUNET_malloc (sizeof (join_msg) + data_size);
301 join_msg->size = htons (sizeof (join_msg) + data_size);
302 join_msg->type = htons (123);
303 GNUNET_memcpy (&join_msg[1], data, data_size);
305 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
306 "Members tries to join multicast group\n");
308 ret = GNUNET_MULTICAST_member_join (cfg,
314 join_msg, /* join message */
316 member_join_decision,
317 NULL, /* no test for member_replay_frag */
318 NULL, /* no test for member_replay_msg */
321 GNUNET_free (join_msg);
327 peer_information_cb (void *cls,
328 struct GNUNET_TESTBED_Operation *op,
329 const struct GNUNET_TESTBED_PeerInformation *pinfo,
332 int i = (int) (long) cls;
336 result = GNUNET_SYSERR;
337 GNUNET_SCHEDULER_shutdown ();
340 peer_id[i] = pinfo->result.id;
342 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
343 "Got peer information of %s (%s)\n", (0==i)?"origin":"member" ,GNUNET_i2s(pinfo->result.id));
345 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
346 "Create member peer\n");
350 /* connect to multicast service of member */
351 op1 = GNUNET_TESTBED_service_connect (NULL, /* Closure for operation */
352 peers[1], /* The peer whose service to connect to */
353 "multicast", /* The name of the service */
354 service_connect1, /* callback to call after a handle to service
356 NULL, /* closure for the above callback */
357 multicast_ca1, /* callback to call with peer's configuration;
358 this should open the needed service connection */
359 multicast_da1, /* callback to be called when closing the
360 opened service connection */
361 NULL); /* closure for the above two callbacks */
366 * Test logic of peer "0" being origin starts here.
368 * @param cls closure, for the example: NULL
369 * @param op should be equal to "dht_op"
370 * @param ca_result result of the connect operation, the
371 * connection to the DHT service
372 * @param emsg error message, if testbed somehow failed to
373 * connect to the DHT.
376 service_connect0 (void *cls,
377 struct GNUNET_TESTBED_Operation *op,
383 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
384 "Connected to multicast service of origin\n");
386 // Get GNUnet identity of origin
387 pi_op0 = GNUNET_TESTBED_peer_get_information (peers[0],
388 GNUNET_TESTBED_PIT_IDENTITY,
391 // Get GNUnet identity of member
392 pi_op1 = GNUNET_TESTBED_peer_get_information (peers[1],
393 GNUNET_TESTBED_PIT_IDENTITY,
397 /* Connection to service successful. Here we'd usually do something with
400 //GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
406 * Function run when service multicast has started and is providing us
407 * with a configuration file.
410 multicast_ca0 (void *cls,
411 const struct GNUNET_CONFIGURATION_Handle *cfg)
413 group_key = GNUNET_CRYPTO_eddsa_key_create ();
414 GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
416 return GNUNET_MULTICAST_origin_start (cfg,
420 NULL, /* no test for origin_replay_frag */
421 NULL, /* no test for origin_replay_msg */
428 multicast_da0 (void *cls,
431 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
432 "Origin closes multicast group\n");
434 GNUNET_MULTICAST_origin_stop (origin, NULL, NULL);
439 * Main function inovked from TESTBED once all of the
440 * peers are up and running. This one then connects
441 * just to the multicast service of peer 0 and 1.
442 * Peer 0 is going to be origin.
443 * Peer 1 is going to be one member.
444 * Origin will start a multicast group and the member will try to join it.
445 * After that we execute some multicast test.
448 * @param h the run handle
449 * @param peers started peers for the test
450 * @param num_peers size of the 'peers' array
451 * @param links_succeeded number of links between peers that were created
452 * @param links_failed number of links testbed was unable to establish
455 testbed_master (void *cls,
456 struct GNUNET_TESTBED_RunHandle *h,
457 unsigned int num_peers,
458 struct GNUNET_TESTBED_Peer **p,
459 unsigned int links_succeeded,
460 unsigned int links_failed)
462 /* Testbed is ready with peers running and connected in a pre-defined overlay
464 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
465 "Connected to testbed_master()\n");
469 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
470 "Create origin peer\n");
471 op0 = GNUNET_TESTBED_service_connect (NULL, /* Closure for operation */
472 peers[0], /* The peer whose service to connect to */
473 "multicast", /* The name of the service */
474 service_connect0, /* callback to call after a handle to service
476 NULL, /* closure for the above callback */
477 multicast_ca0, /* callback to call with peer's configuration;
478 this should open the needed service connection */
479 multicast_da0, /* callback to be called when closing the
480 opened service connection */
481 NULL); /* closure for the above two callbacks */
483 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); /* Schedule a new task on shutdown */
485 /* Schedule the shutdown task with a delay of a few Seconds */
486 timeout_tid = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 50),
487 &timeout_task, NULL);
492 main (int argc, char *argv[])
496 result = GNUNET_SYSERR;
497 ret = GNUNET_TESTBED_test_run
498 ("test-multicast-multipeer", /* test case name */
499 "test_multicast.conf", /* template configuration */
500 NUM_PEERS, /* number of peers to start */
501 0LL, /* Event mask - set to 0 for no event notifications */
502 NULL, /* Controller event callback */
503 NULL, /* Closure for controller event callback */
504 testbed_master, /* continuation callback to be called when testbed setup is complete */
505 NULL); /* Closure for the test_master callback */
506 if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
511 /* end of test_multicast_multipeer.c */