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 find_peer_context_by_pc ( struct TransportTestingHandle *tth,
34 struct PeerContext *p)
36 GNUNET_assert (tth != NULL);
37 struct PeerContext * t = tth->p_head;
51 find_peer_context ( struct TransportTestingHandle *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 TransportTestingHandle *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_TRANSPORT_ATS_Information *ats,
92 struct PeerContext *p = cls;
93 /* Find PeerContext */
94 GNUNET_assert (p->tth != NULL);
95 struct PeerContext * p2 = find_peer_context (p->tth, peer);
100 p->nc (p->cb_cls, peer, ats, ats_count);
105 GNUNET_asprintf(&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
107 GNUNET_asprintf(&p2_s, "`%s'", GNUNET_i2s (peer));
108 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
109 "Peers %s connected to peer %u (`%s')\n",
111 p->no, GNUNET_i2s (&p->id));
116 /* Find ConnectingContext */
117 struct ConnectingContext * cc = find_connecting_context(p->tth, p, p2);
122 cc->p1_c = GNUNET_YES;
125 cc->p2_c = GNUNET_YES;
127 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
129 cc->cb (cc->p1, cc->p2, cc->cb_cls);
130 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(p->tth, cc);
135 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
137 struct PeerContext *p = cls;
138 /* Find PeerContext */
139 GNUNET_assert (p->tth != NULL);
140 struct PeerContext * p2 = find_peer_context (p->tth, peer);
144 GNUNET_asprintf(&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
146 GNUNET_asprintf(&p2_s, "`%s'", GNUNET_i2s (peer));
147 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
148 "Peers %s disconnected from peer %u (`%s')\n",
150 p->no, GNUNET_i2s (&p->id));
156 p->nd (p->cb_cls, peer);
160 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
161 const struct GNUNET_MessageHeader *message,
162 const struct GNUNET_TRANSPORT_ATS_Information *ats,
165 struct PeerContext *p = cls;
170 p->rec (p->cb_cls, peer, message, ats, ats_count);
174 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
176 struct PeerContext *p = cb_cls;
178 GNUNET_assert (message != NULL);
179 GNUNET_assert (GNUNET_OK ==
180 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
183 if (p->hello != NULL)
184 GNUNET_free (p->hello);
186 size_t size = GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *) message);
187 p->hello = GNUNET_malloc (size);
189 (const struct GNUNET_HELLO_Message *) message,
193 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
194 "New HELLO for peer %u (`%s') with size %u\n",
195 p->no, GNUNET_i2s (&p->id), size);
198 if (p->start_cb != NULL)
201 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
202 "Peer %u (`%s') successfully started\n",
203 p->no, GNUNET_i2s (&p->id));
205 p->start_cb(p, p->cb_cls);
212 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
214 struct ConnectingContext *cc = cls;
215 struct PeerContext *p1 = cc->p1;
216 struct PeerContext *p2 = cc->p2;
218 cc->tct = GNUNET_SCHEDULER_NO_TASK;
219 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
222 char * p2_s = strdup(GNUNET_i2s (&p2->id));
223 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
224 "Asking peers %u (`%s') to connect peer %u (`%s')\n",
225 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s);
228 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
229 (const struct GNUNET_MessageHeader *) cc->p2->hello, NULL, NULL);
230 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
233 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
238 * Start a peer with the given configuration
239 * @param rec receive callback
240 * @param nc connect callback
241 * @param nd disconnect callback
242 * @param cb_cls closure for callback
243 * @return the peer context
246 GNUNET_TRANSPORT_TESTING_start_peer (struct TransportTestingHandle * tth,
249 GNUNET_TRANSPORT_ReceiveCallback rec,
250 GNUNET_TRANSPORT_NotifyConnect nc,
251 GNUNET_TRANSPORT_NotifyDisconnect nd,
252 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
255 GNUNET_assert (tth != NULL);
256 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
258 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
259 "File not found: `%s' \n", cfgname);
263 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
265 p->cfg = GNUNET_CONFIGURATION_create ();
267 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
268 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
269 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
271 if (NULL != p->servicehome)
272 GNUNET_DISK_directory_remove (p->servicehome);
274 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
275 "gnunet-service-arm", "-c", cfgname,
288 p->start_cb = start_cb;
295 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
296 ¬ify_connect, ¬ify_disconnect);
297 GNUNET_assert (p->th != NULL);
299 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
300 GNUNET_assert (p->ghh != NULL);
302 GNUNET_CONTAINER_DLL_insert(tth->p_head, tth->p_tail, p);
308 * shutdown the given peer
312 GNUNET_TRANSPORT_TESTING_stop_peer (struct TransportTestingHandle * tth,
313 struct PeerContext *p)
315 GNUNET_assert (p != NULL);
318 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
322 GNUNET_TRANSPORT_disconnect (p->th);
324 if (NULL != p->arm_proc)
326 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
327 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
328 GNUNET_OS_process_wait (p->arm_proc);
329 GNUNET_OS_process_close (p->arm_proc);
333 if (p->servicehome != NULL)
335 GNUNET_DISK_directory_remove (p->servicehome);
336 GNUNET_free (p->servicehome);
339 if (p->hello != NULL)
340 GNUNET_free (p->hello);
343 GNUNET_CONFIGURATION_destroy (p->cfg);
345 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
352 * Initiate peer p1 to connect to peer p2
353 * Get peer p2's HELLO and offer it to p1
354 * p1 then tries to connect to p2
357 * @param cb the callback to call when both peers notified that they are connected
358 * @param cb_cls callback cls
359 * @return connect context
361 GNUNET_TRANSPORT_TESTING_ConnectRequest
362 GNUNET_TRANSPORT_TESTING_connect_peers (struct TransportTestingHandle * tth,
363 struct PeerContext *p1,
364 struct PeerContext *p2,
365 GNUNET_TRANSPORT_TESTING_connect_cb cb,
369 GNUNET_assert (tth != NULL);
371 struct ConnectingContext *cc =
372 GNUNET_malloc (sizeof (struct ConnectingContext));
374 GNUNET_assert (p1 != NULL);
375 GNUNET_assert (p2 != NULL);
386 GNUNET_assert (cc->th_p1 != NULL);
387 GNUNET_assert (cc->th_p2 != NULL);
389 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
391 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
392 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
393 "connect request %X!\n", cc);
399 * Cancel the request to connect two peers
400 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
401 * @param cc a connect request handle
403 void GNUNET_TRANSPORT_TESTING_connect_peers_cancel
404 (struct TransportTestingHandle * tth,
405 GNUNET_TRANSPORT_TESTING_ConnectRequest ccr)
407 struct ConnectingContext *cc = ccr;
409 GNUNET_assert (tth != NULL);
411 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
412 "Canceling connect request %X!\n", cc);
413 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
414 GNUNET_SCHEDULER_cancel (cc->tct);
416 cc->tct = GNUNET_SCHEDULER_NO_TASK;
418 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
424 * Clean up the transport testing
425 * @param tth transport testing handle
428 GNUNET_TRANSPORT_TESTING_done (struct TransportTestingHandle * tth)
430 struct ConnectingContext *cc = tth->cc_head;
431 struct ConnectingContext *ct = NULL;
432 struct PeerContext *p = tth->p_head;
433 struct PeerContext *t = NULL;
435 GNUNET_assert (tth != NULL);
437 while (cc != tth->cc_tail)
440 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
441 "Developer forgot to cancel connect request %X!\n", cc);
442 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
449 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
450 "Developer forgot to stop peer!\n");
451 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
460 * Initialize the transport testing
461 * @return transport testing handle
463 struct TransportTestingHandle *
464 GNUNET_TRANSPORT_TESTING_init ()
466 struct TransportTestingHandle * tth = GNUNET_malloc (sizeof (struct TransportTestingHandle));
473 * Some utility functions
477 * Removes all directory separators from absolute filename
478 * @param file the absolute file name, e.g. as found in argv[0]
479 * @return extracted file name, has to be freed by caller
482 extract_filename (const char *file)
484 char *pch = strdup (file);
486 char *filename = NULL;
489 if (NULL != strstr (pch, "/"))
491 pch = strtok (pch, "/");
494 pch = strtok (NULL, "/");
504 res = strdup (filename);
505 GNUNET_free (backup);
510 * Extracts the test filename from an absolute file name and removes the extension
511 * @param file absolute file name
512 * @param dest where to store result
515 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
517 char *filename = extract_filename (file);
518 char *backup = filename;
521 if (filename == NULL)
525 filename = strstr (filename, "tes");
526 if (filename == NULL)
530 if (NULL != (dotexe = strstr (filename, ".exe")))
540 /* create filename */
541 GNUNET_asprintf (dest, "%s", filename);
542 GNUNET_free (backup);
547 * Extracts the filename from an absolute file name and removes the extension
548 * @param file absolute file name
549 * @param dest where to store result
552 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
554 char *src = extract_filename (file);
557 split = strstr (src, ".");
562 GNUNET_asprintf (dest, "%s", src);
568 * Extracts the plugin anme from an absolute file name and the test name
569 * @param file absolute file name
570 * @param test test name
571 * @param dest where to store result
574 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
575 const char *test, char **dest)
577 char *e = extract_filename (file);
578 char *t = extract_filename (test);
580 char *filename = NULL;
587 filename = strstr (e, "tes");
588 if (filename == NULL)
592 if (NULL != (dotexe = strstr (filename, ".exe")))
596 filename = strstr (filename, t);
597 if (filename == NULL)
601 filename += strlen (t);
603 GNUNET_asprintf (dest, "%s", filename);
615 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
616 * if existing ".exe"-prefix and adds the peer-number
617 * @param file filename of the test, e.g. argv[0]
618 * @param cfgname where to write the result
619 * @param count peer number
622 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
625 char *filename = extract_filename (file);
626 char *backup = filename;
629 if (filename == NULL)
633 filename = strstr (filename, "tes");
634 if (filename == NULL)
638 if (NULL != (dotexe = strstr (filename, ".exe")))
648 /* create cfg filename */
649 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
650 GNUNET_free (backup);
655 /* end of transport_testing.h */