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_common.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_plugin_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_program_lib.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_crypto_lib.h"
40 #include "plugin_transport.h"
41 #include "gnunet_statistics_service.h"
42 #include "transport.h"
43 #include <curl/curl.h>
45 #define VERBOSE GNUNET_YES
46 #define DEBUG GNUNET_NO
47 #define DEBUG_CURL GNUNET_YES
48 #define HTTP_BUFFER_SIZE 2048
50 #define PLUGIN libgnunet_plugin_transport_template
53 * How long until we give up on transmitting the message?
55 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
58 * How long until we give up on transmitting the message?
60 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
63 * How long between recieve and send?
65 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
70 * Struct for plugin addresses
75 * Next field for linked list
77 struct Plugin_Address * next;
80 * buffer containing data to send
85 * amount of data to sent
91 * Message to send using http
98 unsigned char buf[HTTP_BUFFER_SIZE];
101 * current position in buffer
118 * Struct for plugin addresses
123 * amount of bytes we recieved
128 * buffer for http transfers
130 unsigned char buf[HTTP_BUFFER_SIZE];
133 * buffer size this transfer
138 * amount of bytes we recieved
143 * HTTP Header result for transfer
145 unsigned int http_result_code;
150 unsigned int test_failed;
153 * was this test already executed?
155 unsigned int test_executed;
160 * Network format for IPv4 addresses.
162 struct IPv4HttpAddress
165 * IPv4 address, in network byte order.
170 * Port number, in network byte order.
178 * Network format for IPv6 addresses.
180 struct IPv6HttpAddress
185 struct in6_addr ipv6_addr;
188 * Port number, in network byte order.
198 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
203 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
208 static struct GNUNET_PeerIdentity my_identity;
213 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
218 static long long unsigned int port;
223 static char * test_addr;
228 struct GNUNET_SCHEDULER_Handle *sched;
231 * Our statistics handle.
233 struct GNUNET_STATISTICS_Handle *stats;
239 const struct GNUNET_CONFIGURATION_Handle *cfg;
242 * Number of neighbours we'd like to have.
244 static uint32_t max_connect_per_transport;
247 * Environment for this plugin.
249 static struct GNUNET_TRANSPORT_PluginEnvironment env;
252 *handle for the api provided by this plugin
254 static struct GNUNET_TRANSPORT_PluginFunctions *api;
257 * ID of the task controlling the testcase timeout
259 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
261 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
263 //const struct GNUNET_PeerIdentity * p;
266 * buffer for data to send
268 static struct HTTP_Message buffer_out;
271 * buffer for data to recieve
273 static struct HTTP_Message buffer_in;
276 struct Plugin_Address * addr_head;
279 * Did the test pass or fail?
281 static int fail_notify_address;
283 * Did the test pass or fail?
285 static int fail_notify_address_count;
288 * Did the test pass or fail?
290 static int fail_pretty_printer;
293 * Did the test pass or fail?
295 static int fail_pretty_printer_count;
298 * Did the test pass or fail?
300 static int fail_addr_to_str;
303 * No. of msgs transmitted successfully to local addresses
305 static int fail_msgs_transmited_to_local_addrs;
308 * Test: transmit msg of max. size
310 static int fail_msg_transmited_bigger_max_size;
313 * Test: transmit msg of max. size
315 static int fail_msg_transmited_max_size;
318 * Test: transmit 2 msgs. in in send operation
320 static int fail_multiple_msgs_in_transmission;
323 * Test: connect to peer without peer identification
325 static struct HTTP_Transfer test_no_ident;
328 * Test: connect to peer without peer identification
330 static struct HTTP_Transfer test_too_short_ident;
333 * Test: connect to peer without peer identification
335 static struct HTTP_Transfer test_too_long_ident;
338 * Test: connect to peer with valid peer identification
340 static struct HTTP_Transfer test_valid_ident;
343 * Did the test pass or fail?
348 * Number of local addresses
350 static unsigned int count_str_addr;
357 static CURLM *multi_handle;
360 * The task sending data
362 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
370 struct Plugin_Address * cur;
371 struct Plugin_Address * tmp;
373 /* Evaluate results */
375 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test plugin functions failed\n");
380 if ((test_no_ident.test_failed == GNUNET_YES) || (test_too_short_ident.test_failed == GNUNET_YES) || (test_too_long_ident.test_failed == GNUNET_YES) || (test_valid_ident.test_failed == GNUNET_YES))
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test connect with wrong data failed\n");
385 if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_multiple_msgs_in_transmission != 2) || (fail_msg_transmited_max_size == GNUNET_YES))
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test sending with plugin failed\n");
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
395 curl_multi_cleanup(multi_handle);
397 if (NULL != curl_handle)
398 curl_easy_cleanup (curl_handle);
400 /* cleaning addresses */
401 while (addr_head != NULL)
404 tmp = addr_head->next;
405 GNUNET_free (addr_head->addr);
406 GNUNET_free (addr_head);
410 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
412 GNUNET_SCHEDULER_cancel(sched,ti_send);
413 ti_send = GNUNET_SCHEDULER_NO_TASK;
416 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
418 GNUNET_SCHEDULER_cancel(sched,http_task_send);
419 http_task_send = GNUNET_SCHEDULER_NO_TASK;
422 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
424 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
425 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
428 GNUNET_free(test_addr);
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
430 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
432 GNUNET_SCHEDULER_shutdown(sched);
433 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
442 * Continuation called after plugin send message
445 * @result GNUNET_OK or GNUNET_SYSERR
448 static void task_send_cont (void *cls,
449 const struct GNUNET_PeerIdentity * target,
452 struct Plugin_Address * tmp_addr;
453 tmp_addr = addr_head;
455 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
458 fail_msg_transmited_bigger_max_size = GNUNET_NO;
462 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
465 fail_msg_transmited_max_size = GNUNET_NO;
471 static void run_connection_tests( int );
474 * Recieves messages from plugin, in real world transport
476 static struct GNUNET_TIME_Relative
478 const struct GNUNET_PeerIdentity * peer,
479 const struct GNUNET_MessageHeader * message,
481 struct Session *session,
482 const char *sender_address,
483 uint16_t sender_address_len)
485 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));
487 if ((ntohs(message->type)>=10) && (ntohs(message->type)<=20))
489 fail_msgs_transmited_to_local_addrs++;
490 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
491 run_connection_tests(2);
494 if ((ntohs(message->type)==40) || (ntohs(message->type)==41))
496 fail_multiple_msgs_in_transmission ++;
499 if (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1)
501 fail_msg_transmited_max_size = GNUNET_NO;
505 return GNUNET_TIME_UNIT_ZERO;
508 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
512 len = buffer_out.len;
514 if (( buffer_out.pos == len) || (len > (size * nmemb)))
516 memcpy(stream, buffer_out.buf, len);
517 buffer_out.pos = len;
521 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
524 if (buffer_in.pos + size * nmemb > buffer_in.size)
525 return 0; /* overflow */
527 buffer_in.len = size * nmemb;
528 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
529 buffer_in.pos += size * nmemb;
530 buffer_in.len = buffer_in.pos;
531 buffer_in.buf[buffer_in.pos] = '\0';
532 return buffer_in.pos;
535 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
537 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
539 unsigned int len = size * nmemb;
541 tmp = GNUNET_malloc ( len+1 );
543 if (tmp[len-2] == 13)
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
548 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
550 res->http_result_code=100;
552 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
554 res->http_result_code=200;
556 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
558 res->http_result_code=400;
560 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
562 res->http_result_code=404;
564 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
566 res->http_result_code=413;
573 static size_t send_prepare( struct HTTP_Transfer * result);
575 static void run_connection_tests( );
577 static void send_execute (void *cls,
578 const struct GNUNET_SCHEDULER_TaskContext *tc)
580 struct HTTP_Transfer *res;
586 res = (struct HTTP_Transfer *) cls;
587 http_task_send = GNUNET_SCHEDULER_NO_TASK;
588 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
594 mret = curl_multi_perform (multi_handle, &running);
600 msg = curl_multi_info_read (multi_handle, &running);
603 /* get session for affected curl handle */
604 //cs = find_session_by_curlhandle (msg->easy_handle);
605 //GNUNET_assert ( cs != NULL );
610 if ( (msg->data.result != CURLE_OK) &&
611 (msg->data.result != CURLE_GOT_NOTHING) )
614 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
615 _("curl failed for `%s' at %s:%d: `%s'\n"),
616 "curl_multi_perform",
619 curl_easy_strerror (msg->data.result));
620 /* sending msg failed*/
621 curl_easy_cleanup(curl_handle);
624 run_connection_tests(1);
626 if (res == &test_no_ident)
628 if ((res->http_result_code==404) && (buffer_in.len==208))
630 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
631 res->test_failed = GNUNET_NO;
634 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
636 if (res == &test_too_short_ident)
638 if ((res->http_result_code==404) && (buffer_in.len==208))
640 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
641 res->test_failed = GNUNET_NO;
644 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
646 if (res == &test_too_long_ident)
648 if ((res->http_result_code==404) && (buffer_in.len==208))
650 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
651 res->test_failed = GNUNET_NO;
654 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
656 if (res == &test_valid_ident)
658 if ((res->http_result_code==200))
660 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
661 res->test_failed = GNUNET_NO;
664 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
666 curl_easy_cleanup(curl_handle);
669 run_connection_tests(1);
676 while ( (running > 0) );
679 while (mret == CURLM_CALL_MULTI_PERFORM);
684 * Function setting up file descriptors and scheduling task to run
685 * @param ses session to send data to
686 * @return bytes sent to peer
688 static size_t send_prepare( struct HTTP_Transfer * result)
694 struct GNUNET_NETWORK_FDSet *grs;
695 struct GNUNET_NETWORK_FDSet *gws;
703 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
704 if (mret != CURLM_OK)
706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
707 _("%s failed at %s:%d: `%s'\n"),
708 "curl_multi_fdset", __FILE__, __LINE__,
709 curl_multi_strerror (mret));
712 mret = curl_multi_timeout (multi_handle, &to);
713 if (mret != CURLM_OK)
715 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
716 _("%s failed at %s:%d: `%s'\n"),
717 "curl_multi_timeout", __FILE__, __LINE__,
718 curl_multi_strerror (mret));
722 grs = GNUNET_NETWORK_fdset_create ();
723 gws = GNUNET_NETWORK_fdset_create ();
724 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
725 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
726 http_task_send = GNUNET_SCHEDULER_add_select (sched,
727 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
728 GNUNET_SCHEDULER_NO_TASK,
729 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
734 GNUNET_NETWORK_fdset_destroy (gws);
735 GNUNET_NETWORK_fdset_destroy (grs);
737 /* FIXME: return bytes REALLY sent */
742 * function to send data to server
744 static int send_data( struct HTTP_Transfer * result, char * url)
747 curl_handle = curl_easy_init();
748 if( NULL == curl_handle)
750 printf("easy_init failed \n");
751 return GNUNET_SYSERR;
754 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
756 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
757 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
758 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
759 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
760 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
761 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
762 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
763 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
764 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
765 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
766 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
768 curl_multi_add_handle(multi_handle, curl_handle);
770 send_prepare(result);
776 * Plugin notifies transport (aka testcase) about its addresses
779 notify_address (void *cls,
783 struct GNUNET_TIME_Relative expires)
785 char address[INET6_ADDRSTRLEN];
787 struct Plugin_Address * pl_addr;
788 struct Plugin_Address * cur;
790 if (addrlen == (sizeof (struct IPv4HttpAddress)))
792 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
793 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
795 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
797 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
798 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 _("Transport plugin notification for address: `%s':%u\n"),
809 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
810 pl_addr->addrlen = addrlen;
811 pl_addr->addr = GNUNET_malloc(addrlen);
812 memcpy(pl_addr->addr,addr,addrlen);
813 pl_addr->next = NULL;
815 if ( NULL == addr_head)
822 while (NULL != cur->next)
828 fail_notify_address_count++;
829 fail_notify_address = GNUNET_NO;
833 * Setup plugin environment
836 setup_plugin_environment ()
841 env.my_identity = &my_identity;
843 env.receive = &receive;
844 env.notify_address = ¬ify_address;
845 env.max_connections = max_connect_per_transport;
850 * Task shutting down testcase if it a timeout occurs
853 task_timeout (void *cls,
854 const struct GNUNET_SCHEDULER_TaskContext *tc)
856 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
857 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
866 static void pretty_printer_cb (void *cls,
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
872 fail_pretty_printer_count++;
876 * Runs every single test to test the plugin
878 static void run_connection_tests( int phase )
881 char * host_str = NULL;
882 /* resetting buffers */
883 buffer_in.size = HTTP_BUFFER_SIZE;
887 buffer_out.size = HTTP_BUFFER_SIZE;
891 if (test_no_ident.test_executed == GNUNET_NO)
893 /* Connecting to peer without identification */
895 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
896 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
897 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
898 test_no_ident.test_executed = GNUNET_YES;
899 send_data ( &test_no_ident, host_str);
900 GNUNET_free (host_str);
903 if (test_too_short_ident.test_executed == GNUNET_NO)
905 char * ident = "AAAAAAAAAA";
906 /* Connecting to peer with too short identification */
907 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
908 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
909 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
910 test_too_short_ident.test_executed = GNUNET_YES;
911 send_data ( &test_too_short_ident, host_str);
912 GNUNET_free (host_str);
916 if (test_too_long_ident.test_executed == GNUNET_NO)
918 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
920 /* Connecting to peer with too long identification */
921 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
922 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
923 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
924 test_too_long_ident.test_executed = GNUNET_YES;
925 send_data ( &test_too_long_ident, host_str);
926 GNUNET_free (host_str);
929 if (test_valid_ident.test_executed == GNUNET_NO)
931 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
932 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
933 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen ((char *) &ident));
934 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,(char *) &ident);
936 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
937 test_valid_ident.test_executed = GNUNET_YES;
938 send_data ( &test_valid_ident, host_str);
939 GNUNET_free (host_str);
945 /* Using one of the addresses the plugin proposed */
946 GNUNET_assert (addr_head->addr != NULL);
948 struct Plugin_Address * tmp_addr;
949 struct GNUNET_MessageHeader msg;
950 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
951 char address[INET6_ADDRSTRLEN];
953 unsigned int type = 10;
955 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
956 tmp_addr = addr_head;
957 /* send a message to all addresses advertised by plugin */
960 while (tmp_addr != NULL)
962 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
964 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
965 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
968 if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
970 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
971 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
974 msg.type=htons(type);
975 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
976 api->send(api->cls, &my_identity, tmp, sizeof(struct GNUNET_MessageHeader), 0, TIMEOUT, NULL,tmp_addr->addr, tmp_addr->addrlen, GNUNET_YES, &task_send_cont, &fail_msgs_transmited_to_local_addrs);
977 tmp_addr = tmp_addr->next;
987 struct GNUNET_MessageHeader msg;
988 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
989 /* send a multiple GNUNET_messages at a time*/
991 tmp = GNUNET_malloc(4 * sizeof(struct GNUNET_MessageHeader));
992 struct GNUNET_MessageHeader * msg1 = (struct GNUNET_MessageHeader *) tmp;
993 msg1->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
994 msg1->type = htons(40);
995 struct GNUNET_MessageHeader * msg2 = &msg1[2];
996 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
997 msg2->type = htons(41);
998 api->send(api->cls, &my_identity, tmp, 4 * sizeof(struct GNUNET_MessageHeader), 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_YES, &task_send_cont, &fail_multiple_msgs_in_transmission);
1000 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1002 tmp = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE-1);
1003 uint16_t t = (uint16_t)GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1004 msg.size = htons(t);
1005 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1006 api->send(api->cls, &my_identity, tmp, GNUNET_SERVER_MAX_MESSAGE_SIZE-1, 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_YES, &task_send_cont, &fail_msg_transmited_max_size);
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1016 * @param cls closure
1017 * @param s scheduler to use
1018 * @param c configuration to use
1022 struct GNUNET_SCHEDULER_Handle *s,
1024 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1030 unsigned long long tneigh;
1031 struct Plugin_Address * cur;
1032 const char * addr_str;
1035 unsigned int suggest_res;
1037 fail_pretty_printer = GNUNET_YES;
1038 fail_notify_address = GNUNET_YES;
1039 fail_addr_to_str = GNUNET_YES;
1040 fail_msgs_transmited_to_local_addrs = 0;
1041 fail_msg_transmited_max_size = GNUNET_YES;
1042 fail_multiple_msgs_in_transmission = 0;
1046 /* parse configuration */
1048 GNUNET_CONFIGURATION_get_value_number (c,
1053 GNUNET_CONFIGURATION_get_value_filename (c,
1055 "HOSTKEY", &keyfile)))
1057 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1059 ("Transport service is lacking key configuration settings. Exiting.\n"));
1060 GNUNET_SCHEDULER_shutdown (s);
1066 GNUNET_CONFIGURATION_get_value_number (cfg,
1070 (port > 65535) || (port == 0))
1072 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1075 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1079 max_connect_per_transport = (uint32_t) tneigh;
1080 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1081 GNUNET_free (keyfile);
1082 if (my_private_key == NULL)
1084 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1085 _("Transport service could not access hostkey. Exiting.\n"));
1086 GNUNET_SCHEDULER_shutdown (s);
1091 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1092 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1094 /* assertions before start */
1095 GNUNET_assert ((port > 0) && (port <= 65535));
1096 GNUNET_assert(&my_public_key != NULL);
1097 GNUNET_assert(&my_identity.hashPubKey != NULL);
1099 /* load plugins... */
1100 setup_plugin_environment ();
1101 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1102 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1103 api = GNUNET_PLUGIN_load (libname, &env);
1104 GNUNET_free (libname);
1107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1108 _("Failed to load transport plugin for http\n"));
1113 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1115 /* testing plugin functionality */
1116 GNUNET_assert (0!=fail_notify_address_count);
1117 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1119 /* testing pretty printer with all addresses obtained from the plugin*/
1124 api->address_pretty_printer (api->cls, "http",cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT, &pretty_printer_cb,NULL);
1125 addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1126 suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1128 GNUNET_assert (GNUNET_OK == suggest_res);
1129 GNUNET_assert (NULL != addr_str);
1131 GNUNET_free ( (char *) addr_str);
1134 GNUNET_assert (fail_pretty_printer_count > 0);
1135 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1136 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1137 fail_pretty_printer=GNUNET_NO;
1138 fail_addr_to_str=GNUNET_NO;
1140 /* Suggesting addresses with wrong port*/
1141 struct IPv4HttpAddress failing_addr;
1142 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1143 failing_addr.u_port = htons(0);
1144 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv4HttpAddress));
1145 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1147 /* Suggesting addresses with wrong size*/
1148 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1149 failing_addr.u_port = htons(0);
1150 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv6HttpAddress));
1151 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1153 /* Suggesting addresses with wrong address*/
1154 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1155 failing_addr.u_port = htons(12389);
1156 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv4HttpAddress));
1157 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1159 /* test sending to client */
1160 multi_handle = curl_multi_init();
1162 /* Setting up buffers */
1163 buffer_in.size = HTTP_BUFFER_SIZE;
1167 buffer_out.size = HTTP_BUFFER_SIZE;
1171 /* Setting up connection tests */
1173 /* Test: connecting without a peer identification */
1174 test_no_ident.test_executed = GNUNET_NO;
1175 test_no_ident.test_failed = GNUNET_YES;
1177 /* Test: connecting with too short peer identification */
1178 test_too_short_ident.test_executed = GNUNET_NO;
1179 test_too_short_ident.test_failed = GNUNET_YES;
1181 /* Test: connecting with too long peer identification */
1182 test_too_long_ident.test_executed = GNUNET_NO;
1183 test_too_long_ident.test_failed = GNUNET_YES;
1185 /* Test: connecting with valid identification */
1186 test_valid_ident.test_executed = GNUNET_NO;
1187 test_valid_ident.test_failed = GNUNET_YES;
1189 test_addr = (char *) api->address_to_string (api->cls,addr_head->addr,addr_head->addrlen);
1190 run_connection_tests(0);
1192 /* testing finished */
1199 * The main function for the transport service.
1201 * @param argc number of arguments from the command line
1202 * @param argv command line arguments
1203 * @return 0 ok, 1 on error
1206 main (int argc, char *const *argv)
1209 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1210 GNUNET_GETOPT_OPTION_END
1213 char *const argv_prog[] = {
1214 "test_plugin_transport_http",
1216 "test_plugin_transport_data_http.conf",
1225 GNUNET_log_setup ("test_plugin_transport_http",
1234 GNUNET_PROGRAM_run (5,
1236 "test_plugin_transport_http",
1237 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1239 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1244 /* end of test_plugin_transport_http.c */