(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, 1)
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 unsigned int timeout_count;
107
108 /**
109  * Did the test pass or fail?
110  */
111 static int fail;
112
113 pid_t pid;
114
115 /**
116  * Initialize Environment for this plugin
117  */
118 static struct GNUNET_TIME_Relative
119 receive (void *cls,
120          const struct GNUNET_PeerIdentity * peer,
121          const struct GNUNET_MessageHeader * message,
122          uint32_t distance,
123          struct Session *session,
124          const char *sender_address,
125          uint16_t sender_address_len)
126 {
127   /* do nothing */
128   return GNUNET_TIME_UNIT_ZERO;
129 }
130
131 void
132 notify_address (void *cls,
133                 const char *name,
134                 const void *addr,
135                 uint16_t addrlen,
136                 struct GNUNET_TIME_Relative expires)
137 {
138
139 }
140
141
142 static void
143 setup_plugin_environment ()
144 {
145   env.cfg = cfg;
146   env.sched = sched;
147   env.stats = stats;
148   env.my_identity = &my_identity;
149   env.cls = &env;
150   env.receive = &receive;
151   env.notify_address = &notify_address;
152   env.max_connections = max_connect_per_transport;
153 }
154
155 /**
156  * Simple example test that invokes
157  * the check_address function of the plugin.
158  */
159 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
160  * isn't enabled (eg. FreeBSD > 4)
161  */
162 static void
163 shutdown_clean ()
164 {
165   GNUNET_assert (NULL ==
166                  GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http",
167                                        api));
168   GNUNET_SCHEDULER_shutdown(sched);
169   return;
170 }
171
172 /**
173  * Task that checks if we should try to download a hostlist.
174  * If so, we initiate the download, otherwise we schedule
175  * this task again for a later time.
176  */
177 static void
178 task_check_stat (void *cls,
179             const struct GNUNET_SCHEDULER_TaskContext *tc)
180 {
181   ti_check_stat = GNUNET_SCHEDULER_NO_TASK;
182   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
183     return;
184
185   if ( timeout_count > 5 )
186   {
187     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n",  timeout_count);
188     fail = GNUNET_YES;
189     shutdown_clean();
190     return;
191   }
192   timeout_count++;
193
194   ti_check_stat = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_check_stat, NULL);
195   return;
196 }
197
198 /**
199  * Runs the test.
200  *
201  * @param cls closure
202  * @param s scheduler to use
203  * @param c configuration to use
204  */
205 static void
206 run (void *cls,
207      struct GNUNET_SCHEDULER_Handle *s,
208      char *const *args,
209      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
210 {
211   char * libname;
212
213   sched = s;
214   cfg = c;
215
216   /* load plugins... */
217   setup_plugin_environment ();
218   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin\n"));
219   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
220
221   api = GNUNET_PLUGIN_load (libname, &env);
222   GNUNET_free (libname);
223   if (api == NULL)
224   {
225     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
226                 _("Failed to load transport plugin for udp\n"));
227     return;
228   }
229
230   ti_check_stat = GNUNET_SCHEDULER_add_now (sched, &task_check_stat, NULL);
231 }
232
233
234 /**
235  * The main function for the transport service.
236  *
237  * @param argc number of arguments from the command line
238  * @param argv command line arguments
239  * @return 0 ok, 1 on error
240  */
241 int
242 main (int argc, char *const *argv)
243 {
244   static struct GNUNET_GETOPT_CommandLineOption options[] = {
245     GNUNET_GETOPT_OPTION_END
246   };
247   char *const argv_prog[] = {
248     "test_plugin_transport_http",
249     "-c",
250     "test_plugin_transport_data_http.conf",
251     "-L",
252 #if VERBOSE
253     "DEBUG",
254 #else
255     "WARNING",
256 #endif
257     NULL
258   };
259   GNUNET_log_setup ("test_plugin_transport_http",
260 #if VERBOSE
261                     "DEBUG",
262 #else
263                     "WARNING",
264 #endif
265                     NULL);
266   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting statistics service\n");
267   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
268                                  "gnunet-service-statistics",
269                                  "-L", "DEBUG",
270                                  "-c", "test_plugin_transport_data_http.conf", NULL);
271
272   fail = GNUNET_YES;
273   if (pid==-1 )
274   {
275     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Failed to start statistics service\n");
276     return fail;
277   }
278
279   fail = (GNUNET_OK ==
280          GNUNET_PROGRAM_run (5,
281                              argv_prog,
282                              "test_plugin_transport_http",
283                              "testcase", options, &run, NULL)) ? GNUNET_YES : 1;
284   GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
285
286
287   if (0 != PLIBC_KILL (pid, SIGTERM))
288   {
289     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "Failed to kill statistics service");
290     fail = GNUNET_YES;
291   }
292   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Killed statistics service\n");
293   return fail;
294 }
295
296 /* end of test_plugin_transport_http.c */