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 * Peer on slave 2 successfully stopped
136 SLAVE2_PEER_STOP_SUCCESS,
139 * Peer destroy on slave 1 successful
141 SLAVE1_PEER_DESTROY_SUCCESS,
144 * Peer destory on slave 2 successful
146 SLAVE2_PEER_DESTROY_SUCCESS,
149 * Slave 3 has successfully registered
154 * Slave 3 has successfully started
159 * The configuration of slave 3 is acquired
161 SLAVE3_GET_CONFIG_SUCCESS,
164 * Slave 1 has linked to slave 3;
169 * Destory master peer and mark test as success
174 * Marks test as skipped
180 * Host for running master controller
182 static struct GNUNET_TESTBED_Host *host;
185 * The master controller process
187 static struct GNUNET_TESTBED_ControllerProc *cp;
190 * Handle to master controller
192 static struct GNUNET_TESTBED_Controller *mc;
195 * Slave host for running slave controller
197 static struct GNUNET_TESTBED_Host *slave;
200 * Another slave host for running another slave controller
202 static struct GNUNET_TESTBED_Host *slave2;
207 static struct GNUNET_TESTBED_Host *slave3;
210 * Slave host registration handle
212 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
215 * Handle to global configuration
217 static struct GNUNET_CONFIGURATION_Handle *cfg;
220 * Configuration of slave 3 controller
222 static struct GNUNET_CONFIGURATION_Handle *cfg3;
227 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
230 * Operation handle for linking controllers
232 static struct GNUNET_TESTBED_Operation *op;
235 * Handle to peer started at slave 1
237 static struct GNUNET_TESTBED_Peer *slave1_peer;
240 * Handle to peer started at slave 2
242 static struct GNUNET_TESTBED_Peer *slave2_peer;
245 * Handle to a peer started at master controller
247 static struct GNUNET_TESTBED_Peer *master_peer;
250 * The handle for whether a host is habitable or not
252 struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
255 * The task handle for the delay task
257 GNUNET_SCHEDULER_TaskIdentifier delay_task_id;
265 * Global testing status
267 static enum Stage result;
270 * shortcut to exit during failure
272 #define FAIL_TEST(cond) do { \
275 if (GNUNET_SCHEDULER_NO_TASK != abort_task) \
276 GNUNET_SCHEDULER_cancel (abort_task); \
277 abort_task = GNUNET_SCHEDULER_NO_TASK; \
278 GNUNET_SCHEDULER_add_now (do_shutdown, NULL); \
288 * @param tc the task context
291 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
293 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
294 GNUNET_SCHEDULER_cancel (abort_task);
295 if (GNUNET_SCHEDULER_NO_TASK != delay_task_id)
297 GNUNET_SCHEDULER_cancel (delay_task_id);
298 delay_task_id = GNUNET_SCHEDULER_NO_TASK;
300 if (NULL != hc_handle)
301 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
303 GNUNET_TESTBED_host_destroy (slave3);
305 GNUNET_TESTBED_host_destroy (slave2);
307 GNUNET_TESTBED_host_destroy (slave);
309 GNUNET_TESTBED_host_destroy (host);
311 GNUNET_TESTBED_controller_disconnect (mc);
313 GNUNET_CONFIGURATION_destroy (cfg);
315 GNUNET_CONFIGURATION_destroy (cfg3);
317 GNUNET_TESTBED_controller_stop (cp);
319 GNUNET_TESTBED_cancel_registration (rh);
324 * abort task to run on test timed out
327 * @param tc the task context
330 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
332 LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
333 abort_task = GNUNET_SCHEDULER_NO_TASK;
334 do_shutdown (cls, tc);
345 do_abort_now (void *cls)
347 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
348 GNUNET_SCHEDULER_cancel (abort_task);
349 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
354 * Task for inserting delay between tests
360 delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
362 delay_task_id = GNUNET_SCHEDULER_NO_TASK;
365 case SLAVE2_PEER_CREATE_SUCCESS:
366 op = GNUNET_TESTBED_peer_stop (NULL, slave1_peer, NULL, NULL);
367 FAIL_TEST (NULL != op);
369 case MASTER_SLAVE2_PEERS_CONNECTED:
370 op = GNUNET_TESTBED_peer_stop (NULL, slave2_peer, NULL, NULL);
371 FAIL_TEST (NULL != op);
380 * Functions of this signature are called when a peer has been successfully
383 * @param cls the closure from GNUNET_TESTBED_peer_create()
384 * @param peer the handle for the created peer; NULL on any error during
386 * @param emsg NULL if peer is not NULL; else MAY contain the error description
389 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
391 FAIL_TEST (NULL != peer);
392 FAIL_TEST (NULL == emsg);
396 result = MASTER_PEER_CREATE_SUCCESS;
398 GNUNET_TESTBED_operation_done (op);
399 op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
401 case SLAVE1_LINK_SUCCESS:
402 result = SLAVE1_PEER_CREATE_SUCCESS;
404 GNUNET_TESTBED_operation_done (op);
405 op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
407 case SLAVE2_LINK_SUCCESS:
408 result = SLAVE2_PEER_CREATE_SUCCESS;
410 GNUNET_TESTBED_operation_done (op);
412 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
413 (GNUNET_TIME_UNIT_SECONDS, 1),
420 FAIL_TEST (NULL != op);
425 * Checks the event if it is an operation finished event and if indicates a
426 * successfull completion of operation
428 * @param event the event information to check
431 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
433 FAIL_TEST (NULL != event);
434 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
435 FAIL_TEST (event->op == op);
436 FAIL_TEST (NULL == event->op_cls);
437 FAIL_TEST (NULL == event->details.operation_finished.emsg);
438 FAIL_TEST (NULL == event->details.operation_finished.generic);
443 * Callback which will be called to after a host registration succeeded or failed
445 * @param cls the host which has been registered
446 * @param emsg the error message; NULL if host registration is successful
449 registration_cont (void *cls, const char *emsg);
453 * Signature of the event handler function called by the
454 * respective event controller.
457 * @param event information about the event
460 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
464 case SLAVE2_REGISTERED:
465 check_operation_success (event);
466 GNUNET_TESTBED_operation_done (op);
468 result = SLAVE1_LINK_SUCCESS;
469 FAIL_TEST (NULL != slave2);
470 FAIL_TEST (NULL != slave);
471 op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
472 FAIL_TEST (NULL != op);
474 case SLAVE1_PEER_START_SUCCESS:
475 check_operation_success (event);
476 GNUNET_TESTBED_operation_done (op);
477 result = SLAVE2_LINK_SUCCESS;
478 op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
479 FAIL_TEST (NULL != op);
481 case MASTER_PEER_CREATE_SUCCESS:
482 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
483 FAIL_TEST (event->details.peer_start.host == host);
484 FAIL_TEST (event->details.peer_start.peer == master_peer);
485 GNUNET_TESTBED_operation_done (op);
486 result = MASTER_PEER_START_SUCCESS;
487 slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, cfg, 0);
488 FAIL_TEST (NULL != slave);
489 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
490 FAIL_TEST (NULL != rh);
492 case SLAVE1_PEER_CREATE_SUCCESS:
493 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
494 FAIL_TEST (event->details.peer_start.host == slave);
495 FAIL_TEST (event->details.peer_start.peer == slave1_peer);
496 GNUNET_TESTBED_operation_done (op);
497 result = SLAVE1_PEER_START_SUCCESS;
498 op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, cfg,
501 case SLAVE2_PEER_CREATE_SUCCESS:
502 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
503 FAIL_TEST (event->details.peer_stop.peer == slave1_peer);
504 GNUNET_TESTBED_operation_done (op);
505 result = SLAVE1_PEER_STOP_SUCCESS;
506 op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
507 FAIL_TEST (NULL != op);
509 case SLAVE1_PEER_STOP_SUCCESS:
510 FAIL_TEST (GNUNET_TESTBED_ET_PEER_START == event->type);
511 FAIL_TEST (event->details.peer_start.host == slave2);
512 FAIL_TEST (event->details.peer_start.peer == slave2_peer);
513 GNUNET_TESTBED_operation_done (op);
514 result = SLAVE2_PEER_START_SUCCESS;
515 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
518 case SLAVE2_PEER_START_SUCCESS:
519 FAIL_TEST (NULL != event);
520 FAIL_TEST (GNUNET_TESTBED_ET_CONNECT == event->type);
521 FAIL_TEST (event->details.peer_connect.peer1 == master_peer);
522 FAIL_TEST (event->details.peer_connect.peer2 == slave2_peer);
523 result = MASTER_SLAVE2_PEERS_CONNECTED;
524 GNUNET_TESTBED_operation_done (op);
527 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
528 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
531 case MASTER_SLAVE2_PEERS_CONNECTED:
532 FAIL_TEST (GNUNET_TESTBED_ET_PEER_STOP == event->type);
533 FAIL_TEST (event->details.peer_stop.peer == slave2_peer);
534 GNUNET_TESTBED_operation_done (op);
535 result = SLAVE2_PEER_STOP_SUCCESS;
536 op = GNUNET_TESTBED_peer_destroy (slave1_peer);
537 FAIL_TEST (NULL != op);
539 case SLAVE2_PEER_STOP_SUCCESS:
540 check_operation_success (event);
541 GNUNET_TESTBED_operation_done (op);
542 result = SLAVE1_PEER_DESTROY_SUCCESS;
543 op = GNUNET_TESTBED_peer_destroy (slave2_peer);
544 FAIL_TEST (NULL != op);
546 case SLAVE1_PEER_DESTROY_SUCCESS:
547 check_operation_success (event);
548 GNUNET_TESTBED_operation_done (op);
550 result = SLAVE2_PEER_DESTROY_SUCCESS;
551 slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, cfg, 0);
552 rh = GNUNET_TESTBED_register_host (mc, slave3, ®istration_cont, NULL);
554 case SLAVE3_REGISTERED:
555 check_operation_success (event);
556 GNUNET_TESTBED_operation_done (op);
558 result = SLAVE3_STARTED;
559 op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
560 FAIL_TEST (NULL != op);
563 FAIL_TEST (NULL != event);
564 FAIL_TEST (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
565 FAIL_TEST (event->op == op);
566 FAIL_TEST (NULL == event->op_cls);
567 FAIL_TEST (NULL == event->details.operation_finished.emsg);
568 cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
569 GNUNET_TESTBED_operation_done (op);
570 result = SLAVE3_GET_CONFIG_SUCCESS;
571 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, cfg3,
574 case SLAVE3_GET_CONFIG_SUCCESS:
575 result = SLAVE3_LINK_SUCCESS;
576 GNUNET_TESTBED_operation_done (op);
577 op = GNUNET_TESTBED_peer_destroy (master_peer);
579 case SLAVE3_LINK_SUCCESS:
580 check_operation_success (event);
582 GNUNET_TESTBED_operation_done (op);
584 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
585 (GNUNET_TIME_UNIT_SECONDS, 3), &do_shutdown,
595 * Callback which will be called to after a host registration succeeded or failed
597 * @param cls the host which has been registered
598 * @param emsg the error message; NULL if host registration is successful
601 registration_cont (void *cls, const char *emsg)
606 case MASTER_PEER_START_SUCCESS:
607 FAIL_TEST (NULL == emsg);
608 FAIL_TEST (NULL != mc);
609 result = SLAVE1_REGISTERED;
610 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, cfg, 0);
611 FAIL_TEST (NULL != slave2);
612 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
613 FAIL_TEST (NULL != rh);
615 case SLAVE1_REGISTERED:
616 FAIL_TEST (NULL == emsg);
617 FAIL_TEST (NULL != mc);
618 result = SLAVE2_REGISTERED;
619 FAIL_TEST (NULL != cfg);
620 op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, cfg,
622 FAIL_TEST (NULL != op);
624 case SLAVE2_PEER_DESTROY_SUCCESS:
625 FAIL_TEST (NULL == emsg);
626 FAIL_TEST (NULL != mc);
627 FAIL_TEST (NULL == op);
628 result = SLAVE3_REGISTERED;
629 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, cfg,
631 FAIL_TEST (NULL != op);
640 * Callback to signal successfull startup of the controller process
642 * @param cls the closure from GNUNET_TESTBED_controller_start()
643 * @param cfg the configuration with which the controller has been started;
644 * NULL if status is not GNUNET_OK
645 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
646 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
649 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
655 FAIL_TEST (GNUNET_OK == status);
657 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
658 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
659 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
660 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
661 mc = GNUNET_TESTBED_controller_connect (config, host, event_mask,
662 &controller_cb, NULL);
663 FAIL_TEST (NULL != mc);
664 result = MASTER_STARTED;
665 op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
666 FAIL_TEST (NULL != op);
677 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
678 * inform whether the given host is habitable or not. The Handle returned by
679 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
682 * @param host the host whose status is being reported; will be NULL if the host
683 * given to GNUNET_TESTBED_is_host_habitable() is NULL
684 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
687 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *_host,
691 if (GNUNET_NO == status)
694 "Unable to run the test as this system is not configured "
695 "to use password less SSH logins to localhost.\n"
697 GNUNET_SCHEDULER_cancel (abort_task);
698 abort_task = GNUNET_SCHEDULER_NO_TASK;
699 (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
703 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
712 * @param args arguments passed to GNUNET_PROGRAM_run
713 * @param cfgfile the path to configuration file
714 * @param cfg the configuration file handle
717 run (void *cls, char *const *args, const char *cfgfile,
718 const struct GNUNET_CONFIGURATION_Handle *config)
720 cfg = GNUNET_CONFIGURATION_dup (config);
721 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
722 FAIL_TEST (NULL != host);
725 GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
728 GNUNET_TESTBED_host_destroy (host);
729 GNUNET_CONFIGURATION_destroy (cfg);
733 "Unable to run the test as this system is not configured "
734 "to use password less SSH logins to localhost.\n"
735 "Marking test as successful\n");
740 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
741 (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
750 main (int argc, char **argv)
752 char *const argv2[] = { "test_testbed_api_controllerlink",
753 "-c", "test_testbed_api.conf",
756 struct GNUNET_GETOPT_CommandLineOption options[] = {
757 GNUNET_GETOPT_OPTION_END
763 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
764 "test_testbed_api_controllerlink", "nohelp", options,
766 if (GNUNET_OK != ret)
773 return 77; /* Mark test as skipped */
779 /* end of test_testbed_api_controllerlink.c */