2 This file is part of GNUnet
3 (C) 2008--2013 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_controllerlink.c
23 * @brief testcase for testing controller to subcontroller linking
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
29 * The controller architecture we try to achieve in this test case:
34 * Slave Controller 1---------Slave Controller 3
41 #include "gnunet_util_lib.h"
42 #include "gnunet_testing_lib.h"
43 #include "gnunet_testbed_service.h"
46 * Generic logging shortcut
48 #define LOG(kind,...) \
49 GNUNET_log (kind, __VA_ARGS__)
52 * Debug logging shorthand
54 #define LOG_DEBUG(...) \
55 LOG(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
58 * Different stages in testing
69 * Master controller has started
74 * A peer has been created on master
76 MASTER_PEER_CREATE_SUCCESS,
79 * Peer on master controller has been started successfully.
81 MASTER_PEER_START_SUCCESS,
84 * The first slave has been registered at master controller
89 * The second slave has been registered at the master controller
94 * Link from master to slave 1 has been successfully created
99 * Peer create on slave 1 successful
101 SLAVE1_PEER_CREATE_SUCCESS,
104 * Peer startup on slave 1 successful
106 SLAVE1_PEER_START_SUCCESS,
109 * Link from slave 1 to slave 2 has been successfully created.
114 * Peer create on slave 2 successful
116 SLAVE2_PEER_CREATE_SUCCESS,
119 * Peer on slave 1 successfully stopped
121 SLAVE1_PEER_STOP_SUCCESS,
124 * Peer startup on slave 2 successful
126 SLAVE2_PEER_START_SUCCESS,
129 * Try to connect peers on master and slave 2.
131 MASTER_SLAVE2_PEERS_CONNECTED,
134 * Slave 3 has successfully registered
139 * Slave 3 has successfully started
144 * Peer created on slave 3
146 SLAVE3_PEER_CREATE_SUCCESS,
149 * Peer started at slave 3
151 SLAVE3_PEER_START_SUCCESS,
154 * Try to connect peers on slave2 and slave3
156 SLAVE2_SLAVE3_PEERS_CONNECTED,
159 * Peer on slave 2 successfully stopped
161 SLAVE2_PEER_STOP_SUCCESS,
164 * Peer destroy on slave 1 successful
166 SLAVE1_PEER_DESTROY_SUCCESS,
169 * Peer destory on slave 2 successful
171 SLAVE2_PEER_DESTROY_SUCCESS,
174 * The configuration of slave 3 is acquired
176 SLAVE3_GET_CONFIG_SUCCESS,
179 * Slave 1 has linked to slave 3;
184 * Destory master peer and mark test as success
189 * Marks test as skipped
195 * Host for running master controller
197 static struct GNUNET_TESTBED_Host *host;
200 * The master controller process
202 static struct GNUNET_TESTBED_ControllerProc *cp;
205 * Handle to master controller
207 static struct GNUNET_TESTBED_Controller *mc;
210 * Slave host for running slave controller
212 static struct GNUNET_TESTBED_Host *slave;
215 * Another slave host for running another slave controller
217 static struct GNUNET_TESTBED_Host *slave2;
222 static struct GNUNET_TESTBED_Host *slave3;
225 * Slave host registration handle
227 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
230 * Handle to global configuration
232 static struct GNUNET_CONFIGURATION_Handle *cfg;
235 * Configuration of slave 3 controller
237 static struct GNUNET_CONFIGURATION_Handle *cfg3;
242 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
245 * Operation handle for linking controllers
247 static struct GNUNET_TESTBED_Operation *op;
250 * Handle to peer started at slave 1
252 static struct GNUNET_TESTBED_Peer *slave1_peer;
255 * Handle to peer started at slave 2
257 static struct GNUNET_TESTBED_Peer *slave2_peer;
260 * Handle to peer started at slave 2
262 static struct GNUNET_TESTBED_Peer *slave3_peer;
265 * Handle to a peer started at master controller
267 static struct GNUNET_TESTBED_Peer *master_peer;
270 * The handle for whether a host is habitable or not
272 struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
275 * The task handle for the delay task
277 GNUNET_SCHEDULER_TaskIdentifier delay_task_id;
285 * Global testing status
287 static enum Stage result;
290 * shortcut to exit during failure
292 #define FAIL_TEST(cond) do { \
295 if (GNUNET_SCHEDULER_NO_TASK != abort_task) \
296 GNUNET_SCHEDULER_cancel (abort_task); \
297 abort_task = GNUNET_SCHEDULER_NO_TASK; \
298 GNUNET_SCHEDULER_add_now (do_shutdown, NULL); \
308 * @param tc the task context
311 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
313 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
314 GNUNET_SCHEDULER_cancel (abort_task);
315 if (GNUNET_SCHEDULER_NO_TASK != delay_task_id)
317 GNUNET_SCHEDULER_cancel (delay_task_id);
318 delay_task_id = GNUNET_SCHEDULER_NO_TASK;
320 if (NULL != hc_handle)
321 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
323 GNUNET_TESTBED_controller_disconnect (mc);
325 GNUNET_TESTBED_controller_stop (cp);
327 GNUNET_TESTBED_host_destroy (slave3);
329 GNUNET_TESTBED_host_destroy (slave2);
331 GNUNET_TESTBED_host_destroy (slave);
333 GNUNET_TESTBED_host_destroy (host);
335 GNUNET_CONFIGURATION_destroy (cfg);
337 GNUNET_CONFIGURATION_destroy (cfg3);
339 GNUNET_TESTBED_cancel_registration (rh);
344 * abort task to run on test timed out
347 * @param tc the task context
350 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
352 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
353 abort_task = GNUNET_SCHEDULER_NO_TASK;
354 do_shutdown (cls, tc);
365 do_abort_now (void *cls)
367 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
368 GNUNET_SCHEDULER_cancel (abort_task);
369 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
374 * Callback which will be called to after a host registration succeeded or failed
376 * @param cls the host which has been registered
377 * @param emsg the error message; NULL if host registration is successful
380 registration_cont (void *cls, const char *emsg);
384 * Task for inserting delay between tests
390 delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
392 delay_task_id = GNUNET_SCHEDULER_NO_TASK;
395 case SLAVE2_PEER_CREATE_SUCCESS:
396 op = GNUNET_TESTBED_peer_stop (NULL, slave1_peer, NULL, NULL);
397 FAIL_TEST (NULL != op);
399 case MASTER_SLAVE2_PEERS_CONNECTED:
400 slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, cfg, 0);
401 rh = GNUNET_TESTBED_register_host (mc, slave3, ®istration_cont, NULL);
403 case SLAVE2_SLAVE3_PEERS_CONNECTED:
404 op = GNUNET_TESTBED_peer_stop (NULL, slave2_peer, NULL, NULL);
405 FAIL_TEST (NULL != op);
414 * Functions of this signature are called when a peer has been successfully
417 * @param cls the closure from GNUNET_TESTBED_peer_create()
418 * @param peer the handle for the created peer; NULL on any error during
420 * @param emsg NULL if peer is not NULL; else MAY contain the error description
423 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
425 FAIL_TEST (NULL != peer);
426 FAIL_TEST (NULL == emsg);
430 result = MASTER_PEER_CREATE_SUCCESS;
432 GNUNET_TESTBED_operation_done (op);
433 op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
435 case SLAVE1_LINK_SUCCESS:
436 result = SLAVE1_PEER_CREATE_SUCCESS;
438 GNUNET_TESTBED_operation_done (op);
439 op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
441 case SLAVE2_LINK_SUCCESS:
442 result = SLAVE2_PEER_CREATE_SUCCESS;
444 GNUNET_TESTBED_operation_done (op);
446 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
447 (GNUNET_TIME_UNIT_SECONDS, 1),
452 result = SLAVE3_PEER_CREATE_SUCCESS;
454 GNUNET_TESTBED_operation_done (op);
455 op = GNUNET_TESTBED_peer_start (NULL, slave3_peer, NULL, NULL);
460 FAIL_TEST (NULL != op);
465 * Checks the event if it is an operation finished event and if indicates a
466 * successfull completion of operation
468 * @param event the event information to check
471 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
473 FAIL_TEST (NULL != event);
474 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
475 FAIL_TEST (event->op == op);
476 FAIL_TEST (NULL == event->op_cls);
477 FAIL_TEST (NULL == event->details.operation_finished.emsg);
478 FAIL_TEST (NULL == event->details.operation_finished.generic);
483 * Signature of the event handler function called by the
484 * respective event controller.
487 * @param event information about the event
490 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
494 case SLAVE2_REGISTERED:
495 check_operation_success (event);
496 GNUNET_TESTBED_operation_done (op);
498 result = SLAVE1_LINK_SUCCESS;
499 FAIL_TEST (NULL != slave2);
500 FAIL_TEST (NULL != slave);
501 op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
502 FAIL_TEST (NULL != op);
504 case SLAVE1_PEER_START_SUCCESS:
505 check_operation_success (event);
506 GNUNET_TESTBED_operation_done (op);
507 result = SLAVE2_LINK_SUCCESS;
508 op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
509 FAIL_TEST (NULL != op);
511 case MASTER_PEER_CREATE_SUCCESS:
512 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
513 FAIL_TEST (event->details.peer_start.host == host);
514 FAIL_TEST (event->details.peer_start.peer == master_peer);
515 GNUNET_TESTBED_operation_done (op);
516 result = MASTER_PEER_START_SUCCESS;
517 slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, cfg, 0);
518 FAIL_TEST (NULL != slave);
519 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
520 FAIL_TEST (NULL != rh);
522 case SLAVE1_PEER_CREATE_SUCCESS:
523 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
524 FAIL_TEST (event->details.peer_start.host == slave);
525 FAIL_TEST (event->details.peer_start.peer == slave1_peer);
526 GNUNET_TESTBED_operation_done (op);
527 result = SLAVE1_PEER_START_SUCCESS;
528 op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, GNUNET_YES);
530 case SLAVE2_PEER_CREATE_SUCCESS:
531 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
532 FAIL_TEST (event->details.peer_stop.peer == slave1_peer);
533 GNUNET_TESTBED_operation_done (op);
534 result = SLAVE1_PEER_STOP_SUCCESS;
535 op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
536 FAIL_TEST (NULL != op);
538 case SLAVE3_PEER_CREATE_SUCCESS:
539 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
540 FAIL_TEST (event->details.peer_start.host == slave3);
541 FAIL_TEST (event->details.peer_start.peer == slave3_peer);
542 GNUNET_TESTBED_operation_done (op);
543 result = SLAVE3_PEER_START_SUCCESS;
544 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, slave2_peer,
546 FAIL_TEST (NULL != op);
548 case SLAVE3_PEER_START_SUCCESS:
549 FAIL_TEST (NULL != event);
550 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
551 FAIL_TEST (event->details.peer_connect.peer1 == slave2_peer);
552 FAIL_TEST (event->details.peer_connect.peer2 == slave3_peer);
553 result = SLAVE2_SLAVE3_PEERS_CONNECTED;
554 GNUNET_TESTBED_operation_done (op);
557 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
558 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
561 case SLAVE1_PEER_STOP_SUCCESS:
562 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
563 FAIL_TEST (event->details.peer_start.host == slave2);
564 FAIL_TEST (event->details.peer_start.peer == slave2_peer);
565 GNUNET_TESTBED_operation_done (op);
566 result = SLAVE2_PEER_START_SUCCESS;
567 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
570 case SLAVE2_PEER_START_SUCCESS:
571 FAIL_TEST (NULL != event);
572 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
573 FAIL_TEST (event->details.peer_connect.peer1 == master_peer);
574 FAIL_TEST (event->details.peer_connect.peer2 == slave2_peer);
575 result = MASTER_SLAVE2_PEERS_CONNECTED;
576 GNUNET_TESTBED_operation_done (op);
579 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
580 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
583 case SLAVE2_SLAVE3_PEERS_CONNECTED:
584 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
585 FAIL_TEST (event->details.peer_stop.peer == slave2_peer);
586 GNUNET_TESTBED_operation_done (op);
587 result = SLAVE2_PEER_STOP_SUCCESS;
588 op = GNUNET_TESTBED_peer_destroy (slave1_peer);
589 FAIL_TEST (NULL != op);
591 case SLAVE2_PEER_STOP_SUCCESS:
592 check_operation_success (event);
593 GNUNET_TESTBED_operation_done (op);
594 result = SLAVE1_PEER_DESTROY_SUCCESS;
595 op = GNUNET_TESTBED_peer_destroy (slave2_peer);
596 FAIL_TEST (NULL != op);
598 case SLAVE1_PEER_DESTROY_SUCCESS:
599 check_operation_success (event);
600 GNUNET_TESTBED_operation_done (op);
602 result = SLAVE2_PEER_DESTROY_SUCCESS;
603 op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
604 FAIL_TEST (NULL != op);
606 case SLAVE2_PEER_DESTROY_SUCCESS:
607 FAIL_TEST (NULL != event);
608 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
609 FAIL_TEST (event->op == op);
610 FAIL_TEST (NULL == event->op_cls);
611 FAIL_TEST (NULL == event->details.operation_finished.emsg);
612 cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
613 GNUNET_TESTBED_operation_done (op);
614 result = SLAVE3_GET_CONFIG_SUCCESS;
615 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, GNUNET_NO);
617 case SLAVE3_REGISTERED:
618 check_operation_success (event);
619 GNUNET_TESTBED_operation_done (op);
621 result = SLAVE3_STARTED;
622 op = GNUNET_TESTBED_peer_create (mc, slave3, cfg, peer_create_cb, NULL);
623 FAIL_TEST (NULL != op);
625 case SLAVE3_GET_CONFIG_SUCCESS:
626 result = SLAVE3_LINK_SUCCESS;
627 GNUNET_TESTBED_operation_done (op);
628 op = GNUNET_TESTBED_peer_destroy (master_peer);
630 case SLAVE3_LINK_SUCCESS:
631 check_operation_success (event);
633 GNUNET_TESTBED_operation_done (op);
635 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
636 (GNUNET_TIME_UNIT_SECONDS, 1), &do_shutdown,
646 * Callback which will be called to after a host registration succeeded or failed
648 * @param cls the host which has been registered
649 * @param emsg the error message; NULL if host registration is successful
652 registration_cont (void *cls, const char *emsg)
657 case MASTER_PEER_START_SUCCESS:
658 FAIL_TEST (NULL == emsg);
659 FAIL_TEST (NULL != mc);
660 result = SLAVE1_REGISTERED;
661 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, cfg, 0);
662 FAIL_TEST (NULL != slave2);
663 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
664 FAIL_TEST (NULL != rh);
666 case SLAVE1_REGISTERED:
667 FAIL_TEST (NULL == emsg);
668 FAIL_TEST (NULL != mc);
669 result = SLAVE2_REGISTERED;
670 FAIL_TEST (NULL != cfg);
671 op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, GNUNET_YES);
672 FAIL_TEST (NULL != op);
674 case MASTER_SLAVE2_PEERS_CONNECTED:
675 FAIL_TEST (NULL == emsg);
676 FAIL_TEST (NULL != mc);
677 FAIL_TEST (NULL == op);
678 result = SLAVE3_REGISTERED;
679 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, GNUNET_YES);
680 FAIL_TEST (NULL != op);
689 * Callback to signal successfull startup of the controller process
691 * @param cls the closure from GNUNET_TESTBED_controller_start()
692 * @param cfg the configuration with which the controller has been started;
693 * NULL if status is not GNUNET_OK
694 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
695 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
698 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
704 FAIL_TEST (GNUNET_OK == status);
706 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
707 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
708 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
709 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
710 mc = GNUNET_TESTBED_controller_connect (host, event_mask,
711 &controller_cb, NULL);
712 FAIL_TEST (NULL != mc);
713 result = MASTER_STARTED;
714 op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
715 FAIL_TEST (NULL != op);
726 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
727 * inform whether the given host is habitable or not. The Handle returned by
728 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
731 * @param host the host whose status is being reported; will be NULL if the host
732 * given to GNUNET_TESTBED_is_host_habitable() is NULL
733 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
736 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *_host,
740 if (GNUNET_NO == status)
743 "Unable to run the test as this system is not configured "
744 "to use password less SSH logins to localhost.\n"
746 GNUNET_SCHEDULER_cancel (abort_task);
747 abort_task = GNUNET_SCHEDULER_NO_TASK;
748 (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
752 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
761 * @param args arguments passed to GNUNET_PROGRAM_run
762 * @param cfgfile the path to configuration file
763 * @param cfg the configuration file handle
766 run (void *cls, char *const *args, const char *cfgfile,
767 const struct GNUNET_CONFIGURATION_Handle *config)
769 cfg = GNUNET_CONFIGURATION_dup (config);
770 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
771 FAIL_TEST (NULL != host);
774 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
777 GNUNET_TESTBED_host_destroy (host);
778 GNUNET_CONFIGURATION_destroy (cfg);
782 "Unable to run the test as this system is not configured "
783 "to use password less SSH logins to localhost.\n"
784 "Marking test as successful\n");
789 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
790 (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
799 main (int argc, char **argv)
801 char *const argv2[] = { "test_testbed_api_controllerlink",
802 "-c", "test_testbed_api.conf",
805 struct GNUNET_GETOPT_CommandLineOption options[] = {
806 GNUNET_GETOPT_OPTION_END
812 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
813 "test_testbed_api_controllerlink", "nohelp", options,
815 if (GNUNET_OK != ret)
822 return 77; /* Mark test as skipped */
828 /* end of test_testbed_api_controllerlink.c */