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
157 * Struct for plugin addresses
162 * HTTP Header result for transfer
164 unsigned int http_result_code;
167 * amount of bytes we recieved
171 unsigned char buf[2048];
174 * amount of bytes we recieved
182 struct Plugin_Address * addr_head;
185 * Did the test pass or fail?
187 static int fail_notify_address;
189 * Did the test pass or fail?
191 static int fail_notify_address_count;
194 * Did the test pass or fail?
196 static int fail_pretty_printer;
199 * Did the test pass or fail?
201 static int fail_pretty_printer_count;
204 * Did the test pass or fail?
206 static int fail_addr_to_str;
209 * Did the test pass or fail?
211 static int fail_send_data;
212 static struct HTTP_Transfer no_ident;
217 * Did the test pass or fail?
222 * Recieved message already returned to sender?
231 static CURLM *multi_handle;
234 * Test message to send
236 struct HTTP_Message * msg;
239 * The task sending data
241 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
249 curl_multi_cleanup(multi_handle);
251 if (NULL != curl_handle)
252 curl_easy_cleanup (curl_handle);
255 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
257 GNUNET_SCHEDULER_cancel(sched,ti_send);
258 ti_send = GNUNET_SCHEDULER_NO_TASK;
261 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
263 GNUNET_SCHEDULER_cancel(sched,http_task_send);
264 http_task_send = GNUNET_SCHEDULER_NO_TASK;
267 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
269 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
270 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
273 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
275 GNUNET_SCHEDULER_shutdown(sched);
277 GNUNET_free(msg->buf);
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
286 * Continuation called after plugin send message
289 * @result GNUNET_OK or GNUNET_SYSERR
291 static void task_send_cont (void *cls,
292 const struct GNUNET_PeerIdentity * target,
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
301 * Task sending recieved message back to peer
306 task_send (void *cls,
307 const struct GNUNET_SCHEDULER_TaskContext *tc)
309 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
310 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
313 if (GNUNET_YES==sent)
316 struct GNUNET_MessageHeader * msg = cls;
317 unsigned int len = ntohs(msg->size);
318 const char * msgc = (const char *) msg;
320 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
326 * Recieves messages from plugin, in real world transport
328 static struct GNUNET_TIME_Relative
330 const struct GNUNET_PeerIdentity * peer,
331 const struct GNUNET_MessageHeader * message,
333 struct Session *session,
334 const char *sender_address,
335 uint16_t sender_address_len)
337 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));
339 /* take recieved message and send it back to peer */
341 void * c = (void *) message;
342 ti_send =GNUNET_SCHEDULER_add_delayed (sched, WAIT_INTERVALL, &task_send, c);
344 return GNUNET_TIME_UNIT_ZERO;
349 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
352 struct HTTP_Message * cbc = ptr;
356 if (( cbc->pos == len) && (len < (size * nmemb)))
358 memcpy(stream, cbc->buf, len);
363 static size_t copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
365 struct HTTP_Transfer * res = (struct HTTP_Transfer *) ctx;
367 res->data_size = size * nmemb;
369 if (res->pos + size * nmemb > res->size)
370 return 0; /* overflow */
371 memcpy (&res->buf[res->pos], ptr, size * nmemb);
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Send completed. %s\n",res->buf);
373 res->pos += size * nmemb;
377 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
379 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
381 unsigned int len = size * nmemb;
383 tmp = GNUNET_malloc ( len+1 );
385 if (tmp[len-2] == 13)
387 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "404\n");
390 res->http_result_code=404;
397 static size_t send_prepare( void );
399 static void send_execute (void *cls,
400 const struct GNUNET_SCHEDULER_TaskContext *tc)
408 http_task_send = GNUNET_SCHEDULER_NO_TASK;
409 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
415 mret = curl_multi_perform (multi_handle, &running);
421 msg = curl_multi_info_read (multi_handle, &running);
422 GNUNET_break (msg != NULL);
425 /* get session for affected curl handle */
426 //cs = find_session_by_curlhandle (msg->easy_handle);
427 //GNUNET_assert ( cs != NULL );
432 if ( (msg->data.result != CURLE_OK) &&
433 (msg->data.result != CURLE_GOT_NOTHING) )
436 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
437 _("curl failed for `%s' at %s:%d: `%s'\n"),
438 "curl_multi_perform",
441 curl_easy_strerror (msg->data.result));
442 /* sending msg failed*/
443 fail_send_data = GNUNET_YES;
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send completed\n");
448 /* sending completed */
449 fail_send_data = GNUNET_NO;
453 curl_easy_cleanup(curl_handle);
462 while ( (running > 0) );
465 while (mret == CURLM_CALL_MULTI_PERFORM);
470 * Function setting up file descriptors and scheduling task to run
471 * @param ses session to send data to
472 * @return bytes sent to peer
474 static size_t send_prepare( void )
480 struct GNUNET_NETWORK_FDSet *grs;
481 struct GNUNET_NETWORK_FDSet *gws;
489 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
490 if (mret != CURLM_OK)
492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
493 _("%s failed at %s:%d: `%s'\n"),
494 "curl_multi_fdset", __FILE__, __LINE__,
495 curl_multi_strerror (mret));
498 mret = curl_multi_timeout (multi_handle, &to);
499 if (mret != CURLM_OK)
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 _("%s failed at %s:%d: `%s'\n"),
503 "curl_multi_timeout", __FILE__, __LINE__,
504 curl_multi_strerror (mret));
508 grs = GNUNET_NETWORK_fdset_create ();
509 gws = GNUNET_NETWORK_fdset_create ();
510 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
511 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
512 http_task_send = GNUNET_SCHEDULER_add_select (sched,
513 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
514 GNUNET_SCHEDULER_NO_TASK,
515 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
520 GNUNET_NETWORK_fdset_destroy (gws);
521 GNUNET_NETWORK_fdset_destroy (grs);
523 /* FIXME: return bytes REALLY sent */
528 * function to send data to server
530 static int send_data(struct HTTP_Message *msg, struct HTTP_Transfer * result, char * url)
533 curl_handle = curl_easy_init();
534 if( NULL == curl_handle)
536 printf("easy_init failed \n");
537 return GNUNET_SYSERR;
540 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
542 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
543 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
544 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
545 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
546 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, ©Buffer);
547 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
548 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &putBuffer);
549 curl_easy_setopt (curl_handle, CURLOPT_READDATA, msg);
550 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
551 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
552 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
554 curl_multi_add_handle(multi_handle, curl_handle);
562 * Network format for IPv4 addresses.
564 struct IPv4HttpAddress
567 * IPv4 address, in network byte order.
572 * Port number, in network byte order.
580 * Network format for IPv6 addresses.
582 struct IPv6HttpAddress
587 struct in6_addr ipv6_addr;
590 * Port number, in network byte order.
597 * Plugin notifies transport (aka testcase) about its addresses
600 notify_address (void *cls,
604 struct GNUNET_TIME_Relative expires)
606 char * address = NULL;
608 struct Plugin_Address * pl_addr;
609 struct Plugin_Address * cur;
611 if (addrlen == (sizeof (struct IPv4HttpAddress)))
613 address = GNUNET_malloc (INET_ADDRSTRLEN);
614 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
615 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
618 if (addrlen == (sizeof (struct IPv6HttpAddress)))
620 address = GNUNET_malloc (INET6_ADDRSTRLEN);
621 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
622 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transport plugin notification for address: `%s':%u\n"),address,port);
626 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
627 pl_addr->addrlen = addrlen;
628 pl_addr->addr = GNUNET_malloc(addrlen);
629 memcpy(pl_addr->addr,addr,addrlen);
630 pl_addr->next = NULL;
632 if ( NULL == addr_head)
639 while (NULL != cur->next)
646 fail_notify_address_count++;
647 fail_notify_address = GNUNET_NO;
651 * Setup plugin environment
654 setup_plugin_environment ()
659 env.my_identity = &my_identity;
661 env.receive = &receive;
662 env.notify_address = ¬ify_address;
663 env.max_connections = max_connect_per_transport;
668 * Task shutting down testcase if it a timeout occurs
671 task_timeout (void *cls,
672 const struct GNUNET_SCHEDULER_TaskContext *tc)
674 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
675 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
684 static void pretty_printer_cb (void *cls,
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
690 fail_pretty_printer_count++;
698 * @param s scheduler to use
699 * @param c configuration to use
703 struct GNUNET_SCHEDULER_Handle *s,
705 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
711 unsigned long long tneigh;
712 struct Plugin_Address * cur;
713 struct Plugin_Address * tmp;
714 const char * addr_str;
715 unsigned int count_str_addr;
716 unsigned int suggest_res;
719 fail_pretty_printer = GNUNET_YES;
720 fail_notify_address = GNUNET_YES;
721 fail_addr_to_str = GNUNET_YES;
722 fail_send_data = GNUNET_YES;
725 /* parse configuration */
727 GNUNET_CONFIGURATION_get_value_number (c,
732 GNUNET_CONFIGURATION_get_value_filename (c,
734 "HOSTKEY", &keyfile)))
736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
738 ("Transport service is lacking key configuration settings. Exiting.\n"));
739 GNUNET_SCHEDULER_shutdown (s);
743 max_connect_per_transport = (uint32_t) tneigh;
744 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
745 GNUNET_free (keyfile);
746 if (my_private_key == NULL)
748 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
749 _("Transport service could not access hostkey. Exiting.\n"));
750 GNUNET_SCHEDULER_shutdown (s);
754 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
755 GNUNET_CRYPTO_hash (&my_public_key,
756 sizeof (my_public_key), &my_identity.hashPubKey);
758 /* load plugins... */
759 setup_plugin_environment ();
760 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
761 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
762 api = GNUNET_PLUGIN_load (libname, &env);
763 GNUNET_free (libname);
766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
767 _("Failed to load transport plugin for http\n"));
772 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
774 /* testing plugin functionality */
775 GNUNET_assert (0!=fail_notify_address_count);
776 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
778 /* testing pretty printer with all addresses obtained from the plugin*/
779 while (addr_head != NULL)
783 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
784 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
785 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
787 GNUNET_assert (GNUNET_OK == suggest_res);
788 GNUNET_assert (NULL != addr_str);
791 tmp = addr_head->next;
792 GNUNET_free (addr_head->addr);
793 GNUNET_free (addr_head);
794 GNUNET_free ((char *) addr_str);
797 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
798 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
799 fail_pretty_printer=GNUNET_NO;
800 fail_addr_to_str=GNUNET_NO;
802 /* Suggesting addresses with wrong port*/
803 struct IPv4HttpAddress failing_addr;
804 failing_addr.ipv4_addr = INADDR_LOOPBACK;
805 failing_addr.u_port = 0;
806 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
807 GNUNET_assert (GNUNET_SYSERR == suggest_res);
809 /* Suggesting addresses with wrong size*/
810 failing_addr.ipv4_addr = INADDR_LOOPBACK;
811 failing_addr.u_port = 0;
812 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
813 GNUNET_assert (GNUNET_SYSERR == suggest_res);
815 /* Suggesting addresses with wrong address*/
816 failing_addr.ipv4_addr = 0;
817 failing_addr.u_port = 12389;
818 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
819 GNUNET_assert (GNUNET_SYSERR == suggest_res);
821 /* test sending to client */
822 multi_handle = curl_multi_init();
824 /*building messages */
825 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
828 msg->buf = GNUNET_malloc (2048);
831 res = send_data (msg, &no_ident, "http://localhost:12389/");
833 /* testing finished, shutting down */
834 if ((fail_notify_address == GNUNET_NO) && (fail_pretty_printer == GNUNET_NO) && (fail_addr_to_str == GNUNET_NO) && (fail_send_data == GNUNET_NO))
844 * The main function for the transport service.
846 * @param argc number of arguments from the command line
847 * @param argv command line arguments
848 * @return 0 ok, 1 on error
851 main (int argc, char *const *argv)
854 static struct GNUNET_GETOPT_CommandLineOption options[] = {
855 GNUNET_GETOPT_OPTION_END
858 char *const argv_prog[] = {
859 "test_plugin_transport_http",
861 "test_plugin_transport_data_http.conf",
870 GNUNET_log_setup ("test_plugin_transport_http",
879 GNUNET_PROGRAM_run (5,
881 "test_plugin_transport_http",
882 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
884 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
889 /* end of test_plugin_transport_http.c */