2 This file is part of GNUnet.
3 Copyright (C) 2012, 2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/cadet_test_lib.c
22 * @author Bartlomiej Polot
23 * @brief library for writing CADET tests
26 #include "gnunet_util_lib.h"
27 #include "cadet_test_lib.h"
28 #include "gnunet_cadet_service.h"
32 * Test context for a CADET Test.
34 struct GNUNET_CADET_TEST_Context
37 * Array of running peers.
39 struct GNUNET_TESTBED_Peer **peers;
42 * Array of handles to the CADET for each peer.
44 struct GNUNET_CADET_Handle **cadets;
47 * Operation associated with the connection to the CADET.
49 struct GNUNET_TESTBED_Operation **ops;
52 * Number of peers running, size of the arrays above.
54 unsigned int num_peers;
57 * Main function of the test to run once all CADETs are available.
59 GNUNET_CADET_TEST_AppMain app_main;
62 * Closure for 'app_main'.
67 * Handler for incoming tunnels.
69 GNUNET_CADET_ConnectEventHandler connects;
72 * Function called when the transmit window size changes.
74 GNUNET_CADET_WindowSizeEventHandler window_changes;
77 * Cleaner for destroyed incoming tunnels.
79 GNUNET_CADET_DisconnectEventHandler disconnects;
84 struct GNUNET_MQ_MessageHandler *handlers;
89 const struct GNUNET_HashCode **ports;
92 * Number of ports in #ports.
94 unsigned int port_count;
100 * Context for a cadet adapter callback.
102 struct GNUNET_CADET_TEST_AdapterContext
105 * Peer number for the particular peer.
110 * Port handlers for open ports.
112 struct GNUNET_CADET_Port **ports;
117 struct GNUNET_CADET_TEST_Context *ctx;
122 * Adapter function called to establish a connection to
126 * @param cfg configuration of the peer to connect to; will be available until
127 * GNUNET_TESTBED_operation_done() is called on the operation returned
128 * from GNUNET_TESTBED_service_connect()
129 * @return service handle to return in 'op_result', NULL on error
132 cadet_connect_adapter (void *cls,
133 const struct GNUNET_CONFIGURATION_Handle *cfg)
135 struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
136 struct GNUNET_CADET_TEST_Context *ctx = actx->ctx;
137 struct GNUNET_CADET_Handle *h;
139 h = GNUNET_CADET_connect (cfg);
140 if (NULL == ctx->ports)
142 actx->ports = GNUNET_new_array (ctx->port_count,
143 struct GNUNET_CADET_Port *);
144 for (unsigned int i = 0; i < ctx->port_count; i++)
146 actx->ports[i] = GNUNET_CADET_open_port (h,
149 (void *) (long) actx->peer,
159 * Adapter function called to destroy a connection to
163 * @param op_result service handle returned from the connect adapter
166 cadet_disconnect_adapter (void *cls,
169 struct GNUNET_CADET_Handle *cadet = op_result;
170 struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
172 if (NULL != actx->ports)
174 for (unsigned int i = 0; i < actx->ctx->port_count; i++)
176 GNUNET_CADET_close_port (actx->ports[i]);
177 actx->ports[i] = NULL;
179 GNUNET_free (actx->ports);
182 GNUNET_CADET_disconnect (cadet);
187 * Callback to be called when a service connect operation is completed.
189 * @param cls The callback closure from functions generating an operation.
190 * @param op The operation that has been finished.
191 * @param ca_result The service handle returned from
192 * GNUNET_TESTBED_ConnectAdapter() (cadet handle).
193 * @param emsg Error message in case the operation has failed.
194 * NULL if operation has executed successfully.
197 cadet_connect_cb (void *cls,
198 struct GNUNET_TESTBED_Operation *op,
202 struct GNUNET_CADET_TEST_Context *ctx = cls;
207 "Failed to connect to CADET service: %s\n",
209 GNUNET_SCHEDULER_shutdown ();
212 for (unsigned int i = 0; i < ctx->num_peers; i++)
213 if (op == ctx->ops[i])
215 ctx->cadets[i] = ca_result;
216 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
217 "...cadet %u connected\n",
220 for (unsigned int i = 0; i < ctx->num_peers; i++)
221 if (NULL == ctx->cadets[i])
222 return; /* still some CADET connections missing */
223 /* all CADET connections ready! */
224 ctx->app_main (ctx->app_main_cls,
233 GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx)
235 for (unsigned int i = 0; i < ctx->num_peers; i++)
237 GNUNET_assert (NULL != ctx->ops[i]);
238 GNUNET_TESTBED_operation_done (ctx->ops[i]);
241 GNUNET_free (ctx->ops);
242 GNUNET_free (ctx->cadets);
243 GNUNET_free (ctx->handlers);
245 GNUNET_SCHEDULER_shutdown ();
250 * Callback run when the testbed is ready (peers running and connected to
253 * @param cls Closure (context).
254 * @param h the run handle
255 * @param num_peers Number of peers that are running.
256 * @param peers Handles to each one of the @c num_peers peers.
257 * @param links_succeeded the number of overlay link connection attempts that
259 * @param links_failed the number of overlay link connection attempts that
263 cadet_test_run (void *cls,
264 struct GNUNET_TESTBED_RunHandle *h,
265 unsigned int num_peers,
266 struct GNUNET_TESTBED_Peer **peers,
267 unsigned int links_succeeded,
268 unsigned int links_failed)
270 struct GNUNET_CADET_TEST_Context *ctx = cls;
272 if (0 != links_failed)
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
275 "Some links failed (%u), ending\n",
279 if (num_peers != ctx->num_peers)
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 "Peers started %u/%u, ending\n",
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Testbed up, %u peers and %u links\n",
292 for (unsigned int i = 0; i < num_peers; i++)
294 struct GNUNET_CADET_TEST_AdapterContext *newctx;
296 newctx = GNUNET_new (struct GNUNET_CADET_TEST_AdapterContext);
299 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
300 "Connecting to cadet %u\n",
302 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
307 &cadet_connect_adapter,
308 &cadet_disconnect_adapter,
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
318 * Run a test using the given name, configuration file and number of peers.
319 * All cadet callbacks will receive the peer number (long) as the closure.
321 * @param testname Name of the test (for logging).
322 * @param cfgfile Name of the configuration file.
323 * @param num_peers Number of peers to start.
324 * @param tmain Main function to run once the testbed is ready.
325 * @param tmain_cls Closure for @a tmain.
326 * @param connects Handler for incoming channels.
327 * @param window_changes Handler for the window size change notification.
328 * @param disconnects Cleaner for destroyed incoming channels.
329 * @param handlers Message handlers.
330 * @param ports Ports the peers offer, NULL-terminated.
333 GNUNET_CADET_TEST_ruN (const char *testname,
335 unsigned int num_peers,
336 GNUNET_CADET_TEST_AppMain tmain,
338 GNUNET_CADET_ConnectEventHandler connects,
339 GNUNET_CADET_WindowSizeEventHandler window_changes,
340 GNUNET_CADET_DisconnectEventHandler disconnects,
341 struct GNUNET_MQ_MessageHandler *handlers,
342 const struct GNUNET_HashCode **ports)
344 struct GNUNET_CADET_TEST_Context *ctx;
346 ctx = GNUNET_new (struct GNUNET_CADET_TEST_Context);
347 ctx->num_peers = num_peers;
348 ctx->ops = GNUNET_new_array (num_peers,
349 struct GNUNET_TESTBED_Operation *);
350 ctx->cadets = GNUNET_new_array (num_peers,
351 struct GNUNET_CADET_Handle *);
352 ctx->app_main = tmain;
353 ctx->app_main_cls = tmain_cls;
354 ctx->connects = connects;
355 ctx->window_changes = window_changes;
356 ctx->disconnects = disconnects;
357 ctx->handlers = GNUNET_MQ_copy_handlers (handlers);
360 while (NULL != ctx->ports[ctx->port_count])
362 GNUNET_TESTBED_test_run (testname,
370 /* end of cadet_test_lib.c */