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
73 * Host for running master controller
75 static struct GNUNET_TESTBED_Host *host;
78 * The master controller process
80 static struct GNUNET_TESTBED_ControllerProc *cp;
83 * Handle to master controller
85 static struct GNUNET_TESTBED_Controller *mc;
88 * Slave host for running slave controller
90 static struct GNUNET_TESTBED_Host *slave;
93 * Slave host registration handle
95 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
98 * Handle to global configuration
100 static struct GNUNET_CONFIGURATION_Handle *cfg;
105 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
108 * Operation handle for linking controllers
110 static struct GNUNET_TESTBED_Operation *op;
118 * Global testing status
120 static enum Stage result;
127 * @param tc the task context
130 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
132 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
133 GNUNET_SCHEDULER_cancel (abort_task);
135 GNUNET_TESTBED_host_destroy (slave);
137 GNUNET_TESTBED_host_destroy (host);
139 GNUNET_TESTBED_controller_disconnect (mc);
141 GNUNET_CONFIGURATION_destroy (cfg);
143 GNUNET_TESTBED_controller_stop (cp);
145 GNUNET_TESTBED_cancel_registration (rh);
151 * abort task to run on test timed out
154 * @param tc the task context
157 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
159 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
160 abort_task = GNUNET_SCHEDULER_NO_TASK;
161 do_shutdown (cls, tc);
166 * Signature of the event handler function called by the
167 * respective event controller.
170 * @param event information about the event
173 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
177 case SLAVE1_REGISTERED:
178 GNUNET_assert (NULL != event);
179 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
180 GNUNET_assert (event->details.operation_finished.operation == op);
181 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
182 GNUNET_assert (NULL == event->details.operation_finished.emsg);
183 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
184 event->details.operation_finished.pit);
185 GNUNET_assert (NULL == event->details.operation_finished.op_result.generic);
186 GNUNET_TESTBED_operation_done (op);
189 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
198 * Callback which will be called to after a host registration succeeded or failed
200 * @param cls the host which has been registered
201 * @param emsg the error message; NULL if host registration is successful
204 registration_cont (void *cls, const char *emsg)
210 GNUNET_assert (NULL == emsg);
211 GNUNET_assert (NULL != mc);
212 result = SLAVE1_REGISTERED;
213 GNUNET_assert (NULL != cfg);
214 op = GNUNET_TESTBED_controller_link (mc, slave, NULL, cfg, GNUNET_YES);
215 GNUNET_assert (NULL != op);
219 case SLAVE1_REGISTERED:
225 * Callback to signal successfull startup of the controller process
227 * @param cls the closure from GNUNET_TESTBED_controller_start()
228 * @param cfg the configuration with which the controller has been started;
229 * NULL if status is not GNUNET_OK
230 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
231 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
234 status_cb (void *cls,
235 const struct GNUNET_CONFIGURATION_Handle *config, int status)
240 GNUNET_assert (GNUNET_OK == status);
242 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
243 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
244 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
245 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
246 mc = GNUNET_TESTBED_controller_connect (config, host, event_mask,
247 &controller_cb, NULL);
248 GNUNET_assert (NULL != mc);
249 result = MASTER_STARTED;
250 slave = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, 0);
251 GNUNET_assert (NULL != slave);
252 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
253 GNUNET_assert (NULL != rh);
265 * @param args arguments passed to GNUNET_PROGRAM_run
266 * @param cfgfile the path to configuration file
267 * @param cfg the configuration file handle
270 run (void *cls, char *const *args, const char *cfgfile,
271 const struct GNUNET_CONFIGURATION_Handle *config)
273 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
274 GNUNET_assert (NULL != host);
275 cfg = GNUNET_CONFIGURATION_dup (config);
277 GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
278 abort_task = GNUNET_SCHEDULER_add_delayed
279 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5),
287 int main (int argc, char **argv)
291 char *const argv2[] = { "test_testbed_api_controllerlink",
292 "-c", "test_testbed_api.conf",
295 struct GNUNET_GETOPT_CommandLineOption options[] = {
296 GNUNET_GETOPT_OPTION_END
299 ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
300 "test_testbed_api_controllerlink", "nohelp", options, &run,
302 if ((GNUNET_OK != ret) || (SUCCESS != result))