changes
[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 3, 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_plugin_transport.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_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"
37
38 #include "transport.h"
39
40 /**
41  * How long until we give up on transmitting the message?
42  */
43 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
45
46 /**
47  * Our public key.
48  */
49 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
50
51 /**
52  * Our identity.
53  */
54 static struct GNUNET_PeerIdentity my_identity;
55
56 /**
57  * Our private key.
58  */
59 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
60
61 /**
62  * Our configuration.
63  */
64 const struct GNUNET_CONFIGURATION_Handle *cfg;
65
66 /**
67  * Our configuration.
68  */
69 struct GNUNET_STATISTICS_Handle *stats;
70
71 /**
72  * Our HELLO
73  */
74 struct GNUNET_HELLO_Message *hello;
75
76 /**
77  * Number of neighbours we'd like to have.
78  */
79 static uint32_t max_connect_per_transport;
80
81 /**
82  * Environment for this plugin.
83  */
84 struct GNUNET_TRANSPORT_PluginEnvironment env;
85
86 /**
87  *handle for the api provided by this plugin
88  */
89 struct GNUNET_TRANSPORT_PluginFunctions *api;
90
91 /**
92  * Helper handler
93  */
94 struct GNUNET_HELPER_Handle *suid_helper;
95
96 /**
97  * Timeout task
98  */
99 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
100
101 /**
102  * Timeout task
103  */
104 static GNUNET_SCHEDULER_TaskIdentifier timeout_wait;
105
106 /**
107  * Library name
108  */
109 static char *libname;
110
111 /**
112  * Plugin addresses head
113  */
114 struct AddressWrapper *head;
115
116 /**
117  * Plugin addresses tail
118  */
119 struct AddressWrapper *tail;
120
121 unsigned int addresses_reported;
122
123 /**
124  * Did the test pass or fail?
125  */
126 static int ok;
127
128
129 struct AddressWrapper
130 {
131   struct AddressWrapper *next;
132
133   struct AddressWrapper *prev;
134
135   void *addr;
136
137   size_t addrlen;
138
139   char *addrstring;
140 };
141
142 static void
143 end ()
144 {
145   struct AddressWrapper *w;
146   int c = 0;
147
148   if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
149   {
150       GNUNET_SCHEDULER_cancel (timeout_task);
151       timeout_task = GNUNET_SCHEDULER_NO_TASK;
152   }
153   if (NULL != api)
154       GNUNET_PLUGIN_unload (libname, api);
155
156   while (NULL != head)
157   {
158       w = head;
159       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160                 _("Plugin did not remove address `%s' \n"), w->addrstring);
161       GNUNET_CONTAINER_DLL_remove (head, tail, w);
162       c ++;
163       GNUNET_free (w->addr);
164       GNUNET_free (w->addrstring);
165       GNUNET_free (w);
166   }
167   if (c > 0)
168   {
169     GNUNET_break (0);
170     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171               _("Plugin did not remove %u addresses \n"), c);
172   }
173
174
175   GNUNET_free (libname);
176   libname = NULL;
177   GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
178   stats = NULL;
179
180   if (NULL != suid_helper)
181   {
182     GNUNET_HELPER_stop (suid_helper);
183     suid_helper = NULL;
184   }
185
186   ok = 0;
187 }
188
189 static void
190 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
191 {
192   struct AddressWrapper *w;
193   int c = 0;
194   timeout_task = GNUNET_SCHEDULER_NO_TASK;
195   if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
196   {
197       GNUNET_SCHEDULER_cancel (timeout_wait);
198       timeout_wait = GNUNET_SCHEDULER_NO_TASK;
199   }
200
201   if (NULL != cls)
202   {
203       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204                 _("Test took too long to execute, timeout .... \n"));
205   }
206
207   if (NULL != libname)
208   {
209     if (NULL != api)
210       GNUNET_PLUGIN_unload (libname, api);
211     GNUNET_free (libname);
212     libname = NULL;
213   }
214
215   while (NULL != head)
216   {
217       w = head;
218       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219                 _("Plugin did not remove address `%s' \n"), w->addrstring);
220       GNUNET_CONTAINER_DLL_remove (head, tail, w);
221       c ++;
222       GNUNET_free (w->addr);
223       GNUNET_free (w->addrstring);
224       GNUNET_free (w);
225   }
226   if (c > 0)
227   {
228     GNUNET_break (0);
229     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230               _("Plugin did not remove %u addresses \n"), c);
231   }
232
233   if (NULL != stats)
234   {
235     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
236     stats = NULL;
237   }
238
239   if (NULL != suid_helper)
240   {
241     GNUNET_HELPER_stop (suid_helper);
242     suid_helper = NULL;
243   }
244
245   ok = 1;
246 }
247
248 static void
249 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 {
251   timeout_wait = GNUNET_SCHEDULER_NO_TASK;
252   if (0 == addresses_reported)
253     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
254               _("Plugin did not report any addresses, could not check address conversion functions\n"));
255   end ();
256 }
257
258
259 static void
260 end_badly_now ()
261 {
262   if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
263   {
264       GNUNET_SCHEDULER_cancel (timeout_wait);
265       timeout_wait = GNUNET_SCHEDULER_NO_TASK;
266   }
267   if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
268   {
269       GNUNET_SCHEDULER_cancel (timeout_task);
270       timeout_task = GNUNET_SCHEDULER_NO_TASK;
271   }
272   timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
273 }
274
275
276 static struct GNUNET_TIME_Relative
277 env_receive (void *cls,
278             const struct GNUNET_PeerIdentity *peer,
279             const struct GNUNET_MessageHeader *message,
280             const struct GNUNET_ATS_Information *ats,
281             uint32_t ats_count,
282             struct Session * session,
283             const char *sender_address,
284             uint16_t sender_address_len)
285 {
286   /* do nothing */
287   return GNUNET_TIME_relative_get_zero_();
288 }
289
290
291 static void
292 env_notify_address (void *cls,
293                     int add_remove,
294                     const void *addr,
295                     size_t addrlen,
296                     const char *plugin)
297 {
298   struct AddressWrapper *w;
299   char *a2s;
300   void *s2a;
301   size_t s2a_len;
302
303   if (GNUNET_YES == add_remove)
304   {
305       addresses_reported ++;
306       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307                   _("Adding address of length %u\n"), addrlen);
308
309       w = GNUNET_malloc (sizeof (struct AddressWrapper));
310       w->addr = GNUNET_malloc (addrlen);
311       w->addrlen = addrlen;
312       memcpy (w->addr, addr, addrlen);
313       GNUNET_CONTAINER_DLL_insert(head, tail, w);
314
315       a2s = strdup (api->address_to_string (api, w->addr, w->addrlen));
316       if (NULL == a2s)
317       {
318           GNUNET_break (0);
319           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320                       _("Plugin cannot convert address to string!\n"));
321           end_badly_now();
322           return;
323       }
324       w->addrstring = strdup (api->address_to_string (api, w->addr, w->addrlen));
325       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
326                   _("Plugin added address `%s'\n"), a2s);
327
328       if ((GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len)) || (NULL == s2a))
329       {
330           GNUNET_break (0);
331           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332                       _("Plugin cannot convert string to address!\n"));
333           end_badly_now();
334           return;
335       }
336
337       if (s2a_len != w->addrlen)
338       {
339           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
340                       _("Plugin creates different address length when converting address->string->address: %u != %u\n"), w->addrlen, s2a_len);
341       }
342       else
343       {
344           if (0 != memcmp (s2a, w->addr, s2a_len))
345             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346                         _("Plugin creates different address length when connecting back and forth!\n"));
347       }
348       GNUNET_free (s2a);
349       GNUNET_free (a2s);
350       if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
351       {
352           GNUNET_break (0);
353           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
354                       _("Plugin refuses added address!\n"));
355           end_badly_now();
356           return;
357       }
358       if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
359       {
360           GNUNET_SCHEDULER_cancel (timeout_wait);
361           timeout_wait = GNUNET_SCHEDULER_NO_TASK;
362       }
363       timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
364   }
365   else if (GNUNET_NO == add_remove)
366   {
367       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368                   _("Removing address of length %u\n"), addrlen);
369
370       w = head;
371       while (NULL != w)
372       {
373           if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
374           {
375             break;
376           }
377           w = w->next;
378       }
379
380       if (w == NULL)
381       {
382           GNUNET_break (0);
383           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384                       _("Plugin removes address never added!\n"));
385           end_badly_now();
386           return;
387       }
388
389       GNUNET_CONTAINER_DLL_remove (head, tail, w);
390       GNUNET_free (w->addr);
391       GNUNET_free (w->addrstring);
392       GNUNET_free (w);
393   }
394   else
395   {
396       GNUNET_break (0);
397       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
398                   _("Invalid operation: %u \n"), add_remove);
399       end_badly_now ();
400       return;
401   }
402 }
403
404 struct GNUNET_ATS_Information
405 env_get_address_type (void *cls,
406                      const struct sockaddr *addr,
407                      size_t addrlen)
408 {
409   struct GNUNET_ATS_Information ats;
410   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
411   ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
412   return ats;
413 }
414
415 const struct GNUNET_MessageHeader *
416 env_get_our_hello (void)
417 {
418   return (const struct GNUNET_MessageHeader *) hello;
419 }
420
421 void env_session_end (void *cls,
422                       const struct GNUNET_PeerIdentity *peer,
423                       struct Session * session)
424 {
425
426 }
427
428 static void
429 setup_plugin_environment ()
430 {
431   env.cfg = cfg;
432   env.cls = &env;
433   env.my_identity = &my_identity;
434   env.max_connections = max_connect_per_transport;
435   env.stats = stats;
436
437   env.receive = &env_receive;
438   env.notify_address = &env_notify_address;
439   env.get_address_type = &env_get_address_type;
440   env.get_our_hello = &env_get_our_hello;
441   env.session_end = &env_session_end;
442 }
443
444 static int
445 handle_helper_message (void *cls, void *client,
446                        const struct GNUNET_MessageHeader *hdr)
447 {
448   return GNUNET_OK;
449 }
450
451 /**
452  * Runs the test.
453  *
454  * @param cls closure
455  * @param c configuration to use
456  */
457 static void
458 run (void *cls, char *const *args, const char *cfgfile,
459      const struct GNUNET_CONFIGURATION_Handle *c)
460 {
461   char *const *argv = cls;
462   unsigned long long tneigh;
463   char *keyfile;
464   char *plugin;
465   char *sep;
466
467   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
468
469   cfg = c;
470   /* parse configuration */
471   if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
472                           "TRANSPORT",
473                           "NEIGHBOUR_LIMIT",
474                           &tneigh)) ||
475       (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
476                           "GNUNETD", "HOSTKEY",
477                           &keyfile)))
478   {
479     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
480                 _("Transport service is lacking key configuration settings.  Exiting.\n"));
481
482     return;
483   }
484
485   stats = GNUNET_STATISTICS_create ("transport", cfg);
486   if (NULL == stats)
487   {
488       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
489                   _("Could not create statistics.  Exiting.\n"));
490       end_badly_now ();
491       return;
492   }
493
494   max_connect_per_transport = (uint32_t) tneigh;
495   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
496   GNUNET_free (keyfile);
497   if (my_private_key == NULL)
498   {
499     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500                 _("Transport service could not access hostkey.  Exiting.\n"));
501     end_badly_now ();
502     return;
503   }
504   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
505   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
506                       &my_identity.hashPubKey);
507
508
509   hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL);
510
511   /* load plugins... */
512   setup_plugin_environment ();
513
514   GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
515   plugin = strstr(argv[0],"test_plugin_");
516   sep = strrchr(argv[0],'.');
517   if (NULL == plugin)
518   {
519       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
520       end_badly_now ();
521       return;
522   }
523   plugin += strlen ("test_plugin_");
524   if (NULL != sep)
525       sep[0] = '\0';
526
527   /* Hack for WLAN: start a second helper */
528   if (0 == strcmp (plugin, "wlan"))
529   {
530     char * helper_argv[3];
531     helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
532     helper_argv[1] = (char *) "2";
533     helper_argv[2] = NULL;
534     suid_helper = GNUNET_HELPER_start ("gnunet-helper-transport-wlan-dummy",
535                                        helper_argv,
536                                        &handle_helper_message,
537                                        NULL,
538                                        NULL);
539   }
540
541
542   /* Loading plugin */
543   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
544   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
545   api = GNUNET_PLUGIN_load (libname, &env);
546   if (api == NULL)
547   {
548     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
549                 _("Failed to load transport plugin for %s\n"), plugin);
550     end_badly_now ();
551     return;
552   }
553
554   timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
555
556   /* Check if all functions are implemented */
557   if (NULL == api->address_pretty_printer)
558   {
559       GNUNET_break (0);
560       end_badly_now ();
561       return;
562   }
563   if (NULL == api->address_to_string)
564   {
565       GNUNET_break (0);
566       end_badly_now ();
567       return;
568   }
569   GNUNET_assert (NULL != api->check_address);
570   if (NULL == api->check_address)
571   {
572       GNUNET_break (0);
573       end_badly_now ();
574       return;
575   }
576   GNUNET_assert (NULL != api->disconnect);
577   if (NULL == api->disconnect)
578   {
579       GNUNET_break (0);
580       end_badly_now ();
581       return;
582   }
583   GNUNET_assert (NULL != api->get_session);
584   if (NULL == api->get_session)
585   {
586       GNUNET_break (0);
587       end_badly_now ();
588       return;
589   }
590   if (NULL == api->address_pretty_printer)
591   {
592       GNUNET_break (0);
593       end_badly_now ();
594       return;
595   }
596   if (NULL == api->string_to_address)
597   {
598       GNUNET_break (0);
599       end_badly_now ();
600       return;
601   }
602 }
603
604
605 /**
606  * The main function for the transport service.
607  *
608  * @param argc number of arguments from the command line
609  * @param argv command line arguments
610  * @return 0 ok, 1 on error
611  */
612 int
613 main (int argc, char *const *argv)
614 {
615   static struct GNUNET_GETOPT_CommandLineOption options[] = {
616     GNUNET_GETOPT_OPTION_END
617   };
618   int ret;
619
620   char *const argv_prog[] = {
621     "test_plugin_transport",
622     "-c",
623     "test_plugin_transport_data.conf",
624     "-L", "WARNING",
625     NULL
626   };
627   GNUNET_log_setup ("test-plugin-transport",
628                     "WARNING",
629                     NULL);
630   ok = 1;                       /* set to fail */
631   ret = (GNUNET_OK == GNUNET_PROGRAM_run (5,
632                            argv_prog,
633                            "test-plugin-transport",
634                            "testcase",
635                            options,
636                            &run,
637                            (void *) argv)) ? ok : 1;
638   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
639   return ret;
640 }
641
642 /* end of test_plugin_transport.c */