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.
16 * @file dht/dht_test_lib.c
17 * @author Christian Grothoff
18 * @brief library for writing DHT tests
21 #include "dht_test_lib.h"
24 * Test context for a DHT Test.
26 struct GNUNET_DHT_TEST_Context
29 * Array of running peers.
31 struct GNUNET_TESTBED_Peer **peers;
34 * Array of handles to the DHT for each peer.
36 struct GNUNET_DHT_Handle **dhts;
39 * Operation associated with the connection to the DHT.
41 struct GNUNET_TESTBED_Operation **ops;
44 * Main function of the test to run once all DHTs are available.
46 GNUNET_DHT_TEST_AppMain app_main;
49 * Closure for 'app_main'.
54 * Number of peers running, size of the arrays above.
56 unsigned int num_peers;
62 * Adapter function called to establish a connection to
66 * @param cfg configuration of the peer to connect to; will be available until
67 * GNUNET_TESTBED_operation_done() is called on the operation returned
68 * from GNUNET_TESTBED_service_connect()
69 * @return service handle to return in 'op_result', NULL on error
72 dht_connect_adapter (void *cls,
73 const struct GNUNET_CONFIGURATION_Handle *cfg)
75 return GNUNET_DHT_connect (cfg, 16);
80 * Adapter function called to destroy a connection to
84 * @param op_result service handle returned from the connect adapter
87 dht_disconnect_adapter (void *cls,
90 struct GNUNET_DHT_Handle *dht = op_result;
92 GNUNET_DHT_disconnect (dht);
97 * Callback to be called when a service connect operation is completed
99 * @param cls the callback closure from functions generating an operation
100 * @param op the operation that has been finished
101 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
102 * @param emsg error message in case the operation has failed; will be NULL if
103 * operation has executed successfully.
106 dht_connect_cb (void *cls,
107 struct GNUNET_TESTBED_Operation *op,
111 struct GNUNET_DHT_TEST_Context *ctx = cls;
116 "Failed to connect to DHT service: %s\n",
118 GNUNET_SCHEDULER_shutdown ();
121 for (unsigned int i=0;i<ctx->num_peers;i++)
122 if (op == ctx->ops[i])
123 ctx->dhts[i] = ca_result;
124 for (unsigned int i=0;i<ctx->num_peers;i++)
125 if (NULL == ctx->dhts[i])
126 return; /* still some DHT connections missing */
127 /* all DHT connections ready! */
128 ctx->app_main (ctx->app_main_cls,
137 * Clean up the testbed.
139 * @param ctx handle for the testbed
142 GNUNET_DHT_TEST_cleanup (struct GNUNET_DHT_TEST_Context *ctx)
144 for (unsigned int i=0;i<ctx->num_peers;i++)
145 GNUNET_TESTBED_operation_done (ctx->ops[i]);
146 GNUNET_free (ctx->ops);
147 GNUNET_free (ctx->dhts);
149 GNUNET_SCHEDULER_shutdown ();
154 dht_test_run (void *cls,
155 struct GNUNET_TESTBED_RunHandle *h,
156 unsigned int num_peers,
157 struct GNUNET_TESTBED_Peer **peers,
158 unsigned int links_succeeded,
159 unsigned int links_failed)
161 struct GNUNET_DHT_TEST_Context *ctx = cls;
163 GNUNET_assert (num_peers == ctx->num_peers);
165 for (unsigned int i=0;i<num_peers;i++)
166 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
171 &dht_connect_adapter,
172 &dht_disconnect_adapter,
178 * Run a test using the given name, configuration file and number of
181 * @param testname name of the test (for logging)
182 * @param cfgname name of the configuration file
183 * @param num_peers number of peers to start
184 * @param tmain main function to run once the testbed is ready
185 * @param tmain_cls closure for 'tmain'
188 GNUNET_DHT_TEST_run (const char *testname,
190 unsigned int num_peers,
191 GNUNET_DHT_TEST_AppMain tmain,
194 struct GNUNET_DHT_TEST_Context *ctx;
196 ctx = GNUNET_new (struct GNUNET_DHT_TEST_Context);
197 ctx->num_peers = num_peers;
198 ctx->ops = GNUNET_new_array (num_peers,
199 struct GNUNET_TESTBED_Operation *);
200 ctx->dhts = GNUNET_new_array (num_peers,
201 struct GNUNET_DHT_Handle *);
202 ctx->app_main = tmain;
203 ctx->app_main_cls = tmain_cls;
204 (void) GNUNET_TESTBED_test_run (testname,
211 /* end of dht_test_lib.c */