2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/test_testbed_api_3peers_3controllers.c
23 * @brief testcases for the testbed api: 3 peers are configured, started and
24 * connected together. Each peer resides on its own controller.
25 * @author Sree Harsha Totakura
30 * The testing architecture is:
35 * C1 is the master controller and C2, C3 are slave controllers. C2 links to C3
40 #include "gnunet_util_lib.h"
41 #include "gnunet_testing_lib-new.h"
42 #include "gnunet_testbed_service.h"
46 * Generic logging shortcut
48 #define LOG(kind,...) \
49 GNUNET_log (kind, __VA_ARGS__)
52 * Relative time seconds shorthand
54 #define TIME_REL_SECS(sec) \
55 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
66 struct GNUNET_TESTBED_Peer *peer;
69 * Operations involving this peer
71 struct GNUNET_TESTBED_Operation *operation;
74 * set to GNUNET_YES when peer is started
82 static struct GNUNET_TESTBED_Host *host;
85 * The controller process of one controller
87 static struct GNUNET_TESTBED_ControllerProc *cp1;
92 static struct GNUNET_TESTBED_Host *neighbour1;
95 * Another neighbouring host
97 static struct GNUNET_TESTBED_Host *neighbour2;
100 * Handle for neighbour registration
102 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
105 * The controller handle of one controller
107 static struct GNUNET_TESTBED_Controller *controller1;
112 static struct PeerContext peer1;
117 static struct PeerContext peer2;
122 static struct PeerContext peer3;
125 * Handle to starting configuration
127 static struct GNUNET_CONFIGURATION_Handle *cfg;
130 * Handle to controller2 configuration, used to establish lateral link from
133 static struct GNUNET_CONFIGURATION_Handle *cfg2;
136 * Handle to operations involving both peers
138 static struct GNUNET_TESTBED_Operation *common_operation;
141 * Abort task identifier
143 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
146 * Delayed connect job identifier
148 static GNUNET_SCHEDULER_TaskIdentifier delayed_connect_task;
151 * Different stages in testing
162 * Controller 1 has started
177 * Controller 2 has started
192 * Controller 3 has started
207 * peer1 and peer2 are connected
212 * Linking controller B to C
217 * peer2 and peer3 are connected
222 * Peers are connected once again (this should not fail as they are already connected)
232 * Final success stage
240 static enum Stage result;
246 * @param tc the task context
249 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
252 GNUNET_SCHEDULER_cancel (abort_task);
253 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
254 if (NULL != reg_handle)
255 GNUNET_TESTBED_cancel_registration (reg_handle);
256 if (NULL != controller1)
257 GNUNET_TESTBED_controller_disconnect (controller1);
258 if (NULL != controller2)
259 GNUNET_TESTBED_controller_disconnect (controller2);
260 GNUNET_CONFIGURATION_destroy (cfg);
262 GNUNET_CONFIGURATION_destroy (cfg2);
264 GNUNET_TESTBED_controller_stop (cp1);
266 GNUNET_TESTBED_host_destroy (host);
267 if (NULL != neighbour1)
268 GNUNET_TESTBED_host_destroy (neighbour1);
269 if (NULL != neighbour2)
270 GNUNET_TESTBED_host_destroy (neighbour2);
275 * abort task to run on test timed out
278 * @param tc the task context
281 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
283 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
284 abort_task = GNUNET_SCHEDULER_NO_TASK;
285 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
286 do_shutdown (cls, tc);
291 * Callback to be called when an operation is completed
293 * @param cls the callback closure from functions generating an operation
294 * @param op the operation that has been finished
295 * @param emsg error message in case the operation has failed; will be NULL if
296 * operation has executed successfully.
299 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
303 * task for delaying a connect
306 * @param tc the task context
309 do_delayed_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
311 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
312 GNUNET_assert (NULL == common_operation);
313 common_operation = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL,
314 peer1.peer, peer2.peer);
319 * Callback to be called when an operation is completed
321 * @param cls the callback closure from functions generating an operation
322 * @param op the operation that has been finished
323 * @param emsg error message in case the operation has failed; will be NULL if
324 * operation has executed successfully.
327 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
329 GNUNET_assert (common_operation == op);
333 GNUNET_assert (NULL == peer1.operation);
334 GNUNET_assert (NULL == peer2.operation);
335 GNUNET_assert (NULL != common_operation);
336 GNUNET_TESTBED_operation_done (common_operation);
337 common_operation = NULL;
338 result = PEERS_1_2_CONNECTED;
339 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
340 /* delayed_connect_task = */
341 /* GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), */
342 /* &do_delayed_connect, NULL); */
343 common_operation = GNUNET_TESTBED_controller_link (controller,
345 case PEERS_CONNECTED:
346 GNUNET_assert (NULL == peer1.operation);
347 GNUNET_assert (NULL == peer2.operation);
348 GNUNET_assert (NULL != common_operation);
349 GNUNET_TESTBED_operation_done (common_operation);
350 common_operation = NULL;
351 result = PEERS_CONNECTED_2;
352 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
353 peer1.operation = GNUNET_TESTBED_peer_stop (peer1.peer, NULL, NULL);
354 peer2.operation = GNUNET_TESTBED_peer_stop (peer2.peer, NULL, NULL);
355 peer3.operation = GNUNET_TESTBED_peer_stop (peer3.peer, NULL, NULL);
364 * Functions of this signature are called when a peer has been successfully
368 * @param peer the handle for the created peer; NULL on any error during
370 * @param emsg NULL if peer is not NULL; else MAY contain the error description
373 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
378 GNUNET_assert (NULL != peer1.operation);
379 GNUNET_assert (NULL != peer);
380 GNUNET_assert (NULL == peer1.peer);
382 GNUNET_TESTBED_operation_done (peer1.operation);
383 result = PEER1_CREATED;
384 peer1.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
387 GNUNET_assert (NULL != peer2.operation);
388 GNUNET_assert (NULL != peer);
389 GNUNET_assert (NULL == peer2.peer);
391 GNUNET_TESTBED_operation_done (peer2.operation);
392 result = PEER2_CREATED;
393 peer2.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
396 GNUNET_assert (NULL != peer3.operation);
397 GNUNET_assert (NULL != peer);
398 GNUNET_assert (NULL == peer3.peer);
400 GNUNET_TESTBED_operation_done (peer3.operation);
401 result = PEER3_CREATED;
402 peer3.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
411 * Callback to signal successfull startup of the controller process
413 * @param cls the closure from GNUNET_TESTBED_controller_start()
414 * @param cfg the configuration with which the controller has been started;
415 * NULL if status is not GNUNET_OK
416 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
417 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
420 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status);
424 * Signature of the event handler function called by the
425 * respective event controller.
428 * @param event information about the event
431 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
435 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
436 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
437 GNUNET_assert (NULL == event->details.operation_finished.emsg);
438 GNUNET_assert (NULL == event->details.operation_finished.generic);
442 if (event->details.operation_finished.operation == peer1.operation)
444 GNUNET_TESTBED_operation_done (peer1.operation);
445 peer1.operation = NULL;
448 else if (event->details.operation_finished.operation == peer2.operation)
450 GNUNET_TESTBED_operation_done (peer2.operation);
451 peer2.operation = NULL;
454 else if (event->details.operation_finished.operation == peer3.operation)
456 GNUNET_TESTBED_operation_done (peer3.operation);
457 peer3.operation = NULL;
462 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
465 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
469 GNUNET_assert (NULL != common_operation);
470 GNUNET_TESTBED_operation_done (common_operation);
471 common_operation = NULL;
472 result = CONTROLLER2_UP;
474 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
475 &peer_create_cb, NULL);
476 GNUNET_assert (NULL != peer2.operation);
479 GNUNET_assert (NULL != common_operation);
480 GNUNET_TESTBED_operation_done (common_operation);
481 common_operation = NULL;
482 result = CONTROLLER3_UP;
484 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
485 &peer_create_cb, NULL);
486 GNUNET_assert (NULL != peer3.operation);
492 case GNUNET_TESTBED_ET_PEER_START:
496 GNUNET_assert (event->details.peer_start.host == host);
497 peer1.is_running = GNUNET_YES;
498 GNUNET_TESTBED_operation_done (peer1.operation);
499 peer1.operation = NULL;
500 result = PEER1_STARTED;
502 GNUNET_TESTBED_controller_link (controller1, neighbour1, NULL, cfg,
506 GNUNET_assert (event->details.peer_start.host == neighbour1);
507 peer2.is_running = GNUNET_YES;
508 GNUNET_TESTBED_operation_done (peer2.operation);
509 peer2.operation = NULL;
510 result = PEER2_STARTED;
511 GNUNET_assert (NULL == common_operation);
513 GNUNET_TESTBED_controller_link (controller1, neighbour2, NULL, cfg,
515 GNUNET_assert (NULL != common_operation);
518 GNUNET_assert (event->details.peer_start.host == neighbour2);
519 peer3.is_running = GNUNET_YES;
520 GNUNET_TESTBED_operation_done (peer3.operation);
521 peer3.operation = NULL;
522 result = PEER3_STARTED;
524 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
531 case GNUNET_TESTBED_ET_PEER_STOP:
532 GNUNET_assert (PEERS_CONNECTED_2 == result);
533 if (event->details.peer_stop.peer == peer1.peer)
535 peer1.is_running = GNUNET_NO;
536 GNUNET_TESTBED_operation_done (peer1.operation);
538 else if (event->details.peer_stop.peer == peer2.peer)
540 peer2.is_running = GNUNET_NO;
541 GNUNET_TESTBED_operation_done (peer2.operation);
543 else if (event->details.peer_stop.peer == peer3.peer)
545 peer3.is_running = GNUNET_NO;
546 GNUNET_TESTBED_operation_done (peer3.operation);
550 if ((GNUNET_NO == peer1.is_running) &&
551 (GNUNET_NO == peer2.is_running) &&
552 (GNUNET_NO == peer3.is_running))
554 result = PEERS_STOPPED;
555 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
556 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
557 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
560 case GNUNET_TESTBED_ET_CONNECT:
561 GNUNET_assert (NULL == peer1.operation);
562 GNUNET_assert (NULL == peer2.operation);
563 GNUNET_assert (NULL == peer3.operation);
567 GNUNET_assert (NULL != common_operation);
568 GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer) &&
569 (event->details.peer_connect.peer2 == peer2.peer));
571 case PEERS_CONNECTED:
572 GNUNET_assert (NULL != common_operation);
573 GNUNET_assert ((event->details.peer_connect.peer1 == peer2.peer) &&
574 (event->details.peer_connect.peer2 == peer3.peer));
587 * Callback which will be called to after a host registration succeeded or failed
589 * @param cls the host which has been registered
590 * @param emsg the error message; NULL if host registration is successful
593 registration_comp (void *cls, const char *emsg)
596 if (cls == neighbour1)
598 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
599 GNUNET_assert (NULL != neighbour2);
601 GNUNET_TESTBED_register_host (controller1, neighbour2, ®istration_comp,
603 GNUNET_assert (NULL != reg_handle);
606 GNUNET_assert (cls == neighbour2);
608 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
610 GNUNET_assert (NULL != peer1.operation);
615 * Callback to signal successfull startup of the controller process
617 * @param cls the closure from GNUNET_TESTBED_controller_start()
618 * @param cfg the configuration with which the controller has been started;
619 * NULL if status is not GNUNET_OK
620 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
621 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
624 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
628 GNUNET_assert (GNUNET_OK == status);
630 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
631 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
632 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
633 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
638 GNUNET_TESTBED_controller_connect (config, host, event_mask, &controller_cb,
640 GNUNET_assert (NULL != controller1);
641 result = CONTROLLER1_UP;
642 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
643 GNUNET_assert (NULL != neighbour1);
645 GNUNET_TESTBED_register_host (controller1, neighbour1, ®istration_comp,
647 GNUNET_assert (NULL != reg_handle);
660 * @param args arguments passed to GNUNET_PROGRAM_run
661 * @param cfgfile the path to configuration file
662 * @param cfg the configuration file handle
665 run (void *cls, char *const *args, const char *cfgfile,
666 const struct GNUNET_CONFIGURATION_Handle *config)
668 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
669 GNUNET_assert (NULL != host);
670 cfg = GNUNET_CONFIGURATION_dup (config);
671 cp1 = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
674 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
675 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
681 * Function to check if password-less SSH logins to given ip work
683 * @param host_str numeric representation of the host's ip
684 * @return GNUNET_YES if password-less SSH login to the given host works;
688 check_ssh (char *host_str)
690 char *const remote_args[] = {
691 "ssh", "-o", "BatchMode=yes", "-o", "CheckHostIP=no", "-q",
692 host_str, "echo", "SSH", "works", NULL
694 struct GNUNET_OS_Process *auxp;
695 enum GNUNET_OS_ProcessStatusType type;
700 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
701 NULL, "ssh", remote_args);
702 GNUNET_assert (NULL != auxp);
705 ret = GNUNET_OS_process_status (auxp, &type, &code);
706 GNUNET_assert (GNUNET_SYSERR != ret);
709 while (GNUNET_NO == ret);
710 (void) GNUNET_OS_process_wait (auxp);
711 GNUNET_OS_process_destroy (auxp);
712 return (0 != code) ? GNUNET_NO : GNUNET_YES;
720 main (int argc, char **argv)
722 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
723 "-c", "test_testbed_api.conf",
726 struct GNUNET_GETOPT_CommandLineOption options[] = {
727 GNUNET_GETOPT_OPTION_END
731 if (GNUNET_YES != check_ssh ("127.0.0.1"))
735 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
736 "test_testbed_api_3peers_3controllers", "nohelp",
737 options, &run, NULL);
738 if ((GNUNET_OK != ret) || (SUCCESS != result))
744 "Unable to run the test as this system is not configured "
745 "to use password less SSH logins to localhost.\n"
746 "Marking test as successful\n");
750 /* end of test_testbed_api_3peers_3controllers.c */