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)
46 #define HOSTKEY_FILE "test_plugin_hostkey"
51 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
56 static struct GNUNET_PeerIdentity my_identity;
61 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
66 const struct GNUNET_CONFIGURATION_Handle *cfg;
71 struct GNUNET_STATISTICS_Handle *stats;
76 struct GNUNET_HELLO_Message *hello;
79 * Number of neighbours we'd like to have.
81 static uint32_t max_connect_per_transport;
84 * Environment for this plugin.
86 struct GNUNET_TRANSPORT_PluginEnvironment env;
89 *handle for the api provided by this plugin
91 struct GNUNET_TRANSPORT_PluginFunctions *api;
96 struct GNUNET_HELPER_Handle *suid_helper;
101 static GNUNET_SCHEDULER_TaskIdentifier timeout_endbadly;
106 static GNUNET_SCHEDULER_TaskIdentifier timeout_wait;
111 static char *libname;
114 * Plugin addresses head
116 struct AddressWrapper *head;
119 * Plugin addresses tail
121 struct AddressWrapper *tail;
123 unsigned int addresses_reported;
125 unsigned int pretty_printers_running;
128 * Did the test pass or fail?
133 struct AddressWrapper
135 struct AddressWrapper *next;
137 struct AddressWrapper *prev;
150 struct AddressWrapper *w;
154 if (GNUNET_SCHEDULER_NO_TASK != timeout_endbadly)
156 GNUNET_SCHEDULER_cancel (timeout_endbadly);
157 timeout_endbadly = GNUNET_SCHEDULER_NO_TASK;
160 GNUNET_PLUGIN_unload (libname, api);
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 _("Plugin did not remove address `%s' \n"), w->addrstring);
167 GNUNET_CONTAINER_DLL_remove (head, tail, w);
169 GNUNET_free (w->addr);
170 GNUNET_free (w->addrstring);
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177 _("Plugin did not remove %u addresses \n"), c);
182 GNUNET_free (libname);
184 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
187 if (NULL != suid_helper)
189 GNUNET_HELPER_stop (suid_helper);
196 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 struct AddressWrapper *w;
200 timeout_endbadly = GNUNET_SCHEDULER_NO_TASK;
201 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
203 GNUNET_SCHEDULER_cancel (timeout_wait);
204 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
207 if (pretty_printers_running > 0)
209 timeout_endbadly = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, &end_badly, &ok);
210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211 _("Have pending calls to pretty_printer ... deferring shutdown\n"));
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 _("Test took too long to execute, timeout .... \n"));
224 GNUNET_PLUGIN_unload (libname, api);
225 GNUNET_free (libname);
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233 _("Plugin did not remove address `%s' \n"), w->addrstring);
234 GNUNET_CONTAINER_DLL_remove (head, tail, w);
236 GNUNET_free (w->addr);
237 GNUNET_free (w->addrstring);
243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244 _("Plugin did not remove %u addresses \n"), c);
249 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
253 if (NULL != suid_helper)
255 GNUNET_HELPER_stop (suid_helper);
264 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
266 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
267 if (0 == addresses_reported)
268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
269 _("Plugin did not report any addresses, could not check address conversion functions\n"));
277 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
279 GNUNET_SCHEDULER_cancel (timeout_wait);
280 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
282 if (GNUNET_SCHEDULER_NO_TASK != timeout_endbadly)
284 GNUNET_SCHEDULER_cancel (timeout_endbadly);
285 timeout_endbadly = GNUNET_SCHEDULER_NO_TASK;
287 timeout_endbadly = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
291 static struct GNUNET_TIME_Relative
292 env_receive (void *cls,
293 const struct GNUNET_PeerIdentity *peer,
294 const struct GNUNET_MessageHeader *message,
295 const struct GNUNET_ATS_Information *ats,
297 struct Session * session,
298 const char *sender_address,
299 uint16_t sender_address_len)
302 return GNUNET_TIME_relative_get_zero_();
306 static int got_reply;
310 * Take the given address and append it to the set of results sent back to
313 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
314 * @param buf text to transmit
317 address_pretty_printer_cb (void *cls, const char *buf)
321 got_reply = GNUNET_YES;
322 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
323 "Pretty address : `%s'\n", buf);
324 pretty_printers_running --;
328 if (GNUNET_NO == got_reply)
330 pretty_printers_running --;
339 env_notify_address (void *cls,
345 struct AddressWrapper *w;
350 if (GNUNET_YES == add_remove)
352 addresses_reported ++;
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 _("Adding address of length %u\n"), addrlen);
356 w = GNUNET_malloc (sizeof (struct AddressWrapper));
357 w->addr = GNUNET_malloc (addrlen);
358 w->addrlen = addrlen;
359 memcpy (w->addr, addr, addrlen);
360 GNUNET_CONTAINER_DLL_insert(head, tail, w);
361 got_reply = GNUNET_NO;
362 pretty_printers_running ++;
363 api->address_pretty_printer (api->cls, plugin, addr, addrlen,
364 GNUNET_YES, GNUNET_TIME_UNIT_MINUTES,
365 &address_pretty_printer_cb,
368 a2s = strdup (api->address_to_string (api, w->addr, w->addrlen));
372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
373 _("Plugin cannot convert address to string!\n"));
377 w->addrstring = strdup (api->address_to_string (api, w->addr, w->addrlen));
378 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
379 _("Plugin added address `%s'\n"), a2s);
381 if ((GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len)) || (NULL == s2a))
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 _("Plugin cannot convert string to address!\n"));
390 if (s2a_len != w->addrlen)
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 _("Plugin creates different address length when converting address->string->address: %u != %u\n"), w->addrlen, s2a_len);
397 if (0 != memcmp (s2a, w->addr, s2a_len))
398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
399 _("Plugin creates different address length when connecting back and forth!\n"));
403 if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
407 _("Plugin refuses added address!\n"));
411 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
413 GNUNET_SCHEDULER_cancel (timeout_wait);
414 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
417 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
420 else if (GNUNET_NO == add_remove)
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 _("Removing address of length %u\n"), addrlen);
428 if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
439 _("Plugin removes address never added!\n"));
444 GNUNET_CONTAINER_DLL_remove (head, tail, w);
445 GNUNET_free (w->addr);
446 GNUNET_free (w->addrstring);
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 _("Invalid operation: %u \n"), add_remove);
460 static struct GNUNET_ATS_Information
461 env_get_address_type (void *cls,
462 const struct sockaddr *addr,
465 struct GNUNET_ATS_Information ats;
466 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
467 ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
472 static const struct GNUNET_MessageHeader *
475 return (const struct GNUNET_MessageHeader *) hello;
480 env_session_end (void *cls,
481 const struct GNUNET_PeerIdentity *peer,
482 struct Session * session)
488 setup_plugin_environment ()
492 env.my_identity = &my_identity;
493 env.max_connections = max_connect_per_transport;
496 env.receive = &env_receive;
497 env.notify_address = &env_notify_address;
498 env.get_address_type = &env_get_address_type;
499 env.get_our_hello = &env_get_our_hello;
500 env.session_end = &env_session_end;
504 handle_helper_message (void *cls, void *client,
505 const struct GNUNET_MessageHeader *hdr)
515 * @param c configuration to use
518 run (void *cls, char *const *args, const char *cfgfile,
519 const struct GNUNET_CONFIGURATION_Handle *c)
521 char *const *argv = cls;
522 unsigned long long tneigh;
527 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
530 /* parse configuration */
531 if ( (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
535 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
536 "GNUNETD", "HOSTKEY",
539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
540 _("Transport service is lacking key configuration settings. Exiting.\n"));
544 if (NULL == (stats = GNUNET_STATISTICS_create ("transport", cfg)))
546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
547 _("Could not create statistics. Exiting.\n"));
552 if (GNUNET_OK != GNUNET_DISK_file_test (HOSTKEY_FILE))
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
555 _("Hostkey `%s' missing. Exiting.\n"),
559 if (GNUNET_OK != GNUNET_DISK_file_copy (HOSTKEY_FILE, keyfile))
561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
562 _("Could not copy hostkey `%s' to destination `%s'. Exiting.\n"),
563 HOSTKEY_FILE, keyfile);
569 max_connect_per_transport = (uint32_t) tneigh;
570 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
571 GNUNET_free (keyfile);
572 if (NULL == my_private_key)
574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
575 _("Transport service could not access hostkey. Exiting.\n"));
579 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
580 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
581 &my_identity.hashPubKey);
584 hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL);
586 /* load plugins... */
587 setup_plugin_environment ();
589 GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
590 plugin = strstr(argv[0],"test_plugin_");
591 sep = strrchr(argv[0],'.');
594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
598 plugin += strlen ("test_plugin_");
602 /* Hack for WLAN: start a second helper */
603 if (0 == strcmp (plugin, "wlan"))
605 char * helper_argv[3];
606 helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
607 helper_argv[1] = (char *) "2";
608 helper_argv[2] = NULL;
609 suid_helper = GNUNET_HELPER_start (GNUNET_NO,
610 "gnunet-helper-transport-wlan-dummy",
612 &handle_helper_message,
618 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
619 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
620 api = GNUNET_PLUGIN_load (libname, &env);
623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
624 _("Failed to load transport plugin for %s\n"), plugin);
629 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
631 /* Check if all functions are implemented */
632 if (NULL == api->address_pretty_printer)
638 if (NULL == api->address_to_string)
644 GNUNET_assert (NULL != api->check_address);
645 if (NULL == api->check_address)
651 GNUNET_assert (NULL != api->disconnect);
652 if (NULL == api->disconnect)
658 GNUNET_assert (NULL != api->get_session);
659 if (NULL == api->get_session)
665 if (NULL == api->address_pretty_printer)
671 if (NULL == api->string_to_address)
682 * The main function for the test
684 * @param argc number of arguments from the command line
685 * @param argv command line arguments
686 * @return 0 ok, 1 on error
689 main (int argc, char *const *argv)
691 static struct GNUNET_GETOPT_CommandLineOption options[] = {
692 GNUNET_GETOPT_OPTION_END
696 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
698 char *const argv_prog[] = {
699 "test_plugin_transport",
701 "test_plugin_transport_data.conf",
704 GNUNET_log_setup ("test-plugin-transport",
707 ok = 1; /* set to fail */
708 ret = (GNUNET_OK == GNUNET_PROGRAM_run (3,
710 "test-plugin-transport",
714 (void *) argv)) ? ok : 1;
715 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
719 /* end of test_plugin_transport.c */