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 {
124 struct AddressWrapper *next;
126 struct AddressWrapper *prev;
128 struct GNUNET_HELLO_Address *address;
132 struct GNUNET_SCHEDULER_Task *test_task;
139 struct AddressWrapper *w;
144 if (NULL != timeout_endbadly)
146 GNUNET_SCHEDULER_cancel(timeout_endbadly);
147 timeout_endbadly = NULL;
150 GNUNET_PLUGIN_unload(libname, api);
155 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
156 "Plugin did not remove address `%s'\n",
158 GNUNET_CONTAINER_DLL_remove(head, tail, w);
160 GNUNET_HELLO_address_free(w->address);
161 GNUNET_free(w->addrstring);
167 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses \n",
172 GNUNET_free(libname);
174 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
177 if (NULL != suid_helper)
179 GNUNET_HELPER_stop(suid_helper, GNUNET_NO);
188 struct AddressWrapper *w;
191 timeout_endbadly = NULL;
192 if (NULL != timeout_wait)
194 GNUNET_SCHEDULER_cancel(timeout_wait);
198 if (pretty_printers_running > 0)
200 timeout_endbadly = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
202 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
203 "Have pending calls to pretty_printer ... deferring shutdown\n");
209 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
210 "Test took too long to execute, timeout .... \n");
216 GNUNET_PLUGIN_unload(libname, api);
217 GNUNET_free(libname);
224 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove address `%s'\n",
226 GNUNET_CONTAINER_DLL_remove(head, tail, w);
228 GNUNET_HELLO_address_free(w->address);
229 if (NULL != w->test_task)
230 GNUNET_SCHEDULER_cancel(w->test_task);
231 GNUNET_free(w->addrstring);
237 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses\n",
243 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
247 if (NULL != suid_helper)
249 GNUNET_HELPER_stop(suid_helper, GNUNET_NO);
260 if (0 == addresses_reported)
261 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
262 "Plugin did not report any addresses, could not check address conversion functions\n");
270 if (NULL != timeout_wait)
272 GNUNET_SCHEDULER_cancel(timeout_wait);
275 if (NULL != timeout_endbadly)
277 GNUNET_SCHEDULER_cancel(timeout_endbadly);
278 timeout_endbadly = NULL;
280 timeout_endbadly = GNUNET_SCHEDULER_add_now(&end_badly, NULL);
284 static struct GNUNET_TIME_Relative
285 env_receive(void *cls,
286 const struct GNUNET_HELLO_Address *address,
287 struct GNUNET_ATS_Session *session,
288 const struct GNUNET_MessageHeader *message)
291 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
302 * @param address the address to print
303 * @param res result code
306 address_pretty_printer_cb(void *cls, const char *address, int res)
310 got_reply = GNUNET_YES;
311 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Pretty address : `%s'\n", address);
312 pretty_printers_running--;
316 if (GNUNET_NO == got_reply)
318 pretty_printers_running--;
327 test_addr_string(void *cls)
329 struct AddressWrapper *w = cls;
335 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
336 "Testing: address_to_string \n");
337 w->addrstring = GNUNET_strdup(api->address_to_string(api,
339 w->address->address_length));
340 if (NULL == w->addrstring)
343 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
344 "Plugin cannot convert address to string!\n");
348 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
349 "Plugin added address `%s'\n",
351 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
352 "Testing address_to_string: OK\n");
353 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
354 "Testing: string_to_address \n");
358 api->string_to_address(api, w->addrstring,
359 strlen(w->addrstring) + 1,
364 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
365 "Plugin cannot convert string to address!\n");
371 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
372 "Plugin creates `%s' %u\n",api->address_to_string (api, s2a, s2a_len), s2a_len);
375 for (c1 = 0; c1 < s2a_len; c1++ )
376 fprintf (stderr, "%u == %u\n", ((char *) s2a)[c1], ((char *) w->addr)[c1]);
378 if (s2a_len != w->address->address_length)
380 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
381 "Plugin creates different address length when converting address->string->address: %u != %u\n",
382 (unsigned int)w->address->address_length,
383 (unsigned int)s2a_len);
385 else if (0 != memcmp(s2a, w->address->address, s2a_len))
387 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
388 "Plugin creates different address length when converting back and forth %i!\n",
395 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
396 "Testing string_to_address: OK\n");
400 pretty_printers_running++;
401 api->address_pretty_printer(api->cls,
402 w->address->transport_name,
404 w->address->address_length,
406 GNUNET_TIME_UNIT_MINUTES,
407 &address_pretty_printer_cb, w);
410 api->check_address(api->cls,
412 w->address->address_length))
415 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
416 "Plugin refuses added address!\n");
420 if (NULL != timeout_wait)
422 GNUNET_SCHEDULER_cancel(timeout_wait);
425 timeout_wait = GNUNET_SCHEDULER_add_delayed(WAIT, &wait_end, NULL);
430 env_notify_address(void *cls,
432 const struct GNUNET_HELLO_Address *address)
434 struct AddressWrapper *w;
435 struct AddressWrapper *wtmp;
437 if (GNUNET_YES == add_remove)
439 addresses_reported++;
440 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
441 "Adding address of length %u\n",
442 (unsigned int)address->address_length);
444 for (wtmp = head; NULL != wtmp; wtmp = wtmp->next)
446 if ((address->address_length == wtmp->address->address_length) &&
447 (0 == memcmp(address->address, wtmp->address->address, address->address_length)))
449 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
450 "Duplicate address notification .... \n");
455 w = GNUNET_new(struct AddressWrapper);
456 w->address = GNUNET_HELLO_address_copy(address);
457 GNUNET_CONTAINER_DLL_insert(head, tail, w);
458 got_reply = GNUNET_NO;
459 w->test_task = GNUNET_SCHEDULER_add_now(&test_addr_string,
464 if (GNUNET_NO == add_remove)
466 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
467 "Removing address of length %u\n",
468 (unsigned int)address->address_length);
472 if ((address->address_length == w->address->address_length) &&
473 (0 == memcmp(w->address->address, address->address, address->address_length)))
483 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
484 "Plugin removes address never added!\n");
489 GNUNET_CONTAINER_DLL_remove(head, tail, w);
490 GNUNET_HELLO_address_free(w->address);
491 GNUNET_free(w->addrstring);
496 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
497 "Invalid operation: %u\n",
503 static enum GNUNET_NetworkType
504 env_get_address_type(void *cls,
505 const struct sockaddr *addr,
508 return GNUNET_NT_LOOPBACK;
512 static const struct GNUNET_MessageHeader *
515 return (const struct GNUNET_MessageHeader *)hello;
520 env_session_end(void *cls,
521 const struct GNUNET_HELLO_Address *address,
522 struct GNUNET_ATS_Session *session)
528 env_update_distance(void *cls,
529 const struct GNUNET_HELLO_Address *address,
536 setup_plugin_environment()
540 env.my_identity = &my_identity;
541 env.max_connections = max_connect_per_transport;
543 env.receive = &env_receive;
544 env.notify_address = &env_notify_address;
545 env.get_address_type = &env_get_address_type;
546 env.update_address_distance = &env_update_distance;
547 env.get_our_hello = &env_get_our_hello;
548 env.session_end = &env_session_end;
553 handle_helper_message(void *cls,
554 const struct GNUNET_MessageHeader *hdr)
564 * @param c configuration to use
570 const struct GNUNET_CONFIGURATION_Handle *c)
572 char * const *argv = cls;
573 unsigned long long tneigh;
578 timeout_endbadly = GNUNET_SCHEDULER_add_delayed(TIMEOUT,
582 /* parse configuration */
584 GNUNET_CONFIGURATION_get_value_number(c,
589 GNUNET_CONFIGURATION_get_value_filename(c,
594 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
595 "Transport service is lacking key configuration settings. Exiting.\n");
599 if (NULL == (stats = GNUNET_STATISTICS_create("transport",
602 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
603 "Could not create statistics. Exiting.\n");
604 GNUNET_free(keyfile);
609 if (GNUNET_OK != GNUNET_DISK_file_test(HOSTKEY_FILE))
611 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
612 "Hostkey `%s' missing. Exiting.\n",
614 GNUNET_free(keyfile);
620 GNUNET_DISK_directory_create_for_file(keyfile))
622 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
623 "Could not create a directory for hostkey `%s'. Exiting.\n",
625 GNUNET_free(keyfile);
631 GNUNET_DISK_file_copy(HOSTKEY_FILE,
634 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
635 "Could not copy hostkey `%s' to destination `%s'. Exiting.\n",
638 GNUNET_free(keyfile);
643 max_connect_per_transport = (uint32_t)tneigh;
644 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_file(keyfile);
645 GNUNET_free(keyfile);
646 if (NULL == my_private_key)
648 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
649 "Could not access hostkey. Exiting.\n");
653 GNUNET_CRYPTO_eddsa_key_get_public(my_private_key, &my_identity.public_key);
655 hello = GNUNET_HELLO_create(&my_identity.public_key, NULL, NULL, GNUNET_NO);
657 /* load plugins... */
658 setup_plugin_environment();
660 GNUNET_assert(strlen(argv[0]) > strlen("test_plugin_"));
661 plugin = strstr(argv[0], "test_plugin_");
662 sep = strrchr(argv[0], '.');
665 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Not a valid test name\n");
669 plugin += strlen("test_plugin_");
673 /* Hack for WLAN: start a second helper */
674 if (0 == strcmp(plugin, "wlan"))
676 char * helper_argv[3];
677 helper_argv[0] = (char *)"gnunet-helper-transport-wlan-dummy";
678 helper_argv[1] = (char *)"2";
679 helper_argv[2] = NULL;
680 suid_helper = GNUNET_HELPER_start(GNUNET_NO,
681 "gnunet-helper-transport-wlan-dummy", helper_argv,
682 &handle_helper_message, NULL, NULL);
686 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Loading transport plugin %s\n", plugin);
687 GNUNET_asprintf(&libname, "libgnunet_plugin_transport_%s", plugin);
688 api = GNUNET_PLUGIN_load(libname, &env);
691 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
692 "Failed to load transport plugin for %s\n", plugin);
697 timeout_wait = GNUNET_SCHEDULER_add_delayed(WAIT, &wait_end, NULL);
699 /* Check if all functions are implemented */
700 if (NULL == api->address_pretty_printer)
706 if (NULL == api->address_to_string)
712 GNUNET_assert(NULL != api->check_address);
713 if (NULL == api->check_address)
719 GNUNET_assert(NULL != api->disconnect_peer);
720 if (NULL == api->disconnect_peer)
726 GNUNET_assert(NULL != api->get_session);
727 if (NULL == api->get_session)
733 if (NULL == api->address_pretty_printer)
739 if (NULL == api->string_to_address)
749 * The main function for the test
751 * @param argc number of arguments from the command line
752 * @param argv command line arguments
753 * @return 0 ok, 1 on error
759 static struct GNUNET_GETOPT_CommandLineOption options[] = {
760 GNUNET_GETOPT_OPTION_END
763 char * const argv_prog[] = {
764 "test_plugin_transport",
766 "test_plugin_transport_data.conf",
770 GNUNET_log_setup("test-plugin-transport",
773 GNUNET_DISK_purge_cfg_dir("test_plugin_transport_data.conf",
775 ok = 1; /* set to fail */
778 == GNUNET_PROGRAM_run(3, argv_prog, "test-plugin-transport",
779 "testcase", options, &run, (void *)argv)) ? ok : 1;
780 GNUNET_DISK_purge_cfg_dir("test_plugin_transport_data.conf",
785 /* end of test_plugin_transport.c */