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 (WAIT, 3)
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 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
99 static GNUNET_SCHEDULER_TaskIdentifier timeout_wait;
104 static char *libname;
107 * Plugin addresses head
109 struct AddressWrapper *head;
112 * Plugin addresses tail
114 struct AddressWrapper *tail;
116 unsigned int addresses_reported;
119 * Did the test pass or fail?
124 struct AddressWrapper
126 struct AddressWrapper *next;
128 struct AddressWrapper *prev;
138 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
140 GNUNET_SCHEDULER_cancel (timeout_task);
141 timeout_task = GNUNET_SCHEDULER_NO_TASK;
144 GNUNET_PLUGIN_unload (libname, api);
145 GNUNET_free (libname);
147 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
154 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
156 struct AddressWrapper *w;
158 timeout_task = GNUNET_SCHEDULER_NO_TASK;
159 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
161 GNUNET_SCHEDULER_cancel (timeout_wait);
162 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
167 GNUNET_PLUGIN_unload (libname, api);
168 GNUNET_free (libname);
175 GNUNET_CONTAINER_DLL_remove (head, tail, w);
177 GNUNET_free (w->addr);
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184 _("Plugin did not remove %u addresses \n"), c);
189 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
197 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
199 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
200 if (0 == addresses_reported)
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 _("Plugin did not report any addresses, could not check plugin \n"));
210 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
212 GNUNET_SCHEDULER_cancel (timeout_wait);
213 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
215 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
217 GNUNET_SCHEDULER_cancel (timeout_task);
218 timeout_task = GNUNET_SCHEDULER_NO_TASK;
220 timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
224 static struct GNUNET_TIME_Relative
225 env_receive (void *cls,
226 const struct GNUNET_PeerIdentity *peer,
227 const struct GNUNET_MessageHeader *message,
228 const struct GNUNET_ATS_Information *ats,
230 struct Session * session,
231 const char *sender_address,
232 uint16_t sender_address_len)
235 return GNUNET_TIME_relative_get_zero_();
240 env_notify_address (void *cls,
245 struct AddressWrapper *w;
250 if (GNUNET_YES == add_remove)
252 addresses_reported ++;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 _("Adding address of length %u\n"), addrlen);
256 w = GNUNET_malloc (sizeof (struct AddressWrapper));
257 w->addr = GNUNET_malloc (addrlen);
258 w->addrlen = addrlen;
259 memcpy (w->addr, addr, addrlen);
260 GNUNET_CONTAINER_DLL_insert(head, tail, w);
262 a2s = strdup (api->address_to_string (api, w->addr, w->addrlen));
266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267 _("Plugin cannot convert address to string!\n"));
272 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
273 _("Plugin added address `%s'\n"), a2s);
275 if ((GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len)) || (NULL == s2a))
278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
279 _("Plugin cannot convert string to address!\n"));
284 if (s2a_len != w->addrlen)
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
287 _("Plugin creates different address length when converting address->string->address: %u != %u\n"), w->addrlen, s2a_len);
291 if (0 != memcmp (s2a, w->addr, s2a_len))
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293 _("Plugin creates different address length when connecting back and forth!\n"));
297 if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
301 _("Plugin refuses added address!\n"));
305 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
307 GNUNET_SCHEDULER_cancel (timeout_wait);
308 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
310 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
312 else if (GNUNET_NO == add_remove)
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 _("Removing address of length %u\n"), addrlen);
320 if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
331 _("Plugin removes address never added!\n"));
336 GNUNET_CONTAINER_DLL_remove (head, tail, w);
337 GNUNET_free (w->addr);
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344 _("Invalid operation: %u \n"), add_remove);
350 struct GNUNET_ATS_Information
351 env_get_address_type (void *cls,
352 const struct sockaddr *addr,
355 struct GNUNET_ATS_Information ats;
356 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
357 ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
361 const struct GNUNET_MessageHeader *
362 env_get_our_hello (void)
364 return (const struct GNUNET_MessageHeader *) hello;
367 void env_session_end (void *cls,
368 const struct GNUNET_PeerIdentity *peer,
369 struct Session * session)
375 setup_plugin_environment ()
379 env.my_identity = &my_identity;
380 env.max_connections = max_connect_per_transport;
383 env.receive = &env_receive;
384 env.notify_address = &env_notify_address;
385 env.get_address_type = &env_get_address_type;
386 env.get_our_hello = &env_get_our_hello;
387 env.session_end = &env_session_end;
395 * @param c configuration to use
398 run (void *cls, char *const *args, const char *cfgfile,
399 const struct GNUNET_CONFIGURATION_Handle *c)
401 char *const *argv = cls;
402 unsigned long long tneigh;
407 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, NULL);
410 /* parse configuration */
411 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
415 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
416 "GNUNETD", "HOSTKEY",
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 _("Transport service is lacking key configuration settings. Exiting.\n"));
425 stats = GNUNET_STATISTICS_create ("transport", cfg);
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
429 _("Could not create statistics. Exiting.\n"));
434 max_connect_per_transport = (uint32_t) tneigh;
435 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
436 GNUNET_free (keyfile);
437 if (my_private_key == NULL)
439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
440 _("Transport service could not access hostkey. Exiting.\n"));
444 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
445 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
446 &my_identity.hashPubKey);
449 hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL);
451 /* load plugins... */
452 setup_plugin_environment ();
454 GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
455 plugin = strstr(argv[0],"test_plugin_");
456 sep = strrchr(argv[0],'.');
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
463 plugin += strlen ("test_plugin_");
468 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
469 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
470 api = GNUNET_PLUGIN_load (libname, &env);
473 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
474 _("Failed to load transport plugin for %s\n"), plugin);
479 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
481 /* Check if all functions are implemented */
482 if (NULL == api->address_pretty_printer)
488 if (NULL == api->address_to_string)
494 GNUNET_assert (NULL != api->check_address);
495 if (NULL == api->check_address)
501 GNUNET_assert (NULL != api->disconnect);
502 if (NULL == api->disconnect)
508 GNUNET_assert (NULL != api->get_session);
509 if (NULL == api->get_session)
515 if (NULL == api->address_pretty_printer)
521 if (NULL == api->string_to_address)
531 * The main function for the transport service.
533 * @param argc number of arguments from the command line
534 * @param argv command line arguments
535 * @return 0 ok, 1 on error
538 main (int argc, char *const *argv)
540 static struct GNUNET_GETOPT_CommandLineOption options[] = {
541 GNUNET_GETOPT_OPTION_END
545 char *const argv_prog[] = {
546 "test_plugin_transport",
548 "test_plugin_transport_data.conf",
552 GNUNET_log_setup ("test-plugin-transport",
555 ok = 1; /* set to fail */
556 ret = (GNUNET_OK == GNUNET_PROGRAM_run (5,
558 "test-plugin-transport",
562 (void *) argv)) ? ok : 1;
563 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
567 /* end of test_plugin_transport.c */