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
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 *)
175 GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) message);
177 GNUNET_free_non_null (p->hello);
178 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
181 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
182 "New HELLO for peer %u (`%s') with size %u\n", p->no,
183 GNUNET_i2s (&p->id), size);
186 if (p->start_cb != NULL)
189 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
190 "Peer %u (`%s') successfully started\n", p->no,
191 GNUNET_i2s (&p->id));
193 p->start_cb (p, p->cb_cls);
200 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 struct ConnectingContext *cc = cls;
203 struct PeerContext *p1 = cc->p1;
204 struct PeerContext *p2 = cc->p2;
206 cc->tct = GNUNET_SCHEDULER_NO_TASK;
207 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
210 GNUNET_assert (cc != NULL);
211 GNUNET_assert (cc->p1 != NULL);
212 GNUNET_assert (cc->p2 != NULL);
214 char *p2_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
216 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
217 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
218 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
219 GNUNET_HELLO_size (cc->p2->hello));
222 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
223 (const struct GNUNET_MessageHeader *) cc->
224 p2->hello, NULL, NULL);
225 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
228 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
233 * Start a peer with the given configuration
234 * @param rec receive callback
235 * @param nc connect callback
236 * @param nd disconnect callback
237 * @param cb_cls closure for callback
238 * @return the peer context
241 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
242 *tth, const char *cfgname, int peer_id,
243 GNUNET_TRANSPORT_ReceiveCallback rec,
244 GNUNET_TRANSPORT_NotifyConnect nc,
245 GNUNET_TRANSPORT_NotifyDisconnect nd,
246 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
249 GNUNET_assert (tth != NULL);
250 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
252 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
253 "File not found: `%s' \n", cfgname);
257 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
259 p->cfg = GNUNET_CONFIGURATION_create ();
261 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
262 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
263 GNUNET_assert (GNUNET_OK ==
264 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
267 if (NULL != p->servicehome)
268 GNUNET_DISK_directory_remove (p->servicehome);
270 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
271 "gnunet-service-arm", "-c", cfgname,
284 p->start_cb = start_cb;
291 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
292 ¬ify_connect, ¬ify_disconnect);
293 GNUNET_assert (p->th != NULL);
295 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
296 GNUNET_assert (p->ghh != NULL);
298 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
304 * shutdown the given peer
308 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
309 struct PeerContext *p)
311 GNUNET_assert (p != NULL);
314 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
318 GNUNET_TRANSPORT_disconnect (p->th);
320 if (NULL != p->arm_proc)
322 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
323 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
324 GNUNET_OS_process_wait (p->arm_proc);
325 GNUNET_OS_process_close (p->arm_proc);
329 if (p->servicehome != NULL)
331 GNUNET_DISK_directory_remove (p->servicehome);
332 GNUNET_free (p->servicehome);
335 if (p->hello != NULL)
336 GNUNET_free (p->hello);
339 GNUNET_CONFIGURATION_destroy (p->cfg);
341 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
348 * Initiate peer p1 to connect to peer p2
349 * Get peer p2's HELLO and offer it to p1
350 * p1 then tries to connect to p2
353 * @param cb the callback to call when both peers notified that they are connected
354 * @param cb_cls callback cls (or a pointer to the
355 * GNUNET_TRANSPORT_TESTING_ConnectRequest itself if null)
356 * @return connect context
358 GNUNET_TRANSPORT_TESTING_ConnectRequest
359 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
360 *tth, struct PeerContext *p1,
361 struct PeerContext *p2,
362 GNUNET_TRANSPORT_TESTING_connect_cb cb,
365 GNUNET_assert (tth != NULL);
367 struct ConnectingContext *cc =
368 GNUNET_malloc (sizeof (struct ConnectingContext));
370 GNUNET_assert (p1 != NULL);
371 GNUNET_assert (p2 != NULL);
385 GNUNET_assert (cc->th_p1 != NULL);
386 GNUNET_assert (cc->th_p2 != NULL);
388 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
390 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
392 "New connect request %X\n", cc);
398 * Cancel the request to connect two peers
399 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
400 * @param cc a connect request handle
403 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
404 GNUNET_TRANSPORT_TESTING_handle
406 GNUNET_TRANSPORT_TESTING_ConnectRequest
409 struct ConnectingContext *cc = ccr;
411 GNUNET_assert (tth != NULL);
413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
414 "Canceling connect request %X!\n", cc);
416 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
417 GNUNET_SCHEDULER_cancel (cc->tct);
418 cc->tct = GNUNET_SCHEDULER_NO_TASK;
420 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
426 * Clean up the transport testing
427 * @param tth transport testing handle
430 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
432 struct ConnectingContext *cc = tth->cc_head;
433 struct ConnectingContext *ct = NULL;
434 struct PeerContext *p = tth->p_head;
435 struct PeerContext *t = NULL;
437 GNUNET_assert (tth != NULL);
439 while (cc != tth->cc_tail)
442 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
443 "Developer forgot to cancel connect request %X!\n", cc);
444 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
451 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
452 "Developer forgot to stop peer!\n");
453 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
462 * Initialize the transport testing
463 * @return transport testing handle
465 struct GNUNET_TRANSPORT_TESTING_handle *
466 GNUNET_TRANSPORT_TESTING_init ()
468 struct GNUNET_TRANSPORT_TESTING_handle *tth =
469 GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
476 * Some utility functions
480 * Removes all directory separators from absolute filename
481 * @param file the absolute file name, e.g. as found in argv[0]
482 * @return extracted file name, has to be freed by caller
485 extract_filename (const char *file)
487 char *pch = GNUNET_strdup (file);
489 char *filename = NULL;
492 if (NULL != strstr (pch, "/"))
494 pch = strtok (pch, "/");
497 pch = strtok (NULL, "/");
507 res = GNUNET_strdup (filename);
508 GNUNET_free (backup);
513 * Extracts the test filename from an absolute file name and removes the extension
514 * @param file absolute file name
515 * @param dest where to store result
518 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
520 char *filename = extract_filename (file);
521 char *backup = filename;
524 if (filename == NULL)
528 filename = strstr (filename, "tes");
529 if (filename == NULL)
533 if (NULL != (dotexe = strstr (filename, ".exe")))
543 /* create filename */
544 GNUNET_asprintf (dest, "%s", filename);
545 GNUNET_free (backup);
550 * Extracts the filename from an absolute file name and removes the extension
551 * @param file absolute file name
552 * @param dest where to store result
555 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
557 char *src = extract_filename (file);
560 split = strstr (src, ".");
565 GNUNET_asprintf (dest, "%s", src);
571 * Extracts the plugin anme from an absolute file name and the test name
572 * @param file absolute file name
573 * @param test test name
574 * @param dest where to store result
577 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
578 const char *test, char **dest)
580 char *e = extract_filename (file);
581 char *t = extract_filename (test);
583 char *filename = NULL;
590 filename = strstr (e, "tes");
591 if (filename == NULL)
595 if (NULL != (dotexe = strstr (filename, ".exe")))
599 filename = strstr (filename, t);
600 if (filename == NULL)
604 filename += strlen (t);
606 GNUNET_asprintf (dest, "%s", filename);
618 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
619 * if existing ".exe"-prefix and adds the peer-number
620 * @param file filename of the test, e.g. argv[0]
621 * @param cfgname where to write the result
622 * @param count peer number
625 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
628 char *filename = extract_filename (file);
629 char *backup = filename;
632 if (filename == NULL)
636 filename = strstr (filename, "tes");
637 if (filename == NULL)
641 if (NULL != (dotexe = strstr (filename, ".exe")))
651 /* create cfg filename */
652 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
653 GNUNET_free (backup);
658 /* end of transport_testing.h */