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;
231 const struct GNUNET_SCHEDULER_TaskContext *tc;
234 tc = GNUNET_SCHEDULER_get_task_context ();
235 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
238 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
240 LOG (GNUNET_ERROR_TYPE_DEBUG,
241 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
243 GNUNET_i2s (&p1->id),
246 GNUNET_HELLO_size (cc->p2->hello));
251 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
253 GNUNET_TRANSPORT_offer_hello (cc->p1->th,
254 (const struct GNUNET_MessageHeader *) cc->p2->hello,
261 * Start a peer with the given configuration
262 * @param tth the testing handle
263 * @param cfgname configuration file
264 * @param peer_id a unique number to identify the peer
265 * @param rec receive callback
266 * @param nc connect callback
267 * @param nd disconnect callback
268 * @param start_cb start callback
269 * @param cb_cls closure for callback
270 * @return the peer context
273 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
276 GNUNET_TRANSPORT_ReceiveCallback rec,
277 GNUNET_TRANSPORT_NotifyConnect nc,
278 GNUNET_TRANSPORT_NotifyDisconnect nd,
279 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
283 struct GNUNET_PeerIdentity *dummy;
285 GNUNET_assert (NULL != tth);
286 GNUNET_assert (NULL != tth->tl_system);
288 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
290 LOG (GNUNET_ERROR_TYPE_ERROR,
291 "File not found: `%s'\n",
296 struct PeerContext *p = GNUNET_new (struct PeerContext);
297 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
299 /* Create configuration and call testing lib to modify it */
300 p->cfg = GNUNET_CONFIGURATION_create ();
301 GNUNET_assert (GNUNET_OK ==
302 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
303 if (GNUNET_SYSERR == GNUNET_TESTING_configuration_create (tth->tl_system, p->cfg))
305 LOG (GNUNET_ERROR_TYPE_ERROR,
306 "Testing library failed to create unique configuration based on `%s'\n",
313 /* Configure peer with configuration */
314 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system,
315 p->cfg, p->no, NULL, &emsg);
318 LOG (GNUNET_ERROR_TYPE_ERROR,
319 "Testing library failed to create unique configuration based on `%s': `%s'\n",
322 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
323 GNUNET_free_non_null (emsg);
326 GNUNET_free_non_null (emsg);
327 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
329 LOG (GNUNET_ERROR_TYPE_ERROR,
330 "Testing library failed to create unique configuration based on `%s'\n",
332 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
336 memset(&dummy, '\0', sizeof (dummy));
337 GNUNET_TESTING_peer_get_identity (p->peer, &p->id);
338 if (0 == memcmp (&dummy, &p->id, sizeof (struct GNUNET_PeerIdentity)))
340 LOG (GNUNET_ERROR_TYPE_ERROR,
341 "Testing library failed to obtain peer identity for peer %u\n",
343 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
348 LOG (GNUNET_ERROR_TYPE_DEBUG,
349 "Peer %u configured with identity `%s'\n",
351 GNUNET_i2s_full (&p->id));
358 p->start_cb = start_cb;
364 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
370 LOG (GNUNET_ERROR_TYPE_ERROR,
371 "Failed to connect to transport service for peer `%s': `%s'\n",
374 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
377 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
380 LOG (GNUNET_ERROR_TYPE_ERROR,
381 "Failed to connect to ATS service for peer `%s': `%s'\n",
384 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
387 p->ghh = GNUNET_TRANSPORT_get_hello (p->th,
390 GNUNET_assert (p->ghh != NULL);
397 * Restart the given peer
400 * @param cfgname the cfg file used to restart
401 * @param restart_cb callback to call when restarted
402 * @param cb_cls callback closure
403 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
406 GNUNET_TRANSPORT_TESTING_restart_peer (struct PeerContext *p,
408 GNUNET_TRANSPORT_TESTING_start_cb restart_cb,
411 GNUNET_assert (NULL != p->peer);
413 LOG (GNUNET_ERROR_TYPE_DEBUG,
414 "Restarting peer %u (`%s')\n",
416 GNUNET_i2s (&p->id));
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "Stopping peer %u (`%s')\n",
422 GNUNET_i2s (&p->id));
425 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
430 GNUNET_TRANSPORT_disconnect (p->th);
435 GNUNET_ATS_connectivity_done (p->ats);
439 if (GNUNET_SYSERR == GNUNET_TESTING_peer_stop (p->peer))
441 LOG (GNUNET_ERROR_TYPE_ERROR,
442 "Failed to stop peer %u (`%s')\n",
444 GNUNET_i2s (&p->id));
445 return GNUNET_SYSERR;
451 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start (p->peer))
453 LOG (GNUNET_ERROR_TYPE_ERROR,
454 "Failed to restart peer %u (`%s')\n",
456 GNUNET_i2s (&p->id));
457 return GNUNET_SYSERR;
460 GNUNET_assert (NULL == p->start_cb);
461 p->start_cb = restart_cb;
464 p->th = GNUNET_TRANSPORT_connect (p->cfg,
470 GNUNET_assert (NULL != p->th);
471 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
472 p->ghh = GNUNET_TRANSPORT_get_hello (p->th,
475 GNUNET_assert (NULL != p->ghh);
481 * Shutdown the given peer
483 * @param tth testing handle
487 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
488 struct PeerContext *p)
492 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
497 GNUNET_TRANSPORT_disconnect (p->th);
502 if (GNUNET_OK != GNUNET_TESTING_peer_stop (p->peer))
504 LOG (GNUNET_ERROR_TYPE_DEBUG,
505 "Testing lib failed to stop peer %u (`%s') \n", p->no,
506 GNUNET_i2s (&p->id));
508 GNUNET_TESTING_peer_destroy (p->peer);
513 GNUNET_ATS_connectivity_done (p->ats);
516 if (NULL != p->hello)
518 GNUNET_free (p->hello);
523 GNUNET_CONFIGURATION_destroy (p->cfg);
526 GNUNET_CONTAINER_DLL_remove (tth->p_head,
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Peer %u (`%s') stopped \n",
532 GNUNET_i2s (&p->id));
538 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
540 * Remarks: start_peer's notify_connect callback can be called before.
542 * @param tth transport testing handle
545 * @param cb the callback to call when both peers notified that they are connected
546 * @param cls callback cls
547 * @return a connect request handle
549 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
550 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
551 struct PeerContext *p1,
552 struct PeerContext *p2,
553 GNUNET_TRANSPORT_TESTING_connect_cb cb,
556 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
558 cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
566 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
569 cc->tct = GNUNET_SCHEDULER_add_now (&offer_hello,
571 cc->ats_sh = GNUNET_ATS_connectivity_suggest (cc->p1->ats,
575 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "New connect request %p\n",
584 * Cancel the request to connect two peers
585 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
587 * @param tth transport testing handle
588 * @param cc a connect request handle
591 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_handle *tth,
592 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "Canceling connect request %p!\n",
599 GNUNET_SCHEDULER_cancel (cc->tct);
604 GNUNET_TRANSPORT_offer_hello_cancel (cc->oh);
607 if (NULL != cc->ats_sh)
609 GNUNET_ATS_connectivity_suggest_cancel (cc->ats_sh);
613 GNUNET_CONTAINER_DLL_remove (tth->cc_head,
621 * Clean up the transport testing
622 * @param tth transport testing handle
625 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
627 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = tth->cc_head;
628 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct = NULL;
629 struct PeerContext *p = tth->p_head;
630 struct PeerContext *t = NULL;
632 while (cc != tth->cc_tail)
635 LOG (GNUNET_ERROR_TYPE_ERROR,
636 "Developer forgot to cancel connect request %p!\n",
638 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth,
646 LOG (GNUNET_ERROR_TYPE_ERROR,
647 "Developer forgot to stop peer!\n");
648 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
652 GNUNET_TESTING_system_destroy (tth->tl_system,
661 * Initialize the transport testing
662 * @return transport testing handle
664 struct GNUNET_TRANSPORT_TESTING_handle *
665 GNUNET_TRANSPORT_TESTING_init ()
667 struct GNUNET_TRANSPORT_TESTING_handle *tth;
669 /* prepare hostkeys */
670 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_handle);
672 /* Init testing the testing lib */
673 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
677 if (NULL == tth->tl_system)
679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
680 _("Failed to initialize testing library!\n"));
690 * Some utility functions
694 * Removes all directory separators from absolute filename
696 * @param file the absolute file name, e.g. as found in argv[0]
697 * @return extracted file name, has to be freed by caller
700 extract_filename (const char *file)
702 char *pch = GNUNET_strdup (file);
704 char *filename = NULL;
707 if ((strlen (pch) >= 3) && pch[1] == ':')
709 if (NULL != strstr (pch, "\\"))
711 pch = strtok (pch, "\\");
714 pch = strtok (NULL, "\\");
720 if (filename != NULL)
721 pch = filename; /* If we miss the next condition, filename = pch will
725 if (NULL != strstr (pch, "/"))
727 pch = strtok (pch, "/");
730 pch = strtok (NULL, "/");
740 res = GNUNET_strdup (filename);
741 GNUNET_free (backup);
747 * Extracts the test filename from an absolute file name and removes
750 * @param file absolute file name
751 * @param dest where to store result
754 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file,
757 char *filename = extract_filename (file);
758 char *backup = filename;
761 if (filename == NULL)
765 filename = strstr (filename, "tes");
766 if (filename == NULL)
770 if (NULL != (dotexe = strstr (filename, ".exe")))
780 /* create filename */
781 GNUNET_asprintf (dest, "%s", filename);
782 GNUNET_free (backup);
787 * Extracts the filename from an absolute file name and removes the extension
789 * @param file absolute file name
790 * @param dest where to store result
793 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
796 char *src = extract_filename (file);
799 split = strstr (src, ".");
804 GNUNET_asprintf (dest, "%s", src);
810 * Extracts the plugin name from an absolute file name and the test name
812 * @param file absolute file name
813 * @param test test name
814 * @param dest where to store result
817 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
823 char *e = extract_filename (file);
824 char *t = extract_filename (test);
829 filename = strstr (e, "tes");
830 if (NULL == filename)
833 if (NULL != (dotexe = strstr (filename, ".exe")))
837 filename = strstr (filename, t);
838 if (NULL == filename)
841 filename += strlen (t);
842 if ('\0' != *filename)
844 GNUNET_asprintf (dest, "%s", filename);
855 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
856 * if existing ".exe"-prefix and adds the peer-number
858 * @param file filename of the test, e.g. argv[0]
859 * @param dest where to write the filename
860 * @param count peer number
863 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
867 char *filename = extract_filename (file);
868 char *backup = filename;
871 if (NULL == filename)
874 filename = strstr (filename, "tes");
875 if (NULL == filename)
878 if (NULL != (dotexe = strstr (filename, ".exe")))
880 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
881 GNUNET_free (backup);
885 GNUNET_free (backup);
889 /* end of transport-testing.c */