2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, ...) GNUNET_log (kind, __VA_ARGS__)
51 * Debug logging shorthand
53 #define LOG_DEBUG(...) LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
56 * Different stages in testing
66 * Master controller has started
71 * A peer has been created on master
73 MASTER_PEER_CREATE_SUCCESS,
76 * Peer on master controller has been started successfully.
78 MASTER_PEER_START_SUCCESS,
81 * The first slave has been registered at master controller
86 * The second slave has been registered at the master controller
91 * Link from master to slave 1 has been successfully created
96 * Peer create on slave 1 successful
98 SLAVE1_PEER_CREATE_SUCCESS,
101 * Peer startup on slave 1 successful
103 SLAVE1_PEER_START_SUCCESS,
106 * Link from slave 1 to slave 2 has been successfully created.
111 * Peer create on slave 2 successful
113 SLAVE2_PEER_CREATE_SUCCESS,
116 * Peer on slave 1 successfully stopped
118 SLAVE1_PEER_STOP_SUCCESS,
121 * Peer startup on slave 2 successful
123 SLAVE2_PEER_START_SUCCESS,
126 * Try to connect peers on master and slave 2.
128 MASTER_SLAVE2_PEERS_CONNECTED,
131 * Slave 3 has successfully registered
136 * Slave 3 has successfully started
141 * Peer created on slave 3
143 SLAVE3_PEER_CREATE_SUCCESS,
146 * Peer started at slave 3
148 SLAVE3_PEER_START_SUCCESS,
151 * Try to connect peers on slave2 and slave3
153 SLAVE2_SLAVE3_PEERS_CONNECTED,
156 * Peer on slave 2 successfully stopped
158 SLAVE2_PEER_STOP_SUCCESS,
161 * Peer destroy on slave 1 successful
163 SLAVE1_PEER_DESTROY_SUCCESS,
166 * Peer destory on slave 2 successful
168 SLAVE2_PEER_DESTROY_SUCCESS,
171 * The configuration of slave 3 is acquired
173 SLAVE3_GET_CONFIG_SUCCESS,
176 * Slave 1 has linked to slave 3;
181 * Master peer destoryed. Destory slave 3 peer
183 MASTER_PEER_DESTROY_SUCCESS,
186 * Slave 3 peer destroyed. Mark test as success
191 * Marks test as skipped
197 * Host for running master controller
199 static struct GNUNET_TESTBED_Host *host;
202 * The master controller process
204 static struct GNUNET_TESTBED_ControllerProc *cp;
207 * Handle to master controller
209 static struct GNUNET_TESTBED_Controller *mc;
212 * Slave host for running slave controller
214 static struct GNUNET_TESTBED_Host *slave;
217 * Another slave host for running another slave controller
219 static struct GNUNET_TESTBED_Host *slave2;
224 static struct GNUNET_TESTBED_Host *slave3;
227 * Slave host registration handle
229 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
232 * Handle to global configuration
234 static struct GNUNET_CONFIGURATION_Handle *cfg;
237 * Configuration of slave 3 controller
239 static struct GNUNET_CONFIGURATION_Handle *cfg3;
244 static struct GNUNET_SCHEDULER_Task *abort_task;
247 * Operation handle for linking controllers
249 static struct GNUNET_TESTBED_Operation *op;
252 * Handle to peer started at slave 1
254 static struct GNUNET_TESTBED_Peer *slave1_peer;
257 * Handle to peer started at slave 2
259 static struct GNUNET_TESTBED_Peer *slave2_peer;
262 * Handle to peer started at slave 2
264 static struct GNUNET_TESTBED_Peer *slave3_peer;
267 * Handle to a peer started at master controller
269 static struct GNUNET_TESTBED_Peer *master_peer;
272 * The handle for whether a host is habitable or not
274 static struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
277 * The task handle for the delay task
279 static struct GNUNET_SCHEDULER_Task *delay_task_id;
284 static uint64_t event_mask;
287 * Global testing status
289 static enum Stage result;
292 * shortcut to exit during failure
294 #define FAIL_TEST(cond) \
300 if (NULL != abort_task) \
301 GNUNET_SCHEDULER_cancel (abort_task); \
303 GNUNET_SCHEDULER_shutdown (); \
315 do_shutdown (void *cls)
317 if (NULL != abort_task)
318 GNUNET_SCHEDULER_cancel (abort_task);
319 if (NULL != delay_task_id)
321 GNUNET_SCHEDULER_cancel (delay_task_id);
322 delay_task_id = NULL;
324 if (NULL != hc_handle)
325 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
328 GNUNET_TESTBED_operation_done (op);
332 GNUNET_TESTBED_controller_disconnect (mc);
334 GNUNET_TESTBED_controller_stop (cp);
336 GNUNET_TESTBED_host_destroy (slave3);
338 GNUNET_TESTBED_host_destroy (slave2);
340 GNUNET_TESTBED_host_destroy (slave);
342 GNUNET_TESTBED_host_destroy (host);
344 GNUNET_CONFIGURATION_destroy (cfg);
346 GNUNET_CONFIGURATION_destroy (cfg3);
348 GNUNET_TESTBED_cancel_registration (rh);
353 * abort task to run on test timed out
360 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting in stage %d\n", result);
362 GNUNET_SCHEDULER_shutdown ();
373 do_abort_now (void *cls)
375 if (NULL != abort_task)
376 GNUNET_SCHEDULER_cancel (abort_task);
377 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
382 * Callback which will be called to after a host registration succeeded or failed
384 * @param cls the host which has been registered
385 * @param emsg the error message; NULL if host registration is successful
388 registration_cont (void *cls, const char *emsg);
392 * Task for inserting delay between tests
398 delay_task (void *cls)
400 delay_task_id = NULL;
403 case SLAVE2_PEER_CREATE_SUCCESS:
404 op = GNUNET_TESTBED_peer_stop (NULL, slave1_peer, NULL, NULL);
405 FAIL_TEST (NULL != op);
408 case MASTER_SLAVE2_PEERS_CONNECTED:
409 slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, cfg, 0);
410 rh = GNUNET_TESTBED_register_host (mc, slave3, ®istration_cont, NULL);
413 case SLAVE2_SLAVE3_PEERS_CONNECTED:
414 op = GNUNET_TESTBED_peer_stop (NULL, slave2_peer, NULL, NULL);
415 FAIL_TEST (NULL != op);
425 * Functions of this signature are called when a peer has been successfully
428 * @param cls the closure from GNUNET_TESTBED_peer_create()
429 * @param peer the handle for the created peer; NULL on any error during
431 * @param emsg NULL if peer is not NULL; else MAY contain the error description
434 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
436 FAIL_TEST (NULL != peer);
437 FAIL_TEST (NULL == emsg);
441 result = MASTER_PEER_CREATE_SUCCESS;
443 GNUNET_TESTBED_operation_done (op);
444 op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
447 case SLAVE1_LINK_SUCCESS:
448 result = SLAVE1_PEER_CREATE_SUCCESS;
450 GNUNET_TESTBED_operation_done (op);
451 op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
454 case SLAVE2_LINK_SUCCESS:
455 result = SLAVE2_PEER_CREATE_SUCCESS;
457 GNUNET_TESTBED_operation_done (op);
459 delay_task_id = GNUNET_SCHEDULER_add_delayed (
460 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
466 result = SLAVE3_PEER_CREATE_SUCCESS;
468 GNUNET_TESTBED_operation_done (op);
469 op = GNUNET_TESTBED_peer_start (NULL, slave3_peer, NULL, NULL);
475 FAIL_TEST (NULL != op);
480 * Checks the event if it is an operation finished event and if indicates a
481 * successfull completion of operation
483 * @param event the event information to check
486 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
488 FAIL_TEST (NULL != event);
489 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
490 FAIL_TEST (event->op == op);
491 FAIL_TEST (NULL == event->op_cls);
492 FAIL_TEST (NULL == event->details.operation_finished.emsg);
493 FAIL_TEST (NULL == event->details.operation_finished.generic);
498 * Signature of the event handler function called by the
499 * respective event controller.
502 * @param event information about the event
505 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
509 case SLAVE2_REGISTERED:
510 check_operation_success (event);
511 GNUNET_TESTBED_operation_done (op);
513 result = SLAVE1_LINK_SUCCESS;
514 FAIL_TEST (NULL != slave2);
515 FAIL_TEST (NULL != slave);
516 op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
517 FAIL_TEST (NULL != op);
520 case SLAVE1_PEER_START_SUCCESS:
521 check_operation_success (event);
522 GNUNET_TESTBED_operation_done (op);
523 result = SLAVE2_LINK_SUCCESS;
524 op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
525 FAIL_TEST (NULL != op);
528 case MASTER_PEER_CREATE_SUCCESS:
529 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
530 FAIL_TEST (event->details.peer_start.host == host);
531 FAIL_TEST (event->details.peer_start.peer == master_peer);
532 GNUNET_TESTBED_operation_done (op);
534 result = MASTER_PEER_START_SUCCESS;
535 slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, cfg, 0);
536 FAIL_TEST (NULL != slave);
537 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
538 FAIL_TEST (NULL != rh);
541 case SLAVE1_PEER_CREATE_SUCCESS:
542 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
543 FAIL_TEST (event->details.peer_start.host == slave);
544 FAIL_TEST (event->details.peer_start.peer == slave1_peer);
545 GNUNET_TESTBED_operation_done (op);
546 result = SLAVE1_PEER_START_SUCCESS;
547 op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, GNUNET_YES);
550 case SLAVE2_PEER_CREATE_SUCCESS:
551 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
552 FAIL_TEST (event->details.peer_stop.peer == slave1_peer);
553 GNUNET_TESTBED_operation_done (op);
554 result = SLAVE1_PEER_STOP_SUCCESS;
555 op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
556 FAIL_TEST (NULL != op);
559 case SLAVE3_PEER_CREATE_SUCCESS:
560 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
561 FAIL_TEST (event->details.peer_start.host == slave3);
562 FAIL_TEST (event->details.peer_start.peer == slave3_peer);
563 GNUNET_TESTBED_operation_done (op);
564 result = SLAVE3_PEER_START_SUCCESS;
566 LOG_DEBUG ("**************************************\n");
568 GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, slave2_peer, slave3_peer);
569 FAIL_TEST (NULL != op);
572 case SLAVE3_PEER_START_SUCCESS:
573 FAIL_TEST (NULL != event);
574 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
575 FAIL_TEST (event->details.peer_connect.peer1 == slave2_peer);
576 FAIL_TEST (event->details.peer_connect.peer2 == slave3_peer);
577 result = SLAVE2_SLAVE3_PEERS_CONNECTED;
578 GNUNET_TESTBED_operation_done (op);
580 delay_task_id = GNUNET_SCHEDULER_add_delayed (
581 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
586 case SLAVE1_PEER_STOP_SUCCESS:
587 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
588 FAIL_TEST (event->details.peer_start.host == slave2);
589 FAIL_TEST (event->details.peer_start.peer == slave2_peer);
590 GNUNET_TESTBED_operation_done (op);
591 result = SLAVE2_PEER_START_SUCCESS;
593 GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer, slave2_peer);
596 case SLAVE2_PEER_START_SUCCESS:
597 FAIL_TEST (NULL != event);
598 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
599 FAIL_TEST (event->details.peer_connect.peer1 == master_peer);
600 FAIL_TEST (event->details.peer_connect.peer2 == slave2_peer);
601 result = MASTER_SLAVE2_PEERS_CONNECTED;
602 GNUNET_TESTBED_operation_done (op);
604 delay_task_id = GNUNET_SCHEDULER_add_delayed (
605 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
610 case SLAVE2_SLAVE3_PEERS_CONNECTED:
611 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
612 FAIL_TEST (event->details.peer_stop.peer == slave2_peer);
613 GNUNET_TESTBED_operation_done (op);
614 result = SLAVE2_PEER_STOP_SUCCESS;
615 op = GNUNET_TESTBED_peer_destroy (slave1_peer);
616 FAIL_TEST (NULL != op);
619 case SLAVE2_PEER_STOP_SUCCESS:
620 check_operation_success (event);
621 GNUNET_TESTBED_operation_done (op);
622 result = SLAVE1_PEER_DESTROY_SUCCESS;
623 op = GNUNET_TESTBED_peer_destroy (slave2_peer);
624 FAIL_TEST (NULL != op);
627 case SLAVE1_PEER_DESTROY_SUCCESS:
628 check_operation_success (event);
629 GNUNET_TESTBED_operation_done (op);
631 result = SLAVE2_PEER_DESTROY_SUCCESS;
632 op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
633 FAIL_TEST (NULL != op);
636 case SLAVE2_PEER_DESTROY_SUCCESS:
637 FAIL_TEST (NULL != event);
638 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
639 FAIL_TEST (event->op == op);
640 FAIL_TEST (NULL == event->op_cls);
641 FAIL_TEST (NULL == event->details.operation_finished.emsg);
642 cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
643 GNUNET_TESTBED_operation_done (op);
644 result = SLAVE3_GET_CONFIG_SUCCESS;
645 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, GNUNET_NO);
648 case SLAVE3_REGISTERED:
649 check_operation_success (event);
650 GNUNET_TESTBED_operation_done (op);
651 result = SLAVE3_STARTED;
652 op = GNUNET_TESTBED_peer_create (mc, slave3, cfg, peer_create_cb, NULL);
653 FAIL_TEST (NULL != op);
656 case SLAVE3_GET_CONFIG_SUCCESS:
657 result = SLAVE3_LINK_SUCCESS;
658 GNUNET_TESTBED_operation_done (op);
659 op = GNUNET_TESTBED_peer_destroy (master_peer);
662 case SLAVE3_LINK_SUCCESS:
663 check_operation_success (event);
664 result = MASTER_PEER_DESTROY_SUCCESS;
665 GNUNET_TESTBED_operation_done (op);
666 op = GNUNET_TESTBED_peer_destroy (slave3_peer);
669 case MASTER_PEER_DESTROY_SUCCESS:
671 GNUNET_TESTBED_operation_done (op);
673 GNUNET_SCHEDULER_shutdown ();
683 * Callback which will be called to after a host registration succeeded or failed
685 * @param cls the host which has been registered
686 * @param emsg the error message; NULL if host registration is successful
689 registration_cont (void *cls, const char *emsg)
694 case MASTER_PEER_START_SUCCESS:
695 FAIL_TEST (NULL == emsg);
696 FAIL_TEST (NULL != mc);
697 result = SLAVE1_REGISTERED;
698 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, cfg, 0);
699 FAIL_TEST (NULL != slave2);
700 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
701 FAIL_TEST (NULL != rh);
704 case SLAVE1_REGISTERED:
705 FAIL_TEST (NULL == emsg);
706 FAIL_TEST (NULL != mc);
707 result = SLAVE2_REGISTERED;
708 FAIL_TEST (NULL != cfg);
709 op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, GNUNET_YES);
710 FAIL_TEST (NULL != op);
713 case MASTER_SLAVE2_PEERS_CONNECTED:
714 FAIL_TEST (NULL == emsg);
715 FAIL_TEST (NULL != mc);
716 FAIL_TEST (NULL == op);
717 result = SLAVE3_REGISTERED;
718 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, GNUNET_YES);
719 FAIL_TEST (NULL != op);
730 * Callback to signal successfull startup of the controller process
732 * @param cls the closure from GNUNET_TESTBED_controller_start()
733 * @param cfg the configuration with which the controller has been started;
734 * NULL if status is not GNUNET_OK
735 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
736 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
739 status_cb (void *cls,
740 const struct GNUNET_CONFIGURATION_Handle *config,
746 FAIL_TEST (GNUNET_OK == status);
748 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
749 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
750 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
751 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
752 mc = GNUNET_TESTBED_controller_connect (host,
756 FAIL_TEST (NULL != mc);
757 result = MASTER_STARTED;
758 op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
759 FAIL_TEST (NULL != op);
771 * Callbacks of this type are called by #GNUNET_TESTBED_is_host_habitable to
772 * inform whether the given host is habitable or not. The Handle returned by
773 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
776 * @param host the host whose status is being reported; will be NULL if the host
777 * given to GNUNET_TESTBED_is_host_habitable() is NULL
778 * @param status #GNUNET_YES if it is habitable; #GNUNET_NO if not
781 host_habitable_cb (void *cls,
782 const struct GNUNET_TESTBED_Host *_host,
786 if (GNUNET_NO == status)
789 "Unable to run the test as this system is not configured "
790 "to use password less SSH logins to localhost.\n"
792 GNUNET_SCHEDULER_cancel (abort_task);
794 GNUNET_SCHEDULER_shutdown ();
798 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb, NULL);
806 * @param args arguments passed to #GNUNET_PROGRAM_run()
807 * @param cfgfile the path to configuration file
808 * @param cfg the configuration file handle
814 const struct GNUNET_CONFIGURATION_Handle *config)
816 cfg = GNUNET_CONFIGURATION_dup (config);
817 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
818 FAIL_TEST (NULL != host);
819 if (NULL == (hc_handle = GNUNET_TESTBED_is_host_habitable (host,
824 GNUNET_TESTBED_host_destroy (host);
825 GNUNET_CONFIGURATION_destroy (cfg);
829 "Unable to run the test as this system is not configured "
830 "to use password less SSH logins to localhost.\n"
831 "Marking test as successful\n");
835 abort_task = GNUNET_SCHEDULER_add_delayed (
836 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5),
839 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
847 main (int argc, char **argv)
849 char *const argv2[] = { "test_testbed_api_controllerlink",
851 "test_testbed_api.conf",
853 struct GNUNET_GETOPT_CommandLineOption options[] =
854 { GNUNET_GETOPT_OPTION_END };
858 ret = GNUNET_PROGRAM_run ((sizeof(argv2) / sizeof(char *)) - 1,
860 "test_testbed_api_controllerlink",
865 if (GNUNET_OK != ret)
873 return 77; /* Mark test as skipped */
881 /* end of test_testbed_api_controllerlink.c */