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_2peers_2controllers.c
23 * @brief testcases for the testbed api: 2 peers are configured, started and
24 * connected together. Each peer resides on its own controller
25 * @author Sree Harsha Totakura
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib-new.h"
31 #include "gnunet_testbed_service.h"
35 * Generic logging shortcut
37 #define LOG(kind,...) \
38 GNUNET_log (kind, __VA_ARGS__)
41 * Relative time seconds shorthand
43 #define TIME_REL_SECS(sec) \
44 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
55 struct GNUNET_TESTBED_Peer *peer;
58 * Operations involving this peer
60 struct GNUNET_TESTBED_Operation *operation;
63 * set to GNUNET_YES when peer is started
71 static struct GNUNET_TESTBED_Host *host;
74 * The controller process of one controller
76 static struct GNUNET_TESTBED_ControllerProc *cp1;
79 * The controller process of another controller
81 static struct GNUNET_TESTBED_ControllerProc *cp2;
86 static struct GNUNET_TESTBED_Host *neighbour;
89 * Handle for neighbour registration
91 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
94 * The controller handle of one controller
96 static struct GNUNET_TESTBED_Controller *controller1;
99 * The controller handle of another controller
101 static struct GNUNET_TESTBED_Controller *controller2;
106 static struct PeerContext peer1;
111 static struct PeerContext peer2;
114 * Handle to starting configuration
116 static struct GNUNET_CONFIGURATION_Handle *cfg;
119 * Handle to controller2 configuration, used to establish lateral link from
122 static struct GNUNET_CONFIGURATION_Handle *cfg2;
125 * Handle to operations involving both peers
127 static struct GNUNET_TESTBED_Operation *common_operation;
130 * Abort task identifier
132 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
135 * Delayed connect job identifier
137 static GNUNET_SCHEDULER_TaskIdentifier delayed_connect_task;
140 * Different stages in testing
151 * Controller 1 has started
166 * Controller 2 has started
181 * peers are connected
186 * Peers are connected once again (this should not fail as they are already connected)
196 * Final success stage
204 static enum Stage result;
211 * @param tc the task context
214 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
216 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
217 GNUNET_SCHEDULER_cancel (abort_task);
218 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
219 if (NULL != reg_handle)
220 GNUNET_TESTBED_cancel_registration (reg_handle);
221 if (NULL != controller1)
222 GNUNET_TESTBED_controller_disconnect (controller1);
223 if (NULL != controller2)
224 GNUNET_TESTBED_controller_disconnect (controller2);
225 GNUNET_CONFIGURATION_destroy (cfg);
227 GNUNET_CONFIGURATION_destroy (cfg2);
229 GNUNET_TESTBED_controller_stop (cp1);
231 GNUNET_TESTBED_controller_stop (cp2);
232 GNUNET_TESTBED_host_destroy (host);
233 GNUNET_TESTBED_host_destroy (neighbour);
238 * abort task to run on test timed out
241 * @param tc the task context
244 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
246 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
247 abort_task = GNUNET_SCHEDULER_NO_TASK;
248 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == delayed_connect_task);
249 do_shutdown (cls, tc);
254 * Callback to be called when an operation is completed
256 * @param cls the callback closure from functions generating an operation
257 * @param op the operation that has been finished
258 * @param emsg error message in case the operation has failed; will be NULL if
259 * operation has executed successfully.
262 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
266 * task for delaying a connect
269 * @param tc the task context
272 do_delayed_connect (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
274 delayed_connect_task = GNUNET_SCHEDULER_NO_TASK;
275 GNUNET_assert (NULL == common_operation);
276 common_operation = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL,
277 peer1.peer, peer2.peer);
282 * Callback to be called when an operation is completed
284 * @param cls the callback closure from functions generating an operation
285 * @param op the operation that has been finished
286 * @param emsg error message in case the operation has failed; will be NULL if
287 * operation has executed successfully.
290 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
292 GNUNET_assert (common_operation == op);
296 GNUNET_assert (NULL == peer1.operation);
297 GNUNET_assert (NULL == peer2.operation);
298 GNUNET_assert (NULL != common_operation);
299 GNUNET_TESTBED_operation_done (common_operation);
300 common_operation = NULL;
301 result = PEERS_CONNECTED;
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
303 delayed_connect_task =
304 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3),
305 &do_delayed_connect, NULL);
307 case PEERS_CONNECTED:
308 GNUNET_assert (NULL == peer1.operation);
309 GNUNET_assert (NULL == peer2.operation);
310 GNUNET_assert (NULL != common_operation);
311 GNUNET_TESTBED_operation_done (common_operation);
312 common_operation = NULL;
313 result = PEERS_CONNECTED_2;
314 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
315 peer1.operation = GNUNET_TESTBED_peer_stop (peer1.peer, NULL, NULL);
316 peer2.operation = GNUNET_TESTBED_peer_stop (peer2.peer, NULL, NULL);
325 * Functions of this signature are called when a peer has been successfully
329 * @param peer the handle for the created peer; NULL on any error during
331 * @param emsg NULL if peer is not NULL; else MAY contain the error description
334 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
339 GNUNET_assert (NULL != peer1.operation);
340 GNUNET_assert (NULL != peer);
341 GNUNET_assert (NULL == peer1.peer);
343 GNUNET_TESTBED_operation_done (peer1.operation);
344 result = PEER1_CREATED;
345 peer1.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
348 GNUNET_assert (NULL != peer2.operation);
349 GNUNET_assert (NULL != peer);
350 GNUNET_assert (NULL == peer2.peer);
352 GNUNET_TESTBED_operation_done (peer2.operation);
353 result = PEER2_CREATED;
354 peer2.operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
363 * Callback to signal successfull startup of the controller process
365 * @param cls the closure from GNUNET_TESTBED_controller_start()
366 * @param cfg the configuration with which the controller has been started;
367 * NULL if status is not GNUNET_OK
368 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
369 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
372 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status);
376 * Signature of the event handler function called by the
377 * respective event controller.
380 * @param event information about the event
383 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
387 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
388 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
389 GNUNET_assert (NULL == event->details.operation_finished.emsg);
390 GNUNET_assert (NULL == event->details.operation_finished.generic);
394 if (event->details.operation_finished.operation == peer1.operation)
396 GNUNET_TESTBED_operation_done (peer1.operation);
397 peer1.operation = NULL;
400 else if (event->details.operation_finished.operation == peer2.operation)
402 GNUNET_TESTBED_operation_done (peer2.operation);
403 peer2.operation = NULL;
408 if ((NULL == peer1.peer) && (NULL == peer2.peer))
411 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
415 GNUNET_assert (NULL != common_operation);
416 GNUNET_TESTBED_operation_done (common_operation);
417 common_operation = NULL;
418 result = CONTROLLER2_UP;
420 GNUNET_TESTBED_peer_create (controller1, neighbour, cfg, &peer_create_cb,
422 GNUNET_assert (NULL != peer2.operation);
428 case GNUNET_TESTBED_ET_PEER_START:
432 GNUNET_assert (event->details.peer_start.host == host);
433 peer1.is_running = GNUNET_YES;
434 GNUNET_TESTBED_operation_done (peer1.operation);
435 peer1.operation = NULL;
436 result = PEER1_STARTED;
438 GNUNET_TESTBED_controller_link (controller1, neighbour, NULL, cfg, GNUNET_YES);
441 GNUNET_assert (event->details.peer_start.host == neighbour);
442 peer2.is_running = GNUNET_YES;
443 GNUNET_TESTBED_operation_done (peer2.operation);
444 peer2.operation = NULL;
445 result = PEER2_STARTED;
447 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
454 case GNUNET_TESTBED_ET_PEER_STOP:
455 GNUNET_assert (PEERS_CONNECTED_2 == result);
456 if (event->details.peer_stop.peer == peer1.peer)
458 peer1.is_running = GNUNET_NO;
459 GNUNET_TESTBED_operation_done (peer1.operation);
461 else if (event->details.peer_stop.peer == peer2.peer)
463 peer2.is_running = GNUNET_NO;
464 GNUNET_TESTBED_operation_done (peer2.operation);
468 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running))
470 result = PEERS_STOPPED;
471 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
472 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
475 case GNUNET_TESTBED_ET_CONNECT:
479 case PEERS_CONNECTED:
480 GNUNET_assert (NULL == peer1.operation);
481 GNUNET_assert (NULL == peer2.operation);
482 GNUNET_assert (NULL != common_operation);
483 GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer) &&
484 (event->details.peer_connect.peer2 == peer2.peer));
497 * Callback which will be called to after a host registration succeeded or failed
499 * @param cls the host which has been registered
500 * @param emsg the error message; NULL if host registration is successful
503 registration_comp (void *cls, const char *emsg)
505 GNUNET_assert (cls == neighbour);
508 GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
510 GNUNET_assert (NULL != peer1.operation);
515 * Callback to signal successfull startup of the controller process
517 * @param cls the closure from GNUNET_TESTBED_controller_start()
518 * @param cfg the configuration with which the controller has been started;
519 * NULL if status is not GNUNET_OK
520 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
521 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
524 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
528 GNUNET_assert (GNUNET_OK == status);
530 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
531 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
532 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
533 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
538 GNUNET_TESTBED_controller_connect (config, host, event_mask, &controller_cb,
540 GNUNET_assert (NULL != controller1);
541 result = CONTROLLER1_UP;
542 neighbour = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, 0);
543 GNUNET_assert (NULL != neighbour);
545 GNUNET_TESTBED_register_host (controller1, neighbour, ®istration_comp,
547 GNUNET_assert (NULL != reg_handle);
561 * @param args arguments passed to GNUNET_PROGRAM_run
562 * @param cfgfile the path to configuration file
563 * @param cfg the configuration file handle
566 run (void *cls, char *const *args, const char *cfgfile,
567 const struct GNUNET_CONFIGURATION_Handle *config)
569 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
570 GNUNET_assert (NULL != host);
571 cfg = GNUNET_CONFIGURATION_dup (config);
572 cp1 = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
575 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
576 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
585 main (int argc, char **argv)
589 char *const argv2[] = { "test_testbed_api_2peers_2controllers",
590 "-c", "test_testbed_api.conf",
593 struct GNUNET_GETOPT_CommandLineOption options[] = {
594 GNUNET_GETOPT_OPTION_END
598 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
599 "test_testbed_api_2peers_2controllers", "nohelp",
600 options, &run, NULL);
601 if ((GNUNET_OK != ret) || (SUCCESS != result))
606 /* end of test_testbed_api_2peers_2controllers.c */