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))
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"));
296 if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300 _("Plugin refuses added address!\n"));
304 if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
306 GNUNET_SCHEDULER_cancel (timeout_wait);
307 timeout_wait = GNUNET_SCHEDULER_NO_TASK;
309 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
311 else if (GNUNET_NO == add_remove)
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 _("Removing address of length %u\n"), addrlen);
319 if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
330 _("Plugin removes address never added!\n"));
335 GNUNET_CONTAINER_DLL_remove (head, tail, w);
336 GNUNET_free (w->addr);
342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
343 _("Invalid operation: %u \n"), add_remove);
349 struct GNUNET_ATS_Information
350 env_get_address_type (void *cls,
351 const struct sockaddr *addr,
354 struct GNUNET_ATS_Information ats;
355 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
356 ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
360 const struct GNUNET_MessageHeader *
361 env_get_our_hello (void)
363 return (const struct GNUNET_MessageHeader *) hello;
366 void env_session_end (void *cls,
367 const struct GNUNET_PeerIdentity *peer,
368 struct Session * session)
374 setup_plugin_environment ()
378 env.my_identity = &my_identity;
379 env.max_connections = max_connect_per_transport;
382 env.receive = &env_receive;
383 env.notify_address = &env_notify_address;
384 env.get_address_type = &env_get_address_type;
385 env.get_our_hello = &env_get_our_hello;
386 env.session_end = &env_session_end;
394 * @param c configuration to use
397 run (void *cls, char *const *args, const char *cfgfile,
398 const struct GNUNET_CONFIGURATION_Handle *c)
400 char *const *argv = cls;
401 unsigned long long tneigh;
406 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, NULL);
409 /* parse configuration */
410 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
414 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
415 "GNUNETD", "HOSTKEY",
418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
419 _("Transport service is lacking key configuration settings. Exiting.\n"));
424 stats = GNUNET_STATISTICS_create ("transport", cfg);
427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
428 _("Could not create statistics. Exiting.\n"));
433 max_connect_per_transport = (uint32_t) tneigh;
434 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
435 GNUNET_free (keyfile);
436 if (my_private_key == NULL)
438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
439 _("Transport service could not access hostkey. Exiting.\n"));
443 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
444 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
445 &my_identity.hashPubKey);
448 hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL);
450 /* load plugins... */
451 setup_plugin_environment ();
453 GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
454 plugin = strstr(argv[0],"test_plugin_");
455 sep = strrchr(argv[0],'.');
458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
462 plugin += strlen ("test_plugin_");
467 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
468 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
469 api = GNUNET_PLUGIN_load (libname, &env);
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 _("Failed to load transport plugin for %s\n"), plugin);
478 timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
480 /* Check if all functions are implemented */
481 if (NULL == api->address_pretty_printer)
487 if (NULL == api->address_to_string)
493 GNUNET_assert (NULL != api->check_address);
494 if (NULL == api->check_address)
500 GNUNET_assert (NULL != api->disconnect);
501 if (NULL == api->disconnect)
507 GNUNET_assert (NULL != api->get_session);
508 if (NULL == api->get_session)
514 if (NULL == api->address_pretty_printer)
520 if (NULL == api->string_to_address)
530 * The main function for the transport service.
532 * @param argc number of arguments from the command line
533 * @param argv command line arguments
534 * @return 0 ok, 1 on error
537 main (int argc, char *const *argv)
539 static struct GNUNET_GETOPT_CommandLineOption options[] = {
540 GNUNET_GETOPT_OPTION_END
544 char *const argv_prog[] = {
545 "test_plugin_transport",
547 "test_plugin_transport_data.conf",
551 GNUNET_log_setup ("test-plugin-transport",
554 ok = 1; /* set to fail */
555 ret = (GNUNET_OK == GNUNET_PROGRAM_run (5,
557 "test-plugin-transport",
561 (void *) argv)) ? ok : 1;
562 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
566 /* end of test_plugin_transport.c */