2 This file is part of GNUnet
3 (C) 2008--2013 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 * A is the master controller and B, C are slave controllers. B links to C
37 * Peers are mapped to controllers in the following relations:
46 #include "gnunet_util_lib.h"
47 #include "gnunet_testing_lib.h"
48 #include "gnunet_testbed_service.h"
52 * Generic logging shortcut
54 #define LOG(kind,...) \
55 GNUNET_log (kind, __VA_ARGS__)
58 * Relative time seconds shorthand
60 #define TIME_REL_SECS(sec) \
61 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
72 struct GNUNET_TESTBED_Peer *peer;
75 * Operations involving this peer
77 struct GNUNET_TESTBED_Operation *operation;
80 * set to GNUNET_YES when peer is started
88 static struct GNUNET_TESTBED_Host *host;
91 * The controller process of one controller
93 static struct GNUNET_TESTBED_ControllerProc *cp1;
98 static struct GNUNET_TESTBED_Host *neighbour1;
101 * Another neighbouring host
103 static struct GNUNET_TESTBED_Host *neighbour2;
106 * Handle for neighbour registration
108 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
111 * The controller handle of one controller
113 static struct GNUNET_TESTBED_Controller *controller1;
118 static struct PeerContext peer1;
123 static struct PeerContext peer2;
128 static struct PeerContext peer3;
131 * Handle to starting configuration
133 static struct GNUNET_CONFIGURATION_Handle *cfg;
136 * Handle to slave controller C's configuration, used to establish lateral link from
139 static struct GNUNET_CONFIGURATION_Handle *cfg2;
142 * Handle to operations involving both peers
144 static struct GNUNET_TESTBED_Operation *common_operation;
147 * The handle for whether a host is habitable or not
149 struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
152 * Abort task identifier
154 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
157 * Delayed connect job identifier
159 static GNUNET_SCHEDULER_TaskIdentifier delayed_connect_task;
162 * Different stages in testing
173 * Controller 1 has started
188 * Controller 2 has started
203 * Controller 3 has started
218 * peer1 and peer2 are connected
223 * peer2 and peer3 are connected
228 * Peers are connected once again (this should not fail as they are already connected)
238 * Final success stage
243 * Optional stage for marking test to be skipped
251 static enum Stage result;
257 * @param tc the task context
260 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
263 GNUNET_SCHEDULER_cancel (abort_task);
264 if (NULL != hc_handle)
265 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
266 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
267 if (NULL != common_operation)
268 GNUNET_TESTBED_operation_done (common_operation);
269 if (NULL != reg_handle)
270 GNUNET_TESTBED_cancel_registration (reg_handle);
271 if (NULL != controller1)
272 GNUNET_TESTBED_controller_disconnect (controller1);
273 GNUNET_CONFIGURATION_destroy (cfg);
275 GNUNET_CONFIGURATION_destroy (cfg2);
277 GNUNET_TESTBED_controller_stop (cp1);
279 GNUNET_TESTBED_host_destroy (host);
280 if (NULL != neighbour1)
281 GNUNET_TESTBED_host_destroy (neighbour1);
282 if (NULL != neighbour2)
283 GNUNET_TESTBED_host_destroy (neighbour2);
288 * abort task to run on test timed out
291 * @param tc the task context
294 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
296 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
297 abort_task = GNUNET_SCHEDULER_NO_TASK;
298 if (GNUNET_SCHEDULER_NO_TASK != delayed_connect_task)
300 GNUNET_SCHEDULER_cancel (delayed_connect_task);
301 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
303 do_shutdown (cls, tc);
309 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
310 GNUNET_SCHEDULER_cancel (abort_task);
311 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
316 * Callback to be called when an operation is completed
318 * @param cls the callback closure from functions generating an operation
319 * @param op the operation that has been finished
320 * @param emsg error message in case the operation has failed; will be NULL if
321 * operation has executed successfully.
324 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
328 * task for delaying a connect
331 * @param tc the task context
334 do_delayed_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
336 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
337 if (NULL != common_operation)
344 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
350 * Callback to be called when an operation is completed
352 * @param cls the callback closure from functions generating an operation
353 * @param op the operation that has been finished
354 * @param emsg error message in case the operation has failed; will be NULL if
355 * operation has executed successfully.
358 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
360 if (common_operation != op)
370 case PEERS_2_3_CONNECTED:
371 case PEERS_1_2_CONNECTED:
378 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
379 (NULL != peer3.operation))
389 * Functions of this signature are called when a peer has been successfully
393 * @param peer the handle for the created peer; NULL on any error during
395 * @param emsg NULL if peer is not NULL; else MAY contain the error description
398 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
403 if ((NULL == peer1.operation) || (NULL == peer) || (NULL != peer1.peer))
410 GNUNET_TESTBED_operation_done (peer1.operation);
411 result = PEER1_CREATED;
412 peer1.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
415 if ((NULL == peer2.operation) || (NULL == peer) || (NULL != peer2.peer))
422 GNUNET_TESTBED_operation_done (peer2.operation);
423 result = PEER2_CREATED;
424 peer2.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
427 if ((NULL == peer3.operation) || (NULL == peer) || (NULL != peer3.peer))
434 GNUNET_TESTBED_operation_done (peer3.operation);
435 result = PEER3_CREATED;
436 peer3.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
447 * Signature of the event handler function called by the
448 * respective event controller.
451 * @param event information about the event
454 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
458 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
459 if ((NULL != event->op_cls) ||
460 (NULL != event->details.operation_finished.emsg))
469 if (NULL != event->details.operation_finished.generic)
475 if (event->op == peer1.operation)
477 GNUNET_TESTBED_operation_done (peer1.operation);
478 peer1.operation = NULL;
481 else if (event->op == peer2.operation)
483 GNUNET_TESTBED_operation_done (peer2.operation);
484 peer2.operation = NULL;
487 else if (event->op == peer3.operation)
489 GNUNET_TESTBED_operation_done (peer3.operation);
490 peer3.operation = NULL;
499 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
502 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
506 if ((NULL != event->details.operation_finished.generic) ||
507 (NULL == common_operation))
513 GNUNET_TESTBED_operation_done (common_operation);
514 common_operation = NULL;
515 result = CONTROLLER2_UP;
517 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
518 &peer_create_cb, NULL);
519 if (NULL == peer2.operation)
527 if ((NULL != event->details.operation_finished.generic) ||
528 (NULL == common_operation))
534 GNUNET_TESTBED_operation_done (common_operation);
535 common_operation = NULL;
536 result = CONTROLLER3_UP;
538 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
539 &peer_create_cb, NULL);
540 if (NULL == peer3.operation)
553 case GNUNET_TESTBED_ET_PEER_START:
557 if (event->details.peer_start.host != host)
563 peer1.is_running = GNUNET_YES;
564 GNUNET_TESTBED_operation_done (peer1.operation);
565 peer1.operation = NULL;
566 result = PEER1_STARTED;
568 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL,
572 if (event->details.peer_start.host != neighbour1)
578 peer2.is_running = GNUNET_YES;
579 GNUNET_TESTBED_operation_done (peer2.operation);
580 peer2.operation = NULL;
581 result = PEER2_STARTED;
582 if (NULL != common_operation)
589 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL,
591 if (NULL == common_operation)
599 if (event->details.peer_start.host != neighbour2)
605 peer3.is_running = GNUNET_YES;
606 GNUNET_TESTBED_operation_done (peer3.operation);
607 peer3.operation = NULL;
608 result = PEER3_STARTED;
610 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
619 case GNUNET_TESTBED_ET_PEER_STOP:
620 if (PEERS_CONNECTED_2 != result)
626 if (event->details.peer_stop.peer == peer1.peer)
628 peer1.is_running = GNUNET_NO;
629 GNUNET_TESTBED_operation_done (peer1.operation);
631 else if (event->details.peer_stop.peer == peer2.peer)
633 peer2.is_running = GNUNET_NO;
634 GNUNET_TESTBED_operation_done (peer2.operation);
636 else if (event->details.peer_stop.peer == peer3.peer)
638 peer3.is_running = GNUNET_NO;
639 GNUNET_TESTBED_operation_done (peer3.operation);
647 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running) &&
648 (GNUNET_NO == peer3.is_running))
650 result = PEERS_STOPPED;
651 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
652 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
653 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
656 case GNUNET_TESTBED_ET_CONNECT:
657 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
658 (NULL != peer3.operation) || (NULL == common_operation))
667 if ((event->details.peer_connect.peer1 != peer2.peer) ||
668 (event->details.peer_connect.peer2 != peer1.peer))
674 GNUNET_TESTBED_operation_done (common_operation);
675 common_operation = NULL;
676 result = PEERS_1_2_CONNECTED;
677 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
679 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
682 case PEERS_1_2_CONNECTED:
683 if ((event->details.peer_connect.peer1 != peer2.peer) ||
684 (event->details.peer_connect.peer2 != peer3.peer))
690 GNUNET_TESTBED_operation_done (common_operation);
691 common_operation = NULL;
692 result = PEERS_2_3_CONNECTED;
693 delayed_connect_task =
694 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), &do_delayed_connect,
697 case PEERS_2_3_CONNECTED:
698 if ((event->details.peer_connect.peer1 != peer1.peer) ||
699 (event->details.peer_connect.peer2 != peer2.peer))
705 GNUNET_TESTBED_operation_done (common_operation);
706 common_operation = NULL;
707 result = PEERS_CONNECTED_2;
708 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
709 peer1.operation = GNUNET_TESTBED_peer_stop (NULL, peer1.peer, NULL, NULL);
710 peer2.operation = GNUNET_TESTBED_peer_stop (NULL, peer2.peer, NULL, NULL);
711 peer3.operation = GNUNET_TESTBED_peer_stop (NULL, peer3.peer, NULL, NULL);
728 * Callback which will be called to after a host registration succeeded or failed
730 * @param cls the host which has been registered
731 * @param emsg the error message; NULL if host registration is successful
734 registration_comp (void *cls, const char *emsg)
737 if (cls == neighbour1)
739 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
740 if (NULL == neighbour2)
747 GNUNET_TESTBED_register_host (controller1, neighbour2,
748 ®istration_comp, neighbour2);
749 if (NULL == reg_handle)
757 if (cls != neighbour2)
764 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
766 if (NULL == peer1.operation)
776 * Callback to signal successfull startup of the controller process
778 * @param cls the closure from GNUNET_TESTBED_controller_start()
779 * @param cfg the configuration with which the controller has been started;
780 * NULL if status is not GNUNET_OK
781 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
782 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
785 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
790 if (GNUNET_OK != status)
798 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
799 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
800 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
801 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
806 GNUNET_TESTBED_controller_connect (host, event_mask,
807 &controller_cb, NULL);
808 if (NULL == controller1)
814 result = CONTROLLER1_UP;
815 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
816 if (NULL == neighbour1)
823 GNUNET_TESTBED_register_host (controller1, neighbour1,
824 ®istration_comp, neighbour1);
825 if (NULL == reg_handle)
841 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
842 * inform whether the given host is habitable or not. The Handle returned by
843 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
846 * @param host the host whose status is being reported; will be NULL if the host
847 * given to GNUNET_TESTBED_is_host_habitable() is NULL
848 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
851 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *_host,
855 if (GNUNET_NO == status)
858 "Unable to run the test as this system is not configured "
859 "to use password less SSH logins to localhost.\n"
861 GNUNET_SCHEDULER_cancel (abort_task);
862 abort_task = GNUNET_SCHEDULER_NO_TASK;
863 (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
868 GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
876 * @param args arguments passed to GNUNET_PROGRAM_run
877 * @param cfgfile the path to configuration file
878 * @param cfg the configuration file handle
881 run (void *cls, char *const *args, const char *cfgfile,
882 const struct GNUNET_CONFIGURATION_Handle *config)
884 cfg = GNUNET_CONFIGURATION_dup (config);
885 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
894 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
897 GNUNET_TESTBED_host_destroy (host);
900 "Unable to run the test as this system is not configured "
901 "to use password less SSH logins to localhost.\n"
907 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
908 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
917 main (int argc, char **argv)
919 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
920 "-c", "test_testbed_api.conf",
923 struct GNUNET_GETOPT_CommandLineOption options[] = {
924 GNUNET_GETOPT_OPTION_END
930 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
931 "test_testbed_api_3peers_3controllers", "nohelp",
932 options, &run, NULL);
933 if (GNUNET_OK != ret)
940 return 77; /* Mark test as skipped */
946 /* end of test_testbed_api_3peers_3controllers.c */