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 * 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-new.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 * Abort task identifier
149 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
152 * Delayed connect job identifier
154 static GNUNET_SCHEDULER_TaskIdentifier delayed_connect_task;
157 * Different stages in testing
168 * Controller 1 has started
183 * Controller 2 has started
198 * Controller 3 has started
213 * peer1 and peer2 are connected
218 * peer2 and peer3 are connected
223 * Peers are connected once again (this should not fail as they are already connected)
233 * Final success stage
241 static enum Stage result;
247 * @param tc the task context
250 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
252 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
253 GNUNET_SCHEDULER_cancel (abort_task);
254 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
255 if (NULL != reg_handle)
256 GNUNET_TESTBED_cancel_registration (reg_handle);
257 if (NULL != controller1)
258 GNUNET_TESTBED_controller_disconnect (controller1);
259 GNUNET_CONFIGURATION_destroy (cfg);
261 GNUNET_CONFIGURATION_destroy (cfg2);
263 GNUNET_TESTBED_controller_stop (cp1);
265 GNUNET_TESTBED_host_destroy (host);
266 if (NULL != neighbour1)
267 GNUNET_TESTBED_host_destroy (neighbour1);
268 if (NULL != neighbour2)
269 GNUNET_TESTBED_host_destroy (neighbour2);
274 * abort task to run on test timed out
277 * @param tc the task context
280 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
283 abort_task = GNUNET_SCHEDULER_NO_TASK;
284 if (GNUNET_SCHEDULER_NO_TASK != delayed_connect_task)
286 GNUNET_SCHEDULER_cancel (delayed_connect_task);
287 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
289 do_shutdown (cls, tc);
295 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
296 GNUNET_SCHEDULER_cancel (abort_task);
297 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
302 * Callback to be called when an operation is completed
304 * @param cls the callback closure from functions generating an operation
305 * @param op the operation that has been finished
306 * @param emsg error message in case the operation has failed; will be NULL if
307 * operation has executed successfully.
310 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
314 * task for delaying a connect
317 * @param tc the task context
320 do_delayed_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
322 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
323 if (NULL != common_operation)
329 common_operation = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL,
330 peer1.peer, peer2.peer);
335 * Callback to be called when an operation is completed
337 * @param cls the callback closure from functions generating an operation
338 * @param op the operation that has been finished
339 * @param emsg error message in case the operation has failed; will be NULL if
340 * operation has executed successfully.
343 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
345 if (common_operation != op)
354 if ((NULL != peer1.operation) ||
355 (NULL != peer2.operation) ||
356 (NULL == common_operation))
362 GNUNET_TESTBED_operation_done (common_operation);
363 common_operation = NULL;
364 result = PEERS_1_2_CONNECTED;
365 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
367 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
370 case PEERS_1_2_CONNECTED:
371 if (NULL == common_operation)
377 GNUNET_TESTBED_operation_done (common_operation);
378 common_operation = NULL;
379 result = PEERS_2_3_CONNECTED;
380 delayed_connect_task =
381 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3),
382 &do_delayed_connect, NULL);
384 case PEERS_2_3_CONNECTED:
385 if ((NULL != peer1.operation) ||
386 (NULL != peer2.operation) ||
387 (NULL == common_operation))
393 GNUNET_TESTBED_operation_done (common_operation);
394 common_operation = NULL;
395 result = PEERS_CONNECTED_2;
396 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
397 peer1.operation = GNUNET_TESTBED_peer_stop (peer1.peer, NULL, NULL);
398 peer2.operation = GNUNET_TESTBED_peer_stop (peer2.peer, NULL, NULL);
399 peer3.operation = GNUNET_TESTBED_peer_stop (peer3.peer, NULL, NULL);
410 * Functions of this signature are called when a peer has been successfully
414 * @param peer the handle for the created peer; NULL on any error during
416 * @param emsg NULL if peer is not NULL; else MAY contain the error description
419 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
424 if ((NULL == peer1.operation) ||
426 (NULL != peer1.peer))
433 GNUNET_TESTBED_operation_done (peer1.operation);
434 result = PEER1_CREATED;
435 peer1.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
438 if ((NULL == peer2.operation) ||
440 (NULL != peer2.peer))
447 GNUNET_TESTBED_operation_done (peer2.operation);
448 result = PEER2_CREATED;
449 peer2.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
452 if ((NULL == peer3.operation) ||
454 (NULL != peer3.peer))
461 GNUNET_TESTBED_operation_done (peer3.operation);
462 result = PEER3_CREATED;
463 peer3.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
474 * Callback to signal successfull startup of the controller process
476 * @param cls the closure from GNUNET_TESTBED_controller_start()
477 * @param cfg the configuration with which the controller has been started;
478 * NULL if status is not GNUNET_OK
479 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
480 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
483 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status);
487 * Signature of the event handler function called by the
488 * respective event controller.
491 * @param event information about the event
494 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
498 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
499 if ((NULL != event->details.operation_finished.op_cls)
500 || (NULL != event->details.operation_finished.emsg))
509 if (NULL != event->details.operation_finished.generic)
515 if (event->details.operation_finished.operation == peer1.operation)
517 GNUNET_TESTBED_operation_done (peer1.operation);
518 peer1.operation = NULL;
521 else if (event->details.operation_finished.operation == peer2.operation)
523 GNUNET_TESTBED_operation_done (peer2.operation);
524 peer2.operation = NULL;
527 else if (event->details.operation_finished.operation == peer3.operation)
529 GNUNET_TESTBED_operation_done (peer3.operation);
530 peer3.operation = NULL;
539 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
542 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
546 if ((NULL != event->details.operation_finished.generic) ||
547 (NULL == common_operation))
553 GNUNET_TESTBED_operation_done (common_operation);
554 common_operation = NULL;
555 result = CONTROLLER2_UP;
557 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
558 &peer_create_cb, NULL);
559 if (NULL == peer2.operation)
567 if ((NULL != event->details.operation_finished.generic) ||
568 (NULL == common_operation))
574 GNUNET_TESTBED_operation_done (common_operation);
575 common_operation = NULL;
576 result = CONTROLLER3_UP;
578 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
579 &peer_create_cb, NULL);
580 if (NULL == peer3.operation)
593 case GNUNET_TESTBED_ET_PEER_START:
597 if (event->details.peer_start.host != host)
603 peer1.is_running = GNUNET_YES;
604 GNUNET_TESTBED_operation_done (peer1.operation);
605 peer1.operation = NULL;
606 result = PEER1_STARTED;
608 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL, cfg,
612 if (event->details.peer_start.host != neighbour1)
618 peer2.is_running = GNUNET_YES;
619 GNUNET_TESTBED_operation_done (peer2.operation);
620 peer2.operation = NULL;
621 result = PEER2_STARTED;
622 if (NULL != common_operation)
629 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL, cfg,
631 if (NULL == common_operation)
639 if (event->details.peer_start.host != neighbour2)
645 peer3.is_running = GNUNET_YES;
646 GNUNET_TESTBED_operation_done (peer3.operation);
647 peer3.operation = NULL;
648 result = PEER3_STARTED;
650 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
659 case GNUNET_TESTBED_ET_PEER_STOP:
660 if (PEERS_CONNECTED_2 != result)
666 if (event->details.peer_stop.peer == peer1.peer)
668 peer1.is_running = GNUNET_NO;
669 GNUNET_TESTBED_operation_done (peer1.operation);
671 else if (event->details.peer_stop.peer == peer2.peer)
673 peer2.is_running = GNUNET_NO;
674 GNUNET_TESTBED_operation_done (peer2.operation);
676 else if (event->details.peer_stop.peer == peer3.peer)
678 peer3.is_running = GNUNET_NO;
679 GNUNET_TESTBED_operation_done (peer3.operation);
687 if ((GNUNET_NO == peer1.is_running) &&
688 (GNUNET_NO == peer2.is_running) &&
689 (GNUNET_NO == peer3.is_running))
691 result = PEERS_STOPPED;
692 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
693 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
694 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
697 case GNUNET_TESTBED_ET_CONNECT:
698 if ((NULL != peer1.operation) ||
699 (NULL != peer2.operation) ||
700 (NULL != peer3.operation))
709 if ((NULL == common_operation) ||
710 (event->details.peer_connect.peer1 != peer2.peer) ||
711 (event->details.peer_connect.peer2 != peer1.peer))
718 case PEERS_2_3_CONNECTED:
719 if ((NULL == common_operation) ||
720 (event->details.peer_connect.peer1 != peer1.peer) ||
721 (event->details.peer_connect.peer2 != peer2.peer))
728 case PEERS_1_2_CONNECTED:
729 if ((NULL == common_operation) ||
730 (event->details.peer_connect.peer1 != peer2.peer) ||
731 (event->details.peer_connect.peer2 != peer3.peer))
753 * Callback which will be called to after a host registration succeeded or failed
755 * @param cls the host which has been registered
756 * @param emsg the error message; NULL if host registration is successful
759 registration_comp (void *cls, const char *emsg)
762 if (cls == neighbour1)
764 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
765 if (NULL == neighbour2)
772 GNUNET_TESTBED_register_host (controller1, neighbour2, ®istration_comp,
774 if (NULL == reg_handle)
782 if (cls != neighbour2)
789 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
791 if (NULL == peer1.operation)
801 * Callback to signal successfull startup of the controller process
803 * @param cls the closure from GNUNET_TESTBED_controller_start()
804 * @param cfg the configuration with which the controller has been started;
805 * NULL if status is not GNUNET_OK
806 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
807 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
810 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
814 if (GNUNET_OK != status)
821 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
822 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
823 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
824 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
829 GNUNET_TESTBED_controller_connect (config, host, event_mask, &controller_cb,
831 if (NULL == controller1)
837 result = CONTROLLER1_UP;
838 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
839 if (NULL == neighbour1)
846 GNUNET_TESTBED_register_host (controller1, neighbour1, ®istration_comp,
848 if (NULL == reg_handle)
867 * @param args arguments passed to GNUNET_PROGRAM_run
868 * @param cfgfile the path to configuration file
869 * @param cfg the configuration file handle
872 run (void *cls, char *const *args, const char *cfgfile,
873 const struct GNUNET_CONFIGURATION_Handle *config)
875 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
882 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (host, config))
884 GNUNET_TESTBED_host_destroy (host);
887 "Unable to run the test as this system is not configured "
888 "to use password less SSH logins to localhost.\n"
889 "Marking test as successful\n");
893 cfg = GNUNET_CONFIGURATION_dup (config);
894 cp1 = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
897 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
898 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
907 main (int argc, char **argv)
909 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
910 "-c", "test_testbed_api.conf",
913 struct GNUNET_GETOPT_CommandLineOption options[] = {
914 GNUNET_GETOPT_OPTION_END
920 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
921 "test_testbed_api_3peers_3controllers", "nohelp",
922 options, &run, NULL);
923 if ((GNUNET_OK != ret) || (SUCCESS != result))
928 /* end of test_testbed_api_3peers_3controllers.c */