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