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>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib-new.h"
30 #include "gnunet_testbed_service.h"
33 * Generic logging shortcut
35 #define LOG(kind,...) \
36 GNUNET_log (kind, __VA_ARGS__)
39 * Debug logging shorthand
41 #define LOG_DEBUG(...) \
42 LOG(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
45 * Different stages in testing
56 * Master controller has started
61 * The first slave has been registered at master controller
66 * The second slave has been registered at the master controller
71 * Link from master to slave 1 has been successfully created
76 * Link from slave 1 to slave 2 has been successfully created.
81 * Peer create on slave 1 successful
83 SLAVE1_PEER_CREATE_SUCCESS,
86 * Peer create on slave 2 successful
88 SLAVE2_PEER_CREATE_SUCCESS,
91 * Peer startup on slave 1 successful
93 SLAVE1_PEER_START_SUCCESS,
96 * Peer on slave 1 successfully stopped
98 SLAVE1_PEER_STOP_SUCCESS,
101 * Peer startup on slave 2 successful
103 SLAVE2_PEER_START_SUCCESS,
106 * Peer on slave 2 successfully stopped
108 SLAVE2_PEER_STOP_SUCCESS,
111 * Peer destroy on slave 1 successful
113 SLAVE1_PEER_DESTROY_SUCCESS,
116 * Peer destory on slave 2 successful; Marks test as successful
122 * Host for running master controller
124 static struct GNUNET_TESTBED_Host *host;
127 * The master controller process
129 static struct GNUNET_TESTBED_ControllerProc *cp;
132 * Handle to master controller
134 static struct GNUNET_TESTBED_Controller *mc;
137 * Slave host for running slave controller
139 static struct GNUNET_TESTBED_Host *slave;
142 * Another slave host for running another slave controller
144 static struct GNUNET_TESTBED_Host *slave2;
147 * Slave host registration handle
149 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
152 * Handle to global configuration
154 static struct GNUNET_CONFIGURATION_Handle *cfg;
159 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
162 * Operation handle for linking controllers
164 static struct GNUNET_TESTBED_Operation *op;
167 * Handle to peer started at slave 1
169 static struct GNUNET_TESTBED_Peer *slave1_peer;
172 * Handle to peer started at slave 2
174 static struct GNUNET_TESTBED_Peer *slave2_peer;
182 * Global testing status
184 static enum Stage result;
191 * @param tc the task context
194 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
196 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
197 GNUNET_SCHEDULER_cancel (abort_task);
199 GNUNET_TESTBED_host_destroy (slave2);
201 GNUNET_TESTBED_host_destroy (slave);
203 GNUNET_TESTBED_host_destroy (host);
205 GNUNET_TESTBED_controller_disconnect (mc);
207 GNUNET_CONFIGURATION_destroy (cfg);
209 GNUNET_TESTBED_controller_stop (cp);
211 GNUNET_TESTBED_cancel_registration (rh);
217 * abort task to run on test timed out
220 * @param tc the task context
223 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
225 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
226 abort_task = GNUNET_SCHEDULER_NO_TASK;
227 do_shutdown (cls, tc);
232 * Task for inserting delay between tests
238 delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
242 case SLAVE1_PEER_START_SUCCESS:
243 op = GNUNET_TESTBED_peer_stop (slave1_peer);
244 GNUNET_assert (NULL != op);
246 case SLAVE2_PEER_START_SUCCESS:
247 op = GNUNET_TESTBED_peer_stop (slave2_peer);
248 GNUNET_assert (NULL != op);
257 * Functions of this signature are called when a peer has been successfully
260 * @param cls the closure from GNUNET_TESTBED_peer_create()
261 * @param peer the handle for the created peer; NULL on any error during
263 * @param emsg NULL if peer is not NULL; else MAY contain the error description
266 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
270 case SLAVE2_LINK_SUCCESS:
271 GNUNET_assert (NULL != peer);
272 GNUNET_assert (NULL == emsg);
273 result = SLAVE1_PEER_CREATE_SUCCESS;
275 GNUNET_TESTBED_operation_done (op);
276 op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
277 GNUNET_assert (NULL != op);
279 case SLAVE1_PEER_CREATE_SUCCESS:
280 GNUNET_assert (NULL != peer);
281 GNUNET_assert (NULL == emsg);
282 result = SLAVE2_PEER_CREATE_SUCCESS;
284 GNUNET_TESTBED_operation_done (op);
285 op = GNUNET_TESTBED_peer_start (slave1_peer);
286 GNUNET_assert (NULL != op);
295 * Checks the event if it is an operation finished event and if indicates a
296 * successfull completion of operation
298 * @param event the event information to check
301 check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
303 GNUNET_assert (NULL != event);
304 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
305 GNUNET_assert (event->details.operation_finished.operation == op);
306 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
307 GNUNET_assert (NULL == event->details.operation_finished.emsg);
308 GNUNET_assert (NULL == event->details.operation_finished.generic);
313 * Signature of the event handler function called by the
314 * respective event controller.
317 * @param event information about the event
320 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
324 case SLAVE2_REGISTERED:
325 check_operation_success (event);
326 GNUNET_TESTBED_operation_done (op);
328 result = SLAVE1_LINK_SUCCESS;
329 GNUNET_assert (NULL != slave2);
330 GNUNET_assert (NULL != slave);
331 op = GNUNET_TESTBED_controller_link (mc, slave2, slave, cfg, GNUNET_YES);
332 GNUNET_assert (NULL != op);
334 case SLAVE1_LINK_SUCCESS:
335 check_operation_success (event);
336 GNUNET_TESTBED_operation_done (op);
338 result = SLAVE2_LINK_SUCCESS;
339 op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
340 GNUNET_assert (NULL != op);
342 case SLAVE2_PEER_CREATE_SUCCESS:
343 GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
344 GNUNET_assert (event->details.peer_start.host == slave);
345 GNUNET_assert (event->details.peer_start.peer == slave1_peer);
346 GNUNET_TESTBED_operation_done (op);
347 result = SLAVE1_PEER_START_SUCCESS;
348 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
349 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
352 case SLAVE1_PEER_START_SUCCESS:
353 GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type);
354 GNUNET_assert (event->details.peer_stop.peer == slave1_peer);
355 GNUNET_TESTBED_operation_done (op);
356 result = SLAVE1_PEER_STOP_SUCCESS;
357 op = GNUNET_TESTBED_peer_start (slave2_peer);
358 GNUNET_assert (NULL != op);
360 case SLAVE1_PEER_STOP_SUCCESS:
361 GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
362 GNUNET_assert (event->details.peer_start.host == slave2);
363 GNUNET_assert (event->details.peer_start.peer == slave2_peer);
364 GNUNET_TESTBED_operation_done (op);
365 result = SLAVE2_PEER_START_SUCCESS;
366 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
367 (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
370 case SLAVE2_PEER_START_SUCCESS:
371 GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type);
372 GNUNET_assert (event->details.peer_stop.peer == slave2_peer);
373 GNUNET_TESTBED_operation_done (op);
374 result = SLAVE2_PEER_STOP_SUCCESS;
375 op = GNUNET_TESTBED_peer_destroy (slave1_peer);
376 GNUNET_assert (NULL != op);
378 case SLAVE2_PEER_STOP_SUCCESS:
379 check_operation_success (event);
380 GNUNET_TESTBED_operation_done (op);
381 result = SLAVE1_PEER_DESTROY_SUCCESS;
382 op = GNUNET_TESTBED_peer_destroy (slave2_peer);
383 GNUNET_assert (NULL != op);
385 case SLAVE1_PEER_DESTROY_SUCCESS:
386 check_operation_success (event);
387 GNUNET_TESTBED_operation_done (op);
389 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
398 * Callback which will be called to after a host registration succeeded or failed
400 * @param cls the host which has been registered
401 * @param emsg the error message; NULL if host registration is successful
404 registration_cont (void *cls, const char *emsg)
410 GNUNET_assert (NULL == emsg);
411 GNUNET_assert (NULL != mc);
412 result = SLAVE1_REGISTERED;
413 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, 0);
414 GNUNET_assert (NULL != slave2);
415 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
416 GNUNET_assert (NULL != rh);
418 case SLAVE1_REGISTERED:
419 GNUNET_assert (NULL == emsg);
420 GNUNET_assert (NULL != mc);
421 result = SLAVE2_REGISTERED;
422 GNUNET_assert (NULL != cfg);
423 op = GNUNET_TESTBED_controller_link (mc, slave, NULL, cfg, GNUNET_YES);
424 GNUNET_assert (NULL != op);
432 * Callback to signal successfull startup of the controller process
434 * @param cls the closure from GNUNET_TESTBED_controller_start()
435 * @param cfg the configuration with which the controller has been started;
436 * NULL if status is not GNUNET_OK
437 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
438 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
441 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
447 GNUNET_assert (GNUNET_OK == status);
449 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
450 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
451 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
452 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
453 mc = GNUNET_TESTBED_controller_connect (config, host, event_mask,
454 &controller_cb, NULL);
455 GNUNET_assert (NULL != mc);
456 result = MASTER_STARTED;
457 slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, 0);
458 GNUNET_assert (NULL != slave);
459 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
460 GNUNET_assert (NULL != rh);
472 * @param args arguments passed to GNUNET_PROGRAM_run
473 * @param cfgfile the path to configuration file
474 * @param cfg the configuration file handle
477 run (void *cls, char *const *args, const char *cfgfile,
478 const struct GNUNET_CONFIGURATION_Handle *config)
480 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
481 GNUNET_assert (NULL != host);
482 cfg = GNUNET_CONFIGURATION_dup (config);
483 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
486 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
487 (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
496 main (int argc, char **argv)
500 char *const argv2[] = { "test_testbed_api_controllerlink",
501 "-c", "test_testbed_api.conf",
504 struct GNUNET_GETOPT_CommandLineOption options[] = {
505 GNUNET_GETOPT_OPTION_END
507 char *const remote_args[] = {
508 "ssh", "-o", "BatchMode=yes", "127.0.0.1", "echo", "Hello", "World", NULL
510 struct GNUNET_OS_Process *auxp;
511 enum GNUNET_OS_ProcessStatusType type;
515 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
516 NULL, "ssh", remote_args);
517 GNUNET_assert (NULL != auxp);
520 ret = GNUNET_OS_process_status (auxp, &type, &code);
521 GNUNET_assert (GNUNET_SYSERR != ret);
524 while (GNUNET_NO == ret);
525 GNUNET_OS_process_destroy (auxp);
528 (void) printf ("Unable to run the test as this system is not configured "
529 "to use password less SSH logins to localhost.\n"
530 "Marking test as successful\n");
535 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
536 "test_testbed_api_controllerlink", "nohelp", options,
538 if ((GNUNET_OK != ret) || (SUCCESS != result))