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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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;
99 * Context for a cadet adapter callback.
101 struct GNUNET_CADET_TEST_AdapterContext
104 * Peer number for the particular peer.
109 * Port handlers for open ports.
111 struct GNUNET_CADET_Port **ports;
116 struct GNUNET_CADET_TEST_Context *ctx;
121 * Adapter function called to establish a connection to
125 * @param cfg configuration of the peer to connect to; will be available until
126 * GNUNET_TESTBED_operation_done() is called on the operation returned
127 * from GNUNET_TESTBED_service_connect()
128 * @return service handle to return in 'op_result', NULL on error
131 cadet_connect_adapter (void *cls,
132 const struct GNUNET_CONFIGURATION_Handle *cfg)
134 struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
135 struct GNUNET_CADET_TEST_Context *ctx = actx->ctx;
136 struct GNUNET_CADET_Handle *h;
138 h = GNUNET_CADET_connect (cfg);
144 if (NULL == ctx->ports)
146 actx->ports = GNUNET_new_array (ctx->port_count,
147 struct GNUNET_CADET_Port *);
148 for (unsigned int i = 0; i < ctx->port_count; i++)
150 actx->ports[i] = GNUNET_CADET_open_port (h,
153 (void *) (long) actx->peer,
163 * Adapter function called to destroy a connection to
167 * @param op_result service handle returned from the connect adapter
170 cadet_disconnect_adapter (void *cls,
173 struct GNUNET_CADET_Handle *cadet = op_result;
174 struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
176 if (NULL != actx->ports)
178 for (unsigned int i = 0; i < actx->ctx->port_count; i++)
180 GNUNET_CADET_close_port (actx->ports[i]);
181 actx->ports[i] = NULL;
183 GNUNET_free (actx->ports);
186 GNUNET_CADET_disconnect (cadet);
191 * Callback to be called when a service connect operation is completed.
193 * @param cls The callback closure from functions generating an operation.
194 * @param op The operation that has been finished.
195 * @param ca_result The service handle returned from
196 * GNUNET_TESTBED_ConnectAdapter() (cadet handle).
197 * @param emsg Error message in case the operation has failed.
198 * NULL if operation has executed successfully.
201 cadet_connect_cb (void *cls,
202 struct GNUNET_TESTBED_Operation *op,
206 struct GNUNET_CADET_TEST_Context *ctx = cls;
211 "Failed to connect to CADET service: %s\n",
213 GNUNET_SCHEDULER_shutdown ();
216 for (unsigned int i = 0; i < ctx->num_peers; i++)
217 if (op == ctx->ops[i])
219 ctx->cadets[i] = ca_result;
220 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
221 "...cadet %u connected\n",
224 for (unsigned int i = 0; i < ctx->num_peers; i++)
225 if (NULL == ctx->cadets[i])
227 /* still some CADET connections missing */
228 /* all CADET connections ready! */
229 ctx->app_main (ctx->app_main_cls,
238 GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx)
240 for (unsigned int i = 0; i < ctx->num_peers; i++)
242 GNUNET_assert (NULL != ctx->ops[i]);
243 GNUNET_TESTBED_operation_done (ctx->ops[i]);
246 GNUNET_free (ctx->ops);
247 GNUNET_free (ctx->cadets);
248 GNUNET_free (ctx->handlers);
250 GNUNET_SCHEDULER_shutdown ();
255 * Callback run when the testbed is ready (peers running and connected to
258 * @param cls Closure (context).
259 * @param h the run handle
260 * @param num_peers Number of peers that are running.
261 * @param peers Handles to each one of the @c num_peers peers.
262 * @param links_succeeded the number of overlay link connection attempts that
264 * @param links_failed the number of overlay link connection attempts that
268 cadet_test_run (void *cls,
269 struct GNUNET_TESTBED_RunHandle *h,
270 unsigned int num_peers,
271 struct GNUNET_TESTBED_Peer **peers,
272 unsigned int links_succeeded,
273 unsigned int links_failed)
275 struct GNUNET_CADET_TEST_Context *ctx = cls;
277 if (0 != links_failed)
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280 "Some links failed (%u), ending\n",
284 if (num_peers != ctx->num_peers)
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
287 "Peers started %u/%u, ending\n",
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Testbed up, %u peers and %u links\n",
297 for (unsigned int i = 0; i < num_peers; i++)
299 struct GNUNET_CADET_TEST_AdapterContext *newctx;
301 newctx = GNUNET_new (struct GNUNET_CADET_TEST_AdapterContext);
304 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
305 "Connecting to cadet %u\n",
307 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
312 &cadet_connect_adapter,
313 &cadet_disconnect_adapter,
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
323 * Run a test using the given name, configuration file and number of peers.
324 * All cadet callbacks will receive the peer number (long) as the closure.
326 * @param testname Name of the test (for logging).
327 * @param cfgfile Name of the configuration file.
328 * @param num_peers Number of peers to start.
329 * @param tmain Main function to run once the testbed is ready.
330 * @param tmain_cls Closure for @a tmain.
331 * @param connects Handler for incoming channels.
332 * @param window_changes Handler for the window size change notification.
333 * @param disconnects Cleaner for destroyed incoming channels.
334 * @param handlers Message handlers.
335 * @param ports Ports the peers offer, NULL-terminated.
338 GNUNET_CADET_TEST_ruN (const char *testname,
340 unsigned int num_peers,
341 GNUNET_CADET_TEST_AppMain tmain,
343 GNUNET_CADET_ConnectEventHandler connects,
344 GNUNET_CADET_WindowSizeEventHandler window_changes,
345 GNUNET_CADET_DisconnectEventHandler disconnects,
346 struct GNUNET_MQ_MessageHandler *handlers,
347 const struct GNUNET_HashCode **ports)
349 struct GNUNET_CADET_TEST_Context *ctx;
351 ctx = GNUNET_new (struct GNUNET_CADET_TEST_Context);
352 ctx->num_peers = num_peers;
353 ctx->ops = GNUNET_new_array (num_peers,
354 struct GNUNET_TESTBED_Operation *);
355 ctx->cadets = GNUNET_new_array (num_peers,
356 struct GNUNET_CADET_Handle *);
357 ctx->app_main = tmain;
358 ctx->app_main_cls = tmain_cls;
359 ctx->connects = connects;
360 ctx->window_changes = window_changes;
361 ctx->disconnects = disconnects;
362 ctx->handlers = GNUNET_MQ_copy_handlers (handlers);
365 while (NULL != ctx->ports[ctx->port_count])
367 GNUNET_TESTBED_test_run (testname,
376 /* end of cadet_test_lib.c */