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 "gnunet_transport_plugin.h"
41 #include "gnunet_statistics_service.h"
42 #include "transport.h"
43 #include <curl/curl.h>
45 #define VERBOSE GNUNET_NO
46 #define DEBUG_CURL GNUNET_NO
47 #define HTTP_BUFFER_SIZE 2048
49 #define PROTOCOL_PREFIX "http"
51 #define PLUGIN libgnunet_plugin_transport_template
54 * How long until we give up on transmitting the message?
56 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
61 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
64 * How long between recieve and send?
66 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
71 * Struct for plugin addresses
76 * Next field for linked list
78 struct Plugin_Address * next;
81 * buffer containing data to send
86 * amount of data to sent
92 * Message to send using http
99 unsigned char buf[HTTP_BUFFER_SIZE];
102 * current position in buffer
119 * Struct for plugin addresses
124 * amount of bytes we recieved
129 * buffer for http transfers
131 unsigned char buf[HTTP_BUFFER_SIZE];
134 * buffer size this transfer
139 * amount of bytes we recieved
144 * HTTP Header result for transfer
146 unsigned int http_result_code;
151 unsigned int test_failed;
154 * was this test already executed?
156 unsigned int test_executed;
161 * Network format for IPv4 addresses.
163 struct IPv4HttpAddress
165 struct IPv4HttpAddress * next;
167 struct IPv4HttpAddress * prev;
170 * IPv4 address, in network byte order.
172 uint32_t ipv4_addr GNUNET_PACKED;
175 * Port number, in network byte order.
177 uint16_t u_port GNUNET_PACKED;
183 * Network format for IPv6 addresses.
185 struct IPv6HttpAddress
187 struct IPv6HttpAddress * next;
189 struct IPv6HttpAddress * prev;
194 struct in6_addr ipv6_addr GNUNET_PACKED;
197 * Port number, in network byte order.
199 uint16_t u6_port GNUNET_PACKED;
207 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
212 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
217 static struct GNUNET_PeerIdentity my_identity;
222 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
227 static long long unsigned int port;
232 static char * test_addr;
235 * Our statistics handle.
237 struct GNUNET_STATISTICS_Handle *stats;
243 const struct GNUNET_CONFIGURATION_Handle *cfg;
246 * Number of neighbours we'd like to have.
248 static uint32_t max_connect_per_transport;
251 * Environment for this plugin.
253 static struct GNUNET_TRANSPORT_PluginEnvironment env;
256 *handle for the api provided by this plugin
258 static struct GNUNET_TRANSPORT_PluginFunctions *api;
261 * ID of the task controlling the testcase timeout
263 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
265 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
267 //const struct GNUNET_PeerIdentity * p;
270 * buffer for data to send
272 static struct HTTP_Message buffer_out;
275 * buffer for data to recieve
277 static struct HTTP_Message buffer_in;
280 struct Plugin_Address * addr_head;
283 * Did the test pass or fail?
285 static int fail_notify_address;
287 * Did the test pass or fail?
289 static int fail_notify_address_count;
292 * Did the test pass or fail?
294 static int fail_pretty_printer;
297 * Did the test pass or fail?
299 static int fail_pretty_printer_count;
302 * Did the test pass or fail?
304 static int fail_addr_to_str;
307 * No. of msgs transmitted successfully to local addresses
309 static int fail_msgs_transmited_to_local_addrs;
312 * Test: transmit msg of max. size
314 static int fail_msg_transmited_bigger_max_size;
317 * Test: transmit msg of max. size
319 static int fail_msg_transmited_max_size;
322 * Test: transmit 2 msgs. in in send operation
324 static int fail_multiple_msgs_in_transmission;
327 * Test: connect to peer without peer identification
329 static struct HTTP_Transfer test_no_ident;
332 * Test: connect to peer without peer identification
334 static struct HTTP_Transfer test_too_short_ident;
337 * Test: connect to peer without peer identification
339 static struct HTTP_Transfer test_too_long_ident;
342 * Test: connect to peer with valid peer identification
344 static struct HTTP_Transfer test_valid_ident;
347 * Test: session selection, use any existing
349 static int fail_session_selection_any;
352 * Test: session selection, use existing inbound session
354 static int fail_session_selection_session;
357 * Test: session selection, use existing inbound session
358 * max message, not fitting in send & recv buffers at one time
360 static int fail_session_selection_session_big;
363 * Test: session selection, use reliable existing
365 static int fail_session_selection_reliable;
368 * Did the test pass or fail?
373 * Number of local addresses
375 static unsigned int count_str_addr;
382 static CURLM *multi_handle;
385 * The task sending data
387 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
389 static char * servicehome;
397 struct Plugin_Address * tmp;
399 /* Evaluate results */
401 if ( (fail_notify_address == GNUNET_YES) ||
402 (fail_pretty_printer == GNUNET_YES) ||
403 (fail_addr_to_str == GNUNET_YES))
405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
406 "Phase 0: Test plugin functions failed\n");
409 if ( (test_no_ident.test_failed == GNUNET_YES) ||
410 (test_too_short_ident.test_failed == GNUNET_YES) ||
411 (test_too_long_ident.test_failed == GNUNET_YES) ||
412 (test_valid_ident.test_failed == GNUNET_YES) )
414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
415 "Phase 1: Test connect with wrong data failed\n");
418 if ( (fail_session_selection_any != GNUNET_NO) ||
419 (fail_session_selection_reliable != GNUNET_NO) ||
420 (fail_session_selection_session != GNUNET_NO) ||
421 (fail_session_selection_session_big != GNUNET_NO) )
423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
424 "Phase 2: Test session selection failed\n");
427 if ( (fail_msgs_transmited_to_local_addrs != count_str_addr) ||
428 (fail_multiple_msgs_in_transmission != 2) ||
429 (fail_msg_transmited_max_size == GNUNET_YES) )
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
432 "Phase 3: Test sending with plugin failed\n");
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "All tests successful\n");
441 api->disconnect(api->cls,&my_identity);
443 curl_multi_cleanup(multi_handle);
445 if (NULL != curl_handle)
446 curl_easy_cleanup (curl_handle);
448 /* cleaning addresses */
449 while (addr_head != NULL)
451 tmp = addr_head->next;
452 GNUNET_free (addr_head->addr);
453 GNUNET_free (addr_head);
457 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
459 GNUNET_SCHEDULER_cancel(ti_send);
460 ti_send = GNUNET_SCHEDULER_NO_TASK;
463 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
465 GNUNET_SCHEDULER_cancel(http_task_send);
466 http_task_send = GNUNET_SCHEDULER_NO_TASK;
469 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
471 GNUNET_SCHEDULER_cancel(ti_timeout);
472 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
475 GNUNET_free(test_addr);
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Unloading http plugin\n");
478 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_gnunet_transport_plugin_http", api));
480 GNUNET_SCHEDULER_shutdown();
481 GNUNET_DISK_directory_remove ("/tmp/test_gnunet_transport_plugin_http");
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Exiting testcase\n");
486 if (servicehome != NULL)
488 GNUNET_DISK_directory_remove (servicehome);
489 GNUNET_free (servicehome);
498 * Continuation called after plugin send message
501 * @result GNUNET_OK or GNUNET_SYSERR
505 task_send_cont (void *cls,
506 const struct GNUNET_PeerIdentity * target,
509 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512 "Message bigger max msg size was not sent!\n");
513 fail_msg_transmited_bigger_max_size = GNUNET_NO;
517 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Message with max msg size succesfully sent!\n",
521 fail_msgs_transmited_to_local_addrs);
522 fail_msg_transmited_max_size = GNUNET_NO;
527 static void run_connection_tests( int phase , void * cls);
530 * Recieves messages from plugin, in real world transport
532 static struct GNUNET_TIME_Relative
533 receive (void *cls, const struct GNUNET_PeerIdentity *peer,
534 const struct GNUNET_MessageHeader *message,
535 const struct GNUNET_TRANSPORT_ATS_Information *ats,
537 struct Session *session,
538 const char *sender_address,
539 uint16_t sender_address_len)
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Testcase recieved new message from peer `%s' with type %u and length %u, session %X\n",
544 ntohs(message->type),
545 ntohs(message->size),
548 if ( (ntohs(message->type)>=10) &&
549 (ntohs(message->type)<20) )
551 fail_msgs_transmited_to_local_addrs++;
552 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
553 run_connection_tests(2, session);
555 if ((ntohs(message->type)==20))
557 fail_session_selection_reliable = GNUNET_NO;
559 if ((ntohs(message->type)==21))
561 fail_session_selection_any = GNUNET_NO;
563 if ((ntohs(message->type)==22))
565 fail_session_selection_session = GNUNET_NO;
567 if ((ntohs(message->type)==23))
569 fail_session_selection_session_big = GNUNET_NO;
570 run_connection_tests(3, NULL);
572 if ((ntohs(message->type)==30) || (ntohs(message->type)==31))
574 fail_multiple_msgs_in_transmission ++;
576 if ((ntohs(message->type)==32) && (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1))
578 fail_msg_transmited_max_size = GNUNET_NO;
581 return GNUNET_TIME_UNIT_ZERO;
586 send_function (void *stream, size_t size, size_t nmemb, void *ptr)
590 len = buffer_out.len;
592 if (( buffer_out.pos == len) || (len > (size * nmemb)))
594 memcpy(stream, buffer_out.buf, len);
595 buffer_out.pos = len;
601 recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
604 if (buffer_in.pos + size * nmemb > buffer_in.size)
605 return 0; /* overflow */
607 buffer_in.len = size * nmemb;
608 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
609 buffer_in.pos += size * nmemb;
610 buffer_in.len = buffer_in.pos;
611 buffer_in.buf[buffer_in.pos] = '\0';
612 return buffer_in.pos;
616 header_function( void *ptr, size_t size, size_t nmemb, void *stream)
618 struct HTTP_Transfer * res = stream;
620 unsigned int len = size * nmemb;
622 tmp = GNUNET_malloc ( len+1 );
624 if (tmp[len-2] == 13)
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
633 res->http_result_code=100;
635 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
637 res->http_result_code=200;
639 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
641 res->http_result_code=400;
643 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
645 res->http_result_code=404;
647 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
649 res->http_result_code=413;
658 send_prepare( struct HTTP_Transfer * result);
662 send_execute (void *cls,
663 const struct GNUNET_SCHEDULER_TaskContext *tc)
665 struct HTTP_Transfer *res;
671 res = (struct HTTP_Transfer *) cls;
672 http_task_send = GNUNET_SCHEDULER_NO_TASK;
673 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
679 mret = curl_multi_perform (multi_handle, &running);
685 msg = curl_multi_info_read (multi_handle, &running);
688 /* get session for affected curl handle */
689 //cs = find_session_by_curlhandle (msg->easy_handle);
690 //GNUNET_assert ( cs != NULL );
695 if ( (msg->data.result != CURLE_OK) &&
696 (msg->data.result != CURLE_GOT_NOTHING) )
699 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
700 _("curl failed for `%s' at %s:%d: `%s'\n"),
701 "curl_multi_perform",
704 curl_easy_strerror (msg->data.result));
705 /* sending msg failed*/
706 curl_easy_cleanup(curl_handle);
709 run_connection_tests(0, NULL);
711 if (res == &test_no_ident)
713 if ((res->http_result_code==404) && (buffer_in.len==208))
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Connecting to peer without any peer identification: test passed\n");
717 res->test_failed = GNUNET_NO;
720 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
721 _("Connecting to peer without any peer identification: test failed\n"));
723 if (res == &test_too_short_ident)
725 if ((res->http_result_code==404) && (buffer_in.len==208))
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Connecting to peer with too short peer identification: test passed\n");
729 res->test_failed = GNUNET_NO;
732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
733 _("Connecting to peer with too short peer identification: test failed\n"));
735 if (res == &test_too_long_ident)
737 if ((res->http_result_code==404) && (buffer_in.len==208))
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Connecting to peer with too long peer identification: test passed\n");
741 res->test_failed = GNUNET_NO;
744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
745 _("Connecting to peer with too long peer identification: test failed\n"));
747 if (res == &test_valid_ident)
749 if ((res->http_result_code==200))
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Connecting to peer with valid peer identification: test passed\n");
753 res->test_failed = GNUNET_NO;
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757 "Connecting to peer with valid peer identification: test failed\n");
759 curl_easy_cleanup(curl_handle);
761 if ((res == &test_valid_ident) && (res->test_failed == GNUNET_NO))
762 run_connection_tests(1, NULL);
763 run_connection_tests(0, NULL);
770 while ( (running > 0) );
773 while (mret == CURLM_CALL_MULTI_PERFORM);
779 * Function setting up file descriptors and scheduling task to run
780 * @param ses session to send data to
781 * @return bytes sent to peer
784 send_prepare( struct HTTP_Transfer * result)
790 struct GNUNET_NETWORK_FDSet *grs;
791 struct GNUNET_NETWORK_FDSet *gws;
799 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
800 if (mret != CURLM_OK)
802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
803 _("%s failed at %s:%d: `%s'\n"),
804 "curl_multi_fdset", __FILE__, __LINE__,
805 curl_multi_strerror (mret));
808 mret = curl_multi_timeout (multi_handle, &to);
809 if (mret != CURLM_OK)
811 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
812 _("%s failed at %s:%d: `%s'\n"),
813 "curl_multi_timeout", __FILE__, __LINE__,
814 curl_multi_strerror (mret));
818 grs = GNUNET_NETWORK_fdset_create ();
819 gws = GNUNET_NETWORK_fdset_create ();
820 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
821 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
822 http_task_send = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
823 GNUNET_SCHEDULER_NO_TASK,
824 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
829 GNUNET_NETWORK_fdset_destroy (gws);
830 GNUNET_NETWORK_fdset_destroy (grs);
832 /* FIXME: return bytes REALLY sent */
837 * function to send data to server
840 send_data(struct HTTP_Transfer * result,
844 curl_handle = curl_easy_init();
845 if( NULL == curl_handle)
847 printf("easy_init failed \n");
848 return GNUNET_SYSERR;
851 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
853 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
854 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
855 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
856 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
857 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
858 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
859 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
860 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
861 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
862 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
863 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
865 curl_multi_add_handle(multi_handle, curl_handle);
867 send_prepare(result);
873 * Plugin notifies transport (aka testcase) about its addresses
876 notify_address (void *cls,
881 char address[INET6_ADDRSTRLEN];
883 struct Plugin_Address * pl_addr;
884 struct Plugin_Address * cur;
886 if (addrlen == (sizeof (struct IPv4HttpAddress)))
888 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
889 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
891 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
893 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
894 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 _("Unknown address size: ipv6 has %u ipv4 has %u but this has %u\n"),
900 sizeof (struct IPv6HttpAddress),
901 sizeof (struct IPv4HttpAddress),
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 _("Transport plugin notification for address: `%s':%u\n"),
909 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
910 pl_addr->addrlen = addrlen;
911 pl_addr->addr = GNUNET_malloc(addrlen);
912 memcpy(pl_addr->addr,addr,addrlen);
913 pl_addr->next = NULL;
915 if ( NULL == addr_head)
922 while (NULL != cur->next)
928 fail_notify_address_count++;
929 fail_notify_address = GNUNET_NO;
933 plugin_env_session_end (void *cls,
934 const struct GNUNET_PeerIdentity *peer,
935 struct Session *session)
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Pluging tells me: session %X to peer `%s' ended\n",
945 * Setup plugin environment
948 setup_plugin_environment ()
952 env.my_identity = &my_identity;
954 env.receive = &receive;
955 env.notify_address = ¬ify_address;
956 env.max_connections = max_connect_per_transport;
957 env.session_end = &plugin_env_session_end;
962 * Task shutting down testcase if it a timeout occurs
965 task_timeout (void *cls,
966 const struct GNUNET_SCHEDULER_TaskContext *tc)
968 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
969 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
980 pretty_printer_cb (void *cls,
985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
986 "Plugin returned pretty address: `%s'\n",
988 fail_pretty_printer_count++;
992 * Runs every single test to test the plugin
995 run_connection_tests( int phase , void * cls)
997 struct GNUNET_MessageHeader * msg;
1002 char * host_str = NULL;
1003 /* resetting buffers */
1004 buffer_in.size = HTTP_BUFFER_SIZE;
1008 buffer_out.size = HTTP_BUFFER_SIZE;
1012 if (test_no_ident.test_executed == GNUNET_NO)
1014 /* Connecting to peer without identification */
1015 const char * ident = "";
1016 GNUNET_asprintf (&host_str,
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 _("Connecting to peer without any peer identification.\n"));
1022 test_no_ident.test_executed = GNUNET_YES;
1023 send_data ( &test_no_ident, host_str);
1024 GNUNET_free (host_str);
1027 if (test_too_short_ident.test_executed == GNUNET_NO)
1029 const char * ident = "AAAAAAAAAA";
1030 /* Connecting to peer with too short identification */
1031 GNUNET_asprintf (&host_str,
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1037 _("Connecting to peer with too short peer identification.\n"));
1038 test_too_short_ident.test_executed = GNUNET_YES;
1039 send_data ( &test_too_short_ident, host_str);
1040 GNUNET_free (host_str);
1044 if (test_too_long_ident.test_executed == GNUNET_NO)
1046 const char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1048 /* Connecting to peer with too long identification */
1049 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 _("Connecting to peer with too long peer identification.\n"));
1052 test_too_long_ident.test_executed = GNUNET_YES;
1053 send_data ( &test_too_long_ident, host_str);
1054 GNUNET_free (host_str);
1057 if (test_valid_ident.test_executed == GNUNET_NO)
1059 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
1060 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
1061 GNUNET_asprintf (&host_str,
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 _("Connecting to peer with valid peer identification.\n"));
1069 test_valid_ident.test_executed = GNUNET_YES;
1070 send_data ( &test_valid_ident, host_str);
1071 GNUNET_free (host_str);
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "\nPhase 1: transmit data to all suggested addresses\n\n");
1079 /* Using one of the addresses the plugin proposed */
1080 GNUNET_assert (addr_head->addr != NULL);
1082 struct Plugin_Address * tmp_addr;
1083 struct GNUNET_MessageHeader msg;
1084 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
1085 char address[INET6_ADDRSTRLEN];
1087 unsigned int type = 10;
1089 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
1090 tmp_addr = addr_head;
1091 /* send a message to all addresses advertised by plugin */
1094 while (tmp_addr != NULL)
1096 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
1098 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
1099 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1101 "Sending message to addres no. %u: `%s':%u\n",
1106 if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
1108 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
1109 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "Sending message to addres no. %u: `%s':%u\n",
1116 msg.type=htons(type);
1117 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1120 tmp, sizeof(struct GNUNET_MessageHeader),
1123 tmp_addr->addr, tmp_addr->addrlen,
1125 &task_send_cont, &fail_msgs_transmited_to_local_addrs);
1126 tmp_addr = tmp_addr->next;
1136 struct Session * session = cls;
1137 msg = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Phase 2: session selection\n\n");
1141 size = sizeof(struct GNUNET_MessageHeader);
1142 msg->size=htons(size);
1143 msg->type = htons(20);
1146 (const char *) msg, size,
1147 0, TIMEOUT, NULL, NULL, 0, GNUNET_NO,
1148 &task_send_cont, NULL);
1150 msg->type = htons(21);
1153 (const char *) msg, size,
1154 0, TIMEOUT, NULL, NULL, 0, GNUNET_SYSERR,
1155 &task_send_cont, NULL);
1157 /* answer on session*/
1158 size = sizeof( struct GNUNET_MessageHeader);
1159 msg->size = htons(size);
1160 msg->type = htons(22);
1163 (const char *) msg, size,
1164 0, TIMEOUT, session, NULL, 0, GNUNET_SYSERR,
1165 &task_send_cont, NULL);
1168 /* answer on session with big message not fitting in mhd send buffer*/
1169 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1170 msg = GNUNET_malloc (size);
1171 msg->size=htons(size);
1172 msg->type = htons(23);
1175 (const char *) msg, size,
1176 0, TIMEOUT, session, NULL, 0, GNUNET_NO,
1177 &task_send_cont, NULL);
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Phase 3: send multiple or big messages after disconnect\n\n");
1187 /* disconnect from peer, so new connections are created */
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Disconnect from peer: `%s'\n", GNUNET_i2s(&my_identity));
1189 api->disconnect(api->cls, &my_identity);
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 3: sending messages\n");
1192 /* send a multiple GNUNET_messages at a time*/
1193 size = 2 * sizeof(struct GNUNET_MessageHeader);
1194 msg = GNUNET_malloc( 2* size);
1195 msg->size = htons(size);
1196 msg->type = htons(30);
1197 struct GNUNET_MessageHeader * msg2 = &msg[2];
1198 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1199 msg2->type = htons(31);
1202 (const char *) msg, 4 * sizeof(struct GNUNET_MessageHeader),
1204 addr_head->addr, addr_head->addrlen,
1206 &task_send_cont, &fail_multiple_msgs_in_transmission);
1208 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1210 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1211 msg = GNUNET_malloc(size);
1212 msg->size = htons(size);
1213 msg->type = htons(32);
1216 (const char *) msg, size,
1218 addr_head->addr, addr_head->addrlen,
1220 &task_send_cont, &fail_msg_transmited_max_size);
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "No more tests to run\n");
1231 * @param cls closure
1232 * @param c configuration to use
1237 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1242 unsigned long long tneigh;
1243 struct Plugin_Address * cur;
1244 const char * addr_str;
1247 unsigned int suggest_res;
1249 fail_pretty_printer = GNUNET_YES;
1250 fail_notify_address = GNUNET_YES;
1251 fail_addr_to_str = GNUNET_YES;
1252 fail_msgs_transmited_to_local_addrs = 0;
1253 fail_msg_transmited_max_size = GNUNET_YES;
1254 fail_multiple_msgs_in_transmission = 0;
1255 fail_session_selection_reliable = GNUNET_YES;
1256 fail_session_selection_reliable = GNUNET_YES;
1257 fail_session_selection_session = GNUNET_YES;
1258 fail_session_selection_session_big = GNUNET_YES;
1262 /* parse configuration */
1263 if (GNUNET_CONFIGURATION_have_value (c,"PATHS", "SERVICEHOME"))
1264 GNUNET_CONFIGURATION_get_value_string (c, "PATHS", "SERVICEHOME", &servicehome);
1267 GNUNET_CONFIGURATION_get_value_number (c,
1272 GNUNET_CONFIGURATION_get_value_filename (c,
1274 "HOSTKEY", &keyfile)))
1276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1277 _("Transport service is lacking key configuration settings. Exiting.\n"));
1278 GNUNET_SCHEDULER_shutdown ();
1284 GNUNET_CONFIGURATION_get_value_number (cfg,
1288 (port > 65535) || (port == 0))
1290 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1293 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1297 max_connect_per_transport = (uint32_t) tneigh;
1298 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1299 GNUNET_free (keyfile);
1300 if (my_private_key == NULL)
1302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1303 _("Transport service could not access hostkey. Exiting.\n"));
1304 GNUNET_SCHEDULER_shutdown ();
1309 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1310 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1312 /* assertions before start */
1313 GNUNET_assert ((port > 0) && (port <= 65535));
1315 /* load plugins... */
1316 setup_plugin_environment ();
1317 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1318 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1319 _("Loading HTTP transport plugin `%s'\n"),
1321 api = GNUNET_PLUGIN_load (libname, &env);
1322 GNUNET_free (libname);
1325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1326 _("Failed to load transport plugin for http\n"));
1332 ti_timeout = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &task_timeout, NULL);
1334 /* testing plugin functionality */
1335 GNUNET_assert (0!=fail_notify_address_count);
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 "Transport plugin returned %u addresses to connect to\n",
1338 fail_notify_address_count);
1340 /* testing pretty printer with all addresses obtained from the plugin*/
1344 api->address_pretty_printer (api->cls, "http",
1345 cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT,
1346 &pretty_printer_cb, NULL);
1347 addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1348 suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1350 GNUNET_assert (GNUNET_OK == suggest_res);
1351 GNUNET_assert (NULL != addr_str);
1353 GNUNET_free ( (char *) addr_str);
1356 GNUNET_assert (fail_pretty_printer_count > 0);
1357 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1358 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1359 fail_pretty_printer=GNUNET_NO;
1360 fail_addr_to_str=GNUNET_NO;
1362 struct IPv4HttpAddress failing_addr;
1364 /* Suggesting addresses with wrong size*/
1365 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1366 failing_addr.u_port = htons(0);
1367 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv6HttpAddress));
1368 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1370 /* Suggesting addresses with wrong address*/
1371 failing_addr.ipv4_addr = htonl(0xffc00000);
1372 failing_addr.u_port = htons(12389);
1373 suggest_res = api->check_address (api->cls,&failing_addr,100);
1374 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1376 /* test sending to client */
1377 multi_handle = curl_multi_init();
1379 /* Setting up buffers */
1380 buffer_in.size = HTTP_BUFFER_SIZE;
1384 buffer_out.size = HTTP_BUFFER_SIZE;
1388 /* Setting up connection tests */
1390 /* Test: connecting without a peer identification */
1391 test_no_ident.test_executed = GNUNET_NO;
1392 test_no_ident.test_failed = GNUNET_YES;
1394 /* Test: connecting with too short peer identification */
1395 test_too_short_ident.test_executed = GNUNET_NO;
1396 test_too_short_ident.test_failed = GNUNET_YES;
1398 /* Test: connecting with too long peer identification */
1399 test_too_long_ident.test_executed = GNUNET_NO;
1400 test_too_long_ident.test_failed = GNUNET_YES;
1402 /* Test: connecting with valid identification */
1403 test_valid_ident.test_executed = GNUNET_NO;
1404 test_valid_ident.test_failed = GNUNET_YES;
1406 test_addr = (char *) api->address_to_string (api->cls,addr_head->addr,addr_head->addrlen);
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Phase 0\n\n"));
1409 run_connection_tests(0, NULL);
1410 /* testing finished */
1415 * The main function for the transport service.
1417 * @param argc number of arguments from the command line
1418 * @param argv command line arguments
1419 * @return 0 ok, 1 on error
1422 main (int argc, char *const *argv)
1424 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1425 GNUNET_GETOPT_OPTION_END
1428 char *const argv_prog[] = {
1429 "test_gnunet_transport_plugin_http",
1431 "test_plugin_transport_data_http.conf",
1440 GNUNET_log_setup ("test_gnunet_transport_plugin_http",
1448 struct GNUNET_CONFIGURATION_Handle *cfg;
1449 cfg = GNUNET_CONFIGURATION_create ();
1451 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, "test_plugin_transport_data_http.conf"));
1452 if (GNUNET_CONFIGURATION_have_value (cfg,"PATHS", "SERVICEHOME"))
1453 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", "SERVICEHOME", &servicehome);
1454 GNUNET_DISK_directory_remove (servicehome);
1455 GNUNET_CONFIGURATION_destroy (cfg);
1458 GNUNET_PROGRAM_run (5,
1460 "test_gnunet_transport_plugin_http",
1461 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1463 if (servicehome != NULL)
1465 GNUNET_DISK_directory_remove (servicehome);
1466 GNUNET_free (servicehome);
1468 if (ret != GNUNET_OK)
1473 /* end of test_gnunet_transport_plugin_http.c */