2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file transport/test_plugin_transport_http.c
22 * @brief testcase for plugin_transport_http.c
23 * @author Matthias Wachs
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>
42 #define VERBOSE GNUNET_YES
43 #define DEBUG GNUNET_YES
46 * How long until we give up on transmitting the message?
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
51 * How long until we give up on transmitting the message?
53 #define STAT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
58 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
63 static struct GNUNET_PeerIdentity my_identity;
68 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
73 struct GNUNET_SCHEDULER_Handle *sched;
76 * Our statistics handle.
78 struct GNUNET_STATISTICS_Handle *stats;
84 const struct GNUNET_CONFIGURATION_Handle *cfg;
87 * Number of neighbours we'd like to have.
89 static uint32_t max_connect_per_transport;
92 * Environment for this plugin.
94 static struct GNUNET_TRANSPORT_PluginEnvironment env;
97 *handle for the api provided by this plugin
99 static struct GNUNET_TRANSPORT_PluginFunctions *api;
102 * Did the test pass or fail?
109 * ID of the task controlling the locking between two hostlist tests
111 static GNUNET_SCHEDULER_TaskIdentifier ti_check_stat;
113 static unsigned int timeout_count;
116 * Initialize Environment for this plugin
118 static struct GNUNET_TIME_Relative
120 const struct GNUNET_PeerIdentity * peer,
121 const struct GNUNET_MessageHeader * message,
123 struct Session *session,
124 const char *sender_address,
125 uint16_t sender_address_len)
128 return GNUNET_TIME_UNIT_ZERO;
132 notify_address (void *cls,
136 struct GNUNET_TIME_Relative expires)
141 * Simple example test that invokes
142 * the check_address function of the plugin.
144 /* FIXME: won't work on IPv6 enabled systems where IPv4 mapping
145 * isn't enabled (eg. FreeBSD > 4)
150 GNUNET_assert (NULL ==
151 GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http",
153 if (my_private_key != NULL)
154 GNUNET_CRYPTO_rsa_key_free (my_private_key);
156 if (ti_check_stat != GNUNET_SCHEDULER_NO_TASK)
157 GNUNET_SCHEDULER_cancel(sched, ti_check_stat);
158 GNUNET_SCHEDULER_shutdown(sched);
163 setup_plugin_environment ()
168 env.my_identity = &my_identity;
170 env.receive = &receive;
171 env.notify_address = ¬ify_address;
172 env.max_connections = max_connect_per_transport;
176 process_stat (void *cls,
177 const char *subsystem,
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin failed \n");
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown, plugin not failed \n");
200 * Task that checks if we should try to download a hostlist.
201 * If so, we initiate the download, otherwise we schedule
202 * this task again for a later time.
205 task_check_stat (void *cls,
206 const struct GNUNET_SCHEDULER_TaskContext *tc)
209 ti_check_stat = GNUNET_SCHEDULER_NO_TASK;
210 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
213 if ( timeout_count > 10 )
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n", timeout_count);
222 GNUNET_STATISTICS_get (stats,
224 gettext_noop("shutdown"),
225 GNUNET_TIME_UNIT_MINUTES,
230 ti_check_stat = GNUNET_SCHEDULER_add_delayed (sched, STAT_INTERVALL, &task_check_stat, NULL);
238 * @param s scheduler to use
239 * @param c configuration to use
243 struct GNUNET_SCHEDULER_Handle *s,
245 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
247 unsigned long long tneigh;
254 /* parse configuration */
256 GNUNET_CONFIGURATION_get_value_number (c,
261 GNUNET_CONFIGURATION_get_value_filename (c,
263 "HOSTKEY", &keyfile)))
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 _("Transport service is lacking key configuration settings. Exiting.\n"));
267 GNUNET_SCHEDULER_shutdown (s);
271 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
272 "gnunet-service-statistics",
274 "-c", "test_plugin_transport_data_http.conf", NULL);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 _("Failed to start service for `%s' http transport plugin test.\n"),
282 GNUNET_SCHEDULER_shutdown (s);
286 stats = GNUNET_STATISTICS_create (sched, "http-transport", cfg);
289 max_connect_per_transport = (uint32_t) tneigh;
290 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
291 GNUNET_free (keyfile);
293 if (my_private_key == NULL)
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 ("Transport service could not access hostkey. Exiting.\n"));
298 GNUNET_SCHEDULER_shutdown (s);
301 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
302 GNUNET_CRYPTO_hash (&my_public_key,
303 sizeof (my_public_key), &my_identity.hashPubKey);
305 /* load plugins... */
306 setup_plugin_environment ();
308 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
310 api = GNUNET_PLUGIN_load (libname, &env);
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Loading http transport plugin `%s' was successful\n",libname);
315 GNUNET_free (libname);
318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
319 _("Failed to load http transport plugin\n"));
327 char * test_message = "Hello World!";
329 size_t size = strlen(test_message) +1;
331 /* Testing to send */
332 bs = api->send(NULL, &my_identity,test_message,size,0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, NULL, NULL);
333 GNUNET_assert ( bs == size);
335 /* check statistics */
336 ti_check_stat = GNUNET_SCHEDULER_add_now(sched, &task_check_stat, NULL);
343 * The main function for the transport service.
345 * @param argc number of arguments from the command line
346 * @param argv command line arguments
347 * @return 0 ok, 1 on error
350 main (int argc, char *const *argv)
353 static struct GNUNET_GETOPT_CommandLineOption options[] = {
354 GNUNET_GETOPT_OPTION_END
357 char *const argv_prog[] = {
358 "test_plugin_transport_http",
360 "test_plugin_transport_data_http.conf",
369 GNUNET_log_setup ("test_plugin_transport_http",
378 GNUNET_PROGRAM_run (5,
380 "test_plugin_transport_http",
381 "testcase", options, &run, NULL)) ? fail : 1;
382 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
384 if (0 != PLIBC_KILL (pid, SIGTERM))
386 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
392 /* end of test_plugin_transport_http.c */