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,
53 struct PeerContext * p2)
55 GNUNET_assert (tth != NULL);
56 struct ConnectingContext * cc = tth->cc_head;
60 if ((cc->p1 == p1) && (cc->p2 == p2))
62 if ((cc->p1 == p2) && (cc->p2 == p1))
71 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
72 const struct GNUNET_ATS_Information *ats,
75 struct PeerContext *p = cls;
76 /* Find PeerContext */
77 GNUNET_assert (p != 0);
78 GNUNET_assert (p->tth != NULL);
79 struct PeerContext * p2 = find_peer_context (p->tth, peer);
84 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",
95 p->no, GNUNET_i2s (&p->id));
100 /* Find ConnectingContext */
101 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;
122 /* Find PeerContext */
124 struct PeerContext * p2 = NULL;
128 GNUNET_assert (p->tth != NULL);
129 p2 = find_peer_context (p->tth, peer);
135 GNUNET_asprintf(&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
137 GNUNET_asprintf(&p2_s, "`%s'", GNUNET_i2s (peer));
138 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
139 "Peers %s disconnected from peer %u (`%s')\n",
141 no , GNUNET_i2s (&p->id));
147 p->nd (p->cb_cls, peer);
151 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
152 const struct GNUNET_MessageHeader *message,
153 const struct GNUNET_ATS_Information *ats,
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 *)
173 size_t size = GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *) message);
174 GNUNET_free_non_null (p->hello);
175 p->hello = (struct GNUNET_HELLO_Message*) GNUNET_copy_message (message);
178 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
180 "New HELLO for peer %u (`%s') with size %u\n",
181 p->no, 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",
189 p->no, 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 char * p2_s = GNUNET_strdup(GNUNET_i2s (&p2->id));
209 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
210 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
211 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
212 GNUNET_HELLO_size (cc->p2->hello));
215 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
216 (const struct GNUNET_MessageHeader *) cc->p2->hello, NULL, NULL);
217 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
220 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
225 * Start a peer with the given configuration
226 * @param rec receive callback
227 * @param nc connect callback
228 * @param nd disconnect callback
229 * @param cb_cls closure for callback
230 * @return the peer context
233 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle * tth,
236 GNUNET_TRANSPORT_ReceiveCallback rec,
237 GNUNET_TRANSPORT_NotifyConnect nc,
238 GNUNET_TRANSPORT_NotifyDisconnect nd,
239 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
242 GNUNET_assert (tth != NULL);
243 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
245 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
246 "File not found: `%s' \n", cfgname);
250 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
252 p->cfg = GNUNET_CONFIGURATION_create ();
254 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
255 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
256 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
258 if (NULL != p->servicehome)
259 GNUNET_DISK_directory_remove (p->servicehome);
261 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
262 "gnunet-service-arm", "-c", cfgname,
275 p->start_cb = start_cb;
282 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
283 ¬ify_connect, ¬ify_disconnect);
284 GNUNET_assert (p->th != NULL);
286 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
287 GNUNET_assert (p->ghh != NULL);
289 GNUNET_CONTAINER_DLL_insert(tth->p_head, tth->p_tail, p);
295 * shutdown the given peer
299 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle * tth,
300 struct PeerContext *p)
302 GNUNET_assert (p != NULL);
305 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
309 GNUNET_TRANSPORT_disconnect (p->th);
311 if (NULL != p->arm_proc)
313 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
314 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
315 GNUNET_OS_process_wait (p->arm_proc);
316 GNUNET_OS_process_close (p->arm_proc);
320 if (p->servicehome != NULL)
322 GNUNET_DISK_directory_remove (p->servicehome);
323 GNUNET_free (p->servicehome);
326 if (p->hello != NULL)
327 GNUNET_free (p->hello);
330 GNUNET_CONFIGURATION_destroy (p->cfg);
332 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
339 * Initiate peer p1 to connect to peer p2
340 * Get peer p2's HELLO and offer it to p1
341 * p1 then tries to connect to p2
344 * @param cb the callback to call when both peers notified that they are connected
345 * @param cb_cls callback cls
346 * @return connect context
348 GNUNET_TRANSPORT_TESTING_ConnectRequest
349 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle * tth,
350 struct PeerContext *p1,
351 struct PeerContext *p2,
352 GNUNET_TRANSPORT_TESTING_connect_cb cb,
356 GNUNET_assert (tth != NULL);
358 struct ConnectingContext *cc =
359 GNUNET_malloc (sizeof (struct ConnectingContext));
361 GNUNET_assert (p1 != NULL);
362 GNUNET_assert (p2 != NULL);
373 GNUNET_assert (cc->th_p1 != NULL);
374 GNUNET_assert (cc->th_p2 != NULL);
376 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
378 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
379 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
380 "New connect request %X\n", cc);
386 * Cancel the request to connect two peers
387 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
388 * @param cc a connect request handle
390 void GNUNET_TRANSPORT_TESTING_connect_peers_cancel
391 (struct GNUNET_TRANSPORT_TESTING_handle * tth,
392 GNUNET_TRANSPORT_TESTING_ConnectRequest ccr)
394 struct ConnectingContext *cc = ccr;
396 GNUNET_assert (tth != NULL);
398 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
399 "Canceling connect request %X!\n", cc);
400 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
401 GNUNET_SCHEDULER_cancel (cc->tct);
403 cc->tct = GNUNET_SCHEDULER_NO_TASK;
405 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
411 * Clean up the transport testing
412 * @param tth transport testing handle
415 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle * tth)
417 struct ConnectingContext *cc = tth->cc_head;
418 struct ConnectingContext *ct = NULL;
419 struct PeerContext *p = tth->p_head;
420 struct PeerContext *t = NULL;
422 GNUNET_assert (tth != NULL);
424 while (cc != tth->cc_tail)
427 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
428 "Developer forgot to cancel connect request %X!\n", cc);
429 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
436 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
437 "Developer forgot to stop peer!\n");
438 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
447 * Initialize the transport testing
448 * @return transport testing handle
450 struct GNUNET_TRANSPORT_TESTING_handle *
451 GNUNET_TRANSPORT_TESTING_init ()
453 struct GNUNET_TRANSPORT_TESTING_handle * tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
460 * Some utility functions
464 * Removes all directory separators from absolute filename
465 * @param file the absolute file name, e.g. as found in argv[0]
466 * @return extracted file name, has to be freed by caller
469 extract_filename (const char *file)
471 char *pch = GNUNET_strdup (file);
473 char *filename = NULL;
476 if (NULL != strstr (pch, "/"))
478 pch = strtok (pch, "/");
481 pch = strtok (NULL, "/");
491 res = GNUNET_strdup (filename);
492 GNUNET_free (backup);
497 * Extracts the test filename from an absolute file name and removes the extension
498 * @param file absolute file name
499 * @param dest where to store result
502 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
504 char *filename = extract_filename (file);
505 char *backup = filename;
508 if (filename == NULL)
512 filename = strstr (filename, "tes");
513 if (filename == NULL)
517 if (NULL != (dotexe = strstr (filename, ".exe")))
527 /* create filename */
528 GNUNET_asprintf (dest, "%s", filename);
529 GNUNET_free (backup);
534 * Extracts the filename from an absolute file name and removes the extension
535 * @param file absolute file name
536 * @param dest where to store result
539 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
541 char *src = extract_filename (file);
544 split = strstr (src, ".");
549 GNUNET_asprintf (dest, "%s", src);
555 * Extracts the plugin anme from an absolute file name and the test name
556 * @param file absolute file name
557 * @param test test name
558 * @param dest where to store result
561 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
562 const char *test, char **dest)
564 char *e = extract_filename (file);
565 char *t = extract_filename (test);
567 char *filename = NULL;
574 filename = strstr (e, "tes");
575 if (filename == NULL)
579 if (NULL != (dotexe = strstr (filename, ".exe")))
583 filename = strstr (filename, t);
584 if (filename == NULL)
588 filename += strlen (t);
590 GNUNET_asprintf (dest, "%s", filename);
602 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
603 * if existing ".exe"-prefix and adds the peer-number
604 * @param file filename of the test, e.g. argv[0]
605 * @param cfgname where to write the result
606 * @param count peer number
609 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
612 char *filename = extract_filename (file);
613 char *backup = filename;
616 if (filename == NULL)
620 filename = strstr (filename, "tes");
621 if (filename == NULL)
625 if (NULL != (dotexe = strstr (filename, ".exe")))
635 /* create cfg filename */
636 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
637 GNUNET_free (backup);
642 /* end of transport_testing.h */