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 struct PeerContext * t = tth->p_head;
50 find_peer_context ( struct TransportTestingHandle *tth,
51 const struct GNUNET_PeerIdentity *peer)
53 struct PeerContext * t = tth->p_head;
57 if (0 == memcmp (&t->id, peer, sizeof (struct GNUNET_PeerIdentity)))
65 struct ConnectingContext *
66 find_connecting_context ( struct TransportTestingHandle *tth,
67 struct PeerContext *p1,
68 struct PeerContext * p2)
70 struct ConnectingContext * cc = tth->cc_head;
74 if ((cc->p1 == p1) && (cc->p2 == p2))
76 if ((cc->p1 == p2) && (cc->p2 == p1))
85 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
86 const struct GNUNET_TRANSPORT_ATS_Information *ats,
89 struct PeerContext *p = cls;
90 /* Find PeerContext */
91 struct PeerContext * p2 = find_peer_context (p->tth, peer);
96 p->nc (p->cb_cls, peer, ats, ats_count);
101 GNUNET_asprintf(&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
103 GNUNET_asprintf(&p2_s, "`%s'", GNUNET_i2s (peer));
104 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
105 "Peers %s connected to peer %u (`%s')\n",
107 p->no, GNUNET_i2s (&p->id));
112 /* Find ConnectingContext */
113 struct ConnectingContext * cc = find_connecting_context(p->tth, p, p2);
118 cc->p1_c = GNUNET_YES;
121 cc->p2_c = GNUNET_YES;
123 if ((cc->p1_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
125 cc->cb (cc->p1, cc->p2, cc->cb_cls);
126 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(p->tth, cc);
131 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
133 struct PeerContext *p = cls;
134 /* Find PeerContext */
135 struct PeerContext * p2 = find_peer_context (p->tth, peer);
139 GNUNET_asprintf(&p2_s, "%u (`%s')", p2->no, GNUNET_i2s (&p2->id));
141 GNUNET_asprintf(&p2_s, "`%s'", GNUNET_i2s (peer));
142 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
143 "Peers %s disconnected from peer %u (`%s')\n",
145 p->no, GNUNET_i2s (&p->id));
151 p->nd (p->cb_cls, peer);
155 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
156 const struct GNUNET_MessageHeader *message,
157 const struct GNUNET_TRANSPORT_ATS_Information *ats,
160 struct PeerContext *p = cls;
165 p->rec (p->cb_cls, peer, message, ats, ats_count);
169 get_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
171 struct PeerContext *p = cb_cls;
173 GNUNET_assert (message != NULL);
174 GNUNET_assert (GNUNET_OK ==
175 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
178 if (p->hello != NULL)
179 GNUNET_free (p->hello);
181 size_t size = GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *) message);
182 p->hello = GNUNET_malloc (size);
184 (const struct GNUNET_HELLO_Message *) message,
187 if (p->start_cb != NULL)
190 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
191 "Peer %u (`%s') successfully started\n",
192 p->no, GNUNET_i2s (&p->id), size);
194 p->start_cb(p, p->cb_cls);
201 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
203 struct ConnectingContext *cc = cls;
204 struct PeerContext *p1 = cc->p1;
205 struct PeerContext *p2 = cc->p2;
207 cc->tct = GNUNET_SCHEDULER_NO_TASK;
208 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
211 char * p2_s = strdup(GNUNET_i2s (&p2->id));
212 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
213 "Asking peers %u (`%s') to connect peer %u (`%s')\n",
214 p1->no, GNUNET_i2s (&p1->id), p2->no, p2_s);
217 GNUNET_TRANSPORT_offer_hello (cc->th_p1,
218 (const struct GNUNET_MessageHeader *) cc->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 TransportTestingHandle * tth,
238 GNUNET_TRANSPORT_ReceiveCallback rec,
239 GNUNET_TRANSPORT_NotifyConnect nc,
240 GNUNET_TRANSPORT_NotifyDisconnect nd,
241 GNUNET_TRANSPORT_TESTING_start_cb start_cb,
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_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
259 if (NULL != p->servicehome)
260 GNUNET_DISK_directory_remove (p->servicehome);
262 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
263 "gnunet-service-arm", "-c", cfgname,
276 p->start_cb = start_cb;
283 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
284 ¬ify_connect, ¬ify_disconnect);
285 GNUNET_assert (p->th != NULL);
287 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
288 GNUNET_assert (p->ghh != NULL);
290 GNUNET_CONTAINER_DLL_insert(tth->p_head, tth->p_tail, p);
296 * shutdown the given peer
300 GNUNET_TRANSPORT_TESTING_stop_peer (struct TransportTestingHandle * tth,
301 struct PeerContext *p)
303 GNUNET_assert (p != NULL);
306 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
310 GNUNET_TRANSPORT_disconnect (p->th);
312 if (NULL != p->arm_proc)
314 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
315 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
316 GNUNET_OS_process_wait (p->arm_proc);
317 GNUNET_OS_process_close (p->arm_proc);
321 if (p->servicehome != NULL)
323 GNUNET_DISK_directory_remove (p->servicehome);
324 GNUNET_free (p->servicehome);
327 if (p->hello != NULL)
328 GNUNET_free (p->hello);
331 GNUNET_CONFIGURATION_destroy (p->cfg);
333 GNUNET_CONTAINER_DLL_remove (tth->p_head, tth->p_tail, p);
340 * Initiate peer p1 to connect to peer p2
341 * Get peer p2's HELLO and offer it to p1
342 * p1 then tries to connect to p2
345 * @param cb the callback to call
346 * @param cb_cls callback cls
347 * @return connect context
349 GNUNET_TRANSPORT_TESTING_ConnectRequest
350 GNUNET_TRANSPORT_TESTING_connect_peers (struct TransportTestingHandle * tth,
351 struct PeerContext *p1,
352 struct PeerContext *p2,
353 GNUNET_TRANSPORT_TESTING_connect_cb cb,
357 struct ConnectingContext *cc =
358 GNUNET_malloc (sizeof (struct ConnectingContext));
360 GNUNET_assert (p1 != NULL);
361 GNUNET_assert (p2 != NULL);
370 /* GNUNET_TRANSPORT_connect (cc->p1->cfg, NULL, cc, NULL,
371 ¬ify_connect_internal, NULL);*/
374 /* GNUNET_TRANSPORT_connect (cc->p2->cfg, NULL, cc, NULL,
375 ¬ify_connect_internal, NULL);*/
377 GNUNET_assert (cc->th_p1 != NULL);
378 GNUNET_assert (cc->th_p2 != NULL);
380 GNUNET_CONTAINER_DLL_insert (tth->cc_head, tth->cc_tail, cc);
382 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
389 * Cancel the request to connect two peers
390 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
391 * @param cc a connect request handle
393 void GNUNET_TRANSPORT_TESTING_connect_peers_cancel
394 (struct TransportTestingHandle * tth,
395 GNUNET_TRANSPORT_TESTING_ConnectRequest ccr)
397 struct ConnectingContext *cc = ccr;
399 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
400 GNUNET_SCHEDULER_cancel (cc->tct);
402 cc->tct = GNUNET_SCHEDULER_NO_TASK;
404 GNUNET_CONTAINER_DLL_remove (tth->cc_head, tth->cc_tail, cc);
410 GNUNET_TRANSPORT_TESTING_done (struct TransportTestingHandle * tth)
412 struct ConnectingContext *cc = tth->cc_head;
413 struct ConnectingContext *ct = NULL;
414 struct PeerContext *p = tth->p_head;
415 struct PeerContext *t = NULL;
419 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
420 "Developer forgot to cancel connect request!\n");
421 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
428 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
429 "Developer forgot to stop peer!\n");
430 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
437 struct TransportTestingHandle *
438 GNUNET_TRANSPORT_TESTING_init ()
440 struct TransportTestingHandle * tth = GNUNET_malloc (sizeof (struct TransportTestingHandle));
447 * Some utility functions
451 * Removes all directory separators from absolute filename
452 * @param file the absolute file name, e.g. as found in argv[0]
453 * @return extracted file name, has to be freed by caller
456 extract_filename (const char *file)
458 char *pch = strdup (file);
460 char *filename = NULL;
463 if (NULL != strstr (pch, "/"))
465 pch = strtok (pch, "/");
468 pch = strtok (NULL, "/");
478 res = strdup (filename);
479 GNUNET_free (backup);
484 * Extracts the test filename from an absolute file name and removes the extension
485 * @param file absolute file name
486 * @param dest where to store result
489 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
491 char *filename = extract_filename (file);
492 char *backup = filename;
495 if (filename == NULL)
499 filename = strstr (filename, "tes");
500 if (filename == NULL)
504 if (NULL != (dotexe = strstr (filename, ".exe")))
514 /* create filename */
515 GNUNET_asprintf (dest, "%s", filename);
516 GNUNET_free (backup);
521 * Extracts the filename from an absolute file name and removes the extension
522 * @param file absolute file name
523 * @param dest where to store result
526 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
528 char *src = extract_filename (file);
531 split = strstr (src, ".");
536 GNUNET_asprintf (dest, "%s", src);
542 * Extracts the plugin anme from an absolute file name and the test name
543 * @param file absolute file name
544 * @param test test name
545 * @param dest where to store result
548 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
549 const char *test, char **dest)
551 char *e = extract_filename (file);
552 char *t = extract_filename (test);
554 char *filename = NULL;
561 filename = strstr (e, "tes");
562 if (filename == NULL)
566 if (NULL != (dotexe = strstr (filename, ".exe")))
570 filename = strstr (filename, t);
571 if (filename == NULL)
575 filename += strlen (t);
577 GNUNET_asprintf (dest, "%s", filename);
589 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
590 * if existing ".exe"-prefix and adds the peer-number
591 * @param file filename of the test, e.g. argv[0]
592 * @param cfgname where to write the result
593 * @param count peer number
596 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
599 char *filename = extract_filename (file);
600 char *backup = filename;
603 if (filename == NULL)
607 filename = strstr (filename, "tes");
608 if (filename == NULL)
612 if (NULL != (dotexe = strstr (filename, ".exe")))
622 /* create cfg filename */
623 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
624 GNUNET_free (backup);
629 /* end of transport_testing.h */