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
32 static struct PeerContext *
33 find_peer_context_by_pc ( struct GNUNET_TRANSPORT_TESTING_handle *tth,
34 struct PeerContext *p)
36 GNUNET_assert (tth != NULL);
37 struct PeerContext * t = tth->p_head;
50 static struct PeerContext *
51 find_peer_context ( struct GNUNET_TRANSPORT_TESTING_handle *tth,
52 const struct GNUNET_PeerIdentity *peer)
54 GNUNET_assert (tth != NULL);
55 struct PeerContext * t = tth->p_head;
59 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
67 struct ConnectingContext *
68 find_connecting_context ( struct GNUNET_TRANSPORT_TESTING_handle *tth,
69 struct PeerContext *p1,
70 struct PeerContext * p2)
72 GNUNET_assert (tth != NULL);
73 struct ConnectingContext * cc = tth->cc_head;
77 if ((cc->p1 == p1) && (cc->p2 == p2))
79 if ((cc->p1 == p2) && (cc->p2 == p1))
88 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
89 const struct GNUNET_ATS_Information *ats,
92 struct PeerContext *p = cls;
93 /* Find PeerContext */
94 GNUNET_assert (p != 0);
95 GNUNET_assert (p->tth != NULL);
96 struct PeerContext * p2 = find_peer_context (p->tth, peer);
101 p->nc (p->cb_cls, peer, ats, ats_count);
106 GNUNET_asprintf(&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
108 GNUNET_asprintf(&p2_s, "`%s'", GNUNET_i2s (peer));
109 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
110 "Peers %s connected to peer %u (`%s')\n",
112 p->no, GNUNET_i2s (&p->id));
117 /* Find ConnectingContext */
118 struct ConnectingContext * cc = find_connecting_context(p->tth, p, p2);
123 cc->p1_c = GNUNET_YES;
126 cc->p2_c = GNUNET_YES;
128 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
130 cc->cb (cc->p1, cc->p2, cc->cb_cls);
131 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(p->tth, cc);
136 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
138 struct PeerContext *p = cls;
139 /* Find PeerContext */
141 struct PeerContext * p2 = NULL;
145 GNUNET_assert (p->tth != NULL);
146 p2 = find_peer_context (p->tth, peer);
152 GNUNET_asprintf(&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
154 GNUNET_asprintf(&p2_s, "`%s'", GNUNET_i2s (peer));
155 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
156 "Peers %s disconnected from peer %u (`%s')\n",
158 no , GNUNET_i2s (&p->id));
164 p->nd (p->cb_cls, peer);
168 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
169 const struct GNUNET_MessageHeader *message,
170 const struct GNUNET_ATS_Information *ats,
173 struct PeerContext *p = cls;
178 p->rec (p->cb_cls, peer, message, ats, ats_count);
182 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
184 struct PeerContext *p = cb_cls;
186 GNUNET_assert (message != NULL);
187 GNUNET_assert (GNUNET_OK ==
188 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
190 size_t size = GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *) message);
191 GNUNET_free_non_null (p->hello);
192 p->hello = (struct GNUNET_HELLO_Message*) GNUNET_copy_message (message);
195 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
197 "New HELLO for peer %u (`%s') with size %u\n",
198 p->no, GNUNET_i2s (&p->id), size);
201 if (p->start_cb != NULL)
204 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
205 "Peer %u (`%s') successfully started\n",
206 p->no, GNUNET_i2s (&p->id));
208 p->start_cb(p, p->cb_cls);
215 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 struct ConnectingContext *cc = cls;
218 struct PeerContext *p1 = cc->p1;
219 struct PeerContext *p2 = cc->p2;
221 cc->tct = GNUNET_SCHEDULER_NO_TASK;
222 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
225 char * p2_s = GNUNET_strdup(GNUNET_i2s (&p2->id));
226 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
227 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
228 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
229 GNUNET_HELLO_size (cc->p2->hello));
232 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
233 (const struct GNUNET_MessageHeader *) cc->p2->hello, NULL, NULL);
234 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
237 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
242 * Start a peer with the given configuration
243 * @param rec receive callback
244 * @param nc connect callback
245 * @param nd disconnect callback
246 * @param cb_cls closure for callback
247 * @return the peer context
250 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle * tth,
253 GNUNET_TRANSPORT_ReceiveCallback rec,
254 GNUNET_TRANSPORT_NotifyConnect nc,
255 GNUNET_TRANSPORT_NotifyDisconnect nd,
256 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
259 GNUNET_assert (tth != NULL);
260 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
262 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
263 "File not found: `%s' \n", cfgname);
267 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
269 p->cfg = GNUNET_CONFIGURATION_create ();
271 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
272 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
273 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
275 if (NULL != p->servicehome)
276 GNUNET_DISK_directory_remove (p->servicehome);
278 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
279 "gnunet-service-arm", "-c", cfgname,
292 p->start_cb = start_cb;
299 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
300 ¬ify_connect, ¬ify_disconnect);
301 GNUNET_assert (p->th != NULL);
303 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
304 GNUNET_assert (p->ghh != NULL);
306 GNUNET_CONTAINER_DLL_insert(tth->p_head, tth->p_tail, p);
312 * shutdown the given peer
316 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle * tth,
317 struct PeerContext *p)
319 GNUNET_assert (p != NULL);
322 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
326 GNUNET_TRANSPORT_disconnect (p->th);
328 if (NULL != p->arm_proc)
330 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
331 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
332 GNUNET_OS_process_wait (p->arm_proc);
333 GNUNET_OS_process_close (p->arm_proc);
337 if (p->servicehome != NULL)
339 GNUNET_DISK_directory_remove (p->servicehome);
340 GNUNET_free (p->servicehome);
343 if (p->hello != NULL)
344 GNUNET_free (p->hello);
347 GNUNET_CONFIGURATION_destroy (p->cfg);
349 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
356 * Initiate peer p1 to connect to peer p2
357 * Get peer p2's HELLO and offer it to p1
358 * p1 then tries to connect to p2
361 * @param cb the callback to call when both peers notified that they are connected
362 * @param cb_cls callback cls
363 * @return connect context
365 GNUNET_TRANSPORT_TESTING_ConnectRequest
366 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle * tth,
367 struct PeerContext *p1,
368 struct PeerContext *p2,
369 GNUNET_TRANSPORT_TESTING_connect_cb cb,
373 GNUNET_assert (tth != NULL);
375 struct ConnectingContext *cc =
376 GNUNET_malloc (sizeof (struct ConnectingContext));
378 GNUNET_assert (p1 != NULL);
379 GNUNET_assert (p2 != NULL);
390 GNUNET_assert (cc->th_p1 != NULL);
391 GNUNET_assert (cc->th_p2 != NULL);
393 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
395 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
396 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
397 "New connect request %X\n", cc);
403 * Cancel the request to connect two peers
404 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
405 * @param cc a connect request handle
407 void GNUNET_TRANSPORT_TESTING_connect_peers_cancel
408 (struct GNUNET_TRANSPORT_TESTING_handle * tth,
409 GNUNET_TRANSPORT_TESTING_ConnectRequest ccr)
411 struct ConnectingContext *cc = ccr;
413 GNUNET_assert (tth != NULL);
415 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
416 "Canceling connect request %X!\n", cc);
417 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
418 GNUNET_SCHEDULER_cancel (cc->tct);
420 cc->tct = GNUNET_SCHEDULER_NO_TASK;
422 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
428 * Clean up the transport testing
429 * @param tth transport testing handle
432 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle * tth)
434 struct ConnectingContext *cc = tth->cc_head;
435 struct ConnectingContext *ct = NULL;
436 struct PeerContext *p = tth->p_head;
437 struct PeerContext *t = NULL;
439 GNUNET_assert (tth != NULL);
441 while (cc != tth->cc_tail)
444 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
445 "Developer forgot to cancel connect request %X!\n", cc);
446 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
453 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
454 "Developer forgot to stop peer!\n");
455 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
464 * Initialize the transport testing
465 * @return transport testing handle
467 struct GNUNET_TRANSPORT_TESTING_handle *
468 GNUNET_TRANSPORT_TESTING_init ()
470 struct GNUNET_TRANSPORT_TESTING_handle * tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
477 * Some utility functions
481 * Removes all directory separators from absolute filename
482 * @param file the absolute file name, e.g. as found in argv[0]
483 * @return extracted file name, has to be freed by caller
486 extract_filename (const char *file)
488 char *pch = GNUNET_strdup (file);
490 char *filename = NULL;
493 if (NULL != strstr (pch, "/"))
495 pch = strtok (pch, "/");
498 pch = strtok (NULL, "/");
508 res = GNUNET_strdup (filename);
509 GNUNET_free (backup);
514 * Extracts the test filename from an absolute file name and removes the extension
515 * @param file absolute file name
516 * @param dest where to store result
519 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
521 char *filename = extract_filename (file);
522 char *backup = filename;
525 if (filename == NULL)
529 filename = strstr (filename, "tes");
530 if (filename == NULL)
534 if (NULL != (dotexe = strstr (filename, ".exe")))
544 /* create filename */
545 GNUNET_asprintf (dest, "%s", filename);
546 GNUNET_free (backup);
551 * Extracts the filename from an absolute file name and removes the extension
552 * @param file absolute file name
553 * @param dest where to store result
556 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
558 char *src = extract_filename (file);
561 split = strstr (src, ".");
566 GNUNET_asprintf (dest, "%s", src);
572 * Extracts the plugin anme from an absolute file name and the test name
573 * @param file absolute file name
574 * @param test test name
575 * @param dest where to store result
578 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
579 const char *test, char **dest)
581 char *e = extract_filename (file);
582 char *t = extract_filename (test);
584 char *filename = NULL;
591 filename = strstr (e, "tes");
592 if (filename == NULL)
596 if (NULL != (dotexe = strstr (filename, ".exe")))
600 filename = strstr (filename, t);
601 if (filename == NULL)
605 filename += strlen (t);
607 GNUNET_asprintf (dest, "%s", filename);
619 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
620 * if existing ".exe"-prefix and adds the peer-number
621 * @param file filename of the test, e.g. argv[0]
622 * @param cfgname where to write the result
623 * @param count peer number
626 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
629 char *filename = extract_filename (file);
630 char *backup = filename;
633 if (filename == NULL)
637 filename = strstr (filename, "tes");
638 if (filename == NULL)
642 if (NULL != (dotexe = strstr (filename, ".exe")))
652 /* create cfg filename */
653 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
654 GNUNET_free (backup);
659 /* end of transport_testing.h */