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 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;
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 *)
191 if (p->hello != NULL)
192 GNUNET_free (p->hello);
194 size_t size = GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *) message);
195 p->hello = GNUNET_malloc (size);
197 (const struct GNUNET_HELLO_Message *) message,
201 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
202 "New HELLO for peer %u (`%s') with size %u\n",
203 p->no, GNUNET_i2s (&p->id), size);
206 if (p->start_cb != NULL)
209 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
210 "Peer %u (`%s') successfully started\n",
211 p->no, GNUNET_i2s (&p->id));
213 p->start_cb(p, p->cb_cls);
220 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
222 struct ConnectingContext *cc = cls;
223 struct PeerContext *p1 = cc->p1;
224 struct PeerContext *p2 = cc->p2;
226 cc->tct = GNUNET_SCHEDULER_NO_TASK;
227 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
230 char * p2_s = GNUNET_strdup(GNUNET_i2s (&p2->id));
231 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
232 "Asking peers %u (`%s') to connect peer %u (`%s')\n",
233 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s);
236 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
237 (const struct GNUNET_MessageHeader *) cc->p2->hello, NULL, NULL);
238 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
241 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
246 * Start a peer with the given configuration
247 * @param rec receive callback
248 * @param nc connect callback
249 * @param nd disconnect callback
250 * @param cb_cls closure for callback
251 * @return the peer context
254 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle * tth,
257 GNUNET_TRANSPORT_ReceiveCallback rec,
258 GNUNET_TRANSPORT_NotifyConnect nc,
259 GNUNET_TRANSPORT_NotifyDisconnect nd,
260 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
263 GNUNET_assert (tth != NULL);
264 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
266 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
267 "File not found: `%s' \n", cfgname);
271 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
273 p->cfg = GNUNET_CONFIGURATION_create ();
275 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
276 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
277 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
279 if (NULL != p->servicehome)
280 GNUNET_DISK_directory_remove (p->servicehome);
282 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
283 "gnunet-service-arm", "-c", cfgname,
296 p->start_cb = start_cb;
303 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
304 ¬ify_connect, ¬ify_disconnect);
305 GNUNET_assert (p->th != NULL);
307 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
308 GNUNET_assert (p->ghh != NULL);
310 GNUNET_CONTAINER_DLL_insert(tth->p_head, tth->p_tail, p);
316 * shutdown the given peer
320 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle * tth,
321 struct PeerContext *p)
323 GNUNET_assert (p != NULL);
326 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
330 GNUNET_TRANSPORT_disconnect (p->th);
332 if (NULL != p->arm_proc)
334 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
335 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
336 GNUNET_OS_process_wait (p->arm_proc);
337 GNUNET_OS_process_close (p->arm_proc);
341 if (p->servicehome != NULL)
343 GNUNET_DISK_directory_remove (p->servicehome);
344 GNUNET_free (p->servicehome);
347 if (p->hello != NULL)
348 GNUNET_free (p->hello);
351 GNUNET_CONFIGURATION_destroy (p->cfg);
353 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
360 * Initiate peer p1 to connect to peer p2
361 * Get peer p2's HELLO and offer it to p1
362 * p1 then tries to connect to p2
365 * @param cb the callback to call when both peers notified that they are connected
366 * @param cb_cls callback cls
367 * @return connect context
369 GNUNET_TRANSPORT_TESTING_ConnectRequest
370 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle * tth,
371 struct PeerContext *p1,
372 struct PeerContext *p2,
373 GNUNET_TRANSPORT_TESTING_connect_cb cb,
377 GNUNET_assert (tth != NULL);
379 struct ConnectingContext *cc =
380 GNUNET_malloc (sizeof (struct ConnectingContext));
382 GNUNET_assert (p1 != NULL);
383 GNUNET_assert (p2 != NULL);
394 GNUNET_assert (cc->th_p1 != NULL);
395 GNUNET_assert (cc->th_p2 != NULL);
397 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
399 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
400 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
401 "New connect request %X\n", cc);
407 * Cancel the request to connect two peers
408 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
409 * @param cc a connect request handle
411 void GNUNET_TRANSPORT_TESTING_connect_peers_cancel
412 (struct GNUNET_TRANSPORT_TESTING_handle * tth,
413 GNUNET_TRANSPORT_TESTING_ConnectRequest ccr)
415 struct ConnectingContext *cc = ccr;
417 GNUNET_assert (tth != NULL);
419 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
420 "Canceling connect request %X!\n", cc);
421 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
422 GNUNET_SCHEDULER_cancel (cc->tct);
424 cc->tct = GNUNET_SCHEDULER_NO_TASK;
426 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
432 * Clean up the transport testing
433 * @param tth transport testing handle
436 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle * tth)
438 struct ConnectingContext *cc = tth->cc_head;
439 struct ConnectingContext *ct = NULL;
440 struct PeerContext *p = tth->p_head;
441 struct PeerContext *t = NULL;
443 GNUNET_assert (tth != NULL);
445 while (cc != tth->cc_tail)
448 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
449 "Developer forgot to cancel connect request %X!\n", cc);
450 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
457 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
458 "Developer forgot to stop peer!\n");
459 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
468 * Initialize the transport testing
469 * @return transport testing handle
471 struct GNUNET_TRANSPORT_TESTING_handle *
472 GNUNET_TRANSPORT_TESTING_init ()
474 struct GNUNET_TRANSPORT_TESTING_handle * tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
481 * Some utility functions
485 * Removes all directory separators from absolute filename
486 * @param file the absolute file name, e.g. as found in argv[0]
487 * @return extracted file name, has to be freed by caller
490 extract_filename (const char *file)
492 char *pch = GNUNET_strdup (file);
494 char *filename = NULL;
497 if (NULL != strstr (pch, "/"))
499 pch = strtok (pch, "/");
502 pch = strtok (NULL, "/");
512 res = GNUNET_strdup (filename);
513 GNUNET_free (backup);
518 * Extracts the test filename from an absolute file name and removes the extension
519 * @param file absolute file name
520 * @param dest where to store result
523 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
525 char *filename = extract_filename (file);
526 char *backup = filename;
529 if (filename == NULL)
533 filename = strstr (filename, "tes");
534 if (filename == NULL)
538 if (NULL != (dotexe = strstr (filename, ".exe")))
548 /* create filename */
549 GNUNET_asprintf (dest, "%s", filename);
550 GNUNET_free (backup);
555 * Extracts the filename from an absolute file name and removes the extension
556 * @param file absolute file name
557 * @param dest where to store result
560 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
562 char *src = extract_filename (file);
565 split = strstr (src, ".");
570 GNUNET_asprintf (dest, "%s", src);
576 * Extracts the plugin anme from an absolute file name and the test name
577 * @param file absolute file name
578 * @param test test name
579 * @param dest where to store result
582 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
583 const char *test, char **dest)
585 char *e = extract_filename (file);
586 char *t = extract_filename (test);
588 char *filename = NULL;
595 filename = strstr (e, "tes");
596 if (filename == NULL)
600 if (NULL != (dotexe = strstr (filename, ".exe")))
604 filename = strstr (filename, t);
605 if (filename == NULL)
609 filename += strlen (t);
611 GNUNET_asprintf (dest, "%s", filename);
623 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
624 * if existing ".exe"-prefix and adds the peer-number
625 * @param file filename of the test, e.g. argv[0]
626 * @param cfgname where to write the result
627 * @param count peer number
630 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
633 char *filename = extract_filename (file);
634 char *backup = filename;
637 if (filename == NULL)
641 filename = strstr (filename, "tes");
642 if (filename == NULL)
646 if (NULL != (dotexe = strstr (filename, ".exe")))
656 /* create cfg filename */
657 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
658 GNUNET_free (backup);
663 /* end of transport_testing.h */