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 * Signature of the event handler function called by the
475 * respective event controller.
478 * @param event information about the event
481 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
485 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
486 if ((NULL != event->details.operation_finished.op_cls)
487 || (NULL != event->details.operation_finished.emsg))
496 if (NULL != event->details.operation_finished.generic)
502 if (event->details.operation_finished.operation == peer1.operation)
504 GNUNET_TESTBED_operation_done (peer1.operation);
505 peer1.operation = NULL;
508 else if (event->details.operation_finished.operation == peer2.operation)
510 GNUNET_TESTBED_operation_done (peer2.operation);
511 peer2.operation = NULL;
514 else if (event->details.operation_finished.operation == peer3.operation)
516 GNUNET_TESTBED_operation_done (peer3.operation);
517 peer3.operation = NULL;
526 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
529 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
533 if ((NULL != event->details.operation_finished.generic) ||
534 (NULL == common_operation))
540 GNUNET_TESTBED_operation_done (common_operation);
541 common_operation = NULL;
542 result = CONTROLLER2_UP;
544 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
545 &peer_create_cb, NULL);
546 if (NULL == peer2.operation)
554 if ((NULL != event->details.operation_finished.generic) ||
555 (NULL == common_operation))
561 GNUNET_TESTBED_operation_done (common_operation);
562 common_operation = NULL;
563 result = CONTROLLER3_UP;
565 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
566 &peer_create_cb, NULL);
567 if (NULL == peer3.operation)
580 case GNUNET_TESTBED_ET_PEER_START:
584 if (event->details.peer_start.host != host)
590 peer1.is_running = GNUNET_YES;
591 GNUNET_TESTBED_operation_done (peer1.operation);
592 peer1.operation = NULL;
593 result = PEER1_STARTED;
595 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL, cfg,
599 if (event->details.peer_start.host != neighbour1)
605 peer2.is_running = GNUNET_YES;
606 GNUNET_TESTBED_operation_done (peer2.operation);
607 peer2.operation = NULL;
608 result = PEER2_STARTED;
609 if (NULL != common_operation)
616 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL, cfg,
618 if (NULL == common_operation)
626 if (event->details.peer_start.host != neighbour2)
632 peer3.is_running = GNUNET_YES;
633 GNUNET_TESTBED_operation_done (peer3.operation);
634 peer3.operation = NULL;
635 result = PEER3_STARTED;
637 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
646 case GNUNET_TESTBED_ET_PEER_STOP:
647 if (PEERS_CONNECTED_2 != result)
653 if (event->details.peer_stop.peer == peer1.peer)
655 peer1.is_running = GNUNET_NO;
656 GNUNET_TESTBED_operation_done (peer1.operation);
658 else if (event->details.peer_stop.peer == peer2.peer)
660 peer2.is_running = GNUNET_NO;
661 GNUNET_TESTBED_operation_done (peer2.operation);
663 else if (event->details.peer_stop.peer == peer3.peer)
665 peer3.is_running = GNUNET_NO;
666 GNUNET_TESTBED_operation_done (peer3.operation);
674 if ((GNUNET_NO == peer1.is_running) &&
675 (GNUNET_NO == peer2.is_running) &&
676 (GNUNET_NO == peer3.is_running))
678 result = PEERS_STOPPED;
679 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
680 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
681 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
684 case GNUNET_TESTBED_ET_CONNECT:
685 if ((NULL != peer1.operation) ||
686 (NULL != peer2.operation) ||
687 (NULL != peer3.operation))
696 if ((NULL == common_operation) ||
697 (event->details.peer_connect.peer1 != peer2.peer) ||
698 (event->details.peer_connect.peer2 != peer1.peer))
705 case PEERS_2_3_CONNECTED:
706 if ((NULL == common_operation) ||
707 (event->details.peer_connect.peer1 != peer1.peer) ||
708 (event->details.peer_connect.peer2 != peer2.peer))
715 case PEERS_1_2_CONNECTED:
716 if ((NULL == common_operation) ||
717 (event->details.peer_connect.peer1 != peer2.peer) ||
718 (event->details.peer_connect.peer2 != peer3.peer))
740 * Callback which will be called to after a host registration succeeded or failed
742 * @param cls the host which has been registered
743 * @param emsg the error message; NULL if host registration is successful
746 registration_comp (void *cls, const char *emsg)
749 if (cls == neighbour1)
751 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
752 if (NULL == neighbour2)
759 GNUNET_TESTBED_register_host (controller1, neighbour2, ®istration_comp,
761 if (NULL == reg_handle)
769 if (cls != neighbour2)
776 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
778 if (NULL == peer1.operation)
788 * Callback to signal successfull startup of the controller process
790 * @param cls the closure from GNUNET_TESTBED_controller_start()
791 * @param cfg the configuration with which the controller has been started;
792 * NULL if status is not GNUNET_OK
793 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
794 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
797 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
801 if (GNUNET_OK != status)
809 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
810 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
811 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
812 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
817 GNUNET_TESTBED_controller_connect (config, host, event_mask, &controller_cb,
819 if (NULL == controller1)
825 result = CONTROLLER1_UP;
826 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
827 if (NULL == neighbour1)
834 GNUNET_TESTBED_register_host (controller1, neighbour1, ®istration_comp,
836 if (NULL == reg_handle)
855 * @param args arguments passed to GNUNET_PROGRAM_run
856 * @param cfgfile the path to configuration file
857 * @param cfg the configuration file handle
860 run (void *cls, char *const *args, const char *cfgfile,
861 const struct GNUNET_CONFIGURATION_Handle *config)
863 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
870 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (host, config))
872 GNUNET_TESTBED_host_destroy (host);
875 "Unable to run the test as this system is not configured "
876 "to use password less SSH logins to localhost.\n"
877 "Marking test as successful\n");
881 cfg = GNUNET_CONFIGURATION_dup (config);
882 cp1 = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
885 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
886 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
895 main (int argc, char **argv)
897 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
898 "-c", "test_testbed_api.conf",
901 struct GNUNET_GETOPT_CommandLineOption options[] = {
902 GNUNET_GETOPT_OPTION_END
908 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
909 "test_testbed_api_3peers_3controllers", "nohelp",
910 options, &run, NULL);
911 if ((GNUNET_OK != ret) || (SUCCESS != result))
916 /* end of test_testbed_api_3peers_3controllers.c */