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