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 GNUNET_SCHEDULER_TaskIdentifier timeout_endbadly;
97 static GNUNET_SCHEDULER_TaskIdentifier 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?
124 struct AddressWrapper
126 struct AddressWrapper *next;
128 struct AddressWrapper *prev;
141 struct AddressWrapper *w;
145 if (GNUNET_SCHEDULER_NO_TASK != timeout_endbadly)
147 GNUNET_SCHEDULER_cancel (timeout_endbadly);
148 timeout_endbadly = GNUNET_SCHEDULER_NO_TASK;
151 GNUNET_PLUGIN_unload (libname, api);
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 "Plugin did not remove address `%s'\n", w->addrstring);
158 GNUNET_CONTAINER_DLL_remove (head, tail, w);
160 GNUNET_free (w->addr);
161 GNUNET_free (w->addrstring);
167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 "Plugin did not remove %u addresses \n", c);
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 = GNUNET_SCHEDULER_NO_TASK;
193 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
195 GNUNET_SCHEDULER_cancel (timeout_wait);
196 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
199 if (pretty_printers_running > 0)
201 timeout_endbadly = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, &end_badly, &ok);
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,
225 "Plugin did not remove address `%s'\n", w->addrstring);
226 GNUNET_CONTAINER_DLL_remove (head, tail, w);
228 GNUNET_free (w->addr);
229 GNUNET_free (w->addrstring);
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 "Plugin did not remove %u addresses\n", c);
241 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
245 if (NULL != suid_helper)
247 GNUNET_HELPER_stop (suid_helper, GNUNET_NO);
256 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
258 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
259 if (0 == addresses_reported)
260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
261 "Plugin did not report any addresses, could not check address conversion functions\n");
269 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
271 GNUNET_SCHEDULER_cancel (timeout_wait);
272 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
274 if (GNUNET_SCHEDULER_NO_TASK != timeout_endbadly)
276 GNUNET_SCHEDULER_cancel (timeout_endbadly);
277 timeout_endbadly = GNUNET_SCHEDULER_NO_TASK;
279 timeout_endbadly = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
283 static struct GNUNET_TIME_Relative
284 env_receive (void *cls,
285 const struct GNUNET_PeerIdentity *peer,
286 const struct GNUNET_MessageHeader *message,
287 struct Session * session,
288 const char *sender_address,
289 uint16_t sender_address_len)
292 return GNUNET_TIME_relative_get_zero_();
296 static int got_reply;
300 * Take the given address and append it to the set of results sent back to
303 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
304 * @param buf text to transmit
307 address_pretty_printer_cb (void *cls, const char *buf)
311 got_reply = GNUNET_YES;
312 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
313 "Pretty address : `%s'\n", buf);
314 pretty_printers_running --;
318 if (GNUNET_NO == got_reply)
320 pretty_printers_running --;
329 env_notify_address (void *cls,
335 struct AddressWrapper *w;
336 struct AddressWrapper *wtmp;
340 if (GNUNET_YES == add_remove)
342 addresses_reported ++;
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "Adding address of length %u\n", addrlen);
346 for (wtmp = head; NULL != wtmp; wtmp = wtmp->next)
348 if ((addrlen == wtmp->addrlen) && (0 == memcmp (addr, wtmp->addr, addrlen)))
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 "Duplicate address notification .... \n");
356 w = GNUNET_new (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 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
363 "Testing: address_to_string \n");
364 w->addrstring = strdup (api->address_to_string (api, w->addr, w->addrlen));
365 if (NULL == w->addrstring)
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 "Plugin cannot convert address to string!\n");
375 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
376 "Plugin added address `%s'\n", w->addrstring);
377 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
378 "Testing address_to_string: OK\n");
381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
382 "Testing: string_to_address \n");
385 if ((GNUNET_OK != api->string_to_address (api, w->addrstring, strlen (w->addrstring)+1, &s2a, &s2a_len)) || (NULL == s2a))
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 "Plugin cannot convert string to address!\n");
395 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
396 "Plugin creates `%s' %u\n",api->address_to_string (api, s2a, s2a_len), s2a_len);
399 for (c1 = 0; c1 < s2a_len; c1++ )
400 fprintf (stderr, "%u == %u\n", ((char *) s2a)[c1], ((char *) w->addr)[c1]);
402 if (s2a_len != w->addrlen)
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 "Plugin creates different address length when converting address->string->address: %u != %u\n", w->addrlen, s2a_len);
407 else if (0 != memcmp (s2a, w->addr, s2a_len))
409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410 "Plugin creates different address length when converting back and forth %i!\n", memcmp (s2a, w->addr, s2a_len));
414 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
415 "Testing string_to_address: OK\n");
419 pretty_printers_running ++;
420 api->address_pretty_printer (api->cls, plugin, addr, addrlen,
421 GNUNET_YES, GNUNET_TIME_UNIT_MINUTES,
422 &address_pretty_printer_cb,
425 if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
429 "Plugin refuses added address!\n");
433 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
435 GNUNET_SCHEDULER_cancel (timeout_wait);
436 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
439 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
442 else if (GNUNET_NO == add_remove)
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Removing address of length %u\n", addrlen);
450 if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
461 "Plugin removes address never added!\n");
466 GNUNET_CONTAINER_DLL_remove (head, tail, w);
467 GNUNET_free (w->addr);
468 GNUNET_free (w->addrstring);
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
475 "Invalid operation: %u\n", add_remove);
482 static struct GNUNET_ATS_Information
483 env_get_address_type (void *cls,
484 const struct sockaddr *addr,
487 struct GNUNET_ATS_Information ats;
488 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
489 ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
494 static const struct GNUNET_MessageHeader *
497 return (const struct GNUNET_MessageHeader *) hello;
502 env_session_end (void *cls,
503 const struct GNUNET_PeerIdentity *peer,
504 struct Session * session)
510 env_update_metrics (void *cls,
511 const struct GNUNET_PeerIdentity *peer,
513 uint16_t address_len,
514 struct Session *session,
515 const struct GNUNET_ATS_Information *ats,
522 setup_plugin_environment ()
526 env.my_identity = &my_identity;
527 env.max_connections = max_connect_per_transport;
529 env.receive = &env_receive;
530 env.notify_address = &env_notify_address;
531 env.get_address_type = &env_get_address_type;
532 env.update_address_metrics = &env_update_metrics;
533 env.get_our_hello = &env_get_our_hello;
534 env.session_end = &env_session_end;
539 handle_helper_message (void *cls, void *client,
540 const struct GNUNET_MessageHeader *hdr)
550 * @param c configuration to use
553 run (void *cls, char *const *args, const char *cfgfile,
554 const struct GNUNET_CONFIGURATION_Handle *c)
556 char *const *argv = cls;
557 unsigned long long tneigh;
562 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
565 /* parse configuration */
566 if ( (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
570 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
571 "PEER", "PRIVATE_KEY",
574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
575 "Transport service is lacking key configuration settings. Exiting.\n");
579 if (NULL == (stats = GNUNET_STATISTICS_create ("transport", cfg)))
581 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
582 "Could not create statistics. Exiting.\n");
583 GNUNET_free (keyfile);
588 if (GNUNET_OK != GNUNET_DISK_file_test (HOSTKEY_FILE))
590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
591 "Hostkey `%s' missing. Exiting.\n",
593 GNUNET_free (keyfile);
598 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (keyfile))
600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
601 "Could not create a directory for hostkey `%s'. Exiting.\n",
603 GNUNET_free (keyfile);
608 if (GNUNET_OK != GNUNET_DISK_file_copy (HOSTKEY_FILE, keyfile))
610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
611 "Could not copy hostkey `%s' to destination `%s'. Exiting.\n",
612 HOSTKEY_FILE, keyfile);
613 GNUNET_free (keyfile);
619 max_connect_per_transport = (uint32_t) tneigh;
620 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
621 GNUNET_free (keyfile);
622 if (NULL == my_private_key)
624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
625 "Could not access hostkey. Exiting.\n");
629 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
631 hello = GNUNET_HELLO_create (&my_identity.public_key, NULL, NULL, GNUNET_NO);
633 /* load plugins... */
634 setup_plugin_environment ();
636 GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
637 plugin = strstr(argv[0],"test_plugin_");
638 sep = strrchr(argv[0],'.');
641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Not a valid test name\n");
645 plugin += strlen ("test_plugin_");
649 /* Hack for WLAN: start a second helper */
650 if (0 == strcmp (plugin, "wlan"))
652 char * helper_argv[3];
653 helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
654 helper_argv[1] = (char *) "2";
655 helper_argv[2] = NULL;
656 suid_helper = GNUNET_HELPER_start (GNUNET_NO,
657 "gnunet-helper-transport-wlan-dummy",
659 &handle_helper_message,
665 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Loading transport plugin %s\n", plugin);
666 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
667 api = GNUNET_PLUGIN_load (libname, &env);
670 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
671 "Failed to load transport plugin for %s\n", plugin);
676 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
678 /* Check if all functions are implemented */
679 if (NULL == api->address_pretty_printer)
685 if (NULL == api->address_to_string)
691 GNUNET_assert (NULL != api->check_address);
692 if (NULL == api->check_address)
698 GNUNET_assert (NULL != api->disconnect_peer);
699 if (NULL == api->disconnect_peer)
705 GNUNET_assert (NULL != api->get_session);
706 if (NULL == api->get_session)
712 if (NULL == api->address_pretty_printer)
718 if (NULL == api->string_to_address)
729 * The main function for the test
731 * @param argc number of arguments from the command line
732 * @param argv command line arguments
733 * @return 0 ok, 1 on error
736 main (int argc, char *const *argv)
738 static struct GNUNET_GETOPT_CommandLineOption options[] = {
739 GNUNET_GETOPT_OPTION_END
743 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
745 char *const argv_prog[] = {
746 "test_plugin_transport",
748 "test_plugin_transport_data.conf",
751 GNUNET_log_setup ("test-plugin-transport",
754 ok = 1; /* set to fail */
755 ret = (GNUNET_OK == GNUNET_PROGRAM_run (3,
757 "test-plugin-transport",
761 (void *) argv)) ? ok : 1;
762 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
766 /* end of test_plugin_transport.c */