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
44 #define DEBUG_CURL GNUNET_NO
46 #define PLUGIN libgnunet_plugin_transport_template
49 * How long until we give up on transmitting the message?
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
54 * How long until we give up on transmitting the message?
56 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
59 * How long between recieve and send?
61 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
64 * Message to send using http
77 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
82 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
87 static struct GNUNET_PeerIdentity my_identity;
92 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
98 struct GNUNET_SCHEDULER_Handle *sched;
101 * Our statistics handle.
103 struct GNUNET_STATISTICS_Handle *stats;
109 const struct GNUNET_CONFIGURATION_Handle *cfg;
112 * Number of neighbours we'd like to have.
114 static uint32_t max_connect_per_transport;
117 * Environment for this plugin.
119 static struct GNUNET_TRANSPORT_PluginEnvironment env;
122 *handle for the api provided by this plugin
124 static struct GNUNET_TRANSPORT_PluginFunctions *api;
127 * ID of the task controlling the testcase timeout
129 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
131 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
133 const struct GNUNET_PeerIdentity * p;
136 * Struct for plugin addresses
138 struct Plugin_Address
141 * Next field for linked list
143 struct Plugin_Address * next;
146 * buffer containing data to send
151 * amount of data to sent
156 struct Plugin_Address * addr_head;
159 * Did the test pass or fail?
161 static int fail_notify_address;
163 * Did the test pass or fail?
165 static int fail_notify_address_count;
168 * Did the test pass or fail?
170 static int fail_pretty_printer;
173 * Did the test pass or fail?
175 static int fail_pretty_printer_count;
178 * Did the test pass or fail?
180 static int fail_addr_to_str;
183 * Did the test pass or fail?
188 * Recieved message already returned to sender?
197 static CURLM *multi_handle;
200 * Test message to send
202 struct HTTP_Message * msg;
205 * The task sending data
207 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
215 curl_multi_cleanup(multi_handle);
217 if (NULL != curl_handle)
218 curl_easy_cleanup (curl_handle);
221 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
223 GNUNET_SCHEDULER_cancel(sched,ti_send);
224 ti_send = GNUNET_SCHEDULER_NO_TASK;
227 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
229 GNUNET_SCHEDULER_cancel(sched,http_task_send);
230 http_task_send = GNUNET_SCHEDULER_NO_TASK;
233 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
235 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
236 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
239 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
241 GNUNET_SCHEDULER_shutdown(sched);
243 GNUNET_free(msg->buf);
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
252 * Continuation called after plugin send message
255 * @result GNUNET_OK or GNUNET_SYSERR
257 static void task_send_cont (void *cls,
258 const struct GNUNET_PeerIdentity * target,
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
267 * Task sending recieved message back to peer
272 task_send (void *cls,
273 const struct GNUNET_SCHEDULER_TaskContext *tc)
275 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
276 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
279 if (GNUNET_YES==sent)
282 struct GNUNET_MessageHeader * msg = cls;
283 unsigned int len = ntohs(msg->size);
284 const char * msgc = (const char *) msg;
286 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
292 * Recieves messages from plugin, in real world transport
294 static struct GNUNET_TIME_Relative
296 const struct GNUNET_PeerIdentity * peer,
297 const struct GNUNET_MessageHeader * message,
299 struct Session *session,
300 const char *sender_address,
301 uint16_t sender_address_len)
303 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));
305 /* take recieved message and send it back to peer */
307 void * c = (void *) message;
308 ti_send =GNUNET_SCHEDULER_add_delayed (sched, WAIT_INTERVALL, &task_send, c);
310 return GNUNET_TIME_UNIT_ZERO;
315 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
318 struct HTTP_Message * cbc = ptr;
322 if (( cbc->pos == len) && (len < (size * nmemb)))
324 memcpy(stream, cbc->buf, len);
329 static size_t copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
331 struct HTTP_Message *cbc = ctx;
333 if (cbc->pos + size * nmemb > cbc->size)
334 return 0; /* overflow */
335 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
336 cbc->pos += size * nmemb;
340 static size_t send_prepare( void );
342 static void send_execute (void *cls,
343 const struct GNUNET_SCHEDULER_TaskContext *tc)
351 http_task_send = GNUNET_SCHEDULER_NO_TASK;
352 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
358 mret = curl_multi_perform (multi_handle, &running);
364 msg = curl_multi_info_read (multi_handle, &running);
365 GNUNET_break (msg != NULL);
368 /* get session for affected curl handle */
369 //cs = find_session_by_curlhandle (msg->easy_handle);
370 //GNUNET_assert ( cs != NULL );
375 if ( (msg->data.result != CURLE_OK) &&
376 (msg->data.result != CURLE_GOT_NOTHING) )
379 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
380 _("curl failed for `%s' at %s:%d: `%s'\n"),
381 "curl_multi_perform",
384 curl_easy_strerror (msg->data.result));
385 /* sending msg failed*/
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Send completed.\n");
391 /* sending completed */
393 curl_easy_cleanup(curl_handle);
402 while ( (running > 0) );
405 while (mret == CURLM_CALL_MULTI_PERFORM);
410 * Function setting up file descriptors and scheduling task to run
411 * @param ses session to send data to
412 * @return bytes sent to peer
414 static size_t send_prepare( void )
420 struct GNUNET_NETWORK_FDSet *grs;
421 struct GNUNET_NETWORK_FDSet *gws;
429 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
430 if (mret != CURLM_OK)
432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
433 _("%s failed at %s:%d: `%s'\n"),
434 "curl_multi_fdset", __FILE__, __LINE__,
435 curl_multi_strerror (mret));
438 mret = curl_multi_timeout (multi_handle, &to);
439 if (mret != CURLM_OK)
441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
442 _("%s failed at %s:%d: `%s'\n"),
443 "curl_multi_timeout", __FILE__, __LINE__,
444 curl_multi_strerror (mret));
448 grs = GNUNET_NETWORK_fdset_create ();
449 gws = GNUNET_NETWORK_fdset_create ();
450 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
451 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
452 http_task_send = GNUNET_SCHEDULER_add_select (sched,
453 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
454 GNUNET_SCHEDULER_NO_TASK,
455 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
460 GNUNET_NETWORK_fdset_destroy (gws);
461 GNUNET_NETWORK_fdset_destroy (grs);
463 /* FIXME: return bytes REALLY sent */
468 * function to send data to server
470 static int send_data(struct HTTP_Message *msg, char * url)
473 curl_handle = curl_easy_init();
474 if( NULL == curl_handle)
476 printf("easy_init failed \n");
477 return GNUNET_SYSERR;
480 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
482 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
483 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
484 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, ©Buffer);
485 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, msg);
486 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &putBuffer);
487 curl_easy_setopt (curl_handle, CURLOPT_READDATA, msg);
488 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
489 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
490 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
492 curl_multi_add_handle(multi_handle, curl_handle);
500 * Network format for IPv4 addresses.
502 struct IPv4HttpAddress
505 * IPv4 address, in network byte order.
510 * Port number, in network byte order.
518 * Network format for IPv6 addresses.
520 struct IPv6HttpAddress
525 struct in6_addr ipv6_addr;
528 * Port number, in network byte order.
535 * Plugin notifies transport (aka testcase) about its addresses
538 notify_address (void *cls,
542 struct GNUNET_TIME_Relative expires)
544 char * address = NULL;
546 struct Plugin_Address * pl_addr;
547 struct Plugin_Address * cur;
549 if (addrlen == (sizeof (struct IPv4HttpAddress)))
551 address = GNUNET_malloc (INET_ADDRSTRLEN);
552 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
553 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
556 if (addrlen == (sizeof (struct IPv6HttpAddress)))
558 address = GNUNET_malloc (INET6_ADDRSTRLEN);
559 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
560 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transport plugin notification for address: `%s':%u\n"),address,port);
564 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
565 pl_addr->addrlen = addrlen;
566 pl_addr->addr = GNUNET_malloc(addrlen);
567 memcpy(pl_addr->addr,addr,addrlen);
568 pl_addr->next = NULL;
570 if ( NULL == addr_head)
577 while (NULL != cur->next)
584 fail_notify_address_count++;
585 fail_notify_address = GNUNET_NO;
589 * Setup plugin environment
592 setup_plugin_environment ()
597 env.my_identity = &my_identity;
599 env.receive = &receive;
600 env.notify_address = ¬ify_address;
601 env.max_connections = max_connect_per_transport;
606 * Task shutting down testcase if it a timeout occurs
609 task_timeout (void *cls,
610 const struct GNUNET_SCHEDULER_TaskContext *tc)
612 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
613 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
622 static void pretty_printer_cb (void *cls,
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
628 fail_pretty_printer_count++;
636 * @param s scheduler to use
637 * @param c configuration to use
641 struct GNUNET_SCHEDULER_Handle *s,
643 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
649 unsigned long long tneigh;
650 struct Plugin_Address * cur;
651 struct Plugin_Address * tmp;
652 const char * addr_str;
653 unsigned int count_str_addr;
654 unsigned int suggest_res;
657 fail_pretty_printer = GNUNET_YES;
658 fail_notify_address = GNUNET_YES;
659 fail_addr_to_str = GNUNET_YES;
662 /* parse configuration */
664 GNUNET_CONFIGURATION_get_value_number (c,
669 GNUNET_CONFIGURATION_get_value_filename (c,
671 "HOSTKEY", &keyfile)))
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 ("Transport service is lacking key configuration settings. Exiting.\n"));
676 GNUNET_SCHEDULER_shutdown (s);
680 max_connect_per_transport = (uint32_t) tneigh;
681 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
682 GNUNET_free (keyfile);
683 if (my_private_key == NULL)
685 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
686 _("Transport service could not access hostkey. Exiting.\n"));
687 GNUNET_SCHEDULER_shutdown (s);
691 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
692 GNUNET_CRYPTO_hash (&my_public_key,
693 sizeof (my_public_key), &my_identity.hashPubKey);
695 /* load plugins... */
696 setup_plugin_environment ();
697 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
698 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
699 api = GNUNET_PLUGIN_load (libname, &env);
700 GNUNET_free (libname);
703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
704 _("Failed to load transport plugin for http\n"));
709 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
711 /* testing plugin functionality */
712 GNUNET_assert (0!=fail_notify_address_count);
713 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
715 /* testing pretty printer with all addresses obtained from the plugin*/
716 while (addr_head != NULL)
720 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
721 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
722 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
724 GNUNET_assert (GNUNET_OK == suggest_res);
725 GNUNET_assert (NULL != addr_str);
728 tmp = addr_head->next;
729 GNUNET_free (addr_head->addr);
730 GNUNET_free (addr_head);
731 GNUNET_free ((char *) addr_str);
734 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
735 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
736 fail_pretty_printer=GNUNET_NO;
737 fail_addr_to_str=GNUNET_NO;
739 /* Suggesting addresses with wrong port*/
740 struct IPv4HttpAddress failing_addr;
741 failing_addr.ipv4_addr = INADDR_LOOPBACK;
742 failing_addr.u_port = 0;
743 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
744 GNUNET_assert (GNUNET_SYSERR == suggest_res);
746 /* Suggesting addresses with wrong size*/
747 failing_addr.ipv4_addr = INADDR_LOOPBACK;
748 failing_addr.u_port = 0;
749 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
750 GNUNET_assert (GNUNET_SYSERR == suggest_res);
752 /* Suggesting addresses with wrong address*/
753 failing_addr.ipv4_addr = 0;
754 failing_addr.u_port = 12389;
755 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
756 GNUNET_assert (GNUNET_SYSERR == suggest_res);
758 /* test sending to client */
759 multi_handle = curl_multi_init();
761 /*building messages */
762 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
765 msg->buf = GNUNET_malloc (2048);
767 res = send_data (msg, "http://localhost:12389/");
769 /* testing finished, shutting down */
770 if ((fail_notify_address == GNUNET_NO) && (fail_pretty_printer == GNUNET_NO) && (fail_addr_to_str == GNUNET_NO) )
780 * The main function for the transport service.
782 * @param argc number of arguments from the command line
783 * @param argv command line arguments
784 * @return 0 ok, 1 on error
787 main (int argc, char *const *argv)
790 static struct GNUNET_GETOPT_CommandLineOption options[] = {
791 GNUNET_GETOPT_OPTION_END
794 char *const argv_prog[] = {
795 "test_plugin_transport_http",
797 "test_plugin_transport_data_http.conf",
806 GNUNET_log_setup ("test_plugin_transport_http",
815 GNUNET_PROGRAM_run (5,
817 "test_plugin_transport_http",
818 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
820 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
825 /* end of test_plugin_transport_http.c */