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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 struct GNUNET_SCHEDULER_Task *abort_task;
157 * Delayed connect job identifier
159 static struct GNUNET_SCHEDULER_Task *delayed_connect_task;
162 * Different stages in testing
172 * Controller 1 has started
187 * Controller 2 has started
202 * Controller 3 has started
217 * peer1 and peer2 are connected
222 * peer2 and peer3 are connected
227 * Peers are connected once again (this should not fail as they are already connected)
237 * Final success stage
242 * Optional stage for marking test to be skipped
250 static enum Stage result;
258 do_shutdown (void *cls)
260 if (NULL != abort_task)
261 GNUNET_SCHEDULER_cancel (abort_task);
262 if (NULL != hc_handle)
263 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
264 GNUNET_assert (NULL == delayed_connect_task);
265 if (NULL != common_operation)
266 GNUNET_TESTBED_operation_done (common_operation);
267 if (NULL != reg_handle)
268 GNUNET_TESTBED_cancel_registration (reg_handle);
269 if (NULL != controller1)
270 GNUNET_TESTBED_controller_disconnect (controller1);
271 GNUNET_CONFIGURATION_destroy (cfg);
273 GNUNET_CONFIGURATION_destroy (cfg2);
275 GNUNET_TESTBED_controller_stop (cp1);
277 GNUNET_TESTBED_host_destroy (host);
278 if (NULL != neighbour1)
279 GNUNET_TESTBED_host_destroy (neighbour1);
280 if (NULL != neighbour2)
281 GNUNET_TESTBED_host_destroy (neighbour2);
286 * abort task to run on test timed out
293 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
295 if (NULL != delayed_connect_task)
297 GNUNET_SCHEDULER_cancel (delayed_connect_task);
298 delayed_connect_task = NULL;
307 if (NULL != abort_task)
308 GNUNET_SCHEDULER_cancel (abort_task);
309 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
314 * Callback to be called when an operation is completed
316 * @param cls the callback closure from functions generating an operation
317 * @param op the operation that has been finished
318 * @param emsg error message in case the operation has failed; will be NULL if
319 * operation has executed successfully.
322 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
326 * task for delaying a connect
331 do_delayed_connect (void *cls)
333 delayed_connect_task = NULL;
334 if (NULL != common_operation)
341 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
347 * Callback to be called when an operation is completed
349 * @param cls the callback closure from functions generating an operation
350 * @param op the operation that has been finished
351 * @param emsg error message in case the operation has failed; will be NULL if
352 * operation has executed successfully.
355 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
357 if (common_operation != op)
367 case PEERS_2_3_CONNECTED:
368 case PEERS_1_2_CONNECTED:
376 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
377 (NULL != peer3.operation))
387 * Functions of this signature are called when a peer has been successfully
391 * @param peer the handle for the created peer; NULL on any error during
393 * @param emsg NULL if peer is not NULL; else MAY contain the error description
396 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
401 if ((NULL == peer1.operation) || (NULL == peer) || (NULL != peer1.peer))
408 GNUNET_TESTBED_operation_done (peer1.operation);
409 result = PEER1_CREATED;
410 peer1.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
414 if ((NULL == peer2.operation) || (NULL == peer) || (NULL != peer2.peer))
421 GNUNET_TESTBED_operation_done (peer2.operation);
422 result = PEER2_CREATED;
423 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);
448 * Signature of the event handler function called by the
449 * respective event controller.
452 * @param event information about the event
455 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
459 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
460 if ((NULL != event->op_cls) ||
461 (NULL != event->details.operation_finished.emsg))
470 if (NULL != event->details.operation_finished.generic)
476 if (event->op == peer1.operation)
478 GNUNET_TESTBED_operation_done (peer1.operation);
479 peer1.operation = NULL;
482 else if (event->op == peer2.operation)
484 GNUNET_TESTBED_operation_done (peer2.operation);
485 peer2.operation = NULL;
488 else if (event->op == peer3.operation)
490 GNUNET_TESTBED_operation_done (peer3.operation);
491 peer3.operation = NULL;
500 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
503 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
508 if ((NULL != event->details.operation_finished.generic) ||
509 (NULL == common_operation))
515 GNUNET_TESTBED_operation_done (common_operation);
516 common_operation = NULL;
517 result = CONTROLLER2_UP;
519 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
520 &peer_create_cb, NULL);
521 if (NULL == peer2.operation)
530 if ((NULL != event->details.operation_finished.generic) ||
531 (NULL == common_operation))
537 GNUNET_TESTBED_operation_done (common_operation);
538 common_operation = NULL;
539 result = CONTROLLER3_UP;
541 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
542 &peer_create_cb, NULL);
543 if (NULL == peer3.operation)
558 case GNUNET_TESTBED_ET_PEER_START:
562 if (event->details.peer_start.host != host)
568 peer1.is_running = GNUNET_YES;
569 GNUNET_TESTBED_operation_done (peer1.operation);
570 peer1.operation = NULL;
571 result = PEER1_STARTED;
573 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL,
578 if (event->details.peer_start.host != neighbour1)
584 peer2.is_running = GNUNET_YES;
585 GNUNET_TESTBED_operation_done (peer2.operation);
586 peer2.operation = NULL;
587 result = PEER2_STARTED;
588 if (NULL != common_operation)
595 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL,
597 if (NULL == common_operation)
606 if (event->details.peer_start.host != neighbour2)
612 peer3.is_running = GNUNET_YES;
613 GNUNET_TESTBED_operation_done (peer3.operation);
614 peer3.operation = NULL;
615 result = PEER3_STARTED;
617 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
628 case GNUNET_TESTBED_ET_PEER_STOP:
629 if (PEERS_CONNECTED_2 != result)
635 if (event->details.peer_stop.peer == peer1.peer)
637 peer1.is_running = GNUNET_NO;
638 GNUNET_TESTBED_operation_done (peer1.operation);
640 else if (event->details.peer_stop.peer == peer2.peer)
642 peer2.is_running = GNUNET_NO;
643 GNUNET_TESTBED_operation_done (peer2.operation);
645 else if (event->details.peer_stop.peer == peer3.peer)
647 peer3.is_running = GNUNET_NO;
648 GNUNET_TESTBED_operation_done (peer3.operation);
656 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running) &&
657 (GNUNET_NO == peer3.is_running))
659 result = PEERS_STOPPED;
660 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
661 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
662 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
666 case GNUNET_TESTBED_ET_CONNECT:
667 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
668 (NULL != peer3.operation) || (NULL == common_operation))
677 if ((event->details.peer_connect.peer1 != peer2.peer) ||
678 (event->details.peer_connect.peer2 != peer1.peer))
684 GNUNET_TESTBED_operation_done (common_operation);
685 common_operation = NULL;
686 result = PEERS_1_2_CONNECTED;
687 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
689 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
693 case PEERS_1_2_CONNECTED:
694 if ((event->details.peer_connect.peer1 != peer2.peer) ||
695 (event->details.peer_connect.peer2 != peer3.peer))
701 GNUNET_TESTBED_operation_done (common_operation);
702 common_operation = NULL;
703 result = PEERS_2_3_CONNECTED;
704 delayed_connect_task =
705 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), &do_delayed_connect,
709 case PEERS_2_3_CONNECTED:
710 if ((event->details.peer_connect.peer1 != peer1.peer) ||
711 (event->details.peer_connect.peer2 != peer2.peer))
717 GNUNET_TESTBED_operation_done (common_operation);
718 common_operation = NULL;
719 result = PEERS_CONNECTED_2;
720 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
721 peer1.operation = GNUNET_TESTBED_peer_stop (NULL, peer1.peer, NULL, NULL);
722 peer2.operation = GNUNET_TESTBED_peer_stop (NULL, peer2.peer, NULL, NULL);
723 peer3.operation = GNUNET_TESTBED_peer_stop (NULL, peer3.peer, NULL, NULL);
742 * Callback which will be called to after a host registration succeeded or failed
744 * @param cls the host which has been registered
745 * @param emsg the error message; NULL if host registration is successful
748 registration_comp (void *cls, const char *emsg)
751 if (cls == neighbour1)
753 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
754 if (NULL == neighbour2)
761 GNUNET_TESTBED_register_host (controller1, neighbour2,
762 ®istration_comp, neighbour2);
763 if (NULL == reg_handle)
771 if (cls != neighbour2)
778 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
780 if (NULL == peer1.operation)
790 * Callback to signal successfull startup of the controller process
792 * @param cls the closure from GNUNET_TESTBED_controller_start()
793 * @param cfg the configuration with which the controller has been started;
794 * NULL if status is not GNUNET_OK
795 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
796 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
799 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
804 if (GNUNET_OK != status)
812 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
813 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
814 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
815 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
820 GNUNET_TESTBED_controller_connect (host, event_mask,
821 &controller_cb, NULL);
822 if (NULL == controller1)
828 result = CONTROLLER1_UP;
829 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
830 if (NULL == neighbour1)
837 GNUNET_TESTBED_register_host (controller1, neighbour1,
838 ®istration_comp, neighbour1);
839 if (NULL == reg_handle)
856 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
857 * inform whether the given host is habitable or not. The Handle returned by
858 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
861 * @param host the host whose status is being reported; will be NULL if the host
862 * given to GNUNET_TESTBED_is_host_habitable() is NULL
863 * @param status #GNUNET_YES if it is habitable; #GNUNET_NO if not
866 host_habitable_cb (void *cls,
867 const struct GNUNET_TESTBED_Host *_host,
871 if (GNUNET_NO == status)
874 "Unable to run the test as this system is not configured "
875 "to use password less SSH logins to localhost.\n"
877 GNUNET_SCHEDULER_cancel (abort_task);
879 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
884 GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb, NULL);
892 * @param args arguments passed to GNUNET_PROGRAM_run
893 * @param cfgfile the path to configuration file
894 * @param cfg the configuration file handle
897 run (void *cls, char *const *args, const char *cfgfile,
898 const struct GNUNET_CONFIGURATION_Handle *config)
900 cfg = GNUNET_CONFIGURATION_dup (config);
901 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
910 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
913 GNUNET_TESTBED_host_destroy (host);
916 "Unable to run the test as this system is not configured "
917 "to use password less SSH logins to localhost.\n"
923 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
924 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
933 main (int argc, char **argv)
935 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
936 "-c", "test_testbed_api.conf",
938 struct GNUNET_GETOPT_CommandLineOption options[] = {
939 GNUNET_GETOPT_OPTION_END
945 GNUNET_PROGRAM_run ((sizeof(argv2) / sizeof(char *)) - 1, argv2,
946 "test_testbed_api_3peers_3controllers", "nohelp",
947 options, &run, NULL);
948 if (GNUNET_OK != ret)
956 return 77; /* Mark test as skipped */
964 /* end of test_testbed_api_3peers_3controllers.c */