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