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 "mesh_test_lib.h"
27 #include "gnunet_mesh_service.h"
30 * Test context for a MESH Test.
32 struct GNUNET_MESH_TEST_Context
35 * Array of running peers.
37 struct GNUNET_TESTBED_Peer **peers;
40 * Array of handles to the MESH for each peer.
42 struct GNUNET_MESH_Handle **meshes;
45 * Operation associated with the connection to the MESH.
47 struct GNUNET_TESTBED_Operation **ops;
50 * Main function of the test to run once all MESHs are available.
52 GNUNET_MESH_TEST_AppMain app_main;
55 * Closure for 'app_main'.
60 * Number of peers running, size of the arrays above.
62 unsigned int num_peers;
65 * Handler for incoming tunnels.
67 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
70 * Cleaner for destroyed incoming tunnels.
72 GNUNET_MESH_TunnelEndHandler *cleaner;
77 struct GNUNET_MESH_MessageHandler* handlers;
82 const GNUNET_MESH_ApplicationType* stypes;
88 * Context for a mesh adapter callback.
90 struct GNUNET_MESH_TEST_AdapterContext
93 * Peer number for the particular peer.
100 struct GNUNET_MESH_TEST_Context *ctx;
105 * Adapter function called to establish a connection to
109 * @param cfg configuration of the peer to connect to; will be available until
110 * GNUNET_TESTBED_operation_done() is called on the operation returned
111 * from GNUNET_TESTBED_service_connect()
112 * @return service handle to return in 'op_result', NULL on error
115 mesh_connect_adapter (void *cls,
116 const struct GNUNET_CONFIGURATION_Handle *cfg)
118 struct GNUNET_MESH_TEST_AdapterContext *actx = cls;
119 struct GNUNET_MESH_TEST_Context *ctx = actx->ctx;
120 struct GNUNET_MESH_Handle *h;
122 h = GNUNET_MESH_connect (cfg,
123 (void *) (long) actx->peer,
133 * Adapter function called to destroy a connection to
137 * @param op_result service handle returned from the connect adapter
140 mesh_disconnect_adapter (void *cls,
143 struct GNUNET_MESH_Handle *mesh = op_result;
144 struct GNUNET_MESH_TEST_AdapterContext *actx = cls;
147 GNUNET_MESH_disconnect (mesh);
152 * Callback to be called when a service connect operation is completed.
154 * @param cls The callback closure from functions generating an operation.
155 * @param op The operation that has been finished.
156 * @param ca_result The service handle returned from
157 * GNUNET_TESTBED_ConnectAdapter() (mesh handle).
158 * @param emsg Error message in case the operation has failed.
159 * NULL if operation has executed successfully.
162 mesh_connect_cb (void *cls,
163 struct GNUNET_TESTBED_Operation *op,
167 struct GNUNET_MESH_TEST_Context *ctx = cls;
172 fprintf (stderr, "Failed to connect to MESH service: %s\n",
174 GNUNET_SCHEDULER_shutdown ();
177 for (i = 0; i < ctx->num_peers; i++)
178 if (op == ctx->ops[i])
179 ctx->meshes[i] = ca_result;
180 for (i = 0; i < ctx->num_peers; i++)
181 if (NULL == ctx->meshes[i])
182 return; /* still some MESH connections missing */
183 /* all MESH connections ready! */
184 ctx->app_main (ctx->app_main_cls,
193 * Clean up the testbed.
195 * @param ctx handle for the testbed
198 GNUNET_MESH_TEST_cleanup (struct GNUNET_MESH_TEST_Context *ctx)
202 for (i = 0; i < ctx->num_peers; i++)
203 GNUNET_TESTBED_operation_done (ctx->ops[i]);
204 GNUNET_free (ctx->ops);
205 GNUNET_free (ctx->meshes);
207 GNUNET_SCHEDULER_shutdown ();
212 * Callback run when the testbed is ready (peers running and connected to
215 * @param cls Closure (context).
216 * @param num_peers Number of peers that are running.
217 * @param peers Handles to each one of the @c num_peers peers.
220 mesh_test_run (void *cls,
221 unsigned int num_peers,
222 struct GNUNET_TESTBED_Peer **peers)
224 struct GNUNET_MESH_TEST_Context *ctx = cls;
227 GNUNET_assert (num_peers == ctx->num_peers);
229 for (i = 0; i < num_peers; i++)
231 struct GNUNET_MESH_TEST_AdapterContext *newctx;
232 newctx = GNUNET_malloc (sizeof (struct GNUNET_MESH_TEST_AdapterContext));
235 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
240 &mesh_connect_adapter,
241 &mesh_disconnect_adapter,
248 * Run a test using the given name, configuration file and number of
250 * All mesh callbacks will receive the peer number as the closure.
252 * @param testname Name of the test (for logging).
253 * @param cfgname Name of the configuration file.
254 * @param num_peers Number of peers to start.
255 * @param tmain Main function to run once the testbed is ready.
256 * @param tmain_cls Closure for 'tmain'.
257 * @param new_tunnel Handler for incoming tunnels.
258 * @param cleaner Cleaner for destroyed incoming tunnels.
259 * @param handlers Message handlers.
260 * @param stypes Application types.
263 GNUNET_MESH_TEST_run (const char *testname,
265 unsigned int num_peers,
266 GNUNET_MESH_TEST_AppMain tmain,
268 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
269 GNUNET_MESH_TunnelEndHandler cleaner,
270 struct GNUNET_MESH_MessageHandler* handlers,
271 const GNUNET_MESH_ApplicationType* stypes)
273 struct GNUNET_MESH_TEST_Context *ctx;
275 ctx = GNUNET_malloc (sizeof (struct GNUNET_MESH_TEST_Context));
276 ctx->num_peers = num_peers;
277 ctx->ops = GNUNET_malloc (num_peers * sizeof (struct GNUNET_TESTBED_Operation *));
278 ctx->meshes = GNUNET_malloc (num_peers * sizeof (struct GNUNET_MESH_Handle *));
279 ctx->app_main = tmain;
280 ctx->app_main_cls = tmain_cls;
281 ctx->new_tunnel = new_tunnel;
282 ctx->cleaner = cleaner;
283 ctx->handlers = handlers;
284 ctx->stypes = stypes;
285 GNUNET_TESTBED_test_run (testname,
289 &mesh_test_run, ctx);
292 /* end of mesh_test_lib.c */