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
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;
259 do_shutdown (void *cls)
261 if (NULL != abort_task)
262 GNUNET_SCHEDULER_cancel (abort_task);
263 if (NULL != hc_handle)
264 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
265 GNUNET_assert (NULL == delayed_connect_task);
266 if (NULL != common_operation)
267 GNUNET_TESTBED_operation_done (common_operation);
268 if (NULL != reg_handle)
269 GNUNET_TESTBED_cancel_registration (reg_handle);
270 if (NULL != controller1)
271 GNUNET_TESTBED_controller_disconnect (controller1);
272 GNUNET_CONFIGURATION_destroy (cfg);
274 GNUNET_CONFIGURATION_destroy (cfg2);
276 GNUNET_TESTBED_controller_stop (cp1);
278 GNUNET_TESTBED_host_destroy (host);
279 if (NULL != neighbour1)
280 GNUNET_TESTBED_host_destroy (neighbour1);
281 if (NULL != neighbour2)
282 GNUNET_TESTBED_host_destroy (neighbour2);
287 * abort task to run on test timed out
294 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
296 if (NULL != delayed_connect_task)
298 GNUNET_SCHEDULER_cancel (delayed_connect_task);
299 delayed_connect_task = NULL;
308 if (NULL != abort_task)
309 GNUNET_SCHEDULER_cancel (abort_task);
310 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
315 * Callback to be called when an operation is completed
317 * @param cls the callback closure from functions generating an operation
318 * @param op the operation that has been finished
319 * @param emsg error message in case the operation has failed; will be NULL if
320 * operation has executed successfully.
323 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
327 * task for delaying a connect
332 do_delayed_connect (void *cls)
334 delayed_connect_task = NULL;
335 if (NULL != common_operation)
342 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
348 * Callback to be called when an operation is completed
350 * @param cls the callback closure from functions generating an operation
351 * @param op the operation that has been finished
352 * @param emsg error message in case the operation has failed; will be NULL if
353 * operation has executed successfully.
356 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
358 if (common_operation != op)
368 case PEERS_2_3_CONNECTED:
369 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);
413 if ((NULL == peer2.operation) || (NULL == peer) || (NULL != peer2.peer))
420 GNUNET_TESTBED_operation_done (peer2.operation);
421 result = PEER2_CREATED;
422 peer2.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
425 if ((NULL == peer3.operation) || (NULL == peer) || (NULL != peer3.peer))
432 GNUNET_TESTBED_operation_done (peer3.operation);
433 result = PEER3_CREATED;
434 peer3.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
445 * Signature of the event handler function called by the
446 * respective event controller.
449 * @param event information about the event
452 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
456 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
457 if ((NULL != event->op_cls) ||
458 (NULL != event->details.operation_finished.emsg))
467 if (NULL != event->details.operation_finished.generic)
473 if (event->op == peer1.operation)
475 GNUNET_TESTBED_operation_done (peer1.operation);
476 peer1.operation = NULL;
479 else if (event->op == peer2.operation)
481 GNUNET_TESTBED_operation_done (peer2.operation);
482 peer2.operation = NULL;
485 else if (event->op == peer3.operation)
487 GNUNET_TESTBED_operation_done (peer3.operation);
488 peer3.operation = NULL;
497 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
500 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
504 if ((NULL != event->details.operation_finished.generic) ||
505 (NULL == common_operation))
511 GNUNET_TESTBED_operation_done (common_operation);
512 common_operation = NULL;
513 result = CONTROLLER2_UP;
515 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
516 &peer_create_cb, NULL);
517 if (NULL == peer2.operation)
525 if ((NULL != event->details.operation_finished.generic) ||
526 (NULL == common_operation))
532 GNUNET_TESTBED_operation_done (common_operation);
533 common_operation = NULL;
534 result = CONTROLLER3_UP;
536 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
537 &peer_create_cb, NULL);
538 if (NULL == peer3.operation)
551 case GNUNET_TESTBED_ET_PEER_START:
555 if (event->details.peer_start.host != host)
561 peer1.is_running = GNUNET_YES;
562 GNUNET_TESTBED_operation_done (peer1.operation);
563 peer1.operation = NULL;
564 result = PEER1_STARTED;
566 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL,
570 if (event->details.peer_start.host != neighbour1)
576 peer2.is_running = GNUNET_YES;
577 GNUNET_TESTBED_operation_done (peer2.operation);
578 peer2.operation = NULL;
579 result = PEER2_STARTED;
580 if (NULL != common_operation)
587 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL,
589 if (NULL == common_operation)
597 if (event->details.peer_start.host != neighbour2)
603 peer3.is_running = GNUNET_YES;
604 GNUNET_TESTBED_operation_done (peer3.operation);
605 peer3.operation = NULL;
606 result = PEER3_STARTED;
608 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
617 case GNUNET_TESTBED_ET_PEER_STOP:
618 if (PEERS_CONNECTED_2 != result)
624 if (event->details.peer_stop.peer == peer1.peer)
626 peer1.is_running = GNUNET_NO;
627 GNUNET_TESTBED_operation_done (peer1.operation);
629 else if (event->details.peer_stop.peer == peer2.peer)
631 peer2.is_running = GNUNET_NO;
632 GNUNET_TESTBED_operation_done (peer2.operation);
634 else if (event->details.peer_stop.peer == peer3.peer)
636 peer3.is_running = GNUNET_NO;
637 GNUNET_TESTBED_operation_done (peer3.operation);
645 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running) &&
646 (GNUNET_NO == peer3.is_running))
648 result = PEERS_STOPPED;
649 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
650 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
651 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
654 case GNUNET_TESTBED_ET_CONNECT:
655 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
656 (NULL != peer3.operation) || (NULL == common_operation))
665 if ((event->details.peer_connect.peer1 != peer2.peer) ||
666 (event->details.peer_connect.peer2 != peer1.peer))
672 GNUNET_TESTBED_operation_done (common_operation);
673 common_operation = NULL;
674 result = PEERS_1_2_CONNECTED;
675 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
677 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
680 case PEERS_1_2_CONNECTED:
681 if ((event->details.peer_connect.peer1 != peer2.peer) ||
682 (event->details.peer_connect.peer2 != peer3.peer))
688 GNUNET_TESTBED_operation_done (common_operation);
689 common_operation = NULL;
690 result = PEERS_2_3_CONNECTED;
691 delayed_connect_task =
692 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), &do_delayed_connect,
695 case PEERS_2_3_CONNECTED:
696 if ((event->details.peer_connect.peer1 != peer1.peer) ||
697 (event->details.peer_connect.peer2 != peer2.peer))
703 GNUNET_TESTBED_operation_done (common_operation);
704 common_operation = NULL;
705 result = PEERS_CONNECTED_2;
706 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
707 peer1.operation = GNUNET_TESTBED_peer_stop (NULL, peer1.peer, NULL, NULL);
708 peer2.operation = GNUNET_TESTBED_peer_stop (NULL, peer2.peer, NULL, NULL);
709 peer3.operation = GNUNET_TESTBED_peer_stop (NULL, peer3.peer, NULL, NULL);
726 * Callback which will be called to after a host registration succeeded or failed
728 * @param cls the host which has been registered
729 * @param emsg the error message; NULL if host registration is successful
732 registration_comp (void *cls, const char *emsg)
735 if (cls == neighbour1)
737 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
738 if (NULL == neighbour2)
745 GNUNET_TESTBED_register_host (controller1, neighbour2,
746 ®istration_comp, neighbour2);
747 if (NULL == reg_handle)
755 if (cls != neighbour2)
762 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
764 if (NULL == peer1.operation)
774 * Callback to signal successfull startup of the controller process
776 * @param cls the closure from GNUNET_TESTBED_controller_start()
777 * @param cfg the configuration with which the controller has been started;
778 * NULL if status is not GNUNET_OK
779 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
780 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
783 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
788 if (GNUNET_OK != status)
796 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
797 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
798 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
799 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
804 GNUNET_TESTBED_controller_connect (host, event_mask,
805 &controller_cb, NULL);
806 if (NULL == controller1)
812 result = CONTROLLER1_UP;
813 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
814 if (NULL == neighbour1)
821 GNUNET_TESTBED_register_host (controller1, neighbour1,
822 ®istration_comp, neighbour1);
823 if (NULL == reg_handle)
839 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
840 * inform whether the given host is habitable or not. The Handle returned by
841 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
844 * @param host the host whose status is being reported; will be NULL if the host
845 * given to GNUNET_TESTBED_is_host_habitable() is NULL
846 * @param status #GNUNET_YES if it is habitable; #GNUNET_NO if not
849 host_habitable_cb (void *cls,
850 const struct GNUNET_TESTBED_Host *_host,
854 if (GNUNET_NO == status)
857 "Unable to run the test as this system is not configured "
858 "to use password less SSH logins to localhost.\n"
860 GNUNET_SCHEDULER_cancel (abort_task);
862 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
867 GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb, NULL);
875 * @param args arguments passed to GNUNET_PROGRAM_run
876 * @param cfgfile the path to configuration file
877 * @param cfg the configuration file handle
880 run (void *cls, char *const *args, const char *cfgfile,
881 const struct GNUNET_CONFIGURATION_Handle *config)
883 cfg = GNUNET_CONFIGURATION_dup (config);
884 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
893 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
896 GNUNET_TESTBED_host_destroy (host);
899 "Unable to run the test as this system is not configured "
900 "to use password less SSH logins to localhost.\n"
906 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
907 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
916 main (int argc, char **argv)
918 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
919 "-c", "test_testbed_api.conf",
922 struct GNUNET_GETOPT_CommandLineOption options[] = {
923 GNUNET_GETOPT_OPTION_END
929 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
930 "test_testbed_api_3peers_3controllers", "nohelp",
931 options, &run, NULL);
932 if (GNUNET_OK != ret)
939 return 77; /* Mark test as skipped */
945 /* end of test_testbed_api_3peers_3controllers.c */