2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2015 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.
22 * @file transport-testing.c
23 * @brief testing lib for transport service
25 * @author Matthias Wachs
27 #include "transport-testing.h"
30 #define LOG(kind,...) GNUNET_log_from(kind, "transport-testing", __VA_ARGS__)
33 static struct PeerContext *
34 find_peer_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
35 const struct GNUNET_PeerIdentity *peer)
37 struct PeerContext *t = tth->p_head;
41 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
50 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
51 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
52 struct PeerContext *p1,
53 struct PeerContext *p2)
55 GNUNET_assert (tth != NULL);
56 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = tth->cc_head;
60 if ((cc->p1 == p1) && (cc->p2 == p2))
62 if ((cc->p1 == p2) && (cc->p2 == p1))
72 notify_connect (void *cls,
73 const struct GNUNET_PeerIdentity *peer)
75 struct PeerContext *p = cls;
77 struct PeerContext *p2;
79 GNUNET_assert (NULL != p);
80 GNUNET_assert (NULL != p->tth);
81 p2 = find_peer_context (p->tth, peer);
83 p->nc (p->cb_cls, peer);
86 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
88 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
89 LOG (GNUNET_ERROR_TYPE_DEBUG,
90 "Peers %s connected to peer %u (`%s')\n",
96 /* Find ConnectingContext */
97 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = find_connecting_context (p->tth, p, p2);
103 cc->p1_c = GNUNET_YES;
106 cc->p2_c = GNUNET_YES;
108 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
110 cc->cb (cc->p1, cc->p2, cc->cb_cls);
111 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (p->tth, cc);
117 notify_disconnect (void *cls,
118 const struct GNUNET_PeerIdentity *peer)
120 struct PeerContext *p = cls;
122 /* Find PeerContext */
124 struct PeerContext *p2 = NULL;
128 GNUNET_assert (p->tth != NULL);
129 p2 = find_peer_context (p->tth, peer);
136 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
138 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
139 LOG (GNUNET_ERROR_TYPE_DEBUG,
140 "Peers %s disconnected from peer %u (`%s')\n",
143 GNUNET_i2s (&p->id));
149 p->nd (p->cb_cls, peer);
154 notify_receive (void *cls,
155 const struct GNUNET_PeerIdentity *peer,
156 const struct GNUNET_MessageHeader *message)
158 struct PeerContext *p = cls;
163 p->rec (p->cb_cls, peer, message);
168 get_hello (void *cb_cls,
169 const struct GNUNET_MessageHeader *message)
171 struct PeerContext *p = cb_cls;
172 struct GNUNET_PeerIdentity hello_id;
174 GNUNET_assert (message != NULL);
175 GNUNET_assert (GNUNET_OK ==
176 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
177 message, &hello_id));
178 GNUNET_assert (0 == memcmp (&hello_id, &p->id, sizeof (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));
188 p->start_cb (p, p->cb_cls);
195 * Offer the current HELLO of P2 to P1.
197 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest `
200 offer_hello (void *cls);
204 * Function called after the HELLO was passed to the
208 hello_offered (void *cls)
210 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
214 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
221 * Offer the current HELLO of P2 to P1.
223 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest`
226 offer_hello (void *cls)
228 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
229 struct PeerContext *p1 = cc->p1;
230 struct PeerContext *p2 = cc->p2;
234 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
236 LOG (GNUNET_ERROR_TYPE_DEBUG,
237 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
239 GNUNET_i2s (&p1->id),
242 GNUNET_HELLO_size (cc->p2->hello));
247 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
249 GNUNET_TRANSPORT_offer_hello (cc->p1->th,
250 (const struct GNUNET_MessageHeader *) cc->p2->hello,
257 * Start a peer with the given configuration
258 * @param tth the testing handle
259 * @param cfgname configuration file
260 * @param peer_id a unique number to identify the peer
261 * @param rec receive callback
262 * @param nc connect callback
263 * @param nd disconnect callback
264 * @param start_cb start callback
265 * @param cb_cls closure for callback
266 * @return the peer context
269 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
272 GNUNET_TRANSPORT_ReceiveCallback rec,
273 GNUNET_TRANSPORT_NotifyConnect nc,
274 GNUNET_TRANSPORT_NotifyDisconnect nd,
275 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
279 struct GNUNET_PeerIdentity *dummy;
281 GNUNET_assert (NULL != tth);
282 GNUNET_assert (NULL != tth->tl_system);
284 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
286 LOG (GNUNET_ERROR_TYPE_ERROR,
287 "File not found: `%s'\n",
292 struct PeerContext *p = GNUNET_new (struct PeerContext);
293 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
295 /* Create configuration and call testing lib to modify it */
296 p->cfg = GNUNET_CONFIGURATION_create ();
297 GNUNET_assert (GNUNET_OK ==
298 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
299 if (GNUNET_SYSERR == GNUNET_TESTING_configuration_create (tth->tl_system, p->cfg))
301 LOG (GNUNET_ERROR_TYPE_ERROR,
302 "Testing library failed to create unique configuration based on `%s'\n",
309 /* Configure peer with configuration */
310 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
311 p->cfg, p->no, NULL, &emsg);
314 LOG (GNUNET_ERROR_TYPE_ERROR,
315 "Testing library failed to create unique configuration based on `%s': `%s'\n",
318 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
319 GNUNET_free_non_null (emsg);
322 GNUNET_free_non_null (emsg);
323 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
325 LOG (GNUNET_ERROR_TYPE_ERROR,
326 "Testing library failed to create unique configuration based on `%s'\n",
328 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
332 memset(&dummy, '\0', sizeof (dummy));
333 GNUNET_TESTING_peer_get_identity (p->peer, &p->id);
334 if (0 == memcmp (&dummy, &p->id, sizeof (struct GNUNET_PeerIdentity)))
336 LOG (GNUNET_ERROR_TYPE_ERROR,
337 "Testing library failed to obtain peer identity for peer %u\n",
339 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
344 LOG (GNUNET_ERROR_TYPE_DEBUG,
345 "Peer %u configured with identity `%s'\n",
347 GNUNET_i2s_full (&p->id));
354 p->start_cb = start_cb;
360 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
366 LOG (GNUNET_ERROR_TYPE_ERROR,
367 "Failed to connect to transport service for peer `%s': `%s'\n",
370 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
373 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
376 LOG (GNUNET_ERROR_TYPE_ERROR,
377 "Failed to connect to ATS service for peer `%s': `%s'\n",
380 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
383 p->ghh = GNUNET_TRANSPORT_get_hello (p->th,
386 GNUNET_assert (p->ghh != NULL);
393 * Restart the given peer
396 * @param cfgname the cfg file used to restart
397 * @param restart_cb callback to call when restarted
398 * @param cb_cls callback closure
399 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
402 GNUNET_TRANSPORT_TESTING_restart_peer (struct PeerContext *p,
404 GNUNET_TRANSPORT_TESTING_start_cb restart_cb,
407 GNUNET_assert (NULL != p->peer);
409 LOG (GNUNET_ERROR_TYPE_DEBUG,
410 "Restarting peer %u (`%s')\n",
412 GNUNET_i2s (&p->id));
415 LOG (GNUNET_ERROR_TYPE_DEBUG,
416 "Stopping peer %u (`%s')\n",
418 GNUNET_i2s (&p->id));
421 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
426 GNUNET_TRANSPORT_disconnect (p->th);
431 GNUNET_ATS_connectivity_done (p->ats);
435 if (GNUNET_SYSERR == GNUNET_TESTING_peer_stop (p->peer))
437 LOG (GNUNET_ERROR_TYPE_ERROR,
438 "Failed to stop peer %u (`%s')\n",
440 GNUNET_i2s (&p->id));
441 return GNUNET_SYSERR;
447 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
449 LOG (GNUNET_ERROR_TYPE_ERROR,
450 "Failed to restart peer %u (`%s')\n",
452 GNUNET_i2s (&p->id));
453 return GNUNET_SYSERR;
456 GNUNET_assert (NULL == p->start_cb);
457 p->start_cb = restart_cb;
460 p->th = GNUNET_TRANSPORT_connect (p->cfg,
466 GNUNET_assert (NULL != p->th);
467 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
468 p->ghh = GNUNET_TRANSPORT_get_hello (p->th,
471 GNUNET_assert (NULL != p->ghh);
477 * Shutdown the given peer
479 * @param tth testing handle
483 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
484 struct PeerContext *p)
488 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
493 GNUNET_TRANSPORT_disconnect (p->th);
498 if (GNUNET_OK != GNUNET_TESTING_peer_stop (p->peer))
500 LOG (GNUNET_ERROR_TYPE_DEBUG,
501 "Testing lib failed to stop peer %u (`%s') \n", p->no,
502 GNUNET_i2s (&p->id));
504 GNUNET_TESTING_peer_destroy (p->peer);
509 GNUNET_ATS_connectivity_done (p->ats);
512 if (NULL != p->hello)
514 GNUNET_free (p->hello);
519 GNUNET_CONFIGURATION_destroy (p->cfg);
522 GNUNET_CONTAINER_DLL_remove (tth->p_head,
525 LOG (GNUNET_ERROR_TYPE_DEBUG,
526 "Peer %u (`%s') stopped \n",
528 GNUNET_i2s (&p->id));
534 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
536 * Remarks: start_peer's notify_connect callback can be called before.
538 * @param tth transport testing handle
541 * @param cb the callback to call when both peers notified that they are connected
542 * @param cls callback cls
543 * @return a connect request handle
545 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
546 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
547 struct PeerContext *p1,
548 struct PeerContext *p2,
549 GNUNET_TRANSPORT_TESTING_connect_cb cb,
552 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
554 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
562 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
565 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
567 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "New connect request %p\n",
580 * Cancel the request to connect two peers
581 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
583 * @param tth transport testing handle
584 * @param cc a connect request handle
587 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_handle *tth,
588 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Canceling connect request %p!\n",
595 GNUNET_SCHEDULER_cancel (cc->tct);
600 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
603 if (NULL != cc->ats_sh)
605 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
609 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
617 * Clean up the transport testing
618 * @param tth transport testing handle
621 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
623 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = tth->cc_head;
624 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct = NULL;
625 struct PeerContext *p = tth->p_head;
626 struct PeerContext *t = NULL;
628 while (cc != tth->cc_tail)
631 LOG (GNUNET_ERROR_TYPE_ERROR,
632 "Developer forgot to cancel connect request %p!\n",
634 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth,
642 LOG (GNUNET_ERROR_TYPE_ERROR,
643 "Developer forgot to stop peer!\n");
644 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
648 GNUNET_TESTING_system_destroy (tth->tl_system,
657 * Initialize the transport testing
658 * @return transport testing handle
660 struct GNUNET_TRANSPORT_TESTING_handle *
661 GNUNET_TRANSPORT_TESTING_init ()
663 struct GNUNET_TRANSPORT_TESTING_handle *tth;
665 /* prepare hostkeys */
666 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_handle);
668 /* Init testing the testing lib */
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"));
686 * Some utility functions
690 * Removes all directory separators from absolute filename
692 * @param file the absolute file name, e.g. as found in argv[0]
693 * @return extracted file name, has to be freed by caller
696 extract_filename (const char *file)
698 char *pch = GNUNET_strdup (file);
700 char *filename = NULL;
703 if ((strlen (pch) >= 3) && pch[1] == ':')
705 if (NULL != strstr (pch, "\\"))
707 pch = strtok (pch, "\\");
710 pch = strtok (NULL, "\\");
716 if (filename != NULL)
717 pch = filename; /* If we miss the next condition, filename = pch will
721 if (NULL != strstr (pch, "/"))
723 pch = strtok (pch, "/");
726 pch = strtok (NULL, "/");
736 res = GNUNET_strdup (filename);
737 GNUNET_free (backup);
743 * Extracts the test filename from an absolute file name and removes
746 * @param file absolute file name
747 * @param dest where to store result
750 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file,
753 char *filename = extract_filename (file);
754 char *backup = filename;
757 if (filename == NULL)
761 filename = strstr (filename, "tes");
762 if (filename == NULL)
766 if (NULL != (dotexe = strstr (filename, ".exe")))
776 /* create filename */
777 GNUNET_asprintf (dest, "%s", filename);
778 GNUNET_free (backup);
783 * Extracts the filename from an absolute file name and removes the extension
785 * @param file absolute file name
786 * @param dest where to store result
789 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
792 char *src = extract_filename (file);
795 split = strstr (src, ".");
800 GNUNET_asprintf (dest, "%s", src);
806 * Extracts the plugin name from an absolute file name and the test name
808 * @param file absolute file name
809 * @param test test name
810 * @param dest where to store result
813 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
819 char *e = extract_filename (file);
820 char *t = extract_filename (test);
825 filename = strstr (e, "tes");
826 if (NULL == filename)
829 if (NULL != (dotexe = strstr (filename, ".exe")))
833 filename = strstr (filename, t);
834 if (NULL == filename)
837 filename += strlen (t);
838 if ('\0' != *filename)
840 GNUNET_asprintf (dest, "%s", filename);
851 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
852 * if existing ".exe"-prefix and adds the peer-number
854 * @param file filename of the test, e.g. argv[0]
855 * @param dest where to write the filename
856 * @param count peer number
859 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
863 char *filename = extract_filename (file);
864 char *backup = filename;
867 if (NULL == filename)
870 filename = strstr (filename, "tes");
871 if (NULL == filename)
874 if (NULL != (dotexe = strstr (filename, ".exe")))
876 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
877 GNUNET_free (backup);
881 GNUNET_free (backup);
885 /* end of transport-testing.c */