2 This file is part of GNUnet
3 (C) 2008--2012 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;
260 * Global testing status
262 static enum Stage result;
269 * @param tc the task context
272 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
274 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
275 GNUNET_SCHEDULER_cancel (abort_task);
276 if (NULL != hc_handle)
277 GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
279 GNUNET_TESTBED_host_destroy (slave3);
281 GNUNET_TESTBED_host_destroy (slave2);
283 GNUNET_TESTBED_host_destroy (slave);
285 GNUNET_TESTBED_host_destroy (host);
287 GNUNET_TESTBED_controller_disconnect (mc);
289 GNUNET_CONFIGURATION_destroy (cfg);
291 GNUNET_CONFIGURATION_destroy (cfg3);
293 GNUNET_TESTBED_controller_stop (cp);
295 GNUNET_TESTBED_cancel_registration (rh);
300 * abort task to run on test timed out
303 * @param tc the task context
306 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
308 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
309 abort_task = GNUNET_SCHEDULER_NO_TASK;
310 do_shutdown (cls, tc);
321 do_abort_now (void *cls)
323 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
324 GNUNET_SCHEDULER_cancel (abort_task);
325 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
330 * Task for inserting delay between tests
336 delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
340 case SLAVE2_PEER_CREATE_SUCCESS:
341 op = GNUNET_TESTBED_peer_stop (slave1_peer, NULL, NULL);
342 GNUNET_assert (NULL != op);
344 case MASTER_SLAVE2_PEERS_CONNECTED:
345 op = GNUNET_TESTBED_peer_stop (slave2_peer, NULL, NULL);
346 GNUNET_assert (NULL != op);
355 * Functions of this signature are called when a peer has been successfully
358 * @param cls the closure from GNUNET_TESTBED_peer_create()
359 * @param peer the handle for the created peer; NULL on any error during
361 * @param emsg NULL if peer is not NULL; else MAY contain the error description
364 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
366 GNUNET_assert (NULL != peer);
367 GNUNET_assert (NULL == emsg);
371 result = MASTER_PEER_CREATE_SUCCESS;
373 GNUNET_TESTBED_operation_done (op);
374 op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
376 case SLAVE1_LINK_SUCCESS:
377 result = SLAVE1_PEER_CREATE_SUCCESS;
379 GNUNET_TESTBED_operation_done (op);
380 op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
382 case SLAVE2_LINK_SUCCESS:
383 result = SLAVE2_PEER_CREATE_SUCCESS;
385 GNUNET_TESTBED_operation_done (op);
386 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
387 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
393 GNUNET_assert (NULL != op);
398 * Checks the event if it is an operation finished event and if indicates a
399 * successfull completion of operation
401 * @param event the event information to check
404 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
406 GNUNET_assert (NULL != event);
407 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
408 GNUNET_assert (event->details.operation_finished.operation == op);
409 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
410 GNUNET_assert (NULL == event->details.operation_finished.emsg);
411 GNUNET_assert (NULL == event->details.operation_finished.generic);
416 * Callback which will be called to after a host registration succeeded or failed
418 * @param cls the host which has been registered
419 * @param emsg the error message; NULL if host registration is successful
422 registration_cont (void *cls, const char *emsg);
426 * Signature of the event handler function called by the
427 * respective event controller.
430 * @param event information about the event
433 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
437 case SLAVE2_REGISTERED:
438 check_operation_success (event);
439 GNUNET_TESTBED_operation_done (op);
441 result = SLAVE1_LINK_SUCCESS;
442 GNUNET_assert (NULL != slave2);
443 GNUNET_assert (NULL != slave);
444 op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
445 GNUNET_assert (NULL != op);
447 case SLAVE1_PEER_START_SUCCESS:
448 check_operation_success (event);
449 GNUNET_TESTBED_operation_done (op);
450 result = SLAVE2_LINK_SUCCESS;
451 op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
452 GNUNET_assert (NULL != op);
454 case MASTER_PEER_CREATE_SUCCESS:
455 GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
456 GNUNET_assert (event->details.peer_start.host == host);
457 GNUNET_assert (event->details.peer_start.peer == master_peer);
458 GNUNET_TESTBED_operation_done (op);
459 result = MASTER_PEER_START_SUCCESS;
460 slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, 0);
461 GNUNET_assert (NULL != slave);
462 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
463 GNUNET_assert (NULL != rh);
465 case SLAVE1_PEER_CREATE_SUCCESS:
466 GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
467 GNUNET_assert (event->details.peer_start.host == slave);
468 GNUNET_assert (event->details.peer_start.peer == slave1_peer);
469 GNUNET_TESTBED_operation_done (op);
470 result = SLAVE1_PEER_START_SUCCESS;
471 op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, cfg,
474 case SLAVE2_PEER_CREATE_SUCCESS:
475 GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type);
476 GNUNET_assert (event->details.peer_stop.peer == slave1_peer);
477 GNUNET_TESTBED_operation_done (op);
478 result = SLAVE1_PEER_STOP_SUCCESS;
479 op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
480 GNUNET_assert (NULL != op);
482 case SLAVE1_PEER_STOP_SUCCESS:
483 GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
484 GNUNET_assert (event->details.peer_start.host == slave2);
485 GNUNET_assert (event->details.peer_start.peer == slave2_peer);
486 GNUNET_TESTBED_operation_done (op);
487 result = SLAVE2_PEER_START_SUCCESS;
488 op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
491 case SLAVE2_PEER_START_SUCCESS:
492 GNUNET_assert (NULL != event);
493 GNUNET_assert (GNUNET_TESTBED_ET_CONNECT == event->type);
494 GNUNET_assert (event->details.peer_connect.peer1 == master_peer);
495 GNUNET_assert (event->details.peer_connect.peer2 == slave2_peer);
496 result = MASTER_SLAVE2_PEERS_CONNECTED;
497 GNUNET_TESTBED_operation_done (op);
499 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
500 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
503 case MASTER_SLAVE2_PEERS_CONNECTED:
504 GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type);
505 GNUNET_assert (event->details.peer_stop.peer == slave2_peer);
506 GNUNET_TESTBED_operation_done (op);
507 result = SLAVE2_PEER_STOP_SUCCESS;
508 op = GNUNET_TESTBED_peer_destroy (slave1_peer);
509 GNUNET_assert (NULL != op);
511 case SLAVE2_PEER_STOP_SUCCESS:
512 check_operation_success (event);
513 GNUNET_TESTBED_operation_done (op);
514 result = SLAVE1_PEER_DESTROY_SUCCESS;
515 op = GNUNET_TESTBED_peer_destroy (slave2_peer);
516 GNUNET_assert (NULL != op);
518 case SLAVE1_PEER_DESTROY_SUCCESS:
519 check_operation_success (event);
520 GNUNET_TESTBED_operation_done (op);
522 result = SLAVE2_PEER_DESTROY_SUCCESS;
523 slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, 0);
524 rh = GNUNET_TESTBED_register_host (mc, slave3, ®istration_cont, NULL);
526 case SLAVE3_REGISTERED:
527 check_operation_success (event);
528 GNUNET_TESTBED_operation_done (op);
530 result = SLAVE3_STARTED;
531 op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
532 GNUNET_assert (NULL != op);
535 GNUNET_assert (NULL != event);
536 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
537 GNUNET_assert (event->details.operation_finished.operation == op);
538 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
539 GNUNET_assert (NULL == event->details.operation_finished.emsg);
540 cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
541 GNUNET_TESTBED_operation_done (op);
542 result = SLAVE3_GET_CONFIG_SUCCESS;
543 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, cfg3, GNUNET_NO);
545 case SLAVE3_GET_CONFIG_SUCCESS:
546 result = SLAVE3_LINK_SUCCESS;
547 GNUNET_TESTBED_operation_done (op);
548 op = GNUNET_TESTBED_peer_destroy (master_peer);
550 case SLAVE3_LINK_SUCCESS:
551 check_operation_success (event);
553 GNUNET_TESTBED_operation_done (op);
555 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
556 (GNUNET_TIME_UNIT_SECONDS, 3),
566 * Callback which will be called to after a host registration succeeded or failed
568 * @param cls the host which has been registered
569 * @param emsg the error message; NULL if host registration is successful
572 registration_cont (void *cls, const char *emsg)
577 case MASTER_PEER_START_SUCCESS:
578 GNUNET_assert (NULL == emsg);
579 GNUNET_assert (NULL != mc);
580 result = SLAVE1_REGISTERED;
581 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, 0);
582 GNUNET_assert (NULL != slave2);
583 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
584 GNUNET_assert (NULL != rh);
586 case SLAVE1_REGISTERED:
587 GNUNET_assert (NULL == emsg);
588 GNUNET_assert (NULL != mc);
589 result = SLAVE2_REGISTERED;
590 GNUNET_assert (NULL != cfg);
591 op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, cfg, GNUNET_YES);
592 GNUNET_assert (NULL != op);
594 case SLAVE2_PEER_DESTROY_SUCCESS:
595 GNUNET_assert (NULL == emsg);
596 GNUNET_assert (NULL != mc);
597 GNUNET_assert (NULL == op);
598 result = SLAVE3_REGISTERED;
599 op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, cfg, GNUNET_YES);
600 GNUNET_assert (NULL != op);
609 * Callback to signal successfull startup of the controller process
611 * @param cls the closure from GNUNET_TESTBED_controller_start()
612 * @param cfg the configuration with which the controller has been started;
613 * NULL if status is not GNUNET_OK
614 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
615 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
618 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
624 GNUNET_assert (GNUNET_OK == status);
626 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
627 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
628 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
629 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
630 mc = GNUNET_TESTBED_controller_connect (config, host, event_mask,
631 &controller_cb, NULL);
632 GNUNET_assert (NULL != mc);
633 result = MASTER_STARTED;
634 op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
635 GNUNET_assert (NULL != op);
646 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
647 * inform whether the given host is habitable or not. The Handle returned by
648 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
651 * @param host the host whose status is being reported; will be NULL if the host
652 * given to GNUNET_TESTBED_is_host_habitable() is NULL
653 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
656 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *_host, int status)
659 if (GNUNET_NO == status)
662 "Unable to run the test as this system is not configured "
663 "to use password less SSH logins to localhost.\n"
665 GNUNET_SCHEDULER_cancel (abort_task);
666 abort_task = GNUNET_SCHEDULER_NO_TASK;
667 (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
671 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
680 * @param args arguments passed to GNUNET_PROGRAM_run
681 * @param cfgfile the path to configuration file
682 * @param cfg the configuration file handle
685 run (void *cls, char *const *args, const char *cfgfile,
686 const struct GNUNET_CONFIGURATION_Handle *config)
688 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
689 GNUNET_assert (NULL != host);
690 if (NULL == (hc_handle = GNUNET_TESTBED_is_host_habitable (host, config,
694 GNUNET_TESTBED_host_destroy (host);
697 "Unable to run the test as this system is not configured "
698 "to use password less SSH logins to localhost.\n"
699 "Marking test as successful\n");
703 cfg = GNUNET_CONFIGURATION_dup (config);
705 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
706 (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
715 main (int argc, char **argv)
717 char *const argv2[] = { "test_testbed_api_controllerlink",
718 "-c", "test_testbed_api.conf",
721 struct GNUNET_GETOPT_CommandLineOption options[] = {
722 GNUNET_GETOPT_OPTION_END
728 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
729 "test_testbed_api_controllerlink", "nohelp", options,
731 if (GNUNET_OK != ret)
738 return 77; /* Mark test as skipped */
744 /* end of test_testbed_api_controllerlink.c */