2 This file is part of GNUnet.
3 (C) 2006, 2009 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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport-testing.c
23 * @brief testing lib for transport service
25 * @author Matthias Wachs
28 #include "transport-testing.h"
31 static struct PeerContext *
32 find_peer_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
33 const struct GNUNET_PeerIdentity *peer)
35 GNUNET_assert (tth != NULL);
36 struct PeerContext *t = tth->p_head;
40 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
49 static struct ConnectingContext *
50 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
51 struct PeerContext *p1, struct PeerContext *p2)
53 GNUNET_assert (tth != NULL);
54 struct ConnectingContext *cc = tth->cc_head;
58 if ((cc->p1 == p1) && (cc->p2 == p2))
60 if ((cc->p1 == p2) && (cc->p2 == p1))
70 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
71 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
73 struct PeerContext *p = cls;
75 /* Find PeerContext */
76 GNUNET_assert (p != 0);
77 GNUNET_assert (p->tth != NULL);
78 struct PeerContext *p2 = find_peer_context (p->tth, peer);
83 p->nc (p->cb_cls, peer, ats, ats_count);
88 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
90 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
91 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
92 "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
96 /* Find ConnectingContext */
97 struct ConnectingContext *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, const struct GNUNET_PeerIdentity *peer)
119 struct PeerContext *p = cls;
121 /* Find PeerContext */
123 struct PeerContext *p2 = NULL;
127 GNUNET_assert (p->tth != NULL);
128 p2 = find_peer_context (p->tth, peer);
135 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
137 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
138 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
139 "Peers %s disconnected from peer %u (`%s')\n", p2_s, no,
140 GNUNET_i2s (&p->id));
146 p->nd (p->cb_cls, peer);
151 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
152 const struct GNUNET_MessageHeader *message,
153 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
155 struct PeerContext *p = cls;
160 p->rec (p->cb_cls, peer, message, ats, ats_count);
165 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
167 struct PeerContext *p = cb_cls;
168 struct GNUNET_PeerIdentity hello_id;
170 GNUNET_assert (message != NULL);
171 GNUNET_assert (GNUNET_OK ==
172 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
173 message, &hello_id));
174 GNUNET_assert (0 == memcmp (&hello_id, &p->id, sizeof (hello_id)));
175 GNUNET_free_non_null (p->hello);
176 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
178 if (NULL != p->start_cb)
180 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
181 "Peer %u (`%s') successfully started\n", p->no,
182 GNUNET_i2s (&p->id));
183 p->start_cb (p, p->cb_cls);
190 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 struct ConnectingContext *cc = cls;
193 struct PeerContext *p1 = cc->p1;
194 struct PeerContext *p2 = cc->p2;
196 cc->tct = GNUNET_SCHEDULER_NO_TASK;
197 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
200 GNUNET_assert (cc != NULL);
201 GNUNET_assert (cc->p1 != NULL);
202 GNUNET_assert (cc->p2 != NULL);
204 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
206 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
207 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
208 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
209 GNUNET_HELLO_size (cc->p2->hello));
212 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
213 (const struct GNUNET_MessageHeader *) cc->
214 p2->hello, NULL, NULL);
215 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
218 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
223 * Start a peer with the given configuration
224 * @param tth the testing handle
225 * @param cfgname configuration file
226 * @param peer_id a unique number to identify the peer
227 * @param rec receive callback
228 * @param nc connect callback
229 * @param nd disconnect callback
230 * @param start_cb start callback
231 * @param cb_cls closure for callback
232 * @return the peer context
235 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
236 const char *cfgname, int peer_id,
237 GNUNET_TRANSPORT_ReceiveCallback rec,
238 GNUNET_TRANSPORT_NotifyConnect nc,
239 GNUNET_TRANSPORT_NotifyDisconnect nd,
240 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
244 struct GNUNET_PeerIdentity *dummy;
246 GNUNET_assert (NULL != tth);
247 GNUNET_assert (NULL != tth->tl_system);
249 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
251 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
252 "File not found: `%s' \n", cfgname);
256 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
257 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
259 /* Create configuration and call testing lib to modify it */
260 p->cfg = GNUNET_CONFIGURATION_create ();
261 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
263 if (GNUNET_SYSERR == GNUNET_TESTING_configuration_create (tth->tl_system, p->cfg))
265 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
266 "Testing library failed to create unique configuration based on `%s'\n",
273 /* Configure peer with configuration */
274 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system, p->cfg, p->no, NULL, &emsg);
277 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
278 "Testing library failed to create unique configuration based on `%s': `%s'\n",
280 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
284 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
286 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
287 "Testing library failed to create unique configuration based on `%s'\n",
289 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
293 memset(&dummy, '\0', sizeof (dummy));
294 GNUNET_TESTING_peer_get_identity (p->peer, &p->id);
295 if (0 == memcmp (&dummy, &p->id, sizeof (dummy)))
297 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
298 "Testing library failed to obtain peer identity for peer %u\n",
300 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
305 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
306 "Peer %u configured with identity `%s'\n",
308 GNUNET_i2s (&p->id));
315 p->start_cb = start_cb;
321 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
323 ¬ify_connect, ¬ify_disconnect);
326 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
327 "Failed to connect to transport service for peer `%s': `%s'\n",
329 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
333 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
334 GNUNET_assert (p->ghh != NULL);
340 * Restart the given peer
341 * @param tth testing handle
343 * @param cfgname the cfg file used to restart
344 * @param restart_cb callback to call when restarted
345 * @param cb_cls callback closure
346 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
349 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
350 *tth, struct PeerContext *p,
352 GNUNET_TRANSPORT_TESTING_start_cb
353 restart_cb, void *cb_cls)
355 GNUNET_assert (tth != NULL);
356 GNUNET_assert (p != NULL);
357 GNUNET_assert (NULL != p->peer);
359 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
360 "Restarting peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
363 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
364 "Stopping peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
366 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
370 GNUNET_TRANSPORT_disconnect (p->th);
372 if (GNUNET_SYSERR == GNUNET_TESTING_peer_stop(p->peer))
374 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
375 "Failed to stop peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
376 return GNUNET_SYSERR;
382 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start(p->peer))
384 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
385 "Failed to restart peer %u (`%s')\n",
386 p->no, GNUNET_i2s (&p->id));
387 return GNUNET_SYSERR;
390 GNUNET_assert (p->th != NULL);
391 GNUNET_assert (p->start_cb == NULL);
392 p->start_cb = restart_cb;
395 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
399 GNUNET_assert (NULL != p->th);
401 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
402 GNUNET_assert (p->ghh != NULL);
408 * shutdown the given peer
409 * @param tth testing handle
413 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
414 struct PeerContext *p)
416 GNUNET_assert (p != NULL);
419 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
424 GNUNET_TRANSPORT_disconnect (p->th);
430 if (GNUNET_OK != GNUNET_TESTING_peer_stop (p->peer))
432 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
433 "Testing lib failed to stop peer %u (`%s') \n", p->no,
434 GNUNET_i2s (&p->id));
436 GNUNET_TESTING_peer_destroy (p->peer);
440 if (p->hello != NULL)
442 GNUNET_free (p->hello);
447 GNUNET_CONFIGURATION_destroy (p->cfg);
450 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
452 "Peer %u (`%s') stopped \n", p->no,
453 GNUNET_i2s (&p->id));
459 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
461 * Remarks: start_peer's notify_connect callback can be called before.
463 * @param tth transport testing handle
466 * @param cb the callback to call when both peers notified that they are connected
467 * @param cls callback cls
468 * @return a connect request handle
470 GNUNET_TRANSPORT_TESTING_ConnectRequest
471 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
472 struct PeerContext *p1,
473 struct PeerContext *p2,
474 GNUNET_TRANSPORT_TESTING_connect_cb cb,
477 GNUNET_assert (tth != NULL);
479 struct ConnectingContext *cc =
480 GNUNET_malloc (sizeof (struct ConnectingContext));
482 GNUNET_assert (p1 != NULL);
483 GNUNET_assert (p2 != NULL);
493 GNUNET_assert (cc->th_p1 != NULL);
494 GNUNET_assert (cc->th_p2 != NULL);
495 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
496 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
497 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
498 "New connect request %p\n", cc);
505 * Cancel the request to connect two peers
506 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
508 * @param tth transport testing handle
509 * @param ccr a connect request handle
512 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
513 GNUNET_TRANSPORT_TESTING_handle
515 GNUNET_TRANSPORT_TESTING_ConnectRequest
518 struct ConnectingContext *cc = ccr;
520 GNUNET_assert (tth != NULL);
522 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
523 "Canceling connect request %p!\n", cc);
525 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
526 GNUNET_SCHEDULER_cancel (cc->tct);
527 cc->tct = GNUNET_SCHEDULER_NO_TASK;
529 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
535 * Clean up the transport testing
536 * @param tth transport testing handle
539 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
541 struct ConnectingContext *cc = tth->cc_head;
542 struct ConnectingContext *ct = NULL;
543 struct PeerContext *p = tth->p_head;
544 struct PeerContext *t = NULL;
546 GNUNET_assert (tth != NULL);
548 while (cc != tth->cc_tail)
551 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
552 "Developer forgot to cancel connect request %p!\n", cc);
553 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
560 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
561 "Developer forgot to stop peer!\n");
562 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
566 GNUNET_TESTING_system_destroy (tth->tl_system, GNUNET_YES);
574 * Initialize the transport testing
575 * @return transport testing handle
577 struct GNUNET_TRANSPORT_TESTING_handle *
578 GNUNET_TRANSPORT_TESTING_init ()
580 struct GNUNET_TRANSPORT_TESTING_handle *tth;
582 /* prepare hostkeys */
583 tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
585 /* Init testing the testing lib */
586 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing", NULL);
587 if (NULL == tth->tl_system)
589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize testing library!\n"));
599 * Some utility functions
603 * Removes all directory separators from absolute filename
604 * @param file the absolute file name, e.g. as found in argv[0]
605 * @return extracted file name, has to be freed by caller
608 extract_filename (const char *file)
610 char *pch = GNUNET_strdup (file);
612 char *filename = NULL;
615 if ((strlen (pch) >= 3) && pch[1] == ':')
617 if (NULL != strstr (pch, "\\"))
619 pch = strtok (pch, "\\");
622 pch = strtok (NULL, "\\");
628 if (filename != NULL)
629 pch = filename; /* If we miss the next condition, filename = pch will
633 if (NULL != strstr (pch, "/"))
635 pch = strtok (pch, "/");
638 pch = strtok (NULL, "/");
648 res = GNUNET_strdup (filename);
649 GNUNET_free (backup);
655 * Extracts the test filename from an absolute file name and removes the extension
656 * @param file absolute file name
657 * @param dest where to store result
660 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
662 char *filename = extract_filename (file);
663 char *backup = filename;
666 if (filename == NULL)
670 filename = strstr (filename, "tes");
671 if (filename == NULL)
675 if (NULL != (dotexe = strstr (filename, ".exe")))
685 /* create filename */
686 GNUNET_asprintf (dest, "%s", filename);
687 GNUNET_free (backup);
692 * Extracts the filename from an absolute file name and removes the extension
693 * @param file absolute file name
694 * @param dest where to store result
697 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
699 char *src = extract_filename (file);
702 split = strstr (src, ".");
707 GNUNET_asprintf (dest, "%s", src);
713 * Extracts the plugin name from an absolute file name and the test name
715 * @param file absolute file name
716 * @param test test name
717 * @param dest where to store result
720 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
721 const char *test, char **dest)
725 char *e = extract_filename (file);
726 char *t = extract_filename (test);
731 filename = strstr (e, "tes");
732 if (NULL == filename)
735 if (NULL != (dotexe = strstr (filename, ".exe")))
739 filename = strstr (filename, t);
740 if (NULL == filename)
743 filename += strlen (t);
744 if ('\0' != *filename)
746 GNUNET_asprintf (dest, "%s", filename);
757 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
758 * if existing ".exe"-prefix and adds the peer-number
760 * @param file filename of the test, e.g. argv[0]
761 * @param dest where to write the filename
762 * @param count peer number
765 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
768 char *filename = extract_filename (file);
769 char *backup = filename;
772 if (NULL == filename)
775 filename = strstr (filename, "tes");
776 if (NULL == filename)
779 if (NULL != (dotexe = strstr (filename, ".exe")))
781 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
782 GNUNET_free (backup);
786 GNUNET_free (backup);
791 /* end of transport-testing.c */