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.c
23 * @brief testcases for the testbed api
24 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_testbed_service.h"
34 * Generic logging shortcut
36 #define LOG(kind,...) \
37 GNUNET_log (kind, __VA_ARGS__)
40 * Relative time seconds shorthand
42 #define TIME_REL_SECS(sec) \
43 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
48 static struct GNUNET_TESTBED_Host *host;
51 * The controller process
53 static struct GNUNET_TESTBED_ControllerProc *cp;
56 * The controller handle
58 static struct GNUNET_TESTBED_Controller *controller;
63 static struct GNUNET_TESTBED_Host *neighbour;
66 * Handle for neighbour registration
68 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
73 static struct GNUNET_TESTBED_Peer *peer;
76 * Handle to configuration
78 static struct GNUNET_CONFIGURATION_Handle *cfg;
83 static struct GNUNET_TESTBED_Operation *operation;
86 * Handle to peer's ARM service
88 static struct GNUNET_ARM_Handle *arm_handle;
91 * Abort task identifier
93 static struct GNUNET_SCHEDULER_Task *abort_task;
102 * Enumeration of sub testcases
107 * Test cases which are not covered by the below ones
112 * Test where we get a peer config from controller
117 * Test where we connect to a service running on the peer
119 PEER_SERVICE_CONNECT,
122 * Test where we get a peer's identity from controller
130 static enum Test sub_test;
136 * @param tc the task context
139 do_shutdown (void *cls)
141 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down...\n");
142 if (NULL != abort_task)
143 GNUNET_SCHEDULER_cancel (abort_task);
144 if (NULL != reg_handle)
145 GNUNET_TESTBED_cancel_registration (reg_handle);
146 if (NULL != controller)
147 GNUNET_TESTBED_controller_disconnect (controller);
149 GNUNET_CONFIGURATION_destroy (cfg);
151 GNUNET_TESTBED_controller_stop (cp);
152 if (NULL != neighbour)
153 GNUNET_TESTBED_host_destroy (neighbour);
155 GNUNET_TESTBED_host_destroy (host);
160 * shortcut to exit during failure
162 #define FAIL_TEST(cond, ret) do { \
165 if (NULL != abort_task) \
166 GNUNET_SCHEDULER_cancel (abort_task); \
168 GNUNET_SCHEDULER_add_now (do_shutdown, NULL); \
175 * abort task to run on test timed out
178 * @param tc the task context
183 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
190 * Adapter function called to establish a connection to
194 * @param cfg configuration of the peer to connect to; will be available until
195 * GNUNET_TESTBED_operation_done() is called on the operation returned
196 * from GNUNET_TESTBED_service_connect()
197 * @return service handle to return in 'op_result', NULL on error
200 arm_connect_adapter (void *cls,
201 const struct GNUNET_CONFIGURATION_Handle *cfg)
203 FAIL_TEST (NULL == cls, return NULL);
204 FAIL_TEST (OTHER == sub_test, return NULL);
205 sub_test = PEER_SERVICE_CONNECT;
206 arm_handle = GNUNET_ARM_connect (cfg, NULL, NULL);
212 * Adapter function called to destroy a connection to
216 * @param op_result service handle returned from the connect adapter
219 arm_disconnect_adapter (void *cls,
222 FAIL_TEST (NULL != op_result, return);
223 FAIL_TEST (op_result == arm_handle, return);
224 GNUNET_ARM_disconnect (arm_handle);
226 FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
227 FAIL_TEST (NULL != operation, return);
228 operation = GNUNET_TESTBED_peer_stop (NULL, peer, NULL, NULL);
229 FAIL_TEST (NULL != operation, return);
234 * Callback to be called when a service connect operation is completed
236 * @param cls the callback closure from functions generating an operation
237 * @param op the operation that has been finished
238 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
239 * @param emsg error message in case the operation has failed; will be NULL if
240 * operation has executed successfully.
243 service_connect_comp_cb (void *cls,
244 struct GNUNET_TESTBED_Operation *op,
250 case PEER_SERVICE_CONNECT:
251 FAIL_TEST (operation == op, return);
252 FAIL_TEST (NULL == emsg, return);
253 FAIL_TEST (NULL == cls, return);
254 FAIL_TEST (ca_result == arm_handle, return);
255 GNUNET_TESTBED_operation_done (operation); /* This results in call to
256 * disconnect adapter */
259 FAIL_TEST (0, return);
266 * Callback to be called when the requested peer information is available
268 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
269 * @param op the operation this callback corresponds to
270 * @param pinfo the result; will be NULL if the operation has failed
271 * @param emsg error message if the operation has failed; will be NULL if the
272 * operation is successfull
275 peerinfo_cb (void *cb_cls,
276 struct GNUNET_TESTBED_Operation *op,
277 const struct GNUNET_TESTBED_PeerInformation *pinfo,
283 FAIL_TEST (NULL != pinfo, return);
284 FAIL_TEST (NULL == emsg, return);
285 FAIL_TEST (NULL == cb_cls, return);
286 FAIL_TEST (operation == op, return);
287 FAIL_TEST (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit, return);
288 FAIL_TEST (NULL != pinfo->result.cfg, return);
289 sub_test = PEER_DESTROY;
290 GNUNET_TESTBED_operation_done (operation);
291 operation = GNUNET_TESTBED_peer_destroy (peer);
294 FAIL_TEST (0, return);
300 * Signature of the event handler function called by the
301 * respective event controller.
304 * @param event information about the event
307 controller_cb (void *cls,
308 const struct GNUNET_TESTBED_EventInformation *event)
312 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
316 FAIL_TEST (event->op == operation, return);
317 FAIL_TEST (NULL == event->op_cls, return);
318 FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
319 FAIL_TEST (NULL == event->details.operation_finished.generic, return);
320 GNUNET_TESTBED_operation_done (operation);
321 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
323 case PEER_SERVICE_CONNECT:
324 FAIL_TEST (event->op == operation, return);
325 FAIL_TEST (NULL == event->op_cls, return);
326 FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
327 FAIL_TEST (NULL != arm_handle, return);
328 FAIL_TEST (event->details.operation_finished.generic == arm_handle, return);
331 FAIL_TEST (0, return);
335 case GNUNET_TESTBED_ET_PEER_START:
336 FAIL_TEST (event->details.peer_start.host == host, return);
337 FAIL_TEST (event->details.peer_start.peer == peer, return);
338 FAIL_TEST (OTHER == sub_test, return);
339 GNUNET_TESTBED_operation_done (operation);
341 GNUNET_TESTBED_service_connect (NULL, peer, "dht",
342 &service_connect_comp_cb, NULL,
343 &arm_connect_adapter,
344 &arm_disconnect_adapter, NULL);
345 FAIL_TEST (NULL != operation, return);
347 case GNUNET_TESTBED_ET_PEER_STOP:
348 FAIL_TEST (event->details.peer_stop.peer == peer, return);
349 FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
351 sub_test = PEER_GETCONFIG;
352 GNUNET_TESTBED_operation_done (operation);
354 GNUNET_TESTBED_peer_get_information (peer,
355 GNUNET_TESTBED_PIT_CONFIGURATION,
359 FAIL_TEST (0, return); /* We should never reach this state */
365 * Functions of this signature are called when a peer has been successfully
368 * @param cls the closure from GNUNET_TESTBED_peer_create()
369 * @param peer the handle for the created peer; NULL on any error during
371 * @param emsg NULL if peer is not NULL; else MAY contain the error description
374 peer_create_cb (void *cls,
375 struct GNUNET_TESTBED_Peer *peer,
378 struct GNUNET_TESTBED_Peer **peer_ptr;
381 FAIL_TEST (NULL != peer, return);
382 FAIL_TEST (NULL != peer_ptr, return);
384 GNUNET_TESTBED_operation_done (operation);
385 operation = GNUNET_TESTBED_peer_start (NULL,
389 FAIL_TEST (NULL != operation, return);
394 * Callback which will be called to after a host registration succeeded or failed
396 * @param cls the host which has been registered
397 * @param emsg the error message; NULL if host registration is successful
400 registration_comp (void *cls,
403 FAIL_TEST (cls == neighbour, return);
406 GNUNET_TESTBED_peer_create (controller,
411 FAIL_TEST (NULL != operation, return);
416 * Callback to signal successfull startup of the controller process
418 * @param cls the closure from GNUNET_TESTBED_controller_start()
419 * @param cfg the configuration with which the controller has been started;
420 * NULL if status is not #GNUNET_OK
421 * @param status #GNUNET_OK if the startup is successfull; #GNUNET_SYSERR if not,
422 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
425 status_cb (void *cls,
426 const struct GNUNET_CONFIGURATION_Handle *cfg_,
431 if (GNUNET_OK != status)
434 FAIL_TEST (0, return);
438 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
439 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
440 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
441 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
443 GNUNET_TESTBED_controller_connect (host, event_mask, &controller_cb,
445 FAIL_TEST (NULL != controller, return);
446 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
447 FAIL_TEST (NULL != neighbour, return);
449 GNUNET_TESTBED_register_host (controller, neighbour, ®istration_comp,
451 FAIL_TEST (NULL != reg_handle, return);
460 * @param args arguments passed to #GNUNET_PROGRAM_run()
461 * @param cfgfile the path to configuration file
462 * @param cfg the configuration file handle
468 const struct GNUNET_CONFIGURATION_Handle *config)
470 cfg = GNUNET_CONFIGURATION_dup (config);
471 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
472 FAIL_TEST (NULL != host, return);
473 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host,
477 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
478 (GNUNET_TIME_UNIT_MINUTES, 5),
488 main (int argc, char **argv)
492 char *const argv2[] = { "test_testbed_api",
493 "-c", "test_testbed_api.conf",
496 struct GNUNET_GETOPT_CommandLineOption options[] = {
497 GNUNET_GETOPT_OPTION_END
500 result = GNUNET_SYSERR;
502 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
503 "test_testbed_api", "nohelp", options, &run, NULL);
504 if ((GNUNET_OK != ret) || (GNUNET_OK != result))
509 /* end of test_testbed_api.c */