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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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)
84 cx->p1_c = GNUNET_YES;
90 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
92 struct GNUNET_MQ_Handle *mq = cls;
100 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
106 cx->p2_c = GNUNET_YES;
111 clear_p1c (void *cls,
112 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
118 cx->p1_c = GNUNET_NO;
123 clear_p2c (void *cls,
124 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
130 cx->p2_c = GNUNET_NO;
135 notify_connect (void *cls,
136 const struct GNUNET_PeerIdentity *peer,
137 struct GNUNET_MQ_Handle *mq)
139 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
140 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
142 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
143 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
144 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
148 p2 = find_peer_context (p->tth,
151 ret = p->nc (p->cb_cls,
158 GNUNET_asprintf (&p2_s,
161 GNUNET_i2s (&p2->id));
163 GNUNET_asprintf (&p2_s,
166 LOG (GNUNET_ERROR_TYPE_DEBUG,
167 "Peers %s connected to peer %u (`%s')\n",
170 GNUNET_i2s (&p->id));
172 /* update flags in connecting contexts */
174 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
178 if (GNUNET_NO == found)
180 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
183 cc->p1_c = GNUNET_YES;
184 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
189 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
193 if (GNUNET_NO == found)
195 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
198 cc->p1_c = GNUNET_YES;
199 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
203 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
207 /* update set connected flag for all requests */
208 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
210 if (GNUNET_YES == cc->connected)
212 if ( (GNUNET_YES == cc->p1_c) &&
213 (GNUNET_YES == cc->p2_c) )
215 cc->connected = GNUNET_YES;
216 /* stop trying to connect */
219 GNUNET_SCHEDULER_cancel (cc->tct);
224 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
227 if (NULL != cc->ats_sh)
229 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
234 /* then notify application */
235 for (cc = tth->cc_head; NULL != cc; cc = ccn)
238 if ( (GNUNET_YES == cc->connected) &&
242 cc->cb = NULL; /* only notify once! */
250 * Offer the current HELLO of P2 to P1.
252 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
255 offer_hello (void *cls);
259 notify_disconnect (void *cls,
260 const struct GNUNET_PeerIdentity *peer,
263 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
264 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
266 /* Find PeerContext */
268 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL;
269 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
271 p2 = find_peer_context (p->tth,
275 GNUNET_asprintf (&p2_s,
278 GNUNET_i2s (&p2->id));
280 GNUNET_asprintf (&p2_s,
283 LOG (GNUNET_ERROR_TYPE_DEBUG,
284 "Peers %s disconnected from peer %u (`%s')\n",
287 GNUNET_i2s (&p->id));
289 /* notify about disconnect */
296 /* clear MQ, it is now invalid */
297 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
301 /* update set connected flags for all requests */
302 GNUNET_TRANSPORT_TESTING_find_connecting_context (p,
306 GNUNET_TRANSPORT_TESTING_find_connecting_context (p2,
310 /* resume connectivity requests as necessary */
311 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
313 if (GNUNET_NO == cc->connected)
315 if ( (GNUNET_YES != cc->p1_c) ||
316 (GNUNET_YES != cc->p2_c) )
318 cc->connected = GNUNET_NO;
319 /* start trying to connect */
320 if ( (NULL == cc->tct) &&
322 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
324 if (NULL == cc->ats_sh)
325 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
334 get_hello (void *cb_cls,
335 const struct GNUNET_MessageHeader *message)
337 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls;
338 struct GNUNET_PeerIdentity hello_id;
340 GNUNET_assert (GNUNET_OK ==
341 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message,
343 GNUNET_assert (0 == memcmp (&hello_id,
346 GNUNET_free_non_null (p->hello);
347 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
349 if (NULL != p->start_cb)
351 LOG (GNUNET_ERROR_TYPE_DEBUG,
352 "Peer %u (`%s') successfully started\n",
354 GNUNET_i2s (&p->id));
355 p->start_cb (p->start_cb_cls);
362 * Start a peer with the given configuration
363 * @param tth the testing handle
364 * @param cfgname configuration file
365 * @param peer_id a unique number to identify the peer
366 * @param handlers functions for receiving messages
367 * @param nc connect callback
368 * @param nd disconnect callback
369 * @param cb_cls closure for callback
370 * @param start_cb start callback
371 * @param start_cb_cls closure for callback
372 * @return the peer context
374 struct GNUNET_TRANSPORT_TESTING_PeerContext *
375 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
378 const struct GNUNET_MQ_MessageHandler *handlers,
379 GNUNET_TRANSPORT_NotifyConnect nc,
380 GNUNET_TRANSPORT_NotifyDisconnect nd,
382 GNUNET_SCHEDULER_TaskCallback start_cb,
386 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
387 struct GNUNET_PeerIdentity dummy;
390 if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
392 LOG (GNUNET_ERROR_TYPE_ERROR,
393 "File not found: `%s'\n",
398 p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext);
402 if (NULL != handlers)
404 for (i=0;NULL != handlers[i].cb;i++) ;
405 p->handlers = GNUNET_new_array (i + 1,
406 struct GNUNET_MQ_MessageHandler);
407 GNUNET_memcpy (p->handlers,
409 i * sizeof (struct GNUNET_MQ_MessageHandler));
415 p->start_cb = start_cb;
416 if (NULL != start_cb_cls)
417 p->start_cb_cls = start_cb_cls;
420 GNUNET_CONTAINER_DLL_insert (tth->p_head,
424 /* Create configuration and call testing lib to modify it */
425 p->cfg = GNUNET_CONFIGURATION_create ();
426 GNUNET_assert (GNUNET_OK ==
427 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
429 GNUNET_TESTING_configuration_create (tth->tl_system,
432 LOG (GNUNET_ERROR_TYPE_ERROR,
433 "Testing library failed to create unique configuration based on `%s'\n",
435 GNUNET_CONFIGURATION_destroy (p->cfg);
441 /* Configure peer with configuration */
442 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
449 LOG (GNUNET_ERROR_TYPE_ERROR,
450 "Testing library failed to create unique configuration based on `%s': `%s'\n",
453 GNUNET_TRANSPORT_TESTING_stop_peer (p);
454 GNUNET_free_non_null (emsg);
457 GNUNET_free_non_null (emsg);
458 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
460 LOG (GNUNET_ERROR_TYPE_ERROR,
461 "Testing library failed to create unique configuration based on `%s'\n",
463 GNUNET_TRANSPORT_TESTING_stop_peer (p);
470 GNUNET_TESTING_peer_get_identity (p->peer,
472 if (0 == memcmp (&dummy,
474 sizeof (struct GNUNET_PeerIdentity)))
476 LOG (GNUNET_ERROR_TYPE_ERROR,
477 "Testing library failed to obtain peer identity for peer %u\n",
479 GNUNET_TRANSPORT_TESTING_stop_peer (p);
482 LOG (GNUNET_ERROR_TYPE_DEBUG,
483 "Peer %u configured with identity `%s'\n",
485 GNUNET_i2s_full (&p->id));
486 p->tmh = GNUNET_TRANSPORT_manipulation_connect (p->cfg);
487 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
494 if ( (NULL == p->th) ||
497 LOG (GNUNET_ERROR_TYPE_ERROR,
498 "Failed to connect to transport service for peer `%s': `%s'\n",
501 GNUNET_TRANSPORT_TESTING_stop_peer (p);
504 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
507 LOG (GNUNET_ERROR_TYPE_ERROR,
508 "Failed to connect to ATS service for peer `%s': `%s'\n",
511 GNUNET_TRANSPORT_TESTING_stop_peer (p);
514 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
515 GNUNET_TRANSPORT_AC_ANY,
518 GNUNET_assert (NULL != p->ghh);
524 * Stops and restarts the given peer, sleeping (!) for 5s in between.
527 * @param restart_cb callback to call when restarted
528 * @param restart_cb_cls callback closure
529 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
532 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
533 GNUNET_SCHEDULER_TaskCallback restart_cb,
534 void *restart_cb_cls)
536 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
537 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "Stopping peer %u (`%s')\n",
543 GNUNET_i2s (&p->id));
546 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
551 GNUNET_TRANSPORT_core_disconnect (p->th);
556 GNUNET_TRANSPORT_manipulation_disconnect (p->tmh);
559 for (cc = p->tth->cc_head; NULL != cc; cc = ccn)
562 if ( (cc->p1 == p) ||
564 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
568 GNUNET_ATS_connectivity_done (p->ats);
572 GNUNET_TESTING_peer_stop (p->peer))
574 LOG (GNUNET_ERROR_TYPE_ERROR,
575 "Failed to stop peer %u (`%s')\n",
577 GNUNET_i2s (&p->id));
578 return GNUNET_SYSERR;
583 LOG (GNUNET_ERROR_TYPE_DEBUG,
584 "Restarting peer %u (`%s')\n",
586 GNUNET_i2s (&p->id));
588 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
590 LOG (GNUNET_ERROR_TYPE_ERROR,
591 "Failed to restart peer %u (`%s')\n",
593 GNUNET_i2s (&p->id));
594 return GNUNET_SYSERR;
597 GNUNET_assert (NULL == p->start_cb);
598 p->start_cb = restart_cb;
599 p->start_cb_cls = restart_cb_cls;
601 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
608 GNUNET_assert (NULL != p->th);
609 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
610 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
611 GNUNET_TRANSPORT_AC_ANY,
614 GNUNET_assert (NULL != p->ghh);
620 * Shutdown the given peer
625 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
627 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
628 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
629 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
631 for (cc = tth->cc_head; NULL != cc; cc = ccn)
634 if ( (cc->p1 == p) ||
636 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
640 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
645 GNUNET_TRANSPORT_manipulation_disconnect (p->tmh);
650 GNUNET_TRANSPORT_core_disconnect (p->th);
656 GNUNET_TESTING_peer_stop (p->peer))
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Testing lib failed to stop peer %u (`%s')\n",
661 GNUNET_i2s (&p->id));
663 GNUNET_TESTING_peer_destroy (p->peer);
668 GNUNET_ATS_connectivity_done (p->ats);
671 if (NULL != p->hello)
673 GNUNET_free (p->hello);
678 GNUNET_CONFIGURATION_destroy (p->cfg);
681 if (NULL != p->handlers)
683 GNUNET_free (p->handlers);
686 GNUNET_CONTAINER_DLL_remove (tth->p_head,
689 LOG (GNUNET_ERROR_TYPE_DEBUG,
690 "Peer %u (`%s') stopped\n",
692 GNUNET_i2s (&p->id));
698 * Function called after the HELLO was passed to the
702 hello_offered (void *cls)
704 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
707 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
714 * Offer the current HELLO of P2 to P1.
716 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
719 offer_hello (void *cls)
721 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
722 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1;
723 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2;
727 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
729 LOG (GNUNET_ERROR_TYPE_DEBUG,
730 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
732 GNUNET_i2s (&p1->id),
735 GNUNET_HELLO_size (cc->p2->hello));
740 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
742 GNUNET_TRANSPORT_offer_hello (cc->p1->cfg,
743 (const struct GNUNET_MessageHeader *) cc->p2->hello,
750 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
752 * Remarks: start_peer's notify_connect callback can be called before.
754 * @param tth transport testing handle
757 * @param cb the callback to call when both peers notified that they are connected
758 * @param cls callback cls
759 * @return a connect request handle
761 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
762 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
763 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
764 GNUNET_SCHEDULER_TaskCallback cb,
767 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
768 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
769 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
772 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
774 if ( (cc->p1 == p1) &&
782 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
792 cc->p1_c = ccn->p1_c;
793 cc->p2_c = ccn->p2_c;
794 cc->connected = ccn->connected;
796 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
799 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
801 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
804 LOG (GNUNET_ERROR_TYPE_DEBUG,
805 "New connect request %p\n",
812 * Cancel the request to connect two peers
813 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
815 * @param tth transport testing handle
816 * @param cc a connect request handle
819 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
821 struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth;
823 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "Canceling connect request!\n");
827 GNUNET_SCHEDULER_cancel (cc->tct);
832 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
835 if (NULL != cc->ats_sh)
837 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
840 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
848 * Clean up the transport testing
850 * @param tth transport testing handle
853 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth)
855 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
856 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct;
857 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
858 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
866 LOG (GNUNET_ERROR_TYPE_ERROR,
867 "Developer forgot to cancel connect request!\n");
868 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
875 LOG (GNUNET_ERROR_TYPE_ERROR,
876 "Developer forgot to stop peer!\n");
877 GNUNET_TRANSPORT_TESTING_stop_peer (p);
880 GNUNET_TESTING_system_destroy (tth->tl_system,
888 * Initialize the transport testing
890 * @return transport testing handle
892 struct GNUNET_TRANSPORT_TESTING_Handle *
893 GNUNET_TRANSPORT_TESTING_init ()
895 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
897 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle);
898 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
902 if (NULL == tth->tl_system)
904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
905 "Failed to initialize testing library!\n");
912 /* end of transport-testing.c */