fixing mess with search update serialization and parenting
[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 "plugin_transport.h"
37 #include "gnunet_statistics_service.h"
38 #include "transport.h"
39
40 #define VERBOSE GNUNET_YES
41 #define DEBUG GNUNET_YES
42
43 /**
44  * How long until we give up on transmitting the message?
45  */
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47
48 /**
49  * Our public key.
50  */
51 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
52
53 /**
54  * Our identity.
55  */
56 static struct GNUNET_PeerIdentity my_identity;
57
58 /**
59  * Our private key.
60  */
61 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
62
63 /**
64  * Our scheduler.
65  */
66 struct GNUNET_SCHEDULER_Handle *sched;
67
68 /**
69  * Our statistics handle.
70  */
71 struct GNUNET_STATISTICS_Handle *stats;
72
73
74 /**
75  * Our configuration.
76  */
77 const struct GNUNET_CONFIGURATION_Handle *cfg;
78
79 /**
80  * Number of neighbours we'd like to have.
81  */
82 static uint32_t max_connect_per_transport;
83
84 /**
85  * Environment for this plugin.
86  */
87 struct GNUNET_TRANSPORT_PluginEnvironment env;
88
89 /**
90  *handle for the api provided by this plugin
91  */
92 struct GNUNET_TRANSPORT_PluginFunctions *api;
93
94 /**
95  * Did the test pass or fail?
96  */
97 static int fail;
98
99 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
100
101 /**
102  * Initialize Environment for this plugin
103  */
104 static struct GNUNET_TIME_Relative
105 receive (void *cls,
106          const struct GNUNET_PeerIdentity * peer,
107          const struct GNUNET_MessageHeader * message,
108          uint32_t distance,
109          struct Session *session,
110          const char *sender_address,
111          uint16_t sender_address_len)
112 {
113   /* do nothing */
114   return GNUNET_TIME_UNIT_ZERO;
115 }
116
117 void
118 notify_address (void *cls,
119                 const char *name,
120                 const void *addr,
121                 uint16_t addrlen, 
122                 struct GNUNET_TIME_Relative expires)
123 {
124 }
125
126 /**
127  * Function called when the service shuts
128  * down.  Unloads our plugins.
129  *
130  * @param cls closure
131  * @param cfg configuration to use
132  */
133 static void
134 unload_plugins (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
135 {
136   GNUNET_assert (NULL ==
137                  GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http",
138                                        api));
139   if (my_private_key != NULL)
140     GNUNET_CRYPTO_rsa_key_free (my_private_key);
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 (timeout_task != GNUNET_SCHEDULER_NO_TASK)
154     GNUNET_SCHEDULER_cancel( sched, timeout_task );
155   unload_plugins(env.cls, env.cfg);
156 }
157
158 /**
159  * Timeout, give up.
160  */
161 static void
162 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 {
164   timeout_task = GNUNET_SCHEDULER_NO_TASK;
165   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
166     return;
167   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168               "Timeout while executing testcase, test failed.\n");
169   fail = GNUNET_YES;
170   shutdown_clean();
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 /**
187  * Runs the test.
188  *
189  * @param cls closure
190  * @param s scheduler to use
191  * @param c configuration to use
192  */
193 static void
194 run (void *cls,
195      struct GNUNET_SCHEDULER_Handle *s,
196      char *const *args,
197      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
198 {
199   unsigned long long tneigh;
200   char *keyfile;
201   char *libname;
202
203   sched = s;
204   cfg = c;
205
206   timeout_task = GNUNET_SCHEDULER_add_delayed ( sched,  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5), &timeout_error, NULL);
207
208   /* parse configuration */
209   if ((GNUNET_OK !=
210        GNUNET_CONFIGURATION_get_value_number (c,
211                                               "TRANSPORT",
212                                               "NEIGHBOUR_LIMIT",
213                                               &tneigh)) ||
214       (GNUNET_OK !=
215        GNUNET_CONFIGURATION_get_value_filename (c,
216                                                 "GNUNETD",
217                                                 "HOSTKEY", &keyfile)))
218     {
219       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220                   _("Transport service is lacking key configuration settings.  Exiting.\n"));
221       GNUNET_SCHEDULER_shutdown (s);
222       return;
223     }
224
225
226   stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
227
228   /*
229   max_connect_per_transport = (uint32_t) tneigh;
230   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
231   GNUNET_free (keyfile);
232   if (my_private_key == NULL)
233     {
234       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235                   _
236                   ("Transport service could not access hostkey.  Exiting.\n"));
237       GNUNET_SCHEDULER_shutdown (s);
238       return;
239     }
240   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
241   GNUNET_CRYPTO_hash (&my_public_key,
242                       sizeof (my_public_key), &my_identity.hashPubKey);*/
243
244   /* load plugins... */
245   setup_plugin_environment ();
246
247   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
248
249   api = GNUNET_PLUGIN_load (libname, &env);
250   if (api != NULL )
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252               "Loading http transport plugin `%s' was successful\n",libname);
253
254   GNUNET_free (libname);
255   if (api == NULL)
256     {
257       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258                   _("Failed to load http transport plugin\n"));
259       fail = GNUNET_YES;
260       return;
261
262     }
263   fail = GNUNET_NO;
264   shutdown_clean ();
265 }
266
267
268 /**
269  * The main function for the transport service.
270  *
271  * @param argc number of arguments from the command line
272  * @param argv command line arguments
273  * @return 0 ok, 1 on error
274  */
275 int
276 main (int argc, char *const *argv)
277 {
278   static struct GNUNET_GETOPT_CommandLineOption options[] = {
279     GNUNET_GETOPT_OPTION_END
280   };
281   int ret;
282   char *const argv_prog[] = {
283     "test-gnunetd-plugin-transport_http",
284     "-c",
285     "test_plugin_transport_data_http.conf",
286     "-L",
287 #if VERBOSE
288     "DEBUG",
289 #else
290     "WARNING",
291 #endif
292     NULL
293   };
294   GNUNET_log_setup ("test-gnunetd-plugin-transport_http",
295 #if VERBOSE
296                     "DEBUG",
297 #else
298                     "WARNING",
299 #endif
300                     NULL);
301   fail = GNUNET_YES;
302   ret = (GNUNET_OK ==
303          GNUNET_PROGRAM_run (5,
304                              argv_prog,
305                              "test-plugin-transport_http",
306                              "testcase", options, &run, NULL)) ? fail : 1;
307   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-plugin-transport_http");
308
309   return fail;
310 }
311
312 /* end of test_plugin_transport_http.c */