2 This file is part of GNUnet.
3 Copyright (C) 2009, 2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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;
140 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,
169 "Plugin did not remove %u addresses \n",
174 GNUNET_free (libname);
176 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
179 if (NULL != suid_helper)
181 GNUNET_HELPER_stop (suid_helper, GNUNET_NO);
188 end_badly (void *cls)
190 struct AddressWrapper *w;
193 timeout_endbadly = NULL;
194 if (NULL != timeout_wait)
196 GNUNET_SCHEDULER_cancel (timeout_wait);
200 if (pretty_printers_running > 0)
202 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205 "Have pending calls to pretty_printer ... deferring shutdown\n");
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 "Test took too long to execute, timeout .... \n");
218 GNUNET_PLUGIN_unload (libname, api);
219 GNUNET_free (libname);
226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove address `%s'\n",
228 GNUNET_CONTAINER_DLL_remove (head, tail, w);
230 GNUNET_HELLO_address_free (w->address);
231 if (NULL != w->test_task)
232 GNUNET_SCHEDULER_cancel (w->test_task);
233 GNUNET_free (w->addrstring);
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses\n",
245 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
249 if (NULL != suid_helper)
251 GNUNET_HELPER_stop (suid_helper, GNUNET_NO);
263 if (0 == addresses_reported)
264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
265 "Plugin did not report any addresses, could not check address conversion functions\n");
273 if (NULL != timeout_wait)
275 GNUNET_SCHEDULER_cancel (timeout_wait);
278 if (NULL != timeout_endbadly)
280 GNUNET_SCHEDULER_cancel (timeout_endbadly);
281 timeout_endbadly = NULL;
283 timeout_endbadly = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
287 static struct GNUNET_TIME_Relative
288 env_receive (void *cls,
289 const struct GNUNET_HELLO_Address *address,
290 struct GNUNET_ATS_Session *session,
291 const struct GNUNET_MessageHeader *message)
294 return GNUNET_TIME_relative_get_zero_ ();
298 static int got_reply;
302 * Take the given address and append it to the set of results sent back to
306 * @param address the address to print
307 * @param res result code
310 address_pretty_printer_cb (void *cls, const char *address, int res)
314 got_reply = GNUNET_YES;
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Pretty address : `%s'\n", address);
316 pretty_printers_running--;
320 if (GNUNET_NO == got_reply)
322 pretty_printers_running--;
331 test_addr_string (void *cls)
333 struct AddressWrapper *w = cls;
339 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
340 "Testing: address_to_string \n");
341 w->addrstring = GNUNET_strdup (api->address_to_string (api,
345 if (NULL == w->addrstring)
348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349 "Plugin cannot convert address to string!\n");
353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354 "Plugin added address `%s'\n",
356 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
357 "Testing address_to_string: OK\n");
358 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
359 "Testing: string_to_address \n");
363 api->string_to_address (api, w->addrstring,
364 strlen (w->addrstring) + 1,
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 "Plugin cannot convert string to address!\n");
376 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
377 "Plugin creates `%s' %u\n",api->address_to_string (api, s2a, s2a_len), s2a_len);
380 for (c1 = 0; c1 < s2a_len; c1++ )
381 fprintf (stderr, "%u == %u\n", ((char *) s2a)[c1], ((char *) w->addr)[c1]);
382 */if (s2a_len != w->address->address_length)
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 "Plugin creates different address length when converting address->string->address: %u != %u\n",
386 (unsigned int) w->address->address_length,
387 (unsigned int) s2a_len);
389 else if (0 != memcmp (s2a, w->address->address, s2a_len))
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 "Plugin creates different address length when converting back and forth %i!\n",
399 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
400 "Testing string_to_address: OK\n");
404 pretty_printers_running++;
405 api->address_pretty_printer (api->cls,
406 w->address->transport_name,
408 w->address->address_length,
410 GNUNET_TIME_UNIT_MINUTES,
411 &address_pretty_printer_cb, w);
414 api->check_address (api->cls,
416 w->address->address_length))
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 "Plugin refuses added address!\n");
424 if (NULL != timeout_wait)
426 GNUNET_SCHEDULER_cancel (timeout_wait);
429 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
434 env_notify_address (void *cls,
436 const struct GNUNET_HELLO_Address *address)
438 struct AddressWrapper *w;
439 struct AddressWrapper *wtmp;
441 if (GNUNET_YES == add_remove)
443 addresses_reported++;
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Adding address of length %u\n",
446 (unsigned int) address->address_length);
448 for (wtmp = head; NULL != wtmp; wtmp = wtmp->next)
450 if ((address->address_length == wtmp->address->address_length) &&
451 (0 == memcmp (address->address, wtmp->address->address,
452 address->address_length)))
454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
455 "Duplicate address notification .... \n");
460 w = GNUNET_new (struct AddressWrapper);
461 w->address = GNUNET_HELLO_address_copy (address);
462 GNUNET_CONTAINER_DLL_insert (head, tail, w);
463 got_reply = GNUNET_NO;
464 w->test_task = GNUNET_SCHEDULER_add_now (&test_addr_string,
469 if (GNUNET_NO == add_remove)
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Removing address of length %u\n",
473 (unsigned int) address->address_length);
477 if ((address->address_length == w->address->address_length) &&
478 (0 == memcmp (w->address->address, address->address,
479 address->address_length)))
489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
490 "Plugin removes address never added!\n");
495 GNUNET_CONTAINER_DLL_remove (head, tail, w);
496 GNUNET_HELLO_address_free (w->address);
497 GNUNET_free (w->addrstring);
502 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
503 "Invalid operation: %u\n",
509 static enum GNUNET_NetworkType
510 env_get_address_type (void *cls,
511 const struct sockaddr *addr,
514 return GNUNET_NT_LOOPBACK;
518 static const struct GNUNET_MessageHeader *
521 return (const struct GNUNET_MessageHeader *) hello;
526 env_session_end (void *cls,
527 const struct GNUNET_HELLO_Address *address,
528 struct GNUNET_ATS_Session *session)
534 env_update_distance (void *cls,
535 const struct GNUNET_HELLO_Address *address,
542 setup_plugin_environment ()
546 env.my_identity = &my_identity;
547 env.max_connections = max_connect_per_transport;
549 env.receive = &env_receive;
550 env.notify_address = &env_notify_address;
551 env.get_address_type = &env_get_address_type;
552 env.update_address_distance = &env_update_distance;
553 env.get_our_hello = &env_get_our_hello;
554 env.session_end = &env_session_end;
559 handle_helper_message (void *cls,
560 const struct GNUNET_MessageHeader *hdr)
570 * @param c configuration to use
576 const struct GNUNET_CONFIGURATION_Handle *c)
578 char *const *argv = cls;
579 unsigned long long tneigh;
584 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
588 /* parse configuration */
590 GNUNET_CONFIGURATION_get_value_number (c,
595 GNUNET_CONFIGURATION_get_value_filename (c,
600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
601 "Transport service is lacking key configuration settings. Exiting.\n");
605 if (NULL == (stats = GNUNET_STATISTICS_create ("transport",
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "Could not create statistics. Exiting.\n");
610 GNUNET_free (keyfile);
615 if (GNUNET_OK != GNUNET_DISK_file_test (HOSTKEY_FILE))
617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
618 "Hostkey `%s' missing. Exiting.\n",
620 GNUNET_free (keyfile);
626 GNUNET_DISK_directory_create_for_file (keyfile))
628 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
629 "Could not create a directory for hostkey `%s'. Exiting.\n",
631 GNUNET_free (keyfile);
637 GNUNET_DISK_file_copy (HOSTKEY_FILE,
640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
641 "Could not copy hostkey `%s' to destination `%s'. Exiting.\n",
644 GNUNET_free (keyfile);
649 max_connect_per_transport = (uint32_t) tneigh;
650 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
651 GNUNET_free (keyfile);
652 if (NULL == my_private_key)
654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
655 "Could not access hostkey. Exiting.\n");
659 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
661 hello = GNUNET_HELLO_create (&my_identity.public_key, NULL, NULL, GNUNET_NO);
663 /* load plugins... */
664 setup_plugin_environment ();
666 GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
667 plugin = strstr (argv[0], "test_plugin_");
668 sep = strrchr (argv[0], '.');
671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Not a valid test name\n");
675 plugin += strlen ("test_plugin_");
679 /* Hack for WLAN: start a second helper */
680 if (0 == strcmp (plugin, "wlan"))
682 char *helper_argv[3];
683 helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
684 helper_argv[1] = (char *) "2";
685 helper_argv[2] = NULL;
686 suid_helper = GNUNET_HELPER_start (GNUNET_NO,
687 "gnunet-helper-transport-wlan-dummy",
689 &handle_helper_message, NULL, NULL);
693 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loading transport plugin %s\n", plugin);
694 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
695 api = GNUNET_PLUGIN_load (libname, &env);
698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
699 "Failed to load transport plugin for %s\n", plugin);
704 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
706 /* Check if all functions are implemented */
707 if (NULL == api->address_pretty_printer)
713 if (NULL == api->address_to_string)
719 GNUNET_assert (NULL != api->check_address);
720 if (NULL == api->check_address)
726 GNUNET_assert (NULL != api->disconnect_peer);
727 if (NULL == api->disconnect_peer)
733 GNUNET_assert (NULL != api->get_session);
734 if (NULL == api->get_session)
740 if (NULL == api->address_pretty_printer)
746 if (NULL == api->string_to_address)
756 * The main function for the test
758 * @param argc number of arguments from the command line
759 * @param argv command line arguments
760 * @return 0 ok, 1 on error
766 static struct GNUNET_GETOPT_CommandLineOption options[] = {
767 GNUNET_GETOPT_OPTION_END
770 char *const argv_prog[] = {
771 "test_plugin_transport",
773 "test_plugin_transport_data.conf",
777 GNUNET_log_setup ("test-plugin-transport",
780 GNUNET_DISK_purge_cfg_dir ("test_plugin_transport_data.conf",
782 ok = 1; /* set to fail */
785 == GNUNET_PROGRAM_run (3, argv_prog, "test-plugin-transport",
786 "testcase", options, &run, (void *) argv)) ? ok : 1;
787 GNUNET_DISK_purge_cfg_dir ("test_plugin_transport_data.conf",
793 /* end of test_plugin_transport.c */