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_2peers_1controller.c
23 * @brief testcases for the testbed api: 2 peers are configured, started and
24 * connected together. The 2 peer reside on a single controller.
25 * @author Sree Harsha Totakura
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib.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)
54 struct GNUNET_TESTBED_Peer *peer;
57 * Operations involving this peer
59 struct GNUNET_TESTBED_Operation *operation;
62 * set to GNUNET_YES when peer is started
70 static struct GNUNET_TESTBED_Host *host;
73 * The controller process
75 static struct GNUNET_TESTBED_ControllerProc *cp;
78 * The controller handle
80 static struct GNUNET_TESTBED_Controller *controller;
85 static struct GNUNET_TESTBED_Host *neighbour;
88 * Handle for neighbour registration
90 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
95 static struct PeerContext peer1;
100 static struct PeerContext peer2;
103 * Handle to configuration
105 static struct GNUNET_CONFIGURATION_Handle *cfg;
108 * Handle to operations involving both peers
110 static struct GNUNET_TESTBED_Operation *common_operation;
113 * Abort task identifier
115 static struct GNUNET_SCHEDULER_Task * abort_task;
118 * Delayed connect job identifier
120 static struct GNUNET_SCHEDULER_Task * delayed_connect_task;
123 * Different stages in testing
144 * peers are connected
149 * Peers are connected once again (this should not fail as they are already connected)
159 * Final success stage
167 static enum Stage result;
171 * shortcut to exit during failure
173 #define FAIL_TEST(cond) do { \
176 if (NULL != abort_task) \
177 GNUNET_SCHEDULER_cancel (abort_task); \
179 GNUNET_SCHEDULER_add_now (do_shutdown, NULL); \
191 do_shutdown (void *cls)
193 if (NULL != abort_task)
194 GNUNET_SCHEDULER_cancel (abort_task);
195 if (NULL != delayed_connect_task)
196 GNUNET_SCHEDULER_cancel (delayed_connect_task);
197 if (NULL != reg_handle)
198 GNUNET_TESTBED_cancel_registration (reg_handle);
199 GNUNET_TESTBED_controller_disconnect (controller);
200 GNUNET_CONFIGURATION_destroy (cfg);
202 GNUNET_TESTBED_controller_stop (cp);
203 GNUNET_TESTBED_host_destroy (neighbour);
204 GNUNET_TESTBED_host_destroy (host);
209 * abort task to run on test timed out
216 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
223 * Callback to be called when an operation is completed
225 * @param cls the callback closure from functions generating an operation
226 * @param op the operation that has been finished
227 * @param emsg error message in case the operation has failed; will be NULL if
228 * operation has executed successfully.
231 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
235 * task for delaying a connect
240 do_delayed_connect (void *cls)
242 delayed_connect_task = NULL;
243 FAIL_TEST (NULL == common_operation);
245 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
251 * Callback to be called when an operation is completed
253 * @param cls the callback closure from functions generating an operation
254 * @param op the operation that has been finished
255 * @param emsg error message in case the operation has failed; will be NULL if
256 * operation has executed successfully.
259 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
261 FAIL_TEST (common_operation == op);
265 FAIL_TEST (NULL == peer1.operation);
266 FAIL_TEST (NULL == peer2.operation);
267 FAIL_TEST (NULL != common_operation);
269 case PEERS_CONNECTED:
270 FAIL_TEST (NULL == peer1.operation);
271 FAIL_TEST (NULL == peer2.operation);
272 FAIL_TEST (NULL != common_operation);
281 * Signature of the event handler function called by the
282 * respective event controller.
285 * @param event information about the event
288 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
292 case GNUNET_TESTBED_ET_OPERATION_FINISHED: /* Will be reached when we destroy peers */
293 FAIL_TEST (PEERS_STOPPED == result);
294 FAIL_TEST (NULL == event->op_cls);
295 FAIL_TEST (NULL == event->details.operation_finished.emsg);
296 FAIL_TEST (NULL == event->details.operation_finished.generic);
297 if (event->op == peer1.operation)
299 GNUNET_TESTBED_operation_done (peer1.operation);
300 peer1.operation = NULL;
303 else if (event->op == peer2.operation)
305 GNUNET_TESTBED_operation_done (peer2.operation);
306 peer2.operation = NULL;
311 if ((NULL == peer1.peer) && (NULL == peer2.peer))
314 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
317 case GNUNET_TESTBED_ET_PEER_START:
318 FAIL_TEST (INIT == result);
319 FAIL_TEST (event->details.peer_start.host == host);
320 if (event->details.peer_start.peer == peer1.peer)
322 peer1.is_running = GNUNET_YES;
323 GNUNET_TESTBED_operation_done (peer1.operation);
324 peer1.operation = NULL;
326 else if (event->details.peer_start.peer == peer2.peer)
328 peer2.is_running = GNUNET_YES;
329 GNUNET_TESTBED_operation_done (peer2.operation);
330 peer2.operation = NULL;
334 if ((GNUNET_YES == peer1.is_running) && (GNUNET_YES == peer2.is_running))
336 result = PEERS_STARTED;
338 GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
342 case GNUNET_TESTBED_ET_PEER_STOP:
343 FAIL_TEST (PEERS_CONNECTED_2 == result);
344 if (event->details.peer_stop.peer == peer1.peer)
346 peer1.is_running = GNUNET_NO;
347 GNUNET_TESTBED_operation_done (peer1.operation);
348 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
350 else if (event->details.peer_stop.peer == peer2.peer)
352 peer2.is_running = GNUNET_NO;
353 GNUNET_TESTBED_operation_done (peer2.operation);
354 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
358 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running))
359 result = PEERS_STOPPED;
361 case GNUNET_TESTBED_ET_CONNECT:
365 FAIL_TEST (NULL == peer1.operation);
366 FAIL_TEST (NULL == peer2.operation);
367 FAIL_TEST (NULL != common_operation);
368 FAIL_TEST ((event->details.peer_connect.peer1 == peer1.peer) &&
369 (event->details.peer_connect.peer2 == peer2.peer));
370 GNUNET_TESTBED_operation_done (common_operation);
371 common_operation = NULL;
372 result = PEERS_CONNECTED;
373 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
374 delayed_connect_task =
375 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), &do_delayed_connect,
378 case PEERS_CONNECTED:
379 FAIL_TEST (NULL == peer1.operation);
380 FAIL_TEST (NULL == peer2.operation);
381 FAIL_TEST (NULL != common_operation);
382 GNUNET_TESTBED_operation_done (common_operation);
383 common_operation = NULL;
384 result = PEERS_CONNECTED_2;
385 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
386 peer1.operation = GNUNET_TESTBED_peer_stop (NULL, peer1.peer, NULL, NULL);
387 peer2.operation = GNUNET_TESTBED_peer_stop (NULL, peer2.peer, NULL, NULL);
400 * Functions of this signature are called when a peer has been successfully
403 * @param cls the closure from GNUNET_TESTBED_peer_create()
404 * @param peer the handle for the created peer; NULL on any error during
406 * @param emsg NULL if peer is not NULL; else MAY contain the error description
409 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
411 struct PeerContext *pc = cls;
413 FAIL_TEST (NULL != pc->operation);
414 FAIL_TEST (NULL != peer);
415 FAIL_TEST (NULL == pc->peer);
417 GNUNET_TESTBED_operation_done (pc->operation);
418 pc->operation = GNUNET_TESTBED_peer_start (NULL, pc->peer, NULL, NULL);
423 * Callback which will be called to after a host registration succeeded or failed
425 * @param cls the host which has been registered
426 * @param emsg the error message; NULL if host registration is successful
429 registration_comp (void *cls, const char *emsg)
431 FAIL_TEST (cls == neighbour);
434 GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
437 GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
439 FAIL_TEST (NULL != peer1.operation);
440 FAIL_TEST (NULL != peer2.operation);
445 * Callback to signal successfull startup of the controller process
447 * @param cls the closure from GNUNET_TESTBED_controller_start()
448 * @param cfg the configuration with which the controller has been started;
449 * NULL if status is not GNUNET_OK
450 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
451 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
454 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg_, int status)
458 if (GNUNET_OK != status)
464 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
465 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
466 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
467 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
469 GNUNET_TESTBED_controller_connect (host, event_mask, &controller_cb,
471 FAIL_TEST (NULL != controller);
472 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
473 FAIL_TEST (NULL != neighbour);
475 GNUNET_TESTBED_register_host (controller, neighbour, ®istration_comp,
477 FAIL_TEST (NULL != reg_handle);
486 * @param args arguments passed to GNUNET_PROGRAM_run
487 * @param cfgfile the path to configuration file
488 * @param cfg the configuration file handle
491 run (void *cls, char *const *args, const char *cfgfile,
492 const struct GNUNET_CONFIGURATION_Handle *config)
494 cfg = GNUNET_CONFIGURATION_dup (config);
495 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
496 FAIL_TEST (NULL != host);
497 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
500 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
501 (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
510 main (int argc, char **argv)
514 char *const argv2[] = { "test_testbed_api_2peers_1controller",
515 "-c", "test_testbed_api.conf",
518 struct GNUNET_GETOPT_CommandLineOption options[] = {
519 GNUNET_GETOPT_OPTION_END
523 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
524 "test_testbed_api_2peers_1controller", "nohelp",
525 options, &run, NULL);
526 if ((GNUNET_OK != ret) || (SUCCESS != result))
531 /* end of test_testbed_api_2peers_1controller.c */