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, cfg,
531 case SLAVE2_PEER_CREATE_SUCCESS:
532 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
533 FAIL_TEST (event->details.peer_stop.peer == slave1_peer);
534 GNUNET_TESTBED_operation_done (op);
535 result = SLAVE1_PEER_STOP_SUCCESS;
536 op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
537 FAIL_TEST (NULL != op);
539 case SLAVE3_PEER_CREATE_SUCCESS:
540 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
541 FAIL_TEST (event->details.peer_start.host == slave3);
542 FAIL_TEST (event->details.peer_start.peer == slave3_peer);
543 GNUNET_TESTBED_operation_done (op);
544 result = SLAVE3_PEER_START_SUCCESS;
545 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, slave2_peer,
547 FAIL_TEST (NULL != op);
549 case SLAVE3_PEER_START_SUCCESS:
550 FAIL_TEST (NULL != event);
551 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
552 FAIL_TEST (event->details.peer_connect.peer1 == slave2_peer);
553 FAIL_TEST (event->details.peer_connect.peer2 == slave3_peer);
554 result = SLAVE2_SLAVE3_PEERS_CONNECTED;
555 GNUNET_TESTBED_operation_done (op);
558 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
559 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
562 case SLAVE1_PEER_STOP_SUCCESS:
563 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
564 FAIL_TEST (event->details.peer_start.host == slave2);
565 FAIL_TEST (event->details.peer_start.peer == slave2_peer);
566 GNUNET_TESTBED_operation_done (op);
567 result = SLAVE2_PEER_START_SUCCESS;
568 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
571 case SLAVE2_PEER_START_SUCCESS:
572 FAIL_TEST (NULL != event);
573 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
574 FAIL_TEST (event->details.peer_connect.peer1 == master_peer);
575 FAIL_TEST (event->details.peer_connect.peer2 == slave2_peer);
576 result = MASTER_SLAVE2_PEERS_CONNECTED;
577 GNUNET_TESTBED_operation_done (op);
580 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
581 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
584 case SLAVE2_SLAVE3_PEERS_CONNECTED:
585 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
586 FAIL_TEST (event->details.peer_stop.peer == slave2_peer);
587 GNUNET_TESTBED_operation_done (op);
588 result = SLAVE2_PEER_STOP_SUCCESS;
589 op = GNUNET_TESTBED_peer_destroy (slave1_peer);
590 FAIL_TEST (NULL != op);
592 case SLAVE2_PEER_STOP_SUCCESS:
593 check_operation_success (event);
594 GNUNET_TESTBED_operation_done (op);
595 result = SLAVE1_PEER_DESTROY_SUCCESS;
596 op = GNUNET_TESTBED_peer_destroy (slave2_peer);
597 FAIL_TEST (NULL != op);
599 case SLAVE1_PEER_DESTROY_SUCCESS:
600 check_operation_success (event);
601 GNUNET_TESTBED_operation_done (op);
603 result = SLAVE2_PEER_DESTROY_SUCCESS;
604 op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
605 FAIL_TEST (NULL != op);
607 case SLAVE2_PEER_DESTROY_SUCCESS:
608 FAIL_TEST (NULL != event);
609 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
610 FAIL_TEST (event->op == op);
611 FAIL_TEST (NULL == event->op_cls);
612 FAIL_TEST (NULL == event->details.operation_finished.emsg);
613 cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
614 GNUNET_TESTBED_operation_done (op);
615 result = SLAVE3_GET_CONFIG_SUCCESS;
616 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, cfg3,
619 case SLAVE3_REGISTERED:
620 check_operation_success (event);
621 GNUNET_TESTBED_operation_done (op);
623 result = SLAVE3_STARTED;
624 op = GNUNET_TESTBED_peer_create (mc, slave3, cfg, peer_create_cb, NULL);
625 FAIL_TEST (NULL != op);
627 case SLAVE3_GET_CONFIG_SUCCESS:
628 result = SLAVE3_LINK_SUCCESS;
629 GNUNET_TESTBED_operation_done (op);
630 op = GNUNET_TESTBED_peer_destroy (master_peer);
632 case SLAVE3_LINK_SUCCESS:
633 check_operation_success (event);
635 GNUNET_TESTBED_operation_done (op);
637 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
638 (GNUNET_TIME_UNIT_SECONDS, 1), &do_shutdown,
648 * Callback which will be called to after a host registration succeeded or failed
650 * @param cls the host which has been registered
651 * @param emsg the error message; NULL if host registration is successful
654 registration_cont (void *cls, const char *emsg)
659 case MASTER_PEER_START_SUCCESS:
660 FAIL_TEST (NULL == emsg);
661 FAIL_TEST (NULL != mc);
662 result = SLAVE1_REGISTERED;
663 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, cfg, 0);
664 FAIL_TEST (NULL != slave2);
665 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
666 FAIL_TEST (NULL != rh);
668 case SLAVE1_REGISTERED:
669 FAIL_TEST (NULL == emsg);
670 FAIL_TEST (NULL != mc);
671 result = SLAVE2_REGISTERED;
672 FAIL_TEST (NULL != cfg);
673 op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, cfg,
675 FAIL_TEST (NULL != op);
677 case MASTER_SLAVE2_PEERS_CONNECTED:
678 FAIL_TEST (NULL == emsg);
679 FAIL_TEST (NULL != mc);
680 FAIL_TEST (NULL == op);
681 result = SLAVE3_REGISTERED;
682 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, cfg,
684 FAIL_TEST (NULL != op);
693 * Callback to signal successfull startup of the controller process
695 * @param cls the closure from GNUNET_TESTBED_controller_start()
696 * @param cfg the configuration with which the controller has been started;
697 * NULL if status is not GNUNET_OK
698 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
699 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
702 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
708 FAIL_TEST (GNUNET_OK == status);
710 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
711 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
712 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
713 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
714 mc = GNUNET_TESTBED_controller_connect (host, event_mask,
715 &controller_cb, NULL);
716 FAIL_TEST (NULL != mc);
717 result = MASTER_STARTED;
718 op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
719 FAIL_TEST (NULL != op);
730 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
731 * inform whether the given host is habitable or not. The Handle returned by
732 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
735 * @param host the host whose status is being reported; will be NULL if the host
736 * given to GNUNET_TESTBED_is_host_habitable() is NULL
737 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
740 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *_host,
744 if (GNUNET_NO == status)
747 "Unable to run the test as this system is not configured "
748 "to use password less SSH logins to localhost.\n"
750 GNUNET_SCHEDULER_cancel (abort_task);
751 abort_task = GNUNET_SCHEDULER_NO_TASK;
752 (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
756 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
765 * @param args arguments passed to GNUNET_PROGRAM_run
766 * @param cfgfile the path to configuration file
767 * @param cfg the configuration file handle
770 run (void *cls, char *const *args, const char *cfgfile,
771 const struct GNUNET_CONFIGURATION_Handle *config)
773 cfg = GNUNET_CONFIGURATION_dup (config);
774 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
775 FAIL_TEST (NULL != host);
778 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
781 GNUNET_TESTBED_host_destroy (host);
782 GNUNET_CONFIGURATION_destroy (cfg);
786 "Unable to run the test as this system is not configured "
787 "to use password less SSH logins to localhost.\n"
788 "Marking test as successful\n");
793 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
794 (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
803 main (int argc, char **argv)
805 char *const argv2[] = { "test_testbed_api_controllerlink",
806 "-c", "test_testbed_api.conf",
809 struct GNUNET_GETOPT_CommandLineOption options[] = {
810 GNUNET_GETOPT_OPTION_END
816 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
817 "test_testbed_api_controllerlink", "nohelp", options,
819 if (GNUNET_OK != ret)
826 return 77; /* Mark test as skipped */
832 /* end of test_testbed_api_controllerlink.c */