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/>.
20 * @file testbed/test_testbed_api_3peers_3controllers.c
21 * @brief testcases for the testbed api: 3 peers are configured, started and
22 * connected together. Each peer resides on its own controller.
23 * @author Sree Harsha Totakura
28 * The testing architecture is:
33 * A is the master controller and B, C are slave controllers. B links to C
35 * Peers are mapped to controllers in the following relations:
44 #include "gnunet_util_lib.h"
45 #include "gnunet_testing_lib.h"
46 #include "gnunet_testbed_service.h"
50 * Generic logging shortcut
52 #define LOG(kind,...) \
53 GNUNET_log (kind, __VA_ARGS__)
56 * Relative time seconds shorthand
58 #define TIME_REL_SECS(sec) \
59 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
70 struct GNUNET_TESTBED_Peer *peer;
73 * Operations involving this peer
75 struct GNUNET_TESTBED_Operation *operation;
78 * set to GNUNET_YES when peer is started
86 static struct GNUNET_TESTBED_Host *host;
89 * The controller process of one controller
91 static struct GNUNET_TESTBED_ControllerProc *cp1;
96 static struct GNUNET_TESTBED_Host *neighbour1;
99 * Another neighbouring host
101 static struct GNUNET_TESTBED_Host *neighbour2;
104 * Handle for neighbour registration
106 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
109 * The controller handle of one controller
111 static struct GNUNET_TESTBED_Controller *controller1;
116 static struct PeerContext peer1;
121 static struct PeerContext peer2;
126 static struct PeerContext peer3;
129 * Handle to starting configuration
131 static struct GNUNET_CONFIGURATION_Handle *cfg;
134 * Handle to slave controller C's configuration, used to establish lateral link from
137 static struct GNUNET_CONFIGURATION_Handle *cfg2;
140 * Handle to operations involving both peers
142 static struct GNUNET_TESTBED_Operation *common_operation;
145 * The handle for whether a host is habitable or not
147 struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
150 * Abort task identifier
152 static struct GNUNET_SCHEDULER_Task * abort_task;
155 * Delayed connect job identifier
157 static struct GNUNET_SCHEDULER_Task * delayed_connect_task;
160 * Different stages in testing
171 * Controller 1 has started
186 * Controller 2 has started
201 * Controller 3 has started
216 * peer1 and peer2 are connected
221 * peer2 and peer3 are connected
226 * Peers are connected once again (this should not fail as they are already connected)
236 * Final success stage
241 * Optional stage for marking test to be skipped
249 static enum Stage result;
257 do_shutdown (void *cls)
259 if (NULL != abort_task)
260 GNUNET_SCHEDULER_cancel (abort_task);
261 if (NULL != hc_handle)
262 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
263 GNUNET_assert (NULL == delayed_connect_task);
264 if (NULL != common_operation)
265 GNUNET_TESTBED_operation_done (common_operation);
266 if (NULL != reg_handle)
267 GNUNET_TESTBED_cancel_registration (reg_handle);
268 if (NULL != controller1)
269 GNUNET_TESTBED_controller_disconnect (controller1);
270 GNUNET_CONFIGURATION_destroy (cfg);
272 GNUNET_CONFIGURATION_destroy (cfg2);
274 GNUNET_TESTBED_controller_stop (cp1);
276 GNUNET_TESTBED_host_destroy (host);
277 if (NULL != neighbour1)
278 GNUNET_TESTBED_host_destroy (neighbour1);
279 if (NULL != neighbour2)
280 GNUNET_TESTBED_host_destroy (neighbour2);
285 * abort task to run on test timed out
292 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
294 if (NULL != delayed_connect_task)
296 GNUNET_SCHEDULER_cancel (delayed_connect_task);
297 delayed_connect_task = NULL;
306 if (NULL != abort_task)
307 GNUNET_SCHEDULER_cancel (abort_task);
308 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
313 * Callback to be called when an operation is completed
315 * @param cls the callback closure from functions generating an operation
316 * @param op the operation that has been finished
317 * @param emsg error message in case the operation has failed; will be NULL if
318 * operation has executed successfully.
321 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
325 * task for delaying a connect
330 do_delayed_connect (void *cls)
332 delayed_connect_task = NULL;
333 if (NULL != common_operation)
340 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
346 * Callback to be called when an operation is completed
348 * @param cls the callback closure from functions generating an operation
349 * @param op the operation that has been finished
350 * @param emsg error message in case the operation has failed; will be NULL if
351 * operation has executed successfully.
354 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
356 if (common_operation != op)
366 case PEERS_2_3_CONNECTED:
367 case PEERS_1_2_CONNECTED:
374 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
375 (NULL != peer3.operation))
385 * Functions of this signature are called when a peer has been successfully
389 * @param peer the handle for the created peer; NULL on any error during
391 * @param emsg NULL if peer is not NULL; else MAY contain the error description
394 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
399 if ((NULL == peer1.operation) || (NULL == peer) || (NULL != peer1.peer))
406 GNUNET_TESTBED_operation_done (peer1.operation);
407 result = PEER1_CREATED;
408 peer1.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
411 if ((NULL == peer2.operation) || (NULL == peer) || (NULL != peer2.peer))
418 GNUNET_TESTBED_operation_done (peer2.operation);
419 result = PEER2_CREATED;
420 peer2.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
423 if ((NULL == peer3.operation) || (NULL == peer) || (NULL != peer3.peer))
430 GNUNET_TESTBED_operation_done (peer3.operation);
431 result = PEER3_CREATED;
432 peer3.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
443 * Signature of the event handler function called by the
444 * respective event controller.
447 * @param event information about the event
450 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
454 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
455 if ((NULL != event->op_cls) ||
456 (NULL != event->details.operation_finished.emsg))
465 if (NULL != event->details.operation_finished.generic)
471 if (event->op == peer1.operation)
473 GNUNET_TESTBED_operation_done (peer1.operation);
474 peer1.operation = NULL;
477 else if (event->op == peer2.operation)
479 GNUNET_TESTBED_operation_done (peer2.operation);
480 peer2.operation = NULL;
483 else if (event->op == peer3.operation)
485 GNUNET_TESTBED_operation_done (peer3.operation);
486 peer3.operation = NULL;
495 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
498 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
502 if ((NULL != event->details.operation_finished.generic) ||
503 (NULL == common_operation))
509 GNUNET_TESTBED_operation_done (common_operation);
510 common_operation = NULL;
511 result = CONTROLLER2_UP;
513 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
514 &peer_create_cb, NULL);
515 if (NULL == peer2.operation)
523 if ((NULL != event->details.operation_finished.generic) ||
524 (NULL == common_operation))
530 GNUNET_TESTBED_operation_done (common_operation);
531 common_operation = NULL;
532 result = CONTROLLER3_UP;
534 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
535 &peer_create_cb, NULL);
536 if (NULL == peer3.operation)
549 case GNUNET_TESTBED_ET_PEER_START:
553 if (event->details.peer_start.host != host)
559 peer1.is_running = GNUNET_YES;
560 GNUNET_TESTBED_operation_done (peer1.operation);
561 peer1.operation = NULL;
562 result = PEER1_STARTED;
564 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL,
568 if (event->details.peer_start.host != neighbour1)
574 peer2.is_running = GNUNET_YES;
575 GNUNET_TESTBED_operation_done (peer2.operation);
576 peer2.operation = NULL;
577 result = PEER2_STARTED;
578 if (NULL != common_operation)
585 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL,
587 if (NULL == common_operation)
595 if (event->details.peer_start.host != neighbour2)
601 peer3.is_running = GNUNET_YES;
602 GNUNET_TESTBED_operation_done (peer3.operation);
603 peer3.operation = NULL;
604 result = PEER3_STARTED;
606 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
615 case GNUNET_TESTBED_ET_PEER_STOP:
616 if (PEERS_CONNECTED_2 != result)
622 if (event->details.peer_stop.peer == peer1.peer)
624 peer1.is_running = GNUNET_NO;
625 GNUNET_TESTBED_operation_done (peer1.operation);
627 else if (event->details.peer_stop.peer == peer2.peer)
629 peer2.is_running = GNUNET_NO;
630 GNUNET_TESTBED_operation_done (peer2.operation);
632 else if (event->details.peer_stop.peer == peer3.peer)
634 peer3.is_running = GNUNET_NO;
635 GNUNET_TESTBED_operation_done (peer3.operation);
643 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running) &&
644 (GNUNET_NO == peer3.is_running))
646 result = PEERS_STOPPED;
647 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
648 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
649 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
652 case GNUNET_TESTBED_ET_CONNECT:
653 if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
654 (NULL != peer3.operation) || (NULL == common_operation))
663 if ((event->details.peer_connect.peer1 != peer2.peer) ||
664 (event->details.peer_connect.peer2 != peer1.peer))
670 GNUNET_TESTBED_operation_done (common_operation);
671 common_operation = NULL;
672 result = PEERS_1_2_CONNECTED;
673 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
675 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
678 case PEERS_1_2_CONNECTED:
679 if ((event->details.peer_connect.peer1 != peer2.peer) ||
680 (event->details.peer_connect.peer2 != peer3.peer))
686 GNUNET_TESTBED_operation_done (common_operation);
687 common_operation = NULL;
688 result = PEERS_2_3_CONNECTED;
689 delayed_connect_task =
690 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), &do_delayed_connect,
693 case PEERS_2_3_CONNECTED:
694 if ((event->details.peer_connect.peer1 != peer1.peer) ||
695 (event->details.peer_connect.peer2 != peer2.peer))
701 GNUNET_TESTBED_operation_done (common_operation);
702 common_operation = NULL;
703 result = PEERS_CONNECTED_2;
704 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
705 peer1.operation = GNUNET_TESTBED_peer_stop (NULL, peer1.peer, NULL, NULL);
706 peer2.operation = GNUNET_TESTBED_peer_stop (NULL, peer2.peer, NULL, NULL);
707 peer3.operation = GNUNET_TESTBED_peer_stop (NULL, peer3.peer, NULL, NULL);
724 * Callback which will be called to after a host registration succeeded or failed
726 * @param cls the host which has been registered
727 * @param emsg the error message; NULL if host registration is successful
730 registration_comp (void *cls, const char *emsg)
733 if (cls == neighbour1)
735 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
736 if (NULL == neighbour2)
743 GNUNET_TESTBED_register_host (controller1, neighbour2,
744 ®istration_comp, neighbour2);
745 if (NULL == reg_handle)
753 if (cls != neighbour2)
760 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
762 if (NULL == peer1.operation)
772 * Callback to signal successfull startup of the controller process
774 * @param cls the closure from GNUNET_TESTBED_controller_start()
775 * @param cfg the configuration with which the controller has been started;
776 * NULL if status is not GNUNET_OK
777 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
778 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
781 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
786 if (GNUNET_OK != status)
794 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
795 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
796 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
797 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
802 GNUNET_TESTBED_controller_connect (host, event_mask,
803 &controller_cb, NULL);
804 if (NULL == controller1)
810 result = CONTROLLER1_UP;
811 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
812 if (NULL == neighbour1)
819 GNUNET_TESTBED_register_host (controller1, neighbour1,
820 ®istration_comp, neighbour1);
821 if (NULL == reg_handle)
837 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
838 * inform whether the given host is habitable or not. The Handle returned by
839 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
842 * @param host the host whose status is being reported; will be NULL if the host
843 * given to GNUNET_TESTBED_is_host_habitable() is NULL
844 * @param status #GNUNET_YES if it is habitable; #GNUNET_NO if not
847 host_habitable_cb (void *cls,
848 const struct GNUNET_TESTBED_Host *_host,
852 if (GNUNET_NO == status)
855 "Unable to run the test as this system is not configured "
856 "to use password less SSH logins to localhost.\n"
858 GNUNET_SCHEDULER_cancel (abort_task);
860 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
865 GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb, NULL);
873 * @param args arguments passed to GNUNET_PROGRAM_run
874 * @param cfgfile the path to configuration file
875 * @param cfg the configuration file handle
878 run (void *cls, char *const *args, const char *cfgfile,
879 const struct GNUNET_CONFIGURATION_Handle *config)
881 cfg = GNUNET_CONFIGURATION_dup (config);
882 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
891 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
894 GNUNET_TESTBED_host_destroy (host);
897 "Unable to run the test as this system is not configured "
898 "to use password less SSH logins to localhost.\n"
904 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
905 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
914 main (int argc, char **argv)
916 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
917 "-c", "test_testbed_api.conf",
920 struct GNUNET_GETOPT_CommandLineOption options[] = {
921 GNUNET_GETOPT_OPTION_END
927 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
928 "test_testbed_api_3peers_3controllers", "nohelp",
929 options, &run, NULL);
930 if (GNUNET_OK != ret)
937 return 77; /* Mark test as skipped */
943 /* end of test_testbed_api_3peers_3controllers.c */