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