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 GNUNET_assert (tth != NULL);
38 struct PeerContext *t = tth->p_head;
42 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
51 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
52 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
53 struct PeerContext *p1,
54 struct PeerContext *p2)
56 GNUNET_assert (tth != NULL);
57 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = tth->cc_head;
61 if ((cc->p1 == p1) && (cc->p2 == p2))
63 if ((cc->p1 == p2) && (cc->p2 == p1))
73 notify_connect (void *cls,
74 const struct GNUNET_PeerIdentity *peer)
76 struct PeerContext *p = cls;
78 struct PeerContext *p2;
80 GNUNET_assert (NULL != p);
81 GNUNET_assert (NULL != p->tth);
82 p2 = find_peer_context (p->tth, peer);
84 p->nc (p->cb_cls, peer);
87 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
89 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
90 LOG (GNUNET_ERROR_TYPE_DEBUG,
91 "Peers %s connected to peer %u (`%s')\n",
97 /* Find ConnectingContext */
98 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = find_connecting_context (p->tth, p, p2);
104 cc->p1_c = GNUNET_YES;
107 cc->p2_c = GNUNET_YES;
109 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
111 cc->cb (cc->p1, cc->p2, cc->cb_cls);
112 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (p->tth, cc);
118 notify_disconnect (void *cls,
119 const struct GNUNET_PeerIdentity *peer)
121 struct PeerContext *p = cls;
123 /* Find PeerContext */
125 struct PeerContext *p2 = NULL;
129 GNUNET_assert (p->tth != NULL);
130 p2 = find_peer_context (p->tth, peer);
137 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
139 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
140 LOG (GNUNET_ERROR_TYPE_DEBUG,
141 "Peers %s disconnected from peer %u (`%s')\n",
144 GNUNET_i2s (&p->id));
150 p->nd (p->cb_cls, peer);
155 notify_receive (void *cls,
156 const struct GNUNET_PeerIdentity *peer,
157 const struct GNUNET_MessageHeader *message)
159 struct PeerContext *p = cls;
164 p->rec (p->cb_cls, peer, message);
169 get_hello (void *cb_cls,
170 const struct GNUNET_MessageHeader *message)
172 struct PeerContext *p = cb_cls;
173 struct GNUNET_PeerIdentity hello_id;
175 GNUNET_assert (message != NULL);
176 GNUNET_assert (GNUNET_OK ==
177 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
178 message, &hello_id));
179 GNUNET_assert (0 == memcmp (&hello_id, &p->id, sizeof (hello_id)));
180 GNUNET_free_non_null (p->hello);
181 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
183 if (NULL != p->start_cb)
185 LOG (GNUNET_ERROR_TYPE_DEBUG,
186 "Peer %u (`%s') successfully started\n", p->no,
187 GNUNET_i2s (&p->id));
188 p->start_cb (p, p->cb_cls);
195 try_connect (void *cls,
196 const struct GNUNET_SCHEDULER_TaskContext *tc)
198 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = cls;
199 struct PeerContext *p1 = cc->p1;
200 struct PeerContext *p2 = cc->p2;
203 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
206 GNUNET_assert (cc != NULL);
207 GNUNET_assert (cc->p1 != NULL);
208 GNUNET_assert (cc->p2 != NULL);
210 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
213 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
214 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
215 GNUNET_HELLO_size (cc->p2->hello));
218 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
219 (const struct GNUNET_MessageHeader *) cc->
220 p2->hello, NULL, NULL);
221 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id, NULL, NULL); /*FIXME TRY_CONNECT change */
224 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
229 * Start a peer with the given configuration
230 * @param tth the testing handle
231 * @param cfgname configuration file
232 * @param peer_id a unique number to identify the peer
233 * @param rec receive callback
234 * @param nc connect callback
235 * @param nd disconnect callback
236 * @param start_cb start callback
237 * @param cb_cls closure for callback
238 * @return the peer context
241 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
242 const char *cfgname, int peer_id,
243 GNUNET_TRANSPORT_ReceiveCallback rec,
244 GNUNET_TRANSPORT_NotifyConnect nc,
245 GNUNET_TRANSPORT_NotifyDisconnect nd,
246 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
250 struct GNUNET_PeerIdentity *dummy;
252 GNUNET_assert (NULL != tth);
253 GNUNET_assert (NULL != tth->tl_system);
255 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
257 LOG (GNUNET_ERROR_TYPE_ERROR,
258 "File not found: `%s'\n",
263 struct PeerContext *p = GNUNET_new (struct PeerContext);
264 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
266 /* Create configuration and call testing lib to modify it */
267 p->cfg = GNUNET_CONFIGURATION_create ();
268 GNUNET_assert (GNUNET_OK ==
269 GNUNET_CONFIGURATION_load (p->cfg, cfgname));
270 if (GNUNET_SYSERR == GNUNET_TESTING_configuration_create (tth->tl_system, p->cfg))
272 LOG (GNUNET_ERROR_TYPE_ERROR,
273 "Testing library failed to create unique configuration based on `%s'\n",
280 /* Configure peer with configuration */
281 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system, p->cfg, p->no, NULL, &emsg);
284 LOG (GNUNET_ERROR_TYPE_ERROR,
285 "Testing library failed to create unique configuration based on `%s': `%s'\n",
288 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
289 GNUNET_free_non_null (emsg);
292 GNUNET_free_non_null (emsg);
293 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
295 LOG (GNUNET_ERROR_TYPE_ERROR,
296 "Testing library failed to create unique configuration based on `%s'\n",
298 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
302 memset(&dummy, '\0', sizeof (dummy));
303 GNUNET_TESTING_peer_get_identity (p->peer, &p->id);
304 if (0 == memcmp (&dummy, &p->id, sizeof (struct GNUNET_PeerIdentity)))
306 LOG (GNUNET_ERROR_TYPE_ERROR,
307 "Testing library failed to obtain peer identity for peer %u\n",
309 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
314 LOG (GNUNET_ERROR_TYPE_DEBUG,
315 "Peer %u configured with identity `%s'\n",
317 GNUNET_i2s_full (&p->id));
324 p->start_cb = start_cb;
330 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
332 ¬ify_connect, ¬ify_disconnect);
335 LOG (GNUNET_ERROR_TYPE_ERROR,
336 "Failed to connect to transport service for peer `%s': `%s'\n",
339 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
343 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
344 GNUNET_assert (p->ghh != NULL);
350 * Restart the given peer
351 * @param tth testing handle
353 * @param cfgname the cfg file used to restart
354 * @param restart_cb callback to call when restarted
355 * @param cb_cls callback closure
356 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
359 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
360 *tth, struct PeerContext *p,
362 GNUNET_TRANSPORT_TESTING_start_cb
363 restart_cb, void *cb_cls)
365 GNUNET_assert (tth != NULL);
366 GNUNET_assert (p != NULL);
367 GNUNET_assert (NULL != p->peer);
369 LOG (GNUNET_ERROR_TYPE_DEBUG,
370 "Restarting peer %u (`%s')\n",
372 GNUNET_i2s (&p->id));
375 LOG (GNUNET_ERROR_TYPE_DEBUG,
376 "Stopping peer %u (`%s')\n",
378 GNUNET_i2s (&p->id));
380 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
384 GNUNET_TRANSPORT_disconnect (p->th);
386 if (GNUNET_SYSERR == GNUNET_TESTING_peer_stop(p->peer))
388 LOG (GNUNET_ERROR_TYPE_ERROR,
389 "Failed to stop peer %u (`%s')\n",
391 GNUNET_i2s (&p->id));
392 return GNUNET_SYSERR;
398 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start(p->peer))
400 LOG (GNUNET_ERROR_TYPE_ERROR,
401 "Failed to restart peer %u (`%s')\n",
402 p->no, GNUNET_i2s (&p->id));
403 return GNUNET_SYSERR;
406 GNUNET_assert (p->th != NULL);
407 GNUNET_assert (p->start_cb == NULL);
408 p->start_cb = restart_cb;
411 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
415 GNUNET_assert (NULL != p->th);
417 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
418 GNUNET_assert (p->ghh != NULL);
424 * shutdown the given peer
425 * @param tth testing handle
429 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
430 struct PeerContext *p)
432 GNUNET_assert (p != NULL);
435 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
440 GNUNET_TRANSPORT_disconnect (p->th);
446 if (GNUNET_OK != GNUNET_TESTING_peer_stop (p->peer))
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
449 "Testing lib failed to stop peer %u (`%s') \n", p->no,
450 GNUNET_i2s (&p->id));
452 GNUNET_TESTING_peer_destroy (p->peer);
456 if (p->hello != NULL)
458 GNUNET_free (p->hello);
463 GNUNET_CONFIGURATION_destroy (p->cfg);
466 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
467 LOG (GNUNET_ERROR_TYPE_DEBUG,
468 "Peer %u (`%s') stopped \n", p->no,
469 GNUNET_i2s (&p->id));
475 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
477 * Remarks: start_peer's notify_connect callback can be called before.
479 * @param tth transport testing handle
482 * @param cb the callback to call when both peers notified that they are connected
483 * @param cls callback cls
484 * @return a connect request handle
486 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
487 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
488 struct PeerContext *p1,
489 struct PeerContext *p2,
490 GNUNET_TRANSPORT_TESTING_connect_cb cb,
493 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_ConnectRequest);
495 GNUNET_assert (tth != NULL);
496 GNUNET_assert (p1 != NULL);
497 GNUNET_assert (p2 != NULL);
507 GNUNET_assert (cc->th_p1 != NULL);
508 GNUNET_assert (cc->th_p2 != NULL);
509 GNUNET_CONTAINER_DLL_insert (tth->cc_head,
512 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "New connect request %p\n",
522 * Cancel the request to connect two peers
523 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
525 * @param tth transport testing handle
526 * @param cc a connect request handle
529 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_handle *tth,
530 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc)
532 GNUNET_assert (tth != NULL);
534 LOG (GNUNET_ERROR_TYPE_DEBUG,
535 "Canceling connect request %p!\n",
539 GNUNET_SCHEDULER_cancel (cc->tct);
542 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
548 * Clean up the transport testing
549 * @param tth transport testing handle
552 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
554 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc = tth->cc_head;
555 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *ct = NULL;
556 struct PeerContext *p = tth->p_head;
557 struct PeerContext *t = NULL;
559 GNUNET_assert (tth != NULL);
561 while (cc != tth->cc_tail)
564 LOG (GNUNET_ERROR_TYPE_ERROR,
565 "Developer forgot to cancel connect request %p!\n",
567 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
574 LOG (GNUNET_ERROR_TYPE_ERROR,
575 "Developer forgot to stop peer!\n");
576 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
580 GNUNET_TESTING_system_destroy (tth->tl_system, GNUNET_YES);
588 * Initialize the transport testing
589 * @return transport testing handle
591 struct GNUNET_TRANSPORT_TESTING_handle *
592 GNUNET_TRANSPORT_TESTING_init ()
594 struct GNUNET_TRANSPORT_TESTING_handle *tth;
596 /* prepare hostkeys */
597 tth = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_handle);
599 /* Init testing the testing lib */
600 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing",
604 if (NULL == tth->tl_system)
606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
607 _("Failed to initialize testing library!\n"));
617 * Some utility functions
621 * Removes all directory separators from absolute filename
622 * @param file the absolute file name, e.g. as found in argv[0]
623 * @return extracted file name, has to be freed by caller
626 extract_filename (const char *file)
628 char *pch = GNUNET_strdup (file);
630 char *filename = NULL;
633 if ((strlen (pch) >= 3) && pch[1] == ':')
635 if (NULL != strstr (pch, "\\"))
637 pch = strtok (pch, "\\");
640 pch = strtok (NULL, "\\");
646 if (filename != NULL)
647 pch = filename; /* If we miss the next condition, filename = pch will
651 if (NULL != strstr (pch, "/"))
653 pch = strtok (pch, "/");
656 pch = strtok (NULL, "/");
666 res = GNUNET_strdup (filename);
667 GNUNET_free (backup);
673 * Extracts the test filename from an absolute file name and removes the extension
674 * @param file absolute file name
675 * @param dest where to store result
678 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file,
681 char *filename = extract_filename (file);
682 char *backup = filename;
685 if (filename == NULL)
689 filename = strstr (filename, "tes");
690 if (filename == NULL)
694 if (NULL != (dotexe = strstr (filename, ".exe")))
704 /* create filename */
705 GNUNET_asprintf (dest, "%s", filename);
706 GNUNET_free (backup);
711 * Extracts the filename from an absolute file name and removes the extension
712 * @param file absolute file name
713 * @param dest where to store result
716 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file,
719 char *src = extract_filename (file);
722 split = strstr (src, ".");
727 GNUNET_asprintf (dest, "%s", src);
733 * Extracts the plugin name from an absolute file name and the test name
735 * @param file absolute file name
736 * @param test test name
737 * @param dest where to store result
740 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
746 char *e = extract_filename (file);
747 char *t = extract_filename (test);
752 filename = strstr (e, "tes");
753 if (NULL == filename)
756 if (NULL != (dotexe = strstr (filename, ".exe")))
760 filename = strstr (filename, t);
761 if (NULL == filename)
764 filename += strlen (t);
765 if ('\0' != *filename)
767 GNUNET_asprintf (dest, "%s", filename);
778 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
779 * if existing ".exe"-prefix and adds the peer-number
781 * @param file filename of the test, e.g. argv[0]
782 * @param dest where to write the filename
783 * @param count peer number
786 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
790 char *filename = extract_filename (file);
791 char *backup = filename;
794 if (NULL == filename)
797 filename = strstr (filename, "tes");
798 if (NULL == filename)
801 if (NULL != (dotexe = strstr (filename, ".exe")))
803 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
804 GNUNET_free (backup);
808 GNUNET_free (backup);
812 /* end of transport-testing.c */