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"
30 #define VERBOSE GNUNET_EXTRA_LOGGING
31 #define HOSTKEYFILESIZE 914
34 get_host_key (struct GNUNET_TRANSPORT_TESTING_handle *tth)
36 if (tth->hostkey_data == NULL)
38 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
39 "No precomputed hostkeys available\n");
42 if (tth->hostkeys_total > tth->hostkeys_last)
45 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
46 "Used hostkey %u of %u available hostkeys\n",
47 tth->hostkeys_last, tth->hostkeys_total);
48 return &tth->hostkey_data[(tth->hostkeys_last - 1) * HOSTKEYFILESIZE];
50 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
51 "No hostkey available (%u of %u already used)\n",
52 tth->hostkeys_last, tth->hostkeys_total);
56 static struct PeerContext *
57 find_peer_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
58 const struct GNUNET_PeerIdentity *peer)
60 GNUNET_assert (tth != NULL);
61 struct PeerContext *t = tth->p_head;
65 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
73 struct ConnectingContext *
74 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
75 struct PeerContext *p1, struct PeerContext *p2)
77 GNUNET_assert (tth != NULL);
78 struct ConnectingContext *cc = tth->cc_head;
82 if ((cc->p1 == p1) && (cc->p2 == p2))
84 if ((cc->p1 == p2) && (cc->p2 == p1))
93 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
94 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
96 struct PeerContext *p = cls;
98 /* Find PeerContext */
99 GNUNET_assert (p != 0);
100 GNUNET_assert (p->tth != NULL);
101 struct PeerContext *p2 = find_peer_context (p->tth, peer);
106 p->nc (p->cb_cls, peer, ats, ats_count);
112 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
114 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
115 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
116 "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
117 GNUNET_i2s (&p->id));
122 /* Find ConnectingContext */
123 struct ConnectingContext *cc = find_connecting_context (p->tth, p, p2);
129 cc->p1_c = GNUNET_YES;
132 cc->p2_c = GNUNET_YES;
134 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
136 cc->cb (cc->p1, cc->p2, cc->cb_cls);
137 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (p->tth, cc);
142 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
144 struct PeerContext *p = cls;
146 /* Find PeerContext */
148 struct PeerContext *p2 = NULL;
152 GNUNET_assert (p->tth != NULL);
153 p2 = find_peer_context (p->tth, peer);
160 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
162 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
163 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
164 "Peers %s disconnected from peer %u (`%s')\n", p2_s, no,
165 GNUNET_i2s (&p->id));
171 p->nd (p->cb_cls, peer);
175 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
176 const struct GNUNET_MessageHeader *message,
177 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
179 struct PeerContext *p = cls;
184 p->rec (p->cb_cls, peer, message, ats, ats_count);
188 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
190 struct PeerContext *p = cb_cls;
192 GNUNET_assert (message != NULL);
193 GNUNET_assert (GNUNET_OK ==
194 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
198 GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) message);
200 GNUNET_free_non_null (p->hello);
201 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
204 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
205 "New HELLO for peer %u (`%s') with size %u\n", p->no,
206 GNUNET_i2s (&p->id), size);
209 if (p->start_cb != NULL)
212 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
213 "Peer %u (`%s') successfully started\n", p->no,
214 GNUNET_i2s (&p->id));
216 p->start_cb (p, p->cb_cls);
223 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
225 struct ConnectingContext *cc = cls;
226 struct PeerContext *p1 = cc->p1;
227 struct PeerContext *p2 = cc->p2;
229 cc->tct = GNUNET_SCHEDULER_NO_TASK;
230 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
233 GNUNET_assert (cc != NULL);
234 GNUNET_assert (cc->p1 != NULL);
235 GNUNET_assert (cc->p2 != NULL);
237 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
239 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
240 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
241 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
242 GNUNET_HELLO_size (cc->p2->hello));
245 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
246 (const struct GNUNET_MessageHeader *) cc->
247 p2->hello, NULL, NULL);
248 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
251 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
256 * Start a peer with the given configuration
257 * @param rec receive callback
258 * @param nc connect callback
259 * @param nd disconnect callback
260 * @param cb_cls closure for callback
261 * @return the peer context
264 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
265 *tth, const char *cfgname, int peer_id,
266 GNUNET_TRANSPORT_ReceiveCallback rec,
267 GNUNET_TRANSPORT_NotifyConnect nc,
268 GNUNET_TRANSPORT_NotifyDisconnect nd,
269 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
272 const char *hostkey = NULL;
273 struct GNUNET_DISK_FileHandle *fn;
275 GNUNET_assert (tth != NULL);
276 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
278 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
279 "File not found: `%s' \n", cfgname);
283 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
285 p->cfg = GNUNET_CONFIGURATION_create ();
287 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
290 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
291 GNUNET_assert (GNUNET_OK ==
292 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
296 if (NULL != p->servicehome)
297 GNUNET_DISK_directory_remove (p->servicehome);
299 hostkey = get_host_key(tth);
303 GNUNET_asprintf (&p->hostkeyfile, "%s/.hostkey", p->servicehome);
304 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_create_for_file (p->hostkeyfile));
305 fn = GNUNET_DISK_file_open (p->hostkeyfile,
306 GNUNET_DISK_OPEN_READWRITE |
307 GNUNET_DISK_OPEN_CREATE,
308 GNUNET_DISK_PERM_USER_READ |
309 GNUNET_DISK_PERM_USER_WRITE);
310 GNUNET_assert (fn != NULL);
311 GNUNET_assert (HOSTKEYFILESIZE ==
312 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
313 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
314 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
315 "Wrote hostkey to file: `%s' \n", p->hostkeyfile);
319 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
320 "gnunet-service-arm", "-c", cfgname,
333 p->start_cb = start_cb;
340 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
341 ¬ify_connect, ¬ify_disconnect);
342 GNUNET_assert (p->th != NULL);
344 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
345 GNUNET_assert (p->ghh != NULL);
347 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
353 * shutdown the given peer
357 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
358 struct PeerContext *p)
360 GNUNET_assert (p != NULL);
363 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
367 GNUNET_TRANSPORT_disconnect (p->th);
369 if (NULL != p->arm_proc)
371 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
372 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
373 GNUNET_OS_process_wait (p->arm_proc);
374 GNUNET_OS_process_close (p->arm_proc);
378 if (p->hostkeyfile != NULL)
380 GNUNET_DISK_directory_remove (p->hostkeyfile);
381 GNUNET_free (p->hostkeyfile);
384 if (p->servicehome != NULL)
386 GNUNET_DISK_directory_remove (p->servicehome);
387 GNUNET_free (p->servicehome);
390 if (p->hello != NULL)
391 GNUNET_free (p->hello);
394 GNUNET_CONFIGURATION_destroy (p->cfg);
396 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
403 * Initiate peer p1 to connect to peer p2
404 * Get peer p2's HELLO and offer it to p1
405 * p1 then tries to connect to p2
408 * @param cb the callback to call when both peers notified that they are connected
409 * @param cb_cls callback cls (or a pointer to the
410 * GNUNET_TRANSPORT_TESTING_ConnectRequest itself if null)
411 * @return connect context
413 GNUNET_TRANSPORT_TESTING_ConnectRequest
414 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
415 *tth, struct PeerContext *p1,
416 struct PeerContext *p2,
417 GNUNET_TRANSPORT_TESTING_connect_cb cb,
420 GNUNET_assert (tth != NULL);
422 struct ConnectingContext *cc =
423 GNUNET_malloc (sizeof (struct ConnectingContext));
425 GNUNET_assert (p1 != NULL);
426 GNUNET_assert (p2 != NULL);
440 GNUNET_assert (cc->th_p1 != NULL);
441 GNUNET_assert (cc->th_p2 != NULL);
443 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
445 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
446 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
447 "New connect request %X\n", cc);
453 * Cancel the request to connect two peers
454 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
455 * @param cc a connect request handle
458 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
459 GNUNET_TRANSPORT_TESTING_handle
461 GNUNET_TRANSPORT_TESTING_ConnectRequest
464 struct ConnectingContext *cc = ccr;
466 GNUNET_assert (tth != NULL);
468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
469 "Canceling connect request %X!\n", cc);
471 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
472 GNUNET_SCHEDULER_cancel (cc->tct);
473 cc->tct = GNUNET_SCHEDULER_NO_TASK;
475 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
481 * Clean up the transport testing
482 * @param tth transport testing handle
485 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
487 struct ConnectingContext *cc = tth->cc_head;
488 struct ConnectingContext *ct = NULL;
489 struct PeerContext *p = tth->p_head;
490 struct PeerContext *t = NULL;
492 GNUNET_assert (tth != NULL);
494 while (cc != tth->cc_tail)
497 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
498 "Developer forgot to cancel connect request %X!\n", cc);
499 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
506 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
507 "Developer forgot to stop peer!\n");
508 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
512 GNUNET_free_non_null (tth->hostkey_data);
519 * Initialize the transport testing
520 * @return transport testing handle
522 struct GNUNET_TRANSPORT_TESTING_handle *
523 GNUNET_TRANSPORT_TESTING_init ()
525 struct GNUNET_TRANSPORT_TESTING_handle *tth =
526 GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
527 struct GNUNET_DISK_FileHandle *fd;
529 uint64_t total_hostkeys;
532 /* prepare hostkeys */
533 tth->hostkey_data = NULL;
534 char * hostkeys_file = "../../contrib/testing_hostkeys.dat";
535 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
538 _("Could not read hostkeys file!\n"));
542 /* Check hostkey file size, read entire thing into memory */
543 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
544 GNUNET_DISK_PERM_NONE);
547 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
552 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
555 if (0 != (fs % HOSTKEYFILESIZE))
557 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
558 "File size %llu seems incorrect for hostkeys...\n", fs);
562 total_hostkeys = fs / HOSTKEYFILESIZE;
563 tth->hostkey_data = GNUNET_malloc_large (fs);
564 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, tth->hostkey_data, fs));
565 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
566 "Read %llu hostkeys from file\n", total_hostkeys);
567 tth->hostkeys_total = total_hostkeys;
569 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
577 * Some utility functions
581 * Removes all directory separators from absolute filename
582 * @param file the absolute file name, e.g. as found in argv[0]
583 * @return extracted file name, has to be freed by caller
586 extract_filename (const char *file)
588 char *pch = GNUNET_strdup (file);
590 char *filename = NULL;
593 if (NULL != strstr (pch, "/"))
595 pch = strtok (pch, "/");
598 pch = strtok (NULL, "/");
608 res = GNUNET_strdup (filename);
609 GNUNET_free (backup);
614 * Extracts the test filename from an absolute file name and removes the extension
615 * @param file absolute file name
616 * @param dest where to store result
619 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
621 char *filename = extract_filename (file);
622 char *backup = filename;
625 if (filename == NULL)
629 filename = strstr (filename, "tes");
630 if (filename == NULL)
634 if (NULL != (dotexe = strstr (filename, ".exe")))
644 /* create filename */
645 GNUNET_asprintf (dest, "%s", filename);
646 GNUNET_free (backup);
651 * Extracts the filename from an absolute file name and removes the extension
652 * @param file absolute file name
653 * @param dest where to store result
656 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
658 char *src = extract_filename (file);
661 split = strstr (src, ".");
666 GNUNET_asprintf (dest, "%s", src);
672 * Extracts the plugin anme from an absolute file name and the test name
673 * @param file absolute file name
674 * @param test test name
675 * @param dest where to store result
678 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
679 const char *test, char **dest)
681 char *e = extract_filename (file);
682 char *t = extract_filename (test);
684 char *filename = NULL;
691 filename = strstr (e, "tes");
692 if (filename == NULL)
696 if (NULL != (dotexe = strstr (filename, ".exe")))
700 filename = strstr (filename, t);
701 if (filename == NULL)
705 filename += strlen (t);
707 GNUNET_asprintf (dest, "%s", filename);
719 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
720 * if existing ".exe"-prefix and adds the peer-number
721 * @param file filename of the test, e.g. argv[0]
722 * @param cfgname where to write the result
723 * @param count peer number
726 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
729 char *filename = extract_filename (file);
730 char *backup = filename;
733 if (filename == NULL)
737 filename = strstr (filename, "tes");
738 if (filename == NULL)
742 if (NULL != (dotexe = strstr (filename, ".exe")))
752 /* create cfg filename */
753 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
754 GNUNET_free (backup);
759 /* end of transport_testing.h */