(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 #include <curl/curl.h>
41
42 #define VERBOSE GNUNET_YES
43 #define DEBUG GNUNET_YES
44
45 #define PLUGIN libgnunet_plugin_transport_template
46
47 /**
48  * How long until we give up on transmitting the message?
49  */
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
51
52 /**
53  * How long until we give up on transmitting the message?
54  */
55 #define STAT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
56
57 /**
58  * Our public key.
59  */
60 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
61
62 /**
63  * Our public key.
64  */
65 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
66
67 /**
68  * Our identity.
69  */
70 static struct GNUNET_PeerIdentity my_identity;
71
72 /**
73  * Our private key.
74  */
75 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
76
77
78 /**
79  * Our scheduler.
80  */
81 struct GNUNET_SCHEDULER_Handle *sched;
82
83 /**
84  * Our statistics handle.
85  */
86 struct GNUNET_STATISTICS_Handle *stats;
87
88
89 /**
90  * Our configuration.
91  */
92 const struct GNUNET_CONFIGURATION_Handle *cfg;
93
94 /**
95  * Number of neighbours we'd like to have.
96  */
97 static uint32_t max_connect_per_transport;
98
99 /**
100  * Environment for this plugin.
101  */
102 static struct GNUNET_TRANSPORT_PluginEnvironment env;
103
104 /**
105  *handle for the api provided by this plugin
106  */
107 static struct GNUNET_TRANSPORT_PluginFunctions *api;
108
109 /**
110  * ID of the task controlling the testcase timeout
111  */
112 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
113
114 static unsigned int timeout_count;
115
116 /**
117  * Did the test pass or fail?
118  */
119 static int fail;
120
121 pid_t pid;
122
123 /**
124  * Initialize Environment for this plugin
125  */
126 static struct GNUNET_TIME_Relative
127 receive (void *cls,
128          const struct GNUNET_PeerIdentity * peer,
129          const struct GNUNET_MessageHeader * message,
130          uint32_t distance,
131          struct Session *session,
132          const char *sender_address,
133          uint16_t sender_address_len)
134 {
135   /* do nothing */
136   return GNUNET_TIME_UNIT_ZERO;
137 }
138
139 void
140 notify_address (void *cls,
141                 const char *name,
142                 const void *addr,
143                 uint16_t addrlen,
144                 struct GNUNET_TIME_Relative expires)
145 {
146
147 }
148
149 /**
150  * Simple example test that invokes
151  * the check_address function of the plugin.
152  */
153 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
154  * isn't enabled (eg. FreeBSD > 4)[TESTING]
155 WEAKRANDOM = YES
156  */
157 static void
158 shutdown_clean ()
159 {
160 /*  if (stat_get_handle != NULL)
161   {
162     GNUNET_STATISTICS_get_cancel(stat_get_handle);
163   }*/
164
165   /* if ( NULL!=stats )GNUNET_STATISTICS_destroy (stats, GNUNET_YES); */
166   if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
167     GNUNET_SCHEDULER_cancel(sched,ti_timeout);
168   ti_timeout = GNUNET_SCHEDULER_NO_TASK;
169
170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
171   GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
172   
173   GNUNET_SCHEDULER_shutdown(sched);
174   /* FIXME: */ fail = GNUNET_NO;
175   return;
176 }
177
178 static void
179 setup_plugin_environment ()
180 {
181   env.cfg = cfg;
182   env.sched = sched;
183   env.stats = stats;
184   env.my_identity = &my_identity;
185   env.cls = &env;
186   env.receive = &receive;
187   env.notify_address = &notify_address;
188   env.max_connections = max_connect_per_transport;
189 }
190
191 #if 0
192 static int
193 process_stat (void *cls,
194               const char *subsystem,
195               const char *name,
196               uint64_t value,
197               int is_persistent)
198 {
199   stat_get_handle = NULL;
200   if (value==1)
201     {
202     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin failed \n");
203     fail = GNUNET_YES;
204     shutdown_clean();
205     return GNUNET_YES;
206     }
207   if (value==2)
208     {
209     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin not failed \n");
210     shutdown_clean();
211     return GNUNET_YES;
212     }
213   return GNUNET_YES;
214 }
215 #endif
216
217 #if 0
218 static void
219 cont_func (void *cls, int success)
220 {
221   stat_get_handle = NULL;
222 }
223 #endif
224
225 /**
226  * Task that checks if we should try to download a hostlist.
227  * If so, we initiate the download, otherwise we schedule
228  * this task again for a later time.
229  */
230 static void
231 task_timeout (void *cls,
232             const struct GNUNET_SCHEDULER_TaskContext *tc)
233 {
234   ti_timeout = GNUNET_SCHEDULER_NO_TASK;
235   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
236     return;
237
238   if ( timeout_count > 5 )
239   {
240     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n",  timeout_count);
241     fail = GNUNET_YES;
242     shutdown_clean();
243     return;
244   }
245   timeout_count++;
246
247 /*  stat_get_handle = GNUNET_STATISTICS_get (stats,
248                                            "http-transport",
249                                            gettext_noop("shutdown"),
250                                            GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
251                                            &cont_func,
252                                            &process_stat,
253                                            NULL);*/
254
255   ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_timeout, NULL);
256   return;
257 }
258
259 #if 0
260 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
261 {
262   size_t retcode;
263
264   /* in real-world cases, this would probably get this data differently
265      as this fread() stuff is exactly what the library already would do
266      by default internally */
267   retcode = fread(ptr, size, nmemb, stream);
268
269   fprintf(stderr, "*** We read %d bytes from file\n", (int) retcode);
270
271   return retcode;
272 }
273 #endif
274
275 /**
276  * Runs the test.
277  *
278  * @param cls closure
279  * @param s scheduler to use
280  * @param c configuration to use
281  */
282 static void
283 run (void *cls,
284      struct GNUNET_SCHEDULER_Handle *s,
285      char *const *args,
286      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
287 {
288   char * libname;
289   sched = s;
290   cfg = c;
291   char *keyfile;
292   unsigned long long tneigh;
293
294   /* settings up statistics */
295 /*  stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
296   if (NULL == stats)
297   {
298     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299                 _("Failed to retrieve statistics handle\n"));
300     fail = GNUNET_YES;
301     shutdown_clean();
302     return ;
303   }*/
304
305   /* parse configuration */
306   if ((GNUNET_OK !=
307        GNUNET_CONFIGURATION_get_value_number (c,
308                                               "TRANSPORT",
309                                               "NEIGHBOUR_LIMIT",
310                                               &tneigh)) ||
311       (GNUNET_OK !=
312        GNUNET_CONFIGURATION_get_value_filename (c,
313                                                 "GNUNETD",
314                                                 "HOSTKEY", &keyfile)))
315     {
316       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
317                   _
318                   ("Transport service is lacking key configuration settings.  Exiting.\n"));
319       GNUNET_SCHEDULER_shutdown (s);
320       return;
321     }
322   max_connect_per_transport = (uint32_t) tneigh;
323   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
324   GNUNET_free (keyfile);
325   if (my_private_key == NULL)
326     {
327       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328                   _
329                   ("Transport service could not access hostkey.  Exiting.\n"));
330       GNUNET_SCHEDULER_shutdown (s);
331       return;
332     }
333   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
334   GNUNET_CRYPTO_hash (&my_public_key,
335                       sizeof (my_public_key), &my_identity.hashPubKey);
336
337   /* load plugins... */
338   setup_plugin_environment ();
339   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
340   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
341   api = GNUNET_PLUGIN_load (libname, &env);
342   GNUNET_free (libname);
343   if (api == NULL)
344   {
345     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346                 _("Failed to load transport plugin for http\n"));
347     fail = GNUNET_YES;
348     return;
349   }
350
351   ti_timeout = GNUNET_SCHEDULER_add_now (sched, &task_timeout, NULL);
352   return;
353
354 }
355
356
357 /**
358  * The main function for the transport service.
359  *
360  * @param argc number of arguments from the command line
361  * @param argv command line arguments
362  * @return 0 ok, 1 on error
363  */
364 int
365 main (int argc, char *const *argv)
366 {
367
368   static struct GNUNET_GETOPT_CommandLineOption options[] = {
369     GNUNET_GETOPT_OPTION_END
370   };
371   int ret;
372   char *const argv_prog[] = {
373     "test_plugin_transport_http",
374     "-c",
375     "test_plugin_transport_data_http.conf",
376     "-L",
377 #if VERBOSE
378     "DEBUG",
379 #else
380     "WARNING",
381 #endif
382     NULL
383   };
384   GNUNET_log_setup ("test_plugin_transport_http",
385 #if VERBOSE
386                     "DEBUG",
387 #else
388                     "WARNING",
389 #endif
390                     NULL);
391 /*  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting statistics service\n");
392   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
393                                  "gnunet-service-statistics",
394                                  "-L", "DEBUG",
395                                  "-c", "test_plugin_transport_data_http.conf", NULL);
396
397   fail = GNUNET_NO;
398   if (pid==-1 )
399   {
400     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to start statistics service\n");
401     fail = GNUNET_YES;
402     return fail;
403   }*/
404
405   ret = (GNUNET_OK ==
406          GNUNET_PROGRAM_run (5,
407                              argv_prog,
408                              "test_plugin_transport_http",
409                              "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
410
411     GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
412
413
414 /*  if (0 != PLIBC_KILL (pid, SIGTERM))
415   {
416     GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "Failed to kill statistics service");
417     fail = GNUNET_YES;
418   }
419   if (GNUNET_OS_process_wait(pid) != GNUNET_OK)
420     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
421   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Killed statistics service\n");*/
422   return fail;
423 }
424
425 /* end of test_plugin_transport_http.c */