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