-fix NPE
[oweals/gnunet.git] / src / transport / test_plugin_transport.c
1 /*
2  This file is part of GNUnet.
3  Copyright (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., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * timeout_endbadly;
93
94 /**
95  * Timeout task
96  */
97 static struct GNUNET_SCHEDULER_Task * 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 struct AddressWrapper
124 {
125   struct AddressWrapper *next;
126
127   struct AddressWrapper *prev;
128
129   struct GNUNET_HELLO_Address *address;
130
131   char *addrstring;
132
133   struct GNUNET_SCHEDULER_Task *test_task;
134
135 };
136
137
138 static void
139 end ()
140 {
141   struct AddressWrapper *w;
142   int c = 0;
143   ok = 0;
144
145   if (NULL != timeout_endbadly)
146   {
147     GNUNET_SCHEDULER_cancel (timeout_endbadly);
148     timeout_endbadly = NULL;
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",
158                 w->addrstring);
159     GNUNET_CONTAINER_DLL_remove(head, tail, w);
160     c++;
161     GNUNET_HELLO_address_free(w->address);
162     GNUNET_free(w->addrstring);
163     GNUNET_free(w);
164   }
165   if (c > 0)
166   {
167     GNUNET_break(0);
168     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses \n",
169         c);
170     ok = 1;
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 = NULL;
193   if (NULL != timeout_wait)
194   {
195     GNUNET_SCHEDULER_cancel (timeout_wait);
196     timeout_wait = NULL;
197   }
198
199   if (pretty_printers_running > 0)
200   {
201     timeout_endbadly = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
202         &end_badly, &ok);
203     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
204         "Have pending calls to pretty_printer ... deferring shutdown\n");
205     return;
206   }
207
208   if (NULL != cls)
209   {
210     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
211         "Test took too long to execute, timeout .... \n");
212   }
213
214   if (NULL != libname)
215   {
216     if (NULL != api)
217       GNUNET_PLUGIN_unload (libname, api);
218     GNUNET_free(libname);
219     libname = NULL;
220   }
221
222   while (NULL != head)
223   {
224     w = head;
225     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove address `%s'\n",
226         w->addrstring);
227     GNUNET_CONTAINER_DLL_remove(head, tail, w);
228     c++;
229     GNUNET_HELLO_address_free(w->address);
230     if (NULL != w->test_task)
231       GNUNET_SCHEDULER_cancel (w->test_task);
232     GNUNET_free(w->addrstring);
233     GNUNET_free(w);
234   }
235   if (c > 0)
236   {
237     GNUNET_break(0);
238     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Plugin did not remove %u addresses\n",
239         c);
240   }
241
242   if (NULL != stats)
243   {
244     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
245     stats = NULL;
246   }
247
248   if (NULL != suid_helper)
249   {
250     GNUNET_HELPER_stop (suid_helper, GNUNET_NO);
251     suid_helper = NULL;
252   }
253
254   ok = 1;
255 }
256
257 static void
258 wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 {
260   timeout_wait = NULL;
261   if (0 == addresses_reported)
262     GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
263         "Plugin did not report any addresses, could not check address conversion functions\n");
264   end ();
265 }
266
267
268 static void
269 end_badly_now ()
270 {
271   if (NULL != timeout_wait)
272   {
273     GNUNET_SCHEDULER_cancel (timeout_wait);
274     timeout_wait = NULL;
275   }
276   if (NULL != timeout_endbadly)
277   {
278     GNUNET_SCHEDULER_cancel (timeout_endbadly);
279     timeout_endbadly = NULL;
280   }
281   timeout_endbadly = GNUNET_SCHEDULER_add_now (&end_badly, NULL );
282 }
283
284
285 static struct GNUNET_TIME_Relative
286 env_receive (void *cls,
287              const struct GNUNET_HELLO_Address *address,
288              struct GNUNET_ATS_Session *session,
289              const struct GNUNET_MessageHeader *message)
290 {
291   /* do nothing */
292   return GNUNET_TIME_relative_get_zero_ ();
293 }
294
295 static int got_reply;
296
297
298 /**
299  * Take the given address and append it to the set of results sent back to
300  * the client.
301  *
302  * @param cls closure
303  * @param address the address to print
304  * @param res result code
305  */
306 static void
307 address_pretty_printer_cb (void *cls, const char *address, int res)
308 {
309   if (NULL != address)
310   {
311     got_reply = GNUNET_YES;
312     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Pretty address : `%s'\n", address);
313     pretty_printers_running--;
314   }
315   else
316   {
317     if (GNUNET_NO == got_reply)
318     {
319       pretty_printers_running--;
320       GNUNET_break(0);
321       end_badly_now ();
322     }
323   }
324 }
325
326
327 static void
328 test_addr_string (void *cls,
329                   const struct GNUNET_SCHEDULER_TaskContext *tc)
330 {
331   struct AddressWrapper *w = cls;
332   void *s2a;
333   size_t s2a_len;
334
335   w->test_task = NULL;
336
337   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
338               "Testing: address_to_string \n");
339   w->addrstring = GNUNET_strdup (api->address_to_string (api,
340                                                          w->address->address,
341                                                          w->address->address_length));
342   if (NULL == w->addrstring)
343   {
344     GNUNET_break(0);
345     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346                 "Plugin cannot convert address to string!\n");
347     end_badly_now ();
348     return;
349   }
350   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
351               "Plugin added address `%s'\n",
352               w->addrstring);
353   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354               "Testing address_to_string: OK\n");
355   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356               "Testing: string_to_address \n");
357   s2a = NULL;
358   s2a_len = 0;
359   if ((GNUNET_OK !=
360        api->string_to_address (api, w->addrstring,
361                                strlen (w->addrstring) + 1,
362                                &s2a, &s2a_len)) ||
363       (NULL == s2a))
364   {
365     GNUNET_break(0);
366     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
367                "Plugin cannot convert string to address!\n");
368     end_badly_now ();
369     return;
370   }
371
372   /*
373     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
374     "Plugin creates `%s' %u\n",api->address_to_string (api, s2a, s2a_len), s2a_len);
375
376     int c1;
377     for (c1 = 0; c1 < s2a_len; c1++ )
378     fprintf (stderr, "%u == %u\n", ((char *) s2a)[c1], ((char *) w->addr)[c1]);
379   */
380   if (s2a_len != w->address->address_length)
381   {
382     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
383                "Plugin creates different address length when converting address->string->address: %u != %u\n",
384                w->address->address_length, s2a_len);
385   }
386   else if (0 != memcmp (s2a, w->address->address, s2a_len))
387   {
388     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389                 "Plugin creates different address length when converting back and forth %i!\n",
390                 memcmp (s2a,
391                         w->address->address,
392                         s2a_len));
393   }
394   else
395   {
396     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
397                 "Testing string_to_address: OK\n");
398   }
399   GNUNET_free(s2a);
400
401   pretty_printers_running++;
402   api->address_pretty_printer (api->cls,
403                                w->address->transport_name,
404                                w->address->address,
405                                w->address->address_length,
406                                GNUNET_YES,
407                                GNUNET_TIME_UNIT_MINUTES,
408                                &address_pretty_printer_cb, w);
409
410   if (GNUNET_OK !=
411       api->check_address (api->cls,
412                           w->address->address,
413                           w->address->address_length))
414   {
415     GNUNET_break (0);
416     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417                 "Plugin refuses added address!\n");
418     end_badly_now ();
419     return;
420   }
421   if (NULL != timeout_wait)
422   {
423     GNUNET_SCHEDULER_cancel (timeout_wait);
424     timeout_wait = NULL;
425   }
426   timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
427 }
428
429
430 static void
431 env_notify_address (void *cls,
432                     int add_remove,
433                     const struct GNUNET_HELLO_Address *address)
434 {
435   struct AddressWrapper *w;
436   struct AddressWrapper *wtmp;
437
438   if (GNUNET_YES == add_remove)
439   {
440     addresses_reported++;
441     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442                "Adding address of length %u\n",
443                 address->address_length);
444
445     for (wtmp = head; NULL != wtmp; wtmp = wtmp->next)
446     {
447       if ((address->address_length == wtmp->address->address_length) &&
448           (0 == memcmp (address->address, wtmp->address->address, address->address_length)))
449       {
450         GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
451                    "Duplicate address notification .... \n");
452         return;
453       }
454     }
455
456     w = GNUNET_new (struct AddressWrapper);
457     w->address = GNUNET_HELLO_address_copy (address);
458     GNUNET_CONTAINER_DLL_insert (head, tail, w);
459     got_reply = GNUNET_NO;
460     w->test_task = GNUNET_SCHEDULER_add_now (&test_addr_string,
461                                              w);
462     return;
463   }
464
465   if (GNUNET_NO == add_remove)
466   {
467     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468                 "Removing address of length %u\n",
469                 address->address_length);
470     w = head;
471     while (NULL != w)
472     {
473       if ((address->address_length == w->address->address_length) &&
474           (0 == memcmp (w->address->address, address->address, address->address_length)))
475       {
476         break;
477       }
478       w = w->next;
479     }
480
481     if (w == NULL)
482     {
483       GNUNET_break(0);
484       GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
485           "Plugin removes address never added!\n");
486       end_badly_now ();
487       return;
488     }
489
490     GNUNET_CONTAINER_DLL_remove(head, tail, w);
491     GNUNET_HELLO_address_free (w->address);
492     GNUNET_free(w->addrstring);
493     GNUNET_free(w);
494     return;
495   }
496   GNUNET_break(0);
497   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
498               "Invalid operation: %u\n",
499               add_remove);
500   end_badly_now ();
501 }
502
503
504 static enum GNUNET_ATS_Network_Type
505 env_get_address_type (void *cls,
506                       const struct sockaddr *addr,
507                       size_t addrlen)
508 {
509   return GNUNET_ATS_NET_LOOPBACK;
510 }
511
512
513 static const struct GNUNET_MessageHeader *
514 env_get_our_hello ()
515 {
516   return (const struct GNUNET_MessageHeader *) hello;
517 }
518
519
520 static void
521 env_session_end (void *cls,
522                  const struct GNUNET_HELLO_Address *address,
523                  struct GNUNET_ATS_Session *session)
524 {
525
526 }
527
528
529 static void
530 env_update_distance (void *cls,
531                      const struct GNUNET_HELLO_Address *address,
532                      uint32_t distance)
533 {
534 }
535
536
537 static void
538 setup_plugin_environment ()
539 {
540   env.cfg = cfg;
541   env.cls = &env;
542   env.my_identity = &my_identity;
543   env.max_connections = max_connect_per_transport;
544   env.stats = stats;
545   env.receive = &env_receive;
546   env.notify_address = &env_notify_address;
547   env.get_address_type = &env_get_address_type;
548   env.update_address_distance = &env_update_distance;
549   env.get_our_hello = &env_get_our_hello;
550   env.session_end = &env_session_end;
551 }
552
553
554 static int
555 handle_helper_message (void *cls, void *client,
556                        const struct GNUNET_MessageHeader *hdr)
557 {
558   return GNUNET_OK;
559 }
560
561
562 /**
563  * Runs the test.
564  *
565  * @param cls closure
566  * @param c configuration to use
567  */
568 static void
569 run (void *cls, char * const *args, const char *cfgfile,
570     const struct GNUNET_CONFIGURATION_Handle *c)
571 {
572   char * const *argv = cls;
573   unsigned long long tneigh;
574   char *keyfile;
575   char *plugin;
576   char *sep;
577
578   timeout_endbadly = GNUNET_SCHEDULER_add_delayed (TIMEOUT, end_badly, &ok);
579
580   cfg = c;
581   /* parse configuration */
582   if ((GNUNET_OK
583       != GNUNET_CONFIGURATION_get_value_number (c, "TRANSPORT",
584           "NEIGHBOUR_LIMIT", &tneigh))
585       || (GNUNET_OK
586           != GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
587               &keyfile)))
588   {
589     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
590         "Transport service is lacking key configuration settings.  Exiting.\n");
591     return;
592   }
593
594   if (NULL == (stats = GNUNET_STATISTICS_create ("transport", cfg)))
595   {
596     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
597         "Could not create statistics.  Exiting.\n");
598     GNUNET_free(keyfile);
599     end_badly_now ();
600     return;
601   }
602
603   if (GNUNET_OK != GNUNET_DISK_file_test (HOSTKEY_FILE))
604   {
605     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Hostkey `%s' missing.  Exiting.\n",
606         HOSTKEY_FILE);
607     GNUNET_free(keyfile);
608     end_badly_now ();
609     return;
610   }
611
612   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (keyfile))
613   {
614     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
615         "Could not create a directory for hostkey `%s'.  Exiting.\n", keyfile);
616     GNUNET_free(keyfile);
617     end_badly_now ();
618     return;
619   }
620
621   if (GNUNET_OK != GNUNET_DISK_file_copy (HOSTKEY_FILE, keyfile))
622   {
623     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
624         "Could not copy hostkey `%s' to destination `%s'.  Exiting.\n",
625         HOSTKEY_FILE, keyfile);
626     GNUNET_free(keyfile);
627     end_badly_now ();
628     return;
629   }
630
631   max_connect_per_transport = (uint32_t) tneigh;
632   my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
633   GNUNET_free(keyfile);
634   if (NULL == my_private_key)
635   {
636     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
637         "Could not access hostkey.  Exiting.\n");
638     end_badly_now ();
639     return;
640   }
641   GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
642
643   hello = GNUNET_HELLO_create (&my_identity.public_key, NULL, NULL, GNUNET_NO);
644
645   /* load plugins... */
646   setup_plugin_environment ();
647
648   GNUNET_assert(strlen (argv[0]) > strlen ("test_plugin_"));
649   plugin = strstr (argv[0], "test_plugin_");
650   sep = strrchr (argv[0], '.');
651   if (NULL == plugin)
652   {
653     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Not a valid test name\n");
654     end_badly_now ();
655     return;
656   }
657   plugin += strlen ("test_plugin_");
658   if (NULL != sep)
659     sep[0] = '\0';
660
661   /* Hack for WLAN: start a second helper */
662   if (0 == strcmp (plugin, "wlan"))
663   {
664     char * helper_argv[3];
665     helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
666     helper_argv[1] = (char *) "2";
667     helper_argv[2] = NULL;
668     suid_helper = GNUNET_HELPER_start (GNUNET_NO,
669         "gnunet-helper-transport-wlan-dummy", helper_argv,
670         &handle_helper_message, NULL, NULL );
671   }
672
673   /* Loading plugin */
674   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Loading transport plugin %s\n", plugin);
675   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
676   api = GNUNET_PLUGIN_load (libname, &env);
677   if (NULL == api)
678   {
679     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
680         "Failed to load transport plugin for %s\n", plugin);
681     end_badly_now ();
682     return;
683   }
684
685   timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL );
686
687   /* Check if all functions are implemented */
688   if (NULL == api->address_pretty_printer)
689   {
690     GNUNET_break(0);
691     end_badly_now ();
692     return;
693   }
694   if (NULL == api->address_to_string)
695   {
696     GNUNET_break(0);
697     end_badly_now ();
698     return;
699   }
700   GNUNET_assert(NULL != api->check_address);
701   if (NULL == api->check_address)
702   {
703     GNUNET_break(0);
704     end_badly_now ();
705     return;
706   }
707   GNUNET_assert(NULL != api->disconnect_peer);
708   if (NULL == api->disconnect_peer)
709   {
710     GNUNET_break(0);
711     end_badly_now ();
712     return;
713   }
714   GNUNET_assert(NULL != api->get_session);
715   if (NULL == api->get_session)
716   {
717     GNUNET_break(0);
718     end_badly_now ();
719     return;
720   }
721   if (NULL == api->address_pretty_printer)
722   {
723     GNUNET_break(0);
724     end_badly_now ();
725     return;
726   }
727   if (NULL == api->string_to_address)
728   {
729     GNUNET_break(0);
730     end_badly_now ();
731     return;
732   }
733
734 }
735
736 /**
737  * The main function for the test
738  *
739  * @param argc number of arguments from the command line
740  * @param argv command line arguments
741  * @return 0 ok, 1 on error
742  */
743 int
744 main (int argc, char * const *argv)
745 {
746   static struct GNUNET_GETOPT_CommandLineOption options[] = {
747       GNUNET_GETOPT_OPTION_END };
748   int ret;
749
750   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
751
752   char * const argv_prog[] = { "test_plugin_transport", "-c",
753       "test_plugin_transport_data.conf", NULL };
754   GNUNET_log_setup ("test-plugin-transport", "WARNING", NULL );
755   ok = 1; /* set to fail */
756   ret =
757       (GNUNET_OK
758           == GNUNET_PROGRAM_run (3, argv_prog, "test-plugin-transport",
759               "testcase", options, &run, (void *) argv)) ? ok : 1;
760   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport");
761   return ret;
762 }
763
764 /* end of test_plugin_transport.c */