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;
146 struct AddressWrapper *w;
150 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
152 GNUNET_SCHEDULER_cancel (timeout_task);
153 timeout_task = GNUNET_SCHEDULER_NO_TASK;
156 GNUNET_PLUGIN_unload (libname, api);
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 _("Plugin did not remove address `%s' \n"), w->addrstring);
163 GNUNET_CONTAINER_DLL_remove (head, tail, w);
165 GNUNET_free (w->addr);
166 GNUNET_free (w->addrstring);
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173 _("Plugin did not remove %u addresses \n"), c);
178 GNUNET_free (libname);
180 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
183 if (NULL != suid_helper)
185 GNUNET_HELPER_stop (suid_helper);
192 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 struct AddressWrapper *w;
196 timeout_task = GNUNET_SCHEDULER_NO_TASK;
197 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
199 GNUNET_SCHEDULER_cancel (timeout_wait);
200 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 _("Test took too long to execute, timeout .... \n"));
212 GNUNET_PLUGIN_unload (libname, api);
213 GNUNET_free (libname);
220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
221 _("Plugin did not remove address `%s' \n"), w->addrstring);
222 GNUNET_CONTAINER_DLL_remove (head, tail, w);
224 GNUNET_free (w->addr);
225 GNUNET_free (w->addrstring);
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232 _("Plugin did not remove %u addresses \n"), c);
237 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
241 if (NULL != suid_helper)
243 GNUNET_HELPER_stop (suid_helper);
252 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
254 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
255 if (0 == addresses_reported)
256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
257 _("Plugin did not report any addresses, could not check address conversion functions\n"));
265 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
267 GNUNET_SCHEDULER_cancel (timeout_wait);
268 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
270 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
272 GNUNET_SCHEDULER_cancel (timeout_task);
273 timeout_task = GNUNET_SCHEDULER_NO_TASK;
275 timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
279 static struct GNUNET_TIME_Relative
280 env_receive (void *cls,
281 const struct GNUNET_PeerIdentity *peer,
282 const struct GNUNET_MessageHeader *message,
283 const struct GNUNET_ATS_Information *ats,
285 struct Session * session,
286 const char *sender_address,
287 uint16_t sender_address_len)
290 return GNUNET_TIME_relative_get_zero_();
294 static int got_reply;
298 * Take the given address and append it to the set of results sent back to
301 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
302 * @param buf text to transmit
305 address_pretty_printer_cb (void *cls, const char *buf)
309 got_reply = GNUNET_YES;
310 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
311 "Pretty address : `%s'\n", buf);
315 if (GNUNET_NO == got_reply)
325 env_notify_address (void *cls,
331 struct AddressWrapper *w;
336 if (GNUNET_YES == add_remove)
338 addresses_reported ++;
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 _("Adding address of length %u\n"), addrlen);
342 w = GNUNET_malloc (sizeof (struct AddressWrapper));
343 w->addr = GNUNET_malloc (addrlen);
344 w->addrlen = addrlen;
345 memcpy (w->addr, addr, addrlen);
346 GNUNET_CONTAINER_DLL_insert(head, tail, w);
347 got_reply = GNUNET_NO;
348 api->address_pretty_printer (api->cls, plugin, addr, addrlen,
349 GNUNET_YES, GNUNET_TIME_UNIT_MINUTES,
350 &address_pretty_printer_cb,
353 a2s = strdup (api->address_to_string (api, w->addr, w->addrlen));
357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
358 _("Plugin cannot convert address to string!\n"));
362 w->addrstring = strdup (api->address_to_string (api, w->addr, w->addrlen));
363 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
364 _("Plugin added address `%s'\n"), a2s);
366 if ((GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len)) || (NULL == s2a))
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 _("Plugin cannot convert string to address!\n"));
375 if (s2a_len != w->addrlen)
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378 _("Plugin creates different address length when converting address->string->address: %u != %u\n"), w->addrlen, s2a_len);
382 if (0 != memcmp (s2a, w->addr, s2a_len))
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384 _("Plugin creates different address length when connecting back and forth!\n"));
388 if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 _("Plugin refuses added address!\n"));
396 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
398 GNUNET_SCHEDULER_cancel (timeout_wait);
399 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
401 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
403 else if (GNUNET_NO == add_remove)
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 _("Removing address of length %u\n"), addrlen);
411 if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422 _("Plugin removes address never added!\n"));
427 GNUNET_CONTAINER_DLL_remove (head, tail, w);
428 GNUNET_free (w->addr);
429 GNUNET_free (w->addrstring);
435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
436 _("Invalid operation: %u \n"), add_remove);
443 static struct GNUNET_ATS_Information
444 env_get_address_type (void *cls,
445 const struct sockaddr *addr,
448 struct GNUNET_ATS_Information ats;
449 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
450 ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
455 static const struct GNUNET_MessageHeader *
458 return (const struct GNUNET_MessageHeader *) hello;
463 env_session_end (void *cls,
464 const struct GNUNET_PeerIdentity *peer,
465 struct Session * session)
471 setup_plugin_environment ()
475 env.my_identity = &my_identity;
476 env.max_connections = max_connect_per_transport;
479 env.receive = &env_receive;
480 env.notify_address = &env_notify_address;
481 env.get_address_type = &env_get_address_type;
482 env.get_our_hello = &env_get_our_hello;
483 env.session_end = &env_session_end;
487 handle_helper_message (void *cls, void *client,
488 const struct GNUNET_MessageHeader *hdr)
498 * @param c configuration to use
501 run (void *cls, char *const *args, const char *cfgfile,
502 const struct GNUNET_CONFIGURATION_Handle *c)
504 char *const *argv = cls;
505 unsigned long long tneigh;
510 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
513 /* parse configuration */
514 if ( (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
518 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
519 "GNUNETD", "HOSTKEY",
522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
523 _("Transport service is lacking key configuration settings. Exiting.\n"));
527 if (NULL == (stats = GNUNET_STATISTICS_create ("transport", cfg)))
529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
530 _("Could not create statistics. Exiting.\n"));
535 max_connect_per_transport = (uint32_t) tneigh;
536 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
537 GNUNET_free (keyfile);
538 if (NULL == my_private_key)
540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
541 _("Transport service could not access hostkey. Exiting.\n"));
545 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
546 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
547 &my_identity.hashPubKey);
550 hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL);
552 /* load plugins... */
553 setup_plugin_environment ();
555 GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
556 plugin = strstr(argv[0],"test_plugin_");
557 sep = strrchr(argv[0],'.');
560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
564 plugin += strlen ("test_plugin_");
568 /* Hack for WLAN: start a second helper */
569 if (0 == strcmp (plugin, "wlan"))
571 char * helper_argv[3];
572 helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
573 helper_argv[1] = (char *) "2";
574 helper_argv[2] = NULL;
575 suid_helper = GNUNET_HELPER_start (GNUNET_NO,
576 "gnunet-helper-transport-wlan-dummy",
578 &handle_helper_message,
585 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
586 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
587 api = GNUNET_PLUGIN_load (libname, &env);
590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
591 _("Failed to load transport plugin for %s\n"), plugin);
596 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
598 /* Check if all functions are implemented */
599 if (NULL == api->address_pretty_printer)
605 if (NULL == api->address_to_string)
611 GNUNET_assert (NULL != api->check_address);
612 if (NULL == api->check_address)
618 GNUNET_assert (NULL != api->disconnect);
619 if (NULL == api->disconnect)
625 GNUNET_assert (NULL != api->get_session);
626 if (NULL == api->get_session)
632 if (NULL == api->address_pretty_printer)
638 if (NULL == api->string_to_address)
648 * The main function for the test
650 * @param argc number of arguments from the command line
651 * @param argv command line arguments
652 * @return 0 ok, 1 on error
655 main (int argc, char *const *argv)
657 static struct GNUNET_GETOPT_CommandLineOption options[] = {
658 GNUNET_GETOPT_OPTION_END
662 char *const argv_prog[] = {
663 "test_plugin_transport",
665 "test_plugin_transport_data.conf",
668 GNUNET_log_setup ("test-plugin-transport",
671 ok = 1; /* set to fail */
672 ret = (GNUNET_OK == GNUNET_PROGRAM_run (3,
674 "test-plugin-transport",
678 (void *) argv)) ? ok : 1;
679 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
683 /* end of test_plugin_transport.c */