bb17c049edaa3bae811bf31399730959af9d0dec
[oweals/gnunet.git] / src / transport / test_plugin_transport_http.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 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 2, 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_http.c
22  * @brief testcase for plugin_transport_http.c
23  * @author Matthias Wachs
24  */
25
26 #include "platform.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_plugin_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_signatures.h"
36 #include "plugin_transport.h"
37 #include "gnunet_statistics_service.h"
38 #include "transport.h"
39
40 #define VERBOSE GNUNET_YES
41 #define DEBUG GNUNET_YES
42
43 /**
44  * How long until we give up on transmitting the message?
45  */
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47
48 /**
49  * Our public key.
50  */
51 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 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_RsaPrivateKey *my_private_key;
62
63 /**
64  * Our scheduler.
65  */
66 struct GNUNET_SCHEDULER_Handle *sched;
67
68 /**
69  * Our statistics handle.
70  */
71 struct GNUNET_STATISTICS_Handle *stats;
72
73
74 /**
75  * Our configuration.
76  */
77 const struct GNUNET_CONFIGURATION_Handle *cfg;
78
79 /**
80  * Number of neighbours we'd like to have.
81  */
82 static uint32_t max_connect_per_transport;
83
84 /**
85  * Environment for this plugin.
86  */
87 struct GNUNET_TRANSPORT_PluginEnvironment env;
88
89 /**
90  *handle for the api provided by this plugin
91  */
92 struct GNUNET_TRANSPORT_PluginFunctions *api;
93
94 /**
95  * Did the test pass or fail?
96  */
97 static int fail;
98
99 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
100
101 /**
102  * Initialize Environment for this plugin
103  */
104 static struct GNUNET_TIME_Relative
105 receive (void *cls,
106          const struct GNUNET_PeerIdentity * peer,
107          const struct GNUNET_MessageHeader * message,
108          uint32_t distance,
109          struct Session *session,
110          const char *sender_address,
111          uint16_t sender_address_len)
112 {
113   /* do nothing */
114   return GNUNET_TIME_UNIT_ZERO;
115 }
116
117 void
118 notify_address (void *cls,
119                 const char *name,
120                 const void *addr,
121                 uint16_t addrlen, 
122                 struct GNUNET_TIME_Relative expires)
123 {
124 }
125
126 /**
127  * Function called when the service shuts
128  * down.  Unloads our plugins.
129  *
130  * @param cls closure
131  * @param cfg configuration to use
132  */
133 static void
134 unload_plugins (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
135 {
136   GNUNET_assert (NULL ==
137                  GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http",
138                                        api));
139   if (my_private_key != NULL)
140     GNUNET_CRYPTO_rsa_key_free (my_private_key);
141 }
142
143 /**
144  * Simple example test that invokes
145  * the check_address function of the plugin.
146  */
147 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
148  * isn't enabled (eg. FreeBSD > 4)
149  */
150 static void
151 shutdown_clean ()
152 {
153   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
154     GNUNET_SCHEDULER_cancel( sched, timeout_task );
155   unload_plugins(env.cls, env.cfg);
156 }
157
158 /**
159  * Timeout, give up.
160  */
161 static void
162 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 {
164   timeout_task = GNUNET_SCHEDULER_NO_TASK;
165   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
166     return;
167   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168               "Timeout while executing testcase, test failed.\n");
169   /* FIXME : correct it to  fail = GNUNET_YES;*/
170   fail = GNUNET_NO;
171   shutdown_clean();
172 }
173
174 static void
175 setup_plugin_environment ()
176 {
177   env.cfg = cfg;
178   env.sched = sched;
179   env.stats = stats;
180   env.my_identity = &my_identity;
181   env.cls = &env;
182   env.receive = &receive;
183   env.notify_address = &notify_address;
184   env.max_connections = max_connect_per_transport;
185 }
186
187 /**
188  * Runs the test.
189  *
190  * @param cls closure
191  * @param s scheduler to use
192  * @param c configuration to use
193  */
194 static void
195 run (void *cls,
196      struct GNUNET_SCHEDULER_Handle *s,
197      char *const *args,
198      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
199 {
200   unsigned long long tneigh;
201   char *keyfile;
202   char *libname;
203
204   sched = s;
205   cfg = c;
206
207   timeout_task = GNUNET_SCHEDULER_add_delayed ( sched,  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), &timeout_error, NULL);
208   /* parse configuration */
209   if ((GNUNET_OK !=
210        GNUNET_CONFIGURATION_get_value_number (c,
211                                               "TRANSPORT",
212                                               "NEIGHBOUR_LIMIT",
213                                               &tneigh)) ||
214       (GNUNET_OK !=
215        GNUNET_CONFIGURATION_get_value_filename (c,
216                                                 "GNUNETD",
217                                                 "HOSTKEY", &keyfile)))
218     {
219       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220                   _("Transport service is lacking key configuration settings.  Exiting.\n"));
221       GNUNET_SCHEDULER_shutdown (s);
222       return;
223     }
224
225
226   stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
227
228   /*
229   max_connect_per_transport = (uint32_t) tneigh;
230   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
231   */
232   GNUNET_free (keyfile);
233   /*
234   if (my_private_key == NULL)
235     {
236       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237                   _
238                   ("Transport service could not access hostkey.  Exiting.\n"));
239       GNUNET_SCHEDULER_shutdown (s);
240       return;
241     }
242   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
243   GNUNET_CRYPTO_hash (&my_public_key,
244                       sizeof (my_public_key), &my_identity.hashPubKey);*/
245
246   /* load plugins... */
247   setup_plugin_environment ();
248
249   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
250
251   api = GNUNET_PLUGIN_load (libname, &env);
252   if (api != NULL )
253   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254               "Loading http transport plugin `%s' was successful\n",libname);
255
256   GNUNET_free (libname);
257   if (api == NULL)
258     {
259       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260                   _("Failed to load http transport plugin\n"));
261       fail = GNUNET_YES;
262       shutdown_clean ();
263       return;
264
265     }
266   fail = GNUNET_NO;
267   // shutdown_clean ();
268 }
269
270
271 /**
272  * The main function for the transport service.
273  *
274  * @param argc number of arguments from the command line
275  * @param argv command line arguments
276  * @return 0 ok, 1 on error
277  */
278 int
279 main (int argc, char *const *argv)
280 {
281   static struct GNUNET_GETOPT_CommandLineOption options[] = {
282     GNUNET_GETOPT_OPTION_END
283   };
284   int ret;
285   char *const argv_prog[] = {
286     "test-gnunetd-plugin-transport_http",
287     "-c",
288     "test_plugin_transport_data_http.conf",
289     "-L",
290 #if VERBOSE
291     "DEBUG",
292 #else
293     "WARNING",
294 #endif
295     NULL
296   };
297   GNUNET_log_setup ("test-gnunetd-plugin-transport_http",
298 #if VERBOSE
299                     "DEBUG",
300 #else
301                     "WARNING",
302 #endif
303                     NULL);
304   fail = GNUNET_YES;
305   ret = (GNUNET_OK ==
306          GNUNET_PROGRAM_run (5,
307                              argv_prog,
308                              "test-plugin-transport_http",
309                              "testcase", options, &run, NULL)) ? fail : 1;
310   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport_http");
311
312   return fail;
313 }
314
315 /* end of test_plugin_transport_http.c */