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_util_lib.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_transport_plugin.h"
34 #include "transport.h"
37 * How long until we give up on transmitting the message?
39 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
42 #define HOSTKEY_FILE "test_plugin_hostkey.ecc"
47 static struct GNUNET_PeerIdentity my_identity;
52 static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
57 const struct GNUNET_CONFIGURATION_Handle *cfg;
62 struct GNUNET_STATISTICS_Handle *stats;
67 struct GNUNET_HELLO_Message *hello;
70 * Number of neighbours we'd like to have.
72 static uint32_t max_connect_per_transport;
75 * Environment for this plugin.
77 struct GNUNET_TRANSPORT_PluginEnvironment env;
80 *handle for the api provided by this plugin
82 struct GNUNET_TRANSPORT_PluginFunctions *api;
87 struct GNUNET_HELPER_Handle *suid_helper;
92 static struct GNUNET_SCHEDULER_Task * timeout_endbadly;
97 static struct GNUNET_SCHEDULER_Task * timeout_wait;
102 static char *libname;
105 * Plugin addresses head
107 struct AddressWrapper *head;
110 * Plugin addresses tail
112 struct AddressWrapper *tail;
114 unsigned int addresses_reported;
116 unsigned int pretty_printers_running;
119 * Did the test pass or fail?
123 struct AddressWrapper
125 struct AddressWrapper *next;
127 struct AddressWrapper *prev;
129 struct GNUNET_HELLO_Address *address;
137 struct AddressWrapper *w;
141 if (NULL != timeout_endbadly)
143 GNUNET_SCHEDULER_cancel (timeout_endbadly);
144 timeout_endbadly = NULL;
147 GNUNET_PLUGIN_unload (libname, api);
152 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove address `%s'\n",
154 GNUNET_CONTAINER_DLL_remove(head, tail, w);
156 GNUNET_HELLO_address_free(w->address);
157 GNUNET_free(w->addrstring);
163 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses \n",
168 GNUNET_free(libname);
170 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
173 if (NULL != suid_helper)
175 GNUNET_HELPER_stop (suid_helper, GNUNET_NO);
181 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 struct AddressWrapper *w;
186 timeout_endbadly = NULL;
187 if (NULL != timeout_wait)
189 GNUNET_SCHEDULER_cancel (timeout_wait);
193 if (pretty_printers_running > 0)
195 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
197 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
198 "Have pending calls to pretty_printer ... deferring shutdown\n");
204 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
205 "Test took too long to execute, timeout .... \n");
211 GNUNET_PLUGIN_unload (libname, api);
212 GNUNET_free(libname);
219 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove address `%s'\n",
221 GNUNET_CONTAINER_DLL_remove(head, tail, w);
223 GNUNET_HELLO_address_free(w->address);
224 GNUNET_free(w->addrstring);
230 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses\n",
236 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
240 if (NULL != suid_helper)
242 GNUNET_HELPER_stop (suid_helper, GNUNET_NO);
250 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
253 if (0 == addresses_reported)
254 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
255 "Plugin did not report any addresses, could not check address conversion functions\n");
263 if (NULL != timeout_wait)
265 GNUNET_SCHEDULER_cancel (timeout_wait);
268 if (NULL != timeout_endbadly)
270 GNUNET_SCHEDULER_cancel (timeout_endbadly);
271 timeout_endbadly = NULL;
273 timeout_endbadly = GNUNET_SCHEDULER_add_now (&end_badly, NULL );
277 static struct GNUNET_TIME_Relative
278 env_receive (void *cls,
279 const struct GNUNET_HELLO_Address *address,
280 struct Session *session,
281 const struct GNUNET_MessageHeader *message)
284 return GNUNET_TIME_relative_get_zero_ ();
287 static int got_reply;
291 * Take the given address and append it to the set of results sent back to
295 * @param address the address to print
296 * @param res result code
299 address_pretty_printer_cb (void *cls, const char *address, int res)
303 got_reply = GNUNET_YES;
304 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Pretty address : `%s'\n", address);
305 pretty_printers_running--;
309 if (GNUNET_NO == got_reply)
311 pretty_printers_running--;
320 env_notify_address (void *cls,
322 const struct GNUNET_HELLO_Address *address)
324 struct AddressWrapper *w;
325 struct AddressWrapper *wtmp;
329 if (GNUNET_YES == add_remove)
331 addresses_reported++;
332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Adding address of length %u\n",
333 address->address_length);
335 for (wtmp = head; NULL != wtmp; wtmp = wtmp->next)
337 if ((address->address_length == wtmp->address->address_length) &&
338 (0 == memcmp (address->address, wtmp->address->address, address->address_length)))
340 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
341 "Duplicate address notification .... \n");
346 w = GNUNET_new (struct AddressWrapper);
347 w->address = GNUNET_HELLO_address_copy (address);
348 GNUNET_CONTAINER_DLL_insert(head, tail, w);
349 got_reply = GNUNET_NO;
350 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Testing: address_to_string \n");
351 w->addrstring = strdup (
352 api->address_to_string (api, w->address->address,
353 w->address->address_length));
354 if (NULL == w->addrstring)
357 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
358 "Plugin cannot convert address to string!\n");
364 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Plugin added address `%s'\n",
366 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Testing address_to_string: OK\n");
369 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Testing: string_to_address \n");
373 != api->string_to_address (api, w->addrstring,
374 strlen (w->addrstring) + 1, &s2a, &s2a_len)) || (NULL == s2a))
377 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
378 "Plugin cannot convert string to address!\n");
384 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
385 "Plugin creates `%s' %u\n",api->address_to_string (api, s2a, s2a_len), s2a_len);
388 for (c1 = 0; c1 < s2a_len; c1++ )
389 fprintf (stderr, "%u == %u\n", ((char *) s2a)[c1], ((char *) w->addr)[c1]);
391 if (s2a_len != w->address->address_length)
393 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
394 "Plugin creates different address length when converting address->string->address: %u != %u\n",
395 w->address->address_length, s2a_len);
397 else if (0 != memcmp (s2a, w->address->address, s2a_len))
399 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
400 "Plugin creates different address length when converting back and forth %i!\n",
401 memcmp (s2a, w->address->address, s2a_len));
405 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Testing string_to_address: OK\n");
409 pretty_printers_running++;
410 api->address_pretty_printer (api->cls, address->transport_name,
411 address->address, address->address_length, GNUNET_YES,
412 GNUNET_TIME_UNIT_MINUTES, &address_pretty_printer_cb, w);
414 if (GNUNET_OK != api->check_address (api->cls, w->address->address, w->address->address_length))
417 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin refuses added address!\n");
421 if (NULL != timeout_wait)
423 GNUNET_SCHEDULER_cancel (timeout_wait);
427 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL );
430 else if (GNUNET_NO == add_remove)
432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Removing address of length %u\n",
433 address->address_length);
438 if ((address->address_length == w->address->address_length) &&
439 (0 == memcmp (w->address->address, address->address, address->address_length)))
449 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
450 "Plugin removes address never added!\n");
455 GNUNET_CONTAINER_DLL_remove(head, tail, w);
456 GNUNET_HELLO_address_free (w->address);
457 GNUNET_free(w->addrstring);
463 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Invalid operation: %u\n", add_remove);
470 static enum GNUNET_ATS_Network_Type
471 env_get_address_type (void *cls,
472 const struct sockaddr *addr,
475 return GNUNET_ATS_NET_LOOPBACK;
479 static const struct GNUNET_MessageHeader *
482 return (const struct GNUNET_MessageHeader *) hello;
487 env_session_end (void *cls,
488 const struct GNUNET_HELLO_Address *address,
489 struct Session *session)
496 env_update_metrics (void *cls,
497 const struct GNUNET_HELLO_Address *address,
498 struct Session *session,
499 const struct GNUNET_ATS_Information *ats,
506 setup_plugin_environment ()
510 env.my_identity = &my_identity;
511 env.max_connections = max_connect_per_transport;
513 env.receive = &env_receive;
514 env.notify_address = &env_notify_address;
515 env.get_address_type = &env_get_address_type;
516 env.update_address_metrics = &env_update_metrics;
517 env.get_our_hello = &env_get_our_hello;
518 env.session_end = &env_session_end;
523 handle_helper_message (void *cls, void *client,
524 const struct GNUNET_MessageHeader *hdr)
534 * @param c configuration to use
537 run (void *cls, char * const *args, const char *cfgfile,
538 const struct GNUNET_CONFIGURATION_Handle *c)
540 char * const *argv = cls;
541 unsigned long long tneigh;
546 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
549 /* parse configuration */
551 != GNUNET_CONFIGURATION_get_value_number (c, "TRANSPORT",
552 "NEIGHBOUR_LIMIT", &tneigh))
554 != GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
557 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
558 "Transport service is lacking key configuration settings. Exiting.\n");
562 if (NULL == (stats = GNUNET_STATISTICS_create ("transport", cfg)))
564 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
565 "Could not create statistics. Exiting.\n");
566 GNUNET_free(keyfile);
571 if (GNUNET_OK != GNUNET_DISK_file_test (HOSTKEY_FILE))
573 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Hostkey `%s' missing. Exiting.\n",
575 GNUNET_free(keyfile);
580 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (keyfile))
582 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
583 "Could not create a directory for hostkey `%s'. Exiting.\n", keyfile);
584 GNUNET_free(keyfile);
589 if (GNUNET_OK != GNUNET_DISK_file_copy (HOSTKEY_FILE, keyfile))
591 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
592 "Could not copy hostkey `%s' to destination `%s'. Exiting.\n",
593 HOSTKEY_FILE, keyfile);
594 GNUNET_free(keyfile);
599 max_connect_per_transport = (uint32_t) tneigh;
600 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
601 GNUNET_free(keyfile);
602 if (NULL == my_private_key)
604 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
605 "Could not access hostkey. Exiting.\n");
609 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
611 hello = GNUNET_HELLO_create (&my_identity.public_key, NULL, NULL, GNUNET_NO);
613 /* load plugins... */
614 setup_plugin_environment ();
616 GNUNET_assert(strlen (argv[0]) > strlen ("test_plugin_"));
617 plugin = strstr (argv[0], "test_plugin_");
618 sep = strrchr (argv[0], '.');
621 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Not a valid test name\n");
625 plugin += strlen ("test_plugin_");
629 /* Hack for WLAN: start a second helper */
630 if (0 == strcmp (plugin, "wlan"))
632 char * helper_argv[3];
633 helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
634 helper_argv[1] = (char *) "2";
635 helper_argv[2] = NULL;
636 suid_helper = GNUNET_HELPER_start (GNUNET_NO,
637 "gnunet-helper-transport-wlan-dummy", helper_argv,
638 &handle_helper_message, NULL, NULL );
642 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Loading transport plugin %s\n", plugin);
643 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
644 api = GNUNET_PLUGIN_load (libname, &env);
647 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
648 "Failed to load transport plugin for %s\n", plugin);
653 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL );
655 /* Check if all functions are implemented */
656 if (NULL == api->address_pretty_printer)
662 if (NULL == api->address_to_string)
668 GNUNET_assert(NULL != api->check_address);
669 if (NULL == api->check_address)
675 GNUNET_assert(NULL != api->disconnect_peer);
676 if (NULL == api->disconnect_peer)
682 GNUNET_assert(NULL != api->get_session);
683 if (NULL == api->get_session)
689 if (NULL == api->address_pretty_printer)
695 if (NULL == api->string_to_address)
705 * The main function for the test
707 * @param argc number of arguments from the command line
708 * @param argv command line arguments
709 * @return 0 ok, 1 on error
712 main (int argc, char * const *argv)
714 static struct GNUNET_GETOPT_CommandLineOption options[] = {
715 GNUNET_GETOPT_OPTION_END };
718 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
720 char * const argv_prog[] = { "test_plugin_transport", "-c",
721 "test_plugin_transport_data.conf", NULL };
722 GNUNET_log_setup ("test-plugin-transport", "WARNING", NULL );
723 ok = 1; /* set to fail */
726 == GNUNET_PROGRAM_run (3, argv_prog, "test-plugin-transport",
727 "testcase", options, &run, (void *) argv)) ? ok : 1;
728 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
732 /* end of test_plugin_transport.c */