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 notify_connecting_context (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
49 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
50 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2)
52 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
53 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ccn;
55 for (cc = tth->cc_head; NULL != cc; cc = ccn)
58 if ( (cc->p1 == p1) &&
60 cc->p1_c = GNUNET_YES;
61 if ( (cc->p1 == p2) &&
63 cc->p2_c = GNUNET_YES;
64 if ( (cc->p1_c == GNUNET_YES) &&
65 (cc->p2_c == GNUNET_YES) )
68 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
75 notify_connect (void *cls,
76 const struct GNUNET_PeerIdentity *peer)
78 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
80 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
82 p2 = find_peer_context (p->tth,
89 GNUNET_asprintf (&p2_s,
92 GNUNET_i2s (&p2->id));
94 GNUNET_asprintf (&p2_s,
97 LOG (GNUNET_ERROR_TYPE_DEBUG,
98 "Peers %s connected to peer %u (`%s')\n",
101 GNUNET_i2s (&p->id));
103 notify_connecting_context (p->tth,
110 notify_disconnect (void *cls,
111 const struct GNUNET_PeerIdentity *peer)
113 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
115 /* Find PeerContext */
117 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL;
121 p2 = find_peer_context (p->tth,
127 GNUNET_asprintf (&p2_s,
130 GNUNET_i2s (&p2->id));
132 GNUNET_asprintf (&p2_s,
135 LOG (GNUNET_ERROR_TYPE_DEBUG,
136 "Peers %s disconnected from peer %u (`%s')\n",
139 GNUNET_i2s (&p->id));
151 notify_receive (void *cls,
152 const struct GNUNET_PeerIdentity *peer,
153 const struct GNUNET_MessageHeader *message)
155 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
167 get_hello (void *cb_cls,
168 const struct GNUNET_MessageHeader *message)
170 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls;
171 struct GNUNET_PeerIdentity hello_id;
173 GNUNET_assert (GNUNET_OK ==
174 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message,
176 GNUNET_assert (0 == memcmp (&hello_id,
179 GNUNET_free_non_null (p->hello);
180 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
182 if (NULL != p->start_cb)
184 LOG (GNUNET_ERROR_TYPE_DEBUG,
185 "Peer %u (`%s') successfully started\n",
187 GNUNET_i2s (&p->id));
196 * Start a peer with the given configuration
197 * @param tth the testing handle
198 * @param cfgname configuration file
199 * @param peer_id a unique number to identify the peer
200 * @param rec receive callback
201 * @param nc connect callback
202 * @param nd disconnect callback
203 * @param cb_cls closure for callback
204 * @param start_cb start callback
205 * @param start_cb_cls closure for callback
206 * @return the peer context
208 struct GNUNET_TRANSPORT_TESTING_PeerContext *
209 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
212 GNUNET_TRANSPORT_ReceiveCallback rec,
213 GNUNET_TRANSPORT_NotifyConnect nc,
214 GNUNET_TRANSPORT_NotifyDisconnect nd,
216 GNUNET_TRANSPORT_TESTING_StartCallback start_cb,
220 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
221 struct GNUNET_PeerIdentity *dummy;
223 if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
225 LOG (GNUNET_ERROR_TYPE_ERROR,
226 "File not found: `%s'\n",
231 p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext);
240 p->start_cb = start_cb;
241 p->start_cb_cls = start_cb_cls;
242 GNUNET_CONTAINER_DLL_insert (tth->p_head,
246 /* Create configuration and call testing lib to modify it */
247 p->cfg = GNUNET_CONFIGURATION_create ();
248 GNUNET_assert (GNUNET_OK ==
249 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
251 GNUNET_TESTING_configuration_create (tth->tl_system,
254 LOG (GNUNET_ERROR_TYPE_ERROR,
255 "Testing library failed to create unique configuration based on `%s'\n",
257 GNUNET_CONFIGURATION_destroy (p->cfg);
263 /* Configure peer with configuration */
264 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
271 LOG (GNUNET_ERROR_TYPE_ERROR,
272 "Testing library failed to create unique configuration based on `%s': `%s'\n",
275 GNUNET_TRANSPORT_TESTING_stop_peer (p);
276 GNUNET_free_non_null (emsg);
279 GNUNET_free_non_null (emsg);
280 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
282 LOG (GNUNET_ERROR_TYPE_ERROR,
283 "Testing library failed to create unique configuration based on `%s'\n",
285 GNUNET_TRANSPORT_TESTING_stop_peer (p);
292 GNUNET_TESTING_peer_get_identity (p->peer,
294 if (0 == memcmp (&dummy,
296 sizeof (struct GNUNET_PeerIdentity)))
298 LOG (GNUNET_ERROR_TYPE_ERROR,
299 "Testing library failed to obtain peer identity for peer %u\n",
301 GNUNET_TRANSPORT_TESTING_stop_peer (p);
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "Peer %u configured with identity `%s'\n",
307 GNUNET_i2s_full (&p->id));
309 p->th = GNUNET_TRANSPORT_connect (p->cfg,
317 LOG (GNUNET_ERROR_TYPE_ERROR,
318 "Failed to connect to transport service for peer `%s': `%s'\n",
321 GNUNET_TRANSPORT_TESTING_stop_peer (p);
324 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
327 LOG (GNUNET_ERROR_TYPE_ERROR,
328 "Failed to connect to ATS service for peer `%s': `%s'\n",
331 GNUNET_TRANSPORT_TESTING_stop_peer (p);
334 p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
337 GNUNET_assert (p->ghh != NULL);
343 * Stops and restarts the given peer, sleeping (!) for 5s in between.
346 * @param restart_cb callback to call when restarted
347 * @param restart_cb_cls callback closure
348 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
351 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
352 GNUNET_TRANSPORT_TESTING_StartCallback restart_cb,
353 void *restart_cb_cls)
356 LOG (GNUNET_ERROR_TYPE_DEBUG,
357 "Stopping peer %u (`%s')\n",
359 GNUNET_i2s (&p->id));
362 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
367 GNUNET_TRANSPORT_disconnect (p->th);
372 GNUNET_ATS_connectivity_done (p->ats);
376 GNUNET_TESTING_peer_stop (p->peer))
378 LOG (GNUNET_ERROR_TYPE_ERROR,
379 "Failed to stop peer %u (`%s')\n",
381 GNUNET_i2s (&p->id));
382 return GNUNET_SYSERR;
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Restarting peer %u (`%s')\n",
390 GNUNET_i2s (&p->id));
392 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
394 LOG (GNUNET_ERROR_TYPE_ERROR,
395 "Failed to restart peer %u (`%s')\n",
397 GNUNET_i2s (&p->id));
398 return GNUNET_SYSERR;
401 GNUNET_assert (NULL == p->start_cb);
402 p->start_cb = restart_cb;
403 p->start_cb_cls = restart_cb_cls;
405 p->th = GNUNET_TRANSPORT_connect (p->cfg,
411 GNUNET_assert (NULL != p->th);
412 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
413 p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
416 GNUNET_assert (NULL != p->ghh);
422 * Shutdown the given peer
427 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
429 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
433 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
438 GNUNET_TRANSPORT_disconnect (p->th);
444 GNUNET_TESTING_peer_stop (p->peer))
446 LOG (GNUNET_ERROR_TYPE_DEBUG,
447 "Testing lib failed to stop peer %u (`%s')\n",
449 GNUNET_i2s (&p->id));
451 GNUNET_TESTING_peer_destroy (p->peer);
456 GNUNET_ATS_connectivity_done (p->ats);
459 if (NULL != p->hello)
461 GNUNET_free (p->hello);
466 GNUNET_CONFIGURATION_destroy (p->cfg);
469 GNUNET_CONTAINER_DLL_remove (tth->p_head,
472 LOG (GNUNET_ERROR_TYPE_DEBUG,
473 "Peer %u (`%s') stopped\n",
475 GNUNET_i2s (&p->id));
481 * Offer the current HELLO of P2 to P1.
483 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
486 offer_hello (void *cls);
490 * Function called after the HELLO was passed to the
494 hello_offered (void *cls)
496 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
499 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
506 * Offer the current HELLO of P2 to P1.
508 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
511 offer_hello (void *cls)
513 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
514 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1;
515 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2;
519 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
524 GNUNET_i2s (&p1->id),
527 GNUNET_HELLO_size (cc->p2->hello));
532 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
534 GNUNET_TRANSPORT_offer_hello (cc->p1->cfg,
535 (const struct GNUNET_MessageHeader *) cc->p2->hello,
542 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
544 * Remarks: start_peer's notify_connect callback can be called before.
546 * @param tth transport testing handle
549 * @param cb the callback to call when both peers notified that they are connected
550 * @param cls callback cls
551 * @return a connect request handle
553 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
554 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
555 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
556 GNUNET_SCHEDULER_TaskCallback cb,
559 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
560 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
562 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
570 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
573 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
575 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 "New connect request %p\n",
586 * Cancel the request to connect two peers
587 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
589 * @param tth transport testing handle
590 * @param cc a connect request handle
593 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
595 struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth;
597 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 "Canceling connect request!\n");
601 GNUNET_SCHEDULER_cancel (cc->tct);
606 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
609 if (NULL != cc->ats_sh)
611 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
614 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
622 * Clean up the transport testing
624 * @param tth transport testing handle
627 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth)
629 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
630 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct;
631 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
632 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
640 LOG (GNUNET_ERROR_TYPE_ERROR,
641 "Developer forgot to cancel connect request!\n");
642 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
649 LOG (GNUNET_ERROR_TYPE_ERROR,
650 "Developer forgot to stop peer!\n");
651 GNUNET_TRANSPORT_TESTING_stop_peer (p);
654 GNUNET_TESTING_system_destroy (tth->tl_system,
662 * Initialize the transport testing
664 * @return transport testing handle
666 struct GNUNET_TRANSPORT_TESTING_Handle *
667 GNUNET_TRANSPORT_TESTING_init ()
669 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
671 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle);
672 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
676 if (NULL == tth->tl_system)
678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
679 "Failed to initialize testing library!\n");
686 /* end of transport-testing.c */