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 TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
48 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
53 static struct GNUNET_PeerIdentity my_identity;
58 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 struct GNUNET_STATISTICS_Handle *stats;
71 * Number of neighbours we'd like to have.
73 static uint32_t max_connect_per_transport;
76 * Environment for this plugin.
78 struct GNUNET_TRANSPORT_PluginEnvironment env;
81 *handle for the api provided by this plugin
83 struct GNUNET_TRANSPORT_PluginFunctions *api;
88 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
95 struct AddressWrapper *head;
96 struct AddressWrapper *tail;
99 * Did the test pass or fail?
104 struct AddressWrapper
106 struct AddressWrapper *next;
108 struct AddressWrapper *prev;
123 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
125 GNUNET_SCHEDULER_cancel (timeout_task);
126 timeout_task = GNUNET_SCHEDULER_NO_TASK;
131 GNUNET_PLUGIN_unload (libname, api);
133 GNUNET_free (libname);
135 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
143 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
145 struct AddressWrapper *w;
148 timeout_task = GNUNET_SCHEDULER_NO_TASK;
152 GNUNET_PLUGIN_unload (libname, api);
153 GNUNET_free (libname);
160 GNUNET_CONTAINER_DLL_remove (head, tail, w);
162 GNUNET_free (w->addr);
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 _("Plugin did not remove %u addresses \n"), c);
174 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
185 if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
187 GNUNET_SCHEDULER_cancel (timeout_task);
188 timeout_task = GNUNET_SCHEDULER_NO_TASK;
190 timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
194 static struct GNUNET_TIME_Relative
195 env_receive (void *cls,
196 const struct GNUNET_PeerIdentity *peer,
197 const struct GNUNET_MessageHeader *message,
198 const struct GNUNET_ATS_Information *ats,
200 struct Session * session,
201 const char *sender_address,
202 uint16_t sender_address_len)
206 return GNUNET_TIME_relative_get_zero_();
211 env_notify_address (void *cls,
216 struct AddressWrapper *w;
221 if (GNUNET_YES == add_remove)
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
224 _("Adding address of length %u\n"), addrlen);
226 w = GNUNET_malloc (sizeof (struct AddressWrapper));
227 w->addr = GNUNET_malloc (addrlen);
228 w->addrlen = addrlen;
229 memcpy (w->addr, addr, addrlen);
230 GNUNET_CONTAINER_DLL_insert(head, tail, w);
232 a2s = strdup (api->address_to_string (api, w->addr, w->addrlen));
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 _("Plugin cannot convert address to string!\n"));
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243 _("Plugin added address `%s'\n"), a2s);
245 if (GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len))
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 _("Plugin cannot convert string to address!\n"));
254 if (s2a_len != w->addrlen)
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257 _("Plugin creates different address length when converting address->string->address: %u != %u\n"), w->addrlen, s2a_len);
261 if (0 != memcmp (s2a, w->addr, s2a_len))
262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
263 _("Plugin creates different address length when connecting back and forth!\n"));
266 if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
270 _("Plugin refuses added address!\n"));
275 else if (GNUNET_NO == add_remove)
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
278 _("Removing address of length %u\n"), addrlen);
283 if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 _("Plugin removes address never added!\n"));
299 GNUNET_CONTAINER_DLL_remove (head, tail, w);
300 GNUNET_free (w->addr);
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 _("Invalid operation\n"));
312 struct GNUNET_ATS_Information
313 env_get_address_type (void *cls,
314 const struct sockaddr *addr,
317 struct GNUNET_ATS_Information ats;
318 ats.type = htonl (0);
319 ats.value = htonl (0);
324 const struct GNUNET_MessageHeader *
325 env_get_our_hello (void)
331 void env_session_end (void *cls,
332 const struct GNUNET_PeerIdentity *peer,
333 struct Session * session)
339 setup_plugin_environment ()
343 env.my_identity = &my_identity;
344 env.max_connections = max_connect_per_transport;
347 env.receive = &env_receive;
348 env.notify_address = &env_notify_address;
349 env.get_address_type = &env_get_address_type;
350 env.get_our_hello = &env_get_our_hello;
351 env.session_end = &env_session_end;
359 * @param c configuration to use
362 run (void *cls, char *const *args, const char *cfgfile,
363 const struct GNUNET_CONFIGURATION_Handle *c)
365 char *const *argv = cls;
366 unsigned long long tneigh;
371 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, NULL);
374 /* parse configuration */
375 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
379 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
380 "GNUNETD", "HOSTKEY",
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384 _("Transport service is lacking key configuration settings. Exiting.\n"));
389 stats = GNUNET_STATISTICS_create ("transport", cfg);
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 _("Could not create statistics. Exiting.\n"));
398 max_connect_per_transport = (uint32_t) tneigh;
399 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
400 GNUNET_free (keyfile);
401 if (my_private_key == NULL)
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404 _("Transport service could not access hostkey. Exiting.\n"));
408 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
409 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
410 &my_identity.hashPubKey);
412 /* load plugins... */
413 setup_plugin_environment ();
415 plugin = strrchr(argv[0],'_');
416 sep = strrchr(argv[0],'.');
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
428 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
429 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
430 api = GNUNET_PLUGIN_load (libname, &env);
433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
434 _("Failed to load transport plugin for tcp\n"));
439 /* Check if all functions are implemented */
440 if (NULL == api->address_pretty_printer)
446 if (NULL == api->address_to_string)
452 GNUNET_assert (NULL != api->check_address);
453 if (NULL == api->check_address)
459 GNUNET_assert (NULL != api->disconnect);
460 if (NULL == api->disconnect)
466 GNUNET_assert (NULL != api->get_session);
467 if (NULL == api->get_session)
473 if (NULL == api->address_pretty_printer)
479 if (NULL == api->string_to_address)
489 * The main function for the transport service.
491 * @param argc number of arguments from the command line
492 * @param argv command line arguments
493 * @return 0 ok, 1 on error
496 main (int argc, char *const *argv)
498 static struct GNUNET_GETOPT_CommandLineOption options[] = {
499 GNUNET_GETOPT_OPTION_END
503 char *const argv_prog[] = {
504 "test_plugin_transport",
506 "test_plugin_transport_data.conf",
510 GNUNET_log_setup ("test-plugin-transport",
513 ok = 1; /* set to fail */
514 ret = (GNUNET_OK == GNUNET_PROGRAM_run (5,
516 "test-plugin-transport",
520 (void *) argv)) ? ok : 1;
521 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
525 /* end of test_plugin_transport.c */