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 ();
286 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
288 if (GNUNET_CONFIGURATION_have_value (p->
289 cfg, "PATHS", "SERVICEHOME"))
290 GNUNET_assert (GNUNET_OK ==
291 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
295 if (NULL != p->servicehome)
296 GNUNET_DISK_directory_remove (p->servicehome);
298 hostkey = get_host_key(tth);
302 GNUNET_asprintf (&p->hostkeyfile, "%s/.hostkey", p->servicehome);
303 GNUNET_assert(GNUNET_OK == GNUNET_DISK_directory_create_for_file (p->hostkeyfile));
304 fn = GNUNET_DISK_file_open (p->hostkeyfile,
305 GNUNET_DISK_OPEN_READWRITE |
306 GNUNET_DISK_OPEN_CREATE,
307 GNUNET_DISK_PERM_USER_READ |
308 GNUNET_DISK_PERM_USER_WRITE);
309 GNUNET_assert (fn != NULL);
310 GNUNET_assert (HOSTKEYFILESIZE ==
311 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
312 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
313 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
314 "Wrote hostkey to file: `%s' \n", p->hostkeyfile);
318 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
319 "gnunet-service-arm", "-c", cfgname,
332 p->start_cb = start_cb;
339 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
340 ¬ify_connect, ¬ify_disconnect);
341 GNUNET_assert (p->th != NULL);
343 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
344 GNUNET_assert (p->ghh != NULL);
346 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
352 * Restart the given peer
353 * @param tth testing handle
357 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
358 struct PeerContext *p,
361 GNUNET_assert (p != NULL);
364 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
365 "Stopping peer %u (`%s')\n", p->no,
366 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,
395 GNUNET_i2s (&p->id));
397 struct GNUNET_DISK_FileHandle *fn;
399 GNUNET_assert (tth != NULL);
400 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
402 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
403 "File not found: `%s' \n", cfgname);
407 p->cfg = GNUNET_CONFIGURATION_create ();
408 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
409 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
410 GNUNET_assert (GNUNET_OK ==
411 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
415 GNUNET_assert (p->hostkeyfile != NULL);
416 GNUNET_asprintf (&p->hostkeyfile, "%s/.hostkey", p->servicehome);
417 fn = GNUNET_DISK_file_open (p->hostkeyfile,
418 GNUNET_DISK_OPEN_READWRITE |
419 GNUNET_DISK_OPEN_CREATE,
420 GNUNET_DISK_PERM_USER_READ |
421 GNUNET_DISK_PERM_USER_WRITE);
422 GNUNET_assert (fn != NULL);
423 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
425 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
426 "gnunet-service-arm", "-c", cfgname,
435 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
436 ¬ify_connect, ¬ify_disconnect);
437 GNUNET_assert (p->th != NULL);
439 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
440 GNUNET_assert (p->ghh != NULL);
446 * shutdown the given peer
450 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
451 struct PeerContext *p)
453 GNUNET_assert (p != NULL);
456 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
460 GNUNET_TRANSPORT_disconnect (p->th);
462 if (NULL != p->arm_proc)
464 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
465 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
466 GNUNET_OS_process_wait (p->arm_proc);
467 GNUNET_OS_process_close (p->arm_proc);
471 if (p->hostkeyfile != NULL)
473 GNUNET_DISK_directory_remove (p->hostkeyfile);
474 GNUNET_free (p->hostkeyfile);
477 if (p->servicehome != NULL)
479 GNUNET_DISK_directory_remove (p->servicehome);
480 GNUNET_free (p->servicehome);
483 if (p->hello != NULL)
484 GNUNET_free (p->hello);
488 GNUNET_CONFIGURATION_destroy (p->cfg);
491 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
498 * Initiate peer p1 to connect to peer p2
499 * Get peer p2's HELLO and offer it to p1
500 * p1 then tries to connect to p2
503 * @param cb the callback to call when both peers notified that they are connected
504 * @param cb_cls callback cls (or a pointer to the
505 * GNUNET_TRANSPORT_TESTING_ConnectRequest itself if null)
506 * @return connect context
508 GNUNET_TRANSPORT_TESTING_ConnectRequest
509 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
510 *tth, struct PeerContext *p1,
511 struct PeerContext *p2,
512 GNUNET_TRANSPORT_TESTING_connect_cb cb,
515 GNUNET_assert (tth != NULL);
517 struct ConnectingContext *cc =
518 GNUNET_malloc (sizeof (struct ConnectingContext));
520 GNUNET_assert (p1 != NULL);
521 GNUNET_assert (p2 != NULL);
535 GNUNET_assert (cc->th_p1 != NULL);
536 GNUNET_assert (cc->th_p2 != NULL);
538 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
540 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
541 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
542 "New connect request %X\n", cc);
548 * Cancel the request to connect two peers
549 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
550 * @param cc a connect request handle
553 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
554 GNUNET_TRANSPORT_TESTING_handle
556 GNUNET_TRANSPORT_TESTING_ConnectRequest
559 struct ConnectingContext *cc = ccr;
561 GNUNET_assert (tth != NULL);
563 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
564 "Canceling connect request %X!\n", cc);
566 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
567 GNUNET_SCHEDULER_cancel (cc->tct);
568 cc->tct = GNUNET_SCHEDULER_NO_TASK;
570 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
576 * Clean up the transport testing
577 * @param tth transport testing handle
580 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
582 struct ConnectingContext *cc = tth->cc_head;
583 struct ConnectingContext *ct = NULL;
584 struct PeerContext *p = tth->p_head;
585 struct PeerContext *t = NULL;
587 GNUNET_assert (tth != NULL);
589 while (cc != tth->cc_tail)
592 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
593 "Developer forgot to cancel connect request %X!\n", cc);
594 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
601 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
602 "Developer forgot to stop peer!\n");
603 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
607 GNUNET_free_non_null (tth->hostkey_data);
614 * Initialize the transport testing
615 * @return transport testing handle
617 struct GNUNET_TRANSPORT_TESTING_handle *
618 GNUNET_TRANSPORT_TESTING_init ()
620 struct GNUNET_TRANSPORT_TESTING_handle *tth =
621 GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
622 struct GNUNET_DISK_FileHandle *fd;
624 uint64_t total_hostkeys;
627 /* prepare hostkeys */
628 tth->hostkey_data = NULL;
629 char * hostkeys_file = "../../contrib/testing_hostkeys.dat";
630 if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
633 _("Could not read hostkeys file!\n"));
637 /* Check hostkey file size, read entire thing into memory */
638 fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ,
639 GNUNET_DISK_PERM_NONE);
642 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open",
647 if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
650 if (0 != (fs % HOSTKEYFILESIZE))
652 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
653 "File size %llu seems incorrect for hostkeys...\n", fs);
657 total_hostkeys = fs / HOSTKEYFILESIZE;
658 tth->hostkey_data = GNUNET_malloc_large (fs);
659 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, tth->hostkey_data, fs));
660 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
661 "Read %llu hostkeys from file\n", total_hostkeys);
662 tth->hostkeys_total = total_hostkeys;
664 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
672 * Some utility functions
676 * Removes all directory separators from absolute filename
677 * @param file the absolute file name, e.g. as found in argv[0]
678 * @return extracted file name, has to be freed by caller
681 extract_filename (const char *file)
683 char *pch = GNUNET_strdup (file);
685 char *filename = NULL;
688 if (NULL != strstr (pch, "/"))
690 pch = strtok (pch, "/");
693 pch = strtok (NULL, "/");
703 res = GNUNET_strdup (filename);
704 GNUNET_free (backup);
709 * Extracts the test filename from an absolute file name and removes the extension
710 * @param file absolute file name
711 * @param dest where to store result
714 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
716 char *filename = extract_filename (file);
717 char *backup = filename;
720 if (filename == NULL)
724 filename = strstr (filename, "tes");
725 if (filename == NULL)
729 if (NULL != (dotexe = strstr (filename, ".exe")))
739 /* create filename */
740 GNUNET_asprintf (dest, "%s", filename);
741 GNUNET_free (backup);
746 * Extracts the filename from an absolute file name and removes the extension
747 * @param file absolute file name
748 * @param dest where to store result
751 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
753 char *src = extract_filename (file);
756 split = strstr (src, ".");
761 GNUNET_asprintf (dest, "%s", src);
767 * Extracts the plugin anme from an absolute file name and the test name
768 * @param file absolute file name
769 * @param test test name
770 * @param dest where to store result
773 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
774 const char *test, char **dest)
776 char *e = extract_filename (file);
777 char *t = extract_filename (test);
779 char *filename = NULL;
786 filename = strstr (e, "tes");
787 if (filename == NULL)
791 if (NULL != (dotexe = strstr (filename, ".exe")))
795 filename = strstr (filename, t);
796 if (filename == NULL)
800 filename += strlen (t);
802 GNUNET_asprintf (dest, "%s", filename);
814 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
815 * if existing ".exe"-prefix and adds the peer-number
816 * @param file filename of the test, e.g. argv[0]
817 * @param cfgname where to write the result
818 * @param count peer number
821 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
824 char *filename = extract_filename (file);
825 char *backup = filename;
828 if (filename == NULL)
832 filename = strstr (filename, "tes");
833 if (filename == NULL)
837 if (NULL != (dotexe = strstr (filename, ".exe")))
847 /* create cfg filename */
848 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
849 GNUNET_free (backup);
854 /* end of transport_testing.h */