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
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 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;
67 * Adapter function called to establish a connection to
71 * @param cfg configuration of the peer to connect to; will be available until
72 * GNUNET_TESTBED_operation_done() is called on the operation returned
73 * from GNUNET_TESTBED_service_connect()
74 * @return service handle to return in 'op_result', NULL on error
77 dht_connect_adapter (void *cls,
78 const struct GNUNET_CONFIGURATION_Handle *cfg)
80 return GNUNET_DHT_connect (cfg, 16);
85 * Adapter function called to destroy a connection to
89 * @param op_result service handle returned from the connect adapter
92 dht_disconnect_adapter (void *cls,
95 struct GNUNET_DHT_Handle *dht = op_result;
97 GNUNET_DHT_disconnect (dht);
102 * Callback to be called when a service connect operation is completed
104 * @param cls the callback closure from functions generating an operation
105 * @param op the operation that has been finished
106 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
107 * @param emsg error message in case the operation has failed; will be NULL if
108 * operation has executed successfully.
111 dht_connect_cb (void *cls,
112 struct GNUNET_TESTBED_Operation *op,
116 struct GNUNET_DHT_TEST_Context *ctx = cls;
122 "Failed to connect to DHT service: %s\n",
124 GNUNET_SCHEDULER_shutdown ();
127 for (i=0;i<ctx->num_peers;i++)
128 if (op == ctx->ops[i])
129 ctx->dhts[i] = ca_result;
130 for (i=0;i<ctx->num_peers;i++)
131 if (NULL == ctx->dhts[i])
132 return; /* still some DHT connections missing */
133 /* all DHT connections ready! */
134 ctx->app_main (ctx->app_main_cls,
143 * Clean up the testbed.
145 * @param ctx handle for the testbed
148 GNUNET_DHT_TEST_cleanup (struct GNUNET_DHT_TEST_Context *ctx)
152 for (i=0;i<ctx->num_peers;i++)
153 GNUNET_TESTBED_operation_done (ctx->ops[i]);
154 GNUNET_free (ctx->ops);
155 GNUNET_free (ctx->dhts);
157 GNUNET_SCHEDULER_shutdown ();
162 dht_test_run (void *cls,
163 struct GNUNET_TESTBED_RunHandle *h,
164 unsigned int num_peers,
165 struct GNUNET_TESTBED_Peer **peers,
166 unsigned int links_succeeded,
167 unsigned int links_failed)
169 struct GNUNET_DHT_TEST_Context *ctx = cls;
172 GNUNET_assert (num_peers == ctx->num_peers);
174 for (i=0;i<num_peers;i++)
175 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
180 &dht_connect_adapter,
181 &dht_disconnect_adapter,
187 * Run a test using the given name, configuration file and number of
190 * @param testname name of the test (for logging)
191 * @param cfgname name of the configuration file
192 * @param num_peers number of peers to start
193 * @param tmain main function to run once the testbed is ready
194 * @param tmain_cls closure for 'tmain'
197 GNUNET_DHT_TEST_run (const char *testname,
199 unsigned int num_peers,
200 GNUNET_DHT_TEST_AppMain tmain,
203 struct GNUNET_DHT_TEST_Context *ctx;
205 ctx = GNUNET_new (struct GNUNET_DHT_TEST_Context);
206 ctx->num_peers = num_peers;
207 ctx->ops = GNUNET_new_array (num_peers,
208 struct GNUNET_TESTBED_Operation *);
209 ctx->dhts = GNUNET_new_array (num_peers,
210 struct GNUNET_DHT_Handle *);
211 ctx->app_main = tmain;
212 ctx->app_main_cls = tmain_cls;
213 (void) GNUNET_TESTBED_test_run (testname,
220 /* end of dht_test_lib.c */