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_common.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_transport_plugin.h"
38 #include "transport.h"
41 * How long until we give up on transmitting the message?
43 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
49 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
54 static struct GNUNET_PeerIdentity my_identity;
59 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
64 const struct GNUNET_CONFIGURATION_Handle *cfg;
69 struct GNUNET_STATISTICS_Handle *stats;
74 struct GNUNET_HELLO_Message *hello;
77 * Number of neighbours we'd like to have.
79 static uint32_t max_connect_per_transport;
82 * Environment for this plugin.
84 struct GNUNET_TRANSPORT_PluginEnvironment env;
87 *handle for the api provided by this plugin
89 struct GNUNET_TRANSPORT_PluginFunctions *api;
94 struct GNUNET_HELPER_Handle *suid_helper;
99 static GNUNET_SCHEDULER_TaskIdentifier timeout_endbadly;
104 static GNUNET_SCHEDULER_TaskIdentifier timeout_wait;
109 static char *libname;
112 * Plugin addresses head
114 struct AddressWrapper *head;
117 * Plugin addresses tail
119 struct AddressWrapper *tail;
121 unsigned int addresses_reported;
123 unsigned int pretty_printers_running;
126 * Did the test pass or fail?
131 struct AddressWrapper
133 struct AddressWrapper *next;
135 struct AddressWrapper *prev;
148 struct AddressWrapper *w;
152 if (GNUNET_SCHEDULER_NO_TASK != timeout_endbadly)
154 GNUNET_SCHEDULER_cancel (timeout_endbadly);
155 timeout_endbadly = GNUNET_SCHEDULER_NO_TASK;
158 GNUNET_PLUGIN_unload (libname, api);
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164 _("Plugin did not remove address `%s' \n"), w->addrstring);
165 GNUNET_CONTAINER_DLL_remove (head, tail, w);
167 GNUNET_free (w->addr);
168 GNUNET_free (w->addrstring);
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 _("Plugin did not remove %u addresses \n"), c);
180 GNUNET_free (libname);
182 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
185 if (NULL != suid_helper)
187 GNUNET_HELPER_stop (suid_helper);
194 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 struct AddressWrapper *w;
198 timeout_endbadly = GNUNET_SCHEDULER_NO_TASK;
199 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
201 GNUNET_SCHEDULER_cancel (timeout_wait);
202 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
205 if (pretty_printers_running > 0)
207 timeout_endbadly = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, &end_badly, NULL);
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209 _("Have pending calls to pretty_printer ... deferring shutdown\n"));
215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216 _("Test took too long to execute, timeout .... \n"));
222 GNUNET_PLUGIN_unload (libname, api);
223 GNUNET_free (libname);
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 _("Plugin did not remove address `%s' \n"), w->addrstring);
232 GNUNET_CONTAINER_DLL_remove (head, tail, w);
234 GNUNET_free (w->addr);
235 GNUNET_free (w->addrstring);
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 _("Plugin did not remove %u addresses \n"), c);
247 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
251 if (NULL != suid_helper)
253 GNUNET_HELPER_stop (suid_helper);
262 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
264 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
265 if (0 == addresses_reported)
266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
267 _("Plugin did not report any addresses, could not check address conversion functions\n"));
275 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
277 GNUNET_SCHEDULER_cancel (timeout_wait);
278 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
280 if (GNUNET_SCHEDULER_NO_TASK != timeout_endbadly)
282 GNUNET_SCHEDULER_cancel (timeout_endbadly);
283 timeout_endbadly = GNUNET_SCHEDULER_NO_TASK;
285 timeout_endbadly = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
289 static struct GNUNET_TIME_Relative
290 env_receive (void *cls,
291 const struct GNUNET_PeerIdentity *peer,
292 const struct GNUNET_MessageHeader *message,
293 const struct GNUNET_ATS_Information *ats,
295 struct Session * session,
296 const char *sender_address,
297 uint16_t sender_address_len)
300 return GNUNET_TIME_relative_get_zero_();
304 static int got_reply;
308 * Take the given address and append it to the set of results sent back to
311 * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
312 * @param buf text to transmit
315 address_pretty_printer_cb (void *cls, const char *buf)
319 got_reply = GNUNET_YES;
320 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
321 "Pretty address : `%s'\n", buf);
322 pretty_printers_running --;
326 if (GNUNET_NO == got_reply)
328 pretty_printers_running --;
337 env_notify_address (void *cls,
343 struct AddressWrapper *w;
348 if (GNUNET_YES == add_remove)
350 addresses_reported ++;
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 _("Adding address of length %u\n"), addrlen);
354 w = GNUNET_malloc (sizeof (struct AddressWrapper));
355 w->addr = GNUNET_malloc (addrlen);
356 w->addrlen = addrlen;
357 memcpy (w->addr, addr, addrlen);
358 GNUNET_CONTAINER_DLL_insert(head, tail, w);
359 got_reply = GNUNET_NO;
360 pretty_printers_running ++;
361 api->address_pretty_printer (api->cls, plugin, addr, addrlen,
362 GNUNET_YES, GNUNET_TIME_UNIT_MINUTES,
363 &address_pretty_printer_cb,
366 a2s = strdup (api->address_to_string (api, w->addr, w->addrlen));
370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371 _("Plugin cannot convert address to string!\n"));
375 w->addrstring = strdup (api->address_to_string (api, w->addr, w->addrlen));
376 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
377 _("Plugin added address `%s'\n"), a2s);
379 if ((GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len)) || (NULL == s2a))
382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
383 _("Plugin cannot convert string to address!\n"));
388 if (s2a_len != w->addrlen)
390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
391 _("Plugin creates different address length when converting address->string->address: %u != %u\n"), w->addrlen, s2a_len);
395 if (0 != memcmp (s2a, w->addr, s2a_len))
396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
397 _("Plugin creates different address length when connecting back and forth!\n"));
401 if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 _("Plugin refuses added address!\n"));
409 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
411 GNUNET_SCHEDULER_cancel (timeout_wait);
412 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
415 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
418 else if (GNUNET_NO == add_remove)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 _("Removing address of length %u\n"), addrlen);
426 if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
437 _("Plugin removes address never added!\n"));
442 GNUNET_CONTAINER_DLL_remove (head, tail, w);
443 GNUNET_free (w->addr);
444 GNUNET_free (w->addrstring);
450 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
451 _("Invalid operation: %u \n"), add_remove);
458 static struct GNUNET_ATS_Information
459 env_get_address_type (void *cls,
460 const struct sockaddr *addr,
463 struct GNUNET_ATS_Information ats;
464 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
465 ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
470 static const struct GNUNET_MessageHeader *
473 return (const struct GNUNET_MessageHeader *) hello;
478 env_session_end (void *cls,
479 const struct GNUNET_PeerIdentity *peer,
480 struct Session * session)
486 setup_plugin_environment ()
490 env.my_identity = &my_identity;
491 env.max_connections = max_connect_per_transport;
494 env.receive = &env_receive;
495 env.notify_address = &env_notify_address;
496 env.get_address_type = &env_get_address_type;
497 env.get_our_hello = &env_get_our_hello;
498 env.session_end = &env_session_end;
502 handle_helper_message (void *cls, void *client,
503 const struct GNUNET_MessageHeader *hdr)
513 * @param c configuration to use
516 run (void *cls, char *const *args, const char *cfgfile,
517 const struct GNUNET_CONFIGURATION_Handle *c)
519 char *const *argv = cls;
520 unsigned long long tneigh;
525 timeout_endbadly = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
528 /* parse configuration */
529 if ( (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
533 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
534 "GNUNETD", "HOSTKEY",
537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
538 _("Transport service is lacking key configuration settings. Exiting.\n"));
542 if (NULL == (stats = GNUNET_STATISTICS_create ("transport", cfg)))
544 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
545 _("Could not create statistics. Exiting.\n"));
550 max_connect_per_transport = (uint32_t) tneigh;
551 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
552 GNUNET_free (keyfile);
553 if (NULL == my_private_key)
555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
556 _("Transport service could not access hostkey. Exiting.\n"));
560 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
561 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
562 &my_identity.hashPubKey);
565 hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL);
567 /* load plugins... */
568 setup_plugin_environment ();
570 GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
571 plugin = strstr(argv[0],"test_plugin_");
572 sep = strrchr(argv[0],'.');
575 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
579 plugin += strlen ("test_plugin_");
583 /* Hack for WLAN: start a second helper */
584 if (0 == strcmp (plugin, "wlan"))
586 char * helper_argv[3];
587 helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
588 helper_argv[1] = (char *) "2";
589 helper_argv[2] = NULL;
590 suid_helper = GNUNET_HELPER_start (GNUNET_NO,
591 "gnunet-helper-transport-wlan-dummy",
593 &handle_helper_message,
599 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
600 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
601 api = GNUNET_PLUGIN_load (libname, &env);
604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
605 _("Failed to load transport plugin for %s\n"), plugin);
610 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
612 /* Check if all functions are implemented */
613 if (NULL == api->address_pretty_printer)
619 if (NULL == api->address_to_string)
625 GNUNET_assert (NULL != api->check_address);
626 if (NULL == api->check_address)
632 GNUNET_assert (NULL != api->disconnect);
633 if (NULL == api->disconnect)
639 GNUNET_assert (NULL != api->get_session);
640 if (NULL == api->get_session)
646 if (NULL == api->address_pretty_printer)
652 if (NULL == api->string_to_address)
663 * The main function for the test
665 * @param argc number of arguments from the command line
666 * @param argv command line arguments
667 * @return 0 ok, 1 on error
670 main (int argc, char *const *argv)
672 static struct GNUNET_GETOPT_CommandLineOption options[] = {
673 GNUNET_GETOPT_OPTION_END
677 char *const argv_prog[] = {
678 "test_plugin_transport",
680 "test_plugin_transport_data.conf",
683 GNUNET_log_setup ("test-plugin-transport",
686 ok = 1; /* set to fail */
687 ret = (GNUNET_OK == GNUNET_PROGRAM_run (3,
689 "test-plugin-transport",
693 (void *) argv)) ? ok : 1;
694 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
698 /* end of test_plugin_transport.c */