FreeBSD has sockaddr_in.sin_len, and sockaddr_in6.sin6_len.
[oweals/gnunet.git] / src / transport / test_plugin_transport.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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 2, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20 /**
21  * @file transport/test_transport_api.c
22  * @brief testcase for transport_api.c
23  * @author Sailor Siraj
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_plugin_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_program_lib.h"
36 #include "gnunet_signatures.h"
37 #include "plugin_transport.h"
38 #include "transport.h"
39
40 #define VERBOSE GNUNET_YES
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
46
47 /**
48  * Our public key.
49  */
50 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
51
52 /**
53  * Our identity.
54  */
55 static struct GNUNET_PeerIdentity my_identity;
56
57 /**
58  * Our private key.
59  */
60 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
61
62 /**
63  * Our scheduler.
64  */
65 struct GNUNET_SCHEDULER_Handle *sched;
66
67 /**
68  * Our configuration.
69  */
70 const struct GNUNET_CONFIGURATION_Handle *cfg;
71
72 /**
73  * Number of neighbours we'd like to have.
74  */
75 static uint32_t max_connect_per_transport;
76
77 /**
78  * Environment for this plugin.
79  */
80 struct GNUNET_TRANSPORT_PluginEnvironment env;
81
82 /**
83  *handle for the api provided by this plugin
84  */
85 struct GNUNET_TRANSPORT_PluginFunctions *api;
86
87 /**
88  * Did the test pass or fail?
89  */
90 static int ok;
91
92 /**
93  * Initialize Environment for this plugin
94  */
95 struct ReadyList * 
96 receive(void *cls,void *plugin_context,
97         struct ReadyList *
98         service_context,
99         struct GNUNET_TIME_Relative
100         latency,
101         const struct GNUNET_PeerIdentity
102         * peer,
103         const struct GNUNET_MessageHeader
104         * message)
105 {
106   return NULL;
107 }
108
109 void notify_address(void *cls,
110                     const char *name,
111                     const void *addr,
112                     size_t addrlen,
113                     struct
114                     GNUNET_TIME_Relative
115                     expires)
116 {
117 }
118
119 void lookup (void *cls,
120              struct GNUNET_TIME_Relative
121              timeout,
122              const struct
123              GNUNET_PeerIdentity * target,
124              GNUNET_TRANSPORT_AddressCallback
125              iter, void *iter_cls)
126 {       
127 }
128
129
130 /**
131  * Function called when the service shuts
132  * down.  Unloads our plugins.
133  *
134  * @param cls closure
135  * @param cfg configuration to use
136  */
137 static void
138 unload_plugins (void *cls, 
139                 const struct GNUNET_CONFIGURATION_Handle *cfg)
140 {  
141   GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_tcp",api));
142   if (my_private_key != NULL)
143     GNUNET_CRYPTO_rsa_key_free (my_private_key);
144   
145 }
146
147
148 static GNUNET_SCHEDULER_TaskIdentifier validation_timeout_task;
149
150
151 static void 
152 validation_notification (void *cls,
153                          const char *name,
154                          const struct GNUNET_PeerIdentity *peer,
155                          uint32_t challenge,
156                          const char *sender_addr)
157 {
158   /* Sailor: 'test_validation' should get here
159      if the validation worked; so we cancel the
160      "delayed" task that will cause failure */
161   if (validation_timeout_task != GNUNET_SCHEDULER_NO_TASK)
162     {
163       GNUNET_SCHEDULER_cancel (sched, validation_timeout_task);
164       validation_timeout_task = GNUNET_SCHEDULER_NO_TASK;
165     }
166
167   GNUNET_assert (challenge == 42);
168   
169   /* Sailor: if this is the last test, calling this function
170      here will end the process. */
171   ok = 0; /* if the last test succeeded, report success */
172   unload_plugins (NULL, cfg);
173 }
174
175
176 static void
177 validation_failed (void *cls,
178                    const struct GNUNET_SCHEDULER_TaskContext *tc)
179 {
180   validation_timeout_task = GNUNET_SCHEDULER_NO_TASK;
181   GNUNET_break (0); /* output error */
182   /* the "validation_notification" was not called
183      in a timely fashion; we should set an error
184      code for main and shut down */  
185   unload_plugins (NULL, cfg);
186 }
187
188
189 /**
190  * Simple example test that invokes
191  * the "validate" function of the plugin
192  * and tries to see if the plugin would
193  * succeed to validate its own address.
194  * (This test is not well-written since
195  *  we hand-compile the address which
196  *  kind-of works for TCP but would not
197  *  work for other plugins; we should ask
198  *  the plugin about its address instead...).
199  */
200 static void
201 test_validation ()
202 {
203   struct sockaddr_in soaddr;
204   
205   memset (&soaddr, 0, sizeof(soaddr));
206 #if HAVE_SOCKADDR_IN_SIN_LEN
207   soaddr.sin_len = sizeof (soaddr);
208 #endif
209   soaddr.sin_family = AF_INET;
210   /* Sailor: set this port to 2367 to see the
211      testcase fail after 30s (because validation
212      fails); obviously the test should be
213      modified to test both successful and
214      unsuccessful validation in the end... */
215   soaddr.sin_port = htons(2368 /* FIXME: get from config! */);
216   soaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
217   api->validate (api->cls,
218                  &my_identity,
219                  42,
220                  TIMEOUT,
221                  &soaddr,
222                  sizeof(soaddr));                
223   /* add job to catch failure (timeout) */
224   validation_timeout_task =
225     GNUNET_SCHEDULER_add_delayed (sched,
226                                   GNUNET_NO,
227                                   GNUNET_SCHEDULER_PRIORITY_KEEP,
228                                   GNUNET_SCHEDULER_NO_TASK,
229                                   TIMEOUT,
230                                   &validation_failed,
231                                   NULL);
232 }
233
234
235 static void setup_plugin_environment()
236 {
237   env.cfg  = cfg;
238   env.sched = sched;
239   env.my_public_key = &my_public_key;
240   env.my_private_key = my_private_key;
241   env.my_identity = &my_identity;
242   env.cls=&env;
243   env.receive=&receive;
244   env.lookup=&lookup;
245   env.notify_address=&notify_address;
246   env.notify_validation = &validation_notification;
247   env.max_connections = max_connect_per_transport;       
248 }       
249
250
251 /**
252  * Runs the test.
253  *
254  * @param cls closure
255  * @param s scheduler to use
256  * @param c configuration to use
257  */
258 static void
259 run (void *cls,
260      struct GNUNET_SCHEDULER_Handle *s,
261      char *const *args,
262      const char *cfgfile,
263      const struct GNUNET_CONFIGURATION_Handle *c)
264
265   unsigned long long tneigh;
266   char *keyfile;
267   char *libname;
268
269   sched = s;
270   cfg = c;
271   /* parse configuration */
272   if ((GNUNET_OK !=
273        GNUNET_CONFIGURATION_get_value_number (c,
274                                               "TRANSPORT",
275                                               "NEIGHBOUR_LIMIT",
276                                               &tneigh)) ||
277       (GNUNET_OK !=
278        GNUNET_CONFIGURATION_get_value_filename (c,
279                                                 "GNUNETD",
280                                                 "HOSTKEY", &keyfile)))
281     {
282       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283                   _("Transport service is lacking key configuration settings.  Exiting.\n"));
284       GNUNET_SCHEDULER_shutdown (s);
285       return;
286     }
287   max_connect_per_transport = (uint32_t) tneigh;
288   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
289   GNUNET_free (keyfile);
290   if (my_private_key == NULL)
291     {
292       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293                   _("Transport service could not access hostkey.  Exiting.\n"));
294       GNUNET_SCHEDULER_shutdown (s);
295       return;
296     }
297   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, 
298                                     &my_public_key);
299   GNUNET_CRYPTO_hash (&my_public_key,
300                       sizeof (my_public_key),
301                       &my_identity.hashPubKey);
302   
303
304   
305   /* load plugins... */  
306   setup_plugin_environment();
307   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
308               _("Loading tcp transport plugin\n"));
309   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_tcp");
310
311   api = GNUNET_PLUGIN_load(libname, &env);
312   if (api == NULL)
313     {
314       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
315                   _("Failed to load transport plugin for tcp\n"));
316       /* FIXME: set some error code for main */
317       return;
318     } 
319   /* Sailor: if we had no real tests, we
320      could call this function
321      here to end the process; instead, I'll
322      show how one could run a single test below. 
323      Note that the test is not particularly well-written,
324      it just serves to illustrate (also,
325      the "validation_notification" function above is
326      part of the test.*/
327   if (0)
328     unload_plugins (NULL, cfg);
329   else
330     test_validation ();
331 }
332
333
334 /**
335  * The main function for the transport service.
336  *
337  * @param argc number of arguments from the command line
338  * @param argv command line arguments
339  * @return 0 ok, 1 on error
340  */
341 int
342 main (int argc, char *const *argv)
343 {
344   static struct GNUNET_GETOPT_CommandLineOption options[] = {
345     GNUNET_GETOPT_OPTION_END
346   };
347   int ret;
348   char *const argv_prog[] = {
349     "test_plugin_transport",
350     "-c",
351     "test_plugin_transport_data.conf",
352     "-L",
353 #if VERBOSE
354     "DEBUG",
355 #else
356     "WARNING",
357 #endif
358     NULL
359   };  
360   GNUNET_log_setup ("test-plugin-transport",
361 #if VERBOSE
362                     "DEBUG",
363 #else
364                     "WARNING",
365 #endif
366                     NULL);       
367   ok = 1; /* set to fail */
368   ret = (GNUNET_OK ==
369           GNUNET_PROGRAM_run (5,
370                               argv_prog,
371                               "test-plugin-transport",
372                               "testcase",
373                               options,
374                               &run, NULL)) ? ok : 1;
375   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
376   return ret;
377 }
378
379 /* end of test_plugin_transport.c */