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_NO
48 #define DEBUG_CURL GNUNET_NO
49 #define HTTP_BUFFER_SIZE 2048
51 #define PROTOCOL_PREFIX "http"
53 #define PLUGIN libgnunet_plugin_transport_template
56 * How long until we give up on transmitting the message?
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
63 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
66 * How long between recieve and send?
68 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
73 * Struct for plugin addresses
78 * Next field for linked list
80 struct Plugin_Address * next;
83 * buffer containing data to send
88 * amount of data to sent
94 * Message to send using http
101 unsigned char buf[HTTP_BUFFER_SIZE];
104 * current position in buffer
121 * Struct for plugin addresses
126 * amount of bytes we recieved
131 * buffer for http transfers
133 unsigned char buf[HTTP_BUFFER_SIZE];
136 * buffer size this transfer
141 * amount of bytes we recieved
146 * HTTP Header result for transfer
148 unsigned int http_result_code;
153 unsigned int test_failed;
156 * was this test already executed?
158 unsigned int test_executed;
163 * Network format for IPv4 addresses.
165 struct IPv4HttpAddress
168 * IPv4 address, in network byte order.
170 uint32_t ipv4_addr GNUNET_PACKED;
173 * Port number, in network byte order.
175 uint16_t u_port GNUNET_PACKED;
181 * Network format for IPv6 addresses.
183 struct IPv6HttpAddress
188 struct in6_addr ipv6_addr GNUNET_PACKED;
191 * Port number, in network byte order.
193 uint16_t u6_port GNUNET_PACKED;
200 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
205 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
210 static struct GNUNET_PeerIdentity my_identity;
215 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
220 static long long unsigned int port;
225 static char * test_addr;
230 struct GNUNET_SCHEDULER_Handle *sched;
233 * Our statistics handle.
235 struct GNUNET_STATISTICS_Handle *stats;
241 const struct GNUNET_CONFIGURATION_Handle *cfg;
244 * Number of neighbours we'd like to have.
246 static uint32_t max_connect_per_transport;
249 * Environment for this plugin.
251 static struct GNUNET_TRANSPORT_PluginEnvironment env;
254 *handle for the api provided by this plugin
256 static struct GNUNET_TRANSPORT_PluginFunctions *api;
259 * ID of the task controlling the testcase timeout
261 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
263 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
265 //const struct GNUNET_PeerIdentity * p;
268 * buffer for data to send
270 static struct HTTP_Message buffer_out;
273 * buffer for data to recieve
275 static struct HTTP_Message buffer_in;
278 struct Plugin_Address * addr_head;
281 * Did the test pass or fail?
283 static int fail_notify_address;
285 * Did the test pass or fail?
287 static int fail_notify_address_count;
290 * Did the test pass or fail?
292 static int fail_pretty_printer;
295 * Did the test pass or fail?
297 static int fail_pretty_printer_count;
300 * Did the test pass or fail?
302 static int fail_addr_to_str;
305 * No. of msgs transmitted successfully to local addresses
307 static int fail_msgs_transmited_to_local_addrs;
310 * Test: transmit msg of max. size
312 static int fail_msg_transmited_bigger_max_size;
315 * Test: transmit msg of max. size
317 static int fail_msg_transmited_max_size;
320 * Test: transmit 2 msgs. in in send operation
322 static int fail_multiple_msgs_in_transmission;
325 * Test: connect to peer without peer identification
327 static struct HTTP_Transfer test_no_ident;
330 * Test: connect to peer without peer identification
332 static struct HTTP_Transfer test_too_short_ident;
335 * Test: connect to peer without peer identification
337 static struct HTTP_Transfer test_too_long_ident;
340 * Test: connect to peer with valid peer identification
342 static struct HTTP_Transfer test_valid_ident;
345 * Test: session selection, use any existing
347 static int fail_session_selection_any;
350 * Test: session selection, use existing inbound session
352 static int fail_session_selection_session;
355 * Test: session selection, use existing inbound session
356 * max message, not fitting in send & recv buffers at one time
358 static int fail_session_selection_session_big;
361 * Test: session selection, use reliable existing
363 static int fail_session_selection_reliable;
366 * Did the test pass or fail?
371 * Number of local addresses
373 static unsigned int count_str_addr;
380 static CURLM *multi_handle;
383 * The task sending data
385 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
393 struct Plugin_Address * cur;
394 struct Plugin_Address * tmp;
396 /* Evaluate results */
398 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Phase 0: Test plugin functions failed\n");
403 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))
405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Phase 1: Test connect with wrong data failed\n");
408 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))
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Phase 2: Test session selection failed\n");
413 if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_multiple_msgs_in_transmission != 2) || (fail_msg_transmited_max_size == GNUNET_YES))
415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Phase 3: Test sending with plugin failed\n");
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
423 api->disconnect(api->cls,&my_identity);
425 curl_multi_cleanup(multi_handle);
427 if (NULL != curl_handle)
428 curl_easy_cleanup (curl_handle);
430 /* cleaning addresses */
431 while (addr_head != NULL)
434 tmp = addr_head->next;
435 GNUNET_free (addr_head->addr);
436 GNUNET_free (addr_head);
440 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
442 GNUNET_SCHEDULER_cancel(sched,ti_send);
443 ti_send = GNUNET_SCHEDULER_NO_TASK;
446 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
448 GNUNET_SCHEDULER_cancel(sched,http_task_send);
449 http_task_send = GNUNET_SCHEDULER_NO_TASK;
452 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
454 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
455 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
458 GNUNET_free(test_addr);
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
460 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
462 GNUNET_SCHEDULER_shutdown(sched);
463 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
472 * Continuation called after plugin send message
475 * @result GNUNET_OK or GNUNET_SYSERR
478 static void task_send_cont (void *cls,
479 const struct GNUNET_PeerIdentity * target,
482 struct Plugin_Address * tmp_addr;
483 tmp_addr = addr_head;
485 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
488 fail_msg_transmited_bigger_max_size = GNUNET_NO;
492 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
495 fail_msg_transmited_max_size = GNUNET_NO;
500 static void run_connection_tests( int phase , void * cls);
503 * Recieves messages from plugin, in real world transport
505 static struct GNUNET_TIME_Relative
507 const struct GNUNET_PeerIdentity * peer,
508 const struct GNUNET_MessageHeader * message,
510 struct Session *session,
511 const char *sender_address,
512 uint16_t sender_address_len)
514 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);
516 if ((ntohs(message->type)>=10) && (ntohs(message->type)<20))
518 fail_msgs_transmited_to_local_addrs++;
519 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
520 run_connection_tests(2, session);
524 if ((ntohs(message->type)==20))
526 fail_session_selection_reliable = GNUNET_NO;
529 if ((ntohs(message->type)==21))
531 fail_session_selection_any = GNUNET_NO;
533 if ((ntohs(message->type)==22))
535 fail_session_selection_session = GNUNET_NO;
538 if ((ntohs(message->type)==23))
540 fail_session_selection_session_big = GNUNET_NO;
541 run_connection_tests(3, NULL);
544 if ((ntohs(message->type)==30) || (ntohs(message->type)==31))
546 fail_multiple_msgs_in_transmission ++;
549 if ((ntohs(message->type)==32) && (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1))
551 fail_msg_transmited_max_size = GNUNET_NO;
555 return GNUNET_TIME_UNIT_ZERO;
558 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
562 len = buffer_out.len;
564 if (( buffer_out.pos == len) || (len > (size * nmemb)))
566 memcpy(stream, buffer_out.buf, len);
567 buffer_out.pos = len;
571 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
574 if (buffer_in.pos + size * nmemb > buffer_in.size)
575 return 0; /* overflow */
577 buffer_in.len = size * nmemb;
578 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
579 buffer_in.pos += size * nmemb;
580 buffer_in.len = buffer_in.pos;
581 buffer_in.buf[buffer_in.pos] = '\0';
582 return buffer_in.pos;
585 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
587 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
589 unsigned int len = size * nmemb;
591 tmp = GNUNET_malloc ( len+1 );
593 if (tmp[len-2] == 13)
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
598 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
600 res->http_result_code=100;
602 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
604 res->http_result_code=200;
606 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
608 res->http_result_code=400;
610 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
612 res->http_result_code=404;
614 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
616 res->http_result_code=413;
623 static size_t send_prepare( struct HTTP_Transfer * result);
627 static void send_execute (void *cls,
628 const struct GNUNET_SCHEDULER_TaskContext *tc)
630 struct HTTP_Transfer *res;
636 res = (struct HTTP_Transfer *) cls;
637 http_task_send = GNUNET_SCHEDULER_NO_TASK;
638 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
644 mret = curl_multi_perform (multi_handle, &running);
650 msg = curl_multi_info_read (multi_handle, &running);
653 /* get session for affected curl handle */
654 //cs = find_session_by_curlhandle (msg->easy_handle);
655 //GNUNET_assert ( cs != NULL );
660 if ( (msg->data.result != CURLE_OK) &&
661 (msg->data.result != CURLE_GOT_NOTHING) )
664 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
665 _("curl failed for `%s' at %s:%d: `%s'\n"),
666 "curl_multi_perform",
669 curl_easy_strerror (msg->data.result));
670 /* sending msg failed*/
671 curl_easy_cleanup(curl_handle);
674 run_connection_tests(0, NULL);
676 if (res == &test_no_ident)
678 if ((res->http_result_code==404) && (buffer_in.len==208))
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to peer without any peer identification: test passed\n");
681 res->test_failed = GNUNET_NO;
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer without any peer identification: test failed\n"));
686 if (res == &test_too_short_ident)
688 if ((res->http_result_code==404) && (buffer_in.len==208))
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to peer with too short peer identification: test passed\n");
691 res->test_failed = GNUNET_NO;
694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with too short peer identification: test failed\n"));
696 if (res == &test_too_long_ident)
698 if ((res->http_result_code==404) && (buffer_in.len==208))
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to peer with too long peer identification: test passed\n");
701 res->test_failed = GNUNET_NO;
704 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with too long peer identification: test failed\n"));
706 if (res == &test_valid_ident)
708 if ((res->http_result_code==200))
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to peer with valid peer identification: test passed\n");
711 res->test_failed = GNUNET_NO;
714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connecting to peer with valid peer identification: test failed\n");
716 curl_easy_cleanup(curl_handle);
718 if ((res == &test_valid_ident) && (res->test_failed == GNUNET_NO))
719 run_connection_tests(1, NULL);
720 run_connection_tests(0, NULL);
727 while ( (running > 0) );
730 while (mret == CURLM_CALL_MULTI_PERFORM);
735 * Function setting up file descriptors and scheduling task to run
736 * @param ses session to send data to
737 * @return bytes sent to peer
739 static size_t send_prepare( struct HTTP_Transfer * result)
745 struct GNUNET_NETWORK_FDSet *grs;
746 struct GNUNET_NETWORK_FDSet *gws;
754 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
755 if (mret != CURLM_OK)
757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
758 _("%s failed at %s:%d: `%s'\n"),
759 "curl_multi_fdset", __FILE__, __LINE__,
760 curl_multi_strerror (mret));
763 mret = curl_multi_timeout (multi_handle, &to);
764 if (mret != CURLM_OK)
766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
767 _("%s failed at %s:%d: `%s'\n"),
768 "curl_multi_timeout", __FILE__, __LINE__,
769 curl_multi_strerror (mret));
773 grs = GNUNET_NETWORK_fdset_create ();
774 gws = GNUNET_NETWORK_fdset_create ();
775 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
776 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
777 http_task_send = GNUNET_SCHEDULER_add_select (sched,
778 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
779 GNUNET_SCHEDULER_NO_TASK,
780 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
785 GNUNET_NETWORK_fdset_destroy (gws);
786 GNUNET_NETWORK_fdset_destroy (grs);
788 /* FIXME: return bytes REALLY sent */
793 * function to send data to server
795 static int send_data( struct HTTP_Transfer * result, char * url)
798 curl_handle = curl_easy_init();
799 if( NULL == curl_handle)
801 printf("easy_init failed \n");
802 return GNUNET_SYSERR;
805 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
807 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
808 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
809 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
810 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
811 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
812 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
813 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
814 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
815 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
816 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
817 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
819 curl_multi_add_handle(multi_handle, curl_handle);
821 send_prepare(result);
827 * Plugin notifies transport (aka testcase) about its addresses
830 notify_address (void *cls,
834 struct GNUNET_TIME_Relative expires)
836 char address[INET6_ADDRSTRLEN];
838 struct Plugin_Address * pl_addr;
839 struct Plugin_Address * cur;
841 if (addrlen == (sizeof (struct IPv4HttpAddress)))
843 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
844 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
846 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
848 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
849 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 _("Unknown address size: ipv6 has %u ipv4 has %u but this has %u\n"),
855 sizeof (struct IPv6HttpAddress),
856 sizeof (struct IPv4HttpAddress),
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 _("Transport plugin notification for address: `%s':%u\n"),
864 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
865 pl_addr->addrlen = addrlen;
866 pl_addr->addr = GNUNET_malloc(addrlen);
867 memcpy(pl_addr->addr,addr,addrlen);
868 pl_addr->next = NULL;
870 if ( NULL == addr_head)
877 while (NULL != cur->next)
883 fail_notify_address_count++;
884 fail_notify_address = GNUNET_NO;
888 plugin_env_session_end (void *cls,
889 const struct GNUNET_PeerIdentity *peer,
890 struct Session *session)
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Pluging tells me: session %X to peer `%s' ended\n", session, GNUNET_i2s(peer));
897 * Setup plugin environment
900 setup_plugin_environment ()
905 env.my_identity = &my_identity;
907 env.receive = &receive;
908 env.notify_address = ¬ify_address;
909 env.max_connections = max_connect_per_transport;
910 env.session_end = &plugin_env_session_end;
915 * Task shutting down testcase if it a timeout occurs
918 task_timeout (void *cls,
919 const struct GNUNET_SCHEDULER_TaskContext *tc)
921 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
922 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
931 static void pretty_printer_cb (void *cls,
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
937 fail_pretty_printer_count++;
941 * Runs every single test to test the plugin
943 static void run_connection_tests( int phase , void * cls)
945 struct GNUNET_MessageHeader * msg;
950 char * host_str = NULL;
951 /* resetting buffers */
952 buffer_in.size = HTTP_BUFFER_SIZE;
956 buffer_out.size = HTTP_BUFFER_SIZE;
960 if (test_no_ident.test_executed == GNUNET_NO)
962 /* Connecting to peer without identification */
964 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Connecting to peer without any peer identification.\n"));
966 test_no_ident.test_executed = GNUNET_YES;
967 send_data ( &test_no_ident, host_str);
968 GNUNET_free (host_str);
971 if (test_too_short_ident.test_executed == GNUNET_NO)
973 char * ident = "AAAAAAAAAA";
974 /* Connecting to peer with too short identification */
975 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX,test_addr,ident);
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Connecting to peer with too short peer identification.\n"));
977 test_too_short_ident.test_executed = GNUNET_YES;
978 send_data ( &test_too_short_ident, host_str);
979 GNUNET_free (host_str);
983 if (test_too_long_ident.test_executed == GNUNET_NO)
985 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
987 /* Connecting to peer with too long identification */
988 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Connecting to peer with too long peer identification.\n"));
990 test_too_long_ident.test_executed = GNUNET_YES;
991 send_data ( &test_too_long_ident, host_str);
992 GNUNET_free (host_str);
995 if (test_valid_ident.test_executed == GNUNET_NO)
997 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
998 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
999 GNUNET_asprintf (&host_str, "%s://%s/%s%s",PROTOCOL_PREFIX, test_addr,(char *) &ident,";0");
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Connecting to peer with valid peer identification.\n"));
1001 test_valid_ident.test_executed = GNUNET_YES;
1002 send_data ( &test_valid_ident, host_str);
1003 GNUNET_free (host_str);
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nPhase 1: transmit data to all suggested addresses\n\n");
1010 /* Using one of the addresses the plugin proposed */
1011 GNUNET_assert (addr_head->addr != NULL);
1013 struct Plugin_Address * tmp_addr;
1014 struct GNUNET_MessageHeader msg;
1015 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
1016 char address[INET6_ADDRSTRLEN];
1018 unsigned int type = 10;
1020 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
1021 tmp_addr = addr_head;
1022 /* send a message to all addresses advertised by plugin */
1025 while (tmp_addr != NULL)
1027 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
1029 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
1030 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
1033 if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
1035 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
1036 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
1039 msg.type=htons(type);
1040 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1041 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);
1042 tmp_addr = tmp_addr->next;
1053 struct Session * session = cls;
1054 msg = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 2: session selection\n\n");
1057 size = sizeof(struct GNUNET_MessageHeader);
1058 msg->size=htons(size);
1059 msg->type = htons(20);
1060 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1062 msg->type = htons(21);
1063 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1065 /* answer on session*/
1066 size = sizeof( struct GNUNET_MessageHeader);
1067 msg->size = htons(size);
1068 msg->type = htons(22);
1069 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1073 /* answer on session with big message not fitting in mhd send buffer*/
1074 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1075 msg = GNUNET_malloc (size);
1076 msg->size=htons(size);
1077 msg->type = htons(23);
1078 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 3: send multiple or big messages after disconnect\n\n");
1087 /* disconnect from peer, so new connections are created */
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Disconnect from peer: `%s'\n", GNUNET_i2s(&my_identity));
1089 api->disconnect(api->cls, &my_identity);
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 3: sending messages\n");
1092 /* send a multiple GNUNET_messages at a time*/
1093 size = 2 * sizeof(struct GNUNET_MessageHeader);
1094 msg = GNUNET_malloc( 2* size);
1095 msg->size = htons(size);
1096 msg->type = htons(30);
1097 struct GNUNET_MessageHeader * msg2 = &msg[2];
1098 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1099 msg2->type = htons(31);
1100 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);
1102 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1104 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1105 msg = GNUNET_malloc(size);
1106 msg->size = htons(size);
1107 msg->type = htons(32);
1108 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);
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1118 * @param cls closure
1119 * @param s scheduler to use
1120 * @param c configuration to use
1124 struct GNUNET_SCHEDULER_Handle *s,
1126 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1132 unsigned long long tneigh;
1133 struct Plugin_Address * cur;
1134 const char * addr_str;
1137 unsigned int suggest_res;
1139 fail_pretty_printer = GNUNET_YES;
1140 fail_notify_address = GNUNET_YES;
1141 fail_addr_to_str = GNUNET_YES;
1142 fail_msgs_transmited_to_local_addrs = 0;
1143 fail_msg_transmited_max_size = GNUNET_YES;
1144 fail_multiple_msgs_in_transmission = 0;
1145 fail_session_selection_reliable = GNUNET_YES;
1146 fail_session_selection_reliable = GNUNET_YES;
1147 fail_session_selection_session = GNUNET_YES;
1148 fail_session_selection_session_big = GNUNET_YES;
1152 /* parse configuration */
1154 GNUNET_CONFIGURATION_get_value_number (c,
1159 GNUNET_CONFIGURATION_get_value_filename (c,
1161 "HOSTKEY", &keyfile)))
1163 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_asprintf (&libname, "libgnunet_plugin_transport_http");
1207 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),libname);
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"));
1219 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1221 /* testing plugin functionality */
1222 GNUNET_assert (0!=fail_notify_address_count);
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport plugin returned %u addresses to connect to\n", fail_notify_address_count);
1225 /* testing pretty printer with all addresses obtained from the plugin*/
1230 api->address_pretty_printer (api->cls, "http",cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT, &pretty_printer_cb,NULL);
1231 addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1232 suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1234 GNUNET_assert (GNUNET_OK == suggest_res);
1235 GNUNET_assert (NULL != addr_str);
1237 GNUNET_free ( (char *) addr_str);
1240 GNUNET_assert (fail_pretty_printer_count > 0);
1241 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1242 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1243 fail_pretty_printer=GNUNET_NO;
1244 fail_addr_to_str=GNUNET_NO;
1246 /* Suggesting addresses with wrong port*/
1247 struct IPv4HttpAddress failing_addr;
1248 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1249 failing_addr.u_port = htons(0);
1250 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv4HttpAddress));
1251 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1253 /* Suggesting addresses with wrong size*/
1254 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1255 failing_addr.u_port = htons(0);
1256 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv6HttpAddress));
1257 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1259 /* Suggesting addresses with wrong address*/
1260 failing_addr.ipv4_addr = htonl(0xffc00000);
1261 failing_addr.u_port = htons(12389);
1262 suggest_res = api->check_address (api->cls,&failing_addr,100);
1263 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1265 /* test sending to client */
1266 multi_handle = curl_multi_init();
1268 /* Setting up buffers */
1269 buffer_in.size = HTTP_BUFFER_SIZE;
1273 buffer_out.size = HTTP_BUFFER_SIZE;
1277 /* Setting up connection tests */
1279 /* Test: connecting without a peer identification */
1280 test_no_ident.test_executed = GNUNET_NO;
1281 test_no_ident.test_failed = GNUNET_YES;
1283 /* Test: connecting with too short peer identification */
1284 test_too_short_ident.test_executed = GNUNET_NO;
1285 test_too_short_ident.test_failed = GNUNET_YES;
1287 /* Test: connecting with too long peer identification */
1288 test_too_long_ident.test_executed = GNUNET_NO;
1289 test_too_long_ident.test_failed = GNUNET_YES;
1291 /* Test: connecting with valid identification */
1292 test_valid_ident.test_executed = GNUNET_NO;
1293 test_valid_ident.test_failed = GNUNET_YES;
1295 test_addr = (char *) api->address_to_string (api->cls,addr_head->addr,addr_head->addrlen);
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Phase 0\n\n"));
1298 run_connection_tests(0, NULL);
1300 /* testing finished */
1307 * The main function for the transport service.
1309 * @param argc number of arguments from the command line
1310 * @param argv command line arguments
1311 * @return 0 ok, 1 on error
1314 main (int argc, char *const *argv)
1317 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1318 GNUNET_GETOPT_OPTION_END
1321 char *const argv_prog[] = {
1322 "test_plugin_transport_http",
1324 "test_plugin_transport_data_http.conf",
1333 GNUNET_log_setup ("test_plugin_transport_http",
1342 GNUNET_PROGRAM_run (5,
1344 "test_plugin_transport_http",
1345 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1347 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1352 /* end of test_plugin_transport_http.c */