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