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_testing_lib-new.h"
30 #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 const struct GNUNET_CONFIGURATION_Handle *cfg;
83 static struct GNUNET_TESTBED_Operation *operation;
86 * Abort task identifier
88 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
100 * @param tc the task context
103 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
105 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
106 GNUNET_SCHEDULER_cancel (abort_task);
107 if (NULL != reg_handle)
108 GNUNET_TESTBED_cancel_registration (reg_handle);
109 GNUNET_TESTBED_controller_disconnect (controller);
111 GNUNET_TESTBED_controller_stop (cp);
112 GNUNET_TESTBED_host_destroy (neighbour);
113 GNUNET_TESTBED_host_destroy (host);
118 * abort task to run on test timed out
121 * @param tc the task context
124 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
126 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
127 abort_task = GNUNET_SCHEDULER_NO_TASK;
128 do_shutdown (cls, tc);
133 * Signature of the event handler function called by the
134 * respective event controller.
137 * @param event information about the event
140 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
142 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
143 GNUNET_assert (event->details.operation_finished.operation == operation);
144 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
145 GNUNET_assert (NULL == event->details.operation_finished.emsg);
146 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
147 event->details.operation_finished.pit);
148 GNUNET_assert (NULL == event->details.operation_finished.op_result.generic);
150 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
155 * Callback which will be called to after a host registration succeeded or failed
157 * @param cls the host which has been registered
158 * @param emsg the error message; NULL if host registration is successful
161 registration_comp (void *cls, const char *emsg)
163 GNUNET_assert (cls == neighbour);
165 peer = GNUNET_TESTBED_peer_create (controller, host, cfg);
166 GNUNET_assert (NULL != peer);
167 operation = GNUNET_TESTBED_peer_destroy (peer);
168 GNUNET_assert (NULL != operation);
176 * @param args arguments passed to GNUNET_PROGRAM_run
177 * @param cfgfile the path to configuration file
178 * @param cfg the configuration file handle
181 run (void *cls, char *const *args, const char *cfgfile,
182 const struct GNUNET_CONFIGURATION_Handle *config)
185 struct GNUNET_CONFIGURATION_Handle *cdup;
188 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
189 GNUNET_assert (NULL != host);
190 cdup = GNUNET_CONFIGURATION_dup (config);
191 cp = GNUNET_TESTBED_controller_start (system, host, cdup, NULL, NULL);
193 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
194 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
195 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
196 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
197 controller = GNUNET_TESTBED_controller_connect (config, host, event_mask,
198 &controller_cb, NULL);
199 GNUNET_assert (NULL != controller);
200 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
201 GNUNET_assert (NULL != neighbour);
203 GNUNET_TESTBED_register_host (controller, neighbour, ®istration_comp,
205 GNUNET_assert (NULL != reg_handle);
206 abort_task = GNUNET_SCHEDULER_add_delayed
207 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort, NULL);
214 int main (int argc, char **argv)
218 char *const argv2[] = { "test_testbed_api",
219 "-c", "test_testbed_api.conf",
222 struct GNUNET_GETOPT_CommandLineOption options[] = {
223 GNUNET_GETOPT_OPTION_END
225 result = GNUNET_SYSERR;
226 ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
227 "test_testbed_api", "nohelp", options, &run,
229 if ((GNUNET_OK != ret) || (GNUNET_OK != result))