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,
182 const struct GNUNET_CONFIGURATION_Handle *cfg)
184 GNUNET_assert (NULL == cls);
185 GNUNET_assert (OTHER == sub_test);
186 sub_test = PEER_SERVICE_CONNECT;
187 dht_handle = GNUNET_DHT_connect (cfg, 10);
193 * Adapter function called to destroy a connection to
197 * @param op_result service handle returned from the connect adapter
200 dht_disconnect_adapter (void *cls,
203 if (NULL != op_result)
204 GNUNET_DHT_disconnect (op_result);
205 GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
206 GNUNET_assert (NULL != operation);
207 operation = GNUNET_TESTBED_peer_stop (peer);
208 GNUNET_assert (NULL != operation);
214 * Signature of the event handler function called by the
215 * respective event controller.
218 * @param event information about the event
221 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
225 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
229 GNUNET_assert (event->details.operation_finished.operation == operation);
230 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
231 GNUNET_assert (NULL == event->details.operation_finished.emsg);
232 GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION ==
233 event->details.operation_finished.pit);
234 GNUNET_assert (NULL != event->details.operation_finished.op_result.cfg);
235 sub_test = PEER_DESTROY;
236 GNUNET_TESTBED_operation_done (operation);
237 operation = GNUNET_TESTBED_peer_destroy (peer);
240 GNUNET_assert (event->details.operation_finished.operation == operation);
241 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
242 GNUNET_assert (NULL == event->details.operation_finished.emsg);
243 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
244 event->details.operation_finished.pit);
245 GNUNET_assert (NULL ==
246 event->details.operation_finished.op_result.generic);
247 GNUNET_TESTBED_operation_done (operation);
248 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
250 case PEER_SERVICE_CONNECT:
251 GNUNET_assert (event->details.operation_finished.operation == operation);
252 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
253 GNUNET_assert (NULL == event->details.operation_finished.emsg);
254 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
255 event->details.operation_finished.pit);
256 GNUNET_assert (NULL != dht_handle);
257 GNUNET_assert (event->details.operation_finished.op_result.generic
259 GNUNET_TESTBED_operation_done (operation); /* This results in call to
260 * disconnect adapter */
267 case GNUNET_TESTBED_ET_PEER_START:
268 GNUNET_assert (event->details.peer_start.host == host);
269 GNUNET_assert (event->details.peer_start.peer == peer);
270 GNUNET_assert (OTHER == sub_test);
271 GNUNET_TESTBED_operation_done (operation);
272 operation = GNUNET_TESTBED_service_connect (NULL, peer, "dht",
273 &dht_connect_adapter,
274 &dht_disconnect_adapter,
276 GNUNET_assert (NULL != operation);
278 case GNUNET_TESTBED_ET_PEER_STOP:
279 GNUNET_assert (event->details.peer_stop.peer == peer);
280 GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
282 sub_test = PEER_GETCONFIG;
283 GNUNET_TESTBED_operation_done (operation);
285 GNUNET_TESTBED_peer_get_information (peer,
286 GNUNET_TESTBED_PIT_CONFIGURATION);
289 GNUNET_assert (0); /* We should never reach this state */
295 * Functions of this signature are called when a peer has been successfully
298 * @param cls the closure from GNUNET_TESTBED_peer_create()
299 * @param peer the handle for the created peer; NULL on any error during
301 * @param emsg NULL if peer is not NULL; else MAY contain the error description
304 peer_create_cb (void *cls,
305 struct GNUNET_TESTBED_Peer *peer, const char *emsg)
307 struct GNUNET_TESTBED_Peer **peer_ptr;
310 GNUNET_assert (NULL != peer);
311 GNUNET_assert (NULL != peer_ptr);
313 GNUNET_TESTBED_operation_done (operation);
314 operation = GNUNET_TESTBED_peer_start (peer);
315 GNUNET_assert (NULL != operation);
320 * Callback which will be called to after a host registration succeeded or failed
322 * @param cls the host which has been registered
323 * @param emsg the error message; NULL if host registration is successful
326 registration_comp (void *cls, const char *emsg)
328 GNUNET_assert (cls == neighbour);
330 operation = GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb, &peer);
331 GNUNET_assert (NULL != operation);
336 * Callback to signal successfull startup of the controller process
338 * @param cls the closure from GNUNET_TESTBED_controller_start()
339 * @param cfg the configuration with which the controller has been started;
340 * NULL if status is not GNUNET_OK
341 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
342 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
345 status_cb (void *cls,
346 const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
350 GNUNET_assert (GNUNET_OK == status);
352 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
353 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
354 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
355 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
356 controller = GNUNET_TESTBED_controller_connect (cfg, host, event_mask,
357 &controller_cb, NULL);
358 GNUNET_assert (NULL != controller);
359 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
360 GNUNET_assert (NULL != neighbour);
362 GNUNET_TESTBED_register_host (controller, neighbour, ®istration_comp,
364 GNUNET_assert (NULL != reg_handle);
373 * @param args arguments passed to GNUNET_PROGRAM_run
374 * @param cfgfile the path to configuration file
375 * @param cfg the configuration file handle
378 run (void *cls, char *const *args, const char *cfgfile,
379 const struct GNUNET_CONFIGURATION_Handle *config)
381 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
382 GNUNET_assert (NULL != host);
383 cfg = GNUNET_CONFIGURATION_dup (config);
384 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
385 abort_task = GNUNET_SCHEDULER_add_delayed
386 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort, NULL);
393 int main (int argc, char **argv)
396 char *const argv2[] = { "test_testbed_api",
397 "-c", "test_testbed_api.conf",
400 struct GNUNET_GETOPT_CommandLineOption options[] = {
401 GNUNET_GETOPT_OPTION_END
404 result = GNUNET_SYSERR;
405 ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
406 "test_testbed_api", "nohelp", options, &run,
408 if ((GNUNET_OK != ret) || (GNUNET_OK != result))