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 struct ConnectingContext
32 struct PeerContext *p1;
33 struct PeerContext *p2;
34 GNUNET_SCHEDULER_TaskIdentifier tct;
35 GNUNET_TRANSPORT_TESTING_connect_cb cb;
38 struct GNUNET_TRANSPORT_Handle *th_p1;
39 struct GNUNET_TRANSPORT_Handle *th_p2;
45 exchange_hello_last (void *cb_cls, const struct GNUNET_MessageHeader *message);
47 exchange_hello (void *cb_cls, const struct GNUNET_MessageHeader *message);
50 notify_connect_internal (void *cls, const struct GNUNET_PeerIdentity *peer,
51 const struct GNUNET_TRANSPORT_ATS_Information *ats,
54 struct ConnectingContext *cc = cls;
56 GNUNET_assert (cc != NULL);
59 memcmp (&(*peer).hashPubKey, &cc->p1->id.hashPubKey,
60 sizeof (GNUNET_HashCode)))
62 if (cc->p1_c == GNUNET_NO)
63 cc->p1_c = GNUNET_YES;
66 memcmp (&(*peer).hashPubKey, &cc->p2->id.hashPubKey,
67 sizeof (GNUNET_HashCode)))
69 if (cc->p2_c == GNUNET_NO)
70 cc->p2_c = GNUNET_YES;
73 if ((cc->p2_c == GNUNET_YES) && (cc->p2_c == GNUNET_YES))
76 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p2, &exchange_hello_last, cc);
77 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p1, &exchange_hello, cc);
79 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
80 GNUNET_SCHEDULER_cancel (cc->tct);
82 cc->tct = GNUNET_SCHEDULER_NO_TASK;
84 GNUNET_TRANSPORT_disconnect (cc->th_p1);
85 GNUNET_TRANSPORT_disconnect (cc->th_p2);
88 cc->cb (cc->p1, cc->p2, cc->cb_cls);
95 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
96 const struct GNUNET_TRANSPORT_ATS_Information *ats,
99 struct PeerContext *p = cls;
104 p->nc (p->cb_cls, peer, ats, ats_count);
108 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
110 struct PeerContext *p = cls;
115 p->nd (p->cb_cls, peer);
119 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
120 const struct GNUNET_MessageHeader *message,
121 const struct GNUNET_TRANSPORT_ATS_Information *ats,
124 struct PeerContext *p = cls;
129 p->rec (p->cb_cls, peer, message, ats, ats_count);
134 exchange_hello_last (void *cb_cls, const struct GNUNET_MessageHeader *message)
136 struct ConnectingContext *cc = cb_cls;
137 struct PeerContext *me = cc->p2;
139 //struct PeerContext *p1 = cc->p1;
141 GNUNET_assert (message != NULL);
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Exchanging HELLO of size %d with peer (%s)!\n",
144 (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
145 message), GNUNET_i2s (&me->id));
146 GNUNET_assert (GNUNET_OK ==
147 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
149 GNUNET_TRANSPORT_offer_hello (cc->th_p1, message, NULL, NULL);
154 exchange_hello (void *cb_cls, const struct GNUNET_MessageHeader *message)
156 struct ConnectingContext *cc = cb_cls;
157 struct PeerContext *me = cc->p1;
159 //struct PeerContext *p2 = cc->p2;
161 GNUNET_assert (message != NULL);
162 GNUNET_assert (GNUNET_OK ==
163 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Exchanging HELLO of size %d from peer %s!\n",
167 (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
168 message), GNUNET_i2s (&me->id));
169 GNUNET_TRANSPORT_offer_hello (cc->th_p2, message, NULL, NULL);
173 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 struct ConnectingContext *cc = cls;
176 struct PeerContext *p1 = cc->p1;
177 struct PeerContext *p2 = cc->p2;
179 cc->tct = GNUNET_SCHEDULER_NO_TASK;
180 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking peers to connect...\n");
184 /* FIXME: 'pX.id' may still be all-zeros here... */
185 GNUNET_TRANSPORT_try_connect (cc->th_p1, &p2->id);
186 GNUNET_TRANSPORT_try_connect (cc->th_p2, &p1->id);
189 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect, cc);
194 * Start a peer with the given configuration
195 * @param rec receive callback
196 * @param nc connect callback
197 * @param nd disconnect callback
198 * @param cb_cls closure for callback
199 * @return the peer context
202 GNUNET_TRANSPORT_TESTING_start_peer (const char *cfgname,
203 GNUNET_TRANSPORT_ReceiveCallback rec,
204 GNUNET_TRANSPORT_NotifyConnect nc,
205 GNUNET_TRANSPORT_NotifyDisconnect nd,
208 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", cfgname);
214 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
216 p->cfg = GNUNET_CONFIGURATION_create ();
218 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
219 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
220 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
222 if (NULL != p->servicehome)
223 GNUNET_DISK_directory_remove (p->servicehome);
225 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
226 "gnunet-service-arm", "-c", cfgname,
242 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
243 ¬ify_connect, ¬ify_disconnect);
244 GNUNET_assert (p->th != NULL);
249 * shutdown the given peer
253 GNUNET_TRANSPORT_TESTING_stop_peer (struct PeerContext *p)
255 GNUNET_assert (p != NULL);
257 GNUNET_TRANSPORT_disconnect (p->th);
259 if (NULL != p->arm_proc)
261 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
262 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
263 GNUNET_OS_process_wait (p->arm_proc);
264 GNUNET_OS_process_close (p->arm_proc);
267 if (p->servicehome != NULL)
269 GNUNET_DISK_directory_remove (p->servicehome);
270 GNUNET_free (p->servicehome);
274 GNUNET_CONFIGURATION_destroy (p->cfg);
280 * Connect the two given peers and call the callback when both peers report the
281 * inbound connect. Remarks: start_peer's notify_connect callback can be called
285 * @param cb the callback to call
286 * @param cb_cls callback cls
287 * @return connect context
289 GNUNET_TRANSPORT_TESTING_ConnectRequest
290 GNUNET_TRANSPORT_TESTING_connect_peers (struct PeerContext *p1,
291 struct PeerContext *p2,
292 GNUNET_TRANSPORT_TESTING_connect_cb cb,
295 struct ConnectingContext *cc =
296 GNUNET_malloc (sizeof (struct ConnectingContext));
298 GNUNET_assert (p1 != NULL);
299 GNUNET_assert (p2 != NULL);
308 GNUNET_TRANSPORT_connect (cc->p1->cfg, NULL, cc, NULL,
309 ¬ify_connect_internal, NULL);
312 GNUNET_TRANSPORT_connect (cc->p2->cfg, NULL, cc, NULL,
313 ¬ify_connect_internal, NULL);
315 GNUNET_assert (cc->th_p1 != NULL);
316 GNUNET_assert (cc->th_p2 != NULL);
318 GNUNET_TRANSPORT_get_hello (cc->th_p1, &exchange_hello, cc);
319 GNUNET_TRANSPORT_get_hello (cc->th_p2, &exchange_hello_last, cc);
321 cc->tct = GNUNET_SCHEDULER_add_now (&try_connect, cc);
326 * Cancel the request to connect two peers
327 * Tou MUST cancel the request if you stop the peers before the peers connected succesfully
328 * @param cc a connect request handle
330 void GNUNET_TRANSPORT_TESTING_connect_peers_cancel
331 (GNUNET_TRANSPORT_TESTING_ConnectRequest ccr)
333 struct ConnectingContext *cc = ccr;
336 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p2, &exchange_hello_last, cc);
337 GNUNET_TRANSPORT_get_hello_cancel (cc->th_p1, &exchange_hello, cc);
339 if (cc->tct != GNUNET_SCHEDULER_NO_TASK)
340 GNUNET_SCHEDULER_cancel (cc->tct);
342 cc->tct = GNUNET_SCHEDULER_NO_TASK;
344 GNUNET_TRANSPORT_disconnect (cc->th_p1);
345 GNUNET_TRANSPORT_disconnect (cc->th_p2);
352 * Some utility functions
356 * Removes all directory separators from absolute filename
357 * @param file the absolute file name, e.g. as found in argv[0]
358 * @return extracted file name, has to be freed by caller
361 extract_filename (const char *file)
363 char *pch = strdup (file);
365 char *filename = NULL;
368 if (NULL != strstr (pch, "/"))
370 pch = strtok (pch, "/");
373 pch = strtok (NULL, "/");
383 res = strdup (filename);
384 GNUNET_free (backup);
389 * Extracts the test filename from an absolute file name and removes the extension
390 * @param file absolute file name
391 * @param dest where to store result
394 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
396 char *filename = extract_filename (file);
397 char *backup = filename;
400 if (filename == NULL)
404 filename = strstr (filename, "tes");
405 if (filename == NULL)
409 if (NULL != (dotexe = strstr (filename, ".exe")))
412 if (filename == NULL)
421 /* create filename */
422 GNUNET_asprintf (dest, "%s", filename);
423 GNUNET_free (backup);
428 * Extracts the filename from an absolute file name and removes the extension
429 * @param file absolute file name
430 * @param dest where to store result
433 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
435 char *src = extract_filename (file);
438 split = strstr (src, ".");
443 GNUNET_asprintf (dest, "%s", src);
449 * Extracts the plugin anme from an absolute file name and the test name
450 * @param file absolute file name
451 * @param test test name
452 * @param dest where to store result
455 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
456 const char *test, char **dest)
458 char *e = extract_filename (file);
459 char *t = extract_filename (test);
461 char *filename = NULL;
468 filename = strstr (e, "tes");
469 if (filename == NULL)
473 if (NULL != (dotexe = strstr (filename, ".exe")))
477 filename = strstr (filename, t);
478 if (filename == NULL)
482 filename += strlen (t);
484 GNUNET_asprintf (dest, "%s", filename);
496 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
497 * if existing ".exe"-prefix and adds the peer-number
498 * @param file filename of the test, e.g. argv[0]
499 * @param cfgname where to write the result
500 * @param count peer number
503 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
506 char *filename = extract_filename (file);
507 char *backup = filename;
510 if (filename == NULL)
514 filename = strstr (filename, "tes");
515 if (filename == NULL)
519 if (NULL != (dotexe = strstr (filename, ".exe")))
522 if (filename == NULL)
531 /* create cfg filename */
532 GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
533 GNUNET_free (backup);
538 /* end of transport_testing.h */