(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, 30)
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 identity.
64  */
65 static struct GNUNET_PeerIdentity my_identity;
66
67 #if 0
68 /**
69  * Our private key.
70  */
71 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
72 #endif
73
74 /**
75  * Our scheduler.
76  */
77 struct GNUNET_SCHEDULER_Handle *sched;
78
79 /**
80  * Our statistics handle.
81  */
82 struct GNUNET_STATISTICS_Handle *stats;
83
84
85 /**
86  * Our configuration.
87  */
88 const struct GNUNET_CONFIGURATION_Handle *cfg;
89
90 /**
91  * Number of neighbours we'd like to have.
92  */
93 static uint32_t max_connect_per_transport;
94
95 /**
96  * Environment for this plugin.
97  */
98 static struct GNUNET_TRANSPORT_PluginEnvironment env;
99
100 /**
101  *handle for the api provided by this plugin
102  */
103 static struct GNUNET_TRANSPORT_PluginFunctions *api;
104
105 /**
106  * ID of the task controlling the locking between two hostlist tests
107  */
108 static GNUNET_SCHEDULER_TaskIdentifier ti_check_stat;
109
110 static struct GNUNET_STATISTICS_GetHandle * stat_get_handle;
111
112 static unsigned int timeout_count;
113
114 /**
115  * Did the test pass or fail?
116  */
117 static int fail;
118
119 pid_t pid;
120
121 /**
122  * Initialize Environment for this plugin
123  */
124 static struct GNUNET_TIME_Relative
125 receive (void *cls,
126          const struct GNUNET_PeerIdentity * peer,
127          const struct GNUNET_MessageHeader * message,
128          uint32_t distance,
129          struct Session *session,
130          const char *sender_address,
131          uint16_t sender_address_len)
132 {
133   /* do nothing */
134   return GNUNET_TIME_UNIT_ZERO;
135 }
136
137 void
138 notify_address (void *cls,
139                 const char *name,
140                 const void *addr,
141                 uint16_t addrlen,
142                 struct GNUNET_TIME_Relative expires)
143 {
144
145 }
146
147 /**
148  * Simple example test that invokes
149  * the check_address function of the plugin.
150  */
151 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
152  * isn't enabled (eg. FreeBSD > 4)
153  */
154 static void
155 shutdown_clean ()
156 {
157 /*  if (stat_get_handle != NULL)
158   {
159     GNUNET_STATISTICS_get_cancel(stat_get_handle);
160   }*/
161
162   /* if ( NULL!=stats )GNUNET_STATISTICS_destroy (stats, GNUNET_YES); */
163   if (ti_check_stat != GNUNET_SCHEDULER_NO_TASK)
164     GNUNET_SCHEDULER_cancel(sched,ti_check_stat);
165   ti_check_stat = GNUNET_SCHEDULER_NO_TASK;
166
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
168   GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
169   
170   GNUNET_SCHEDULER_shutdown(sched);
171   /* FIXME: */ fail = GNUNET_NO;
172   return;
173 }
174
175 static void
176 setup_plugin_environment ()
177 {
178   env.cfg = cfg;
179   env.sched = sched;
180   env.stats = stats;
181   env.my_identity = &my_identity;
182   env.cls = &env;
183   env.receive = &receive;
184   env.notify_address = &notify_address;
185   env.max_connections = max_connect_per_transport;
186 }
187
188 #if 0
189 static int
190 process_stat (void *cls,
191               const char *subsystem,
192               const char *name,
193               uint64_t value,
194               int is_persistent)
195 {
196   stat_get_handle = NULL;
197   if (value==1)
198     {
199     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin failed \n");
200     fail = GNUNET_YES;
201     shutdown_clean();
202     return GNUNET_YES;
203     }
204   if (value==2)
205     {
206     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin not failed \n");
207     shutdown_clean();
208     return GNUNET_YES;
209     }
210   return GNUNET_YES;
211 }
212 #endif
213
214 #if 0
215 static void
216 cont_func (void *cls, int success)
217 {
218   stat_get_handle = NULL;
219 }
220
221 /**
222  * Task that checks if we should try to download a hostlist.
223  * If so, we initiate the download, otherwise we schedule
224  * this task again for a later time.
225  */
226 static void
227 task_check_stat (void *cls,
228             const struct GNUNET_SCHEDULER_TaskContext *tc)
229 {
230   ti_check_stat = GNUNET_SCHEDULER_NO_TASK;
231   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
232     return;
233
234   if ( timeout_count > 2 )
235   {
236     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n",  timeout_count);
237     fail = GNUNET_YES;
238     shutdown_clean();
239     return;
240   }
241   timeout_count++;
242
243 /*  stat_get_handle = GNUNET_STATISTICS_get (stats,
244                                            "http-transport",
245                                            gettext_noop("shutdown"),
246                                            GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
247                                            &cont_func,
248                                            &process_stat,
249                                            NULL);*/
250
251   ti_check_stat = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_check_stat, NULL);
252   return;
253 }
254 #endif
255
256 /**
257  * Runs the test.
258  *
259  * @param cls closure
260  * @param s scheduler to use
261  * @param c configuration to use
262  */
263 static void
264 run (void *cls,
265      struct GNUNET_SCHEDULER_Handle *s,
266      char *const *args,
267      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
268 {
269   char * libname;
270   sched = s;
271   cfg = c;
272
273   /* settings up statistics */
274 /*  stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
275   if (NULL == stats)
276   {
277     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
278                 _("Failed to retrieve statistics handle\n"));
279     fail = GNUNET_YES;
280     shutdown_clean();
281     return ;
282   }*/
283
284   /* load plugins... */
285   setup_plugin_environment ();
286   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
287   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
288   api = GNUNET_PLUGIN_load (libname, &env);
289   GNUNET_free (libname);
290   if (api == NULL)
291   {
292     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293                 _("Failed to load transport plugin for http\n"));
294     fail = GNUNET_YES;
295     return;
296   }
297
298   ti_check_stat = GNUNET_SCHEDULER_add_now (sched, &task_check_stat, NULL);
299 }
300
301
302 /**
303  * The main function for the transport service.
304  *
305  * @param argc number of arguments from the command line
306  * @param argv command line arguments
307  * @return 0 ok, 1 on error
308  */
309 int
310 main (int argc, char *const *argv)
311 {
312
313   static struct GNUNET_GETOPT_CommandLineOption options[] = {
314     GNUNET_GETOPT_OPTION_END
315   };
316   int ret;
317   char *const argv_prog[] = {
318     "test_plugin_transport_http",
319     "-c",
320     "test_plugin_transport_data_http.conf",
321     "-L",
322 #if VERBOSE
323     "DEBUG",
324 #else
325     "WARNING",
326 #endif
327     NULL
328   };
329   GNUNET_log_setup ("test_plugin_transport_http",
330 #if VERBOSE
331                     "DEBUG",
332 #else
333                     "WARNING",
334 #endif
335                     NULL);
336 /*  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting statistics service\n");
337   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
338                                  "gnunet-service-statistics",
339                                  "-L", "DEBUG",
340                                  "-c", "test_plugin_transport_data_http.conf", NULL);
341
342   fail = GNUNET_NO;
343   if (pid==-1 )
344   {
345     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to start statistics service\n");
346     fail = GNUNET_YES;
347     return fail;
348   }*/
349
350   ret = (GNUNET_OK ==
351          GNUNET_PROGRAM_run (5,
352                              argv_prog,
353                              "test_plugin_transport_http",
354                              "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
355   GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
356
357 /*  if (0 != PLIBC_KILL (pid, SIGTERM))
358   {
359     GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "Failed to kill statistics service");
360     fail = GNUNET_YES;
361   }
362   if (GNUNET_OS_process_wait(pid) != GNUNET_OK)
363     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
364   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Killed statistics service\n");*/
365   return fail;
366 }
367
368 /* end of test_plugin_transport_http.c */