9b1760919ab45697cc2d82340a9670e2611f8394
[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, GNUNET_NO);
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, GNUNET_NO);
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             struct Session * session,
296             const char *sender_address,
297             uint16_t sender_address_len)
298 {
299   /* do nothing */
300   return GNUNET_TIME_relative_get_zero_();
301 }
302
303
304 static int got_reply;
305
306
307 /**
308  * Take the given address and append it to the set of results sent back to
309  * the client.
310  *
311  * @param cls the transmission context used ('struct GNUNET_SERVER_TransmitContext*')
312  * @param buf text to transmit
313  */
314 static void
315 address_pretty_printer_cb (void *cls, const char *buf)
316 {
317   if (NULL != buf)
318   {
319     got_reply = GNUNET_YES;
320     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
321                 "Pretty address : `%s'\n", buf);
322     pretty_printers_running --;
323   }
324   else
325   {
326       if (GNUNET_NO == got_reply)
327       {
328           pretty_printers_running --;
329           GNUNET_break (0);
330           end_badly_now ();
331       }
332   }
333 }
334
335
336 static void
337 env_notify_address (void *cls,
338                     int add_remove,
339                     const void *addr,
340                     size_t addrlen,
341                     const char *plugin)
342 {
343   struct AddressWrapper *w;
344   char *a2s;
345   void *s2a;
346   size_t s2a_len;
347
348   if (GNUNET_YES == add_remove)
349   {
350       addresses_reported ++;
351       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352                   _("Adding address of length %u\n"), addrlen);
353
354       w = GNUNET_malloc (sizeof (struct AddressWrapper));
355       w->addr = GNUNET_malloc (addrlen);
356       w->addrlen = addrlen;
357       memcpy (w->addr, addr, addrlen);
358       GNUNET_CONTAINER_DLL_insert(head, tail, w);
359       got_reply = GNUNET_NO;
360       pretty_printers_running ++;
361       api->address_pretty_printer (api->cls, plugin, addr, addrlen,
362                                     GNUNET_YES, GNUNET_TIME_UNIT_MINUTES,
363                                     &address_pretty_printer_cb,
364                                     w);
365
366       a2s = strdup (api->address_to_string (api, w->addr, w->addrlen));
367       if (NULL == a2s)
368       {
369           GNUNET_break (0);
370           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371                       _("Plugin cannot convert address to string!\n"));
372           end_badly_now();
373           return;
374       }
375       w->addrstring = strdup (api->address_to_string (api, w->addr, w->addrlen));
376       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
377                   _("Plugin added address `%s'\n"), a2s);
378
379       if ((GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len)) || (NULL == s2a))
380       {
381           GNUNET_break (0);
382           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
383                       _("Plugin cannot convert string to address!\n"));
384           end_badly_now();
385           return;
386       }
387
388       if (s2a_len != w->addrlen)
389       {
390           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
391                       _("Plugin creates different address length when converting address->string->address: %u != %u\n"), w->addrlen, s2a_len);
392       }
393       else
394       {
395           if (0 != memcmp (s2a, w->addr, s2a_len))
396             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
397                         _("Plugin creates different address length when connecting back and forth!\n"));
398       }
399       GNUNET_free (s2a);
400       GNUNET_free (a2s);
401       if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
402       {
403           GNUNET_break (0);
404           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405                       _("Plugin refuses added address!\n"));
406           end_badly_now();
407           return;
408       }
409       if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
410       {
411           GNUNET_SCHEDULER_cancel (timeout_wait);
412           timeout_wait = GNUNET_SCHEDULER_NO_TASK;
413       }
414
415       timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
416
417   }
418   else if (GNUNET_NO == add_remove)
419   {
420       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421                   _("Removing address of length %u\n"), addrlen);
422
423       w = head;
424       while (NULL != w)
425       {
426           if ((addrlen == w->addrlen) && (0 == memcmp (w->addr, addr, addrlen)))
427           {
428             break;
429           }
430           w = w->next;
431       }
432
433       if (w == NULL)
434       {
435           GNUNET_break (0);
436           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
437                       _("Plugin removes address never added!\n"));
438           end_badly_now();
439           return;
440       }
441
442       GNUNET_CONTAINER_DLL_remove (head, tail, w);
443       GNUNET_free (w->addr);
444       GNUNET_free (w->addrstring);
445       GNUNET_free (w);
446   }
447   else
448   {
449       GNUNET_break (0);
450       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
451                   _("Invalid operation: %u \n"), add_remove);
452       end_badly_now ();
453       return;
454   }
455 }
456
457
458 static struct GNUNET_ATS_Information
459 env_get_address_type (void *cls,
460                      const struct sockaddr *addr,
461                      size_t addrlen)
462 {
463   struct GNUNET_ATS_Information ats;
464   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
465   ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
466   return ats;
467 }
468
469
470 static const struct GNUNET_MessageHeader *
471 env_get_our_hello ()
472 {
473   return (const struct GNUNET_MessageHeader *) hello;
474 }
475
476
477 static void 
478 env_session_end (void *cls,
479                  const struct GNUNET_PeerIdentity *peer,
480                  struct Session * session)
481 {
482 }
483
484
485 static void
486 env_update_metrics (void *cls,
487           const struct GNUNET_PeerIdentity *peer,
488           const void *address,
489           uint16_t address_len,
490           struct Session *session,
491           const struct GNUNET_ATS_Information *ats,
492           uint32_t ats_count)
493 {
494 }
495
496
497 static void
498 setup_plugin_environment ()
499 {
500   env.cfg = cfg;
501   env.cls = &env;
502   env.my_identity = &my_identity;
503   env.max_connections = max_connect_per_transport;
504   env.stats = stats;
505
506   env.receive = &env_receive;
507   env.notify_address = &env_notify_address;
508   env.get_address_type = &env_get_address_type;
509   env.update_address_metrics = &env_update_metrics;
510   env.get_our_hello = &env_get_our_hello;
511   env.session_end = &env_session_end;
512 }
513
514 static int
515 handle_helper_message (void *cls, void *client,
516                        const struct GNUNET_MessageHeader *hdr)
517 {
518   return GNUNET_OK;
519 }
520
521
522 /**
523  * Runs the test.
524  *
525  * @param cls closure
526  * @param c configuration to use
527  */
528 static void
529 run (void *cls, char *const *args, const char *cfgfile,
530      const struct GNUNET_CONFIGURATION_Handle *c)
531 {
532   char *const *argv = cls;
533   unsigned long long tneigh;
534   char *keyfile;
535   char *plugin;
536   char *sep;
537
538   timeout_endbadly = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
539
540   cfg = c;
541   /* parse configuration */
542   if ( (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c,
543                                                             "TRANSPORT",
544                                                             "NEIGHBOUR_LIMIT",
545                                                             &tneigh)) ||
546        (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
547                                                               "PEER", "PRIVATE_KEY",
548                                                               &keyfile)))
549   {
550     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
551                 _("Transport service is lacking key configuration settings.  Exiting.\n"));
552     return;
553   }
554
555   if (NULL == (stats = GNUNET_STATISTICS_create ("transport", cfg)))
556   {
557     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
558                 _("Could not create statistics.  Exiting.\n"));
559     end_badly_now ();
560     return;
561   }
562
563   if (GNUNET_OK != GNUNET_DISK_file_test (HOSTKEY_FILE))
564   {
565       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
566                   _("Hostkey `%s' missing.  Exiting.\n"),
567                   HOSTKEY_FILE);
568   }
569
570   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (keyfile))
571   {
572       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
573                   _("Could not create a directory for hostkey `%s'.  Exiting.\n"),
574                   keyfile);
575       end_badly_now ();
576       return;
577   }
578
579   if (GNUNET_OK !=  GNUNET_DISK_file_copy (HOSTKEY_FILE, keyfile))
580   {
581       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
582                   _("Could not copy hostkey `%s' to destination `%s'.  Exiting.\n"),
583                   HOSTKEY_FILE, keyfile);
584       end_badly_now ();
585       return;
586   }
587
588
589   max_connect_per_transport = (uint32_t) tneigh;
590   my_private_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
591   GNUNET_free (keyfile);
592   if (NULL == my_private_key)
593   {
594     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
595                 _("Transport service could not access hostkey.  Exiting.\n"));
596     end_badly_now ();
597     return;
598   }
599   GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
600   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
601                       &my_identity.hashPubKey);
602
603
604   hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL, GNUNET_NO);
605
606   /* load plugins... */
607   setup_plugin_environment ();
608
609   GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
610   plugin = strstr(argv[0],"test_plugin_");
611   sep = strrchr(argv[0],'.');
612   if (NULL == plugin)
613   {
614       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Not a valid test name\n"));
615       end_badly_now ();
616       return;
617   }
618   plugin += strlen ("test_plugin_");
619   if (NULL != sep)
620       sep[0] = '\0';
621
622   /* Hack for WLAN: start a second helper */
623   if (0 == strcmp (plugin, "wlan"))
624   {
625     char * helper_argv[3];
626     helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
627     helper_argv[1] = (char *) "2";
628     helper_argv[2] = NULL;
629     suid_helper = GNUNET_HELPER_start (GNUNET_NO,
630                                        "gnunet-helper-transport-wlan-dummy",
631                                        helper_argv,
632                                        &handle_helper_message,
633                                        NULL,
634                                        NULL);
635   }
636
637   /* Loading plugin */
638   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
639   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
640   api = GNUNET_PLUGIN_load (libname, &env);
641   if (api == NULL)
642   {
643     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
644                 _("Failed to load transport plugin for %s\n"), plugin);
645     end_badly_now ();
646     return;
647   }
648
649   timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
650
651   /* Check if all functions are implemented */
652   if (NULL == api->address_pretty_printer)
653   {
654       GNUNET_break (0);
655       end_badly_now ();
656       return;
657   }
658   if (NULL == api->address_to_string)
659   {
660       GNUNET_break (0);
661       end_badly_now ();
662       return;
663   }
664   GNUNET_assert (NULL != api->check_address);
665   if (NULL == api->check_address)
666   {
667       GNUNET_break (0);
668       end_badly_now ();
669       return;
670   }
671   GNUNET_assert (NULL != api->disconnect);
672   if (NULL == api->disconnect)
673   {
674       GNUNET_break (0);
675       end_badly_now ();
676       return;
677   }
678   GNUNET_assert (NULL != api->get_session);
679   if (NULL == api->get_session)
680   {
681       GNUNET_break (0);
682       end_badly_now ();
683       return;
684   }
685   if (NULL == api->address_pretty_printer)
686   {
687       GNUNET_break (0);
688       end_badly_now ();
689       return;
690   }
691   if (NULL == api->string_to_address)
692   {
693       GNUNET_break (0);
694       end_badly_now ();
695       return;
696   }
697
698 }
699
700
701 /**
702  * The main function for the test
703  *
704  * @param argc number of arguments from the command line
705  * @param argv command line arguments
706  * @return 0 ok, 1 on error
707  */
708 int
709 main (int argc, char *const *argv)
710 {
711   static struct GNUNET_GETOPT_CommandLineOption options[] = {
712     GNUNET_GETOPT_OPTION_END
713   };
714   int ret;
715
716   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
717
718   char *const argv_prog[] = {
719     "test_plugin_transport",
720     "-c",
721     "test_plugin_transport_data.conf",
722     NULL
723   };
724   GNUNET_log_setup ("test-plugin-transport",
725                     "WARNING",
726                     NULL);
727   ok = 1;                       /* set to fail */
728   ret = (GNUNET_OK == GNUNET_PROGRAM_run (3,
729                                           argv_prog,
730                                           "test-plugin-transport",
731                                           "testcase",
732                                           options,
733                                           &run,
734                                           (void *) argv)) ? ok : 1;
735   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
736   return ret;
737 }
738
739 /* end of test_plugin_transport.c */