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 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., 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
27 #include "transport-testing.h"
30 static struct PeerContext *
31 find_peer_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
32 const struct GNUNET_PeerIdentity *peer)
34 GNUNET_assert (tth != NULL);
35 struct PeerContext *t = tth->p_head;
39 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
48 static struct ConnectingContext *
49 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
50 struct PeerContext *p1, struct PeerContext *p2)
52 GNUNET_assert (tth != NULL);
53 struct ConnectingContext *cc = tth->cc_head;
57 if ((cc->p1 == p1) && (cc->p2 == p2))
59 if ((cc->p1 == p2) && (cc->p2 == p1))
69 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
71 struct PeerContext *p = cls;
73 struct PeerContext *p2;
75 GNUNET_assert (NULL != p);
76 GNUNET_assert (NULL != p->tth);
77 p2 = find_peer_context (p->tth, peer);
79 p->nc (p->cb_cls, peer);
82 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
84 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
85 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
86 "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
90 /* Find ConnectingContext */
91 struct ConnectingContext *cc = find_connecting_context (p->tth, p, p2);
97 cc->p1_c = GNUNET_YES;
100 cc->p2_c = GNUNET_YES;
102 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
104 cc->cb (cc->p1, cc->p2, cc->cb_cls);
105 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (p->tth, cc);
111 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
113 struct PeerContext *p = cls;
115 /* Find PeerContext */
117 struct PeerContext *p2 = NULL;
121 GNUNET_assert (p->tth != NULL);
122 p2 = find_peer_context (p->tth, peer);
129 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
131 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
132 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
133 "Peers %s disconnected from peer %u (`%s')\n", p2_s, no,
134 GNUNET_i2s (&p->id));
140 p->nd (p->cb_cls, peer);
145 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
146 const struct GNUNET_MessageHeader *message)
148 struct PeerContext *p = cls;
153 p->rec (p->cb_cls, peer, message);
158 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
160 struct PeerContext *p = cb_cls;
161 struct GNUNET_PeerIdentity hello_id;
163 GNUNET_assert (message != NULL);
164 GNUNET_assert (GNUNET_OK ==
165 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
166 message, &hello_id));
167 GNUNET_assert (0 == memcmp (&hello_id, &p->id, sizeof (hello_id)));
168 GNUNET_free_non_null (p->hello);
169 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
171 if (NULL != p->start_cb)
173 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
174 "Peer %u (`%s') successfully started\n", p->no,
175 GNUNET_i2s (&p->id));
176 p->start_cb (p, p->cb_cls);
183 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 struct ConnectingContext *cc = cls;
186 struct PeerContext *p1 = cc->p1;
187 struct PeerContext *p2 = cc->p2;
189 cc->tct = GNUNET_SCHEDULER_NO_TASK;
190 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
193 GNUNET_assert (cc != NULL);
194 GNUNET_assert (cc->p1 != NULL);
195 GNUNET_assert (cc->p2 != NULL);
197 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
199 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
200 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
201 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
202 GNUNET_HELLO_size (cc->p2->hello));
205 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
206 (const struct GNUNET_MessageHeader *) cc->
207 p2->hello, NULL, NULL);
208 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id, NULL, NULL); /*FIXME TRY_CONNECT change */
211 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
216 * Start a peer with the given configuration
217 * @param tth the testing handle
218 * @param cfgname configuration file
219 * @param peer_id a unique number to identify the peer
220 * @param rec receive callback
221 * @param nc connect callback
222 * @param nd disconnect callback
223 * @param start_cb start callback
224 * @param cb_cls closure for callback
225 * @return the peer context
228 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
229 const char *cfgname, int peer_id,
230 GNUNET_TRANSPORT_ReceiveCallback rec,
231 GNUNET_TRANSPORT_NotifyConnect nc,
232 GNUNET_TRANSPORT_NotifyDisconnect nd,
233 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
237 struct GNUNET_PeerIdentity *dummy;
239 GNUNET_assert (NULL != tth);
240 GNUNET_assert (NULL != tth->tl_system);
242 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
244 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
245 "File not found: `%s' \n", cfgname);
249 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
250 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
252 /* Create configuration and call testing lib to modify it */
253 p->cfg = GNUNET_CONFIGURATION_create ();
254 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
256 if (GNUNET_SYSERR == GNUNET_TESTING_configuration_create (tth->tl_system, p->cfg))
258 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
259 "Testing library failed to create unique configuration based on `%s'\n",
266 /* Configure peer with configuration */
267 p->peer = GNUNET_TESTING_peer_configure (tth->tl_system, p->cfg, p->no, NULL, &emsg);
270 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
271 "Testing library failed to create unique configuration based on `%s': `%s'\n",
273 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
274 GNUNET_free_non_null (emsg);
277 GNUNET_free_non_null (emsg);
278 if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer))
280 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
281 "Testing library failed to create unique configuration based on `%s'\n",
283 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
287 memset(&dummy, '\0', sizeof (dummy));
288 GNUNET_TESTING_peer_get_identity (p->peer, &p->id);
289 if (0 == memcmp (&dummy, &p->id, sizeof (struct GNUNET_PeerIdentity)))
291 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
292 "Testing library failed to obtain peer identity for peer %u\n",
294 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
299 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
300 "Peer %u configured with identity `%s'\n",
302 GNUNET_i2s_full (&p->id));
309 p->start_cb = start_cb;
315 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
317 ¬ify_connect, ¬ify_disconnect);
320 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
321 "Failed to connect to transport service for peer `%s': `%s'\n",
323 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
327 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
328 GNUNET_assert (p->ghh != NULL);
334 * Restart the given peer
335 * @param tth testing handle
337 * @param cfgname the cfg file used to restart
338 * @param restart_cb callback to call when restarted
339 * @param cb_cls callback closure
340 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
343 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
344 *tth, struct PeerContext *p,
346 GNUNET_TRANSPORT_TESTING_start_cb
347 restart_cb, void *cb_cls)
349 GNUNET_assert (tth != NULL);
350 GNUNET_assert (p != NULL);
351 GNUNET_assert (NULL != p->peer);
353 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
354 "Restarting peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
357 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
358 "Stopping peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
360 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
364 GNUNET_TRANSPORT_disconnect (p->th);
366 if (GNUNET_SYSERR == GNUNET_TESTING_peer_stop(p->peer))
368 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
369 "Failed to stop peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
370 return GNUNET_SYSERR;
376 if (GNUNET_SYSERR == GNUNET_TESTING_peer_start(p->peer))
378 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
379 "Failed to restart peer %u (`%s')\n",
380 p->no, GNUNET_i2s (&p->id));
381 return GNUNET_SYSERR;
384 GNUNET_assert (p->th != NULL);
385 GNUNET_assert (p->start_cb == NULL);
386 p->start_cb = restart_cb;
389 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p,
393 GNUNET_assert (NULL != p->th);
395 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
396 GNUNET_assert (p->ghh != NULL);
402 * shutdown the given peer
403 * @param tth testing handle
407 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
408 struct PeerContext *p)
410 GNUNET_assert (p != NULL);
413 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
418 GNUNET_TRANSPORT_disconnect (p->th);
424 if (GNUNET_OK != GNUNET_TESTING_peer_stop (p->peer))
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
427 "Testing lib failed to stop peer %u (`%s') \n", p->no,
428 GNUNET_i2s (&p->id));
430 GNUNET_TESTING_peer_destroy (p->peer);
434 if (p->hello != NULL)
436 GNUNET_free (p->hello);
441 GNUNET_CONFIGURATION_destroy (p->cfg);
444 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
445 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
446 "Peer %u (`%s') stopped \n", p->no,
447 GNUNET_i2s (&p->id));
453 * Initiate a connection from p1 to p2 by offering p1 p2's HELLO message
455 * Remarks: start_peer's notify_connect callback can be called before.
457 * @param tth transport testing handle
460 * @param cb the callback to call when both peers notified that they are connected
461 * @param cls callback cls
462 * @return a connect request handle
464 GNUNET_TRANSPORT_TESTING_ConnectRequest
465 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
466 struct PeerContext *p1,
467 struct PeerContext *p2,
468 GNUNET_TRANSPORT_TESTING_connect_cb cb,
471 GNUNET_assert (tth != NULL);
473 struct ConnectingContext *cc =
474 GNUNET_malloc (sizeof (struct ConnectingContext));
476 GNUNET_assert (p1 != NULL);
477 GNUNET_assert (p2 != NULL);
487 GNUNET_assert (cc->th_p1 != NULL);
488 GNUNET_assert (cc->th_p2 != NULL);
489 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
490 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
491 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
492 "New connect request %p\n", cc);
499 * Cancel the request to connect two peers
500 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
502 * @param tth transport testing handle
503 * @param ccr a connect request handle
506 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
507 GNUNET_TRANSPORT_TESTING_handle
509 GNUNET_TRANSPORT_TESTING_ConnectRequest
512 struct ConnectingContext *cc = ccr;
514 GNUNET_assert (tth != NULL);
516 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
517 "Canceling connect request %p!\n", cc);
519 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
520 GNUNET_SCHEDULER_cancel (cc->tct);
521 cc->tct = GNUNET_SCHEDULER_NO_TASK;
523 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
529 * Clean up the transport testing
530 * @param tth transport testing handle
533 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
535 struct ConnectingContext *cc = tth->cc_head;
536 struct ConnectingContext *ct = NULL;
537 struct PeerContext *p = tth->p_head;
538 struct PeerContext *t = NULL;
540 GNUNET_assert (tth != NULL);
542 while (cc != tth->cc_tail)
545 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
546 "Developer forgot to cancel connect request %p!\n", cc);
547 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
554 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
555 "Developer forgot to stop peer!\n");
556 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
560 GNUNET_TESTING_system_destroy (tth->tl_system, GNUNET_YES);
568 * Initialize the transport testing
569 * @return transport testing handle
571 struct GNUNET_TRANSPORT_TESTING_handle *
572 GNUNET_TRANSPORT_TESTING_init ()
574 struct GNUNET_TRANSPORT_TESTING_handle *tth;
576 /* prepare hostkeys */
577 tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
579 /* Init testing the testing lib */
580 tth->tl_system = GNUNET_TESTING_system_create ("transport-testing", NULL,
582 if (NULL == tth->tl_system)
584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to initialize testing library!\n"));
594 * Some utility functions
598 * Removes all directory separators from absolute filename
599 * @param file the absolute file name, e.g. as found in argv[0]
600 * @return extracted file name, has to be freed by caller
603 extract_filename (const char *file)
605 char *pch = GNUNET_strdup (file);
607 char *filename = NULL;
610 if ((strlen (pch) >= 3) && pch[1] == ':')
612 if (NULL != strstr (pch, "\\"))
614 pch = strtok (pch, "\\");
617 pch = strtok (NULL, "\\");
623 if (filename != NULL)
624 pch = filename; /* If we miss the next condition, filename = pch will
628 if (NULL != strstr (pch, "/"))
630 pch = strtok (pch, "/");
633 pch = strtok (NULL, "/");
643 res = GNUNET_strdup (filename);
644 GNUNET_free (backup);
650 * Extracts the test filename from an absolute file name and removes the extension
651 * @param file absolute file name
652 * @param dest where to store result
655 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
657 char *filename = extract_filename (file);
658 char *backup = filename;
661 if (filename == NULL)
665 filename = strstr (filename, "tes");
666 if (filename == NULL)
670 if (NULL != (dotexe = strstr (filename, ".exe")))
680 /* create filename */
681 GNUNET_asprintf (dest, "%s", filename);
682 GNUNET_free (backup);
687 * Extracts the filename from an absolute file name and removes the extension
688 * @param file absolute file name
689 * @param dest where to store result
692 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
694 char *src = extract_filename (file);
697 split = strstr (src, ".");
702 GNUNET_asprintf (dest, "%s", src);
708 * Extracts the plugin name from an absolute file name and the test name
710 * @param file absolute file name
711 * @param test test name
712 * @param dest where to store result
715 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
716 const char *test, char **dest)
720 char *e = extract_filename (file);
721 char *t = extract_filename (test);
726 filename = strstr (e, "tes");
727 if (NULL == filename)
730 if (NULL != (dotexe = strstr (filename, ".exe")))
734 filename = strstr (filename, t);
735 if (NULL == filename)
738 filename += strlen (t);
739 if ('\0' != *filename)
741 GNUNET_asprintf (dest, "%s", filename);
752 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
753 * if existing ".exe"-prefix and adds the peer-number
755 * @param file filename of the test, e.g. argv[0]
756 * @param dest where to write the filename
757 * @param count peer number
760 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
763 char *filename = extract_filename (file);
764 char *backup = filename;
767 if (NULL == filename)
770 filename = strstr (filename, "tes");
771 if (NULL == filename)
774 if (NULL != (dotexe = strstr (filename, ".exe")))
776 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
777 GNUNET_free (backup);
781 GNUNET_free (backup);
785 /* end of transport-testing.c */