2 This file is part of GNUnet.
3 Copyright (C) 2012 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 dht/dht_test_lib.c
22 * @author Christian Grothoff
23 * @brief library for writing DHT tests
26 #include "dht_test_lib.h"
29 * Test context for a DHT Test.
31 struct GNUNET_DHT_TEST_Context
34 * Array of running peers.
36 struct GNUNET_TESTBED_Peer **peers;
39 * Array of handles to the DHT for each peer.
41 struct GNUNET_DHT_Handle **dhts;
44 * Operation associated with the connection to the DHT.
46 struct GNUNET_TESTBED_Operation **ops;
49 * Main function of the test to run once all DHTs are available.
51 GNUNET_DHT_TEST_AppMain app_main;
54 * Closure for 'app_main'.
59 * Number of peers running, size of the arrays above.
61 unsigned int num_peers;
66 * Adapter function called to establish a connection to
70 * @param cfg configuration of the peer to connect to; will be available until
71 * GNUNET_TESTBED_operation_done() is called on the operation returned
72 * from GNUNET_TESTBED_service_connect()
73 * @return service handle to return in 'op_result', NULL on error
76 dht_connect_adapter (void *cls,
77 const struct GNUNET_CONFIGURATION_Handle *cfg)
79 return GNUNET_DHT_connect (cfg, 16);
84 * Adapter function called to destroy a connection to
88 * @param op_result service handle returned from the connect adapter
91 dht_disconnect_adapter (void *cls,
94 struct GNUNET_DHT_Handle *dht = op_result;
96 GNUNET_DHT_disconnect (dht);
101 * Callback to be called when a service connect operation is completed
103 * @param cls the callback closure from functions generating an operation
104 * @param op the operation that has been finished
105 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
106 * @param emsg error message in case the operation has failed; will be NULL if
107 * operation has executed successfully.
110 dht_connect_cb (void *cls,
111 struct GNUNET_TESTBED_Operation *op,
115 struct GNUNET_DHT_TEST_Context *ctx = cls;
120 "Failed to connect to DHT service: %s\n",
122 GNUNET_SCHEDULER_shutdown ();
125 for (unsigned int i = 0; i < ctx->num_peers; i++)
126 if (op == ctx->ops[i])
127 ctx->dhts[i] = ca_result;
128 for (unsigned int i = 0; i < ctx->num_peers; i++)
129 if (NULL == ctx->dhts[i])
131 /* still some DHT connections missing */
132 /* all DHT connections ready! */
133 ctx->app_main (ctx->app_main_cls,
142 * Clean up the testbed.
144 * @param ctx handle for the testbed
147 GNUNET_DHT_TEST_cleanup (struct GNUNET_DHT_TEST_Context *ctx)
149 for (unsigned int i = 0; i < ctx->num_peers; i++)
150 GNUNET_TESTBED_operation_done (ctx->ops[i]);
151 GNUNET_free (ctx->ops);
152 GNUNET_free (ctx->dhts);
154 GNUNET_SCHEDULER_shutdown ();
159 dht_test_run (void *cls,
160 struct GNUNET_TESTBED_RunHandle *h,
161 unsigned int num_peers,
162 struct GNUNET_TESTBED_Peer **peers,
163 unsigned int links_succeeded,
164 unsigned int links_failed)
166 struct GNUNET_DHT_TEST_Context *ctx = cls;
168 GNUNET_assert (num_peers == ctx->num_peers);
170 for (unsigned int i = 0; i < num_peers; i++)
171 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
176 &dht_connect_adapter,
177 &dht_disconnect_adapter,
183 * Run a test using the given name, configuration file and number of
186 * @param testname name of the test (for logging)
187 * @param cfgname name of the configuration file
188 * @param num_peers number of peers to start
189 * @param tmain main function to run once the testbed is ready
190 * @param tmain_cls closure for 'tmain'
193 GNUNET_DHT_TEST_run (const char *testname,
195 unsigned int num_peers,
196 GNUNET_DHT_TEST_AppMain tmain,
199 struct GNUNET_DHT_TEST_Context *ctx;
201 ctx = GNUNET_new (struct GNUNET_DHT_TEST_Context);
202 ctx->num_peers = num_peers;
203 ctx->ops = GNUNET_new_array (num_peers,
204 struct GNUNET_TESTBED_Operation *);
205 ctx->dhts = GNUNET_new_array (num_peers,
206 struct GNUNET_DHT_Handle *);
207 ctx->app_main = tmain;
208 ctx->app_main_cls = tmain_cls;
209 (void) GNUNET_TESTBED_test_run (testname,
217 /* end of dht_test_lib.c */