(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 unsigned int timeout_count;
111
112 /**
113  * Did the test pass or fail?
114  */
115 static int fail;
116
117 pid_t pid;
118
119 /**
120  * Initialize Environment for this plugin
121  */
122 static struct GNUNET_TIME_Relative
123 receive (void *cls,
124          const struct GNUNET_PeerIdentity * peer,
125          const struct GNUNET_MessageHeader * message,
126          uint32_t distance,
127          struct Session *session,
128          const char *sender_address,
129          uint16_t sender_address_len)
130 {
131   /* do nothing */
132   return GNUNET_TIME_UNIT_ZERO;
133 }
134
135 void
136 notify_address (void *cls,
137                 const char *name,
138                 const void *addr,
139                 uint16_t addrlen,
140                 struct GNUNET_TIME_Relative expires)
141 {
142
143 }
144
145 /**
146  * Simple example test that invokes
147  * the check_address function of the plugin.
148  */
149 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
150  * isn't enabled (eg. FreeBSD > 4)
151  */
152 static void
153 shutdown_clean ()
154 {
155 /*  if (stat_get_handle != NULL)
156   {
157     GNUNET_STATISTICS_get_cancel(stat_get_handle);
158   }*/
159
160   /* if ( NULL!=stats )GNUNET_STATISTICS_destroy (stats, GNUNET_YES); */
161   if (ti_check_stat != GNUNET_SCHEDULER_NO_TASK)
162     GNUNET_SCHEDULER_cancel(sched,ti_check_stat);
163   ti_check_stat = GNUNET_SCHEDULER_NO_TASK;
164
165   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
166   GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
167   
168   GNUNET_SCHEDULER_shutdown(sched);
169   /* FIXME: */ fail = GNUNET_NO;
170   return;
171 }
172
173 static void
174 setup_plugin_environment ()
175 {
176   env.cfg = cfg;
177   env.sched = sched;
178   env.stats = stats;
179   env.my_identity = &my_identity;
180   env.cls = &env;
181   env.receive = &receive;
182   env.notify_address = &notify_address;
183   env.max_connections = max_connect_per_transport;
184 }
185
186 #if 0
187 static int
188 process_stat (void *cls,
189               const char *subsystem,
190               const char *name,
191               uint64_t value,
192               int is_persistent)
193 {
194   stat_get_handle = NULL;
195   if (value==1)
196     {
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin failed \n");
198     fail = GNUNET_YES;
199     shutdown_clean();
200     return GNUNET_YES;
201     }
202   if (value==2)
203     {
204     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin not failed \n");
205     shutdown_clean();
206     return GNUNET_YES;
207     }
208   return GNUNET_YES;
209 }
210 #endif
211
212 #if 0
213 static void
214 cont_func (void *cls, int success)
215 {
216   stat_get_handle = NULL;
217 }
218 #endif
219
220 /**
221  * Task that checks if we should try to download a hostlist.
222  * If so, we initiate the download, otherwise we schedule
223  * this task again for a later time.
224  */
225 static void
226 task_check_stat (void *cls,
227             const struct GNUNET_SCHEDULER_TaskContext *tc)
228 {
229   ti_check_stat = GNUNET_SCHEDULER_NO_TASK;
230   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
231     return;
232
233   if ( timeout_count > 5 )
234   {
235     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n",  timeout_count);
236     fail = GNUNET_YES;
237     shutdown_clean();
238     return;
239   }
240   timeout_count++;
241
242 /*  stat_get_handle = GNUNET_STATISTICS_get (stats,
243                                            "http-transport",
244                                            gettext_noop("shutdown"),
245                                            GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2),
246                                            &cont_func,
247                                            &process_stat,
248                                            NULL);*/
249
250   ti_check_stat = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_check_stat, NULL);
251   return;
252 }
253
254 #if 0
255 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
256 {
257   size_t retcode;
258
259   /* in real-world cases, this would probably get this data differently
260      as this fread() stuff is exactly what the library already would do
261      by default internally */
262   retcode = fread(ptr, size, nmemb, stream);
263
264   fprintf(stderr, "*** We read %d bytes from file\n", (int) retcode);
265
266   return retcode;
267 }
268 #endif
269
270 /**
271  * Runs the test.
272  *
273  * @param cls closure
274  * @param s scheduler to use
275  * @param c configuration to use
276  */
277 static void
278 run (void *cls,
279      struct GNUNET_SCHEDULER_Handle *s,
280      char *const *args,
281      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
282 {
283   char * libname;
284   sched = s;
285   cfg = c;
286
287   /* settings up statistics */
288 /*  stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
289   if (NULL == stats)
290   {
291     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292                 _("Failed to retrieve statistics handle\n"));
293     fail = GNUNET_YES;
294     shutdown_clean();
295     return ;
296   }*/
297
298   /* load plugins... */
299   setup_plugin_environment ();
300   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
301   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
302   api = GNUNET_PLUGIN_load (libname, &env);
303   GNUNET_free (libname);
304   if (api == NULL)
305   {
306     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307                 _("Failed to load transport plugin for http\n"));
308     fail = GNUNET_YES;
309     return;
310   }
311
312   ti_check_stat = GNUNET_SCHEDULER_add_now (sched, &task_check_stat, NULL);
313   return;
314
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
329   static struct GNUNET_GETOPT_CommandLineOption options[] = {
330     GNUNET_GETOPT_OPTION_END
331   };
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 /*  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting statistics service\n");
353   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
354                                  "gnunet-service-statistics",
355                                  "-L", "DEBUG",
356                                  "-c", "test_plugin_transport_data_http.conf", NULL);
357
358   fail = GNUNET_NO;
359   if (pid==-1 )
360   {
361     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to start statistics service\n");
362     fail = GNUNET_YES;
363     return fail;
364   }*/
365
366   ret = (GNUNET_OK ==
367          GNUNET_PROGRAM_run (5,
368                              argv_prog,
369                              "test_plugin_transport_http",
370                              "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
371   GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
372
373 /*  if (0 != PLIBC_KILL (pid, SIGTERM))
374   {
375     GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "Failed to kill statistics service");
376     fail = GNUNET_YES;
377   }
378   if (GNUNET_OS_process_wait(pid) != GNUNET_OK)
379     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
380   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Killed statistics service\n");*/
381   return fail;
382 }
383
384 /* end of test_plugin_transport_http.c */