2 This file is part of GNUnet.
3 (C) 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.
21 * @file mesh/mesh_test_lib.c
22 * @author Bartlomiej Polot
23 * @brief library for writing MESH tests
26 #include "gnunet_util_lib.h"
27 #include "mesh_test_lib.h"
28 #include "gnunet_mesh_service.h"
31 * Test context for a MESH Test.
33 struct GNUNET_MESH_TEST_Context
36 * Array of running peers.
38 struct GNUNET_TESTBED_Peer **peers;
41 * Array of handles to the MESH for each peer.
43 struct GNUNET_MESH_Handle **meshes;
46 * Operation associated with the connection to the MESH.
48 struct GNUNET_TESTBED_Operation **ops;
51 * Main function of the test to run once all MESHs are available.
53 GNUNET_MESH_TEST_AppMain app_main;
56 * Closure for 'app_main'.
61 * Number of peers running, size of the arrays above.
63 unsigned int num_peers;
66 * Handler for incoming tunnels.
68 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
71 * Cleaner for destroyed incoming tunnels.
73 GNUNET_MESH_TunnelEndHandler *cleaner;
78 struct GNUNET_MESH_MessageHandler* handlers;
83 const GNUNET_MESH_ApplicationType* stypes;
89 * Context for a mesh adapter callback.
91 struct GNUNET_MESH_TEST_AdapterContext
94 * Peer number for the particular peer.
101 struct GNUNET_MESH_TEST_Context *ctx;
106 * Adapter function called to establish a connection to
110 * @param cfg configuration of the peer to connect to; will be available until
111 * GNUNET_TESTBED_operation_done() is called on the operation returned
112 * from GNUNET_TESTBED_service_connect()
113 * @return service handle to return in 'op_result', NULL on error
116 mesh_connect_adapter (void *cls,
117 const struct GNUNET_CONFIGURATION_Handle *cfg)
119 struct GNUNET_MESH_TEST_AdapterContext *actx = cls;
120 struct GNUNET_MESH_TEST_Context *ctx = actx->ctx;
121 struct GNUNET_MESH_Handle *h;
123 h = GNUNET_MESH_connect (cfg,
124 (void *) (long) actx->peer,
134 * Adapter function called to destroy a connection to
138 * @param op_result service handle returned from the connect adapter
141 mesh_disconnect_adapter (void *cls,
144 struct GNUNET_MESH_Handle *mesh = op_result;
145 struct GNUNET_MESH_TEST_AdapterContext *actx = cls;
148 GNUNET_MESH_disconnect (mesh);
153 * Callback to be called when a service connect operation is completed.
155 * @param cls The callback closure from functions generating an operation.
156 * @param op The operation that has been finished.
157 * @param ca_result The service handle returned from
158 * GNUNET_TESTBED_ConnectAdapter() (mesh handle).
159 * @param emsg Error message in case the operation has failed.
160 * NULL if operation has executed successfully.
163 mesh_connect_cb (void *cls,
164 struct GNUNET_TESTBED_Operation *op,
168 struct GNUNET_MESH_TEST_Context *ctx = cls;
173 fprintf (stderr, "Failed to connect to MESH service: %s\n",
175 GNUNET_SCHEDULER_shutdown ();
178 for (i = 0; i < ctx->num_peers; i++)
179 if (op == ctx->ops[i])
180 ctx->meshes[i] = ca_result;
181 for (i = 0; i < ctx->num_peers; i++)
182 if (NULL == ctx->meshes[i])
183 return; /* still some MESH connections missing */
184 /* all MESH connections ready! */
185 ctx->app_main (ctx->app_main_cls,
194 * Clean up the testbed.
196 * @param ctx handle for the testbed
199 GNUNET_MESH_TEST_cleanup (struct GNUNET_MESH_TEST_Context *ctx)
203 for (i = 0; i < ctx->num_peers; i++)
205 GNUNET_assert (NULL != ctx->ops[i]);
206 GNUNET_TESTBED_operation_done (ctx->ops[i]);
209 GNUNET_free (ctx->ops);
210 GNUNET_free (ctx->meshes);
212 GNUNET_SCHEDULER_shutdown ();
217 * Callback run when the testbed is ready (peers running and connected to
220 * @param cls Closure (context).
221 * @param num_peers Number of peers that are running.
222 * @param peers Handles to each one of the @c num_peers peers.
223 * @param links_succeeded the number of overlay link connection attempts that
225 * @param links_failed the number of overlay link connection attempts that
229 mesh_test_run (void *cls,
230 unsigned int num_peers,
231 struct GNUNET_TESTBED_Peer **peers,
232 unsigned int links_succeeded,
233 unsigned int links_failed)
235 struct GNUNET_MESH_TEST_Context *ctx = cls;
238 GNUNET_assert (num_peers == ctx->num_peers);
240 for (i = 0; i < num_peers; i++)
242 struct GNUNET_MESH_TEST_AdapterContext *newctx;
243 newctx = GNUNET_malloc (sizeof (struct GNUNET_MESH_TEST_AdapterContext));
246 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
251 &mesh_connect_adapter,
252 &mesh_disconnect_adapter,
259 * Run a test using the given name, configuration file and number of
261 * All mesh callbacks will receive the peer number as the closure.
263 * @param testname Name of the test (for logging).
264 * @param cfgname Name of the configuration file.
265 * @param num_peers Number of peers to start.
266 * @param tmain Main function to run once the testbed is ready.
267 * @param tmain_cls Closure for 'tmain'.
268 * @param new_tunnel Handler for incoming tunnels.
269 * @param cleaner Cleaner for destroyed incoming tunnels.
270 * @param handlers Message handlers.
271 * @param stypes Application types.
274 GNUNET_MESH_TEST_run (const char *testname,
276 unsigned int num_peers,
277 GNUNET_MESH_TEST_AppMain tmain,
279 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
280 GNUNET_MESH_TunnelEndHandler cleaner,
281 struct GNUNET_MESH_MessageHandler* handlers,
282 const GNUNET_MESH_ApplicationType* stypes)
284 struct GNUNET_MESH_TEST_Context *ctx;
286 ctx = GNUNET_malloc (sizeof (struct GNUNET_MESH_TEST_Context));
287 ctx->num_peers = num_peers;
288 ctx->ops = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Operation *));
289 ctx->meshes = GNUNET_malloc (num_peers * sizeof (struct GNUNET_MESH_Handle *));
290 ctx->app_main = tmain;
291 ctx->app_main_cls = tmain_cls;
292 ctx->new_tunnel = new_tunnel;
293 ctx->cleaner = cleaner;
294 ctx->handlers = handlers;
295 ctx->stypes = stypes;
296 GNUNET_TESTBED_test_run (testname,
300 &mesh_test_run, ctx);
303 /* end of mesh_test_lib.c */