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 HOSTKEYFILESIZE 914
33 get_host_key (struct GNUNET_TRANSPORT_TESTING_handle *tth)
35 if (tth->hostkey_data == NULL)
37 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
38 "No precomputed hostkeys available\n");
41 if (tth->hostkeys_total > tth->hostkeys_last)
44 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
45 "Used hostkey %u of %u available hostkeys\n",
46 tth->hostkeys_last, tth->hostkeys_total);
47 return &tth->hostkey_data[(tth->hostkeys_last - 1) * HOSTKEYFILESIZE];
49 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
50 "No hostkey available (%u of %u already used)\n",
51 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)))
74 static struct ConnectingContext *
75 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
76 struct PeerContext *p1, struct PeerContext *p2)
78 GNUNET_assert (tth != NULL);
79 struct ConnectingContext *cc = tth->cc_head;
83 if ((cc->p1 == p1) && (cc->p2 == p2))
85 if ((cc->p1 == p2) && (cc->p2 == p1))
95 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
96 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
98 struct PeerContext *p = cls;
100 /* Find PeerContext */
101 GNUNET_assert (p != 0);
102 GNUNET_assert (p->tth != NULL);
103 struct PeerContext *p2 = find_peer_context (p->tth, peer);
108 p->nc (p->cb_cls, peer, ats, ats_count);
113 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
115 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
116 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
117 "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
118 GNUNET_i2s (&p->id));
121 /* Find ConnectingContext */
122 struct ConnectingContext *cc = find_connecting_context (p->tth, p, p2);
128 cc->p1_c = GNUNET_YES;
131 cc->p2_c = GNUNET_YES;
133 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
135 cc->cb (cc->p1, cc->p2, cc->cb_cls);
136 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);
176 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
177 const struct GNUNET_MessageHeader *message,
178 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
180 struct PeerContext *p = cls;
185 p->rec (p->cb_cls, peer, message, ats, ats_count);
190 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
192 struct PeerContext *p = cb_cls;
194 GNUNET_assert (message != NULL);
195 GNUNET_assert (GNUNET_OK ==
196 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
198 GNUNET_free_non_null (p->hello);
199 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
201 if (p->start_cb != NULL)
203 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
204 "Peer %u (`%s') successfully started\n", p->no,
205 GNUNET_i2s (&p->id));
207 p->start_cb (p, p->cb_cls);
214 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 struct ConnectingContext *cc = cls;
217 struct PeerContext *p1 = cc->p1;
218 struct PeerContext *p2 = cc->p2;
220 cc->tct = GNUNET_SCHEDULER_NO_TASK;
221 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
224 GNUNET_assert (cc != NULL);
225 GNUNET_assert (cc->p1 != NULL);
226 GNUNET_assert (cc->p2 != NULL);
228 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
230 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
231 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
232 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
233 GNUNET_HELLO_size (cc->p2->hello));
236 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
237 (const struct GNUNET_MessageHeader *) cc->
238 p2->hello, NULL, NULL);
239 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
242 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
247 * Start a peer with the given configuration
248 * @param tth the testing handle
249 * @param cfgname configuration file
250 * @param peer_id the peer_id
251 * @param rec receive callback
252 * @param nc connect callback
253 * @param nd disconnect callback
254 * @param start_cb start callback
255 * @param cb_cls closure for callback
256 * @return the peer context
259 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
260 *tth, const char *cfgname, int peer_id,
261 GNUNET_TRANSPORT_ReceiveCallback rec,
262 GNUNET_TRANSPORT_NotifyConnect nc,
263 GNUNET_TRANSPORT_NotifyDisconnect nd,
264 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
267 const char *hostkey = NULL;
268 struct GNUNET_DISK_FileHandle *fn;
270 GNUNET_assert (tth != NULL);
271 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
273 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
274 "File not found: `%s' \n", cfgname);
278 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
280 p->cfg = GNUNET_CONFIGURATION_create ();
281 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
283 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
284 GNUNET_assert (GNUNET_OK ==
285 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
289 if (NULL != p->servicehome)
290 GNUNET_DISK_directory_remove (p->servicehome);
292 hostkey = get_host_key (tth);
296 GNUNET_asprintf (&p->hostkeyfile, "%s/.hostkey", p->servicehome);
297 GNUNET_assert (GNUNET_OK ==
298 GNUNET_DISK_directory_create_for_file (p->hostkeyfile));
299 fn = GNUNET_DISK_file_open (p->hostkeyfile,
300 GNUNET_DISK_OPEN_READWRITE |
301 GNUNET_DISK_OPEN_CREATE,
302 GNUNET_DISK_PERM_USER_READ |
303 GNUNET_DISK_PERM_USER_WRITE);
304 GNUNET_assert (fn != NULL);
305 GNUNET_assert (HOSTKEYFILESIZE ==
306 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
307 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
308 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
309 "Wrote hostkey to file: `%s' \n", p->hostkeyfile);
313 GNUNET_OS_start_process (GNUNET_YES,
314 NULL, NULL, "gnunet-service-arm",
315 "gnunet-service-arm", "-c", cfgname,
324 p->start_cb = start_cb;
331 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
332 ¬ify_connect, ¬ify_disconnect);
333 GNUNET_assert (p->th != NULL);
335 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
336 GNUNET_assert (p->ghh != NULL);
338 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
343 * Restart the given peer
344 * @param tth testing handle
346 * @param cfgname the cfg file used to restart
347 * @param restart_cb callback to call when restarted
348 * @param cb_cls callback closure
349 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
352 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
353 *tth, struct PeerContext *p,
355 GNUNET_TRANSPORT_TESTING_start_cb
356 restart_cb, void *cb_cls)
358 struct GNUNET_DISK_FileHandle *fn;
360 GNUNET_assert (tth != NULL);
361 GNUNET_assert (p != NULL);
362 GNUNET_assert (p->hostkeyfile != NULL);
363 GNUNET_assert (p->servicehome != NULL);
366 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
367 "Stopping peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
369 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
373 GNUNET_TRANSPORT_disconnect (p->th);
375 if (NULL != p->arm_proc)
377 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
378 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
379 GNUNET_OS_process_wait (p->arm_proc);
380 GNUNET_OS_process_close (p->arm_proc);
383 if (p->hello != NULL)
384 GNUNET_free (p->hello);
388 GNUNET_CONFIGURATION_destroy (p->cfg);
393 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
394 "Restarting peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
397 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
399 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
400 "File not found: `%s' \n", cfgname);
404 p->cfg = GNUNET_CONFIGURATION_create ();
405 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
407 if (!GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
410 fn = GNUNET_DISK_file_open (p->hostkeyfile,
411 GNUNET_DISK_OPEN_READWRITE |
412 GNUNET_DISK_OPEN_CREATE,
413 GNUNET_DISK_PERM_USER_READ |
414 GNUNET_DISK_PERM_USER_WRITE);
417 if (GNUNET_OK != GNUNET_DISK_file_close (fn))
421 GNUNET_OS_start_process (GNUNET_YES,
422 NULL, NULL, "gnunet-service-arm",
423 "gnunet-service-arm", "-c", cfgname,
428 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
429 ¬ify_connect, ¬ify_disconnect);
430 GNUNET_assert (p->th != NULL);
432 p->start_cb = restart_cb;
435 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
436 GNUNET_assert (p->ghh != NULL);
440 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
441 "Restarting peer %u (`%s') failed, removing peer\n", p->no,
442 GNUNET_i2s (&p->id));
443 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
444 return GNUNET_SYSERR;
449 * shutdown the given peer
450 * @param tth testing handle
454 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
455 struct PeerContext *p)
457 GNUNET_assert (p != NULL);
460 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
464 GNUNET_TRANSPORT_disconnect (p->th);
466 if (NULL != p->arm_proc)
468 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
469 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
470 GNUNET_OS_process_wait (p->arm_proc);
471 GNUNET_OS_process_close (p->arm_proc);
475 if (p->hostkeyfile != NULL)
477 GNUNET_DISK_directory_remove (p->hostkeyfile);
478 GNUNET_free (p->hostkeyfile);
481 if (p->servicehome != NULL)
483 GNUNET_DISK_directory_remove (p->servicehome);
484 GNUNET_free (p->servicehome);
487 if (p->hello != NULL)
488 GNUNET_free (p->hello);
492 GNUNET_CONFIGURATION_destroy (p->cfg);
495 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
497 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
498 "Peer %u (`%s') stopped \n", p->no,
499 GNUNET_i2s (&p->id));
507 * Connect the given peers and call the callback when both peers report the
508 * inbound connection. Remarks: start_peer's notify_connect callback can be called
511 * @param tth transport testing handle
514 * @param cb the callback to call when both peers notified that they are connected
515 * @param cls callback cls
516 * @return a connect request handle
518 GNUNET_TRANSPORT_TESTING_ConnectRequest
519 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
520 struct PeerContext *p1,
521 struct PeerContext *p2,
522 GNUNET_TRANSPORT_TESTING_connect_cb cb,
525 GNUNET_assert (tth != NULL);
527 struct ConnectingContext *cc =
528 GNUNET_malloc (sizeof (struct ConnectingContext));
530 GNUNET_assert (p1 != NULL);
531 GNUNET_assert (p2 != NULL);
545 GNUNET_assert (cc->th_p1 != NULL);
546 GNUNET_assert (cc->th_p2 != NULL);
548 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
550 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
551 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
552 "New connect request %X\n", cc);
559 * Cancel the request to connect two peers
560 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
562 * @param tth transport testing handle
563 * @param ccr a connect request handle
566 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
567 GNUNET_TRANSPORT_TESTING_handle
569 GNUNET_TRANSPORT_TESTING_ConnectRequest
572 struct ConnectingContext *cc = ccr;
574 GNUNET_assert (tth != NULL);
576 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
577 "Canceling connect request %X!\n", cc);
579 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
580 GNUNET_SCHEDULER_cancel (cc->tct);
581 cc->tct = GNUNET_SCHEDULER_NO_TASK;
583 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
589 * Clean up the transport testing
590 * @param tth transport testing handle
593 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
595 struct ConnectingContext *cc = tth->cc_head;
596 struct ConnectingContext *ct = NULL;
597 struct PeerContext *p = tth->p_head;
598 struct PeerContext *t = NULL;
600 GNUNET_assert (tth != NULL);
602 while (cc != tth->cc_tail)
605 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
606 "Developer forgot to cancel connect request %X!\n", cc);
607 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
614 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
615 "Developer forgot to stop peer!\n");
616 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
620 GNUNET_free_non_null (tth->hostkey_data);
628 * Initialize the transport testing
629 * @return transport testing handle
631 struct GNUNET_TRANSPORT_TESTING_handle *
632 GNUNET_TRANSPORT_TESTING_init ()
634 struct GNUNET_TRANSPORT_TESTING_handle *tth;
635 struct GNUNET_DISK_FileHandle *fd;
637 uint64_t total_hostkeys;
640 /* prepare hostkeys */
641 tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
642 tth->hostkey_data = NULL;
643 const char *hostkeys_file = "../../contrib/testing_hostkeys.dat";
645 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
647 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not read hostkeys file!\n"));
651 /* Check hostkey file size, read entire thing into memory */
652 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
653 GNUNET_DISK_PERM_NONE);
656 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
661 if (GNUNET_OK != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES, GNUNET_YES))
664 if (0 != (fs % HOSTKEYFILESIZE))
666 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
667 "File size %llu seems incorrect for hostkeys...\n", fs);
671 total_hostkeys = fs / HOSTKEYFILESIZE;
672 tth->hostkey_data = GNUNET_malloc_large (fs);
673 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, tth->hostkey_data, fs));
674 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
675 "Read %llu hostkeys from file\n", total_hostkeys);
676 tth->hostkeys_total = total_hostkeys;
678 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
686 * Some utility functions
690 * Removes all directory separators from absolute filename
691 * @param file the absolute file name, e.g. as found in argv[0]
692 * @return extracted file name, has to be freed by caller
695 extract_filename (const char *file)
697 char *pch = GNUNET_strdup (file);
699 char *filename = NULL;
702 if (NULL != strstr (pch, "/"))
704 pch = strtok (pch, "/");
707 pch = strtok (NULL, "/");
717 res = GNUNET_strdup (filename);
718 GNUNET_free (backup);
724 * Extracts the test filename from an absolute file name and removes the extension
725 * @param file absolute file name
726 * @param dest where to store result
729 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
731 char *filename = extract_filename (file);
732 char *backup = filename;
735 if (filename == NULL)
739 filename = strstr (filename, "tes");
740 if (filename == NULL)
744 if (NULL != (dotexe = strstr (filename, ".exe")))
754 /* create filename */
755 GNUNET_asprintf (dest, "%s", filename);
756 GNUNET_free (backup);
761 * Extracts the filename from an absolute file name and removes the extension
762 * @param file absolute file name
763 * @param dest where to store result
766 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
768 char *src = extract_filename (file);
771 split = strstr (src, ".");
776 GNUNET_asprintf (dest, "%s", src);
782 * Extracts the plugin name from an absolute file name and the test name
784 * @param file absolute file name
785 * @param test test name
786 * @param dest where to store result
789 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
790 const char *test, char **dest)
794 char *e = extract_filename (file);
795 char *t = extract_filename (test);
800 filename = strstr (e, "tes");
801 if (NULL == filename)
804 if (NULL != (dotexe = strstr (filename, ".exe")))
808 filename = strstr (filename, t);
809 if (NULL == filename)
812 filename += strlen (t);
813 if ('\0' != *filename)
815 GNUNET_asprintf (dest, "%s", filename);
826 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
827 * if existing ".exe"-prefix and adds the peer-number
829 * @param file filename of the test, e.g. argv[0]
830 * @param dest where to write the filename
831 * @param count peer number
834 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
837 char *filename = extract_filename (file);
838 char *backup = filename;
841 if (NULL == filename)
844 filename = strstr (filename, "tes");
845 if (NULL == filename)
848 if (NULL != (dotexe = strstr (filename, ".exe")))
850 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
851 GNUNET_free (backup);
855 GNUNET_free (backup);
860 /* end of transport-testing.c */