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
45 #define PLUGIN libgnunet_plugin_transport_template
48 * How long until we give up on transmitting the message?
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
53 * How long until we give up on transmitting the message?
55 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
58 * How long between recieve and send?
60 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
65 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
70 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
75 static struct GNUNET_PeerIdentity my_identity;
80 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
86 struct GNUNET_SCHEDULER_Handle *sched;
89 * Our statistics handle.
91 struct GNUNET_STATISTICS_Handle *stats;
97 const struct GNUNET_CONFIGURATION_Handle *cfg;
100 * Number of neighbours we'd like to have.
102 static uint32_t max_connect_per_transport;
105 * Environment for this plugin.
107 static struct GNUNET_TRANSPORT_PluginEnvironment env;
110 *handle for the api provided by this plugin
112 static struct GNUNET_TRANSPORT_PluginFunctions *api;
115 * ID of the task controlling the testcase timeout
117 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
120 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
122 const struct GNUNET_PeerIdentity * p;
125 * Did the test pass or fail?
130 * Recieved message already returned to sender?
140 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
142 GNUNET_SCHEDULER_cancel(sched,ti_send);
143 ti_send = GNUNET_SCHEDULER_NO_TASK;
146 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
148 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
149 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
153 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
155 GNUNET_SCHEDULER_shutdown(sched);
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
162 * Continuation called after plugin send message
165 * @result GNUNET_OK or GNUNET_SYSERR
167 static void task_send_cont (void *cls,
168 const struct GNUNET_PeerIdentity * target,
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
177 * Task sending recieved message back to peer
182 task_send (void *cls,
183 const struct GNUNET_SCHEDULER_TaskContext *tc)
185 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
186 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
189 if (GNUNET_YES==sent)
192 struct GNUNET_MessageHeader * msg = cls;
193 unsigned int len = ntohs(msg->size);
194 const char * msgc = (const char *) msg;
196 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
202 * Recieves messages from plugin, in real world transport
204 static struct GNUNET_TIME_Relative
206 const struct GNUNET_PeerIdentity * peer,
207 const struct GNUNET_MessageHeader * message,
209 struct Session *session,
210 const char *sender_address,
211 uint16_t sender_address_len)
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase recieved new message from peer `%s' with type %u and length %u\n", GNUNET_i2s(peer),ntohs(message->type),ntohs(message->size));
215 /* take recieved message and send it back to peer */
217 void * c = (void *) message;
218 ti_send =GNUNET_SCHEDULER_add_delayed (sched, WAIT_INTERVALL, &task_send, c);
220 return GNUNET_TIME_UNIT_ZERO;
224 notify_address (void *cls,
228 struct GNUNET_TIME_Relative expires)
234 * Setup plugin environment
237 setup_plugin_environment ()
242 env.my_identity = &my_identity;
244 env.receive = &receive;
245 env.notify_address = ¬ify_address;
246 env.max_connections = max_connect_per_transport;
251 * Task shutting down testcase if it a timeout occurs
254 task_timeout (void *cls,
255 const struct GNUNET_SCHEDULER_TaskContext *tc)
257 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
258 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
272 * @param s scheduler to use
273 * @param c configuration to use
277 struct GNUNET_SCHEDULER_Handle *s,
279 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
285 unsigned long long tneigh;
287 /* parse configuration */
289 GNUNET_CONFIGURATION_get_value_number (c,
294 GNUNET_CONFIGURATION_get_value_filename (c,
296 "HOSTKEY", &keyfile)))
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300 ("Transport service is lacking key configuration settings. Exiting.\n"));
301 GNUNET_SCHEDULER_shutdown (s);
304 max_connect_per_transport = (uint32_t) tneigh;
305 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
306 GNUNET_free (keyfile);
307 if (my_private_key == NULL)
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
311 ("Transport service could not access hostkey. Exiting.\n"));
312 GNUNET_SCHEDULER_shutdown (s);
315 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
316 GNUNET_CRYPTO_hash (&my_public_key,
317 sizeof (my_public_key), &my_identity.hashPubKey);
319 /* load plugins... */
320 setup_plugin_environment ();
321 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
322 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
323 api = GNUNET_PLUGIN_load (libname, &env);
324 GNUNET_free (libname);
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328 _("Failed to load transport plugin for http\n"));
333 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, 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)) ? GNUNET_NO : GNUNET_YES;
383 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
388 /* end of test_plugin_transport_http.c */