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.
81 * Peer create on slave 1 successful
83 SLAVE1_PEER_CREATE_SUCCESS,
92 * Host for running master controller
94 static struct GNUNET_TESTBED_Host *host;
97 * The master controller process
99 static struct GNUNET_TESTBED_ControllerProc *cp;
102 * Handle to master controller
104 static struct GNUNET_TESTBED_Controller *mc;
107 * Slave host for running slave controller
109 static struct GNUNET_TESTBED_Host *slave;
112 * Another slave host for running another slave controller
114 static struct GNUNET_TESTBED_Host *slave2;
117 * Slave host registration handle
119 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
122 * Handle to global configuration
124 static struct GNUNET_CONFIGURATION_Handle *cfg;
129 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
132 * Operation handle for linking controllers
134 static struct GNUNET_TESTBED_Operation *op;
137 * Handle to peer started at slave 1
139 static struct GNUNET_TESTBED_Peer *slave1_peer;
142 * Handle to peer started at slave 2
144 static struct GNUNET_TESTBED_Peer *slave2_peer;
152 * Global testing status
154 static enum Stage result;
161 * @param tc the task context
164 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
166 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
167 GNUNET_SCHEDULER_cancel (abort_task);
169 GNUNET_TESTBED_host_destroy (slave2);
171 GNUNET_TESTBED_host_destroy (slave);
173 GNUNET_TESTBED_host_destroy (host);
175 GNUNET_TESTBED_controller_disconnect (mc);
177 GNUNET_CONFIGURATION_destroy (cfg);
179 GNUNET_TESTBED_controller_stop (cp);
181 GNUNET_TESTBED_cancel_registration (rh);
187 * abort task to run on test timed out
190 * @param tc the task context
193 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
195 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
196 abort_task = GNUNET_SCHEDULER_NO_TASK;
197 do_shutdown (cls, tc);
202 * Functions of this signature are called when a peer has been successfully
205 * @param cls the closure from GNUNET_TESTBED_peer_create()
206 * @param peer the handle for the created peer; NULL on any error during
208 * @param emsg NULL if peer is not NULL; else MAY contain the error description
211 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
215 case SLAVE2_LINK_SUCCESS:
216 GNUNET_assert (NULL != peer);
217 GNUNET_assert (NULL == emsg);
218 result = SLAVE1_PEER_CREATE_SUCCESS;
220 GNUNET_TESTBED_operation_done (op);
221 op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
222 GNUNET_assert (NULL != op);
224 case SLAVE1_PEER_CREATE_SUCCESS:
225 GNUNET_assert (NULL != peer);
226 GNUNET_assert (NULL == emsg);
229 GNUNET_TESTBED_operation_done (op);
230 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
239 * Signature of the event handler function called by the
240 * respective event controller.
243 * @param event information about the event
246 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
250 case SLAVE2_REGISTERED:
251 GNUNET_assert (NULL != event);
252 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
253 GNUNET_assert (event->details.operation_finished.operation == op);
254 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
255 GNUNET_assert (NULL == event->details.operation_finished.emsg);
256 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
257 event->details.operation_finished.pit);
258 GNUNET_assert (NULL == event->details.operation_finished.op_result.generic);
259 GNUNET_TESTBED_operation_done (op);
261 result = SLAVE1_LINK_SUCCESS;
262 GNUNET_assert (NULL != slave2);
263 GNUNET_assert (NULL != slave);
264 op = GNUNET_TESTBED_controller_link (mc, slave2, slave, cfg, GNUNET_YES);
265 GNUNET_assert (NULL != op);
267 case SLAVE1_LINK_SUCCESS:
268 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
269 GNUNET_assert (event->details.operation_finished.operation == op);
270 GNUNET_assert (NULL == event->details.operation_finished.op_cls);
271 GNUNET_assert (NULL == event->details.operation_finished.emsg);
272 GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
273 event->details.operation_finished.pit);
274 GNUNET_assert (NULL == event->details.operation_finished.op_result.generic);
275 GNUNET_TESTBED_operation_done (op);
277 result = SLAVE2_LINK_SUCCESS;
278 op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
279 GNUNET_assert (NULL != op);
288 * Callback which will be called to after a host registration succeeded or failed
290 * @param cls the host which has been registered
291 * @param emsg the error message; NULL if host registration is successful
294 registration_cont (void *cls, const char *emsg)
300 GNUNET_assert (NULL == emsg);
301 GNUNET_assert (NULL != mc);
302 result = SLAVE1_REGISTERED;
303 slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, 0);
304 GNUNET_assert (NULL != slave2);
305 rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL);
306 GNUNET_assert (NULL != rh);
308 case SLAVE1_REGISTERED:
309 GNUNET_assert (NULL == emsg);
310 GNUNET_assert (NULL != mc);
311 result = SLAVE2_REGISTERED;
312 GNUNET_assert (NULL != cfg);
313 op = GNUNET_TESTBED_controller_link (mc, slave, NULL, cfg, GNUNET_YES);
314 GNUNET_assert (NULL != op);
322 * Callback to signal successfull startup of the controller process
324 * @param cls the closure from GNUNET_TESTBED_controller_start()
325 * @param cfg the configuration with which the controller has been started;
326 * NULL if status is not GNUNET_OK
327 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
328 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
331 status_cb (void *cls,
332 const struct GNUNET_CONFIGURATION_Handle *config, int status)
337 GNUNET_assert (GNUNET_OK == status);
339 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
340 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
341 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
342 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
343 mc = GNUNET_TESTBED_controller_connect (config, host, event_mask,
344 &controller_cb, NULL);
345 GNUNET_assert (NULL != mc);
346 result = MASTER_STARTED;
347 slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, 0);
348 GNUNET_assert (NULL != slave);
349 rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL);
350 GNUNET_assert (NULL != rh);
362 * @param args arguments passed to GNUNET_PROGRAM_run
363 * @param cfgfile the path to configuration file
364 * @param cfg the configuration file handle
367 run (void *cls, char *const *args, const char *cfgfile,
368 const struct GNUNET_CONFIGURATION_Handle *config)
370 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
371 GNUNET_assert (NULL != host);
372 cfg = GNUNET_CONFIGURATION_dup (config);
374 GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
375 abort_task = GNUNET_SCHEDULER_add_delayed
376 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5),
384 int main (int argc, char **argv)
387 char *const argv2[] = { "test_testbed_api_controllerlink",
388 "-c", "test_testbed_api.conf",
391 struct GNUNET_GETOPT_CommandLineOption options[] = {
392 GNUNET_GETOPT_OPTION_END
394 char *const remote_args[] = {
395 "ssh", "-o", "BatchMode=yes", "127.0.0.1", "echo", "Hello", "World", NULL
397 struct GNUNET_OS_Process *auxp;
398 enum GNUNET_OS_ProcessStatusType type;
401 auxp = GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL,
402 NULL, NULL, "ssh", remote_args);
403 GNUNET_assert (NULL != auxp);
406 ret = GNUNET_OS_process_status (auxp, &type, &code);
407 GNUNET_assert (GNUNET_SYSERR != ret);
409 } while (GNUNET_NO == ret);
412 (void) printf ("Unable to run the test as this system is not configured "
413 "to use password less SSH logins to localhost.\n"
414 "Marking test as successful\n");
418 ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
419 "test_testbed_api_controllerlink", "nohelp", options, &run,
421 if ((GNUNET_OK != ret) || (SUCCESS != result))