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