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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file testbed/test_testbed_api.c
21 * @brief testcases for the testbed api
22 * @author Sree Harsha Totakura
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_testbed_service.h"
32 * Generic logging shortcut
34 #define LOG(kind,...) \
35 GNUNET_log (kind, __VA_ARGS__)
38 * Relative time seconds shorthand
40 #define TIME_REL_SECS(sec) \
41 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
46 static struct GNUNET_TESTBED_Host *host;
49 * The controller process
51 static struct GNUNET_TESTBED_ControllerProc *cp;
54 * The controller handle
56 static struct GNUNET_TESTBED_Controller *controller;
61 static struct GNUNET_TESTBED_Host *neighbour;
64 * Handle for neighbour registration
66 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
71 static struct GNUNET_TESTBED_Peer *peer;
74 * Handle to configuration
76 static struct GNUNET_CONFIGURATION_Handle *cfg;
81 static struct GNUNET_TESTBED_Operation *operation;
84 * Handle to peer's ARM service
86 static struct GNUNET_ARM_Handle *arm_handle;
89 * Abort task identifier
91 static struct GNUNET_SCHEDULER_Task *abort_task;
100 * Enumeration of sub testcases
105 * Test cases which are not covered by the below ones
110 * Test where we get a peer config from controller
115 * Test where we connect to a service running on the peer
117 PEER_SERVICE_CONNECT,
120 * Test where we get a peer's identity from controller
128 static enum Test sub_test;
134 * @param tc the task context
137 do_shutdown (void *cls)
139 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down...\n");
140 if (NULL != abort_task)
141 GNUNET_SCHEDULER_cancel (abort_task);
142 if (NULL != reg_handle)
143 GNUNET_TESTBED_cancel_registration (reg_handle);
144 if (NULL != controller)
145 GNUNET_TESTBED_controller_disconnect (controller);
147 GNUNET_CONFIGURATION_destroy (cfg);
149 GNUNET_TESTBED_controller_stop (cp);
150 if (NULL != neighbour)
151 GNUNET_TESTBED_host_destroy (neighbour);
153 GNUNET_TESTBED_host_destroy (host);
158 * shortcut to exit during failure
160 #define FAIL_TEST(cond, ret) do { \
163 if (NULL != abort_task) \
164 GNUNET_SCHEDULER_cancel (abort_task); \
166 GNUNET_SCHEDULER_add_now (do_shutdown, NULL); \
173 * abort task to run on test timed out
176 * @param tc the task context
181 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
188 * Adapter function called to establish a connection to
192 * @param cfg configuration of the peer to connect to; will be available until
193 * GNUNET_TESTBED_operation_done() is called on the operation returned
194 * from GNUNET_TESTBED_service_connect()
195 * @return service handle to return in 'op_result', NULL on error
198 arm_connect_adapter (void *cls,
199 const struct GNUNET_CONFIGURATION_Handle *cfg)
201 FAIL_TEST (NULL == cls, return NULL);
202 FAIL_TEST (OTHER == sub_test, return NULL);
203 sub_test = PEER_SERVICE_CONNECT;
204 arm_handle = GNUNET_ARM_connect (cfg, NULL, NULL);
210 * Adapter function called to destroy a connection to
214 * @param op_result service handle returned from the connect adapter
217 arm_disconnect_adapter (void *cls,
220 FAIL_TEST (NULL != op_result, return);
221 FAIL_TEST (op_result == arm_handle, return);
222 GNUNET_ARM_disconnect (arm_handle);
224 FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
225 FAIL_TEST (NULL != operation, return);
226 operation = GNUNET_TESTBED_peer_stop (NULL, peer, NULL, NULL);
227 FAIL_TEST (NULL != operation, return);
232 * Callback to be called when a service connect operation is completed
234 * @param cls the callback closure from functions generating an operation
235 * @param op the operation that has been finished
236 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
237 * @param emsg error message in case the operation has failed; will be NULL if
238 * operation has executed successfully.
241 service_connect_comp_cb (void *cls,
242 struct GNUNET_TESTBED_Operation *op,
248 case PEER_SERVICE_CONNECT:
249 FAIL_TEST (operation == op, return);
250 FAIL_TEST (NULL == emsg, return);
251 FAIL_TEST (NULL == cls, return);
252 FAIL_TEST (ca_result == arm_handle, return);
253 GNUNET_TESTBED_operation_done (operation); /* This results in call to
254 * disconnect adapter */
257 FAIL_TEST (0, return);
264 * Callback to be called when the requested peer information is available
266 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
267 * @param op the operation this callback corresponds to
268 * @param pinfo the result; will be NULL if the operation has failed
269 * @param emsg error message if the operation has failed; will be NULL if the
270 * operation is successfull
273 peerinfo_cb (void *cb_cls,
274 struct GNUNET_TESTBED_Operation *op,
275 const struct GNUNET_TESTBED_PeerInformation *pinfo,
281 FAIL_TEST (NULL != pinfo, return);
282 FAIL_TEST (NULL == emsg, return);
283 FAIL_TEST (NULL == cb_cls, return);
284 FAIL_TEST (operation == op, return);
285 FAIL_TEST (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit, return);
286 FAIL_TEST (NULL != pinfo->result.cfg, return);
287 sub_test = PEER_DESTROY;
288 GNUNET_TESTBED_operation_done (operation);
289 operation = GNUNET_TESTBED_peer_destroy (peer);
292 FAIL_TEST (0, return);
298 * Signature of the event handler function called by the
299 * respective event controller.
302 * @param event information about the event
305 controller_cb (void *cls,
306 const struct GNUNET_TESTBED_EventInformation *event)
310 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
314 FAIL_TEST (event->op == operation, return);
315 FAIL_TEST (NULL == event->op_cls, return);
316 FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
317 FAIL_TEST (NULL == event->details.operation_finished.generic, return);
318 GNUNET_TESTBED_operation_done (operation);
319 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
321 case PEER_SERVICE_CONNECT:
322 FAIL_TEST (event->op == operation, return);
323 FAIL_TEST (NULL == event->op_cls, return);
324 FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
325 FAIL_TEST (NULL != arm_handle, return);
326 FAIL_TEST (event->details.operation_finished.generic == arm_handle, return);
329 FAIL_TEST (0, return);
333 case GNUNET_TESTBED_ET_PEER_START:
334 FAIL_TEST (event->details.peer_start.host == host, return);
335 FAIL_TEST (event->details.peer_start.peer == peer, return);
336 FAIL_TEST (OTHER == sub_test, return);
337 GNUNET_TESTBED_operation_done (operation);
339 GNUNET_TESTBED_service_connect (NULL, peer, "dht",
340 &service_connect_comp_cb, NULL,
341 &arm_connect_adapter,
342 &arm_disconnect_adapter, NULL);
343 FAIL_TEST (NULL != operation, return);
345 case GNUNET_TESTBED_ET_PEER_STOP:
346 FAIL_TEST (event->details.peer_stop.peer == peer, return);
347 FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
349 sub_test = PEER_GETCONFIG;
350 GNUNET_TESTBED_operation_done (operation);
352 GNUNET_TESTBED_peer_get_information (peer,
353 GNUNET_TESTBED_PIT_CONFIGURATION,
357 FAIL_TEST (0, return); /* We should never reach this state */
363 * Functions of this signature are called when a peer has been successfully
366 * @param cls the closure from GNUNET_TESTBED_peer_create()
367 * @param peer the handle for the created peer; NULL on any error during
369 * @param emsg NULL if peer is not NULL; else MAY contain the error description
372 peer_create_cb (void *cls,
373 struct GNUNET_TESTBED_Peer *peer,
376 struct GNUNET_TESTBED_Peer **peer_ptr;
379 FAIL_TEST (NULL != peer, return);
380 FAIL_TEST (NULL != peer_ptr, return);
382 GNUNET_TESTBED_operation_done (operation);
383 operation = GNUNET_TESTBED_peer_start (NULL,
387 FAIL_TEST (NULL != operation, return);
392 * Callback which will be called to after a host registration succeeded or failed
394 * @param cls the host which has been registered
395 * @param emsg the error message; NULL if host registration is successful
398 registration_comp (void *cls,
401 FAIL_TEST (cls == neighbour, return);
404 GNUNET_TESTBED_peer_create (controller,
409 FAIL_TEST (NULL != operation, return);
414 * Callback to signal successfull startup of the controller process
416 * @param cls the closure from GNUNET_TESTBED_controller_start()
417 * @param cfg the configuration with which the controller has been started;
418 * NULL if status is not #GNUNET_OK
419 * @param status #GNUNET_OK if the startup is successfull; #GNUNET_SYSERR if not,
420 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
423 status_cb (void *cls,
424 const struct GNUNET_CONFIGURATION_Handle *cfg_,
429 if (GNUNET_OK != status)
432 FAIL_TEST (0, return);
436 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
437 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
438 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
439 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
441 GNUNET_TESTBED_controller_connect (host, event_mask, &controller_cb,
443 FAIL_TEST (NULL != controller, return);
444 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
445 FAIL_TEST (NULL != neighbour, return);
447 GNUNET_TESTBED_register_host (controller, neighbour, ®istration_comp,
449 FAIL_TEST (NULL != reg_handle, return);
458 * @param args arguments passed to #GNUNET_PROGRAM_run()
459 * @param cfgfile the path to configuration file
460 * @param cfg the configuration file handle
466 const struct GNUNET_CONFIGURATION_Handle *config)
468 cfg = GNUNET_CONFIGURATION_dup (config);
469 host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
470 FAIL_TEST (NULL != host, return);
471 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host,
475 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
476 (GNUNET_TIME_UNIT_MINUTES, 5),
486 main (int argc, char **argv)
490 char *const argv2[] = { "test_testbed_api",
491 "-c", "test_testbed_api.conf",
494 struct GNUNET_GETOPT_CommandLineOption options[] = {
495 GNUNET_GETOPT_OPTION_END
498 result = GNUNET_SYSERR;
500 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
501 "test_testbed_api", "nohelp", options, &run, NULL);
502 if ((GNUNET_OK != ret) || (GNUNET_OK != result))
507 /* end of test_testbed_api.c */