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 start_cb start callback
204 * @param cb_cls closure for callback
205 * @return the peer context
207 struct GNUNET_TRANSPORT_TESTING_PeerContext *
208 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
211 GNUNET_TRANSPORT_ReceiveCallback rec,
212 GNUNET_TRANSPORT_NotifyConnect nc,
213 GNUNET_TRANSPORT_NotifyDisconnect nd,
214 GNUNET_TRANSPORT_TESTING_StartCallback start_cb,
218 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
219 struct GNUNET_PeerIdentity *dummy;
221 if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
223 LOG (GNUNET_ERROR_TYPE_ERROR,
224 "File not found: `%s'\n",
229 p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext);
234 p->start_cb = start_cb;
239 GNUNET_CONTAINER_DLL_insert (tth->p_head,
243 /* Create configuration and call testing lib to modify it */
244 p->cfg = GNUNET_CONFIGURATION_create ();
245 GNUNET_assert (GNUNET_OK ==
246 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
248 GNUNET_TESTING_configuration_create (tth->tl_system,
251 LOG (GNUNET_ERROR_TYPE_ERROR,
252 "Testing library failed to create unique configuration based on `%s'\n",
254 GNUNET_CONFIGURATION_destroy (p->cfg);
260 /* Configure peer with configuration */
261 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
268 LOG (GNUNET_ERROR_TYPE_ERROR,
269 "Testing library failed to create unique configuration based on `%s': `%s'\n",
272 GNUNET_TRANSPORT_TESTING_stop_peer (p);
273 GNUNET_free_non_null (emsg);
276 GNUNET_free_non_null (emsg);
277 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
279 LOG (GNUNET_ERROR_TYPE_ERROR,
280 "Testing library failed to create unique configuration based on `%s'\n",
282 GNUNET_TRANSPORT_TESTING_stop_peer (p);
289 GNUNET_TESTING_peer_get_identity (p->peer,
291 if (0 == memcmp (&dummy,
293 sizeof (struct GNUNET_PeerIdentity)))
295 LOG (GNUNET_ERROR_TYPE_ERROR,
296 "Testing library failed to obtain peer identity for peer %u\n",
298 GNUNET_TRANSPORT_TESTING_stop_peer (p);
301 LOG (GNUNET_ERROR_TYPE_DEBUG,
302 "Peer %u configured with identity `%s'\n",
304 GNUNET_i2s_full (&p->id));
306 p->th = GNUNET_TRANSPORT_connect (p->cfg,
314 LOG (GNUNET_ERROR_TYPE_ERROR,
315 "Failed to connect to transport service for peer `%s': `%s'\n",
318 GNUNET_TRANSPORT_TESTING_stop_peer (p);
321 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
324 LOG (GNUNET_ERROR_TYPE_ERROR,
325 "Failed to connect to ATS service for peer `%s': `%s'\n",
328 GNUNET_TRANSPORT_TESTING_stop_peer (p);
331 p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
334 GNUNET_assert (p->ghh != NULL);
340 * Stops and restarts the given peer, sleeping (!) for 5s in between.
343 * @param restart_cb callback to call when restarted
344 * @param cb_cls callback closure
345 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
348 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
349 GNUNET_TRANSPORT_TESTING_StartCallback restart_cb,
353 LOG (GNUNET_ERROR_TYPE_DEBUG,
354 "Stopping peer %u (`%s')\n",
356 GNUNET_i2s (&p->id));
359 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
364 GNUNET_TRANSPORT_disconnect (p->th);
369 GNUNET_ATS_connectivity_done (p->ats);
373 GNUNET_TESTING_peer_stop (p->peer))
375 LOG (GNUNET_ERROR_TYPE_ERROR,
376 "Failed to stop peer %u (`%s')\n",
378 GNUNET_i2s (&p->id));
379 return GNUNET_SYSERR;
384 LOG (GNUNET_ERROR_TYPE_DEBUG,
385 "Restarting peer %u (`%s')\n",
387 GNUNET_i2s (&p->id));
389 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
391 LOG (GNUNET_ERROR_TYPE_ERROR,
392 "Failed to restart peer %u (`%s')\n",
394 GNUNET_i2s (&p->id));
395 return GNUNET_SYSERR;
398 GNUNET_assert (NULL == p->start_cb);
399 p->start_cb = restart_cb;
402 p->th = GNUNET_TRANSPORT_connect (p->cfg,
408 GNUNET_assert (NULL != p->th);
409 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
410 p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
413 GNUNET_assert (NULL != p->ghh);
419 * Shutdown the given peer
424 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
426 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
430 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
435 GNUNET_TRANSPORT_disconnect (p->th);
441 GNUNET_TESTING_peer_stop (p->peer))
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "Testing lib failed to stop peer %u (`%s')\n",
446 GNUNET_i2s (&p->id));
448 GNUNET_TESTING_peer_destroy (p->peer);
453 GNUNET_ATS_connectivity_done (p->ats);
456 if (NULL != p->hello)
458 GNUNET_free (p->hello);
463 GNUNET_CONFIGURATION_destroy (p->cfg);
466 GNUNET_CONTAINER_DLL_remove (tth->p_head,
469 LOG (GNUNET_ERROR_TYPE_DEBUG,
470 "Peer %u (`%s') stopped\n",
472 GNUNET_i2s (&p->id));
478 * Offer the current HELLO of P2 to P1.
480 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
483 offer_hello (void *cls);
487 * Function called after the HELLO was passed to the
491 hello_offered (void *cls)
493 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
496 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
503 * Offer the current HELLO of P2 to P1.
505 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
508 offer_hello (void *cls)
510 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
511 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1;
512 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2;
516 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
521 GNUNET_i2s (&p1->id),
524 GNUNET_HELLO_size (cc->p2->hello));
529 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
531 GNUNET_TRANSPORT_offer_hello (cc->p1->cfg,
532 (const struct GNUNET_MessageHeader *) cc->p2->hello,
539 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
541 * Remarks: start_peer's notify_connect callback can be called before.
543 * @param tth transport testing handle
546 * @param cb the callback to call when both peers notified that they are connected
547 * @param cls callback cls
548 * @return a connect request handle
550 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
551 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
552 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
553 GNUNET_SCHEDULER_TaskCallback cb,
556 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
557 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
559 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
567 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
570 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
572 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
575 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "New connect request %p\n",
583 * Cancel the request to connect two peers
584 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
586 * @param tth transport testing handle
587 * @param cc a connect request handle
590 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
592 struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth;
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "Canceling connect request!\n");
598 GNUNET_SCHEDULER_cancel (cc->tct);
603 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
606 if (NULL != cc->ats_sh)
608 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
611 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
619 * Clean up the transport testing
621 * @param tth transport testing handle
624 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth)
626 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
627 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct;
628 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
629 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
637 LOG (GNUNET_ERROR_TYPE_ERROR,
638 "Developer forgot to cancel connect request!\n");
639 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
646 LOG (GNUNET_ERROR_TYPE_ERROR,
647 "Developer forgot to stop peer!\n");
648 GNUNET_TRANSPORT_TESTING_stop_peer (p);
651 GNUNET_TESTING_system_destroy (tth->tl_system,
659 * Initialize the transport testing
661 * @return transport testing handle
663 struct GNUNET_TRANSPORT_TESTING_Handle *
664 GNUNET_TRANSPORT_TESTING_init ()
666 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
668 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle);
669 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
673 if (NULL == tth->tl_system)
675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
676 "Failed to initialize testing library!\n");
683 /* end of transport-testing.c */