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_2peers.c
23 * @brief testcases for the testbed api: 2 peers are configured, started and
25 * @author Sree Harsha Totakura
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib-new.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)
55 struct GNUNET_TESTBED_Peer *peer;
58 * Operations involving this peer
60 struct GNUNET_TESTBED_Operation *operation;
63 * set to GNUNET_YES when peer is started
71 static struct GNUNET_TESTBED_Host *host;
74 * The controller process
76 static struct GNUNET_TESTBED_ControllerProc *cp;
79 * The controller handle
81 static struct GNUNET_TESTBED_Controller *controller;
86 static struct GNUNET_TESTBED_Host *neighbour;
89 * Handle for neighbour registration
91 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
96 static struct PeerContext peer1;
101 static struct PeerContext peer2;
104 * Handle to configuration
106 static struct GNUNET_CONFIGURATION_Handle *cfg;
109 * Handle to operations involving both peers
111 static struct GNUNET_TESTBED_Operation *common_operation;
114 * Abort task identifier
116 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
119 * Different stages in testing
140 * peers are connected
150 * Final success stage
158 static enum Stage result;
165 * @param tc the task context
168 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
170 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
171 GNUNET_SCHEDULER_cancel (abort_task);
172 if (NULL != reg_handle)
173 GNUNET_TESTBED_cancel_registration (reg_handle);
174 GNUNET_TESTBED_controller_disconnect (controller);
175 GNUNET_CONFIGURATION_destroy (cfg);
177 GNUNET_TESTBED_controller_stop (cp);
178 GNUNET_TESTBED_host_destroy (neighbour);
179 GNUNET_TESTBED_host_destroy (host);
184 * abort task to run on test timed out
187 * @param tc the task context
190 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
192 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
193 abort_task = GNUNET_SCHEDULER_NO_TASK;
194 do_shutdown (cls, tc);
199 * Signature of the event handler function called by the
200 * respective event controller.
203 * @param event information about the event
206 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
210 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
211 GNUNET_assert (PEERS_STOPPED == result);
212 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
213 GNUNET_assert (NULL == event->details.operation_finished.emsg);
214 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
215 event->details.operation_finished.pit);
216 GNUNET_assert (NULL ==
217 event->details.operation_finished.op_result.generic);
218 if (event->details.operation_finished.operation == peer1.operation)
220 GNUNET_TESTBED_operation_done (peer1.operation);
221 peer1.operation = NULL;
224 else if (event->details.operation_finished.operation == peer2.operation)
226 GNUNET_TESTBED_operation_done (peer2.operation);
227 peer2.operation = NULL;
233 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
235 case GNUNET_TESTBED_ET_PEER_START:
236 GNUNET_assert (INIT == result);
237 GNUNET_assert (event->details.peer_start.host == host);
238 if (event->details.peer_start.peer == peer1.peer)
240 peer1.is_running = GNUNET_YES;
241 GNUNET_TESTBED_operation_done (peer1.operation);
242 peer1.operation = NULL;
244 else if (event->details.peer_start.peer == peer2.peer)
246 peer2.is_running = GNUNET_YES;
247 GNUNET_TESTBED_operation_done (peer2.operation);
248 peer2.operation = NULL;
252 if ((GNUNET_YES == peer1.is_running) && (GNUNET_YES == peer2.is_running))
254 result = PEERS_STARTED;
256 GNUNET_TESTBED_overlay_connect (NULL, peer1.peer, peer2.peer);
259 case GNUNET_TESTBED_ET_PEER_STOP:
260 GNUNET_assert (PEERS_CONNECTED == result);
261 if (event->details.peer_stop.peer == peer1.peer)
263 peer1.is_running = GNUNET_NO;
264 GNUNET_TESTBED_operation_done (peer1.operation);
265 peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
267 else if (event->details.peer_stop.peer == peer2.peer)
269 peer2.is_running = GNUNET_NO;
270 GNUNET_TESTBED_operation_done (peer2.operation);
271 peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
275 if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running))
276 result = PEERS_STOPPED;
278 case GNUNET_TESTBED_ET_CONNECT:
279 GNUNET_assert (PEERS_STARTED == result);
280 GNUNET_assert (NULL == peer1.operation);
281 GNUNET_assert (NULL == peer2.operation);
282 GNUNET_assert (NULL != common_operation);
283 GNUNET_assert ((event->details.peer_connect.peer1 == peer1.peer)
284 && (event->details.peer_connect.peer2 == peer2.peer));
285 GNUNET_TESTBED_operation_done (common_operation);
286 common_operation = NULL;
287 result = PEERS_CONNECTED;
288 peer1.operation = GNUNET_TESTBED_peer_stop (peer1.peer);
289 peer2.operation = GNUNET_TESTBED_peer_stop (peer2.peer);
298 * Functions of this signature are called when a peer has been successfully
301 * @param cls the closure from GNUNET_TESTBED_peer_create()
302 * @param peer the handle for the created peer; NULL on any error during
304 * @param emsg NULL if peer is not NULL; else MAY contain the error description
307 peer_create_cb (void *cls,
308 struct GNUNET_TESTBED_Peer *peer, const char *emsg)
310 struct PeerContext *pc = cls;
312 GNUNET_assert (NULL != pc->operation);
313 GNUNET_assert (NULL != peer);
314 GNUNET_assert (NULL == pc->peer);
316 GNUNET_TESTBED_operation_done (pc->operation);
317 pc->operation = GNUNET_TESTBED_peer_start (pc->peer);
322 * Callback which will be called to after a host registration succeeded or failed
324 * @param cls the host which has been registered
325 * @param emsg the error message; NULL if host registration is successful
328 registration_comp (void *cls, const char *emsg)
330 GNUNET_assert (cls == neighbour);
332 peer1.operation = GNUNET_TESTBED_peer_create (controller, host, cfg,
333 &peer_create_cb, &peer1);
334 peer2.operation = GNUNET_TESTBED_peer_create (controller, host, cfg,
335 &peer_create_cb, &peer2);
336 GNUNET_assert (NULL != peer1.operation);
337 GNUNET_assert (NULL != peer2.operation);
342 * Callback to signal successfull startup of the controller process
344 * @param cls the closure from GNUNET_TESTBED_controller_start()
345 * @param cfg the configuration with which the controller has been started;
346 * NULL if status is not GNUNET_OK
347 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
348 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
351 status_cb (void *cls,
352 const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
356 GNUNET_assert (GNUNET_OK == status);
358 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
359 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
360 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
361 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
362 controller = GNUNET_TESTBED_controller_connect (cfg, host, event_mask,
363 &controller_cb, NULL);
364 GNUNET_assert (NULL != controller);
365 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
366 GNUNET_assert (NULL != neighbour);
368 GNUNET_TESTBED_register_host (controller, neighbour, ®istration_comp,
370 GNUNET_assert (NULL != reg_handle);
379 * @param args arguments passed to GNUNET_PROGRAM_run
380 * @param cfgfile the path to configuration file
381 * @param cfg the configuration file handle
384 run (void *cls, char *const *args, const char *cfgfile,
385 const struct GNUNET_CONFIGURATION_Handle *config)
387 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
388 GNUNET_assert (NULL != host);
389 cfg = GNUNET_CONFIGURATION_dup (config);
390 cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
391 abort_task = GNUNET_SCHEDULER_add_delayed
392 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort, NULL);
399 int main (int argc, char **argv)
403 char *const argv2[] = { "test_testbed_api",
404 "-c", "test_testbed_api.conf",
407 struct GNUNET_GETOPT_CommandLineOption options[] = {
408 GNUNET_GETOPT_OPTION_END
411 ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
412 "test_testbed_api", "nohelp", options, &run,
414 if ((GNUNET_OK != ret) || (SUCCESS != result))