2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2015, 2016 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 transport-testing.c
22 * @brief testing lib for transport service
23 * @author Matthias Wachs
24 * @author Christian Grothoff
26 #include "transport-testing.h"
29 #define LOG(kind,...) GNUNET_log_from(kind, "transport-testing", __VA_ARGS__)
32 static struct GNUNET_TRANSPORT_TESTING_PeerContext *
33 find_peer_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
34 const struct GNUNET_PeerIdentity *peer)
36 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
38 for (t = tth->p_head; NULL != t; t = t->next)
39 if (0 == memcmp (&t->id,
41 sizeof (struct GNUNET_PeerIdentity)))
48 * Find any connecting context matching the given pair of peers.
50 * @param p1 first peer
51 * @param p2 second peer
52 * @param cb function to call
53 * @param cb_cls closure for @a cb
56 GNUNET_TRANSPORT_TESTING_find_connecting_context (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
57 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
58 GNUNET_TRANSPORT_TESTING_ConnectContextCallback cb,
61 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
62 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
63 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
65 for (cc = tth->cc_head; NULL != cc; cc = ccn)
68 if ( (cc->p1 == p1) &&
78 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
80 cx->p1_c = GNUNET_YES;
86 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
88 cx->p2_c = GNUNET_YES;
94 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
101 clear_p2c (void *cls,
102 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
104 cx->p2_c = GNUNET_NO;
109 notify_connect (void *cls,
110 const struct GNUNET_PeerIdentity *peer)
112 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
113 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
115 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
116 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
117 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
119 p2 = find_peer_context (p->tth,
126 GNUNET_asprintf (&p2_s,
129 GNUNET_i2s (&p2->id));
131 GNUNET_asprintf (&p2_s,
134 LOG (GNUNET_ERROR_TYPE_DEBUG,
135 "Peers %s connected to peer %u (`%s')\n",
138 GNUNET_i2s (&p->id));
140 /* update flags in connecting contexts */
141 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
145 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
149 /* update set connected flag for all requests */
150 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
152 if (GNUNET_YES == cc->connected)
154 if ( (GNUNET_YES == cc->p1_c) &&
155 (GNUNET_YES == cc->p2_c) )
157 cc->connected = GNUNET_YES;
158 /* stop trying to connect */
161 GNUNET_SCHEDULER_cancel (cc->tct);
166 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
169 if (NULL != cc->ats_sh)
171 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
176 /* then notify application */
177 for (cc = tth->cc_head; NULL != cc; cc = ccn)
180 if ( (GNUNET_YES == cc->connected) &&
184 cc->cb = NULL; /* only notify once! */
191 * Offer the current HELLO of P2 to P1.
193 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
196 offer_hello (void *cls);
200 notify_disconnect (void *cls,
201 const struct GNUNET_PeerIdentity *peer)
203 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
204 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
206 /* Find PeerContext */
208 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL;
209 struct TRANSPORT_TESTING_SendJob *sj;
210 struct TRANSPORT_TESTING_SendJob *sjn;
211 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
213 p2 = find_peer_context (p->tth,
217 GNUNET_asprintf (&p2_s,
220 GNUNET_i2s (&p2->id));
222 GNUNET_asprintf (&p2_s,
225 LOG (GNUNET_ERROR_TYPE_DEBUG,
226 "Peers %s disconnected from peer %u (`%s')\n",
229 GNUNET_i2s (&p->id));
231 /* notify about disconnect */
237 /* abort all transmissions this disconnected pair is involved in */
238 for (sj = tth->sj_head; NULL != sj; sj = sjn)
241 if ( ( (sj->sender == p2) &&
242 (sj->receiver == p) ) ||
243 ( (sj->receiver == p2) &&
244 (sj->sender == p) ) )
246 GNUNET_CONTAINER_DLL_remove (tth->sj_head,
249 GNUNET_TRANSPORT_notify_transmit_ready_cancel (sj->th);
253 /* update set connected flags for all requests */
254 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
258 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
262 /* resume connectivity requests as necessary */
263 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
265 if (GNUNET_NO == cc->connected)
267 if ( (GNUNET_YES != cc->p1_c) ||
268 (GNUNET_YES != cc->p2_c) )
270 cc->connected = GNUNET_NO;
271 /* start trying to connect */
272 if ( (NULL == cc->tct) &&
274 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
276 if (NULL == cc->ats_sh)
277 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
286 notify_receive (void *cls,
287 const struct GNUNET_PeerIdentity *peer,
288 const struct GNUNET_MessageHeader *message)
290 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
302 get_hello (void *cb_cls,
303 const struct GNUNET_MessageHeader *message)
305 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls;
306 struct GNUNET_PeerIdentity hello_id;
308 GNUNET_assert (GNUNET_OK ==
309 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message,
311 GNUNET_assert (0 == memcmp (&hello_id,
314 GNUNET_free_non_null (p->hello);
315 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
317 if (NULL != p->start_cb)
319 LOG (GNUNET_ERROR_TYPE_DEBUG,
320 "Peer %u (`%s') successfully started\n",
322 GNUNET_i2s (&p->id));
331 * Start a peer with the given configuration
332 * @param tth the testing handle
333 * @param cfgname configuration file
334 * @param peer_id a unique number to identify the peer
335 * @param rec receive callback
336 * @param nc connect callback
337 * @param nd disconnect callback
338 * @param cb_cls closure for callback
339 * @param start_cb start callback
340 * @param start_cb_cls closure for callback
341 * @return the peer context
343 struct GNUNET_TRANSPORT_TESTING_PeerContext *
344 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
347 GNUNET_TRANSPORT_ReceiveCallback rec,
348 GNUNET_TRANSPORT_NotifyConnect nc,
349 GNUNET_TRANSPORT_NotifyDisconnect nd,
351 GNUNET_TRANSPORT_TESTING_StartCallback start_cb,
355 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
356 struct GNUNET_PeerIdentity *dummy;
358 if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
360 LOG (GNUNET_ERROR_TYPE_ERROR,
361 "File not found: `%s'\n",
366 p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext);
375 p->start_cb = start_cb;
376 p->start_cb_cls = start_cb_cls;
377 GNUNET_CONTAINER_DLL_insert (tth->p_head,
381 /* Create configuration and call testing lib to modify it */
382 p->cfg = GNUNET_CONFIGURATION_create ();
383 GNUNET_assert (GNUNET_OK ==
384 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
386 GNUNET_TESTING_configuration_create (tth->tl_system,
389 LOG (GNUNET_ERROR_TYPE_ERROR,
390 "Testing library failed to create unique configuration based on `%s'\n",
392 GNUNET_CONFIGURATION_destroy (p->cfg);
398 /* Configure peer with configuration */
399 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
406 LOG (GNUNET_ERROR_TYPE_ERROR,
407 "Testing library failed to create unique configuration based on `%s': `%s'\n",
410 GNUNET_TRANSPORT_TESTING_stop_peer (p);
411 GNUNET_free_non_null (emsg);
414 GNUNET_free_non_null (emsg);
415 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
417 LOG (GNUNET_ERROR_TYPE_ERROR,
418 "Testing library failed to create unique configuration based on `%s'\n",
420 GNUNET_TRANSPORT_TESTING_stop_peer (p);
427 GNUNET_TESTING_peer_get_identity (p->peer,
429 if (0 == memcmp (&dummy,
431 sizeof (struct GNUNET_PeerIdentity)))
433 LOG (GNUNET_ERROR_TYPE_ERROR,
434 "Testing library failed to obtain peer identity for peer %u\n",
436 GNUNET_TRANSPORT_TESTING_stop_peer (p);
439 LOG (GNUNET_ERROR_TYPE_DEBUG,
440 "Peer %u configured with identity `%s'\n",
442 GNUNET_i2s_full (&p->id));
444 p->th = GNUNET_TRANSPORT_connect (p->cfg,
452 LOG (GNUNET_ERROR_TYPE_ERROR,
453 "Failed to connect to transport service for peer `%s': `%s'\n",
456 GNUNET_TRANSPORT_TESTING_stop_peer (p);
459 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
462 LOG (GNUNET_ERROR_TYPE_ERROR,
463 "Failed to connect to ATS service for peer `%s': `%s'\n",
466 GNUNET_TRANSPORT_TESTING_stop_peer (p);
469 p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
472 GNUNET_assert (p->ghh != NULL);
478 * Stops and restarts the given peer, sleeping (!) for 5s in between.
481 * @param restart_cb callback to call when restarted
482 * @param restart_cb_cls callback closure
483 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
486 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
487 GNUNET_TRANSPORT_TESTING_StartCallback restart_cb,
488 void *restart_cb_cls)
490 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
491 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
494 LOG (GNUNET_ERROR_TYPE_DEBUG,
495 "Stopping peer %u (`%s')\n",
497 GNUNET_i2s (&p->id));
500 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
505 GNUNET_TRANSPORT_disconnect (p->th);
508 for (cc = p->tth->cc_head; NULL != cc; cc = ccn)
511 if ( (cc->p1 == p) ||
513 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
517 GNUNET_ATS_connectivity_done (p->ats);
521 GNUNET_TESTING_peer_stop (p->peer))
523 LOG (GNUNET_ERROR_TYPE_ERROR,
524 "Failed to stop peer %u (`%s')\n",
526 GNUNET_i2s (&p->id));
527 return GNUNET_SYSERR;
532 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 "Restarting peer %u (`%s')\n",
535 GNUNET_i2s (&p->id));
537 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
539 LOG (GNUNET_ERROR_TYPE_ERROR,
540 "Failed to restart peer %u (`%s')\n",
542 GNUNET_i2s (&p->id));
543 return GNUNET_SYSERR;
546 GNUNET_assert (NULL == p->start_cb);
547 p->start_cb = restart_cb;
548 p->start_cb_cls = restart_cb_cls;
550 p->th = GNUNET_TRANSPORT_connect (p->cfg,
556 GNUNET_assert (NULL != p->th);
557 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
558 p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
561 GNUNET_assert (NULL != p->ghh);
567 * Shutdown the given peer
572 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
574 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
575 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
576 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
578 for (cc = tth->cc_head; NULL != cc; cc = ccn)
581 if ( (cc->p1 == p) ||
583 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
587 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
592 GNUNET_TRANSPORT_disconnect (p->th);
598 GNUNET_TESTING_peer_stop (p->peer))
600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "Testing lib failed to stop peer %u (`%s')\n",
603 GNUNET_i2s (&p->id));
605 GNUNET_TESTING_peer_destroy (p->peer);
610 GNUNET_ATS_connectivity_done (p->ats);
613 if (NULL != p->hello)
615 GNUNET_free (p->hello);
620 GNUNET_CONFIGURATION_destroy (p->cfg);
623 GNUNET_CONTAINER_DLL_remove (tth->p_head,
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "Peer %u (`%s') stopped\n",
629 GNUNET_i2s (&p->id));
635 * Function called after the HELLO was passed to the
639 hello_offered (void *cls)
641 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
644 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
651 * Offer the current HELLO of P2 to P1.
653 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
656 offer_hello (void *cls)
658 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
659 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1;
660 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2;
664 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
669 GNUNET_i2s (&p1->id),
672 GNUNET_HELLO_size (cc->p2->hello));
677 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
679 GNUNET_TRANSPORT_offer_hello (cc->p1->cfg,
680 (const struct GNUNET_MessageHeader *) cc->p2->hello,
687 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
689 * Remarks: start_peer's notify_connect callback can be called before.
691 * @param tth transport testing handle
694 * @param cb the callback to call when both peers notified that they are connected
695 * @param cls callback cls
696 * @return a connect request handle
698 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
699 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
700 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
701 GNUNET_SCHEDULER_TaskCallback cb,
704 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
705 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
707 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
715 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
718 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
720 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
723 LOG (GNUNET_ERROR_TYPE_DEBUG,
724 "New connect request %p\n",
731 * Cancel the request to connect two peers
732 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
734 * @param tth transport testing handle
735 * @param cc a connect request handle
738 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
740 struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth;
742 LOG (GNUNET_ERROR_TYPE_DEBUG,
743 "Canceling connect request!\n");
746 GNUNET_SCHEDULER_cancel (cc->tct);
751 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
754 if (NULL != cc->ats_sh)
756 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
759 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
767 * Clean up the transport testing
769 * @param tth transport testing handle
772 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth)
774 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
775 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct;
776 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
777 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
785 LOG (GNUNET_ERROR_TYPE_ERROR,
786 "Developer forgot to cancel connect request!\n");
787 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
794 LOG (GNUNET_ERROR_TYPE_ERROR,
795 "Developer forgot to stop peer!\n");
796 GNUNET_TRANSPORT_TESTING_stop_peer (p);
799 GNUNET_TESTING_system_destroy (tth->tl_system,
807 * Initialize the transport testing
809 * @return transport testing handle
811 struct GNUNET_TRANSPORT_TESTING_Handle *
812 GNUNET_TRANSPORT_TESTING_init ()
814 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
816 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle);
817 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
821 if (NULL == tth->tl_system)
823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
824 "Failed to initialize testing library!\n");
831 /* end of transport-testing.c */