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);
55 static struct PeerContext *
56 find_peer_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
57 const struct GNUNET_PeerIdentity *peer)
59 GNUNET_assert (tth != NULL);
60 struct PeerContext *t = tth->p_head;
64 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
72 struct ConnectingContext *
73 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
74 struct PeerContext *p1, struct PeerContext *p2)
76 GNUNET_assert (tth != NULL);
77 struct ConnectingContext *cc = tth->cc_head;
81 if ((cc->p1 == p1) && (cc->p2 == p2))
83 if ((cc->p1 == p2) && (cc->p2 == p1))
92 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
93 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
95 struct PeerContext *p = cls;
97 /* Find PeerContext */
98 GNUNET_assert (p != 0);
99 GNUNET_assert (p->tth != NULL);
100 struct PeerContext *p2 = find_peer_context (p->tth, peer);
105 p->nc (p->cb_cls, peer, ats, ats_count);
111 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
113 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
114 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
115 "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
116 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);
141 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
143 struct PeerContext *p = cls;
145 /* Find PeerContext */
147 struct PeerContext *p2 = NULL;
151 GNUNET_assert (p->tth != NULL);
152 p2 = find_peer_context (p->tth, peer);
159 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
161 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
162 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
163 "Peers %s disconnected from peer %u (`%s')\n", p2_s, no,
164 GNUNET_i2s (&p->id));
170 p->nd (p->cb_cls, peer);
174 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
175 const struct GNUNET_MessageHeader *message,
176 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
178 struct PeerContext *p = cls;
183 p->rec (p->cb_cls, peer, message, ats, ats_count);
187 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
189 struct PeerContext *p = cb_cls;
191 GNUNET_assert (message != NULL);
192 GNUNET_assert (GNUNET_OK ==
193 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
197 GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) message);
199 GNUNET_free_non_null (p->hello);
200 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
203 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
204 "New HELLO for peer %u (`%s') with size %u\n", p->no,
205 GNUNET_i2s (&p->id), size);
208 if (p->start_cb != NULL)
211 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
212 "Peer %u (`%s') successfully started\n", p->no,
213 GNUNET_i2s (&p->id));
215 p->start_cb (p, p->cb_cls);
222 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
224 struct ConnectingContext *cc = cls;
225 struct PeerContext *p1 = cc->p1;
226 struct PeerContext *p2 = cc->p2;
228 cc->tct = GNUNET_SCHEDULER_NO_TASK;
229 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
232 GNUNET_assert (cc != NULL);
233 GNUNET_assert (cc->p1 != NULL);
234 GNUNET_assert (cc->p2 != NULL);
236 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
238 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
239 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
240 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
241 GNUNET_HELLO_size (cc->p2->hello));
244 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
245 (const struct GNUNET_MessageHeader *) cc->
246 p2->hello, NULL, NULL);
247 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
250 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
255 * Start a peer with the given configuration
256 * @param tth the testing handle
257 * @param cfgname configuration file
258 * @param peer_id the peer_id
259 * @param rec receive callback
260 * @param nc connect callback
261 * @param nd disconnect callback
262 * @param start_cb start callback
263 * @param cb_cls closure for callback
264 * @return the peer context
267 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
268 *tth, const char *cfgname, int peer_id,
269 GNUNET_TRANSPORT_ReceiveCallback rec,
270 GNUNET_TRANSPORT_NotifyConnect nc,
271 GNUNET_TRANSPORT_NotifyDisconnect nd,
272 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
275 const char *hostkey = NULL;
276 struct GNUNET_DISK_FileHandle *fn;
278 GNUNET_assert (tth != NULL);
279 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
281 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
282 "File not found: `%s' \n", cfgname);
286 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
288 p->cfg = GNUNET_CONFIGURATION_create ();
289 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
291 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
292 GNUNET_assert (GNUNET_OK ==
293 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
297 if (NULL != p->servicehome)
298 GNUNET_DISK_directory_remove (p->servicehome);
300 hostkey = get_host_key (tth);
304 GNUNET_asprintf (&p->hostkeyfile, "%s/.hostkey", p->servicehome);
305 GNUNET_assert (GNUNET_OK ==
306 GNUNET_DISK_directory_create_for_file (p->hostkeyfile));
307 fn = GNUNET_DISK_file_open (p->hostkeyfile,
308 GNUNET_DISK_OPEN_READWRITE |
309 GNUNET_DISK_OPEN_CREATE,
310 GNUNET_DISK_PERM_USER_READ |
311 GNUNET_DISK_PERM_USER_WRITE);
312 GNUNET_assert (fn != NULL);
313 GNUNET_assert (HOSTKEYFILESIZE ==
314 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
315 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
316 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
317 "Wrote hostkey to file: `%s' \n", p->hostkeyfile);
321 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
322 "gnunet-service-arm", "-c", cfgname,
335 p->start_cb = start_cb;
342 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
343 ¬ify_connect, ¬ify_disconnect);
344 GNUNET_assert (p->th != NULL);
346 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
347 GNUNET_assert (p->ghh != NULL);
349 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
355 * Restart the given peer
356 * @param tth testing handle
358 * @param cfgname the cfg file used to restart
359 * @param restart_cb callback to call when restarted
360 * @param cb_cls callback closure
361 * @return GNUNET_OK in success otherwise GNUNET_SYSERR
364 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle
365 *tth, struct PeerContext *p,
367 GNUNET_TRANSPORT_TESTING_start_cb
368 restart_cb, void *cb_cls)
370 struct GNUNET_DISK_FileHandle *fn;
372 GNUNET_assert (tth != NULL);
373 GNUNET_assert (p != NULL);
374 GNUNET_assert (p->hostkeyfile != NULL);
375 GNUNET_assert (p->servicehome != NULL);
379 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
380 "Stopping peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
383 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
387 GNUNET_TRANSPORT_disconnect (p->th);
389 if (NULL != p->arm_proc)
391 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
392 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
393 GNUNET_OS_process_wait (p->arm_proc);
394 GNUNET_OS_process_close (p->arm_proc);
397 if (p->hello != NULL)
398 GNUNET_free (p->hello);
402 GNUNET_CONFIGURATION_destroy (p->cfg);
408 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
409 "Restarting peer %u (`%s')\n", p->no, GNUNET_i2s (&p->id));
414 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
416 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
417 "File not found: `%s' \n", cfgname);
421 p->cfg = GNUNET_CONFIGURATION_create ();
422 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
424 if (!GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
427 fn = GNUNET_DISK_file_open (p->hostkeyfile,
428 GNUNET_DISK_OPEN_READWRITE |
429 GNUNET_DISK_OPEN_CREATE,
430 GNUNET_DISK_PERM_USER_READ |
431 GNUNET_DISK_PERM_USER_WRITE);
434 if (GNUNET_OK != GNUNET_DISK_file_close (fn))
438 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
439 "gnunet-service-arm", "-c", cfgname,
448 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
449 ¬ify_connect, ¬ify_disconnect);
450 GNUNET_assert (p->th != NULL);
452 p->start_cb = restart_cb;
455 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
456 GNUNET_assert (p->ghh != NULL);
460 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
461 "Restarting peer %u (`%s') failed, removing peer\n", p->no,
462 GNUNET_i2s (&p->id));
463 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
464 return GNUNET_SYSERR;
468 * shutdown the given peer
469 * @param tth testing handle
473 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
474 struct PeerContext *p)
476 GNUNET_assert (p != NULL);
479 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
483 GNUNET_TRANSPORT_disconnect (p->th);
485 if (NULL != p->arm_proc)
487 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
488 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
489 GNUNET_OS_process_wait (p->arm_proc);
490 GNUNET_OS_process_close (p->arm_proc);
494 if (p->hostkeyfile != NULL)
496 GNUNET_DISK_directory_remove (p->hostkeyfile);
497 GNUNET_free (p->hostkeyfile);
500 if (p->servicehome != NULL)
502 GNUNET_DISK_directory_remove (p->servicehome);
503 GNUNET_free (p->servicehome);
506 if (p->hello != NULL)
507 GNUNET_free (p->hello);
511 GNUNET_CONFIGURATION_destroy (p->cfg);
514 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
521 * Connect the given peers and call the callback when both peers report the
522 * inbound connection. Remarks: start_peer's notify_connect callback can be called
525 * @param tth transport testing handle
528 * @param cb the callback to call when both peers notified that they are connected
529 * @param cls callback cls
530 * @return a connect request handle
532 GNUNET_TRANSPORT_TESTING_ConnectRequest
533 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle *tth,
534 struct PeerContext *p1,
535 struct PeerContext *p2,
536 GNUNET_TRANSPORT_TESTING_connect_cb cb,
539 GNUNET_assert (tth != NULL);
541 struct ConnectingContext *cc =
542 GNUNET_malloc (sizeof (struct ConnectingContext));
544 GNUNET_assert (p1 != NULL);
545 GNUNET_assert (p2 != NULL);
559 GNUNET_assert (cc->th_p1 != NULL);
560 GNUNET_assert (cc->th_p2 != NULL);
562 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
564 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
565 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
566 "New connect request %X\n", cc);
572 * Cancel the request to connect two peers
573 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
575 * @param tth transport testing handle
576 * @param ccr a connect request handle
579 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
580 GNUNET_TRANSPORT_TESTING_handle
582 GNUNET_TRANSPORT_TESTING_ConnectRequest
585 struct ConnectingContext *cc = ccr;
587 GNUNET_assert (tth != NULL);
589 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
590 "Canceling connect request %X!\n", cc);
592 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
593 GNUNET_SCHEDULER_cancel (cc->tct);
594 cc->tct = GNUNET_SCHEDULER_NO_TASK;
596 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
602 * Clean up the transport testing
603 * @param tth transport testing handle
606 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
608 struct ConnectingContext *cc = tth->cc_head;
609 struct ConnectingContext *ct = NULL;
610 struct PeerContext *p = tth->p_head;
611 struct PeerContext *t = NULL;
613 GNUNET_assert (tth != NULL);
615 while (cc != tth->cc_tail)
618 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
619 "Developer forgot to cancel connect request %X!\n", cc);
620 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
627 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
628 "Developer forgot to stop peer!\n");
629 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
633 GNUNET_free_non_null (tth->hostkey_data);
640 * Initialize the transport testing
641 * @return transport testing handle
643 struct GNUNET_TRANSPORT_TESTING_handle *
644 GNUNET_TRANSPORT_TESTING_init ()
646 struct GNUNET_TRANSPORT_TESTING_handle *tth;
647 struct GNUNET_DISK_FileHandle *fd;
649 uint64_t total_hostkeys;
652 /* prepare hostkeys */
653 tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
654 tth->hostkey_data = NULL;
655 const char *hostkeys_file = "../../contrib/testing_hostkeys.dat";
657 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not read hostkeys file!\n"));
663 /* Check hostkey file size, read entire thing into memory */
664 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
665 GNUNET_DISK_PERM_NONE);
668 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file);
673 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
676 if (0 != (fs % HOSTKEYFILESIZE))
678 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
679 "File size %llu seems incorrect for hostkeys...\n", fs);
683 total_hostkeys = fs / HOSTKEYFILESIZE;
684 tth->hostkey_data = GNUNET_malloc_large (fs);
685 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, tth->hostkey_data, fs));
686 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
687 "Read %llu hostkeys from file\n", total_hostkeys);
688 tth->hostkeys_total = total_hostkeys;
690 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
698 * Some utility functions
702 * Removes all directory separators from absolute filename
703 * @param file the absolute file name, e.g. as found in argv[0]
704 * @return extracted file name, has to be freed by caller
707 extract_filename (const char *file)
709 char *pch = GNUNET_strdup (file);
711 char *filename = NULL;
714 if (NULL != strstr (pch, "/"))
716 pch = strtok (pch, "/");
719 pch = strtok (NULL, "/");
729 res = GNUNET_strdup (filename);
730 GNUNET_free (backup);
735 * Extracts the test filename from an absolute file name and removes the extension
736 * @param file absolute file name
737 * @param dest where to store result
740 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
742 char *filename = extract_filename (file);
743 char *backup = filename;
746 if (filename == NULL)
750 filename = strstr (filename, "tes");
751 if (filename == NULL)
755 if (NULL != (dotexe = strstr (filename, ".exe")))
765 /* create filename */
766 GNUNET_asprintf (dest, "%s", filename);
767 GNUNET_free (backup);
772 * Extracts the filename from an absolute file name and removes the extension
773 * @param file absolute file name
774 * @param dest where to store result
777 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
779 char *src = extract_filename (file);
782 split = strstr (src, ".");
787 GNUNET_asprintf (dest, "%s", src);
793 * Extracts the plugin anme from an absolute file name and the test name
794 * @param file absolute file name
795 * @param test test name
796 * @param dest where to store result
799 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
800 const char *test, char **dest)
802 char *e = extract_filename (file);
803 char *t = extract_filename (test);
805 char *filename = NULL;
812 filename = strstr (e, "tes");
813 if (filename == NULL)
817 if (NULL != (dotexe = strstr (filename, ".exe")))
821 filename = strstr (filename, t);
822 if (filename == NULL)
826 filename += strlen (t);
828 GNUNET_asprintf (dest, "%s", filename);
840 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
841 * if existing ".exe"-prefix and adds the peer-number
843 * @param file filename of the test, e.g. argv[0]
844 * @param dest where to write the filename
845 * @param count peer number
848 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
851 char *filename = extract_filename (file);
852 char *backup = filename;
855 if (filename == NULL)
859 filename = strstr (filename, "tes");
860 if (filename == NULL)
864 if (NULL != (dotexe = strstr (filename, ".exe")))
874 /* create cfg filename */
875 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
876 GNUNET_free (backup);
881 /* end of transport_testing.h */