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.c
23 * @brief testcases for the testbed api
24 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib-new.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 DHT service
88 static struct GNUNET_DHT_Handle *dht_handle;
91 * Abort task identifier
93 static GNUNET_SCHEDULER_TaskIdentifier 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, const const struct GNUNET_SCHEDULER_TaskContext *tc)
141 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down...\n");
142 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
143 GNUNET_SCHEDULER_cancel (abort_task);
144 if (NULL != reg_handle)
145 GNUNET_TESTBED_cancel_registration (reg_handle);
146 GNUNET_TESTBED_controller_disconnect (controller);
147 GNUNET_CONFIGURATION_destroy (cfg);
149 GNUNET_TESTBED_controller_stop (cp);
150 GNUNET_TESTBED_host_destroy (neighbour);
151 GNUNET_TESTBED_host_destroy (host);
156 * abort task to run on test timed out
159 * @param tc the task context
162 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
164 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
165 abort_task = GNUNET_SCHEDULER_NO_TASK;
166 do_shutdown (cls, tc);
171 * Adapter function called to establish a connection to
175 * @param cfg configuration of the peer to connect to; will be available until
176 * GNUNET_TESTBED_operation_done() is called on the operation returned
177 * from GNUNET_TESTBED_service_connect()
178 * @return service handle to return in 'op_result', NULL on error
181 dht_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
183 GNUNET_assert (NULL == cls);
184 GNUNET_assert (OTHER == sub_test);
185 sub_test = PEER_SERVICE_CONNECT;
186 dht_handle = GNUNET_DHT_connect (cfg, 10);
192 * Adapter function called to destroy a connection to
196 * @param op_result service handle returned from the connect adapter
199 dht_disconnect_adapter (void *cls, void *op_result)
201 GNUNET_assert (NULL != op_result);
202 GNUNET_assert (op_result == dht_handle);
203 GNUNET_DHT_disconnect (dht_handle);
205 GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
206 GNUNET_assert (NULL != operation);
207 operation = GNUNET_TESTBED_peer_stop (peer, NULL, NULL);
208 GNUNET_assert (NULL != operation);
213 * Callback to be called when a service connect operation is completed
215 * @param cls the callback closure from functions generating an operation
216 * @param op the operation that has been finished
217 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
218 * @param emsg error message in case the operation has failed; will be NULL if
219 * operation has executed successfully.
222 service_connect_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
223 void *ca_result, const char *emsg)
227 case PEER_SERVICE_CONNECT:
228 GNUNET_assert (operation == op);
229 GNUNET_assert (NULL == emsg);
230 GNUNET_assert (NULL == cls);
231 GNUNET_assert (ca_result == dht_handle);
232 GNUNET_TESTBED_operation_done (operation); /* This results in call to
233 * disconnect adapter */
243 * Callback to be called when the requested peer information is available
245 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
246 * @param op the operation this callback corresponds to
247 * @param pinfo the result; will be NULL if the operation has failed
248 * @param emsg error message if the operation has failed; will be NULL if the
249 * operation is successfull
252 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
253 const struct GNUNET_TESTBED_PeerInformation *pinfo,
259 GNUNET_assert (NULL != pinfo);
260 GNUNET_assert (NULL == emsg);
261 GNUNET_assert (NULL == cb_cls);
262 GNUNET_assert (operation == op);
263 GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit);
264 GNUNET_assert (NULL != pinfo->result.cfg);
265 sub_test = PEER_DESTROY;
266 GNUNET_TESTBED_operation_done (operation);
267 operation = GNUNET_TESTBED_peer_destroy (peer);
276 * Signature of the event handler function called by the
277 * respective event controller.
280 * @param event information about the event
283 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
287 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
291 GNUNET_assert (event->details.operation_finished.operation == operation);
292 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
293 GNUNET_assert (NULL == event->details.operation_finished.emsg);
294 GNUNET_assert (NULL == event->details.operation_finished.generic);
295 GNUNET_TESTBED_operation_done (operation);
296 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
298 case PEER_SERVICE_CONNECT:
299 GNUNET_assert (event->details.operation_finished.operation == operation);
300 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
301 GNUNET_assert (NULL == event->details.operation_finished.emsg);
302 GNUNET_assert (NULL != dht_handle);
303 GNUNET_assert (event->details.operation_finished.generic == dht_handle);
310 case GNUNET_TESTBED_ET_PEER_START:
311 GNUNET_assert (event->details.peer_start.host == host);
312 GNUNET_assert (event->details.peer_start.peer == peer);
313 GNUNET_assert (OTHER == sub_test);
314 GNUNET_TESTBED_operation_done (operation);
316 GNUNET_TESTBED_service_connect (NULL, peer, "dht",
317 &service_connect_comp_cb, NULL,
318 &dht_connect_adapter,
319 &dht_disconnect_adapter, NULL);
320 GNUNET_assert (NULL != operation);
322 case GNUNET_TESTBED_ET_PEER_STOP:
323 GNUNET_assert (event->details.peer_stop.peer == peer);
324 GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
326 sub_test = PEER_GETCONFIG;
327 GNUNET_TESTBED_operation_done (operation);
329 GNUNET_TESTBED_peer_get_information (peer,
330 GNUNET_TESTBED_PIT_CONFIGURATION,
334 GNUNET_assert (0); /* We should never reach this state */
340 * Functions of this signature are called when a peer has been successfully
343 * @param cls the closure from GNUNET_TESTBED_peer_create()
344 * @param peer the handle for the created peer; NULL on any error during
346 * @param emsg NULL if peer is not NULL; else MAY contain the error description
349 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
351 struct GNUNET_TESTBED_Peer **peer_ptr;
354 GNUNET_assert (NULL != peer);
355 GNUNET_assert (NULL != peer_ptr);
357 GNUNET_TESTBED_operation_done (operation);
358 operation = GNUNET_TESTBED_peer_start (peer, NULL, NULL);
359 GNUNET_assert (NULL != operation);
364 * Callback which will be called to after a host registration succeeded or failed
366 * @param cls the host which has been registered
367 * @param emsg the error message; NULL if host registration is successful
370 registration_comp (void *cls, const char *emsg)
372 GNUNET_assert (cls == neighbour);
375 GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
377 GNUNET_assert (NULL != operation);
382 * Callback to signal successfull startup of the controller process
384 * @param cls the closure from GNUNET_TESTBED_controller_start()
385 * @param cfg the configuration with which the controller has been started;
386 * NULL if status is not GNUNET_OK
387 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
388 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
391 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
395 GNUNET_assert (GNUNET_OK == status);
397 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
398 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
399 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
400 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
402 GNUNET_TESTBED_controller_connect (cfg, host, event_mask, &controller_cb,
404 GNUNET_assert (NULL != controller);
405 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
406 GNUNET_assert (NULL != neighbour);
408 GNUNET_TESTBED_register_host (controller, neighbour, ®istration_comp,
410 GNUNET_assert (NULL != reg_handle);
419 * @param args arguments passed to GNUNET_PROGRAM_run
420 * @param cfgfile the path to configuration file
421 * @param cfg the configuration file handle
424 run (void *cls, char *const *args, const char *cfgfile,
425 const struct GNUNET_CONFIGURATION_Handle *config)
427 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
428 GNUNET_assert (NULL != host);
429 cfg = GNUNET_CONFIGURATION_dup (config);
430 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
433 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
434 (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
443 main (int argc, char **argv)
447 char *const argv2[] = { "test_testbed_api",
448 "-c", "test_testbed_api.conf",
451 struct GNUNET_GETOPT_CommandLineOption options[] = {
452 GNUNET_GETOPT_OPTION_END
455 result = GNUNET_SYSERR;
457 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
458 "test_testbed_api", "nohelp", options, &run, NULL);
459 if ((GNUNET_OK != ret) || (GNUNET_OK != result))