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_NO
43 #define DEBUG GNUNET_NO
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
180 unsigned int test_failed;
184 struct Plugin_Address * addr_head;
187 * Did the test pass or fail?
189 static int fail_notify_address;
191 * Did the test pass or fail?
193 static int fail_notify_address_count;
196 * Did the test pass or fail?
198 static int fail_pretty_printer;
201 * Did the test pass or fail?
203 static int fail_pretty_printer_count;
206 * Did the test pass or fail?
208 static int fail_addr_to_str;
211 * Did the test pass or fail?
213 static struct HTTP_Transfer testtransfer_no_ident;
218 * Did the test pass or fail?
223 * Recieved message already returned to sender?
232 static CURLM *multi_handle;
235 * Test message to send
237 struct HTTP_Message * msg;
240 * The task sending data
242 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
250 if ((fail_notify_address == GNUNET_NO) && (fail_pretty_printer == GNUNET_NO) && (fail_addr_to_str == GNUNET_NO) && (testtransfer_no_ident.test_failed == GNUNET_NO))
255 curl_multi_cleanup(multi_handle);
257 if (NULL != curl_handle)
258 curl_easy_cleanup (curl_handle);
261 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
263 GNUNET_SCHEDULER_cancel(sched,ti_send);
264 ti_send = GNUNET_SCHEDULER_NO_TASK;
267 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
269 GNUNET_SCHEDULER_cancel(sched,http_task_send);
270 http_task_send = GNUNET_SCHEDULER_NO_TASK;
273 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
275 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
276 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
279 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
281 GNUNET_SCHEDULER_shutdown(sched);
283 GNUNET_free(msg->buf);
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
292 * Continuation called after plugin send message
295 * @result GNUNET_OK or GNUNET_SYSERR
297 static void task_send_cont (void *cls,
298 const struct GNUNET_PeerIdentity * target,
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
307 * Task sending recieved message back to peer
312 task_send (void *cls,
313 const struct GNUNET_SCHEDULER_TaskContext *tc)
315 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
316 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
319 if (GNUNET_YES==sent)
322 struct GNUNET_MessageHeader * msg = cls;
323 unsigned int len = ntohs(msg->size);
324 const char * msgc = (const char *) msg;
326 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
332 * Recieves messages from plugin, in real world transport
334 static struct GNUNET_TIME_Relative
336 const struct GNUNET_PeerIdentity * peer,
337 const struct GNUNET_MessageHeader * message,
339 struct Session *session,
340 const char *sender_address,
341 uint16_t sender_address_len)
343 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));
345 /* take recieved message and send it back to peer */
347 void * c = (void *) message;
348 ti_send =GNUNET_SCHEDULER_add_delayed (sched, WAIT_INTERVALL, &task_send, c);
350 return GNUNET_TIME_UNIT_ZERO;
355 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
358 struct HTTP_Message * cbc = ptr;
362 if (( cbc->pos == len) && (len < (size * nmemb)))
364 memcpy(stream, cbc->buf, len);
369 static size_t copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
371 struct HTTP_Transfer * res = (struct HTTP_Transfer *) ctx;
373 res->data_size = size * nmemb;
375 if (res->pos + size * nmemb > res->size)
376 return 0; /* overflow */
377 memcpy (&res->buf[res->pos], ptr, size * nmemb);
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Send completed. %s\n",res->buf);
379 res->pos += size * nmemb;
383 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
385 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
387 unsigned int len = size * nmemb;
389 tmp = GNUNET_malloc ( len+1 );
391 if (tmp[len-2] == 13)
393 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "404\n");
396 res->http_result_code=404;
403 static size_t send_prepare( struct HTTP_Transfer * result);
405 static void send_execute (void *cls,
406 const struct GNUNET_SCHEDULER_TaskContext *tc)
408 struct HTTP_Transfer *res;
414 res = (struct HTTP_Transfer *) cls;
415 http_task_send = GNUNET_SCHEDULER_NO_TASK;
416 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
422 mret = curl_multi_perform (multi_handle, &running);
428 msg = curl_multi_info_read (multi_handle, &running);
429 GNUNET_break (msg != NULL);
432 /* get session for affected curl handle */
433 //cs = find_session_by_curlhandle (msg->easy_handle);
434 //GNUNET_assert ( cs != NULL );
439 if ( (msg->data.result != CURLE_OK) &&
440 (msg->data.result != CURLE_GOT_NOTHING) )
443 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
444 _("curl failed for `%s' at %s:%d: `%s'\n"),
445 "curl_multi_perform",
448 curl_easy_strerror (msg->data.result));
449 /* sending msg failed*/
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send completed with code %u\n", res->data_size);
454 /* sending completed */
456 if ( (cls == &testtransfer_no_ident) && (res->http_result_code==404) && (res->data_size==208))
457 res->test_failed = GNUNET_NO;
459 curl_easy_cleanup(curl_handle);
468 while ( (running > 0) );
471 while (mret == CURLM_CALL_MULTI_PERFORM);
476 * Function setting up file descriptors and scheduling task to run
477 * @param ses session to send data to
478 * @return bytes sent to peer
480 static size_t send_prepare( struct HTTP_Transfer * result)
486 struct GNUNET_NETWORK_FDSet *grs;
487 struct GNUNET_NETWORK_FDSet *gws;
495 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
496 if (mret != CURLM_OK)
498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499 _("%s failed at %s:%d: `%s'\n"),
500 "curl_multi_fdset", __FILE__, __LINE__,
501 curl_multi_strerror (mret));
504 mret = curl_multi_timeout (multi_handle, &to);
505 if (mret != CURLM_OK)
507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
508 _("%s failed at %s:%d: `%s'\n"),
509 "curl_multi_timeout", __FILE__, __LINE__,
510 curl_multi_strerror (mret));
514 grs = GNUNET_NETWORK_fdset_create ();
515 gws = GNUNET_NETWORK_fdset_create ();
516 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
517 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
518 http_task_send = GNUNET_SCHEDULER_add_select (sched,
519 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
520 GNUNET_SCHEDULER_NO_TASK,
521 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
526 GNUNET_NETWORK_fdset_destroy (gws);
527 GNUNET_NETWORK_fdset_destroy (grs);
529 /* FIXME: return bytes REALLY sent */
534 * function to send data to server
536 static int send_data(struct HTTP_Message *msg, struct HTTP_Transfer * result, char * url)
539 curl_handle = curl_easy_init();
540 if( NULL == curl_handle)
542 printf("easy_init failed \n");
543 return GNUNET_SYSERR;
546 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
548 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
549 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
550 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
551 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
552 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, ©Buffer);
553 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
554 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &putBuffer);
555 curl_easy_setopt (curl_handle, CURLOPT_READDATA, msg);
556 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
557 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
558 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
560 curl_multi_add_handle(multi_handle, curl_handle);
562 send_prepare(result);
568 * Network format for IPv4 addresses.
570 struct IPv4HttpAddress
573 * IPv4 address, in network byte order.
578 * Port number, in network byte order.
586 * Network format for IPv6 addresses.
588 struct IPv6HttpAddress
593 struct in6_addr ipv6_addr;
596 * Port number, in network byte order.
603 * Plugin notifies transport (aka testcase) about its addresses
606 notify_address (void *cls,
610 struct GNUNET_TIME_Relative expires)
612 char address[INET6_ADDRSTRLEN];
614 struct Plugin_Address * pl_addr;
615 struct Plugin_Address * cur;
617 if (addrlen == (sizeof (struct IPv4HttpAddress)))
619 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
620 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
622 if (addrlen == (sizeof (struct IPv6HttpAddress)))
624 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
625 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 _("Transport plugin notification for address: `%s':%u\n"),
631 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
632 pl_addr->addrlen = addrlen;
633 pl_addr->addr = GNUNET_malloc(addrlen);
634 memcpy(pl_addr->addr,addr,addrlen);
635 pl_addr->next = NULL;
637 if ( NULL == addr_head)
644 while (NULL != cur->next)
650 fail_notify_address_count++;
651 fail_notify_address = GNUNET_NO;
655 * Setup plugin environment
658 setup_plugin_environment ()
663 env.my_identity = &my_identity;
665 env.receive = &receive;
666 env.notify_address = ¬ify_address;
667 env.max_connections = max_connect_per_transport;
672 * Task shutting down testcase if it a timeout occurs
675 task_timeout (void *cls,
676 const struct GNUNET_SCHEDULER_TaskContext *tc)
678 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
679 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
688 static void pretty_printer_cb (void *cls,
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
694 fail_pretty_printer_count++;
702 * @param s scheduler to use
703 * @param c configuration to use
707 struct GNUNET_SCHEDULER_Handle *s,
709 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
715 unsigned long long tneigh;
716 struct Plugin_Address * cur;
717 struct Plugin_Address * tmp;
718 const char * addr_str;
719 unsigned int count_str_addr;
720 unsigned int suggest_res;
723 fail_pretty_printer = GNUNET_YES;
724 fail_notify_address = GNUNET_YES;
725 fail_addr_to_str = GNUNET_YES;
729 /* parse configuration */
731 GNUNET_CONFIGURATION_get_value_number (c,
736 GNUNET_CONFIGURATION_get_value_filename (c,
738 "HOSTKEY", &keyfile)))
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
742 ("Transport service is lacking key configuration settings. Exiting.\n"));
743 GNUNET_SCHEDULER_shutdown (s);
747 max_connect_per_transport = (uint32_t) tneigh;
748 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
749 GNUNET_free (keyfile);
750 if (my_private_key == NULL)
752 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
753 _("Transport service could not access hostkey. Exiting.\n"));
754 GNUNET_SCHEDULER_shutdown (s);
758 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
759 GNUNET_CRYPTO_hash (&my_public_key,
760 sizeof (my_public_key), &my_identity.hashPubKey);
762 /* load plugins... */
763 setup_plugin_environment ();
764 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
765 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
766 api = GNUNET_PLUGIN_load (libname, &env);
767 GNUNET_free (libname);
770 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
771 _("Failed to load transport plugin for http\n"));
776 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
778 /* testing plugin functionality */
779 GNUNET_assert (0!=fail_notify_address_count);
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
782 /* testing pretty printer with all addresses obtained from the plugin*/
783 while (addr_head != NULL)
787 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
788 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
789 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
791 GNUNET_assert (GNUNET_OK == suggest_res);
792 GNUNET_assert (NULL != addr_str);
795 tmp = addr_head->next;
796 GNUNET_free (addr_head->addr);
797 GNUNET_free (addr_head);
798 GNUNET_free ((char *) addr_str);
801 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
802 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
803 fail_pretty_printer=GNUNET_NO;
804 fail_addr_to_str=GNUNET_NO;
806 /* Suggesting addresses with wrong port*/
807 struct IPv4HttpAddress failing_addr;
808 failing_addr.ipv4_addr = INADDR_LOOPBACK;
809 failing_addr.u_port = 0;
810 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
811 GNUNET_assert (GNUNET_SYSERR == suggest_res);
813 /* Suggesting addresses with wrong size*/
814 failing_addr.ipv4_addr = INADDR_LOOPBACK;
815 failing_addr.u_port = 0;
816 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
817 GNUNET_assert (GNUNET_SYSERR == suggest_res);
819 /* Suggesting addresses with wrong address*/
820 failing_addr.ipv4_addr = 0;
821 failing_addr.u_port = 12389;
822 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
823 GNUNET_assert (GNUNET_SYSERR == suggest_res);
825 /* test sending to client */
826 multi_handle = curl_multi_init();
828 /*building messages */
829 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
832 msg->buf = GNUNET_malloc (2048);
833 testtransfer_no_ident.size=2048;
834 testtransfer_no_ident.test_failed = GNUNET_YES;
836 /* Connecting to peer without identification */
837 res = send_data (msg, &testtransfer_no_ident, "http://localhost:12389/");
841 /* testing finished */
847 * The main function for the transport service.
849 * @param argc number of arguments from the command line
850 * @param argv command line arguments
851 * @return 0 ok, 1 on error
854 main (int argc, char *const *argv)
857 static struct GNUNET_GETOPT_CommandLineOption options[] = {
858 GNUNET_GETOPT_OPTION_END
861 char *const argv_prog[] = {
862 "test_plugin_transport_http",
864 "test_plugin_transport_data_http.conf",
873 GNUNET_log_setup ("test_plugin_transport_http",
882 GNUNET_PROGRAM_run (5,
884 "test_plugin_transport_http",
885 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
887 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
892 /* end of test_plugin_transport_http.c */