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_controllerlink.c
23 * @brief testcase for testing controller to subcontroller linking
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib-new.h"
30 #include "gnunet_testbed_service.h"
33 * Generic logging shortcut
35 #define LOG(kind,...) \
36 GNUNET_log (kind, __VA_ARGS__)
39 * Debug logging shorthand
41 #define LOG_DEBUG(...) \
42 LOG(GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
45 * Different stages in testing
56 * Master controller has started
61 * The first slave has been registered at master controller
66 * The second slave has been registered at the master controller
71 * Link from master to slave 1 has been successfully created
76 * Link from slave 1 to slave 2 has been successfully created.
83 * Host for running master controller
85 static struct GNUNET_TESTBED_Host *host;
88 * The master controller process
90 static struct GNUNET_TESTBED_ControllerProc *cp;
93 * Handle to master controller
95 static struct GNUNET_TESTBED_Controller *mc;
98 * Slave host for running slave controller
100 static struct GNUNET_TESTBED_Host *slave;
103 * Another slave host for running another slave controller
105 static struct GNUNET_TESTBED_Host *slave2;
108 * Slave host registration handle
110 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
113 * Handle to global configuration
115 static struct GNUNET_CONFIGURATION_Handle *cfg;
120 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
123 * Operation handle for linking controllers
125 static struct GNUNET_TESTBED_Operation *op;
133 * Global testing status
135 static enum Stage result;
142 * @param tc the task context
145 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
147 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
148 GNUNET_SCHEDULER_cancel (abort_task);
150 GNUNET_TESTBED_host_destroy (slave2);
152 GNUNET_TESTBED_host_destroy (slave);
154 GNUNET_TESTBED_host_destroy (host);
156 GNUNET_TESTBED_controller_disconnect (mc);
158 GNUNET_CONFIGURATION_destroy (cfg);
160 GNUNET_TESTBED_controller_stop (cp);
162 GNUNET_TESTBED_cancel_registration (rh);
168 * abort task to run on test timed out
171 * @param tc the task context
174 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
176 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
177 abort_task = GNUNET_SCHEDULER_NO_TASK;
178 do_shutdown (cls, tc);
183 * Signature of the event handler function called by the
184 * respective event controller.
187 * @param event information about the event
190 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
194 case SLAVE2_REGISTERED:
195 GNUNET_assert (NULL != event);
196 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
197 GNUNET_assert (event->details.operation_finished.operation == op);
198 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
199 GNUNET_assert (NULL == event->details.operation_finished.emsg);
200 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
201 event->details.operation_finished.pit);
202 GNUNET_assert (NULL == event->details.operation_finished.op_result.generic);
203 GNUNET_TESTBED_operation_done (op);
205 result = SLAVE1_LINK_SUCCESS;
206 GNUNET_assert (NULL != slave2);
207 GNUNET_assert (NULL != slave);
208 op = GNUNET_TESTBED_controller_link (mc, slave2, slave, cfg, GNUNET_YES);
209 GNUNET_assert (NULL != op);
211 case SLAVE1_LINK_SUCCESS:
212 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
213 GNUNET_assert (event->details.operation_finished.operation == op);
214 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
215 GNUNET_assert (NULL == event->details.operation_finished.emsg);
216 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
217 event->details.operation_finished.pit);
218 GNUNET_assert (NULL == event->details.operation_finished.op_result.generic);
219 GNUNET_TESTBED_operation_done (op);
222 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
231 * Callback which will be called to after a host registration succeeded or failed
233 * @param cls the host which has been registered
234 * @param emsg the error message; NULL if host registration is successful
237 registration_cont (void *cls, const char *emsg)
243 GNUNET_assert (NULL == emsg);
244 GNUNET_assert (NULL != mc);
245 result = SLAVE1_REGISTERED;
246 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, 0);
247 GNUNET_assert (NULL != slave2);
248 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
249 GNUNET_assert (NULL != rh);
251 case SLAVE1_REGISTERED:
252 GNUNET_assert (NULL == emsg);
253 GNUNET_assert (NULL != mc);
254 result = SLAVE2_REGISTERED;
255 GNUNET_assert (NULL != cfg);
256 op = GNUNET_TESTBED_controller_link (mc, slave, NULL, cfg, GNUNET_YES);
257 GNUNET_assert (NULL != op);
261 case SLAVE2_REGISTERED:
262 case SLAVE1_LINK_SUCCESS:
268 * Callback to signal successfull startup of the controller process
270 * @param cls the closure from GNUNET_TESTBED_controller_start()
271 * @param cfg the configuration with which the controller has been started;
272 * NULL if status is not GNUNET_OK
273 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
274 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
277 status_cb (void *cls,
278 const struct GNUNET_CONFIGURATION_Handle *config, int status)
283 GNUNET_assert (GNUNET_OK == status);
285 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
286 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
287 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
288 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
289 mc = GNUNET_TESTBED_controller_connect (config, host, event_mask,
290 &controller_cb, NULL);
291 GNUNET_assert (NULL != mc);
292 result = MASTER_STARTED;
293 slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, 0);
294 GNUNET_assert (NULL != slave);
295 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
296 GNUNET_assert (NULL != rh);
308 * @param args arguments passed to GNUNET_PROGRAM_run
309 * @param cfgfile the path to configuration file
310 * @param cfg the configuration file handle
313 run (void *cls, char *const *args, const char *cfgfile,
314 const struct GNUNET_CONFIGURATION_Handle *config)
316 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
317 GNUNET_assert (NULL != host);
318 cfg = GNUNET_CONFIGURATION_dup (config);
320 GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
321 abort_task = GNUNET_SCHEDULER_add_delayed
322 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5),
330 int main (int argc, char **argv)
334 char *const argv2[] = { "test_testbed_api_controllerlink",
335 "-c", "test_testbed_api.conf",
338 struct GNUNET_GETOPT_CommandLineOption options[] = {
339 GNUNET_GETOPT_OPTION_END
342 ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
343 "test_testbed_api_controllerlink", "nohelp", options, &run,
345 if ((GNUNET_OK != ret) || (SUCCESS != result))