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 3, 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>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
47 #define VERBOSE GNUNET_YES
48 #define DEBUG GNUNET_YES
49 #define DEBUG_CURL GNUNET_NO
50 #define HTTP_BUFFER_SIZE 2048
52 #define PLUGIN libgnunet_plugin_transport_template
55 * How long until we give up on transmitting the message?
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
62 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
65 * How long between recieve and send?
67 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
72 * Struct for plugin addresses
77 * Next field for linked list
79 struct Plugin_Address * next;
82 * buffer containing data to send
87 * amount of data to sent
93 * Message to send using http
100 unsigned char buf[HTTP_BUFFER_SIZE];
103 * current position in buffer
120 * Struct for plugin addresses
125 * amount of bytes we recieved
130 * buffer for http transfers
132 unsigned char buf[HTTP_BUFFER_SIZE];
135 * buffer size this transfer
140 * amount of bytes we recieved
145 * HTTP Header result for transfer
147 unsigned int http_result_code;
152 unsigned int test_failed;
155 * was this test already executed?
157 unsigned int test_executed;
162 * Network format for IPv4 addresses.
164 struct IPv4HttpAddress
167 * IPv4 address, in network byte order.
169 uint32_t ipv4_addr GNUNET_PACKED;
172 * Port number, in network byte order.
174 uint16_t u_port GNUNET_PACKED;
180 * Network format for IPv6 addresses.
182 struct IPv6HttpAddress
187 struct in6_addr ipv6_addr GNUNET_PACKED;
190 * Port number, in network byte order.
192 uint16_t u6_port GNUNET_PACKED;
199 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
204 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
209 static struct GNUNET_PeerIdentity my_identity;
214 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
219 static long long unsigned int port;
224 static char * test_addr;
229 struct GNUNET_SCHEDULER_Handle *sched;
232 * Our statistics handle.
234 struct GNUNET_STATISTICS_Handle *stats;
240 const struct GNUNET_CONFIGURATION_Handle *cfg;
243 * Number of neighbours we'd like to have.
245 static uint32_t max_connect_per_transport;
248 * Environment for this plugin.
250 static struct GNUNET_TRANSPORT_PluginEnvironment env;
253 *handle for the api provided by this plugin
255 static struct GNUNET_TRANSPORT_PluginFunctions *api;
258 * ID of the task controlling the testcase timeout
260 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
262 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
264 //const struct GNUNET_PeerIdentity * p;
267 * buffer for data to send
269 static struct HTTP_Message buffer_out;
272 * buffer for data to recieve
274 static struct HTTP_Message buffer_in;
277 struct Plugin_Address * addr_head;
280 * Did the test pass or fail?
282 static int fail_notify_address;
284 * Did the test pass or fail?
286 static int fail_notify_address_count;
289 * Did the test pass or fail?
291 static int fail_pretty_printer;
294 * Did the test pass or fail?
296 static int fail_pretty_printer_count;
299 * Did the test pass or fail?
301 static int fail_addr_to_str;
304 * No. of msgs transmitted successfully to local addresses
306 static int fail_msgs_transmited_to_local_addrs;
309 * Test: transmit msg of max. size
311 static int fail_msg_transmited_bigger_max_size;
314 * Test: transmit msg of max. size
316 static int fail_msg_transmited_max_size;
319 * Test: transmit 2 msgs. in in send operation
321 static int fail_multiple_msgs_in_transmission;
324 * Test: connect to peer without peer identification
326 static struct HTTP_Transfer test_no_ident;
329 * Test: connect to peer without peer identification
331 static struct HTTP_Transfer test_too_short_ident;
334 * Test: connect to peer without peer identification
336 static struct HTTP_Transfer test_too_long_ident;
339 * Test: connect to peer with valid peer identification
341 static struct HTTP_Transfer test_valid_ident;
344 * Test: session selection, use any existing
346 static int fail_session_selection_any;
349 * Test: session selection, use existing inbound session
351 static int fail_session_selection_session;
354 * Test: session selection, use existing inbound session
355 * max message, not fitting in send & recv buffers at one time
357 static int fail_session_selection_session_big;
360 * Test: session selection, use reliable existing
362 static int fail_session_selection_reliable;
365 * Did the test pass or fail?
370 * Number of local addresses
372 static unsigned int count_str_addr;
379 static CURLM *multi_handle;
382 * The task sending data
384 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
392 struct Plugin_Address * cur;
393 struct Plugin_Address * tmp;
395 /* Evaluate results */
397 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 0: Test plugin functions failed\n");
402 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))
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 1: Test connect with wrong data failed\n");
407 if ((fail_session_selection_any != GNUNET_NO) || (fail_session_selection_reliable != GNUNET_NO) || (fail_session_selection_session != GNUNET_NO) || (fail_session_selection_session_big != GNUNET_NO))
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 2: Test session selection failed\n");
412 if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_multiple_msgs_in_transmission != 2) || (fail_msg_transmited_max_size == GNUNET_YES))
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 3: Test sending with plugin failed\n");
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
422 api->disconnect(api->cls,&my_identity);
424 curl_multi_cleanup(multi_handle);
426 if (NULL != curl_handle)
427 curl_easy_cleanup (curl_handle);
429 /* cleaning addresses */
430 while (addr_head != NULL)
433 tmp = addr_head->next;
434 GNUNET_free (addr_head->addr);
435 GNUNET_free (addr_head);
439 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
441 GNUNET_SCHEDULER_cancel(sched,ti_send);
442 ti_send = GNUNET_SCHEDULER_NO_TASK;
445 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
447 GNUNET_SCHEDULER_cancel(sched,http_task_send);
448 http_task_send = GNUNET_SCHEDULER_NO_TASK;
451 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
453 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
454 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
457 GNUNET_free(test_addr);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
459 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
461 GNUNET_SCHEDULER_shutdown(sched);
462 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
471 * Continuation called after plugin send message
474 * @result GNUNET_OK or GNUNET_SYSERR
477 static void task_send_cont (void *cls,
478 const struct GNUNET_PeerIdentity * target,
481 struct Plugin_Address * tmp_addr;
482 tmp_addr = addr_head;
484 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
487 fail_msg_transmited_bigger_max_size = GNUNET_NO;
491 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
494 fail_msg_transmited_max_size = GNUNET_NO;
499 static void run_connection_tests( int phase , void * cls);
502 * Recieves messages from plugin, in real world transport
504 static struct GNUNET_TIME_Relative
506 const struct GNUNET_PeerIdentity * peer,
507 const struct GNUNET_MessageHeader * message,
509 struct Session *session,
510 const char *sender_address,
511 uint16_t sender_address_len)
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase recieved new message from peer `%s' with type %u and length %u, session %X\n", GNUNET_i2s(peer), ntohs(message->type), ntohs(message->size),session);
515 if ((ntohs(message->type)>=10) && (ntohs(message->type)<20))
517 fail_msgs_transmited_to_local_addrs++;
518 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
519 run_connection_tests(2, session);
523 if ((ntohs(message->type)==20))
525 fail_session_selection_reliable = GNUNET_NO;
528 if ((ntohs(message->type)==21))
530 fail_session_selection_any = GNUNET_NO;
532 if ((ntohs(message->type)==22))
534 fail_session_selection_session = GNUNET_NO;
537 if ((ntohs(message->type)==23))
539 fail_session_selection_session_big = GNUNET_NO;
540 run_connection_tests(3, NULL);
543 if ((ntohs(message->type)==30) || (ntohs(message->type)==31))
545 fail_multiple_msgs_in_transmission ++;
548 if ((ntohs(message->type)==32) && (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1))
550 fail_msg_transmited_max_size = GNUNET_NO;
554 return GNUNET_TIME_UNIT_ZERO;
557 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
561 len = buffer_out.len;
563 if (( buffer_out.pos == len) || (len > (size * nmemb)))
565 memcpy(stream, buffer_out.buf, len);
566 buffer_out.pos = len;
570 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
573 if (buffer_in.pos + size * nmemb > buffer_in.size)
574 return 0; /* overflow */
576 buffer_in.len = size * nmemb;
577 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
578 buffer_in.pos += size * nmemb;
579 buffer_in.len = buffer_in.pos;
580 buffer_in.buf[buffer_in.pos] = '\0';
581 return buffer_in.pos;
584 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
586 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
588 unsigned int len = size * nmemb;
590 tmp = GNUNET_malloc ( len+1 );
592 if (tmp[len-2] == 13)
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
597 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
599 res->http_result_code=100;
601 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
603 res->http_result_code=200;
605 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
607 res->http_result_code=400;
609 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
611 res->http_result_code=404;
613 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
615 res->http_result_code=413;
622 static size_t send_prepare( struct HTTP_Transfer * result);
626 static void send_execute (void *cls,
627 const struct GNUNET_SCHEDULER_TaskContext *tc)
629 struct HTTP_Transfer *res;
635 res = (struct HTTP_Transfer *) cls;
636 http_task_send = GNUNET_SCHEDULER_NO_TASK;
637 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
643 mret = curl_multi_perform (multi_handle, &running);
649 msg = curl_multi_info_read (multi_handle, &running);
652 /* get session for affected curl handle */
653 //cs = find_session_by_curlhandle (msg->easy_handle);
654 //GNUNET_assert ( cs != NULL );
659 if ( (msg->data.result != CURLE_OK) &&
660 (msg->data.result != CURLE_GOT_NOTHING) )
663 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
664 _("curl failed for `%s' at %s:%d: `%s'\n"),
665 "curl_multi_perform",
668 curl_easy_strerror (msg->data.result));
669 /* sending msg failed*/
670 curl_easy_cleanup(curl_handle);
673 run_connection_tests(0, NULL);
675 if (res == &test_no_ident)
677 if ((res->http_result_code==404) && (buffer_in.len==208))
679 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
680 res->test_failed = GNUNET_NO;
683 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
685 if (res == &test_too_short_ident)
687 if ((res->http_result_code==404) && (buffer_in.len==208))
689 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
690 res->test_failed = GNUNET_NO;
693 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
695 if (res == &test_too_long_ident)
697 if ((res->http_result_code==404) && (buffer_in.len==208))
699 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
700 res->test_failed = GNUNET_NO;
703 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
705 if (res == &test_valid_ident)
707 if ((res->http_result_code==200))
709 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
710 res->test_failed = GNUNET_NO;
713 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
715 curl_easy_cleanup(curl_handle);
717 if ((res == &test_valid_ident) && (res->test_failed == GNUNET_NO))
718 run_connection_tests(1, NULL);
719 run_connection_tests(0, NULL);
726 while ( (running > 0) );
729 while (mret == CURLM_CALL_MULTI_PERFORM);
734 * Function setting up file descriptors and scheduling task to run
735 * @param ses session to send data to
736 * @return bytes sent to peer
738 static size_t send_prepare( struct HTTP_Transfer * result)
744 struct GNUNET_NETWORK_FDSet *grs;
745 struct GNUNET_NETWORK_FDSet *gws;
753 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
754 if (mret != CURLM_OK)
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757 _("%s failed at %s:%d: `%s'\n"),
758 "curl_multi_fdset", __FILE__, __LINE__,
759 curl_multi_strerror (mret));
762 mret = curl_multi_timeout (multi_handle, &to);
763 if (mret != CURLM_OK)
765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
766 _("%s failed at %s:%d: `%s'\n"),
767 "curl_multi_timeout", __FILE__, __LINE__,
768 curl_multi_strerror (mret));
772 grs = GNUNET_NETWORK_fdset_create ();
773 gws = GNUNET_NETWORK_fdset_create ();
774 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
775 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
776 http_task_send = GNUNET_SCHEDULER_add_select (sched,
777 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
778 GNUNET_SCHEDULER_NO_TASK,
779 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
784 GNUNET_NETWORK_fdset_destroy (gws);
785 GNUNET_NETWORK_fdset_destroy (grs);
787 /* FIXME: return bytes REALLY sent */
792 * function to send data to server
794 static int send_data( struct HTTP_Transfer * result, char * url)
797 curl_handle = curl_easy_init();
798 if( NULL == curl_handle)
800 printf("easy_init failed \n");
801 return GNUNET_SYSERR;
804 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
806 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
807 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
808 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
809 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
810 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
811 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
812 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
813 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
814 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
815 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
816 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
818 curl_multi_add_handle(multi_handle, curl_handle);
820 send_prepare(result);
826 * Plugin notifies transport (aka testcase) about its addresses
829 notify_address (void *cls,
833 struct GNUNET_TIME_Relative expires)
835 char address[INET6_ADDRSTRLEN];
837 struct Plugin_Address * pl_addr;
838 struct Plugin_Address * cur;
840 if (addrlen == (sizeof (struct IPv4HttpAddress)))
842 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
843 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
845 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
847 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
848 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 _("Unknown address size: ipv6 has %u ipv4 has %u but this has %u\n"),
854 sizeof (struct IPv6HttpAddress),
855 sizeof (struct IPv4HttpAddress),
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 _("Transport plugin notification for address: `%s':%u\n"),
863 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
864 pl_addr->addrlen = addrlen;
865 pl_addr->addr = GNUNET_malloc(addrlen);
866 memcpy(pl_addr->addr,addr,addrlen);
867 pl_addr->next = NULL;
869 if ( NULL == addr_head)
876 while (NULL != cur->next)
882 fail_notify_address_count++;
883 fail_notify_address = GNUNET_NO;
887 plugin_env_session_end (void *cls,
888 const struct GNUNET_PeerIdentity *peer,
889 struct Session *session)
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Pluging tells me: session %X to peer `%s' ended\n", session, GNUNET_i2s(peer));
896 * Setup plugin environment
899 setup_plugin_environment ()
904 env.my_identity = &my_identity;
906 env.receive = &receive;
907 env.notify_address = ¬ify_address;
908 env.max_connections = max_connect_per_transport;
909 env.session_end = &plugin_env_session_end;
914 * Task shutting down testcase if it a timeout occurs
917 task_timeout (void *cls,
918 const struct GNUNET_SCHEDULER_TaskContext *tc)
920 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
921 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
930 static void pretty_printer_cb (void *cls,
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
936 fail_pretty_printer_count++;
940 * Runs every single test to test the plugin
942 static void run_connection_tests( int phase , void * cls)
944 struct GNUNET_MessageHeader * msg;
949 char * host_str = NULL;
950 /* resetting buffers */
951 buffer_in.size = HTTP_BUFFER_SIZE;
955 buffer_out.size = HTTP_BUFFER_SIZE;
959 if (test_no_ident.test_executed == GNUNET_NO)
961 /* Connecting to peer without identification */
963 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
964 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
965 test_no_ident.test_executed = GNUNET_YES;
966 send_data ( &test_no_ident, host_str);
967 GNUNET_free (host_str);
970 if (test_too_short_ident.test_executed == GNUNET_NO)
972 char * ident = "AAAAAAAAAA";
973 /* Connecting to peer with too short identification */
974 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
975 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
976 test_too_short_ident.test_executed = GNUNET_YES;
977 send_data ( &test_too_short_ident, host_str);
978 GNUNET_free (host_str);
982 if (test_too_long_ident.test_executed == GNUNET_NO)
984 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
986 /* Connecting to peer with too long identification */
987 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
988 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
989 test_too_long_ident.test_executed = GNUNET_YES;
990 send_data ( &test_too_long_ident, host_str);
991 GNUNET_free (host_str);
994 if (test_valid_ident.test_executed == GNUNET_NO)
996 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
997 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
998 GNUNET_asprintf (&host_str, "http://%s/%s%s",test_addr,(char *) &ident,";0");
999 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
1000 test_valid_ident.test_executed = GNUNET_YES;
1001 send_data ( &test_valid_ident, host_str);
1002 GNUNET_free (host_str);
1008 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 1: transmit data to all suggested addresses\n\n"));
1009 /* Using one of the addresses the plugin proposed */
1010 GNUNET_assert (addr_head->addr != NULL);
1012 struct Plugin_Address * tmp_addr;
1013 struct GNUNET_MessageHeader msg;
1014 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
1015 char address[INET6_ADDRSTRLEN];
1017 unsigned int type = 10;
1019 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
1020 tmp_addr = addr_head;
1021 /* send a message to all addresses advertised by plugin */
1024 while (tmp_addr != NULL)
1026 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
1028 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
1029 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
1032 if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
1034 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
1035 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
1038 msg.type=htons(type);
1039 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1040 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);
1041 tmp_addr = tmp_addr->next;
1052 struct Session * session = cls;
1053 msg = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
1055 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 2: session selection\n\n"));
1056 size = sizeof(struct GNUNET_MessageHeader);
1057 msg->size=htons(size);
1058 msg->type = htons(20);
1059 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1061 msg->type = htons(21);
1062 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1064 /* answer on session*/
1065 size = sizeof( struct GNUNET_MessageHeader);
1066 msg->size = htons(size);
1067 msg->type = htons(22);
1068 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1072 /* answer on session with big message not fitting in mhd send buffer*/
1073 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1074 msg = GNUNET_malloc (size);
1075 msg->size=htons(size);
1076 msg->type = htons(23);
1077 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1085 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 3: send multiple or big messages after disconnect\n\n"));
1086 /* disconnect from peer, so new connections are created */
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Disconnect from peer: `%s'\n", GNUNET_i2s(&my_identity));
1088 api->disconnect(api->cls, &my_identity);
1090 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Phase 3: sending messages\n"));
1091 /* send a multiple GNUNET_messages at a time*/
1092 size = 2 * sizeof(struct GNUNET_MessageHeader);
1093 msg = GNUNET_malloc( 2* size);
1094 msg->size = htons(size);
1095 msg->type = htons(30);
1096 struct GNUNET_MessageHeader * msg2 = &msg[2];
1097 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1098 msg2->type = htons(31);
1099 api->send(api->cls, &my_identity, (const char *) msg, 4 * sizeof(struct GNUNET_MessageHeader), 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_NO, &task_send_cont, &fail_multiple_msgs_in_transmission);
1101 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1103 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1104 msg = GNUNET_malloc(size);
1105 msg->size = htons(size);
1106 msg->type = htons(32);
1107 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_NO, &task_send_cont, &fail_msg_transmited_max_size);
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1117 * @param cls closure
1118 * @param s scheduler to use
1119 * @param c configuration to use
1123 struct GNUNET_SCHEDULER_Handle *s,
1125 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1131 unsigned long long tneigh;
1132 struct Plugin_Address * cur;
1133 const char * addr_str;
1136 unsigned int suggest_res;
1138 fail_pretty_printer = GNUNET_YES;
1139 fail_notify_address = GNUNET_YES;
1140 fail_addr_to_str = GNUNET_YES;
1141 fail_msgs_transmited_to_local_addrs = 0;
1142 fail_msg_transmited_max_size = GNUNET_YES;
1143 fail_multiple_msgs_in_transmission = 0;
1144 fail_session_selection_reliable = GNUNET_YES;
1145 fail_session_selection_reliable = GNUNET_YES;
1146 fail_session_selection_session = GNUNET_YES;
1147 fail_session_selection_session_big = GNUNET_YES;
1151 /* parse configuration */
1153 GNUNET_CONFIGURATION_get_value_number (c,
1158 GNUNET_CONFIGURATION_get_value_filename (c,
1160 "HOSTKEY", &keyfile)))
1162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1164 ("Transport service is lacking key configuration settings. Exiting.\n"));
1165 GNUNET_SCHEDULER_shutdown (s);
1171 GNUNET_CONFIGURATION_get_value_number (cfg,
1175 (port > 65535) || (port == 0))
1177 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1180 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1184 max_connect_per_transport = (uint32_t) tneigh;
1185 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1186 GNUNET_free (keyfile);
1187 if (my_private_key == NULL)
1189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1190 _("Transport service could not access hostkey. Exiting.\n"));
1191 GNUNET_SCHEDULER_shutdown (s);
1196 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1197 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1199 /* assertions before start */
1200 GNUNET_assert ((port > 0) && (port <= 65535));
1201 GNUNET_assert(&my_public_key != NULL);
1202 GNUNET_assert(&my_identity.hashPubKey != NULL);
1204 /* load plugins... */
1205 setup_plugin_environment ();
1206 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1207 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1208 api = GNUNET_PLUGIN_load (libname, &env);
1209 GNUNET_free (libname);
1212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1213 _("Failed to load transport plugin for http\n"));
1218 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1220 /* testing plugin functionality */
1221 GNUNET_assert (0!=fail_notify_address_count);
1222 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1224 /* testing pretty printer with all addresses obtained from the plugin*/
1229 api->address_pretty_printer (api->cls, "http",cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT, &pretty_printer_cb,NULL);
1230 addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1231 suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1233 GNUNET_assert (GNUNET_OK == suggest_res);
1234 GNUNET_assert (NULL != addr_str);
1236 GNUNET_free ( (char *) addr_str);
1239 GNUNET_assert (fail_pretty_printer_count > 0);
1240 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1241 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1242 fail_pretty_printer=GNUNET_NO;
1243 fail_addr_to_str=GNUNET_NO;
1245 /* Suggesting addresses with wrong port*/
1246 struct IPv4HttpAddress failing_addr;
1247 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1248 failing_addr.u_port = htons(0);
1249 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv4HttpAddress));
1250 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1252 /* Suggesting addresses with wrong size*/
1253 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1254 failing_addr.u_port = htons(0);
1255 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv6HttpAddress));
1256 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1258 /* Suggesting addresses with wrong address*/
1259 failing_addr.ipv4_addr = htonl(0xffc00000);
1260 failing_addr.u_port = htons(12389);
1261 suggest_res = api->check_address (api->cls,&failing_addr,100);
1262 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1264 /* test sending to client */
1265 multi_handle = curl_multi_init();
1267 /* Setting up buffers */
1268 buffer_in.size = HTTP_BUFFER_SIZE;
1272 buffer_out.size = HTTP_BUFFER_SIZE;
1276 /* Setting up connection tests */
1278 /* Test: connecting without a peer identification */
1279 test_no_ident.test_executed = GNUNET_NO;
1280 test_no_ident.test_failed = GNUNET_YES;
1282 /* Test: connecting with too short peer identification */
1283 test_too_short_ident.test_executed = GNUNET_NO;
1284 test_too_short_ident.test_failed = GNUNET_YES;
1286 /* Test: connecting with too long peer identification */
1287 test_too_long_ident.test_executed = GNUNET_NO;
1288 test_too_long_ident.test_failed = GNUNET_YES;
1290 /* Test: connecting with valid identification */
1291 test_valid_ident.test_executed = GNUNET_NO;
1292 test_valid_ident.test_failed = GNUNET_YES;
1294 test_addr = (char *) api->address_to_string (api->cls,addr_head->addr,addr_head->addrlen);
1296 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 0\n\n"));
1297 run_connection_tests(0, NULL);
1299 /* testing finished */
1306 * The main function for the transport service.
1308 * @param argc number of arguments from the command line
1309 * @param argv command line arguments
1310 * @return 0 ok, 1 on error
1313 main (int argc, char *const *argv)
1316 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1317 GNUNET_GETOPT_OPTION_END
1320 char *const argv_prog[] = {
1321 "test_plugin_transport_http",
1323 "test_plugin_transport_data_http.conf",
1332 GNUNET_log_setup ("test_plugin_transport_http",
1341 GNUNET_PROGRAM_run (5,
1343 "test_plugin_transport_http",
1344 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1346 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1351 /* end of test_plugin_transport_http.c */