2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2015 Christian Grothoff (and other contributing authors)
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 `
198 * @param tc scheduler context
201 offer_hello (void *cls,
202 const struct GNUNET_SCHEDULER_TaskContext *tc);
206 * Function called after the HELLO was passed to the
210 hello_offered (void *cls,
211 const struct GNUNET_SCHEDULER_TaskContext *tc)
213 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
217 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
224 * Offer the current HELLO of P2 to P1.
226 * @param cls our `struct GNUNET_TRANSPORT_TESTING_ConnectRequest `
227 * @param tc scheduler context
230 offer_hello (void *cls,
231 const struct GNUNET_SCHEDULER_TaskContext *tc)
233 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
234 struct PeerContext *p1 = cc->p1;
235 struct PeerContext *p2 = cc->p2;
238 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
241 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
243 LOG (GNUNET_ERROR_TYPE_DEBUG,
244 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
246 GNUNET_i2s (&p1->id),
249 GNUNET_HELLO_size (cc->p2->hello));
254 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
256 GNUNET_TRANSPORT_offer_hello (cc->p1->th,
257 (const struct GNUNET_MessageHeader *) cc->p2->hello,
264 * Start a peer with the given configuration
265 * @param tth the testing handle
266 * @param cfgname configuration file
267 * @param peer_id a unique number to identify the peer
268 * @param rec receive callback
269 * @param nc connect callback
270 * @param nd disconnect callback
271 * @param start_cb start callback
272 * @param cb_cls closure for callback
273 * @return the peer context
276 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
279 GNUNET_TRANSPORT_ReceiveCallback rec,
280 GNUNET_TRANSPORT_NotifyConnect nc,
281 GNUNET_TRANSPORT_NotifyDisconnect nd,
282 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
286 struct GNUNET_PeerIdentity *dummy;
288 GNUNET_assert (NULL != tth);
289 GNUNET_assert (NULL != tth->tl_system);
291 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
293 LOG (GNUNET_ERROR_TYPE_ERROR,
294 "File not found: `%s'\n",
299 struct PeerContext *p = GNUNET_new (struct PeerContext);
300 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
302 /* Create configuration and call testing lib to modify it */
303 p->cfg = GNUNET_CONFIGURATION_create ();
304 GNUNET_assert (GNUNET_OK ==
305 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
306 if (GNUNET_SYSERR == GNUNET_TESTING_configuration_create (tth->tl_system, p->cfg))
308 LOG (GNUNET_ERROR_TYPE_ERROR,
309 "Testing library failed to create unique configuration based on `%s'\n",
316 /* Configure peer with configuration */
317 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
318 p->cfg, p->no, NULL, &emsg);
321 LOG (GNUNET_ERROR_TYPE_ERROR,
322 "Testing library failed to create unique configuration based on `%s': `%s'\n",
325 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
326 GNUNET_free_non_null (emsg);
329 GNUNET_free_non_null (emsg);
330 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
332 LOG (GNUNET_ERROR_TYPE_ERROR,
333 "Testing library failed to create unique configuration based on `%s'\n",
335 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
339 memset(&dummy, '\0', sizeof (dummy));
340 GNUNET_TESTING_peer_get_identity (p->peer, &p->id);
341 if (0 == memcmp (&dummy, &p->id, sizeof (struct GNUNET_PeerIdentity)))
343 LOG (GNUNET_ERROR_TYPE_ERROR,
344 "Testing library failed to obtain peer identity for peer %u\n",
346 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
351 LOG (GNUNET_ERROR_TYPE_DEBUG,
352 "Peer %u configured with identity `%s'\n",
354 GNUNET_i2s_full (&p->id));
361 p->start_cb = start_cb;
367 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
373 LOG (GNUNET_ERROR_TYPE_ERROR,
374 "Failed to connect to transport service for peer `%s': `%s'\n",
377 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
380 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
383 LOG (GNUNET_ERROR_TYPE_ERROR,
384 "Failed to connect to ATS service for peer `%s': `%s'\n",
387 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
390 p->ghh = GNUNET_TRANSPORT_get_hello (p->th,
393 GNUNET_assert (p->ghh != NULL);
400 * Restart the given peer
403 * @param cfgname the cfg file used to restart
404 * @param restart_cb callback to call when restarted
405 * @param cb_cls callback closure
406 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
409 GNUNET_TRANSPORT_TESTING_restart_peer (struct PeerContext *p,
411 GNUNET_TRANSPORT_TESTING_start_cb restart_cb,
414 GNUNET_assert (NULL != p->peer);
416 LOG (GNUNET_ERROR_TYPE_DEBUG,
417 "Restarting peer %u (`%s')\n",
419 GNUNET_i2s (&p->id));
422 LOG (GNUNET_ERROR_TYPE_DEBUG,
423 "Stopping peer %u (`%s')\n",
425 GNUNET_i2s (&p->id));
428 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
433 GNUNET_TRANSPORT_disconnect (p->th);
438 GNUNET_ATS_connectivity_done (p->ats);
442 if (GNUNET_SYSERR == GNUNET_TESTING_peer_stop (p->peer))
444 LOG (GNUNET_ERROR_TYPE_ERROR,
445 "Failed to stop peer %u (`%s')\n",
447 GNUNET_i2s (&p->id));
448 return GNUNET_SYSERR;
454 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
456 LOG (GNUNET_ERROR_TYPE_ERROR,
457 "Failed to restart peer %u (`%s')\n",
459 GNUNET_i2s (&p->id));
460 return GNUNET_SYSERR;
463 GNUNET_assert (NULL == p->start_cb);
464 p->start_cb = restart_cb;
467 p->th = GNUNET_TRANSPORT_connect (p->cfg,
473 GNUNET_assert (NULL != p->th);
474 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
475 p->ghh = GNUNET_TRANSPORT_get_hello (p->th,
478 GNUNET_assert (NULL != p->ghh);
484 * Shutdown the given peer
486 * @param tth testing handle
490 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
491 struct PeerContext *p)
495 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
500 GNUNET_TRANSPORT_disconnect (p->th);
505 if (GNUNET_OK != GNUNET_TESTING_peer_stop (p->peer))
507 LOG (GNUNET_ERROR_TYPE_DEBUG,
508 "Testing lib failed to stop peer %u (`%s') \n", p->no,
509 GNUNET_i2s (&p->id));
511 GNUNET_TESTING_peer_destroy (p->peer);
516 GNUNET_ATS_connectivity_done (p->ats);
519 if (NULL != p->hello)
521 GNUNET_free (p->hello);
526 GNUNET_CONFIGURATION_destroy (p->cfg);
529 GNUNET_CONTAINER_DLL_remove (tth->p_head,
532 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 "Peer %u (`%s') stopped \n",
535 GNUNET_i2s (&p->id));
541 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
543 * Remarks: start_peer's notify_connect callback can be called before.
545 * @param tth transport testing handle
548 * @param cb the callback to call when both peers notified that they are connected
549 * @param cls callback cls
550 * @return a connect request handle
552 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
553 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
554 struct PeerContext *p1,
555 struct PeerContext *p2,
556 GNUNET_TRANSPORT_TESTING_connect_cb cb,
559 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
561 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
569 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
572 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
574 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 "New connect request %p\n",
587 * Cancel the request to connect two peers
588 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
590 * @param tth transport testing handle
591 * @param cc a connect request handle
594 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_handle *tth,
595 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
597 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 "Canceling connect request %p!\n",
602 GNUNET_SCHEDULER_cancel (cc->tct);
607 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
610 if (NULL != cc->ats_sh)
612 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
616 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
624 * Clean up the transport testing
625 * @param tth transport testing handle
628 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
630 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = tth->cc_head;
631 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct = NULL;
632 struct PeerContext *p = tth->p_head;
633 struct PeerContext *t = NULL;
635 while (cc != tth->cc_tail)
638 LOG (GNUNET_ERROR_TYPE_ERROR,
639 "Developer forgot to cancel connect request %p!\n",
641 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth,
649 LOG (GNUNET_ERROR_TYPE_ERROR,
650 "Developer forgot to stop peer!\n");
651 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
655 GNUNET_TESTING_system_destroy (tth->tl_system,
664 * Initialize the transport testing
665 * @return transport testing handle
667 struct GNUNET_TRANSPORT_TESTING_handle *
668 GNUNET_TRANSPORT_TESTING_init ()
670 struct GNUNET_TRANSPORT_TESTING_handle *tth;
672 /* prepare hostkeys */
673 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_handle);
675 /* Init testing the testing lib */
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"));
693 * Some utility functions
697 * Removes all directory separators from absolute filename
699 * @param file the absolute file name, e.g. as found in argv[0]
700 * @return extracted file name, has to be freed by caller
703 extract_filename (const char *file)
705 char *pch = GNUNET_strdup (file);
707 char *filename = NULL;
710 if ((strlen (pch) >= 3) && pch[1] == ':')
712 if (NULL != strstr (pch, "\\"))
714 pch = strtok (pch, "\\");
717 pch = strtok (NULL, "\\");
723 if (filename != NULL)
724 pch = filename; /* If we miss the next condition, filename = pch will
728 if (NULL != strstr (pch, "/"))
730 pch = strtok (pch, "/");
733 pch = strtok (NULL, "/");
743 res = GNUNET_strdup (filename);
744 GNUNET_free (backup);
750 * Extracts the test filename from an absolute file name and removes
753 * @param file absolute file name
754 * @param dest where to store result
757 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file,
760 char *filename = extract_filename (file);
761 char *backup = filename;
764 if (filename == NULL)
768 filename = strstr (filename, "tes");
769 if (filename == NULL)
773 if (NULL != (dotexe = strstr (filename, ".exe")))
783 /* create filename */
784 GNUNET_asprintf (dest, "%s", filename);
785 GNUNET_free (backup);
790 * Extracts the filename from an absolute file name and removes the extension
792 * @param file absolute file name
793 * @param dest where to store result
796 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
799 char *src = extract_filename (file);
802 split = strstr (src, ".");
807 GNUNET_asprintf (dest, "%s", src);
813 * Extracts the plugin name from an absolute file name and the test name
815 * @param file absolute file name
816 * @param test test name
817 * @param dest where to store result
820 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
826 char *e = extract_filename (file);
827 char *t = extract_filename (test);
832 filename = strstr (e, "tes");
833 if (NULL == filename)
836 if (NULL != (dotexe = strstr (filename, ".exe")))
840 filename = strstr (filename, t);
841 if (NULL == filename)
844 filename += strlen (t);
845 if ('\0' != *filename)
847 GNUNET_asprintf (dest, "%s", filename);
858 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
859 * if existing ".exe"-prefix and adds the peer-number
861 * @param file filename of the test, e.g. argv[0]
862 * @param dest where to write the filename
863 * @param count peer number
866 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
870 char *filename = extract_filename (file);
871 char *backup = filename;
874 if (NULL == filename)
877 filename = strstr (filename, "tes");
878 if (NULL == filename)
881 if (NULL != (dotexe = strstr (filename, ".exe")))
883 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
884 GNUNET_free (backup);
888 GNUNET_free (backup);
892 /* end of transport-testing.c */