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