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