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 <sreeharsha@totakura.in>
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__)
43 static struct GNUNET_TESTBED_Host *host;
46 * The controller handle
48 static struct GNUNET_TESTBED_Controller *c;
53 static struct GNUNET_TESTBED_Host *neighbour;
56 * Handle for neighbour registration
58 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
61 * Abort task identifier
63 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
75 * @param tc the task context
78 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
80 if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
81 GNUNET_SCHEDULER_cancel (abort_task_id);
82 if (NULL != reg_handle)
83 GNUNET_TESTBED_cancel_registration (reg_handle);
84 GNUNET_TESTBED_controller_disconnect (c);
85 GNUNET_TESTBED_host_destroy (neighbour);
86 GNUNET_TESTBED_host_destroy (host);
91 * abort task to run on test timed out
94 * @param tc the task context
97 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
99 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
100 abort_task_id = GNUNET_SCHEDULER_NO_TASK;
101 do_shutdown (cls, tc);
106 * Signature of the event handler function called by the
107 * respective event controller.
110 * @param event information about the event
113 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
120 * Callback which will be called to after a host registration succeeded or failed
122 * @param cls the host which has been registered
123 * @param emsg the error message; NULL if host registration is successful
126 registration_comp (void *cls, const char *emsg)
128 GNUNET_assert (cls == neighbour);
131 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
136 * Main point of test execution
140 const struct GNUNET_CONFIGURATION_Handle *cfg,
141 struct GNUNET_TESTING_Peer *peer)
145 host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
146 GNUNET_assert (NULL != host);
147 event_mask ^= event_mask; /* NULL out */
148 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
149 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
150 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
151 c = GNUNET_TESTBED_controller_connect (cfg, host, event_mask,
152 &controller_cb, NULL);
153 GNUNET_assert (NULL != c);
154 neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
155 GNUNET_assert (NULL != neighbour);
157 GNUNET_TESTBED_register_host (c, neighbour, ®istration_comp, neighbour);
158 GNUNET_assert (NULL != reg_handle);
160 abort_task_id = GNUNET_SCHEDULER_add_delayed
161 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30), &do_abort, NULL);
168 int main (int argc, char **argv)
170 result = GNUNET_SYSERR;
171 if (0 != GNUNET_TESTING_service_run ("test_testbed_api",
173 "test_testbed_api.conf",
176 else return (GNUNET_OK == result) ? 0 : 1;