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)))
47 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
48 find_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;
54 for (cc = tth->cc_head; NULL != cc; cc = cc->next)
56 if ( (cc->p1 == p1) &&
59 if ( (cc->p1 == p2) &&
68 notify_connect (void *cls,
69 const struct GNUNET_PeerIdentity *peer)
71 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
73 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
74 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
76 p2 = find_peer_context (p->tth,
83 GNUNET_asprintf (&p2_s,
86 GNUNET_i2s (&p2->id));
88 GNUNET_asprintf (&p2_s,
91 LOG (GNUNET_ERROR_TYPE_DEBUG,
92 "Peers %s connected to peer %u (`%s')\n",
98 /* Find ConnectingContext */
99 cc = find_connecting_context (p->tth,
105 cc->p1_c = GNUNET_YES;
107 cc->p2_c = GNUNET_YES;
109 if ( (cc->p1_c == GNUNET_YES) &&
110 (cc->p2_c == GNUNET_YES) )
113 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
119 notify_disconnect (void *cls,
120 const struct GNUNET_PeerIdentity *peer)
122 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
124 /* Find PeerContext */
126 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = NULL;
130 p2 = find_peer_context (p->tth,
136 GNUNET_asprintf (&p2_s,
139 GNUNET_i2s (&p2->id));
141 GNUNET_asprintf (&p2_s,
144 LOG (GNUNET_ERROR_TYPE_DEBUG,
145 "Peers %s disconnected from peer %u (`%s')\n",
148 GNUNET_i2s (&p->id));
160 notify_receive (void *cls,
161 const struct GNUNET_PeerIdentity *peer,
162 const struct GNUNET_MessageHeader *message)
164 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
176 get_hello (void *cb_cls,
177 const struct GNUNET_MessageHeader *message)
179 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cb_cls;
180 struct GNUNET_PeerIdentity hello_id;
182 GNUNET_assert (GNUNET_OK ==
183 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message,
185 GNUNET_assert (0 == memcmp (&hello_id,
188 GNUNET_free_non_null (p->hello);
189 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
191 if (NULL != p->start_cb)
193 LOG (GNUNET_ERROR_TYPE_DEBUG,
194 "Peer %u (`%s') successfully started\n",
196 GNUNET_i2s (&p->id));
205 * Start a peer with the given configuration
206 * @param tth the testing handle
207 * @param cfgname configuration file
208 * @param peer_id a unique number to identify the peer
209 * @param rec receive callback
210 * @param nc connect callback
211 * @param nd disconnect callback
212 * @param start_cb start callback
213 * @param cb_cls closure for callback
214 * @return the peer context
216 struct GNUNET_TRANSPORT_TESTING_PeerContext *
217 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
220 GNUNET_TRANSPORT_ReceiveCallback rec,
221 GNUNET_TRANSPORT_NotifyConnect nc,
222 GNUNET_TRANSPORT_NotifyDisconnect nd,
223 GNUNET_TRANSPORT_TESTING_StartCallback start_cb,
227 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
228 struct GNUNET_PeerIdentity *dummy;
230 if (GNUNET_NO == GNUNET_DISK_file_test (cfgname))
232 LOG (GNUNET_ERROR_TYPE_ERROR,
233 "File not found: `%s'\n",
238 p = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_PeerContext);
243 p->start_cb = start_cb;
248 GNUNET_CONTAINER_DLL_insert (tth->p_head,
252 /* Create configuration and call testing lib to modify it */
253 p->cfg = GNUNET_CONFIGURATION_create ();
254 GNUNET_assert (GNUNET_OK ==
255 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
257 GNUNET_TESTING_configuration_create (tth->tl_system,
260 LOG (GNUNET_ERROR_TYPE_ERROR,
261 "Testing library failed to create unique configuration based on `%s'\n",
263 GNUNET_CONFIGURATION_destroy (p->cfg);
269 /* Configure peer with configuration */
270 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
277 LOG (GNUNET_ERROR_TYPE_ERROR,
278 "Testing library failed to create unique configuration based on `%s': `%s'\n",
281 GNUNET_TRANSPORT_TESTING_stop_peer (p);
282 GNUNET_free_non_null (emsg);
285 GNUNET_free_non_null (emsg);
286 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
288 LOG (GNUNET_ERROR_TYPE_ERROR,
289 "Testing library failed to create unique configuration based on `%s'\n",
291 GNUNET_TRANSPORT_TESTING_stop_peer (p);
298 GNUNET_TESTING_peer_get_identity (p->peer,
300 if (0 == memcmp (&dummy,
302 sizeof (struct GNUNET_PeerIdentity)))
304 LOG (GNUNET_ERROR_TYPE_ERROR,
305 "Testing library failed to obtain peer identity for peer %u\n",
307 GNUNET_TRANSPORT_TESTING_stop_peer (p);
310 LOG (GNUNET_ERROR_TYPE_DEBUG,
311 "Peer %u configured with identity `%s'\n",
313 GNUNET_i2s_full (&p->id));
315 p->th = GNUNET_TRANSPORT_connect (p->cfg,
323 LOG (GNUNET_ERROR_TYPE_ERROR,
324 "Failed to connect to transport service for peer `%s': `%s'\n",
327 GNUNET_TRANSPORT_TESTING_stop_peer (p);
330 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
333 LOG (GNUNET_ERROR_TYPE_ERROR,
334 "Failed to connect to ATS service for peer `%s': `%s'\n",
337 GNUNET_TRANSPORT_TESTING_stop_peer (p);
340 p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
343 GNUNET_assert (p->ghh != NULL);
349 * Stops and restarts the given peer, sleeping (!) for 5s in between.
352 * @param restart_cb callback to call when restarted
353 * @param cb_cls callback closure
354 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
357 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
358 GNUNET_TRANSPORT_TESTING_StartCallback restart_cb,
362 LOG (GNUNET_ERROR_TYPE_DEBUG,
363 "Stopping peer %u (`%s')\n",
365 GNUNET_i2s (&p->id));
368 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
373 GNUNET_TRANSPORT_disconnect (p->th);
378 GNUNET_ATS_connectivity_done (p->ats);
382 GNUNET_TESTING_peer_stop (p->peer))
384 LOG (GNUNET_ERROR_TYPE_ERROR,
385 "Failed to stop peer %u (`%s')\n",
387 GNUNET_i2s (&p->id));
388 return GNUNET_SYSERR;
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 "Restarting peer %u (`%s')\n",
396 GNUNET_i2s (&p->id));
398 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
400 LOG (GNUNET_ERROR_TYPE_ERROR,
401 "Failed to restart peer %u (`%s')\n",
403 GNUNET_i2s (&p->id));
404 return GNUNET_SYSERR;
407 GNUNET_assert (NULL == p->start_cb);
408 p->start_cb = restart_cb;
411 p->th = GNUNET_TRANSPORT_connect (p->cfg,
417 GNUNET_assert (NULL != p->th);
418 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
419 p->ghh = GNUNET_TRANSPORT_get_hello (p->cfg,
422 GNUNET_assert (NULL != p->ghh);
428 * Shutdown the given peer
433 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
435 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p->tth;
439 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
444 GNUNET_TRANSPORT_disconnect (p->th);
450 GNUNET_TESTING_peer_stop (p->peer))
452 LOG (GNUNET_ERROR_TYPE_DEBUG,
453 "Testing lib failed to stop peer %u (`%s')\n",
455 GNUNET_i2s (&p->id));
457 GNUNET_TESTING_peer_destroy (p->peer);
462 GNUNET_ATS_connectivity_done (p->ats);
465 if (NULL != p->hello)
467 GNUNET_free (p->hello);
472 GNUNET_CONFIGURATION_destroy (p->cfg);
475 GNUNET_CONTAINER_DLL_remove (tth->p_head,
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "Peer %u (`%s') stopped\n",
481 GNUNET_i2s (&p->id));
487 * Offer the current HELLO of P2 to P1.
489 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
492 offer_hello (void *cls);
496 * Function called after the HELLO was passed to the
500 hello_offered (void *cls)
502 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
505 cc->tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
512 * Offer the current HELLO of P2 to P1.
514 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
517 offer_hello (void *cls)
519 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
520 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1 = cc->p1;
521 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2 = cc->p2;
525 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
527 LOG (GNUNET_ERROR_TYPE_DEBUG,
528 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
530 GNUNET_i2s (&p1->id),
533 GNUNET_HELLO_size (cc->p2->hello));
538 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
540 GNUNET_TRANSPORT_offer_hello (cc->p1->cfg,
541 (const struct GNUNET_MessageHeader *) cc->p2->hello,
548 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
550 * Remarks: start_peer's notify_connect callback can be called before.
552 * @param tth transport testing handle
555 * @param cb the callback to call when both peers notified that they are connected
556 * @param cls callback cls
557 * @return a connect request handle
559 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
560 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
561 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
562 GNUNET_SCHEDULER_TaskCallback cb,
565 struct GNUNET_TRANSPORT_TESTING_Handle *tth = p1->tth;
566 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
568 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
576 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
579 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
581 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 "New connect request %p\n",
592 * Cancel the request to connect two peers
593 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
595 * @param tth transport testing handle
596 * @param cc a connect request handle
599 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
601 struct GNUNET_TRANSPORT_TESTING_Handle *tth = cc->p1->tth;
603 LOG (GNUNET_ERROR_TYPE_DEBUG,
604 "Canceling connect request!\n");
607 GNUNET_SCHEDULER_cancel (cc->tct);
612 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
615 if (NULL != cc->ats_sh)
617 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
620 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
628 * Clean up the transport testing
630 * @param tth transport testing handle
633 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth)
635 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
636 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct;
637 struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
638 struct GNUNET_TRANSPORT_TESTING_PeerContext *t;
644 LOG (GNUNET_ERROR_TYPE_ERROR,
645 "Developer forgot to cancel connect request!\n");
646 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
653 LOG (GNUNET_ERROR_TYPE_ERROR,
654 "Developer forgot to stop peer!\n");
655 GNUNET_TRANSPORT_TESTING_stop_peer (p);
658 GNUNET_TESTING_system_destroy (tth->tl_system,
666 * Initialize the transport testing
668 * @return transport testing handle
670 struct GNUNET_TRANSPORT_TESTING_Handle *
671 GNUNET_TRANSPORT_TESTING_init ()
673 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
675 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_Handle);
676 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
680 if (NULL == tth->tl_system)
682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
683 "Failed to initialize testing library!\n");
690 /* end of transport-testing.c */