diff options
Diffstat (limited to 'src/cadet/cadet_test_lib.c')
-rw-r--r-- | src/cadet/cadet_test_lib.c | 376 |
1 files changed, 0 insertions, 376 deletions
diff --git a/src/cadet/cadet_test_lib.c b/src/cadet/cadet_test_lib.c deleted file mode 100644 index 9c7b9063d..000000000 --- a/src/cadet/cadet_test_lib.c +++ /dev/null | |||
@@ -1,376 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012, 2017 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 cadet/cadet_test_lib.c | ||
22 | * @author Bartlomiej Polot | ||
23 | * @brief library for writing CADET tests | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #include "cadet_test_lib.h" | ||
28 | #include "gnunet_cadet_service.h" | ||
29 | |||
30 | |||
31 | /** | ||
32 | * Test context for a CADET Test. | ||
33 | */ | ||
34 | struct GNUNET_CADET_TEST_Context | ||
35 | { | ||
36 | /** | ||
37 | * Array of running peers. | ||
38 | */ | ||
39 | struct GNUNET_TESTBED_Peer **peers; | ||
40 | |||
41 | /** | ||
42 | * Array of handles to the CADET for each peer. | ||
43 | */ | ||
44 | struct GNUNET_CADET_Handle **cadets; | ||
45 | |||
46 | /** | ||
47 | * Operation associated with the connection to the CADET. | ||
48 | */ | ||
49 | struct GNUNET_TESTBED_Operation **ops; | ||
50 | |||
51 | /** | ||
52 | * Number of peers running, size of the arrays above. | ||
53 | */ | ||
54 | unsigned int num_peers; | ||
55 | |||
56 | /** | ||
57 | * Main function of the test to run once all CADETs are available. | ||
58 | */ | ||
59 | GNUNET_CADET_TEST_AppMain app_main; | ||
60 | |||
61 | /** | ||
62 | * Closure for 'app_main'. | ||
63 | */ | ||
64 | void *app_main_cls; | ||
65 | |||
66 | /** | ||
67 | * Handler for incoming tunnels. | ||
68 | */ | ||
69 | GNUNET_CADET_ConnectEventHandler connects; | ||
70 | |||
71 | /** | ||
72 | * Function called when the transmit window size changes. | ||
73 | */ | ||
74 | GNUNET_CADET_WindowSizeEventHandler window_changes; | ||
75 | |||
76 | /** | ||
77 | * Cleaner for destroyed incoming tunnels. | ||
78 | */ | ||
79 | GNUNET_CADET_DisconnectEventHandler disconnects; | ||
80 | |||
81 | /** | ||
82 | * Message handlers. | ||
83 | */ | ||
84 | struct GNUNET_MQ_MessageHandler *handlers; | ||
85 | |||
86 | /** | ||
87 | * Application ports. | ||
88 | */ | ||
89 | const struct GNUNET_HashCode **ports; | ||
90 | |||
91 | /** | ||
92 | * Number of ports in #ports. | ||
93 | */ | ||
94 | unsigned int port_count; | ||
95 | }; | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Context for a cadet adapter callback. | ||
100 | */ | ||
101 | struct GNUNET_CADET_TEST_AdapterContext | ||
102 | { | ||
103 | /** | ||
104 | * Peer number for the particular peer. | ||
105 | */ | ||
106 | unsigned int peer; | ||
107 | |||
108 | /** | ||
109 | * Port handlers for open ports. | ||
110 | */ | ||
111 | struct GNUNET_CADET_Port **ports; | ||
112 | |||
113 | /** | ||
114 | * General context. | ||
115 | */ | ||
116 | struct GNUNET_CADET_TEST_Context *ctx; | ||
117 | }; | ||
118 | |||
119 | |||
120 | /** | ||
121 | * Adapter function called to establish a connection to | ||
122 | * the CADET service. | ||
123 | * | ||
124 | * @param cls closure | ||
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 | ||
129 | */ | ||
130 | static void * | ||
131 | cadet_connect_adapter (void *cls, | ||
132 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
133 | { | ||
134 | struct GNUNET_CADET_TEST_AdapterContext *actx = cls; | ||
135 | struct GNUNET_CADET_TEST_Context *ctx = actx->ctx; | ||
136 | struct GNUNET_CADET_Handle *h; | ||
137 | |||
138 | h = GNUNET_CADET_connect (cfg); | ||
139 | if (NULL == h) | ||
140 | { | ||
141 | GNUNET_break (0); | ||
142 | return NULL; | ||
143 | } | ||
144 | if (NULL == ctx->ports) | ||
145 | return h; | ||
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++) | ||
149 | { | ||
150 | actx->ports[i] = GNUNET_CADET_open_port (h, | ||
151 | ctx->ports[i], | ||
152 | ctx->connects, | ||
153 | (void *) (long) actx->peer, | ||
154 | ctx->window_changes, | ||
155 | ctx->disconnects, | ||
156 | ctx->handlers); | ||
157 | } | ||
158 | return h; | ||
159 | } | ||
160 | |||
161 | |||
162 | /** | ||
163 | * Adapter function called to destroy a connection to | ||
164 | * the CADET service. | ||
165 | * | ||
166 | * @param cls closure | ||
167 | * @param op_result service handle returned from the connect adapter | ||
168 | */ | ||
169 | static void | ||
170 | cadet_disconnect_adapter (void *cls, | ||
171 | void *op_result) | ||
172 | { | ||
173 | struct GNUNET_CADET_Handle *cadet = op_result; | ||
174 | struct GNUNET_CADET_TEST_AdapterContext *actx = cls; | ||
175 | |||
176 | if (NULL != actx->ports) | ||
177 | { | ||
178 | for (unsigned int i = 0; i < actx->ctx->port_count; i++) | ||
179 | { | ||
180 | GNUNET_CADET_close_port (actx->ports[i]); | ||
181 | actx->ports[i] = NULL; | ||
182 | } | ||
183 | GNUNET_free (actx->ports); | ||
184 | } | ||
185 | GNUNET_free (actx); | ||
186 | GNUNET_CADET_disconnect (cadet); | ||
187 | } | ||
188 | |||
189 | |||
190 | /** | ||
191 | * Callback to be called when a service connect operation is completed. | ||
192 | * | ||
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. | ||
199 | */ | ||
200 | static void | ||
201 | cadet_connect_cb (void *cls, | ||
202 | struct GNUNET_TESTBED_Operation *op, | ||
203 | void *ca_result, | ||
204 | const char *emsg) | ||
205 | { | ||
206 | struct GNUNET_CADET_TEST_Context *ctx = cls; | ||
207 | |||
208 | if (NULL != emsg) | ||
209 | { | ||
210 | fprintf (stderr, | ||
211 | "Failed to connect to CADET service: %s\n", | ||
212 | emsg); | ||
213 | GNUNET_SCHEDULER_shutdown (); | ||
214 | return; | ||
215 | } | ||
216 | for (unsigned int i = 0; i < ctx->num_peers; i++) | ||
217 | if (op == ctx->ops[i]) | ||
218 | { | ||
219 | ctx->cadets[i] = ca_result; | ||
220 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
221 | "...cadet %u connected\n", | ||
222 | i); | ||
223 | } | ||
224 | for (unsigned int i = 0; i < ctx->num_peers; i++) | ||
225 | if (NULL == ctx->cadets[i]) | ||
226 | return; | ||
227 | /* still some CADET connections missing */ | ||
228 | /* all CADET connections ready! */ | ||
229 | ctx->app_main (ctx->app_main_cls, | ||
230 | ctx, | ||
231 | ctx->num_peers, | ||
232 | ctx->peers, | ||
233 | ctx->cadets); | ||
234 | } | ||
235 | |||
236 | |||
237 | void | ||
238 | GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx) | ||
239 | { | ||
240 | for (unsigned int i = 0; i < ctx->num_peers; i++) | ||
241 | { | ||
242 | GNUNET_assert (NULL != ctx->ops[i]); | ||
243 | GNUNET_TESTBED_operation_done (ctx->ops[i]); | ||
244 | ctx->ops[i] = NULL; | ||
245 | } | ||
246 | GNUNET_free (ctx->ops); | ||
247 | GNUNET_free (ctx->cadets); | ||
248 | GNUNET_free (ctx->handlers); | ||
249 | GNUNET_free (ctx); | ||
250 | GNUNET_SCHEDULER_shutdown (); | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Callback run when the testbed is ready (peers running and connected to | ||
256 | * each other) | ||
257 | * | ||
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 | ||
263 | * succeeded | ||
264 | * @param links_failed the number of overlay link connection attempts that | ||
265 | * failed | ||
266 | */ | ||
267 | static void | ||
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) | ||
274 | { | ||
275 | struct GNUNET_CADET_TEST_Context *ctx = cls; | ||
276 | |||
277 | if (0 != links_failed) | ||
278 | { | ||
279 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
280 | "Some links failed (%u), ending\n", | ||
281 | links_failed); | ||
282 | exit (77); | ||
283 | } | ||
284 | if (num_peers != ctx->num_peers) | ||
285 | { | ||
286 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
287 | "Peers started %u/%u, ending\n", | ||
288 | num_peers, | ||
289 | ctx->num_peers); | ||
290 | exit (1); | ||
291 | } | ||
292 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
293 | "Testbed up, %u peers and %u links\n", | ||
294 | num_peers, | ||
295 | links_succeeded); | ||
296 | ctx->peers = peers; | ||
297 | for (unsigned int i = 0; i < num_peers; i++) | ||
298 | { | ||
299 | struct GNUNET_CADET_TEST_AdapterContext *newctx; | ||
300 | |||
301 | newctx = GNUNET_new (struct GNUNET_CADET_TEST_AdapterContext); | ||
302 | newctx->peer = i; | ||
303 | newctx->ctx = ctx; | ||
304 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
305 | "Connecting to cadet %u\n", | ||
306 | i); | ||
307 | ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx, | ||
308 | peers[i], | ||
309 | "cadet", | ||
310 | &cadet_connect_cb, | ||
311 | ctx, | ||
312 | &cadet_connect_adapter, | ||
313 | &cadet_disconnect_adapter, | ||
314 | newctx); | ||
315 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
316 | "op handle %p\n", | ||
317 | ctx->ops[i]); | ||
318 | } | ||
319 | } | ||
320 | |||
321 | |||
322 | /** | ||
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. | ||
325 | * | ||
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. | ||
336 | */ | ||
337 | void | ||
338 | GNUNET_CADET_TEST_ruN (const char *testname, | ||
339 | const char *cfgfile, | ||
340 | unsigned int num_peers, | ||
341 | GNUNET_CADET_TEST_AppMain tmain, | ||
342 | void *tmain_cls, | ||
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) | ||
348 | { | ||
349 | struct GNUNET_CADET_TEST_Context *ctx; | ||
350 | |||
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); | ||
363 | ctx->ports = ports; | ||
364 | ctx->port_count = 0; | ||
365 | while (NULL != ctx->ports[ctx->port_count]) | ||
366 | ctx->port_count++; | ||
367 | GNUNET_TESTBED_test_run (testname, | ||
368 | cfgfile, | ||
369 | num_peers, | ||
370 | 0LL, NULL, NULL, | ||
371 | &cadet_test_run, | ||
372 | ctx); | ||
373 | } | ||
374 | |||
375 | |||
376 | /* end of cadet_test_lib.c */ | ||