aboutsummaryrefslogtreecommitdiff
path: root/src/service/dht/dht_test_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/dht/dht_test_lib.c')
-rw-r--r--src/service/dht/dht_test_lib.c202
1 files changed, 202 insertions, 0 deletions
diff --git a/src/service/dht/dht_test_lib.c b/src/service/dht/dht_test_lib.c
new file mode 100644
index 000000000..29c5088d1
--- /dev/null
+++ b/src/service/dht/dht_test_lib.c
@@ -0,0 +1,202 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file dht/dht_test_lib.c
22 * @author Christian Grothoff
23 * @brief library for writing DHT tests
24 */
25#include "platform.h"
26#include "dht_test_lib.h"
27
28/**
29 * Test context for a DHT Test.
30 */
31struct GNUNET_DHT_TEST_Context
32{
33 /**
34 * Array of running peers.
35 */
36 struct GNUNET_TESTBED_Peer **peers;
37
38 /**
39 * Array of handles to the DHT for each peer.
40 */
41 struct GNUNET_DHT_Handle **dhts;
42
43 /**
44 * Operation associated with the connection to the DHT.
45 */
46 struct GNUNET_TESTBED_Operation **ops;
47
48 /**
49 * Main function of the test to run once all DHTs are available.
50 */
51 GNUNET_DHT_TEST_AppMain app_main;
52
53 /**
54 * Closure for 'app_main'.
55 */
56 void *app_main_cls;
57
58 /**
59 * Number of peers running, size of the arrays above.
60 */
61 unsigned int num_peers;
62};
63
64
65/**
66 * Adapter function called to establish a connection to
67 * the DHT service.
68 *
69 * @param cls closure
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
74 */
75static void *
76dht_connect_adapter (void *cls,
77 const struct GNUNET_CONFIGURATION_Handle *cfg)
78{
79 return GNUNET_DHT_connect (cfg, 16);
80}
81
82
83/**
84 * Adapter function called to destroy a connection to
85 * the DHT service.
86 *
87 * @param cls closure
88 * @param op_result service handle returned from the connect adapter
89 */
90static void
91dht_disconnect_adapter (void *cls,
92 void *op_result)
93{
94 struct GNUNET_DHT_Handle *dht = op_result;
95
96 GNUNET_DHT_disconnect (dht);
97}
98
99
100/**
101 * Callback to be called when a service connect operation is completed
102 *
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.
108 */
109static void
110dht_connect_cb (void *cls,
111 struct GNUNET_TESTBED_Operation *op,
112 void *ca_result,
113 const char *emsg)
114{
115 struct GNUNET_DHT_TEST_Context *ctx = cls;
116
117 if (NULL != emsg)
118 {
119 fprintf (stderr,
120 "Failed to connect to DHT service: %s\n",
121 emsg);
122 GNUNET_SCHEDULER_shutdown ();
123 return;
124 }
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])
130 return;
131 /* still some DHT connections missing */
132 /* all DHT connections ready! */
133 ctx->app_main (ctx->app_main_cls,
134 ctx,
135 ctx->num_peers,
136 ctx->peers,
137 ctx->dhts);
138}
139
140
141void
142GNUNET_DHT_TEST_cleanup (struct GNUNET_DHT_TEST_Context *ctx)
143{
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);
148 GNUNET_free (ctx);
149 GNUNET_SCHEDULER_shutdown ();
150}
151
152
153static void
154dht_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)
160{
161 struct GNUNET_DHT_TEST_Context *ctx = cls;
162
163 GNUNET_assert (num_peers == ctx->num_peers);
164 ctx->peers = peers;
165 for (unsigned int i = 0; i < num_peers; i++)
166 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx,
167 peers[i],
168 "dht",
169 &dht_connect_cb,
170 ctx,
171 &dht_connect_adapter,
172 &dht_disconnect_adapter,
173 ctx);
174}
175
176
177void
178GNUNET_DHT_TEST_run (const char *testname,
179 const char *cfgname,
180 unsigned int num_peers,
181 GNUNET_DHT_TEST_AppMain tmain,
182 void *tmain_cls)
183{
184 struct GNUNET_DHT_TEST_Context *ctx;
185
186 ctx = GNUNET_new (struct GNUNET_DHT_TEST_Context);
187 ctx->num_peers = num_peers;
188 ctx->ops = GNUNET_new_array (num_peers,
189 struct GNUNET_TESTBED_Operation *);
190 ctx->dhts = GNUNET_new_array (num_peers,
191 struct GNUNET_DHT_Handle *);
192 ctx->app_main = tmain;
193 ctx->app_main_cls = tmain_cls;
194 (void) GNUNET_TESTBED_test_run (testname,
195 cfgname,
196 num_peers,
197 0LL, NULL, NULL,
198 &dht_test_run, ctx);
199}
200
201
202/* end of dht_test_lib.c */