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_YES
33 static struct PeerContext *
34 find_peer_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
35 const struct GNUNET_PeerIdentity *peer)
37 GNUNET_assert (tth != NULL);
38 struct PeerContext *t = tth->p_head;
42 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
50 struct ConnectingContext *
51 find_connecting_context (struct GNUNET_TRANSPORT_TESTING_handle *tth,
52 struct PeerContext *p1, struct PeerContext *p2)
54 GNUNET_assert (tth != NULL);
55 struct ConnectingContext *cc = tth->cc_head;
59 if ((cc->p1 == p1) && (cc->p2 == p2))
61 if ((cc->p1 == p2) && (cc->p2 == p1))
70 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
71 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
73 struct PeerContext *p = cls;
75 /* Find PeerContext */
76 GNUNET_assert (p != 0);
77 GNUNET_assert (p->tth != NULL);
78 struct PeerContext *p2 = find_peer_context (p->tth, peer);
83 p->nc (p->cb_cls, peer, ats, ats_count);
89 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
91 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
92 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
93 "Peers %s connected to peer %u (`%s')\n", p2_s, p->no,
99 /* Find ConnectingContext */
100 struct ConnectingContext *cc = find_connecting_context (p->tth, p, p2);
106 cc->p1_c = GNUNET_YES;
109 cc->p2_c = GNUNET_YES;
111 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
113 cc->cb (cc->p1, cc->p2, cc->cb_cls);
114 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (p->tth, cc);
119 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
121 struct PeerContext *p = cls;
123 /* Find PeerContext */
125 struct PeerContext *p2 = NULL;
129 GNUNET_assert (p->tth != NULL);
130 p2 = find_peer_context (p->tth, peer);
137 GNUNET_asprintf (&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
139 GNUNET_asprintf (&p2_s, "`%s'", GNUNET_i2s (peer));
140 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
141 "Peers %s disconnected from peer %u (`%s')\n", p2_s, no,
142 GNUNET_i2s (&p->id));
148 p->nd (p->cb_cls, peer);
152 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
153 const struct GNUNET_MessageHeader *message,
154 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
156 struct PeerContext *p = cls;
161 p->rec (p->cb_cls, peer, message, ats, ats_count);
165 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
167 struct PeerContext *p = cb_cls;
169 GNUNET_assert (message != NULL);
170 GNUNET_assert (GNUNET_OK ==
171 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
174 GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) message);
175 GNUNET_free_non_null (p->hello);
176 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
179 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
180 "New HELLO for peer %u (`%s') with size %u\n", p->no,
181 GNUNET_i2s (&p->id), size);
184 if (p->start_cb != NULL)
187 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
188 "Peer %u (`%s') successfully started\n", p->no,
189 GNUNET_i2s (&p->id));
191 p->start_cb (p, p->cb_cls);
198 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 struct ConnectingContext *cc = cls;
201 struct PeerContext *p1 = cc->p1;
202 struct PeerContext *p2 = cc->p2;
204 cc->tct = GNUNET_SCHEDULER_NO_TASK;
205 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
208 GNUNET_assert (cc != NULL);
209 GNUNET_assert (cc->p1 != NULL);
210 GNUNET_assert (cc->p2 != NULL);
212 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
214 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
215 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
216 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
217 GNUNET_HELLO_size (cc->p2->hello));
220 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
221 (const struct GNUNET_MessageHeader *) cc->
222 p2->hello, NULL, NULL);
223 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
226 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
231 * Start a peer with the given configuration
232 * @param rec receive callback
233 * @param nc connect callback
234 * @param nd disconnect callback
235 * @param cb_cls closure for callback
236 * @return the peer context
239 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
240 *tth, const char *cfgname, int peer_id,
241 GNUNET_TRANSPORT_ReceiveCallback rec,
242 GNUNET_TRANSPORT_NotifyConnect nc,
243 GNUNET_TRANSPORT_NotifyDisconnect nd,
244 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
247 GNUNET_assert (tth != NULL);
248 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
250 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
251 "File not found: `%s' \n", cfgname);
255 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
257 p->cfg = GNUNET_CONFIGURATION_create ();
259 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
260 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
261 GNUNET_assert (GNUNET_OK ==
262 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
265 if (NULL != p->servicehome)
266 GNUNET_DISK_directory_remove (p->servicehome);
268 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
269 "gnunet-service-arm", "-c", cfgname,
282 p->start_cb = start_cb;
289 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
290 ¬ify_connect, ¬ify_disconnect);
291 GNUNET_assert (p->th != NULL);
293 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
294 GNUNET_assert (p->ghh != NULL);
296 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
302 * shutdown the given peer
306 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
307 struct PeerContext *p)
309 GNUNET_assert (p != NULL);
312 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
316 GNUNET_TRANSPORT_disconnect (p->th);
318 if (NULL != p->arm_proc)
320 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
321 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
322 GNUNET_OS_process_wait (p->arm_proc);
323 GNUNET_OS_process_close (p->arm_proc);
327 if (p->servicehome != NULL)
329 GNUNET_DISK_directory_remove (p->servicehome);
330 GNUNET_free (p->servicehome);
333 if (p->hello != NULL)
334 GNUNET_free (p->hello);
337 GNUNET_CONFIGURATION_destroy (p->cfg);
339 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
346 * Initiate peer p1 to connect to peer p2
347 * Get peer p2's HELLO and offer it to p1
348 * p1 then tries to connect to p2
351 * @param cb the callback to call when both peers notified that they are connected
352 * @param cb_cls callback cls
353 * @return connect context
355 GNUNET_TRANSPORT_TESTING_ConnectRequest
356 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
357 *tth, struct PeerContext *p1,
358 struct PeerContext *p2,
359 GNUNET_TRANSPORT_TESTING_connect_cb cb,
362 GNUNET_assert (tth != NULL);
364 struct ConnectingContext *cc =
365 GNUNET_malloc (sizeof (struct ConnectingContext));
367 GNUNET_assert (p1 != NULL);
368 GNUNET_assert (p2 != NULL);
379 GNUNET_assert (cc->th_p1 != NULL);
380 GNUNET_assert (cc->th_p2 != NULL);
382 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
384 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
385 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
386 "New connect request %X\n", cc);
392 * Cancel the request to connect two peers
393 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
394 * @param cc a connect request handle
397 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
398 GNUNET_TRANSPORT_TESTING_handle
400 GNUNET_TRANSPORT_TESTING_ConnectRequest
403 struct ConnectingContext *cc = ccr;
405 GNUNET_assert (tth != NULL);
407 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
408 "Canceling connect request %X!\n", cc);
410 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
411 GNUNET_SCHEDULER_cancel (cc->tct);
412 cc->tct = GNUNET_SCHEDULER_NO_TASK;
414 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
420 * Clean up the transport testing
421 * @param tth transport testing handle
424 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
426 struct ConnectingContext *cc = tth->cc_head;
427 struct ConnectingContext *ct = NULL;
428 struct PeerContext *p = tth->p_head;
429 struct PeerContext *t = NULL;
431 GNUNET_assert (tth != NULL);
433 while (cc != tth->cc_tail)
436 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
437 "Developer forgot to cancel connect request %X!\n", cc);
438 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
445 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
446 "Developer forgot to stop peer!\n");
447 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
456 * Initialize the transport testing
457 * @return transport testing handle
459 struct GNUNET_TRANSPORT_TESTING_handle *
460 GNUNET_TRANSPORT_TESTING_init ()
462 struct GNUNET_TRANSPORT_TESTING_handle *tth =
463 GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
470 * Some utility functions
474 * Removes all directory separators from absolute filename
475 * @param file the absolute file name, e.g. as found in argv[0]
476 * @return extracted file name, has to be freed by caller
479 extract_filename (const char *file)
481 char *pch = GNUNET_strdup (file);
483 char *filename = NULL;
486 if (NULL != strstr (pch, "/"))
488 pch = strtok (pch, "/");
491 pch = strtok (NULL, "/");
501 res = GNUNET_strdup (filename);
502 GNUNET_free (backup);
507 * Extracts the test filename from an absolute file name and removes the extension
508 * @param file absolute file name
509 * @param dest where to store result
512 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
514 char *filename = extract_filename (file);
515 char *backup = filename;
518 if (filename == NULL)
522 filename = strstr (filename, "tes");
523 if (filename == NULL)
527 if (NULL != (dotexe = strstr (filename, ".exe")))
537 /* create filename */
538 GNUNET_asprintf (dest, "%s", filename);
539 GNUNET_free (backup);
544 * Extracts the filename from an absolute file name and removes the extension
545 * @param file absolute file name
546 * @param dest where to store result
549 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
551 char *src = extract_filename (file);
554 split = strstr (src, ".");
559 GNUNET_asprintf (dest, "%s", src);
565 * Extracts the plugin anme from an absolute file name and the test name
566 * @param file absolute file name
567 * @param test test name
568 * @param dest where to store result
571 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
572 const char *test, char **dest)
574 char *e = extract_filename (file);
575 char *t = extract_filename (test);
577 char *filename = NULL;
584 filename = strstr (e, "tes");
585 if (filename == NULL)
589 if (NULL != (dotexe = strstr (filename, ".exe")))
593 filename = strstr (filename, t);
594 if (filename == NULL)
598 filename += strlen (t);
600 GNUNET_asprintf (dest, "%s", filename);
612 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
613 * if existing ".exe"-prefix and adds the peer-number
614 * @param file filename of the test, e.g. argv[0]
615 * @param cfgname where to write the result
616 * @param count peer number
619 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
622 char *filename = extract_filename (file);
623 char *backup = filename;
626 if (filename == NULL)
630 filename = strstr (filename, "tes");
631 if (filename == NULL)
635 if (NULL != (dotexe = strstr (filename, ".exe")))
645 /* create cfg filename */
646 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
647 GNUNET_free (backup);
652 /* end of transport_testing.h */