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, 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 *)
174 GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *) message);
175 GNUNET_free_non_null (p->hello);
176 p->hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (message);
179 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
180 "New HELLO for peer %u (`%s') with size %u\n", p->no,
181 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", p->no,
189 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));
210 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
211 "Asking peer %u (`%s') to connect peer %u (`%s'), providing HELLO with %u bytes\n",
212 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s,
213 GNUNET_HELLO_size (cc->p2->hello));
216 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
217 (const struct GNUNET_MessageHeader *) cc->
218 p2->hello, NULL, NULL);
219 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
222 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
227 * Start a peer with the given configuration
228 * @param rec receive callback
229 * @param nc connect callback
230 * @param nd disconnect callback
231 * @param cb_cls closure for callback
232 * @return the peer context
235 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
236 *tth, const char *cfgname, int peer_id,
237 GNUNET_TRANSPORT_ReceiveCallback rec,
238 GNUNET_TRANSPORT_NotifyConnect nc,
239 GNUNET_TRANSPORT_NotifyDisconnect nd,
240 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
243 GNUNET_assert (tth != NULL);
244 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
246 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
247 "File not found: `%s' \n", cfgname);
251 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
253 p->cfg = GNUNET_CONFIGURATION_create ();
255 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
256 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
257 GNUNET_assert (GNUNET_OK ==
258 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
261 if (NULL != p->servicehome)
262 GNUNET_DISK_directory_remove (p->servicehome);
264 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
265 "gnunet-service-arm", "-c", cfgname,
278 p->start_cb = start_cb;
285 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
286 ¬ify_connect, ¬ify_disconnect);
287 GNUNET_assert (p->th != NULL);
289 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
290 GNUNET_assert (p->ghh != NULL);
292 GNUNET_CONTAINER_DLL_insert (tth->p_head, tth->p_tail, p);
298 * shutdown the given peer
302 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
303 struct PeerContext *p)
305 GNUNET_assert (p != NULL);
308 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
312 GNUNET_TRANSPORT_disconnect (p->th);
314 if (NULL != p->arm_proc)
316 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
317 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
318 GNUNET_OS_process_wait (p->arm_proc);
319 GNUNET_OS_process_close (p->arm_proc);
323 if (p->servicehome != NULL)
325 GNUNET_DISK_directory_remove (p->servicehome);
326 GNUNET_free (p->servicehome);
329 if (p->hello != NULL)
330 GNUNET_free (p->hello);
333 GNUNET_CONFIGURATION_destroy (p->cfg);
335 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
342 * Initiate peer p1 to connect to peer p2
343 * Get peer p2's HELLO and offer it to p1
344 * p1 then tries to connect to p2
347 * @param cb the callback to call when both peers notified that they are connected
348 * @param cb_cls callback cls
349 * @return connect context
351 GNUNET_TRANSPORT_TESTING_ConnectRequest
352 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_handle
353 *tth, struct PeerContext *p1,
354 struct PeerContext *p2,
355 GNUNET_TRANSPORT_TESTING_connect_cb cb,
358 GNUNET_assert (tth != NULL);
360 struct ConnectingContext *cc =
361 GNUNET_malloc (sizeof (struct ConnectingContext));
363 GNUNET_assert (p1 != NULL);
364 GNUNET_assert (p2 != NULL);
375 GNUNET_assert (cc->th_p1 != NULL);
376 GNUNET_assert (cc->th_p2 != NULL);
378 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
380 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
381 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
382 "New connect request %X\n", cc);
388 * Cancel the request to connect two peers
389 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
390 * @param cc a connect request handle
393 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct
394 GNUNET_TRANSPORT_TESTING_handle
396 GNUNET_TRANSPORT_TESTING_ConnectRequest
399 struct ConnectingContext *cc = ccr;
401 GNUNET_assert (tth != NULL);
403 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
404 "Canceling connect request %X!\n", cc);
405 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
406 GNUNET_SCHEDULER_cancel (cc->tct);
408 cc->tct = GNUNET_SCHEDULER_NO_TASK;
410 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
416 * Clean up the transport testing
417 * @param tth transport testing handle
420 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
422 struct ConnectingContext *cc = tth->cc_head;
423 struct ConnectingContext *ct = NULL;
424 struct PeerContext *p = tth->p_head;
425 struct PeerContext *t = NULL;
427 GNUNET_assert (tth != NULL);
429 while (cc != tth->cc_tail)
432 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
433 "Developer forgot to cancel connect request %X!\n", cc);
434 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
441 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
442 "Developer forgot to stop peer!\n");
443 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
452 * Initialize the transport testing
453 * @return transport testing handle
455 struct GNUNET_TRANSPORT_TESTING_handle *
456 GNUNET_TRANSPORT_TESTING_init ()
458 struct GNUNET_TRANSPORT_TESTING_handle *tth =
459 GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
466 * Some utility functions
470 * Removes all directory separators from absolute filename
471 * @param file the absolute file name, e.g. as found in argv[0]
472 * @return extracted file name, has to be freed by caller
475 extract_filename (const char *file)
477 char *pch = GNUNET_strdup (file);
479 char *filename = NULL;
482 if (NULL != strstr (pch, "/"))
484 pch = strtok (pch, "/");
487 pch = strtok (NULL, "/");
497 res = GNUNET_strdup (filename);
498 GNUNET_free (backup);
503 * Extracts the test filename from an absolute file name and removes the extension
504 * @param file absolute file name
505 * @param dest where to store result
508 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
510 char *filename = extract_filename (file);
511 char *backup = filename;
514 if (filename == NULL)
518 filename = strstr (filename, "tes");
519 if (filename == NULL)
523 if (NULL != (dotexe = strstr (filename, ".exe")))
533 /* create filename */
534 GNUNET_asprintf (dest, "%s", filename);
535 GNUNET_free (backup);
540 * Extracts the filename from an absolute file name and removes the extension
541 * @param file absolute file name
542 * @param dest where to store result
545 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
547 char *src = extract_filename (file);
550 split = strstr (src, ".");
555 GNUNET_asprintf (dest, "%s", src);
561 * Extracts the plugin anme from an absolute file name and the test name
562 * @param file absolute file name
563 * @param test test name
564 * @param dest where to store result
567 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
568 const char *test, char **dest)
570 char *e = extract_filename (file);
571 char *t = extract_filename (test);
573 char *filename = NULL;
580 filename = strstr (e, "tes");
581 if (filename == NULL)
585 if (NULL != (dotexe = strstr (filename, ".exe")))
589 filename = strstr (filename, t);
590 if (filename == NULL)
594 filename += strlen (t);
596 GNUNET_asprintf (dest, "%s", filename);
608 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
609 * if existing ".exe"-prefix and adds the peer-number
610 * @param file filename of the test, e.g. argv[0]
611 * @param cfgname where to write the result
612 * @param count peer number
615 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
618 char *filename = extract_filename (file);
619 char *backup = filename;
622 if (filename == NULL)
626 filename = strstr (filename, "tes");
627 if (filename == NULL)
631 if (NULL != (dotexe = strstr (filename, ".exe")))
641 /* create cfg filename */
642 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
643 GNUNET_free (backup);
648 /* end of transport_testing.h */