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;
545 LOG_DEBUG ("**************************************\n");
546 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, slave2_peer,
548 FAIL_TEST (NULL != op);
550 case SLAVE3_PEER_START_SUCCESS:
551 FAIL_TEST (NULL != event);
552 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
553 FAIL_TEST (event->details.peer_connect.peer1 == slave2_peer);
554 FAIL_TEST (event->details.peer_connect.peer2 == slave3_peer);
555 result = SLAVE2_SLAVE3_PEERS_CONNECTED;
556 GNUNET_TESTBED_operation_done (op);
559 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
560 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
563 case SLAVE1_PEER_STOP_SUCCESS:
564 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
565 FAIL_TEST (event->details.peer_start.host == slave2);
566 FAIL_TEST (event->details.peer_start.peer == slave2_peer);
567 GNUNET_TESTBED_operation_done (op);
568 result = SLAVE2_PEER_START_SUCCESS;
569 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
572 case SLAVE2_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 == master_peer);
576 FAIL_TEST (event->details.peer_connect.peer2 == slave2_peer);
577 result = MASTER_SLAVE2_PEERS_CONNECTED;
578 GNUNET_TESTBED_operation_done (op);
581 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
582 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
585 case SLAVE2_SLAVE3_PEERS_CONNECTED:
586 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
587 FAIL_TEST (event->details.peer_stop.peer == slave2_peer);
588 GNUNET_TESTBED_operation_done (op);
589 result = SLAVE2_PEER_STOP_SUCCESS;
590 op = GNUNET_TESTBED_peer_destroy (slave1_peer);
591 FAIL_TEST (NULL != op);
593 case SLAVE2_PEER_STOP_SUCCESS:
594 check_operation_success (event);
595 GNUNET_TESTBED_operation_done (op);
596 result = SLAVE1_PEER_DESTROY_SUCCESS;
597 op = GNUNET_TESTBED_peer_destroy (slave2_peer);
598 FAIL_TEST (NULL != op);
600 case SLAVE1_PEER_DESTROY_SUCCESS:
601 check_operation_success (event);
602 GNUNET_TESTBED_operation_done (op);
604 result = SLAVE2_PEER_DESTROY_SUCCESS;
605 op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
606 FAIL_TEST (NULL != op);
608 case SLAVE2_PEER_DESTROY_SUCCESS:
609 FAIL_TEST (NULL != event);
610 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
611 FAIL_TEST (event->op == op);
612 FAIL_TEST (NULL == event->op_cls);
613 FAIL_TEST (NULL == event->details.operation_finished.emsg);
614 cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
615 GNUNET_TESTBED_operation_done (op);
616 result = SLAVE3_GET_CONFIG_SUCCESS;
617 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, GNUNET_NO);
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, GNUNET_YES);
674 FAIL_TEST (NULL != op);
676 case MASTER_SLAVE2_PEERS_CONNECTED:
677 FAIL_TEST (NULL == emsg);
678 FAIL_TEST (NULL != mc);
679 FAIL_TEST (NULL == op);
680 result = SLAVE3_REGISTERED;
681 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, GNUNET_YES);
682 FAIL_TEST (NULL != op);
691 * Callback to signal successfull startup of the controller process
693 * @param cls the closure from GNUNET_TESTBED_controller_start()
694 * @param cfg the configuration with which the controller has been started;
695 * NULL if status is not GNUNET_OK
696 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
697 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
700 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
706 FAIL_TEST (GNUNET_OK == status);
708 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
709 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
710 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
711 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
712 mc = GNUNET_TESTBED_controller_connect (host, event_mask,
713 &controller_cb, NULL);
714 FAIL_TEST (NULL != mc);
715 result = MASTER_STARTED;
716 op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
717 FAIL_TEST (NULL != op);
728 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
729 * inform whether the given host is habitable or not. The Handle returned by
730 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
733 * @param host the host whose status is being reported; will be NULL if the host
734 * given to GNUNET_TESTBED_is_host_habitable() is NULL
735 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
738 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *_host,
742 if (GNUNET_NO == status)
745 "Unable to run the test as this system is not configured "
746 "to use password less SSH logins to localhost.\n"
748 GNUNET_SCHEDULER_cancel (abort_task);
749 abort_task = GNUNET_SCHEDULER_NO_TASK;
750 (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
754 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
763 * @param args arguments passed to GNUNET_PROGRAM_run
764 * @param cfgfile the path to configuration file
765 * @param cfg the configuration file handle
768 run (void *cls, char *const *args, const char *cfgfile,
769 const struct GNUNET_CONFIGURATION_Handle *config)
771 cfg = GNUNET_CONFIGURATION_dup (config);
772 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
773 FAIL_TEST (NULL != host);
776 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
779 GNUNET_TESTBED_host_destroy (host);
780 GNUNET_CONFIGURATION_destroy (cfg);
784 "Unable to run the test as this system is not configured "
785 "to use password less SSH logins to localhost.\n"
786 "Marking test as successful\n");
791 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
792 (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
801 main (int argc, char **argv)
803 char *const argv2[] = { "test_testbed_api_controllerlink",
804 "-c", "test_testbed_api.conf",
807 struct GNUNET_GETOPT_CommandLineOption options[] = {
808 GNUNET_GETOPT_OPTION_END
814 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
815 "test_testbed_api_controllerlink", "nohelp", options,
817 if (GNUNET_OK != ret)
824 return 77; /* Mark test as skipped */
830 /* end of test_testbed_api_controllerlink.c */