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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 * Master peer destoryed. Destory slave 3 peer
186 MASTER_PEER_DESTROY_SUCCESS,
189 * Slave 3 peer destroyed. Mark test as success
194 * Marks test as skipped
200 * Host for running master controller
202 static struct GNUNET_TESTBED_Host *host;
205 * The master controller process
207 static struct GNUNET_TESTBED_ControllerProc *cp;
210 * Handle to master controller
212 static struct GNUNET_TESTBED_Controller *mc;
215 * Slave host for running slave controller
217 static struct GNUNET_TESTBED_Host *slave;
220 * Another slave host for running another slave controller
222 static struct GNUNET_TESTBED_Host *slave2;
227 static struct GNUNET_TESTBED_Host *slave3;
230 * Slave host registration handle
232 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
235 * Handle to global configuration
237 static struct GNUNET_CONFIGURATION_Handle *cfg;
240 * Configuration of slave 3 controller
242 static struct GNUNET_CONFIGURATION_Handle *cfg3;
247 static struct GNUNET_SCHEDULER_Task * abort_task;
250 * Operation handle for linking controllers
252 static struct GNUNET_TESTBED_Operation *op;
255 * Handle to peer started at slave 1
257 static struct GNUNET_TESTBED_Peer *slave1_peer;
260 * Handle to peer started at slave 2
262 static struct GNUNET_TESTBED_Peer *slave2_peer;
265 * Handle to peer started at slave 2
267 static struct GNUNET_TESTBED_Peer *slave3_peer;
270 * Handle to a peer started at master controller
272 static struct GNUNET_TESTBED_Peer *master_peer;
275 * The handle for whether a host is habitable or not
277 struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
280 * The task handle for the delay task
282 struct GNUNET_SCHEDULER_Task * delay_task_id;
290 * Global testing status
292 static enum Stage result;
295 * shortcut to exit during failure
297 #define FAIL_TEST(cond) do { \
300 if (NULL != abort_task) \
301 GNUNET_SCHEDULER_cancel (abort_task); \
303 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); \
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);
327 GNUNET_TESTBED_controller_disconnect (mc);
329 GNUNET_TESTBED_controller_stop (cp);
331 GNUNET_TESTBED_host_destroy (slave3);
333 GNUNET_TESTBED_host_destroy (slave2);
335 GNUNET_TESTBED_host_destroy (slave);
337 GNUNET_TESTBED_host_destroy (host);
339 GNUNET_CONFIGURATION_destroy (cfg);
341 GNUNET_CONFIGURATION_destroy (cfg3);
343 GNUNET_TESTBED_cancel_registration (rh);
348 * abort task to run on test timed out
355 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
368 do_abort_now (void *cls)
370 if (NULL != abort_task)
371 GNUNET_SCHEDULER_cancel (abort_task);
372 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
377 * Callback which will be called to after a host registration succeeded or failed
379 * @param cls the host which has been registered
380 * @param emsg the error message; NULL if host registration is successful
383 registration_cont (void *cls, const char *emsg);
387 * Task for inserting delay between tests
393 delay_task (void *cls)
395 delay_task_id = NULL;
398 case SLAVE2_PEER_CREATE_SUCCESS:
399 op = GNUNET_TESTBED_peer_stop (NULL, slave1_peer, NULL, NULL);
400 FAIL_TEST (NULL != op);
402 case MASTER_SLAVE2_PEERS_CONNECTED:
403 slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, cfg, 0);
404 rh = GNUNET_TESTBED_register_host (mc, slave3, ®istration_cont, NULL);
406 case SLAVE2_SLAVE3_PEERS_CONNECTED:
407 op = GNUNET_TESTBED_peer_stop (NULL, slave2_peer, NULL, NULL);
408 FAIL_TEST (NULL != op);
417 * Functions of this signature are called when a peer has been successfully
420 * @param cls the closure from GNUNET_TESTBED_peer_create()
421 * @param peer the handle for the created peer; NULL on any error during
423 * @param emsg NULL if peer is not NULL; else MAY contain the error description
426 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
428 FAIL_TEST (NULL != peer);
429 FAIL_TEST (NULL == emsg);
433 result = MASTER_PEER_CREATE_SUCCESS;
435 GNUNET_TESTBED_operation_done (op);
436 op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
438 case SLAVE1_LINK_SUCCESS:
439 result = SLAVE1_PEER_CREATE_SUCCESS;
441 GNUNET_TESTBED_operation_done (op);
442 op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
444 case SLAVE2_LINK_SUCCESS:
445 result = SLAVE2_PEER_CREATE_SUCCESS;
447 GNUNET_TESTBED_operation_done (op);
449 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
450 (GNUNET_TIME_UNIT_SECONDS, 1),
455 result = SLAVE3_PEER_CREATE_SUCCESS;
457 GNUNET_TESTBED_operation_done (op);
458 op = GNUNET_TESTBED_peer_start (NULL, slave3_peer, NULL, NULL);
463 FAIL_TEST (NULL != op);
468 * Checks the event if it is an operation finished event and if indicates a
469 * successfull completion of operation
471 * @param event the event information to check
474 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
476 FAIL_TEST (NULL != event);
477 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
478 FAIL_TEST (event->op == op);
479 FAIL_TEST (NULL == event->op_cls);
480 FAIL_TEST (NULL == event->details.operation_finished.emsg);
481 FAIL_TEST (NULL == event->details.operation_finished.generic);
486 * Signature of the event handler function called by the
487 * respective event controller.
490 * @param event information about the event
493 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
497 case SLAVE2_REGISTERED:
498 check_operation_success (event);
499 GNUNET_TESTBED_operation_done (op);
501 result = SLAVE1_LINK_SUCCESS;
502 FAIL_TEST (NULL != slave2);
503 FAIL_TEST (NULL != slave);
504 op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
505 FAIL_TEST (NULL != op);
507 case SLAVE1_PEER_START_SUCCESS:
508 check_operation_success (event);
509 GNUNET_TESTBED_operation_done (op);
510 result = SLAVE2_LINK_SUCCESS;
511 op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
512 FAIL_TEST (NULL != op);
514 case MASTER_PEER_CREATE_SUCCESS:
515 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
516 FAIL_TEST (event->details.peer_start.host == host);
517 FAIL_TEST (event->details.peer_start.peer == master_peer);
518 GNUNET_TESTBED_operation_done (op);
519 result = MASTER_PEER_START_SUCCESS;
520 slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, cfg, 0);
521 FAIL_TEST (NULL != slave);
522 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
523 FAIL_TEST (NULL != rh);
525 case SLAVE1_PEER_CREATE_SUCCESS:
526 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
527 FAIL_TEST (event->details.peer_start.host == slave);
528 FAIL_TEST (event->details.peer_start.peer == slave1_peer);
529 GNUNET_TESTBED_operation_done (op);
530 result = SLAVE1_PEER_START_SUCCESS;
531 op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, GNUNET_YES);
533 case SLAVE2_PEER_CREATE_SUCCESS:
534 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
535 FAIL_TEST (event->details.peer_stop.peer == slave1_peer);
536 GNUNET_TESTBED_operation_done (op);
537 result = SLAVE1_PEER_STOP_SUCCESS;
538 op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
539 FAIL_TEST (NULL != op);
541 case SLAVE3_PEER_CREATE_SUCCESS:
542 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
543 FAIL_TEST (event->details.peer_start.host == slave3);
544 FAIL_TEST (event->details.peer_start.peer == slave3_peer);
545 GNUNET_TESTBED_operation_done (op);
546 result = SLAVE3_PEER_START_SUCCESS;
548 LOG_DEBUG ("**************************************\n");
549 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, slave2_peer,
551 FAIL_TEST (NULL != op);
553 case SLAVE3_PEER_START_SUCCESS:
554 FAIL_TEST (NULL != event);
555 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
556 FAIL_TEST (event->details.peer_connect.peer1 == slave2_peer);
557 FAIL_TEST (event->details.peer_connect.peer2 == slave3_peer);
558 result = SLAVE2_SLAVE3_PEERS_CONNECTED;
559 GNUNET_TESTBED_operation_done (op);
562 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
563 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
566 case SLAVE1_PEER_STOP_SUCCESS:
567 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
568 FAIL_TEST (event->details.peer_start.host == slave2);
569 FAIL_TEST (event->details.peer_start.peer == slave2_peer);
570 GNUNET_TESTBED_operation_done (op);
571 result = SLAVE2_PEER_START_SUCCESS;
572 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
575 case SLAVE2_PEER_START_SUCCESS:
576 FAIL_TEST (NULL != event);
577 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
578 FAIL_TEST (event->details.peer_connect.peer1 == master_peer);
579 FAIL_TEST (event->details.peer_connect.peer2 == slave2_peer);
580 result = MASTER_SLAVE2_PEERS_CONNECTED;
581 GNUNET_TESTBED_operation_done (op);
584 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
585 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
588 case SLAVE2_SLAVE3_PEERS_CONNECTED:
589 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
590 FAIL_TEST (event->details.peer_stop.peer == slave2_peer);
591 GNUNET_TESTBED_operation_done (op);
592 result = SLAVE2_PEER_STOP_SUCCESS;
593 op = GNUNET_TESTBED_peer_destroy (slave1_peer);
594 FAIL_TEST (NULL != op);
596 case SLAVE2_PEER_STOP_SUCCESS:
597 check_operation_success (event);
598 GNUNET_TESTBED_operation_done (op);
599 result = SLAVE1_PEER_DESTROY_SUCCESS;
600 op = GNUNET_TESTBED_peer_destroy (slave2_peer);
601 FAIL_TEST (NULL != op);
603 case SLAVE1_PEER_DESTROY_SUCCESS:
604 check_operation_success (event);
605 GNUNET_TESTBED_operation_done (op);
607 result = SLAVE2_PEER_DESTROY_SUCCESS;
608 op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
609 FAIL_TEST (NULL != op);
611 case SLAVE2_PEER_DESTROY_SUCCESS:
612 FAIL_TEST (NULL != event);
613 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
614 FAIL_TEST (event->op == op);
615 FAIL_TEST (NULL == event->op_cls);
616 FAIL_TEST (NULL == event->details.operation_finished.emsg);
617 cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
618 GNUNET_TESTBED_operation_done (op);
619 result = SLAVE3_GET_CONFIG_SUCCESS;
620 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, GNUNET_NO);
622 case SLAVE3_REGISTERED:
623 check_operation_success (event);
624 GNUNET_TESTBED_operation_done (op);
626 result = SLAVE3_STARTED;
627 op = GNUNET_TESTBED_peer_create (mc, slave3, cfg, peer_create_cb, NULL);
628 FAIL_TEST (NULL != op);
630 case SLAVE3_GET_CONFIG_SUCCESS:
631 result = SLAVE3_LINK_SUCCESS;
632 GNUNET_TESTBED_operation_done (op);
633 op = GNUNET_TESTBED_peer_destroy (master_peer);
635 case SLAVE3_LINK_SUCCESS:
636 check_operation_success (event);
637 result = MASTER_PEER_DESTROY_SUCCESS;
638 GNUNET_TESTBED_operation_done (op);
639 op = GNUNET_TESTBED_peer_destroy (slave3_peer);
641 case MASTER_PEER_DESTROY_SUCCESS:
643 GNUNET_TESTBED_operation_done (op);
645 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
656 * Callback which will be called to after a host registration succeeded or failed
658 * @param cls the host which has been registered
659 * @param emsg the error message; NULL if host registration is successful
662 registration_cont (void *cls, const char *emsg)
667 case MASTER_PEER_START_SUCCESS:
668 FAIL_TEST (NULL == emsg);
669 FAIL_TEST (NULL != mc);
670 result = SLAVE1_REGISTERED;
671 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, cfg, 0);
672 FAIL_TEST (NULL != slave2);
673 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
674 FAIL_TEST (NULL != rh);
676 case SLAVE1_REGISTERED:
677 FAIL_TEST (NULL == emsg);
678 FAIL_TEST (NULL != mc);
679 result = SLAVE2_REGISTERED;
680 FAIL_TEST (NULL != cfg);
681 op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, GNUNET_YES);
682 FAIL_TEST (NULL != op);
684 case MASTER_SLAVE2_PEERS_CONNECTED:
685 FAIL_TEST (NULL == emsg);
686 FAIL_TEST (NULL != mc);
687 FAIL_TEST (NULL == op);
688 result = SLAVE3_REGISTERED;
689 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, GNUNET_YES);
690 FAIL_TEST (NULL != op);
699 * Callback to signal successfull startup of the controller process
701 * @param cls the closure from GNUNET_TESTBED_controller_start()
702 * @param cfg the configuration with which the controller has been started;
703 * NULL if status is not GNUNET_OK
704 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
705 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
708 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
714 FAIL_TEST (GNUNET_OK == status);
716 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
717 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
718 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
719 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
720 mc = GNUNET_TESTBED_controller_connect (host, event_mask,
721 &controller_cb, NULL);
722 FAIL_TEST (NULL != mc);
723 result = MASTER_STARTED;
724 op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
725 FAIL_TEST (NULL != op);
736 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
737 * inform whether the given host is habitable or not. The Handle returned by
738 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
741 * @param host the host whose status is being reported; will be NULL if the host
742 * given to GNUNET_TESTBED_is_host_habitable() is NULL
743 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
746 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *_host,
750 if (GNUNET_NO == status)
753 "Unable to run the test as this system is not configured "
754 "to use password less SSH logins to localhost.\n"
756 GNUNET_SCHEDULER_cancel (abort_task);
758 (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
762 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
771 * @param args arguments passed to GNUNET_PROGRAM_run
772 * @param cfgfile the path to configuration file
773 * @param cfg the configuration file handle
776 run (void *cls, char *const *args, const char *cfgfile,
777 const struct GNUNET_CONFIGURATION_Handle *config)
779 cfg = GNUNET_CONFIGURATION_dup (config);
780 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
781 FAIL_TEST (NULL != host);
784 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
787 GNUNET_TESTBED_host_destroy (host);
788 GNUNET_CONFIGURATION_destroy (cfg);
792 "Unable to run the test as this system is not configured "
793 "to use password less SSH logins to localhost.\n"
794 "Marking test as successful\n");
799 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
800 (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
809 main (int argc, char **argv)
811 char *const argv2[] = { "test_testbed_api_controllerlink",
812 "-c", "test_testbed_api.conf",
815 struct GNUNET_GETOPT_CommandLineOption options[] = {
816 GNUNET_GETOPT_OPTION_END
822 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
823 "test_testbed_api_controllerlink", "nohelp", options,
825 if (GNUNET_OK != ret)
832 return 77; /* Mark test as skipped */
838 /* end of test_testbed_api_controllerlink.c */