2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file testbed/test_testbed_api_3peers_3controllers.c
18 * @brief testcases for the testbed api: 3 peers are configured, started and
19 * connected together. Each peer resides on its own controller.
20 * @author Sree Harsha Totakura
25 * The testing architecture is:
30 * A is the master controller and B, C are slave controllers. B links to C
32 * Peers are mapped to controllers in the following relations:
41 #include "gnunet_util_lib.h"
42 #include "gnunet_testing_lib.h"
43 #include "gnunet_testbed_service.h"
47 * Generic logging shortcut
49 #define LOG(kind,...) \
50 GNUNET_log (kind, __VA_ARGS__)
53 * Relative time seconds shorthand
55 #define TIME_REL_SECS(sec) \
56 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
67 struct GNUNET_TESTBED_Peer *peer;
70 * Operations involving this peer
72 struct GNUNET_TESTBED_Operation *operation;
75 * set to GNUNET_YES when peer is started
83 static struct GNUNET_TESTBED_Host *host;
86 * The controller process of one controller
88 static struct GNUNET_TESTBED_ControllerProc *cp1;
93 static struct GNUNET_TESTBED_Host *neighbour1;
96 * Another neighbouring host
98 static struct GNUNET_TESTBED_Host *neighbour2;
101 * Handle for neighbour registration
103 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
106 * The controller handle of one controller
108 static struct GNUNET_TESTBED_Controller *controller1;
113 static struct PeerContext peer1;
118 static struct PeerContext peer2;
123 static struct PeerContext peer3;
126 * Handle to starting configuration
128 static struct GNUNET_CONFIGURATION_Handle *cfg;
131 * Handle to slave controller C's configuration, used to establish lateral link from
134 static struct GNUNET_CONFIGURATION_Handle *cfg2;
137 * Handle to operations involving both peers
139 static struct GNUNET_TESTBED_Operation *common_operation;
142 * The handle for whether a host is habitable or not
144 struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
147 * Abort task identifier
149 static struct GNUNET_SCHEDULER_Task * abort_task;
152 * Delayed connect job identifier
154 static struct GNUNET_SCHEDULER_Task * 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
238 * Optional stage for marking test to be skipped
246 static enum Stage result;
254 do_shutdown (void *cls)
256 if (NULL != abort_task)
257 GNUNET_SCHEDULER_cancel (abort_task);
258 if (NULL != hc_handle)
259 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
260 GNUNET_assert (NULL == delayed_connect_task);
261 if (NULL != common_operation)
262 GNUNET_TESTBED_operation_done (common_operation);
263 if (NULL != reg_handle)
264 GNUNET_TESTBED_cancel_registration (reg_handle);
265 if (NULL != controller1)
266 GNUNET_TESTBED_controller_disconnect (controller1);
267 GNUNET_CONFIGURATION_destroy (cfg);
269 GNUNET_CONFIGURATION_destroy (cfg2);
271 GNUNET_TESTBED_controller_stop (cp1);
273 GNUNET_TESTBED_host_destroy (host);
274 if (NULL != neighbour1)
275 GNUNET_TESTBED_host_destroy (neighbour1);
276 if (NULL != neighbour2)
277 GNUNET_TESTBED_host_destroy (neighbour2);
282 * abort task to run on test timed out
289 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
291 if (NULL != delayed_connect_task)
293 GNUNET_SCHEDULER_cancel (delayed_connect_task);
294 delayed_connect_task = NULL;
303 if (NULL != abort_task)
304 GNUNET_SCHEDULER_cancel (abort_task);
305 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
310 * Callback to be called when an operation is completed
312 * @param cls the callback closure from functions generating an operation
313 * @param op the operation that has been finished
314 * @param emsg error message in case the operation has failed; will be NULL if
315 * operation has executed successfully.
318 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
322 * task for delaying a connect
327 do_delayed_connect (void *cls)
329 delayed_connect_task = NULL;
330 if (NULL != common_operation)
337 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
343 * Callback to be called when an operation is completed
345 * @param cls the callback closure from functions generating an operation
346 * @param op the operation that has been finished
347 * @param emsg error message in case the operation has failed; will be NULL if
348 * operation has executed successfully.
351 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
353 if (common_operation != op)
363 case PEERS_2_3_CONNECTED:
364 case PEERS_1_2_CONNECTED:
371 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
372 (NULL != peer3.operation))
382 * Functions of this signature are called when a peer has been successfully
386 * @param peer the handle for the created peer; NULL on any error during
388 * @param emsg NULL if peer is not NULL; else MAY contain the error description
391 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
396 if ((NULL == peer1.operation) || (NULL == peer) || (NULL != peer1.peer))
403 GNUNET_TESTBED_operation_done (peer1.operation);
404 result = PEER1_CREATED;
405 peer1.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
408 if ((NULL == peer2.operation) || (NULL == peer) || (NULL != peer2.peer))
415 GNUNET_TESTBED_operation_done (peer2.operation);
416 result = PEER2_CREATED;
417 peer2.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
420 if ((NULL == peer3.operation) || (NULL == peer) || (NULL != peer3.peer))
427 GNUNET_TESTBED_operation_done (peer3.operation);
428 result = PEER3_CREATED;
429 peer3.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
440 * Signature of the event handler function called by the
441 * respective event controller.
444 * @param event information about the event
447 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
451 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
452 if ((NULL != event->op_cls) ||
453 (NULL != event->details.operation_finished.emsg))
462 if (NULL != event->details.operation_finished.generic)
468 if (event->op == peer1.operation)
470 GNUNET_TESTBED_operation_done (peer1.operation);
471 peer1.operation = NULL;
474 else if (event->op == peer2.operation)
476 GNUNET_TESTBED_operation_done (peer2.operation);
477 peer2.operation = NULL;
480 else if (event->op == peer3.operation)
482 GNUNET_TESTBED_operation_done (peer3.operation);
483 peer3.operation = NULL;
492 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
495 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
499 if ((NULL != event->details.operation_finished.generic) ||
500 (NULL == common_operation))
506 GNUNET_TESTBED_operation_done (common_operation);
507 common_operation = NULL;
508 result = CONTROLLER2_UP;
510 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
511 &peer_create_cb, NULL);
512 if (NULL == peer2.operation)
520 if ((NULL != event->details.operation_finished.generic) ||
521 (NULL == common_operation))
527 GNUNET_TESTBED_operation_done (common_operation);
528 common_operation = NULL;
529 result = CONTROLLER3_UP;
531 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
532 &peer_create_cb, NULL);
533 if (NULL == peer3.operation)
546 case GNUNET_TESTBED_ET_PEER_START:
550 if (event->details.peer_start.host != host)
556 peer1.is_running = GNUNET_YES;
557 GNUNET_TESTBED_operation_done (peer1.operation);
558 peer1.operation = NULL;
559 result = PEER1_STARTED;
561 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL,
565 if (event->details.peer_start.host != neighbour1)
571 peer2.is_running = GNUNET_YES;
572 GNUNET_TESTBED_operation_done (peer2.operation);
573 peer2.operation = NULL;
574 result = PEER2_STARTED;
575 if (NULL != common_operation)
582 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL,
584 if (NULL == common_operation)
592 if (event->details.peer_start.host != neighbour2)
598 peer3.is_running = GNUNET_YES;
599 GNUNET_TESTBED_operation_done (peer3.operation);
600 peer3.operation = NULL;
601 result = PEER3_STARTED;
603 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
612 case GNUNET_TESTBED_ET_PEER_STOP:
613 if (PEERS_CONNECTED_2 != result)
619 if (event->details.peer_stop.peer == peer1.peer)
621 peer1.is_running = GNUNET_NO;
622 GNUNET_TESTBED_operation_done (peer1.operation);
624 else if (event->details.peer_stop.peer == peer2.peer)
626 peer2.is_running = GNUNET_NO;
627 GNUNET_TESTBED_operation_done (peer2.operation);
629 else if (event->details.peer_stop.peer == peer3.peer)
631 peer3.is_running = GNUNET_NO;
632 GNUNET_TESTBED_operation_done (peer3.operation);
640 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running) &&
641 (GNUNET_NO == peer3.is_running))
643 result = PEERS_STOPPED;
644 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
645 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
646 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
649 case GNUNET_TESTBED_ET_CONNECT:
650 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
651 (NULL != peer3.operation) || (NULL == common_operation))
660 if ((event->details.peer_connect.peer1 != peer2.peer) ||
661 (event->details.peer_connect.peer2 != peer1.peer))
667 GNUNET_TESTBED_operation_done (common_operation);
668 common_operation = NULL;
669 result = PEERS_1_2_CONNECTED;
670 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
672 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
675 case PEERS_1_2_CONNECTED:
676 if ((event->details.peer_connect.peer1 != peer2.peer) ||
677 (event->details.peer_connect.peer2 != peer3.peer))
683 GNUNET_TESTBED_operation_done (common_operation);
684 common_operation = NULL;
685 result = PEERS_2_3_CONNECTED;
686 delayed_connect_task =
687 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), &do_delayed_connect,
690 case PEERS_2_3_CONNECTED:
691 if ((event->details.peer_connect.peer1 != peer1.peer) ||
692 (event->details.peer_connect.peer2 != peer2.peer))
698 GNUNET_TESTBED_operation_done (common_operation);
699 common_operation = NULL;
700 result = PEERS_CONNECTED_2;
701 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
702 peer1.operation = GNUNET_TESTBED_peer_stop (NULL, peer1.peer, NULL, NULL);
703 peer2.operation = GNUNET_TESTBED_peer_stop (NULL, peer2.peer, NULL, NULL);
704 peer3.operation = GNUNET_TESTBED_peer_stop (NULL, peer3.peer, NULL, NULL);
721 * Callback which will be called to after a host registration succeeded or failed
723 * @param cls the host which has been registered
724 * @param emsg the error message; NULL if host registration is successful
727 registration_comp (void *cls, const char *emsg)
730 if (cls == neighbour1)
732 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
733 if (NULL == neighbour2)
740 GNUNET_TESTBED_register_host (controller1, neighbour2,
741 ®istration_comp, neighbour2);
742 if (NULL == reg_handle)
750 if (cls != neighbour2)
757 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
759 if (NULL == peer1.operation)
769 * Callback to signal successfull startup of the controller process
771 * @param cls the closure from GNUNET_TESTBED_controller_start()
772 * @param cfg the configuration with which the controller has been started;
773 * NULL if status is not GNUNET_OK
774 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
775 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
778 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
783 if (GNUNET_OK != status)
791 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
792 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
793 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
794 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
799 GNUNET_TESTBED_controller_connect (host, event_mask,
800 &controller_cb, NULL);
801 if (NULL == controller1)
807 result = CONTROLLER1_UP;
808 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
809 if (NULL == neighbour1)
816 GNUNET_TESTBED_register_host (controller1, neighbour1,
817 ®istration_comp, neighbour1);
818 if (NULL == reg_handle)
834 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
835 * inform whether the given host is habitable or not. The Handle returned by
836 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
839 * @param host the host whose status is being reported; will be NULL if the host
840 * given to GNUNET_TESTBED_is_host_habitable() is NULL
841 * @param status #GNUNET_YES if it is habitable; #GNUNET_NO if not
844 host_habitable_cb (void *cls,
845 const struct GNUNET_TESTBED_Host *_host,
849 if (GNUNET_NO == status)
852 "Unable to run the test as this system is not configured "
853 "to use password less SSH logins to localhost.\n"
855 GNUNET_SCHEDULER_cancel (abort_task);
857 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
862 GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb, NULL);
870 * @param args arguments passed to GNUNET_PROGRAM_run
871 * @param cfgfile the path to configuration file
872 * @param cfg the configuration file handle
875 run (void *cls, char *const *args, const char *cfgfile,
876 const struct GNUNET_CONFIGURATION_Handle *config)
878 cfg = GNUNET_CONFIGURATION_dup (config);
879 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
888 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
891 GNUNET_TESTBED_host_destroy (host);
894 "Unable to run the test as this system is not configured "
895 "to use password less SSH logins to localhost.\n"
901 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
902 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
911 main (int argc, char **argv)
913 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
914 "-c", "test_testbed_api.conf",
917 struct GNUNET_GETOPT_CommandLineOption options[] = {
918 GNUNET_GETOPT_OPTION_END
924 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
925 "test_testbed_api_3peers_3controllers", "nohelp",
926 options, &run, NULL);
927 if (GNUNET_OK != ret)
934 return 77; /* Mark test as skipped */
940 /* end of test_testbed_api_3peers_3controllers.c */