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 * Configuration of C has been acquired
220 CONTROLLER_CFG_ACQUIRED,
223 * Linking controller B to C laterally
225 CONTROLLERS_LATERALLY_LINKED,
228 * peer2 and peer3 are connected
233 * Peers are connected once again (this should not fail as they are already connected)
243 * Final success stage
251 static enum Stage result;
257 * @param tc the task context
260 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
263 GNUNET_SCHEDULER_cancel (abort_task);
264 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
265 if (NULL != reg_handle)
266 GNUNET_TESTBED_cancel_registration (reg_handle);
267 if (NULL != controller1)
268 GNUNET_TESTBED_controller_disconnect (controller1);
269 GNUNET_CONFIGURATION_destroy (cfg);
271 GNUNET_CONFIGURATION_destroy (cfg2);
273 GNUNET_TESTBED_controller_stop (cp1);
275 GNUNET_TESTBED_host_destroy (host);
276 if (NULL != neighbour1)
277 GNUNET_TESTBED_host_destroy (neighbour1);
278 if (NULL != neighbour2)
279 GNUNET_TESTBED_host_destroy (neighbour2);
284 * abort task to run on test timed out
287 * @param tc the task context
290 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
292 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
293 abort_task = GNUNET_SCHEDULER_NO_TASK;
294 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
295 do_shutdown (cls, tc);
300 * Callback to be called when an operation is completed
302 * @param cls the callback closure from functions generating an operation
303 * @param op the operation that has been finished
304 * @param emsg error message in case the operation has failed; will be NULL if
305 * operation has executed successfully.
308 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
312 * task for delaying a connect
315 * @param tc the task context
318 do_delayed_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
320 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
321 GNUNET_assert (NULL == common_operation);
322 common_operation = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL,
323 peer1.peer, peer2.peer);
328 * Callback to be called when an operation is completed
330 * @param cls the callback closure from functions generating an operation
331 * @param op the operation that has been finished
332 * @param emsg error message in case the operation has failed; will be NULL if
333 * operation has executed successfully.
336 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
338 GNUNET_assert (common_operation == op);
342 GNUNET_assert (NULL == peer1.operation);
343 GNUNET_assert (NULL == peer2.operation);
344 GNUNET_assert (NULL != common_operation);
345 GNUNET_TESTBED_operation_done (common_operation);
346 common_operation = NULL;
347 result = PEERS_1_2_CONNECTED;
348 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
350 GNUNET_TESTBED_get_slave_config (NULL, controller1, neighbour2);
352 case PEERS_2_3_CONNECTED:
353 GNUNET_assert (NULL == peer1.operation);
354 GNUNET_assert (NULL == peer2.operation);
355 GNUNET_assert (NULL != common_operation);
356 GNUNET_TESTBED_operation_done (common_operation);
357 common_operation = NULL;
358 result = PEERS_CONNECTED_2;
359 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
360 peer1.operation = GNUNET_TESTBED_peer_stop (peer1.peer, NULL, NULL);
361 peer2.operation = GNUNET_TESTBED_peer_stop (peer2.peer, NULL, NULL);
362 peer3.operation = GNUNET_TESTBED_peer_stop (peer3.peer, NULL, NULL);
364 case CONTROLLERS_LATERALLY_LINKED:
365 GNUNET_TESTBED_operation_done (common_operation);
366 common_operation = NULL;
367 result = PEERS_2_3_CONNECTED;
368 delayed_connect_task =
369 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3),
370 &do_delayed_connect, NULL);
379 * Functions of this signature are called when a peer has been successfully
383 * @param peer the handle for the created peer; NULL on any error during
385 * @param emsg NULL if peer is not NULL; else MAY contain the error description
388 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
393 GNUNET_assert (NULL != peer1.operation);
394 GNUNET_assert (NULL != peer);
395 GNUNET_assert (NULL == peer1.peer);
397 GNUNET_TESTBED_operation_done (peer1.operation);
398 result = PEER1_CREATED;
399 peer1.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
402 GNUNET_assert (NULL != peer2.operation);
403 GNUNET_assert (NULL != peer);
404 GNUNET_assert (NULL == peer2.peer);
406 GNUNET_TESTBED_operation_done (peer2.operation);
407 result = PEER2_CREATED;
408 peer2.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
411 GNUNET_assert (NULL != peer3.operation);
412 GNUNET_assert (NULL != peer);
413 GNUNET_assert (NULL == peer3.peer);
415 GNUNET_TESTBED_operation_done (peer3.operation);
416 result = PEER3_CREATED;
417 peer3.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
426 * Callback to signal successfull startup of the controller process
428 * @param cls the closure from GNUNET_TESTBED_controller_start()
429 * @param cfg the configuration with which the controller has been started;
430 * NULL if status is not GNUNET_OK
431 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
432 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
435 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status);
439 * Signature of the event handler function called by the
440 * respective event controller.
443 * @param event information about the event
446 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
450 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
451 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
452 GNUNET_assert (NULL == event->details.operation_finished.emsg);
456 GNUNET_assert (NULL == event->details.operation_finished.generic);
457 if (event->details.operation_finished.operation == peer1.operation)
459 GNUNET_TESTBED_operation_done (peer1.operation);
460 peer1.operation = NULL;
463 else if (event->details.operation_finished.operation == peer2.operation)
465 GNUNET_TESTBED_operation_done (peer2.operation);
466 peer2.operation = NULL;
469 else if (event->details.operation_finished.operation == peer3.operation)
471 GNUNET_TESTBED_operation_done (peer3.operation);
472 peer3.operation = NULL;
477 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
480 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
484 GNUNET_assert (NULL == event->details.operation_finished.generic);
485 GNUNET_assert (NULL != common_operation);
486 GNUNET_TESTBED_operation_done (common_operation);
487 common_operation = NULL;
488 result = CONTROLLER2_UP;
490 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
491 &peer_create_cb, NULL);
492 GNUNET_assert (NULL != peer2.operation);
495 GNUNET_assert (NULL == event->details.operation_finished.generic);
496 GNUNET_assert (NULL != common_operation);
497 GNUNET_TESTBED_operation_done (common_operation);
498 common_operation = NULL;
499 result = CONTROLLER3_UP;
501 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
502 &peer_create_cb, NULL);
503 GNUNET_assert (NULL != peer3.operation);
505 case PEERS_1_2_CONNECTED:
506 GNUNET_assert (NULL != event->details.operation_finished.generic);
508 GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
509 GNUNET_TESTBED_operation_done (common_operation);
510 result = CONTROLLER_CFG_ACQUIRED;
512 GNUNET_TESTBED_controller_link (controller1, neighbour2, neighbour1,
515 case CONTROLLER_CFG_ACQUIRED:
516 GNUNET_assert (NULL == event->details.operation_finished.generic);
517 GNUNET_TESTBED_operation_done (common_operation);
518 result = CONTROLLERS_LATERALLY_LINKED;
520 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
527 case GNUNET_TESTBED_ET_PEER_START:
531 GNUNET_assert (event->details.peer_start.host == host);
532 peer1.is_running = GNUNET_YES;
533 GNUNET_TESTBED_operation_done (peer1.operation);
534 peer1.operation = NULL;
535 result = PEER1_STARTED;
537 GNUNET_TESTBED_controller_link (controller1, neighbour1, NULL, cfg,
541 GNUNET_assert (event->details.peer_start.host == neighbour1);
542 peer2.is_running = GNUNET_YES;
543 GNUNET_TESTBED_operation_done (peer2.operation);
544 peer2.operation = NULL;
545 result = PEER2_STARTED;
546 GNUNET_assert (NULL == common_operation);
548 GNUNET_TESTBED_controller_link (controller1, neighbour2, NULL, cfg,
550 GNUNET_assert (NULL != common_operation);
553 GNUNET_assert (event->details.peer_start.host == neighbour2);
554 peer3.is_running = GNUNET_YES;
555 GNUNET_TESTBED_operation_done (peer3.operation);
556 peer3.operation = NULL;
557 result = PEER3_STARTED;
559 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
566 case GNUNET_TESTBED_ET_PEER_STOP:
567 GNUNET_assert (PEERS_CONNECTED_2 == result);
568 if (event->details.peer_stop.peer == peer1.peer)
570 peer1.is_running = GNUNET_NO;
571 GNUNET_TESTBED_operation_done (peer1.operation);
573 else if (event->details.peer_stop.peer == peer2.peer)
575 peer2.is_running = GNUNET_NO;
576 GNUNET_TESTBED_operation_done (peer2.operation);
578 else if (event->details.peer_stop.peer == peer3.peer)
580 peer3.is_running = GNUNET_NO;
581 GNUNET_TESTBED_operation_done (peer3.operation);
585 if ((GNUNET_NO == peer1.is_running) &&
586 (GNUNET_NO == peer2.is_running) &&
587 (GNUNET_NO == peer3.is_running))
589 result = PEERS_STOPPED;
590 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
591 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
592 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
595 case GNUNET_TESTBED_ET_CONNECT:
596 GNUNET_assert (NULL == peer1.operation);
597 GNUNET_assert (NULL == peer2.operation);
598 GNUNET_assert (NULL == peer3.operation);
602 GNUNET_assert (NULL != common_operation);
603 GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer) &&
604 (event->details.peer_connect.peer2 == peer2.peer));
606 case PEERS_2_3_CONNECTED:
607 GNUNET_assert (NULL != common_operation);
608 GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer) &&
609 (event->details.peer_connect.peer2 == peer2.peer));
611 case CONTROLLERS_LATERALLY_LINKED:
612 GNUNET_assert (NULL != common_operation);
613 GNUNET_assert ((event->details.peer_connect.peer1 == peer2.peer) &&
614 (event->details.peer_connect.peer2 == peer3.peer));
627 * Callback which will be called to after a host registration succeeded or failed
629 * @param cls the host which has been registered
630 * @param emsg the error message; NULL if host registration is successful
633 registration_comp (void *cls, const char *emsg)
636 if (cls == neighbour1)
638 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
639 GNUNET_assert (NULL != neighbour2);
641 GNUNET_TESTBED_register_host (controller1, neighbour2, ®istration_comp,
643 GNUNET_assert (NULL != reg_handle);
646 GNUNET_assert (cls == neighbour2);
648 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
650 GNUNET_assert (NULL != peer1.operation);
655 * Callback to signal successfull startup of the controller process
657 * @param cls the closure from GNUNET_TESTBED_controller_start()
658 * @param cfg the configuration with which the controller has been started;
659 * NULL if status is not GNUNET_OK
660 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
661 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
664 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
668 GNUNET_assert (GNUNET_OK == status);
670 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
671 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
672 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
673 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
678 GNUNET_TESTBED_controller_connect (config, host, event_mask, &controller_cb,
680 GNUNET_assert (NULL != controller1);
681 result = CONTROLLER1_UP;
682 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
683 GNUNET_assert (NULL != neighbour1);
685 GNUNET_TESTBED_register_host (controller1, neighbour1, ®istration_comp,
687 GNUNET_assert (NULL != reg_handle);
700 * @param args arguments passed to GNUNET_PROGRAM_run
701 * @param cfgfile the path to configuration file
702 * @param cfg the configuration file handle
705 run (void *cls, char *const *args, const char *cfgfile,
706 const struct GNUNET_CONFIGURATION_Handle *config)
708 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
709 GNUNET_assert (NULL != host);
710 cfg = GNUNET_CONFIGURATION_dup (config);
711 cp1 = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
714 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
715 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
721 * Function to check if password-less SSH logins to given ip work
723 * @param host_str numeric representation of the host's ip
724 * @return GNUNET_YES if password-less SSH login to the given host works;
728 check_ssh (char *host_str)
730 char *const remote_args[] = {
731 "ssh", "-o", "BatchMode=yes", "-o", "CheckHostIP=no", "-q",
732 host_str, "echo", "SSH", "works", NULL
734 struct GNUNET_OS_Process *auxp;
735 enum GNUNET_OS_ProcessStatusType type;
740 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
741 NULL, "ssh", remote_args);
742 GNUNET_assert (NULL != auxp);
745 ret = GNUNET_OS_process_status (auxp, &type, &code);
746 GNUNET_assert (GNUNET_SYSERR != ret);
749 while (GNUNET_NO == ret);
750 (void) GNUNET_OS_process_wait (auxp);
751 GNUNET_OS_process_destroy (auxp);
752 return (0 != code) ? GNUNET_NO : GNUNET_YES;
760 main (int argc, char **argv)
762 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
763 "-c", "test_testbed_api.conf",
766 struct GNUNET_GETOPT_CommandLineOption options[] = {
767 GNUNET_GETOPT_OPTION_END
771 if (GNUNET_YES != check_ssh ("127.0.0.1"))
775 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
776 "test_testbed_api_3peers_3controllers", "nohelp",
777 options, &run, NULL);
778 if ((GNUNET_OK != ret) || (SUCCESS != result))
784 "Unable to run the test as this system is not configured "
785 "to use password less SSH logins to localhost.\n"
786 "Marking test as successful\n");
790 /* end of test_testbed_api_3peers_3controllers.c */