2 This file is part of GNUnet.
3 Copyright (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;
133 struct GNUNET_SCHEDULER_Task *test_task;
141 struct AddressWrapper *w;
145 if (NULL != timeout_endbadly)
147 GNUNET_SCHEDULER_cancel (timeout_endbadly);
148 timeout_endbadly = NULL;
151 GNUNET_PLUGIN_unload (libname, api);
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 "Plugin did not remove address `%s'\n",
159 GNUNET_CONTAINER_DLL_remove(head, tail, w);
161 GNUNET_HELLO_address_free(w->address);
162 GNUNET_free(w->addrstring);
168 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses \n",
173 GNUNET_free(libname);
175 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
178 if (NULL != suid_helper)
180 GNUNET_HELPER_stop (suid_helper, GNUNET_NO);
187 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 struct AddressWrapper *w;
192 timeout_endbadly = NULL;
193 if (NULL != timeout_wait)
195 GNUNET_SCHEDULER_cancel (timeout_wait);
199 if (pretty_printers_running > 0)
201 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
203 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
204 "Have pending calls to pretty_printer ... deferring shutdown\n");
210 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
211 "Test took too long to execute, timeout .... \n");
217 GNUNET_PLUGIN_unload (libname, api);
218 GNUNET_free(libname);
225 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove address `%s'\n",
227 GNUNET_CONTAINER_DLL_remove(head, tail, w);
229 GNUNET_HELLO_address_free(w->address);
230 if (NULL != w->test_task)
231 GNUNET_SCHEDULER_cancel (w->test_task);
232 GNUNET_free(w->addrstring);
238 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses\n",
244 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
248 if (NULL != suid_helper)
250 GNUNET_HELPER_stop (suid_helper, GNUNET_NO);
258 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 if (0 == addresses_reported)
262 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
263 "Plugin did not report any addresses, could not check address conversion functions\n");
271 if (NULL != timeout_wait)
273 GNUNET_SCHEDULER_cancel (timeout_wait);
276 if (NULL != timeout_endbadly)
278 GNUNET_SCHEDULER_cancel (timeout_endbadly);
279 timeout_endbadly = NULL;
281 timeout_endbadly = GNUNET_SCHEDULER_add_now (&end_badly, NULL );
285 static struct GNUNET_TIME_Relative
286 env_receive (void *cls,
287 const struct GNUNET_HELLO_Address *address,
288 struct Session *session,
289 const struct GNUNET_MessageHeader *message)
292 return GNUNET_TIME_relative_get_zero_ ();
295 static int got_reply;
299 * Take the given address and append it to the set of results sent back to
303 * @param address the address to print
304 * @param res result code
307 address_pretty_printer_cb (void *cls, const char *address, int res)
311 got_reply = GNUNET_YES;
312 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Pretty address : `%s'\n", address);
313 pretty_printers_running--;
317 if (GNUNET_NO == got_reply)
319 pretty_printers_running--;
328 test_addr_string (void *cls,
329 const struct GNUNET_SCHEDULER_TaskContext *tc)
331 struct AddressWrapper *w = cls;
337 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
338 "Testing: address_to_string \n");
339 w->addrstring = GNUNET_strdup (api->address_to_string (api,
341 w->address->address_length));
342 if (NULL == w->addrstring)
345 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
346 "Plugin cannot convert address to string!\n");
350 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
351 "Plugin added address `%s'\n",
353 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
354 "Testing address_to_string: OK\n");
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356 "Testing: string_to_address \n");
360 api->string_to_address (api, w->addrstring,
361 strlen (w->addrstring) + 1,
366 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
367 "Plugin cannot convert string to address!\n");
373 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
374 "Plugin creates `%s' %u\n",api->address_to_string (api, s2a, s2a_len), s2a_len);
377 for (c1 = 0; c1 < s2a_len; c1++ )
378 fprintf (stderr, "%u == %u\n", ((char *) s2a)[c1], ((char *) w->addr)[c1]);
380 if (s2a_len != w->address->address_length)
382 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
383 "Plugin creates different address length when converting address->string->address: %u != %u\n",
384 w->address->address_length, s2a_len);
386 else if (0 != memcmp (s2a, w->address->address, s2a_len))
388 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
389 "Plugin creates different address length when converting back and forth %i!\n",
390 memcmp (s2a, w->address->address, s2a_len));
394 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
395 "Testing string_to_address: OK\n");
399 pretty_printers_running++;
400 api->address_pretty_printer (api->cls,
401 w->address->transport_name,
403 w->address->address_length,
405 GNUNET_TIME_UNIT_MINUTES,
406 &address_pretty_printer_cb, w);
409 api->check_address (api->cls, w->address->address, w->address->address_length))
412 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin refuses added address!\n");
416 if (NULL != timeout_wait)
418 GNUNET_SCHEDULER_cancel (timeout_wait);
421 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
426 env_notify_address (void *cls,
428 const struct GNUNET_HELLO_Address *address)
430 struct AddressWrapper *w;
431 struct AddressWrapper *wtmp;
433 if (GNUNET_YES == add_remove)
435 addresses_reported++;
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Adding address of length %u\n",
438 address->address_length);
440 for (wtmp = head; NULL != wtmp; wtmp = wtmp->next)
442 if ((address->address_length == wtmp->address->address_length) &&
443 (0 == memcmp (address->address, wtmp->address->address, address->address_length)))
445 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
446 "Duplicate address notification .... \n");
451 w = GNUNET_new (struct AddressWrapper);
452 w->address = GNUNET_HELLO_address_copy (address);
453 GNUNET_CONTAINER_DLL_insert (head, tail, w);
454 got_reply = GNUNET_NO;
455 w->test_task = GNUNET_SCHEDULER_add_now (&test_addr_string,
460 if (GNUNET_NO == add_remove)
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Removing address of length %u\n",
464 address->address_length);
468 if ((address->address_length == w->address->address_length) &&
469 (0 == memcmp (w->address->address, address->address, address->address_length)))
479 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
480 "Plugin removes address never added!\n");
485 GNUNET_CONTAINER_DLL_remove(head, tail, w);
486 GNUNET_HELLO_address_free (w->address);
487 GNUNET_free(w->addrstring);
492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
493 "Invalid operation: %u\n",
499 static enum GNUNET_ATS_Network_Type
500 env_get_address_type (void *cls,
501 const struct sockaddr *addr,
504 return GNUNET_ATS_NET_LOOPBACK;
508 static const struct GNUNET_MessageHeader *
511 return (const struct GNUNET_MessageHeader *) hello;
516 env_session_end (void *cls,
517 const struct GNUNET_HELLO_Address *address,
518 struct Session *session)
525 env_update_metrics (void *cls,
526 const struct GNUNET_HELLO_Address *address,
527 struct Session *session,
528 const struct GNUNET_ATS_Information *ats,
535 setup_plugin_environment ()
539 env.my_identity = &my_identity;
540 env.max_connections = max_connect_per_transport;
542 env.receive = &env_receive;
543 env.notify_address = &env_notify_address;
544 env.get_address_type = &env_get_address_type;
545 env.update_address_metrics = &env_update_metrics;
546 env.get_our_hello = &env_get_our_hello;
547 env.session_end = &env_session_end;
552 handle_helper_message (void *cls, void *client,
553 const struct GNUNET_MessageHeader *hdr)
563 * @param c configuration to use
566 run (void *cls, char * const *args, const char *cfgfile,
567 const struct GNUNET_CONFIGURATION_Handle *c)
569 char * const *argv = cls;
570 unsigned long long tneigh;
575 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
578 /* parse configuration */
580 != GNUNET_CONFIGURATION_get_value_number (c, "TRANSPORT",
581 "NEIGHBOUR_LIMIT", &tneigh))
583 != GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
586 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
587 "Transport service is lacking key configuration settings. Exiting.\n");
591 if (NULL == (stats = GNUNET_STATISTICS_create ("transport", cfg)))
593 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
594 "Could not create statistics. Exiting.\n");
595 GNUNET_free(keyfile);
600 if (GNUNET_OK != GNUNET_DISK_file_test (HOSTKEY_FILE))
602 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Hostkey `%s' missing. Exiting.\n",
604 GNUNET_free(keyfile);
609 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (keyfile))
611 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
612 "Could not create a directory for hostkey `%s'. Exiting.\n", keyfile);
613 GNUNET_free(keyfile);
618 if (GNUNET_OK != GNUNET_DISK_file_copy (HOSTKEY_FILE, keyfile))
620 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
621 "Could not copy hostkey `%s' to destination `%s'. Exiting.\n",
622 HOSTKEY_FILE, keyfile);
623 GNUNET_free(keyfile);
628 max_connect_per_transport = (uint32_t) tneigh;
629 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
630 GNUNET_free(keyfile);
631 if (NULL == my_private_key)
633 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
634 "Could not access hostkey. Exiting.\n");
638 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
640 hello = GNUNET_HELLO_create (&my_identity.public_key, NULL, NULL, GNUNET_NO);
642 /* load plugins... */
643 setup_plugin_environment ();
645 GNUNET_assert(strlen (argv[0]) > strlen ("test_plugin_"));
646 plugin = strstr (argv[0], "test_plugin_");
647 sep = strrchr (argv[0], '.');
650 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Not a valid test name\n");
654 plugin += strlen ("test_plugin_");
658 /* Hack for WLAN: start a second helper */
659 if (0 == strcmp (plugin, "wlan"))
661 char * helper_argv[3];
662 helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
663 helper_argv[1] = (char *) "2";
664 helper_argv[2] = NULL;
665 suid_helper = GNUNET_HELPER_start (GNUNET_NO,
666 "gnunet-helper-transport-wlan-dummy", helper_argv,
667 &handle_helper_message, NULL, NULL );
671 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Loading transport plugin %s\n", plugin);
672 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
673 api = GNUNET_PLUGIN_load (libname, &env);
676 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
677 "Failed to load transport plugin for %s\n", plugin);
682 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL );
684 /* Check if all functions are implemented */
685 if (NULL == api->address_pretty_printer)
691 if (NULL == api->address_to_string)
697 GNUNET_assert(NULL != api->check_address);
698 if (NULL == api->check_address)
704 GNUNET_assert(NULL != api->disconnect_peer);
705 if (NULL == api->disconnect_peer)
711 GNUNET_assert(NULL != api->get_session);
712 if (NULL == api->get_session)
718 if (NULL == api->address_pretty_printer)
724 if (NULL == api->string_to_address)
734 * The main function for the test
736 * @param argc number of arguments from the command line
737 * @param argv command line arguments
738 * @return 0 ok, 1 on error
741 main (int argc, char * const *argv)
743 static struct GNUNET_GETOPT_CommandLineOption options[] = {
744 GNUNET_GETOPT_OPTION_END };
747 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
749 char * const argv_prog[] = { "test_plugin_transport", "-c",
750 "test_plugin_transport_data.conf", NULL };
751 GNUNET_log_setup ("test-plugin-transport", "WARNING", NULL );
752 ok = 1; /* set to fail */
755 == GNUNET_PROGRAM_run (3, argv_prog, "test-plugin-transport",
756 "testcase", options, &run, (void *) argv)) ? ok : 1;
757 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
761 /* end of test_plugin_transport.c */