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 * C1 is the master controller and C2, C3 are slave controllers. C2 links to C3
40 #include "gnunet_util_lib.h"
41 #include "gnunet_testing_lib-new.h"
42 #include "gnunet_testbed_service.h"
46 * Generic logging shortcut
48 #define LOG(kind,...) \
49 GNUNET_log (kind, __VA_ARGS__)
52 * Relative time seconds shorthand
54 #define TIME_REL_SECS(sec) \
55 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
66 struct GNUNET_TESTBED_Peer *peer;
69 * Operations involving this peer
71 struct GNUNET_TESTBED_Operation *operation;
74 * set to GNUNET_YES when peer is started
82 static struct GNUNET_TESTBED_Host *host;
85 * The controller process of one controller
87 static struct GNUNET_TESTBED_ControllerProc *cp1;
92 static struct GNUNET_TESTBED_Host *neighbour1;
95 * Another neighbouring host
97 static struct GNUNET_TESTBED_Host *neighbour2;
100 * Handle for neighbour registration
102 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
105 * The controller handle of one controller
107 static struct GNUNET_TESTBED_Controller *controller1;
110 * The controller handle of another controller
112 static struct GNUNET_TESTBED_Controller *controller2;
117 static struct PeerContext peer1;
122 static struct PeerContext peer2;
127 static struct PeerContext peer3;
130 * Handle to starting configuration
132 static struct GNUNET_CONFIGURATION_Handle *cfg;
135 * Handle to controller2 configuration, used to establish lateral link from
138 static struct GNUNET_CONFIGURATION_Handle *cfg2;
141 * Handle to operations involving both peers
143 static struct GNUNET_TESTBED_Operation *common_operation;
146 * Abort task identifier
148 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
151 * Delayed connect job identifier
153 static GNUNET_SCHEDULER_TaskIdentifier delayed_connect_task;
156 * Different stages in testing
167 * Controller 1 has started
182 * Controller 2 has started
197 * Controller 3 has started
212 * peers are connected
217 * Peers are connected once again (this should not fail as they are already connected)
227 * Final success stage
235 static enum Stage result;
241 * @param tc the task context
244 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
247 GNUNET_SCHEDULER_cancel (abort_task);
248 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
249 if (NULL != reg_handle)
250 GNUNET_TESTBED_cancel_registration (reg_handle);
251 if (NULL != controller1)
252 GNUNET_TESTBED_controller_disconnect (controller1);
253 if (NULL != controller2)
254 GNUNET_TESTBED_controller_disconnect (controller2);
255 GNUNET_CONFIGURATION_destroy (cfg);
257 GNUNET_CONFIGURATION_destroy (cfg2);
259 GNUNET_TESTBED_controller_stop (cp1);
261 GNUNET_TESTBED_host_destroy (host);
262 if (NULL != neighbour1)
263 GNUNET_TESTBED_host_destroy (neighbour1);
264 if (NULL != neighbour2)
265 GNUNET_TESTBED_host_destroy (neighbour2);
270 * abort task to run on test timed out
273 * @param tc the task context
276 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
278 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
279 abort_task = GNUNET_SCHEDULER_NO_TASK;
280 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
281 do_shutdown (cls, tc);
286 * Callback to be called when an operation is completed
288 * @param cls the callback closure from functions generating an operation
289 * @param op the operation that has been finished
290 * @param emsg error message in case the operation has failed; will be NULL if
291 * operation has executed successfully.
294 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
298 * task for delaying a connect
301 * @param tc the task context
304 do_delayed_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
306 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
307 GNUNET_assert (NULL == common_operation);
308 common_operation = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL,
309 peer1.peer, peer2.peer);
314 * Callback to be called when an operation is completed
316 * @param cls the callback closure from functions generating an operation
317 * @param op the operation that has been finished
318 * @param emsg error message in case the operation has failed; will be NULL if
319 * operation has executed successfully.
322 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
324 GNUNET_assert (common_operation == op);
328 GNUNET_assert (NULL == peer1.operation);
329 GNUNET_assert (NULL == peer2.operation);
330 GNUNET_assert (NULL != common_operation);
331 GNUNET_TESTBED_operation_done (common_operation);
332 common_operation = NULL;
333 result = PEERS_CONNECTED;
334 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
335 delayed_connect_task =
336 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3),
337 &do_delayed_connect, NULL);
339 case PEERS_CONNECTED:
340 GNUNET_assert (NULL == peer1.operation);
341 GNUNET_assert (NULL == peer2.operation);
342 GNUNET_assert (NULL != common_operation);
343 GNUNET_TESTBED_operation_done (common_operation);
344 common_operation = NULL;
345 result = PEERS_CONNECTED_2;
346 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
347 peer1.operation = GNUNET_TESTBED_peer_stop (peer1.peer, NULL, NULL);
348 peer2.operation = GNUNET_TESTBED_peer_stop (peer2.peer, NULL, NULL);
349 peer3.operation = GNUNET_TESTBED_peer_stop (peer3.peer, NULL, NULL);
358 * Functions of this signature are called when a peer has been successfully
362 * @param peer the handle for the created peer; NULL on any error during
364 * @param emsg NULL if peer is not NULL; else MAY contain the error description
367 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
372 GNUNET_assert (NULL != peer1.operation);
373 GNUNET_assert (NULL != peer);
374 GNUNET_assert (NULL == peer1.peer);
376 GNUNET_TESTBED_operation_done (peer1.operation);
377 result = PEER1_CREATED;
378 peer1.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
381 GNUNET_assert (NULL != peer2.operation);
382 GNUNET_assert (NULL != peer);
383 GNUNET_assert (NULL == peer2.peer);
385 GNUNET_TESTBED_operation_done (peer2.operation);
386 result = PEER2_CREATED;
387 peer2.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
390 GNUNET_assert (NULL != peer3.operation);
391 GNUNET_assert (NULL != peer);
392 GNUNET_assert (NULL == peer3.peer);
394 GNUNET_TESTBED_operation_done (peer3.operation);
395 result = PEER3_CREATED;
396 peer3.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
405 * Callback to signal successfull startup of the controller process
407 * @param cls the closure from GNUNET_TESTBED_controller_start()
408 * @param cfg the configuration with which the controller has been started;
409 * NULL if status is not GNUNET_OK
410 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
411 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
414 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status);
418 * Signature of the event handler function called by the
419 * respective event controller.
422 * @param event information about the event
425 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
429 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
430 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
431 GNUNET_assert (NULL == event->details.operation_finished.emsg);
432 GNUNET_assert (NULL == event->details.operation_finished.generic);
436 if (event->details.operation_finished.operation == peer1.operation)
438 GNUNET_TESTBED_operation_done (peer1.operation);
439 peer1.operation = NULL;
442 else if (event->details.operation_finished.operation == peer2.operation)
444 GNUNET_TESTBED_operation_done (peer2.operation);
445 peer2.operation = NULL;
448 else if (event->details.operation_finished.operation == peer3.operation)
450 GNUNET_TESTBED_operation_done (peer3.operation);
451 peer3.operation = NULL;
456 if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
459 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
463 GNUNET_assert (NULL != common_operation);
464 GNUNET_TESTBED_operation_done (common_operation);
465 common_operation = NULL;
466 result = CONTROLLER2_UP;
468 GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
469 &peer_create_cb, NULL);
470 GNUNET_assert (NULL != peer2.operation);
473 GNUNET_assert (NULL != common_operation);
474 GNUNET_TESTBED_operation_done (common_operation);
475 common_operation = NULL;
476 result = CONTROLLER3_UP;
478 GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
479 &peer_create_cb, NULL);
480 GNUNET_assert (NULL != peer3.operation);
486 case GNUNET_TESTBED_ET_PEER_START:
490 GNUNET_assert (event->details.peer_start.host == host);
491 peer1.is_running = GNUNET_YES;
492 GNUNET_TESTBED_operation_done (peer1.operation);
493 peer1.operation = NULL;
494 result = PEER1_STARTED;
496 GNUNET_TESTBED_controller_link (controller1, neighbour1, NULL, cfg,
500 GNUNET_assert (event->details.peer_start.host == neighbour1);
501 peer2.is_running = GNUNET_YES;
502 GNUNET_TESTBED_operation_done (peer2.operation);
503 peer2.operation = NULL;
504 result = PEER2_STARTED;
505 GNUNET_assert (NULL == common_operation);
507 GNUNET_TESTBED_controller_link (controller1, neighbour2, NULL, cfg,
509 GNUNET_assert (NULL != common_operation);
512 GNUNET_assert (event->details.peer_start.host == neighbour2);
513 peer3.is_running = GNUNET_YES;
514 GNUNET_TESTBED_operation_done (peer3.operation);
515 peer3.operation = NULL;
516 result = PEER3_STARTED;
518 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
525 case GNUNET_TESTBED_ET_PEER_STOP:
526 GNUNET_assert (PEERS_CONNECTED_2 == result);
527 if (event->details.peer_stop.peer == peer1.peer)
529 peer1.is_running = GNUNET_NO;
530 GNUNET_TESTBED_operation_done (peer1.operation);
532 else if (event->details.peer_stop.peer == peer2.peer)
534 peer2.is_running = GNUNET_NO;
535 GNUNET_TESTBED_operation_done (peer2.operation);
537 else if (event->details.peer_stop.peer == peer3.peer)
539 peer3.is_running = GNUNET_NO;
540 GNUNET_TESTBED_operation_done (peer3.operation);
544 if ((GNUNET_NO == peer1.is_running) &&
545 (GNUNET_NO == peer2.is_running) &&
546 (GNUNET_NO == peer3.is_running))
548 result = PEERS_STOPPED;
549 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
550 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
551 peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
554 case GNUNET_TESTBED_ET_CONNECT:
558 case PEERS_CONNECTED:
559 GNUNET_assert (NULL == peer1.operation);
560 GNUNET_assert (NULL == peer2.operation);
561 GNUNET_assert (NULL == peer3.operation);
562 GNUNET_assert (NULL != common_operation);
563 /* GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer) && */
564 /* (event->details.peer_connect.peer2 == peer2.peer)); */
577 * Callback which will be called to after a host registration succeeded or failed
579 * @param cls the host which has been registered
580 * @param emsg the error message; NULL if host registration is successful
583 registration_comp (void *cls, const char *emsg)
586 if (cls == neighbour1)
588 neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
589 GNUNET_assert (NULL != neighbour2);
591 GNUNET_TESTBED_register_host (controller1, neighbour2, ®istration_comp,
593 GNUNET_assert (NULL != reg_handle);
596 GNUNET_assert (cls == neighbour2);
598 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
600 GNUNET_assert (NULL != peer1.operation);
605 * Callback to signal successfull startup of the controller process
607 * @param cls the closure from GNUNET_TESTBED_controller_start()
608 * @param cfg the configuration with which the controller has been started;
609 * NULL if status is not GNUNET_OK
610 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
611 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
614 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
618 GNUNET_assert (GNUNET_OK == status);
620 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
621 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
622 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
623 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
628 GNUNET_TESTBED_controller_connect (config, host, event_mask, &controller_cb,
630 GNUNET_assert (NULL != controller1);
631 result = CONTROLLER1_UP;
632 neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
633 GNUNET_assert (NULL != neighbour1);
635 GNUNET_TESTBED_register_host (controller1, neighbour1, ®istration_comp,
637 GNUNET_assert (NULL != reg_handle);
650 * @param args arguments passed to GNUNET_PROGRAM_run
651 * @param cfgfile the path to configuration file
652 * @param cfg the configuration file handle
655 run (void *cls, char *const *args, const char *cfgfile,
656 const struct GNUNET_CONFIGURATION_Handle *config)
658 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
659 GNUNET_assert (NULL != host);
660 cfg = GNUNET_CONFIGURATION_dup (config);
661 cp1 = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
664 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
665 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
671 * Function to check if password-less SSH logins to given ip work
673 * @param host_str numeric representation of the host's ip
674 * @return GNUNET_YES if password-less SSH login to the given host works;
678 check_ssh (char *host_str)
680 char *const remote_args[] = {
681 "ssh", "-o", "BatchMode=yes", "-o", "CheckHostIP=no", "-q",
682 host_str, "echo", "SSH", "works", NULL
684 struct GNUNET_OS_Process *auxp;
685 enum GNUNET_OS_ProcessStatusType type;
690 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
691 NULL, "ssh", remote_args);
692 GNUNET_assert (NULL != auxp);
695 ret = GNUNET_OS_process_status (auxp, &type, &code);
696 GNUNET_assert (GNUNET_SYSERR != ret);
699 while (GNUNET_NO == ret);
700 (void) GNUNET_OS_process_wait (auxp);
701 GNUNET_OS_process_destroy (auxp);
702 return (0 != code) ? GNUNET_NO : GNUNET_YES;
710 main (int argc, char **argv)
712 char *const argv2[] = { "test_testbed_api_3peers_3controllers",
713 "-c", "test_testbed_api.conf",
716 struct GNUNET_GETOPT_CommandLineOption options[] = {
717 GNUNET_GETOPT_OPTION_END
721 //GNUNET_OS_network_interfaces_list (&interface_processor, NULL);
723 if (GNUNET_YES != check_ssh ("127.0.0.1"))
727 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
728 "test_testbed_api_3peers_3controllers", "nohelp",
729 options, &run, NULL);
730 if ((GNUNET_OK != ret) || (SUCCESS != result))
735 (void) PRINTF ("Unable to run the test as this system is not configured "
736 "to use password less SSH logins to localhost.\n"
737 "Marking test as successful\n");
741 /* end of test_testbed_api_3peers_3controllers.c */