(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
41 #define VERBOSE GNUNET_YES
42 #define DEBUG GNUNET_YES
43
44 /**
45  * How long until we give up on transmitting the message?
46  */
47 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
48
49
50 /**
51  * Our public key.
52  */
53 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
54
55 /**
56  * Our identity.
57  */
58 static struct GNUNET_PeerIdentity my_identity;
59
60 /**
61  * Our private key.
62  */
63 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
64
65 /**
66  * Our scheduler.
67  */
68 struct GNUNET_SCHEDULER_Handle *sched;
69
70 /**
71  * Our statistics handle.
72  */
73 struct GNUNET_STATISTICS_Handle *stats;
74
75
76 /**
77  * Our configuration.
78  */
79 const struct GNUNET_CONFIGURATION_Handle *cfg;
80
81 /**
82  * Number of neighbours we'd like to have.
83  */
84 static uint32_t max_connect_per_transport;
85
86 /**
87  * Environment for this plugin.
88  */
89 static struct GNUNET_TRANSPORT_PluginEnvironment env;
90
91 /**
92  *handle for the api provided by this plugin
93  */
94 static struct GNUNET_TRANSPORT_PluginFunctions *api;
95
96 /**
97  * Did the test pass or fail?
98  */
99 static int fail;
100
101 pid_t pid;
102
103 /**
104  * Initialize Environment for this plugin
105  */
106 static struct GNUNET_TIME_Relative
107 receive (void *cls,
108          const struct GNUNET_PeerIdentity * peer,
109          const struct GNUNET_MessageHeader * message,
110          uint32_t distance,
111          struct Session *session,
112          const char *sender_address,
113          uint16_t sender_address_len)
114 {
115   /* do nothing */
116   return GNUNET_TIME_UNIT_ZERO;
117 }
118
119 void
120 notify_address (void *cls,
121                 const char *name,
122                 const void *addr,
123                 uint16_t addrlen, 
124                 struct GNUNET_TIME_Relative expires)
125 {
126 }
127
128 /**
129  * Simple example test that invokes
130  * the check_address function of the plugin.
131  */
132 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
133  * isn't enabled (eg. FreeBSD > 4)
134  */
135 static void
136 shutdown_clean ()
137 {
138   GNUNET_assert (NULL ==
139                  GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http",
140                                        api));
141   if (my_private_key != NULL)
142     GNUNET_CRYPTO_rsa_key_free (my_private_key);
143   GNUNET_SCHEDULER_shutdown(sched);
144   return;
145 }
146
147 static void
148 setup_plugin_environment ()
149 {
150   env.cfg = cfg;
151   env.sched = sched;
152   env.stats = stats;
153   env.my_identity = &my_identity;
154   env.cls = &env;
155   env.receive = &receive;
156   env.notify_address = &notify_address;
157   env.max_connections = max_connect_per_transport;
158 }
159
160 static int
161 process_stat (void *cls,
162               const char *subsystem,
163               const char *name,
164               uint64_t value,
165               int is_persistent)
166 {
167   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
168               _("Value: %llums\n"),
169               (unsigned long long) value);
170   return GNUNET_OK;
171 }
172
173
174 /**
175  * Runs the test.
176  *
177  * @param cls closure
178  * @param s scheduler to use
179  * @param c configuration to use
180  */
181 static void
182 run (void *cls,
183      struct GNUNET_SCHEDULER_Handle *s,
184      char *const *args,
185      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
186 {
187   unsigned long long tneigh;
188   char *keyfile;
189   char *libname;
190
191   sched = s;
192   cfg = c;
193
194   /* parse configuration */
195   if ((GNUNET_OK !=
196        GNUNET_CONFIGURATION_get_value_number (c,
197                                               "TRANSPORT",
198                                               "NEIGHBOUR_LIMIT",
199                                               &tneigh)) ||
200       (GNUNET_OK !=
201        GNUNET_CONFIGURATION_get_value_filename (c,
202                                                 "GNUNETD",
203                                                 "HOSTKEY", &keyfile)))
204     {
205       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206                   _("Transport service is lacking key configuration settings.  Exiting.\n"));
207       GNUNET_SCHEDULER_shutdown (s);
208       return;
209     }
210
211   pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
212                                  "gnunet-service-statistics",
213                                  "-L", "DEBUG",
214                                  "-c", "test_plugin_transport_data_http.conf", NULL);
215
216
217   if ( pid == -1)
218   {
219     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220                      _("Failed to start service for `%s' http transport plugin test.\n"),
221                      "statistics");
222     GNUNET_SCHEDULER_shutdown (s);
223     return;
224   }
225
226   stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
227   env.stats = stats;
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
233   if (my_private_key == NULL)
234     {
235       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236                   _
237                   ("Transport service could not access hostkey.  Exiting.\n"));
238       GNUNET_SCHEDULER_shutdown (s);
239       return;
240     }
241   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
242   GNUNET_CRYPTO_hash (&my_public_key,
243                       sizeof (my_public_key), &my_identity.hashPubKey);
244
245   /* load plugins... */
246   setup_plugin_environment ();
247
248   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
249
250   api = GNUNET_PLUGIN_load (libname, &env);
251   if (api != NULL )
252   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253               "Loading http transport plugin `%s' was successful\n",libname);
254
255   GNUNET_free (libname);
256   if (api == NULL)
257     {
258       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259                   _("Failed to load http transport plugin\n"));
260       fail = GNUNET_YES;
261       shutdown_clean ();
262       return;
263
264     }
265   fail = GNUNET_NO;
266
267   char * test_message  = "Hello World!";
268   size_t size = strlen(test_message) +1;
269
270   /* Testing to send */
271   api->send(NULL, &my_identity,test_message,size,0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, NULL, NULL);
272   shutdown_clean ();
273   return;
274 }
275
276
277 /**
278  * The main function for the transport service.
279  *
280  * @param argc number of arguments from the command line
281  * @param argv command line arguments
282  * @return 0 ok, 1 on error
283  */
284 int
285 main (int argc, char *const *argv)
286 {
287   static struct GNUNET_GETOPT_CommandLineOption options[] = {
288     GNUNET_GETOPT_OPTION_END
289   };
290   int ret;
291   char *const argv_prog[] = {
292     "test_plugin_transport_http",
293     "-c",
294     "test_plugin_transport_data_http.conf",
295     "-L",
296 #if VERBOSE
297     "DEBUG",
298 #else
299     "WARNING",
300 #endif
301     NULL
302   };
303   GNUNET_log_setup ("test_plugin_transport_http",
304 #if VERBOSE
305                     "DEBUG",
306 #else
307                     "WARNING",
308 #endif
309                     NULL);
310   fail = GNUNET_YES;
311   ret = (GNUNET_OK ==
312          GNUNET_PROGRAM_run (5,
313                              argv_prog,
314                              "test_plugin_transport_http",
315                              "testcase", options, &run, NULL)) ? fail : 1;
316   GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
317
318   if (0 != PLIBC_KILL (pid, SIGTERM))
319   {
320     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
321     fail = 1;
322   }
323   return fail;
324 }
325
326 /* end of test_plugin_transport_http.c */