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