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 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
285 do_shutdown (cls, tc);
290 * Callback to be called when an operation is completed
292 * @param cls the callback closure from functions generating an operation
293 * @param op the operation that has been finished
294 * @param emsg error message in case the operation has failed; will be NULL if
295 * operation has executed successfully.
298 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
302 * task for delaying a connect
305 * @param tc the task context
308 do_delayed_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
310 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
311 GNUNET_assert (NULL == common_operation);
312 common_operation = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL,
313 peer1.peer, peer2.peer);
318 * Callback to be called when an operation is completed
320 * @param cls the callback closure from functions generating an operation
321 * @param op the operation that has been finished
322 * @param emsg error message in case the operation has failed; will be NULL if
323 * operation has executed successfully.
326 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
328 GNUNET_assert (common_operation == op);
332 GNUNET_assert (NULL == peer1.operation);
333 GNUNET_assert (NULL == peer2.operation);
334 GNUNET_assert (NULL != common_operation);
335 GNUNET_TESTBED_operation_done (common_operation);
336 common_operation = NULL;
337 result = PEERS_1_2_CONNECTED;
338 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
340 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
343 case PEERS_1_2_CONNECTED:
344 GNUNET_assert (NULL != common_operation);
345 GNUNET_TESTBED_operation_done (common_operation);
346 common_operation = NULL;
347 result = PEERS_2_3_CONNECTED;
348 delayed_connect_task =
349 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3),
350 &do_delayed_connect, NULL);
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);
371 * Functions of this signature are called when a peer has been successfully
375 * @param peer the handle for the created peer; NULL on any error during
377 * @param emsg NULL if peer is not NULL; else MAY contain the error description
380 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
385 GNUNET_assert (NULL != peer1.operation);
386 GNUNET_assert (NULL != peer);
387 GNUNET_assert (NULL == peer1.peer);
389 GNUNET_TESTBED_operation_done (peer1.operation);
390 result = PEER1_CREATED;
391 peer1.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
394 GNUNET_assert (NULL != peer2.operation);
395 GNUNET_assert (NULL != peer);
396 GNUNET_assert (NULL == peer2.peer);
398 GNUNET_TESTBED_operation_done (peer2.operation);
399 result = PEER2_CREATED;
400 peer2.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
403 GNUNET_assert (NULL != peer3.operation);
404 GNUNET_assert (NULL != peer);
405 GNUNET_assert (NULL == peer3.peer);
407 GNUNET_TESTBED_operation_done (peer3.operation);
408 result = PEER3_CREATED;
409 peer3.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
418 * Callback to signal successfull startup of the controller process
420 * @param cls the closure from GNUNET_TESTBED_controller_start()
421 * @param cfg the configuration with which the controller has been started;
422 * NULL if status is not GNUNET_OK
423 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
424 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
427 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status);
431 * Signature of the event handler function called by the
432 * respective event controller.
435 * @param event information about the event
438 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
442 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
443 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
444 GNUNET_assert (NULL == event->details.operation_finished.emsg);
448 GNUNET_assert (NULL == event->details.operation_finished.generic);
449 if (event->details.operation_finished.operation == peer1.operation)
451 GNUNET_TESTBED_operation_done (peer1.operation);
452 peer1.operation = NULL;
455 else if (event->details.operation_finished.operation == peer2.operation)
457 GNUNET_TESTBED_operation_done (peer2.operation);
458 peer2.operation = NULL;
461 else if (event->details.operation_finished.operation == peer3.operation)
463 GNUNET_TESTBED_operation_done (peer3.operation);
464 peer3.operation = NULL;
469 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
472 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
476 GNUNET_assert (NULL == event->details.operation_finished.generic);
477 GNUNET_assert (NULL != common_operation);
478 GNUNET_TESTBED_operation_done (common_operation);
479 common_operation = NULL;
480 result = CONTROLLER2_UP;
482 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
483 &peer_create_cb, NULL);
484 GNUNET_assert (NULL != peer2.operation);
487 GNUNET_assert (NULL == event->details.operation_finished.generic);
488 GNUNET_assert (NULL != common_operation);
489 GNUNET_TESTBED_operation_done (common_operation);
490 common_operation = NULL;
491 result = CONTROLLER3_UP;
493 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
494 &peer_create_cb, NULL);
495 GNUNET_assert (NULL != peer3.operation);
501 case GNUNET_TESTBED_ET_PEER_START:
505 GNUNET_assert (event->details.peer_start.host == host);
506 peer1.is_running = GNUNET_YES;
507 GNUNET_TESTBED_operation_done (peer1.operation);
508 peer1.operation = NULL;
509 result = PEER1_STARTED;
511 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL, cfg,
515 GNUNET_assert (event->details.peer_start.host == neighbour1);
516 peer2.is_running = GNUNET_YES;
517 GNUNET_TESTBED_operation_done (peer2.operation);
518 peer2.operation = NULL;
519 result = PEER2_STARTED;
520 GNUNET_assert (NULL == common_operation);
522 GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL, cfg,
524 GNUNET_assert (NULL != common_operation);
527 GNUNET_assert (event->details.peer_start.host == neighbour2);
528 peer3.is_running = GNUNET_YES;
529 GNUNET_TESTBED_operation_done (peer3.operation);
530 peer3.operation = NULL;
531 result = PEER3_STARTED;
533 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
540 case GNUNET_TESTBED_ET_PEER_STOP:
541 GNUNET_assert (PEERS_CONNECTED_2 == result);
542 if (event->details.peer_stop.peer == peer1.peer)
544 peer1.is_running = GNUNET_NO;
545 GNUNET_TESTBED_operation_done (peer1.operation);
547 else if (event->details.peer_stop.peer == peer2.peer)
549 peer2.is_running = GNUNET_NO;
550 GNUNET_TESTBED_operation_done (peer2.operation);
552 else if (event->details.peer_stop.peer == peer3.peer)
554 peer3.is_running = GNUNET_NO;
555 GNUNET_TESTBED_operation_done (peer3.operation);
559 if ((GNUNET_NO == peer1.is_running) &&
560 (GNUNET_NO == peer2.is_running) &&
561 (GNUNET_NO == peer3.is_running))
563 result = PEERS_STOPPED;
564 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
565 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
566 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
569 case GNUNET_TESTBED_ET_CONNECT:
570 GNUNET_assert (NULL == peer1.operation);
571 GNUNET_assert (NULL == peer2.operation);
572 GNUNET_assert (NULL == peer3.operation);
576 GNUNET_assert (NULL != common_operation);
577 GNUNET_assert ((event->details.peer_connect.peer1 == peer2.peer) &&
578 (event->details.peer_connect.peer2 == peer1.peer));
580 case PEERS_2_3_CONNECTED:
581 GNUNET_assert (NULL != common_operation);
582 GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer) &&
583 (event->details.peer_connect.peer2 == peer2.peer));
585 case PEERS_1_2_CONNECTED:
586 GNUNET_assert (NULL != common_operation);
587 GNUNET_assert ((event->details.peer_connect.peer1 == peer2.peer) &&
588 (event->details.peer_connect.peer2 == peer3.peer));
601 * Callback which will be called to after a host registration succeeded or failed
603 * @param cls the host which has been registered
604 * @param emsg the error message; NULL if host registration is successful
607 registration_comp (void *cls, const char *emsg)
610 if (cls == neighbour1)
612 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
613 GNUNET_assert (NULL != neighbour2);
615 GNUNET_TESTBED_register_host (controller1, neighbour2, ®istration_comp,
617 GNUNET_assert (NULL != reg_handle);
620 GNUNET_assert (cls == neighbour2);
622 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
624 GNUNET_assert (NULL != peer1.operation);
629 * Callback to signal successfull startup of the controller process
631 * @param cls the closure from GNUNET_TESTBED_controller_start()
632 * @param cfg the configuration with which the controller has been started;
633 * NULL if status is not GNUNET_OK
634 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
635 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
638 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
642 GNUNET_assert (GNUNET_OK == status);
644 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
645 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
646 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
647 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
652 GNUNET_TESTBED_controller_connect (config, host, event_mask, &controller_cb,
654 GNUNET_assert (NULL != controller1);
655 result = CONTROLLER1_UP;
656 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
657 GNUNET_assert (NULL != neighbour1);
659 GNUNET_TESTBED_register_host (controller1, neighbour1, ®istration_comp,
661 GNUNET_assert (NULL != reg_handle);
673 * @param args arguments passed to GNUNET_PROGRAM_run
674 * @param cfgfile the path to configuration file
675 * @param cfg the configuration file handle
678 run (void *cls, char *const *args, const char *cfgfile,
679 const struct GNUNET_CONFIGURATION_Handle *config)
681 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
682 GNUNET_assert (NULL != host);
683 cfg = GNUNET_CONFIGURATION_dup (config);
684 cp1 = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
687 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
688 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
694 * Function to check if
695 * 1. Password-less SSH logins to given ip work
696 * 2. gnunet-helper-testbed is found on the PATH on the remote side
698 * @param host_str numeric representation of the host's ip
699 * @return GNUNET_YES if password-less SSH login to the given host works;
703 check_ssh (char *host_str)
705 char *const remote_args[] = {
706 "ssh", "-o", "BatchMode=yes", "-o", "CheckHostIP=no",
707 "-o", "NoHostAuthenticationForLocalhost=yes", "-q",
708 host_str, "which", "gnunet-helper-testbed", NULL
710 // FIXME: the above no longer works with libexec/-installation!
711 struct GNUNET_OS_Process *auxp;
712 enum GNUNET_OS_ProcessStatusType type;
717 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
718 NULL, "ssh", remote_args);
719 GNUNET_assert (NULL != auxp);
722 ret = GNUNET_OS_process_status (auxp, &type, &code);
723 GNUNET_assert (GNUNET_SYSERR != ret);
726 while (GNUNET_NO == ret);
727 (void) GNUNET_OS_process_wait (auxp);
728 GNUNET_OS_process_destroy (auxp);
729 return (0 != code) ? GNUNET_NO : GNUNET_YES;
737 main (int argc, char **argv)
739 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
740 "-c", "test_testbed_api.conf",
743 struct GNUNET_GETOPT_CommandLineOption options[] = {
744 GNUNET_GETOPT_OPTION_END
748 if (GNUNET_YES != check_ssh ("127.0.0.1"))
752 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
753 "test_testbed_api_3peers_3controllers", "nohelp",
754 options, &run, NULL);
755 if ((GNUNET_OK != ret) || (SUCCESS != result))
761 "Unable to run the test as this system is not configured "
762 "to use password less SSH logins to localhost.\n"
763 "Marking test as successful\n");
767 /* end of test_testbed_api_3peers_3controllers.c */