2 This file is part of GNUnet.
3 (C) 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 3, 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.
21 * @file transport/test_plugin_transport.c
22 * @brief testcase for transport_api.c
23 * @author Sailor Siraj
24 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_transport_plugin.h"
38 #include "transport.h"
41 * How long until we give up on transmitting the message?
43 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
49 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
54 static struct GNUNET_PeerIdentity my_identity;
59 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
64 const struct GNUNET_CONFIGURATION_Handle *cfg;
69 struct GNUNET_STATISTICS_Handle *stats;
74 struct GNUNET_HELLO_Message *hello;
77 * Number of neighbours we'd like to have.
79 static uint32_t max_connect_per_transport;
82 * Environment for this plugin.
84 struct GNUNET_TRANSPORT_PluginEnvironment env;
87 *handle for the api provided by this plugin
89 struct GNUNET_TRANSPORT_PluginFunctions *api;
94 struct GNUNET_HELPER_Handle *suid_helper;
99 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
104 static GNUNET_SCHEDULER_TaskIdentifier timeout_wait;
109 static char *libname;
112 * Plugin addresses head
114 struct AddressWrapper *head;
117 * Plugin addresses tail
119 struct AddressWrapper *tail;
121 unsigned int addresses_reported;
124 * Did the test pass or fail?
129 struct AddressWrapper
131 struct AddressWrapper *next;
133 struct AddressWrapper *prev;
145 struct AddressWrapper *w;
149 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
151 GNUNET_SCHEDULER_cancel (timeout_task);
152 timeout_task = GNUNET_SCHEDULER_NO_TASK;
155 GNUNET_PLUGIN_unload (libname, api);
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161 _("Plugin did not remove address `%s' \n"), w->addrstring);
162 GNUNET_CONTAINER_DLL_remove (head, tail, w);
164 GNUNET_free (w->addr);
165 GNUNET_free (w->addrstring);
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 _("Plugin did not remove %u addresses \n"), c);
177 GNUNET_free (libname);
179 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
182 if (NULL != suid_helper)
184 GNUNET_HELPER_stop (suid_helper);
190 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 struct AddressWrapper *w;
194 timeout_task = GNUNET_SCHEDULER_NO_TASK;
195 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
197 GNUNET_SCHEDULER_cancel (timeout_wait);
198 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 _("Test took too long to execute, timeout .... \n"));
210 GNUNET_PLUGIN_unload (libname, api);
211 GNUNET_free (libname);
218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219 _("Plugin did not remove address `%s' \n"), w->addrstring);
220 GNUNET_CONTAINER_DLL_remove (head, tail, w);
222 GNUNET_free (w->addr);
223 GNUNET_free (w->addrstring);
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 _("Plugin did not remove %u addresses \n"), c);
235 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
239 if (NULL != suid_helper)
241 GNUNET_HELPER_stop (suid_helper);
249 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
252 if (0 == addresses_reported)
253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
254 _("Plugin did not report any addresses, could not check address conversion functions\n"));
262 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
264 GNUNET_SCHEDULER_cancel (timeout_wait);
265 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
267 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
269 GNUNET_SCHEDULER_cancel (timeout_task);
270 timeout_task = GNUNET_SCHEDULER_NO_TASK;
272 timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
276 static struct GNUNET_TIME_Relative
277 env_receive (void *cls,
278 const struct GNUNET_PeerIdentity *peer,
279 const struct GNUNET_MessageHeader *message,
280 const struct GNUNET_ATS_Information *ats,
282 struct Session * session,
283 const char *sender_address,
284 uint16_t sender_address_len)
287 return GNUNET_TIME_relative_get_zero_();
291 * Take the given address and append it to the set of results sent back to
294 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
295 * @param buf text to transmit
298 address_pretty_printer_cb (void *cls, const char *buf)
302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
303 "Pretty address : `%s'\n", buf);
314 env_notify_address (void *cls,
320 struct AddressWrapper *w;
325 if (GNUNET_YES == add_remove)
327 addresses_reported ++;
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 _("Adding address of length %u\n"), addrlen);
331 w = GNUNET_malloc (sizeof (struct AddressWrapper));
332 w->addr = GNUNET_malloc (addrlen);
333 w->addrlen = addrlen;
334 memcpy (w->addr, addr, addrlen);
335 GNUNET_CONTAINER_DLL_insert(head, tail, w);
337 api->address_pretty_printer (api->cls, plugin, addr, addrlen,
338 GNUNET_YES, GNUNET_TIME_UNIT_MINUTES,
339 &address_pretty_printer_cb,
342 a2s = strdup (api->address_to_string (api, w->addr, w->addrlen));
346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
347 _("Plugin cannot convert address to string!\n"));
351 w->addrstring = strdup (api->address_to_string (api, w->addr, w->addrlen));
352 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
353 _("Plugin added address `%s'\n"), a2s);
355 if ((GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len)) || (NULL == s2a))
358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
359 _("Plugin cannot convert string to address!\n"));
364 if (s2a_len != w->addrlen)
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 _("Plugin creates different address length when converting address->string->address: %u != %u\n"), w->addrlen, s2a_len);
371 if (0 != memcmp (s2a, w->addr, s2a_len))
372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
373 _("Plugin creates different address length when connecting back and forth!\n"));
377 if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381 _("Plugin refuses added address!\n"));
385 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
387 GNUNET_SCHEDULER_cancel (timeout_wait);
388 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
390 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
392 else if (GNUNET_NO == add_remove)
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 _("Removing address of length %u\n"), addrlen);
400 if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
411 _("Plugin removes address never added!\n"));
416 GNUNET_CONTAINER_DLL_remove (head, tail, w);
417 GNUNET_free (w->addr);
418 GNUNET_free (w->addrstring);
424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
425 _("Invalid operation: %u \n"), add_remove);
431 struct GNUNET_ATS_Information
432 env_get_address_type (void *cls,
433 const struct sockaddr *addr,
436 struct GNUNET_ATS_Information ats;
437 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
438 ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
442 const struct GNUNET_MessageHeader *
443 env_get_our_hello (void)
445 return (const struct GNUNET_MessageHeader *) hello;
448 void env_session_end (void *cls,
449 const struct GNUNET_PeerIdentity *peer,
450 struct Session * session)
456 setup_plugin_environment ()
460 env.my_identity = &my_identity;
461 env.max_connections = max_connect_per_transport;
464 env.receive = &env_receive;
465 env.notify_address = &env_notify_address;
466 env.get_address_type = &env_get_address_type;
467 env.get_our_hello = &env_get_our_hello;
468 env.session_end = &env_session_end;
472 handle_helper_message (void *cls, void *client,
473 const struct GNUNET_MessageHeader *hdr)
482 * @param c configuration to use
485 run (void *cls, char *const *args, const char *cfgfile,
486 const struct GNUNET_CONFIGURATION_Handle *c)
488 char *const *argv = cls;
489 unsigned long long tneigh;
494 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
497 /* parse configuration */
498 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
502 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
503 "GNUNETD", "HOSTKEY",
506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
507 _("Transport service is lacking key configuration settings. Exiting.\n"));
512 stats = GNUNET_STATISTICS_create ("transport", cfg);
515 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
516 _("Could not create statistics. Exiting.\n"));
521 max_connect_per_transport = (uint32_t) tneigh;
522 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
523 GNUNET_free (keyfile);
524 if (my_private_key == NULL)
526 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
527 _("Transport service could not access hostkey. Exiting.\n"));
531 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
532 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
533 &my_identity.hashPubKey);
536 hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL);
538 /* load plugins... */
539 setup_plugin_environment ();
541 GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
542 plugin = strstr(argv[0],"test_plugin_");
543 sep = strrchr(argv[0],'.');
546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
550 plugin += strlen ("test_plugin_");
554 /* Hack for WLAN: start a second helper */
555 if (0 == strcmp (plugin, "wlan"))
557 char * helper_argv[3];
558 helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
559 helper_argv[1] = (char *) "2";
560 helper_argv[2] = NULL;
561 suid_helper = GNUNET_HELPER_start (GNUNET_NO,
562 "gnunet-helper-transport-wlan-dummy",
564 &handle_helper_message,
571 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
572 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
573 api = GNUNET_PLUGIN_load (libname, &env);
576 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
577 _("Failed to load transport plugin for %s\n"), plugin);
582 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
584 /* Check if all functions are implemented */
585 if (NULL == api->address_pretty_printer)
591 if (NULL == api->address_to_string)
597 GNUNET_assert (NULL != api->check_address);
598 if (NULL == api->check_address)
604 GNUNET_assert (NULL != api->disconnect);
605 if (NULL == api->disconnect)
611 GNUNET_assert (NULL != api->get_session);
612 if (NULL == api->get_session)
618 if (NULL == api->address_pretty_printer)
624 if (NULL == api->string_to_address)
634 * The main function for the transport service.
636 * @param argc number of arguments from the command line
637 * @param argv command line arguments
638 * @return 0 ok, 1 on error
641 main (int argc, char *const *argv)
643 static struct GNUNET_GETOPT_CommandLineOption options[] = {
644 GNUNET_GETOPT_OPTION_END
648 char *const argv_prog[] = {
649 "test_plugin_transport",
651 "test_plugin_transport_data.conf",
655 GNUNET_log_setup ("test-plugin-transport",
658 ok = 1; /* set to fail */
659 ret = (GNUNET_OK == GNUNET_PROGRAM_run (5,
661 "test-plugin-transport",
665 (void *) argv)) ? ok : 1;
666 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
670 /* end of test_plugin_transport.c */