diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/transport/Makefile.am | 5 | ||||
-rw-r--r-- | src/transport/transport-testing2.c | 923 | ||||
-rw-r--r-- | src/transport/transport-testing2.h | 924 |
3 files changed, 1852 insertions, 0 deletions
diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index 477973af9..605d89cc0 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am | |||
@@ -168,10 +168,15 @@ libgnunettransporttesting_la_LDFLAGS = \ | |||
168 | $(GN_LIB_LDFLAGS) | 168 | $(GN_LIB_LDFLAGS) |
169 | 169 | ||
170 | libgnunettransporttesting2_la_SOURCES = \ | 170 | libgnunettransporttesting2_la_SOURCES = \ |
171 | transport-testing2.c transport-testing2.h \ | ||
171 | transport-testing-communicator.c transport-testing-communicator.h | 172 | transport-testing-communicator.c transport-testing-communicator.h |
172 | libgnunettransporttesting2_la_LIBADD = \ | 173 | libgnunettransporttesting2_la_LIBADD = \ |
173 | libgnunettransport.la \ | 174 | libgnunettransport.la \ |
175 | libgnunettransportapplication.la \ | ||
176 | libgnunettransportcore.la \ | ||
177 | $(top_builddir)/src/testing/libgnunettesting.la \ | ||
174 | $(top_builddir)/src/hello/libgnunethello.la \ | 178 | $(top_builddir)/src/hello/libgnunethello.la \ |
179 | $(top_builddir)/src/peerstore/libgnunetpeerstore.la \ | ||
175 | $(top_builddir)/src/util/libgnunetutil.la | 180 | $(top_builddir)/src/util/libgnunetutil.la |
176 | libgnunettransporttesting2_la_LDFLAGS = \ | 181 | libgnunettransporttesting2_la_LDFLAGS = \ |
177 | $(GN_LIBINTL) \ | 182 | $(GN_LIBINTL) \ |
diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c new file mode 100644 index 000000000..ca338c52f --- /dev/null +++ b/src/transport/transport-testing2.c | |||
@@ -0,0 +1,923 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2006, 2009, 2015, 2016 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 transport-testing.c | ||
22 | * @brief testing lib for transport service | ||
23 | * @author Matthias Wachs | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "transport-testing2.h" | ||
27 | |||
28 | |||
29 | #define LOG(kind, ...) GNUNET_log_from (kind, "transport-testing", __VA_ARGS__) | ||
30 | |||
31 | |||
32 | static struct GNUNET_TRANSPORT_TESTING_PeerContext * | ||
33 | find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth, | ||
34 | const struct GNUNET_PeerIdentity *peer) | ||
35 | { | ||
36 | struct GNUNET_TRANSPORT_TESTING_PeerContext *t; | ||
37 | |||
38 | for (t = tth->p_head; NULL != t; t = t->next) | ||
39 | if (0 == memcmp (&t->id, | ||
40 | peer, | ||
41 | sizeof(struct GNUNET_PeerIdentity))) | ||
42 | return t; | ||
43 | return NULL; | ||
44 | } | ||
45 | |||
46 | |||
47 | /** | ||
48 | * Find any connecting context matching the given pair of peers. | ||
49 | * | ||
50 | * @param p1 first peer | ||
51 | * @param p2 second peer | ||
52 | * @param cb function to call | ||
53 | * @param cb_cls closure for @a cb | ||
54 | */ | ||
55 | void | ||
56 | GNUNET_TRANSPORT_TESTING_find_connecting_context (struct | ||
57 | GNUNET_TRANSPORT_TESTING_PeerContext | ||
58 | *p1, | ||
59 | struct | ||
60 | GNUNET_TRANSPORT_TESTING_PeerContext | ||
61 | *p2, | ||
62 | GNUNET_TRANSPORT_TESTING_ConnectContextCallback | ||
63 | cb, | ||
64 | void *cb_cls) | ||
65 | { | ||
66 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth; | ||
67 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
68 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
69 | |||
70 | for (cc = tth->cc_head; NULL != cc; cc = ccn) | ||
71 | { | ||
72 | ccn = cc->next; | ||
73 | if ((cc->p1 == p1) && | ||
74 | (cc->p2 == p2)) | ||
75 | cb (cb_cls, | ||
76 | cc); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | |||
81 | static void | ||
82 | set_p1c (void *cls, | ||
83 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
84 | { | ||
85 | int *found = cls; | ||
86 | |||
87 | if (NULL != found) | ||
88 | *found = GNUNET_YES; | ||
89 | cx->p1_c = GNUNET_YES; | ||
90 | } | ||
91 | |||
92 | |||
93 | static void | ||
94 | set_mq (void *cls, | ||
95 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
96 | { | ||
97 | struct GNUNET_MQ_Handle *mq = cls; | ||
98 | |||
99 | cx->mq = mq; | ||
100 | } | ||
101 | |||
102 | |||
103 | static void | ||
104 | set_p2c (void *cls, | ||
105 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
106 | { | ||
107 | int *found = cls; | ||
108 | |||
109 | if (NULL != found) | ||
110 | *found = GNUNET_YES; | ||
111 | cx->p2_c = GNUNET_YES; | ||
112 | } | ||
113 | |||
114 | |||
115 | static void | ||
116 | clear_p1c (void *cls, | ||
117 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
118 | { | ||
119 | int *found = cls; | ||
120 | |||
121 | if (NULL != found) | ||
122 | *found = GNUNET_YES; | ||
123 | cx->p1_c = GNUNET_NO; | ||
124 | } | ||
125 | |||
126 | |||
127 | static void | ||
128 | clear_p2c (void *cls, | ||
129 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx) | ||
130 | { | ||
131 | int *found = cls; | ||
132 | |||
133 | if (NULL != found) | ||
134 | *found = GNUNET_YES; | ||
135 | cx->p2_c = GNUNET_NO; | ||
136 | } | ||
137 | |||
138 | |||
139 | static void * | ||
140 | notify_connect (void *cls, | ||
141 | const struct GNUNET_PeerIdentity *peer, | ||
142 | struct GNUNET_MQ_Handle *mq) | ||
143 | { | ||
144 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
145 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
146 | char *p2_s; | ||
147 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; | ||
148 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
149 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
150 | int found; | ||
151 | void *ret; | ||
152 | |||
153 | p2 = find_peer_context (p->tth, | ||
154 | peer); | ||
155 | if (NULL != p->nc) | ||
156 | ret = p->nc (p->cb_cls, | ||
157 | peer, | ||
158 | mq); | ||
159 | else | ||
160 | ret = NULL; | ||
161 | |||
162 | if (NULL != p2) | ||
163 | GNUNET_asprintf (&p2_s, | ||
164 | "%u (`%s')", | ||
165 | p2->no, | ||
166 | GNUNET_i2s (&p2->id)); | ||
167 | else | ||
168 | GNUNET_asprintf (&p2_s, | ||
169 | "`%s'", | ||
170 | GNUNET_i2s (peer)); | ||
171 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
172 | "Peers %s connected to peer %u (`%s')\n", | ||
173 | p2_s, | ||
174 | p->no, | ||
175 | GNUNET_i2s (&p->id)); | ||
176 | GNUNET_free (p2_s); | ||
177 | /* update flags in connecting contexts */ | ||
178 | found = GNUNET_NO; | ||
179 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
180 | p2, | ||
181 | &set_p1c, | ||
182 | &found); | ||
183 | if (GNUNET_NO == found) | ||
184 | { | ||
185 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); | ||
186 | cc->p1 = p; | ||
187 | cc->p2 = p2; | ||
188 | cc->p1_c = GNUNET_YES; | ||
189 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, | ||
190 | tth->cc_tail, | ||
191 | cc); | ||
192 | } | ||
193 | found = GNUNET_NO; | ||
194 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p2, | ||
195 | p, | ||
196 | &set_p2c, | ||
197 | &found); | ||
198 | if (GNUNET_NO == found) | ||
199 | { | ||
200 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); | ||
201 | cc->p1 = p2; | ||
202 | cc->p2 = p; | ||
203 | cc->p1_c = GNUNET_YES; | ||
204 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, | ||
205 | tth->cc_tail, | ||
206 | cc); | ||
207 | } | ||
208 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
209 | p2, | ||
210 | &set_mq, | ||
211 | mq); | ||
212 | /* update set connected flag for all requests */ | ||
213 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
214 | { | ||
215 | if (GNUNET_YES == cc->connected) | ||
216 | continue; | ||
217 | if ((GNUNET_YES == cc->p1_c) && | ||
218 | (GNUNET_YES == cc->p2_c)) | ||
219 | { | ||
220 | cc->connected = GNUNET_YES; | ||
221 | /* stop trying to connect */ | ||
222 | if (NULL != cc->tct) | ||
223 | { | ||
224 | GNUNET_SCHEDULER_cancel (cc->tct); | ||
225 | cc->tct = NULL; | ||
226 | } | ||
227 | if (NULL != cc->ats_sh) | ||
228 | { | ||
229 | GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh); | ||
230 | cc->ats_sh = NULL; | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | /* then notify application */ | ||
235 | for (cc = tth->cc_head; NULL != cc; cc = ccn) | ||
236 | { | ||
237 | ccn = cc->next; | ||
238 | if ((GNUNET_YES == cc->connected) && | ||
239 | (NULL != cc->cb)) | ||
240 | { | ||
241 | cc->cb (cc->cb_cls); | ||
242 | cc->cb = NULL; /* only notify once! */ | ||
243 | } | ||
244 | } | ||
245 | return ret; | ||
246 | } | ||
247 | |||
248 | |||
249 | /** | ||
250 | * Offer the current HELLO of P2 to P1. | ||
251 | * | ||
252 | * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest` | ||
253 | */ | ||
254 | static void | ||
255 | offer_hello (void *cls); | ||
256 | |||
257 | |||
258 | static void | ||
259 | notify_disconnect (void *cls, | ||
260 | const struct GNUNET_PeerIdentity *peer, | ||
261 | void *handler_cls) | ||
262 | { | ||
263 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
264 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
265 | char *p2_s; | ||
266 | /* Find PeerContext */ | ||
267 | int no = 0; | ||
268 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL; | ||
269 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
270 | |||
271 | p2 = find_peer_context (p->tth, | ||
272 | peer); | ||
273 | no = p->no; | ||
274 | if (NULL != p2) | ||
275 | GNUNET_asprintf (&p2_s, | ||
276 | "%u (`%s')", | ||
277 | p2->no, | ||
278 | GNUNET_i2s (&p2->id)); | ||
279 | else | ||
280 | GNUNET_asprintf (&p2_s, | ||
281 | "`%s'", | ||
282 | GNUNET_i2s (peer)); | ||
283 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
284 | "Peers %s disconnected from peer %u (`%s')\n", | ||
285 | p2_s, | ||
286 | no, | ||
287 | GNUNET_i2s (&p->id)); | ||
288 | GNUNET_free (p2_s); | ||
289 | /* notify about disconnect */ | ||
290 | if (NULL != p->nd) | ||
291 | p->nd (p->cb_cls, | ||
292 | peer, | ||
293 | handler_cls); | ||
294 | if (NULL == p2) | ||
295 | return; | ||
296 | /* clear MQ, it is now invalid */ | ||
297 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
298 | p2, | ||
299 | &set_mq, | ||
300 | NULL); | ||
301 | /* update set connected flags for all requests */ | ||
302 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p, | ||
303 | p2, | ||
304 | &clear_p1c, | ||
305 | NULL); | ||
306 | GNUNET_TRANSPORT_TESTING_find_connecting_context (p2, | ||
307 | p, | ||
308 | &clear_p2c, | ||
309 | NULL); | ||
310 | /* resume connectivity requests as necessary */ | ||
311 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
312 | { | ||
313 | if (GNUNET_NO == cc->connected) | ||
314 | continue; | ||
315 | if ((GNUNET_YES != cc->p1_c) || | ||
316 | (GNUNET_YES != cc->p2_c)) | ||
317 | { | ||
318 | cc->connected = GNUNET_NO; | ||
319 | /* start trying to connect */ | ||
320 | if (NULL == cc->tct) | ||
321 | cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello, | ||
322 | cc); | ||
323 | if (NULL == cc->ats_sh) | ||
324 | cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats, | ||
325 | &p2->id, | ||
326 | 1); | ||
327 | } | ||
328 | } | ||
329 | } | ||
330 | |||
331 | |||
332 | static void | ||
333 | hello_iter_cb (void *cb_cls, | ||
334 | const struct GNUNET_PEERSTORE_Record *record, | ||
335 | const char *emsg) | ||
336 | { | ||
337 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls; | ||
338 | //Check record type et al? | ||
339 | p->hello = GNUNET_malloc (record->value_size); | ||
340 | memcpy (p->hello, record->value, record->value_size); | ||
341 | |||
342 | GNUNET_PEERSTORE_iterate_cancel (p->pic); | ||
343 | if (NULL != p->start_cb) | ||
344 | { | ||
345 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
346 | "Peer %u (`%s') successfully started\n", | ||
347 | p->no, | ||
348 | GNUNET_i2s (&p->id)); | ||
349 | p->start_cb (p->start_cb_cls); | ||
350 | p->start_cb = NULL; | ||
351 | } | ||
352 | } | ||
353 | |||
354 | |||
355 | /** | ||
356 | * Start a peer with the given configuration | ||
357 | * @param tth the testing handle | ||
358 | * @param cfgname configuration file | ||
359 | * @param peer_id a unique number to identify the peer | ||
360 | * @param handlers functions for receiving messages | ||
361 | * @param nc connect callback | ||
362 | * @param nd disconnect callback | ||
363 | * @param cb_cls closure for callback | ||
364 | * @param start_cb start callback | ||
365 | * @param start_cb_cls closure for callback | ||
366 | * @return the peer context | ||
367 | */ | ||
368 | struct GNUNET_TRANSPORT_TESTING_PeerContext * | ||
369 | GNUNET_TRANSPORT_TESTING_start_peer (struct | ||
370 | GNUNET_TRANSPORT_TESTING_Handle *tth, | ||
371 | const char *cfgname, | ||
372 | int peer_id, | ||
373 | const struct | ||
374 | GNUNET_MQ_MessageHandler *handlers, | ||
375 | GNUNET_TRANSPORT_NotifyConnect nc, | ||
376 | GNUNET_TRANSPORT_NotifyDisconnect nd, | ||
377 | void *cb_cls, | ||
378 | GNUNET_SCHEDULER_TaskCallback start_cb, | ||
379 | void *start_cb_cls) | ||
380 | { | ||
381 | char *emsg = NULL; | ||
382 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p; | ||
383 | struct GNUNET_PeerIdentity dummy; | ||
384 | unsigned int i; | ||
385 | |||
386 | if (GNUNET_NO == GNUNET_DISK_file_test (cfgname)) | ||
387 | { | ||
388 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
389 | "File not found: `%s'\n", | ||
390 | cfgname); | ||
391 | return NULL; | ||
392 | } | ||
393 | |||
394 | p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext); | ||
395 | p->tth = tth; | ||
396 | p->nc = nc; | ||
397 | p->nd = nd; | ||
398 | if (NULL != handlers) | ||
399 | { | ||
400 | for (i = 0; NULL != handlers[i].cb; i++) | ||
401 | ; | ||
402 | p->handlers = GNUNET_new_array (i + 1, | ||
403 | struct GNUNET_MQ_MessageHandler); | ||
404 | GNUNET_memcpy (p->handlers, | ||
405 | handlers, | ||
406 | i * sizeof(struct GNUNET_MQ_MessageHandler)); | ||
407 | } | ||
408 | if (NULL != cb_cls) | ||
409 | p->cb_cls = cb_cls; | ||
410 | else | ||
411 | p->cb_cls = p; | ||
412 | p->start_cb = start_cb; | ||
413 | if (NULL != start_cb_cls) | ||
414 | p->start_cb_cls = start_cb_cls; | ||
415 | else | ||
416 | p->start_cb_cls = p; | ||
417 | GNUNET_CONTAINER_DLL_insert (tth->p_head, | ||
418 | tth->p_tail, | ||
419 | p); | ||
420 | |||
421 | /* Create configuration and call testing lib to modify it */ | ||
422 | p->cfg = GNUNET_CONFIGURATION_create (); | ||
423 | GNUNET_assert (GNUNET_OK == | ||
424 | GNUNET_CONFIGURATION_load (p->cfg, cfgname)); | ||
425 | if (GNUNET_SYSERR == | ||
426 | GNUNET_TESTING_configuration_create (tth->tl_system, | ||
427 | p->cfg)) | ||
428 | { | ||
429 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
430 | "Testing library failed to create unique configuration based on `%s'\n", | ||
431 | cfgname); | ||
432 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
433 | GNUNET_free (p); | ||
434 | return NULL; | ||
435 | } | ||
436 | |||
437 | p->no = peer_id; | ||
438 | /* Configure peer with configuration */ | ||
439 | p->peer = GNUNET_TESTING_peer_configure (tth->tl_system, | ||
440 | p->cfg, | ||
441 | p->no, | ||
442 | NULL, | ||
443 | &emsg); | ||
444 | if (NULL == p->peer) | ||
445 | { | ||
446 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
447 | "Testing library failed to create unique configuration based on `%s': `%s'\n", | ||
448 | cfgname, | ||
449 | emsg); | ||
450 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
451 | GNUNET_free (emsg); | ||
452 | return NULL; | ||
453 | } | ||
454 | GNUNET_free (emsg); | ||
455 | if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer)) | ||
456 | { | ||
457 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
458 | "Testing library failed to create unique configuration based on `%s'\n", | ||
459 | cfgname); | ||
460 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
461 | return NULL; | ||
462 | } | ||
463 | |||
464 | memset (&dummy, | ||
465 | '\0', | ||
466 | sizeof(dummy)); | ||
467 | GNUNET_TESTING_peer_get_identity (p->peer, | ||
468 | &p->id); | ||
469 | if (0 == memcmp (&dummy, | ||
470 | &p->id, | ||
471 | sizeof(struct GNUNET_PeerIdentity))) | ||
472 | { | ||
473 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
474 | "Testing library failed to obtain peer identity for peer %u\n", | ||
475 | p->no); | ||
476 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
477 | return NULL; | ||
478 | } | ||
479 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
480 | "Peer %u configured with identity `%s'\n", | ||
481 | p->no, | ||
482 | GNUNET_i2s_full (&p->id)); | ||
483 | p->tmh = GNUNET_TRANSPORT_manipulation_connect (p->cfg); | ||
484 | p->th = GNUNET_TRANSPORT_core_connect (p->cfg, | ||
485 | NULL, | ||
486 | handlers, | ||
487 | p, | ||
488 | ¬ify_connect, | ||
489 | ¬ify_disconnect); | ||
490 | if ((NULL == p->th) || | ||
491 | (NULL == p->tmh)) | ||
492 | { | ||
493 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
494 | "Failed to connect to transport service for peer `%s': `%s'\n", | ||
495 | cfgname, | ||
496 | emsg); | ||
497 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
498 | return NULL; | ||
499 | } | ||
500 | p->ats = GNUNET_ATS_connectivity_init (p->cfg); | ||
501 | if (NULL == p->ats) | ||
502 | { | ||
503 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
504 | "Failed to connect to ATS service for peer `%s': `%s'\n", | ||
505 | cfgname, | ||
506 | emsg); | ||
507 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
508 | return NULL; | ||
509 | } | ||
510 | p->pic = GNUNET_PEERSTORE_iterate (p->ph, | ||
511 | "transport", | ||
512 | &p->id, | ||
513 | GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY, | ||
514 | hello_iter_cb, | ||
515 | p); | ||
516 | GNUNET_assert (NULL != p->pic); | ||
517 | |||
518 | return p; | ||
519 | } | ||
520 | |||
521 | |||
522 | /** | ||
523 | * Stops and restarts the given peer, sleeping (!) for 5s in between. | ||
524 | * | ||
525 | * @param p the peer | ||
526 | * @param restart_cb callback to call when restarted | ||
527 | * @param restart_cb_cls callback closure | ||
528 | * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR | ||
529 | */ | ||
530 | int | ||
531 | GNUNET_TRANSPORT_TESTING_restart_peer (struct | ||
532 | GNUNET_TRANSPORT_TESTING_PeerContext *p, | ||
533 | GNUNET_SCHEDULER_TaskCallback restart_cb, | ||
534 | void *restart_cb_cls) | ||
535 | { | ||
536 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
537 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
538 | |||
539 | /* shutdown */ | ||
540 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
541 | "Stopping peer %u (`%s')\n", | ||
542 | p->no, | ||
543 | GNUNET_i2s (&p->id)); | ||
544 | if (NULL != p->pic) | ||
545 | { | ||
546 | GNUNET_PEERSTORE_iterate_cancel (p->pic); | ||
547 | p->pic = NULL; | ||
548 | } | ||
549 | if (NULL != p->th) | ||
550 | { | ||
551 | GNUNET_TRANSPORT_core_disconnect (p->th); | ||
552 | p->th = NULL; | ||
553 | } | ||
554 | if (NULL != p->tmh) | ||
555 | { | ||
556 | GNUNET_TRANSPORT_manipulation_disconnect (p->tmh); | ||
557 | p->tmh = NULL; | ||
558 | } | ||
559 | for (cc = p->tth->cc_head; NULL != cc; cc = ccn) | ||
560 | { | ||
561 | ccn = cc->next; | ||
562 | if ((cc->p1 == p) || | ||
563 | (cc->p2 == p)) | ||
564 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
565 | } | ||
566 | if (NULL != p->ats) | ||
567 | { | ||
568 | GNUNET_ATS_connectivity_done (p->ats); | ||
569 | p->ats = NULL; | ||
570 | } | ||
571 | if (GNUNET_SYSERR == | ||
572 | GNUNET_TESTING_peer_stop (p->peer)) | ||
573 | { | ||
574 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
575 | "Failed to stop peer %u (`%s')\n", | ||
576 | p->no, | ||
577 | GNUNET_i2s (&p->id)); | ||
578 | return GNUNET_SYSERR; | ||
579 | } | ||
580 | |||
581 | sleep (5); // YUCK! | ||
582 | |||
583 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
584 | "Restarting peer %u (`%s')\n", | ||
585 | p->no, | ||
586 | GNUNET_i2s (&p->id)); | ||
587 | /* restart */ | ||
588 | if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer)) | ||
589 | { | ||
590 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
591 | "Failed to restart peer %u (`%s')\n", | ||
592 | p->no, | ||
593 | GNUNET_i2s (&p->id)); | ||
594 | return GNUNET_SYSERR; | ||
595 | } | ||
596 | |||
597 | GNUNET_assert (NULL == p->start_cb); | ||
598 | p->start_cb = restart_cb; | ||
599 | p->start_cb_cls = restart_cb_cls; | ||
600 | |||
601 | p->th = GNUNET_TRANSPORT_core_connect (p->cfg, | ||
602 | NULL, | ||
603 | p->handlers, | ||
604 | p, | ||
605 | ¬ify_connect, | ||
606 | ¬ify_disconnect); | ||
607 | GNUNET_assert (NULL != p->th); | ||
608 | p->ats = GNUNET_ATS_connectivity_init (p->cfg); | ||
609 | p->pic = GNUNET_PEERSTORE_iterate (p->ph, | ||
610 | "transport", | ||
611 | &p->id, | ||
612 | GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY, | ||
613 | hello_iter_cb, | ||
614 | p); | ||
615 | GNUNET_assert (NULL != p->pic); | ||
616 | return GNUNET_OK; | ||
617 | } | ||
618 | |||
619 | |||
620 | /** | ||
621 | * Shutdown the given peer | ||
622 | * | ||
623 | * @param p the peer | ||
624 | */ | ||
625 | void | ||
626 | GNUNET_TRANSPORT_TESTING_stop_peer (struct | ||
627 | GNUNET_TRANSPORT_TESTING_PeerContext *p) | ||
628 | { | ||
629 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth; | ||
630 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
631 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
632 | |||
633 | for (cc = tth->cc_head; NULL != cc; cc = ccn) | ||
634 | { | ||
635 | ccn = cc->next; | ||
636 | if ((cc->p1 == p) || | ||
637 | (cc->p2 == p)) | ||
638 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
639 | } | ||
640 | if (NULL != p->pic) | ||
641 | { | ||
642 | GNUNET_PEERSTORE_iterate_cancel (p->pic); | ||
643 | p->pic = NULL; | ||
644 | } | ||
645 | if (NULL != p->tmh) | ||
646 | { | ||
647 | GNUNET_TRANSPORT_manipulation_disconnect (p->tmh); | ||
648 | p->tmh = NULL; | ||
649 | } | ||
650 | if (NULL != p->th) | ||
651 | { | ||
652 | GNUNET_TRANSPORT_core_disconnect (p->th); | ||
653 | p->th = NULL; | ||
654 | } | ||
655 | if (NULL != p->peer) | ||
656 | { | ||
657 | if (GNUNET_OK != | ||
658 | GNUNET_TESTING_peer_stop (p->peer)) | ||
659 | { | ||
660 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
661 | "Testing lib failed to stop peer %u (`%s')\n", | ||
662 | p->no, | ||
663 | GNUNET_i2s (&p->id)); | ||
664 | } | ||
665 | GNUNET_TESTING_peer_destroy (p->peer); | ||
666 | p->peer = NULL; | ||
667 | } | ||
668 | if (NULL != p->ats) | ||
669 | { | ||
670 | GNUNET_ATS_connectivity_done (p->ats); | ||
671 | p->ats = NULL; | ||
672 | } | ||
673 | if (NULL != p->hello) | ||
674 | { | ||
675 | GNUNET_free (p->hello); | ||
676 | p->hello = NULL; | ||
677 | } | ||
678 | if (NULL != p->cfg) | ||
679 | { | ||
680 | GNUNET_CONFIGURATION_destroy (p->cfg); | ||
681 | p->cfg = NULL; | ||
682 | } | ||
683 | if (NULL != p->handlers) | ||
684 | { | ||
685 | GNUNET_free (p->handlers); | ||
686 | p->handlers = NULL; | ||
687 | } | ||
688 | GNUNET_CONTAINER_DLL_remove (tth->p_head, | ||
689 | tth->p_tail, | ||
690 | p); | ||
691 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
692 | "Peer %u (`%s') stopped\n", | ||
693 | p->no, | ||
694 | GNUNET_i2s (&p->id)); | ||
695 | GNUNET_free (p); | ||
696 | } | ||
697 | |||
698 | |||
699 | /** | ||
700 | * Function called after the HELLO was passed to the | ||
701 | * transport service. | ||
702 | * FIXME maybe schedule the application_validate somehow | ||
703 | */ | ||
704 | /* | ||
705 | static void | ||
706 | hello_offered (void *cls) | ||
707 | { | ||
708 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls; | ||
709 | |||
710 | cc->oh = NULL; | ||
711 | cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
712 | &offer_hello, | ||
713 | cc); | ||
714 | }*/ | ||
715 | |||
716 | |||
717 | /** | ||
718 | * Offer the current HELLO of P2 to P1. | ||
719 | * | ||
720 | * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest` | ||
721 | */ | ||
722 | static void | ||
723 | offer_hello (void *cls) | ||
724 | { | ||
725 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls; | ||
726 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1; | ||
727 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2; | ||
728 | struct GNUNET_TIME_Absolute t; | ||
729 | enum GNUNET_NetworkType nt; | ||
730 | char *addr; | ||
731 | |||
732 | cc->tct = NULL; | ||
733 | { | ||
734 | char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id)); | ||
735 | |||
736 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
737 | "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %s\n", | ||
738 | p1->no, | ||
739 | GNUNET_i2s (&p1->id), | ||
740 | p2->no, | ||
741 | p2_s, | ||
742 | cc->p2->hello); | ||
743 | GNUNET_free (p2_s); | ||
744 | } | ||
745 | |||
746 | addr = GNUNET_HELLO_extract_address (cc->p2->hello, | ||
747 | cc->p2->hello_size, | ||
748 | &cc->p2->id, | ||
749 | &nt, | ||
750 | &t); | ||
751 | GNUNET_assert (NULL == addr); | ||
752 | GNUNET_TRANSPORT_application_validate (cc->p1->ah, | ||
753 | &cc->p2->id, | ||
754 | nt, | ||
755 | addr); | ||
756 | GNUNET_free (addr); | ||
757 | } | ||
758 | |||
759 | |||
760 | /** | ||
761 | * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message | ||
762 | * | ||
763 | * Remarks: start_peer's notify_connect callback can be called before. | ||
764 | * | ||
765 | * @param tth transport testing handle | ||
766 | * @param p1 peer 1 | ||
767 | * @param p2 peer 2 | ||
768 | * @param cb the callback to call when both peers notified that they are connected | ||
769 | * @param cls callback cls | ||
770 | * @return a connect request handle | ||
771 | */ | ||
772 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest * | ||
773 | GNUNET_TRANSPORT_TESTING_connect_peers (struct | ||
774 | GNUNET_TRANSPORT_TESTING_PeerContext *p1, | ||
775 | struct | ||
776 | GNUNET_TRANSPORT_TESTING_PeerContext *p2, | ||
777 | GNUNET_SCHEDULER_TaskCallback cb, | ||
778 | void *cls) | ||
779 | { | ||
780 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth; | ||
781 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
782 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn; | ||
783 | |||
784 | ccn = NULL; | ||
785 | for (cc = tth->cc_head; NULL != cc; cc = cc->next) | ||
786 | { | ||
787 | if ((cc->p1 == p1) && | ||
788 | (cc->p2 == p2)) | ||
789 | { | ||
790 | ccn = cc; | ||
791 | break; | ||
792 | } | ||
793 | } | ||
794 | |||
795 | cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest); | ||
796 | cc->p1 = p1; | ||
797 | cc->p2 = p2; | ||
798 | cc->cb = cb; | ||
799 | if (NULL != cls) | ||
800 | cc->cb_cls = cls; | ||
801 | else | ||
802 | cc->cb_cls = cc; | ||
803 | if (NULL != ccn) | ||
804 | { | ||
805 | cc->p1_c = ccn->p1_c; | ||
806 | cc->p2_c = ccn->p2_c; | ||
807 | cc->connected = ccn->connected; | ||
808 | } | ||
809 | GNUNET_CONTAINER_DLL_insert (tth->cc_head, | ||
810 | tth->cc_tail, | ||
811 | cc); | ||
812 | cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello, | ||
813 | cc); | ||
814 | cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats, | ||
815 | &p2->id, | ||
816 | 1); | ||
817 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
818 | "New connect request %p\n", | ||
819 | cc); | ||
820 | return cc; | ||
821 | } | ||
822 | |||
823 | |||
824 | /** | ||
825 | * Cancel the request to connect two peers | ||
826 | * Tou MUST cancel the request if you stop the peers before the peers connected succesfully | ||
827 | * | ||
828 | * @param tth transport testing handle | ||
829 | * @param cc a connect request handle | ||
830 | */ | ||
831 | void | ||
832 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct | ||
833 | GNUNET_TRANSPORT_TESTING_ConnectRequest | ||
834 | *cc) | ||
835 | { | ||
836 | struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth; | ||
837 | |||
838 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
839 | "Canceling connect request!\n"); | ||
840 | if (NULL != cc->tct) | ||
841 | { | ||
842 | GNUNET_SCHEDULER_cancel (cc->tct); | ||
843 | cc->tct = NULL; | ||
844 | } | ||
845 | if (NULL != cc->ats_sh) | ||
846 | { | ||
847 | GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh); | ||
848 | cc->ats_sh = NULL; | ||
849 | } | ||
850 | GNUNET_CONTAINER_DLL_remove (tth->cc_head, | ||
851 | tth->cc_tail, | ||
852 | cc); | ||
853 | GNUNET_free (cc); | ||
854 | } | ||
855 | |||
856 | |||
857 | /** | ||
858 | * Clean up the transport testing | ||
859 | * | ||
860 | * @param tth transport testing handle | ||
861 | */ | ||
862 | void | ||
863 | GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth) | ||
864 | { | ||
865 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc; | ||
866 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct; | ||
867 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p; | ||
868 | struct GNUNET_TRANSPORT_TESTING_PeerContext *t; | ||
869 | |||
870 | if (NULL == tth) | ||
871 | return; | ||
872 | cc = tth->cc_head; | ||
873 | while (NULL != cc) | ||
874 | { | ||
875 | ct = cc->next; | ||
876 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
877 | "Developer forgot to cancel connect request!\n"); | ||
878 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc); | ||
879 | cc = ct; | ||
880 | } | ||
881 | p = tth->p_head; | ||
882 | while (NULL != p) | ||
883 | { | ||
884 | t = p->next; | ||
885 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
886 | "Developer forgot to stop peer!\n"); | ||
887 | GNUNET_TRANSPORT_TESTING_stop_peer (p); | ||
888 | p = t; | ||
889 | } | ||
890 | GNUNET_TESTING_system_destroy (tth->tl_system, | ||
891 | GNUNET_YES); | ||
892 | |||
893 | GNUNET_free (tth); | ||
894 | } | ||
895 | |||
896 | |||
897 | /** | ||
898 | * Initialize the transport testing | ||
899 | * | ||
900 | * @return transport testing handle | ||
901 | */ | ||
902 | struct GNUNET_TRANSPORT_TESTING_Handle * | ||
903 | GNUNET_TRANSPORT_TESTING_init () | ||
904 | { | ||
905 | struct GNUNET_TRANSPORT_TESTING_Handle *tth; | ||
906 | |||
907 | tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle); | ||
908 | tth->tl_system = GNUNET_TESTING_system_create ("transport-testing", | ||
909 | NULL, | ||
910 | NULL, | ||
911 | NULL); | ||
912 | if (NULL == tth->tl_system) | ||
913 | { | ||
914 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
915 | "Failed to initialize testing library!\n"); | ||
916 | GNUNET_free (tth); | ||
917 | return NULL; | ||
918 | } | ||
919 | return tth; | ||
920 | } | ||
921 | |||
922 | |||
923 | /* end of transport-testing.c */ | ||
diff --git a/src/transport/transport-testing2.h b/src/transport/transport-testing2.h new file mode 100644 index 000000000..3554436d8 --- /dev/null +++ b/src/transport/transport-testing2.h | |||
@@ -0,0 +1,924 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2006, 2009, 2015, 2016 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 | /** | ||
22 | * @file transport-testing.h | ||
23 | * @brief testing lib for transport service | ||
24 | * @author Matthias Wachs | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #ifndef TRANSPORT_TESTING_H | ||
28 | #define TRANSPORT_TESTING_H | ||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_peerstore_service.h" | ||
33 | #include "gnunet_transport_core_service.h" | ||
34 | #include "gnunet_transport_application_service.h" | ||
35 | #include "gnunet_transport_manipulation_service.h" | ||
36 | #include "gnunet_testing_lib.h" | ||
37 | |||
38 | |||
39 | /* ************* Basic functions for starting/stopping/connecting *********** */ | ||
40 | |||
41 | /** | ||
42 | * Context for a single peer | ||
43 | */ | ||
44 | struct GNUNET_TRANSPORT_TESTING_PeerContext; | ||
45 | |||
46 | /** | ||
47 | * Definition for a transport testing handle | ||
48 | */ | ||
49 | struct GNUNET_TRANSPORT_TESTING_Handle; | ||
50 | |||
51 | |||
52 | /** | ||
53 | * Context for a single peer | ||
54 | */ | ||
55 | struct GNUNET_TRANSPORT_TESTING_PeerContext | ||
56 | { | ||
57 | /** | ||
58 | * Next element in the DLL | ||
59 | */ | ||
60 | struct GNUNET_TRANSPORT_TESTING_PeerContext *next; | ||
61 | |||
62 | /** | ||
63 | * Previous element in the DLL | ||
64 | */ | ||
65 | struct GNUNET_TRANSPORT_TESTING_PeerContext *prev; | ||
66 | |||
67 | /** | ||
68 | * Transport testing handle this peer belongs to | ||
69 | */ | ||
70 | struct GNUNET_TRANSPORT_TESTING_Handle *tth; | ||
71 | |||
72 | /** | ||
73 | * Application handle | ||
74 | */ | ||
75 | struct GNUNET_TRANSPORT_ApplicationHandle *ah; | ||
76 | |||
77 | /** | ||
78 | * Peer's configuration | ||
79 | */ | ||
80 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
81 | |||
82 | /** | ||
83 | * Peer's transport service handle | ||
84 | */ | ||
85 | struct GNUNET_TRANSPORT_CoreHandle *th; | ||
86 | |||
87 | /** | ||
88 | * Peer's transport service manipulation handle | ||
89 | */ | ||
90 | struct GNUNET_TRANSPORT_ManipulationHandle *tmh; | ||
91 | |||
92 | /** | ||
93 | * Peer's ATS handle. | ||
94 | */ | ||
95 | struct GNUNET_ATS_ConnectivityHandle *ats; | ||
96 | |||
97 | /** | ||
98 | * Peer's PEERSTORE Handle | ||
99 | */ | ||
100 | struct GNUNET_PEERSTORE_Handle *ph; | ||
101 | |||
102 | /** | ||
103 | * Peer's transport get hello handle to retrieve peer's HELLO message | ||
104 | */ | ||
105 | struct GNUNET_PEERSTORE_IterateContext *pic; | ||
106 | |||
107 | /** | ||
108 | * Hello | ||
109 | */ | ||
110 | char *hello; | ||
111 | |||
112 | /** | ||
113 | * Hello size | ||
114 | */ | ||
115 | size_t hello_size; | ||
116 | |||
117 | /** | ||
118 | * Peer's testing handle | ||
119 | */ | ||
120 | struct GNUNET_TESTING_Peer *peer; | ||
121 | |||
122 | /** | ||
123 | * Peer identity | ||
124 | */ | ||
125 | struct GNUNET_PeerIdentity id; | ||
126 | |||
127 | /** | ||
128 | * Handle for the peer's ARM process | ||
129 | */ | ||
130 | struct GNUNET_OS_Process *arm_proc; | ||
131 | |||
132 | /** | ||
133 | * Receive callback | ||
134 | */ | ||
135 | struct GNUNET_MQ_MessageHandler *handlers; | ||
136 | |||
137 | /** | ||
138 | * Notify connect callback | ||
139 | */ | ||
140 | GNUNET_TRANSPORT_NotifyConnect nc; | ||
141 | |||
142 | /** | ||
143 | * Notify disconnect callback | ||
144 | */ | ||
145 | GNUNET_TRANSPORT_NotifyDisconnect nd; | ||
146 | |||
147 | /** | ||
148 | * Startup completed callback | ||
149 | */ | ||
150 | GNUNET_SCHEDULER_TaskCallback start_cb; | ||
151 | |||
152 | /** | ||
153 | * Closure for the @a nc and @a nd callbacks | ||
154 | */ | ||
155 | void *cb_cls; | ||
156 | |||
157 | /** | ||
158 | * Closure for @e start_cb. | ||
159 | */ | ||
160 | void *start_cb_cls; | ||
161 | |||
162 | /** | ||
163 | * An unique number to identify the peer | ||
164 | */ | ||
165 | unsigned int no; | ||
166 | }; | ||
167 | |||
168 | |||
169 | /** | ||
170 | * Handle for a request to connect two peers. | ||
171 | */ | ||
172 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest | ||
173 | { | ||
174 | /** | ||
175 | * Kept in a DLL. | ||
176 | */ | ||
177 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *next; | ||
178 | |||
179 | /** | ||
180 | * Kept in a DLL. | ||
181 | */ | ||
182 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *prev; | ||
183 | |||
184 | /** | ||
185 | * Peer we want to connect. | ||
186 | */ | ||
187 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1; | ||
188 | |||
189 | /** | ||
190 | * Peer we want to connect. | ||
191 | */ | ||
192 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2; | ||
193 | |||
194 | /** | ||
195 | * Task by which we accomplish the connection. | ||
196 | */ | ||
197 | struct GNUNET_SCHEDULER_Task *tct; | ||
198 | |||
199 | /** | ||
200 | * Handle by which we ask ATS to faciliate the connection. | ||
201 | */ | ||
202 | struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh; | ||
203 | |||
204 | /** | ||
205 | * Function to call upon completion. | ||
206 | */ | ||
207 | GNUNET_SCHEDULER_TaskCallback cb; | ||
208 | |||
209 | /** | ||
210 | * Closure for @e cb. | ||
211 | */ | ||
212 | void *cb_cls; | ||
213 | |||
214 | /** | ||
215 | * Message queue for sending from @a p1 to @a p2. | ||
216 | */ | ||
217 | struct GNUNET_MQ_Handle *mq; | ||
218 | |||
219 | /** | ||
220 | * Set if peer1 says the connection is up to peer2. | ||
221 | */ | ||
222 | int p1_c; | ||
223 | |||
224 | /** | ||
225 | * Set if peer2 says the connection is up to peer1. | ||
226 | */ | ||
227 | int p2_c; | ||
228 | |||
229 | /** | ||
230 | * #GNUNET_YES if both @e p1_c and @e p2_c are #GNUNET_YES. | ||
231 | */ | ||
232 | int connected; | ||
233 | }; | ||
234 | |||
235 | |||
236 | /** | ||
237 | * Handle for a test run. | ||
238 | */ | ||
239 | struct GNUNET_TRANSPORT_TESTING_Handle | ||
240 | { | ||
241 | /** | ||
242 | * Testing library system handle | ||
243 | */ | ||
244 | struct GNUNET_TESTING_System *tl_system; | ||
245 | |||
246 | /** | ||
247 | * head DLL of connect contexts | ||
248 | */ | ||
249 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_head; | ||
250 | |||
251 | /** | ||
252 | * head DLL of connect contexts | ||
253 | */ | ||
254 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_tail; | ||
255 | |||
256 | /** | ||
257 | * head DLL of peers | ||
258 | */ | ||
259 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p_head; | ||
260 | |||
261 | /** | ||
262 | * tail DLL of peers | ||
263 | */ | ||
264 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p_tail; | ||
265 | }; | ||
266 | |||
267 | |||
268 | /** | ||
269 | * Initialize the transport testing | ||
270 | * | ||
271 | * @return transport testing handle | ||
272 | */ | ||
273 | struct GNUNET_TRANSPORT_TESTING_Handle * | ||
274 | GNUNET_TRANSPORT_TESTING_init (void); | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Clean up the transport testing | ||
279 | * | ||
280 | * @param tth transport testing handle | ||
281 | */ | ||
282 | void | ||
283 | GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth); | ||
284 | |||
285 | |||
286 | /** | ||
287 | * Start a peer with the given configuration | ||
288 | * | ||
289 | * @param tth the testing handle | ||
290 | * @param cfgname configuration file | ||
291 | * @param peer_id the peer_id | ||
292 | * @param handlers functions for receiving messages | ||
293 | * @param nc connect callback | ||
294 | * @param nd disconnect callback | ||
295 | * @param cb_cls closure for @a nc and @a nd callback | ||
296 | * @param start_cb start callback | ||
297 | * @param start_cb_cls closure for @a start_cb | ||
298 | * @return the peer context | ||
299 | */ | ||
300 | struct GNUNET_TRANSPORT_TESTING_PeerContext * | ||
301 | GNUNET_TRANSPORT_TESTING_start_peer ( | ||
302 | struct GNUNET_TRANSPORT_TESTING_Handle *tth, | ||
303 | const char *cfgname, | ||
304 | int peer_id, | ||
305 | const struct GNUNET_MQ_MessageHandler *handlers, | ||
306 | GNUNET_TRANSPORT_NotifyConnect nc, | ||
307 | GNUNET_TRANSPORT_NotifyDisconnect nd, | ||
308 | void *cb_cls, | ||
309 | GNUNET_SCHEDULER_TaskCallback start_cb, | ||
310 | void *start_cb_cls); | ||
311 | |||
312 | |||
313 | /** | ||
314 | * Shutdown the given peer | ||
315 | * | ||
316 | * @param p the peer | ||
317 | */ | ||
318 | void | ||
319 | GNUNET_TRANSPORT_TESTING_stop_peer ( | ||
320 | struct GNUNET_TRANSPORT_TESTING_PeerContext *pc); | ||
321 | |||
322 | |||
323 | /** | ||
324 | * Stops and restarts the given peer, sleeping (!) for 5s in between. | ||
325 | * | ||
326 | * @param p the peer | ||
327 | * @param restart_cb restart callback | ||
328 | * @param restart_cb_cls callback closure | ||
329 | * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR | ||
330 | */ | ||
331 | int | ||
332 | GNUNET_TRANSPORT_TESTING_restart_peer ( | ||
333 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p, | ||
334 | GNUNET_SCHEDULER_TaskCallback restart_cb, | ||
335 | void *restart_cb_cls); | ||
336 | |||
337 | |||
338 | /** | ||
339 | * Connect the given peers and call the callback when both peers | ||
340 | * report the inbound connection. Remarks: start_peer's notify_connect | ||
341 | * callback can be called before. | ||
342 | * | ||
343 | * @param p1 peer 1 | ||
344 | * @param p2 peer 2 | ||
345 | * @param cb the callback to call when both peers notified that they are | ||
346 | * connected | ||
347 | * @param cls callback cls | ||
348 | * @return a connect request handle | ||
349 | */ | ||
350 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest * | ||
351 | GNUNET_TRANSPORT_TESTING_connect_peers ( | ||
352 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1, | ||
353 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2, | ||
354 | GNUNET_SCHEDULER_TaskCallback cb, | ||
355 | void *cls); | ||
356 | |||
357 | |||
358 | /** | ||
359 | * Cancel the request to connect two peers. You MUST cancel the | ||
360 | * request if you stop the peers before the peers connected | ||
361 | * succesfully. | ||
362 | * | ||
363 | * @param cc a connect request handle | ||
364 | */ | ||
365 | void | ||
366 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel ( | ||
367 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc); | ||
368 | |||
369 | |||
370 | /** | ||
371 | * Function called on matching connect requests. | ||
372 | * | ||
373 | * @param cls closure | ||
374 | * @param cc request matching the query | ||
375 | */ | ||
376 | typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContextCallback) ( | ||
377 | void *cls, | ||
378 | struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc); | ||
379 | |||
380 | |||
381 | /** | ||
382 | * Find any connecting context matching the given pair of peers. | ||
383 | * | ||
384 | * @param p1 first peer | ||
385 | * @param p2 second peer | ||
386 | * @param cb function to call | ||
387 | * @param cb_cls closure for @a cb | ||
388 | */ | ||
389 | void | ||
390 | GNUNET_TRANSPORT_TESTING_find_connecting_context ( | ||
391 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p1, | ||
392 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p2, | ||
393 | GNUNET_TRANSPORT_TESTING_ConnectContextCallback cb, | ||
394 | void *cb_cls); | ||
395 | |||
396 | |||
397 | /* ********************** high-level process functions *************** */ | ||
398 | |||
399 | |||
400 | /** | ||
401 | * Function called once the peers have been launched and | ||
402 | * connected by #GNUNET_TRANSPORT_TESTING_connect_check(). | ||
403 | * | ||
404 | * @param cls closure | ||
405 | * @param num_peers size of the @a p array | ||
406 | * @param p the peers that were launched | ||
407 | */ | ||
408 | typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContinuation) ( | ||
409 | void *cls, | ||
410 | unsigned int num_peers, | ||
411 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p[]); | ||
412 | |||
413 | |||
414 | /** | ||
415 | * Internal data structure. | ||
416 | */ | ||
417 | struct GNUNET_TRANSPORT_TESTING_ConnectRequestList; | ||
418 | |||
419 | /** | ||
420 | * Internal data structure. | ||
421 | */ | ||
422 | struct GNUNET_TRANSPORT_TESTING_InternalPeerContext; | ||
423 | |||
424 | |||
425 | GNUNET_NETWORK_STRUCT_BEGIN | ||
426 | struct GNUNET_TRANSPORT_TESTING_TestMessage | ||
427 | { | ||
428 | /** | ||
429 | * Type is (usually) #GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE. | ||
430 | */ | ||
431 | struct GNUNET_MessageHeader header; | ||
432 | |||
433 | /** | ||
434 | * Monotonically increasing counter throughout the test. | ||
435 | */ | ||
436 | uint32_t num GNUNET_PACKED; | ||
437 | }; | ||
438 | GNUNET_NETWORK_STRUCT_END | ||
439 | |||
440 | |||
441 | /** | ||
442 | * Function called by the transport for each received message. | ||
443 | * | ||
444 | * @param cls closure | ||
445 | * @param receiver receiver of the message | ||
446 | * @param sender sender of the message | ||
447 | * @param message the message | ||
448 | */ | ||
449 | typedef void (*GNUNET_TRANSPORT_TESTING_ReceiveCallback) ( | ||
450 | void *cls, | ||
451 | struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver, | ||
452 | const struct GNUNET_PeerIdentity *sender, | ||
453 | const struct GNUNET_TRANSPORT_TESTING_TestMessage *message); | ||
454 | |||
455 | |||
456 | /** | ||
457 | * Function called to notify transport users that another | ||
458 | * peer connected to us. | ||
459 | * | ||
460 | * @param cls closure | ||
461 | * @param me peer experiencing the event | ||
462 | * @param other peer that connected to @a me | ||
463 | */ | ||
464 | typedef void (*GNUNET_TRANSPORT_TESTING_NotifyConnect) ( | ||
465 | void *cls, | ||
466 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
467 | const struct GNUNET_PeerIdentity *other); | ||
468 | |||
469 | |||
470 | /** | ||
471 | * Function called to notify transport users that another | ||
472 | * peer disconnected from us. | ||
473 | * | ||
474 | * @param cls closure | ||
475 | * @param me peer experiencing the event | ||
476 | * @param other peer that disconnected from @a me | ||
477 | */ | ||
478 | typedef void (*GNUNET_TRANSPORT_TESTING_NotifyDisconnect) ( | ||
479 | void *cls, | ||
480 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
481 | const struct GNUNET_PeerIdentity *other); | ||
482 | |||
483 | |||
484 | /** | ||
485 | * Closure that must be passed to | ||
486 | * #GNUNET_TRANSPORT_TESTING_connect_check. | ||
487 | */ | ||
488 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext | ||
489 | { | ||
490 | /** | ||
491 | * How should we continue after the connect? | ||
492 | */ | ||
493 | GNUNET_SCHEDULER_TaskCallback connect_continuation; | ||
494 | |||
495 | /** | ||
496 | * Closure for @e connect_continuation. | ||
497 | */ | ||
498 | void *connect_continuation_cls; | ||
499 | |||
500 | /** | ||
501 | * Which configuration file should we pass to the | ||
502 | * #GNUNET_PROGRAM_run() of the testcase? | ||
503 | */ | ||
504 | const char *config_file; | ||
505 | |||
506 | /** | ||
507 | * Receiver argument to give for peers we start. | ||
508 | */ | ||
509 | GNUNET_TRANSPORT_TESTING_ReceiveCallback rec; | ||
510 | |||
511 | /** | ||
512 | * Notify connect argument to give for peers we start. | ||
513 | */ | ||
514 | GNUNET_TRANSPORT_TESTING_NotifyConnect nc; | ||
515 | |||
516 | /** | ||
517 | * Notify disconnect argument to give for peers we start. | ||
518 | */ | ||
519 | GNUNET_TRANSPORT_TESTING_NotifyDisconnect nd; | ||
520 | |||
521 | /** | ||
522 | * Closure for @e rec, @e nc and @e nd. | ||
523 | */ | ||
524 | void *cls; | ||
525 | |||
526 | /** | ||
527 | * Custom task to run on shutdown. | ||
528 | */ | ||
529 | GNUNET_SCHEDULER_TaskCallback shutdown_task; | ||
530 | |||
531 | /** | ||
532 | * Closure for @e shutdown_task. | ||
533 | */ | ||
534 | void *shutdown_task_cls; | ||
535 | |||
536 | /** | ||
537 | * Custom task to run after peers were started but before we try to | ||
538 | * connect them. If this function is set, we wait ONE second after | ||
539 | * running this function until we continue with connecting the | ||
540 | * peers. | ||
541 | */ | ||
542 | GNUNET_SCHEDULER_TaskCallback pre_connect_task; | ||
543 | |||
544 | /** | ||
545 | * Closure for @e shutdown_task. | ||
546 | */ | ||
547 | void *pre_connect_task_cls; | ||
548 | |||
549 | /** | ||
550 | * When should the testcase time out? | ||
551 | */ | ||
552 | struct GNUNET_TIME_Relative timeout; | ||
553 | |||
554 | /** | ||
555 | * Should we try to create connections in both directions? | ||
556 | */ | ||
557 | int bi_directional; | ||
558 | |||
559 | /* ******* fields set by #GNUNET_TRANSPORT_TESTING_connect_check **** */ | ||
560 | |||
561 | /** | ||
562 | * Number of peers involved in the test. | ||
563 | */ | ||
564 | unsigned int num_peers; | ||
565 | |||
566 | /** | ||
567 | * Configuration files we have, array with @e num_peers entries. | ||
568 | */ | ||
569 | char **cfg_files; | ||
570 | |||
571 | /** | ||
572 | * Array with @e num_peers entries. | ||
573 | */ | ||
574 | struct GNUNET_TRANSPORT_TESTING_PeerContext **p; | ||
575 | |||
576 | /** | ||
577 | * Name of the plugin. | ||
578 | */ | ||
579 | const char *test_plugin; | ||
580 | |||
581 | /** | ||
582 | * Name of the testcase. | ||
583 | */ | ||
584 | const char *test_name; | ||
585 | |||
586 | /** | ||
587 | * Configuration object for the testcase. | ||
588 | */ | ||
589 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
590 | |||
591 | /** | ||
592 | * Main testing handle. | ||
593 | */ | ||
594 | struct GNUNET_TRANSPORT_TESTING_Handle *tth; | ||
595 | |||
596 | /** | ||
597 | * Result from the main function, set to #GNUNET_OK on success. | ||
598 | * Clients should set to #GNUNET_SYSERR to indicate test failure. | ||
599 | */ | ||
600 | int global_ret; | ||
601 | |||
602 | /** | ||
603 | * Generator for the `num` field in test messages. Incremented each | ||
604 | * time #GNUNET_TRANSPORT_TESTING_simple_send or | ||
605 | * #GNUNET_TRANSPORT_TESTING_large_send are used to transmit a | ||
606 | * message. | ||
607 | */ | ||
608 | uint32_t send_num_gen; | ||
609 | |||
610 | /* ******* internal state, clients should not mess with this **** */ | ||
611 | |||
612 | /** | ||
613 | * Task run on timeout. | ||
614 | */ | ||
615 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
616 | |||
617 | /** | ||
618 | * Task run to connect peers. | ||
619 | */ | ||
620 | struct GNUNET_SCHEDULER_Task *connect_task; | ||
621 | |||
622 | /** | ||
623 | * Number of peers that have been started. | ||
624 | */ | ||
625 | unsigned int started; | ||
626 | |||
627 | /** | ||
628 | * DLL of active connect requests. | ||
629 | */ | ||
630 | struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_head; | ||
631 | |||
632 | /** | ||
633 | * DLL of active connect requests. | ||
634 | */ | ||
635 | struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_tail; | ||
636 | |||
637 | /** | ||
638 | * Array with @e num_peers entries. | ||
639 | */ | ||
640 | struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ip; | ||
641 | }; | ||
642 | |||
643 | |||
644 | /** | ||
645 | * Find peer by peer ID. | ||
646 | * | ||
647 | * @param ccc context to search | ||
648 | * @param peer peer to look for | ||
649 | * @return NULL if @a peer was not found | ||
650 | */ | ||
651 | struct GNUNET_TRANSPORT_TESTING_PeerContext * | ||
652 | GNUNET_TRANSPORT_TESTING_find_peer ( | ||
653 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc, | ||
654 | const struct GNUNET_PeerIdentity *peer); | ||
655 | |||
656 | |||
657 | /** | ||
658 | * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback. | ||
659 | * Starts and connects the two peers, then invokes the | ||
660 | * `connect_continuation` from @a cls. Sets up a timeout to | ||
661 | * abort the test, and a shutdown handler to clean up properly | ||
662 | * on exit. | ||
663 | * | ||
664 | * @param cls closure of type `struct | ||
665 | * GNUNET_TRANSPORT_TESTING_ConnectCheckContext` | ||
666 | * @param tth_ initialized testing handle | ||
667 | * @param test_plugin_ name of the plugin | ||
668 | * @param test_name_ name of the test | ||
669 | * @param num_peers number of entries in the @a cfg_file array | ||
670 | * @param cfg_files array of names of configuration files for the peers | ||
671 | * @return #GNUNET_SYSERR on error | ||
672 | */ | ||
673 | int | ||
674 | GNUNET_TRANSPORT_TESTING_connect_check ( | ||
675 | void *cls, | ||
676 | struct GNUNET_TRANSPORT_TESTING_Handle *tth_, | ||
677 | const char *test_plugin_, | ||
678 | const char *test_name_, | ||
679 | unsigned int num_peers, | ||
680 | char *cfg_files[]); | ||
681 | |||
682 | |||
683 | /** | ||
684 | * Main function of a testcase. Called with the initial setup data | ||
685 | * for the test as derived from the source name and the binary name. | ||
686 | * | ||
687 | * @param cls closure | ||
688 | * @param tth_ initialized testing handle | ||
689 | * @param test_plugin_ name of the plugin | ||
690 | * @param test_name_ name of the test | ||
691 | * @param num_peers number of entries in the @a cfg_file array | ||
692 | * @param cfg_files array of names of configuration files for the peers | ||
693 | * @return #GNUNET_SYSERR on error | ||
694 | */ | ||
695 | typedef int (*GNUNET_TRANSPORT_TESTING_CheckCallback) ( | ||
696 | void *cls, | ||
697 | struct GNUNET_TRANSPORT_TESTING_Handle *tth_, | ||
698 | const char *test_plugin_, | ||
699 | const char *test_name_, | ||
700 | unsigned int num_peers, | ||
701 | char *cfg_files[]); | ||
702 | |||
703 | |||
704 | /** | ||
705 | * Setup testcase. Calls @a check with the data the test needs. | ||
706 | * | ||
707 | * @param argv0 binary name (argv[0]) | ||
708 | * @param filename source file name (__FILE__) | ||
709 | * @param num_peers number of peers to start | ||
710 | * @param check main function to run | ||
711 | * @param check_cls closure for @a check | ||
712 | * @return #GNUNET_OK on success | ||
713 | */ | ||
714 | int | ||
715 | GNUNET_TRANSPORT_TESTING_main_ (const char *argv0, | ||
716 | const char *filename, | ||
717 | unsigned int num_peers, | ||
718 | GNUNET_TRANSPORT_TESTING_CheckCallback check, | ||
719 | void *check_cls); | ||
720 | |||
721 | |||
722 | /** | ||
723 | * Setup testcase. Calls @a check with the data the test needs. | ||
724 | * | ||
725 | * @param num_peers number of peers to start | ||
726 | * @param check main function to run | ||
727 | * @param check_cls closure for @a check | ||
728 | * @return #GNUNET_OK on success | ||
729 | */ | ||
730 | #define GNUNET_TRANSPORT_TESTING_main(num_peers, check, check_cls) \ | ||
731 | GNUNET_TRANSPORT_TESTING_main_ (argv[0], \ | ||
732 | __FILE__, \ | ||
733 | num_peers, \ | ||
734 | check, \ | ||
735 | check_cls) | ||
736 | |||
737 | /* ***************** Convenience functions for sending ********* */ | ||
738 | |||
739 | /** | ||
740 | * Send a test message of type @a mtype and size @a msize from | ||
741 | * peer @a sender to peer @a receiver. The peers should be | ||
742 | * connected when this function is called. | ||
743 | * | ||
744 | * @param sender the sending peer | ||
745 | * @param receiver the receiving peer | ||
746 | * @param mtype message type to use | ||
747 | * @param msize size of the message, at least `sizeof (struct | ||
748 | * GNUNET_TRANSPORT_TESTING_TestMessage)` | ||
749 | * @param num unique message number | ||
750 | * @param cont continuation to call after transmission | ||
751 | * @param cont_cls closure for @a cont | ||
752 | * @return #GNUNET_OK if message was queued, | ||
753 | * #GNUNET_NO if peers are not connected | ||
754 | * #GNUNET_SYSERR if @a msize is illegal | ||
755 | */ | ||
756 | int | ||
757 | GNUNET_TRANSPORT_TESTING_send ( | ||
758 | struct GNUNET_TRANSPORT_TESTING_PeerContext *sender, | ||
759 | struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver, | ||
760 | uint16_t mtype, | ||
761 | uint16_t msize, | ||
762 | uint32_t num, | ||
763 | GNUNET_SCHEDULER_TaskCallback cont, | ||
764 | void *cont_cls); | ||
765 | |||
766 | |||
767 | /** | ||
768 | * Message type used by #GNUNET_TRANSPORT_TESTING_simple_send(). | ||
769 | */ | ||
770 | #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE 12345 | ||
771 | |||
772 | /** | ||
773 | * Alternative message type for tests. | ||
774 | */ | ||
775 | #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2 12346 | ||
776 | |||
777 | |||
778 | /** | ||
779 | * Type of the closure argument to pass to | ||
780 | * #GNUNET_TRANSPORT_TESTING_simple_send() and | ||
781 | * #GNUNET_TRANSPORT_TESTING_large_send(). | ||
782 | */ | ||
783 | struct GNUNET_TRANSPORT_TESTING_SendClosure | ||
784 | { | ||
785 | /** | ||
786 | * Context for the transmission. | ||
787 | */ | ||
788 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc; | ||
789 | |||
790 | /** | ||
791 | * Function that returns the desired message size. Overrides | ||
792 | * the message size, can be NULL in which case the message | ||
793 | * size is the default. | ||
794 | */ | ||
795 | size_t (*get_size_cb) (unsigned int n); | ||
796 | |||
797 | /** | ||
798 | * Number of messages to be transmitted in a loop. | ||
799 | * Use zero for "forever" (until external shutdown). | ||
800 | */ | ||
801 | unsigned int num_messages; | ||
802 | |||
803 | /** | ||
804 | * Function to call after all transmissions, can be NULL. | ||
805 | */ | ||
806 | GNUNET_SCHEDULER_TaskCallback cont; | ||
807 | |||
808 | /** | ||
809 | * Closure for @e cont. | ||
810 | */ | ||
811 | void *cont_cls; | ||
812 | }; | ||
813 | |||
814 | |||
815 | /** | ||
816 | * Task that sends a minimalistic test message from the | ||
817 | * first peer to the second peer. | ||
818 | * | ||
819 | * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure` | ||
820 | * which should contain at least two peers, the first two | ||
821 | * of which should be currently connected | ||
822 | */ | ||
823 | void | ||
824 | GNUNET_TRANSPORT_TESTING_simple_send (void *cls); | ||
825 | |||
826 | /** | ||
827 | * Size of a message sent with | ||
828 | * #GNUNET_TRANSPORT_TESTING_large_send(). Big enough | ||
829 | * to usually force defragmentation. | ||
830 | */ | ||
831 | #define GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE 2600 | ||
832 | |||
833 | /** | ||
834 | * Task that sends a large test message from the | ||
835 | * first peer to the second peer. | ||
836 | * | ||
837 | * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure` | ||
838 | * which should contain at least two peers, the first two | ||
839 | * of which should be currently connected | ||
840 | */ | ||
841 | void | ||
842 | GNUNET_TRANSPORT_TESTING_large_send (void *cls); | ||
843 | |||
844 | |||
845 | /* ********************** log-only convenience functions ************* */ | ||
846 | |||
847 | |||
848 | /** | ||
849 | * Log a connect event. | ||
850 | * | ||
851 | * @param cls NULL | ||
852 | * @param me peer that had the event | ||
853 | * @param other peer that connected. | ||
854 | */ | ||
855 | void | ||
856 | GNUNET_TRANSPORT_TESTING_log_connect ( | ||
857 | void *cls, | ||
858 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
859 | const struct GNUNET_PeerIdentity *other); | ||
860 | |||
861 | |||
862 | /** | ||
863 | * Log a disconnect event. | ||
864 | * | ||
865 | * @param cls NULL | ||
866 | * @param me peer that had the event | ||
867 | * @param other peer that disconnected. | ||
868 | */ | ||
869 | void | ||
870 | GNUNET_TRANSPORT_TESTING_log_disconnect ( | ||
871 | void *cls, | ||
872 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
873 | const struct GNUNET_PeerIdentity *other); | ||
874 | |||
875 | |||
876 | /* ********************** low-level filename functions *************** */ | ||
877 | |||
878 | |||
879 | /** | ||
880 | * Extracts the test filename from an absolute file name and removes | ||
881 | * the extension. | ||
882 | * | ||
883 | * @param file absolute file name | ||
884 | * @return resulting test name | ||
885 | */ | ||
886 | char * | ||
887 | GNUNET_TRANSPORT_TESTING_get_test_name (const char *file); | ||
888 | |||
889 | |||
890 | /** | ||
891 | * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and | ||
892 | * if existing ".exe"-prefix and adds the peer-number | ||
893 | * | ||
894 | * @param file filename of the test, e.g. argv[0] | ||
895 | * @param count peer number | ||
896 | * @return configuration name to use | ||
897 | */ | ||
898 | char * | ||
899 | GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, int count); | ||
900 | |||
901 | |||
902 | /** | ||
903 | * Extracts the plugin anme from an absolute file name and the test name | ||
904 | * @param file absolute file name | ||
905 | * @param test test name | ||
906 | * @return the plugin name | ||
907 | */ | ||
908 | char * | ||
909 | GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable, | ||
910 | const char *testname); | ||
911 | |||
912 | |||
913 | /** | ||
914 | * Extracts the filename from an absolute file name and removes the | ||
915 | * extenstion | ||
916 | * | ||
917 | * @param file absolute file name | ||
918 | * @return the source name | ||
919 | */ | ||
920 | char * | ||
921 | GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file); | ||
922 | |||
923 | #endif | ||
924 | /* end of transport_testing.h */ | ||