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>
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;
237 struct GNUNET_SCHEDULER_Handle *sched;
240 * Our statistics handle.
242 struct GNUNET_STATISTICS_Handle *stats;
248 const struct GNUNET_CONFIGURATION_Handle *cfg;
251 * Number of neighbours we'd like to have.
253 static uint32_t max_connect_per_transport;
256 * Environment for this plugin.
258 static struct GNUNET_TRANSPORT_PluginEnvironment env;
261 *handle for the api provided by this plugin
263 static struct GNUNET_TRANSPORT_PluginFunctions *api;
266 * ID of the task controlling the testcase timeout
268 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
270 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
272 //const struct GNUNET_PeerIdentity * p;
275 * buffer for data to send
277 static struct HTTP_Message buffer_out;
280 * buffer for data to recieve
282 static struct HTTP_Message buffer_in;
285 struct Plugin_Address * addr_head;
288 * Did the test pass or fail?
290 static int fail_notify_address;
292 * Did the test pass or fail?
294 static int fail_notify_address_count;
297 * Did the test pass or fail?
299 static int fail_pretty_printer;
302 * Did the test pass or fail?
304 static int fail_pretty_printer_count;
307 * Did the test pass or fail?
309 static int fail_addr_to_str;
312 * No. of msgs transmitted successfully to local addresses
314 static int fail_msgs_transmited_to_local_addrs;
317 * Test: transmit msg of max. size
319 static int fail_msg_transmited_bigger_max_size;
322 * Test: transmit msg of max. size
324 static int fail_msg_transmited_max_size;
327 * Test: transmit 2 msgs. in in send operation
329 static int fail_multiple_msgs_in_transmission;
332 * Test: connect to peer without peer identification
334 static struct HTTP_Transfer test_no_ident;
337 * Test: connect to peer without peer identification
339 static struct HTTP_Transfer test_too_short_ident;
342 * Test: connect to peer without peer identification
344 static struct HTTP_Transfer test_too_long_ident;
347 * Test: connect to peer with valid peer identification
349 static struct HTTP_Transfer test_valid_ident;
352 * Test: session selection, use any existing
354 static int fail_session_selection_any;
357 * Test: session selection, use existing inbound session
359 static int fail_session_selection_session;
362 * Test: session selection, use existing inbound session
363 * max message, not fitting in send & recv buffers at one time
365 static int fail_session_selection_session_big;
368 * Test: session selection, use reliable existing
370 static int fail_session_selection_reliable;
373 * Did the test pass or fail?
378 * Number of local addresses
380 static unsigned int count_str_addr;
387 static CURLM *multi_handle;
390 * The task sending data
392 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
400 struct Plugin_Address * cur;
401 struct Plugin_Address * tmp;
403 /* Evaluate results */
405 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Phase 0: Test plugin functions failed\n");
410 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))
412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Phase 1: Test connect with wrong data failed\n");
415 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))
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Phase 2: Test session selection failed\n");
420 if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_multiple_msgs_in_transmission != 2) || (fail_msg_transmited_max_size == GNUNET_YES))
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Phase 3: Test sending with plugin failed\n");
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
430 api->disconnect(api->cls,&my_identity);
432 curl_multi_cleanup(multi_handle);
434 if (NULL != curl_handle)
435 curl_easy_cleanup (curl_handle);
437 /* cleaning addresses */
438 while (addr_head != NULL)
441 tmp = addr_head->next;
442 GNUNET_free (addr_head->addr);
443 GNUNET_free (addr_head);
447 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
449 GNUNET_SCHEDULER_cancel(sched,ti_send);
450 ti_send = GNUNET_SCHEDULER_NO_TASK;
453 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
455 GNUNET_SCHEDULER_cancel(sched,http_task_send);
456 http_task_send = GNUNET_SCHEDULER_NO_TASK;
459 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
461 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
462 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
465 GNUNET_free(test_addr);
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
467 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
469 GNUNET_SCHEDULER_shutdown(sched);
470 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
479 * Continuation called after plugin send message
482 * @result GNUNET_OK or GNUNET_SYSERR
485 static void task_send_cont (void *cls,
486 const struct GNUNET_PeerIdentity * target,
489 struct Plugin_Address * tmp_addr;
490 tmp_addr = addr_head;
492 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
495 fail_msg_transmited_bigger_max_size = GNUNET_NO;
499 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
502 fail_msg_transmited_max_size = GNUNET_NO;
507 static void run_connection_tests( int phase , void * cls);
510 * Recieves messages from plugin, in real world transport
512 static struct GNUNET_TIME_Relative
514 const struct GNUNET_PeerIdentity * peer,
515 const struct GNUNET_MessageHeader * message,
517 struct Session *session,
518 const char *sender_address,
519 uint16_t sender_address_len)
521 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);
523 if ((ntohs(message->type)>=10) && (ntohs(message->type)<20))
525 fail_msgs_transmited_to_local_addrs++;
526 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
527 run_connection_tests(2, session);
531 if ((ntohs(message->type)==20))
533 fail_session_selection_reliable = GNUNET_NO;
536 if ((ntohs(message->type)==21))
538 fail_session_selection_any = GNUNET_NO;
540 if ((ntohs(message->type)==22))
542 fail_session_selection_session = GNUNET_NO;
545 if ((ntohs(message->type)==23))
547 fail_session_selection_session_big = GNUNET_NO;
548 run_connection_tests(3, NULL);
551 if ((ntohs(message->type)==30) || (ntohs(message->type)==31))
553 fail_multiple_msgs_in_transmission ++;
556 if ((ntohs(message->type)==32) && (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1))
558 fail_msg_transmited_max_size = GNUNET_NO;
562 return GNUNET_TIME_UNIT_ZERO;
565 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
569 len = buffer_out.len;
571 if (( buffer_out.pos == len) || (len > (size * nmemb)))
573 memcpy(stream, buffer_out.buf, len);
574 buffer_out.pos = len;
578 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
581 if (buffer_in.pos + size * nmemb > buffer_in.size)
582 return 0; /* overflow */
584 buffer_in.len = size * nmemb;
585 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
586 buffer_in.pos += size * nmemb;
587 buffer_in.len = buffer_in.pos;
588 buffer_in.buf[buffer_in.pos] = '\0';
589 return buffer_in.pos;
592 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
594 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
596 unsigned int len = size * nmemb;
598 tmp = GNUNET_malloc ( len+1 );
600 if (tmp[len-2] == 13)
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
605 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
607 res->http_result_code=100;
609 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
611 res->http_result_code=200;
613 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
615 res->http_result_code=400;
617 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
619 res->http_result_code=404;
621 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
623 res->http_result_code=413;
630 static size_t send_prepare( struct HTTP_Transfer * result);
634 static void send_execute (void *cls,
635 const struct GNUNET_SCHEDULER_TaskContext *tc)
637 struct HTTP_Transfer *res;
643 res = (struct HTTP_Transfer *) cls;
644 http_task_send = GNUNET_SCHEDULER_NO_TASK;
645 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
651 mret = curl_multi_perform (multi_handle, &running);
657 msg = curl_multi_info_read (multi_handle, &running);
660 /* get session for affected curl handle */
661 //cs = find_session_by_curlhandle (msg->easy_handle);
662 //GNUNET_assert ( cs != NULL );
667 if ( (msg->data.result != CURLE_OK) &&
668 (msg->data.result != CURLE_GOT_NOTHING) )
671 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
672 _("curl failed for `%s' at %s:%d: `%s'\n"),
673 "curl_multi_perform",
676 curl_easy_strerror (msg->data.result));
677 /* sending msg failed*/
678 curl_easy_cleanup(curl_handle);
681 run_connection_tests(0, NULL);
683 if (res == &test_no_ident)
685 if ((res->http_result_code==404) && (buffer_in.len==208))
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to peer without any peer identification: test passed\n");
688 res->test_failed = GNUNET_NO;
691 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer without any peer identification: test failed\n"));
693 if (res == &test_too_short_ident)
695 if ((res->http_result_code==404) && (buffer_in.len==208))
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to peer with too short peer identification: test passed\n");
698 res->test_failed = GNUNET_NO;
701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with too short peer identification: test failed\n"));
703 if (res == &test_too_long_ident)
705 if ((res->http_result_code==404) && (buffer_in.len==208))
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to peer with too long peer identification: test passed\n");
708 res->test_failed = GNUNET_NO;
711 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with too long peer identification: test failed\n"));
713 if (res == &test_valid_ident)
715 if ((res->http_result_code==200))
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to peer with valid peer identification: test passed\n");
718 res->test_failed = GNUNET_NO;
721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connecting to peer with valid peer identification: test failed\n");
723 curl_easy_cleanup(curl_handle);
725 if ((res == &test_valid_ident) && (res->test_failed == GNUNET_NO))
726 run_connection_tests(1, NULL);
727 run_connection_tests(0, NULL);
734 while ( (running > 0) );
737 while (mret == CURLM_CALL_MULTI_PERFORM);
742 * Function setting up file descriptors and scheduling task to run
743 * @param ses session to send data to
744 * @return bytes sent to peer
746 static size_t send_prepare( struct HTTP_Transfer * result)
752 struct GNUNET_NETWORK_FDSet *grs;
753 struct GNUNET_NETWORK_FDSet *gws;
761 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
762 if (mret != CURLM_OK)
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
765 _("%s failed at %s:%d: `%s'\n"),
766 "curl_multi_fdset", __FILE__, __LINE__,
767 curl_multi_strerror (mret));
770 mret = curl_multi_timeout (multi_handle, &to);
771 if (mret != CURLM_OK)
773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
774 _("%s failed at %s:%d: `%s'\n"),
775 "curl_multi_timeout", __FILE__, __LINE__,
776 curl_multi_strerror (mret));
780 grs = GNUNET_NETWORK_fdset_create ();
781 gws = GNUNET_NETWORK_fdset_create ();
782 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
783 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
784 http_task_send = GNUNET_SCHEDULER_add_select (sched,
785 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
786 GNUNET_SCHEDULER_NO_TASK,
787 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
792 GNUNET_NETWORK_fdset_destroy (gws);
793 GNUNET_NETWORK_fdset_destroy (grs);
795 /* FIXME: return bytes REALLY sent */
800 * function to send data to server
802 static int send_data( struct HTTP_Transfer * result, char * url)
805 curl_handle = curl_easy_init();
806 if( NULL == curl_handle)
808 printf("easy_init failed \n");
809 return GNUNET_SYSERR;
812 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
814 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
815 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
816 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
817 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
818 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
819 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
820 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
821 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
822 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
823 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
824 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
826 curl_multi_add_handle(multi_handle, curl_handle);
828 send_prepare(result);
834 * Plugin notifies transport (aka testcase) about its addresses
837 notify_address (void *cls,
841 struct GNUNET_TIME_Relative expires)
843 char address[INET6_ADDRSTRLEN];
845 struct Plugin_Address * pl_addr;
846 struct Plugin_Address * cur;
848 if (addrlen == (sizeof (struct IPv4HttpAddress)))
850 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
851 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
853 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
855 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
856 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 _("Unknown address size: ipv6 has %u ipv4 has %u but this has %u\n"),
862 sizeof (struct IPv6HttpAddress),
863 sizeof (struct IPv4HttpAddress),
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 _("Transport plugin notification for address: `%s':%u\n"),
871 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
872 pl_addr->addrlen = addrlen;
873 pl_addr->addr = GNUNET_malloc(addrlen);
874 memcpy(pl_addr->addr,addr,addrlen);
875 pl_addr->next = NULL;
877 if ( NULL == addr_head)
884 while (NULL != cur->next)
890 fail_notify_address_count++;
891 fail_notify_address = GNUNET_NO;
895 plugin_env_session_end (void *cls,
896 const struct GNUNET_PeerIdentity *peer,
897 struct Session *session)
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Pluging tells me: session %X to peer `%s' ended\n", session, GNUNET_i2s(peer));
904 * Setup plugin environment
907 setup_plugin_environment ()
912 env.my_identity = &my_identity;
914 env.receive = &receive;
915 env.notify_address = ¬ify_address;
916 env.max_connections = max_connect_per_transport;
917 env.session_end = &plugin_env_session_end;
922 * Task shutting down testcase if it a timeout occurs
925 task_timeout (void *cls,
926 const struct GNUNET_SCHEDULER_TaskContext *tc)
928 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
929 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
938 static void pretty_printer_cb (void *cls,
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
944 fail_pretty_printer_count++;
948 * Runs every single test to test the plugin
950 static void run_connection_tests( int phase , void * cls)
952 struct GNUNET_MessageHeader * msg;
957 char * host_str = NULL;
958 /* resetting buffers */
959 buffer_in.size = HTTP_BUFFER_SIZE;
963 buffer_out.size = HTTP_BUFFER_SIZE;
967 if (test_no_ident.test_executed == GNUNET_NO)
969 /* Connecting to peer without identification */
971 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Connecting to peer without any peer identification.\n"));
973 test_no_ident.test_executed = GNUNET_YES;
974 send_data ( &test_no_ident, host_str);
975 GNUNET_free (host_str);
978 if (test_too_short_ident.test_executed == GNUNET_NO)
980 char * ident = "AAAAAAAAAA";
981 /* Connecting to peer with too short identification */
982 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX,test_addr,ident);
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Connecting to peer with too short peer identification.\n"));
984 test_too_short_ident.test_executed = GNUNET_YES;
985 send_data ( &test_too_short_ident, host_str);
986 GNUNET_free (host_str);
990 if (test_too_long_ident.test_executed == GNUNET_NO)
992 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
994 /* Connecting to peer with too long identification */
995 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Connecting to peer with too long peer identification.\n"));
997 test_too_long_ident.test_executed = GNUNET_YES;
998 send_data ( &test_too_long_ident, host_str);
999 GNUNET_free (host_str);
1002 if (test_valid_ident.test_executed == GNUNET_NO)
1004 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
1005 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
1006 GNUNET_asprintf (&host_str, "%s://%s/%s%s",PROTOCOL_PREFIX, test_addr,(char *) &ident,";0");
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Connecting to peer with valid peer identification.\n"));
1008 test_valid_ident.test_executed = GNUNET_YES;
1009 send_data ( &test_valid_ident, host_str);
1010 GNUNET_free (host_str);
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nPhase 1: transmit data to all suggested addresses\n\n");
1017 /* Using one of the addresses the plugin proposed */
1018 GNUNET_assert (addr_head->addr != NULL);
1020 struct Plugin_Address * tmp_addr;
1021 struct GNUNET_MessageHeader msg;
1022 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
1023 char address[INET6_ADDRSTRLEN];
1025 unsigned int type = 10;
1027 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
1028 tmp_addr = addr_head;
1029 /* send a message to all addresses advertised by plugin */
1032 while (tmp_addr != NULL)
1034 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
1036 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
1037 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
1040 if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
1042 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
1043 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
1046 msg.type=htons(type);
1047 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1048 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);
1049 tmp_addr = tmp_addr->next;
1060 struct Session * session = cls;
1061 msg = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 2: session selection\n\n");
1064 size = sizeof(struct GNUNET_MessageHeader);
1065 msg->size=htons(size);
1066 msg->type = htons(20);
1067 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1069 msg->type = htons(21);
1070 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1072 /* answer on session*/
1073 size = sizeof( struct GNUNET_MessageHeader);
1074 msg->size = htons(size);
1075 msg->type = htons(22);
1076 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1080 /* answer on session with big message not fitting in mhd send buffer*/
1081 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1082 msg = GNUNET_malloc (size);
1083 msg->size=htons(size);
1084 msg->type = htons(23);
1085 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 3: send multiple or big messages after disconnect\n\n");
1094 /* disconnect from peer, so new connections are created */
1095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Disconnect from peer: `%s'\n", GNUNET_i2s(&my_identity));
1096 api->disconnect(api->cls, &my_identity);
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 3: sending messages\n");
1099 /* send a multiple GNUNET_messages at a time*/
1100 size = 2 * sizeof(struct GNUNET_MessageHeader);
1101 msg = GNUNET_malloc( 2* size);
1102 msg->size = htons(size);
1103 msg->type = htons(30);
1104 struct GNUNET_MessageHeader * msg2 = &msg[2];
1105 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1106 msg2->type = htons(31);
1107 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);
1109 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1111 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1112 msg = GNUNET_malloc(size);
1113 msg->size = htons(size);
1114 msg->type = htons(32);
1115 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);
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1125 * @param cls closure
1126 * @param s scheduler to use
1127 * @param c configuration to use
1131 struct GNUNET_SCHEDULER_Handle *s,
1133 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1139 unsigned long long tneigh;
1140 struct Plugin_Address * cur;
1141 const char * addr_str;
1144 unsigned int suggest_res;
1146 fail_pretty_printer = GNUNET_YES;
1147 fail_notify_address = GNUNET_YES;
1148 fail_addr_to_str = GNUNET_YES;
1149 fail_msgs_transmited_to_local_addrs = 0;
1150 fail_msg_transmited_max_size = GNUNET_YES;
1151 fail_multiple_msgs_in_transmission = 0;
1152 fail_session_selection_reliable = GNUNET_YES;
1153 fail_session_selection_reliable = GNUNET_YES;
1154 fail_session_selection_session = GNUNET_YES;
1155 fail_session_selection_session_big = GNUNET_YES;
1159 /* parse configuration */
1161 GNUNET_CONFIGURATION_get_value_number (c,
1166 GNUNET_CONFIGURATION_get_value_filename (c,
1168 "HOSTKEY", &keyfile)))
1170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1171 _("Transport service is lacking key configuration settings. Exiting.\n"));
1172 GNUNET_SCHEDULER_shutdown (s);
1178 GNUNET_CONFIGURATION_get_value_number (cfg,
1182 (port > 65535) || (port == 0))
1184 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1187 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1191 max_connect_per_transport = (uint32_t) tneigh;
1192 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1193 GNUNET_free (keyfile);
1194 if (my_private_key == NULL)
1196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1197 _("Transport service could not access hostkey. Exiting.\n"));
1198 GNUNET_SCHEDULER_shutdown (s);
1203 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1204 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1206 /* assertions before start */
1207 GNUNET_assert ((port > 0) && (port <= 65535));
1208 GNUNET_assert(&my_public_key != NULL);
1209 GNUNET_assert(&my_identity.hashPubKey != NULL);
1211 /* load plugins... */
1212 setup_plugin_environment ();
1213 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1214 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),libname);
1215 api = GNUNET_PLUGIN_load (libname, &env);
1216 GNUNET_free (libname);
1219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1220 _("Failed to load transport plugin for http\n"));
1226 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1228 /* testing plugin functionality */
1229 GNUNET_assert (0!=fail_notify_address_count);
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transport plugin returned %u addresses to connect to\n", fail_notify_address_count);
1232 /* testing pretty printer with all addresses obtained from the plugin*/
1237 api->address_pretty_printer (api->cls, "http",cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT, &pretty_printer_cb,NULL);
1238 addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1239 suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1241 GNUNET_assert (GNUNET_OK == suggest_res);
1242 GNUNET_assert (NULL != addr_str);
1244 GNUNET_free ( (char *) addr_str);
1247 GNUNET_assert (fail_pretty_printer_count > 0);
1248 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1249 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1250 fail_pretty_printer=GNUNET_NO;
1251 fail_addr_to_str=GNUNET_NO;
1253 struct IPv4HttpAddress failing_addr;
1255 /* Suggesting addresses with wrong size*/
1256 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1257 failing_addr.u_port = htons(0);
1258 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv6HttpAddress));
1259 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1261 /* Suggesting addresses with wrong address*/
1262 failing_addr.ipv4_addr = htonl(0xffc00000);
1263 failing_addr.u_port = htons(12389);
1264 suggest_res = api->check_address (api->cls,&failing_addr,100);
1265 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1267 /* test sending to client */
1268 multi_handle = curl_multi_init();
1270 /* Setting up buffers */
1271 buffer_in.size = HTTP_BUFFER_SIZE;
1275 buffer_out.size = HTTP_BUFFER_SIZE;
1279 /* Setting up connection tests */
1281 /* Test: connecting without a peer identification */
1282 test_no_ident.test_executed = GNUNET_NO;
1283 test_no_ident.test_failed = GNUNET_YES;
1285 /* Test: connecting with too short peer identification */
1286 test_too_short_ident.test_executed = GNUNET_NO;
1287 test_too_short_ident.test_failed = GNUNET_YES;
1289 /* Test: connecting with too long peer identification */
1290 test_too_long_ident.test_executed = GNUNET_NO;
1291 test_too_long_ident.test_failed = GNUNET_YES;
1293 /* Test: connecting with valid identification */
1294 test_valid_ident.test_executed = GNUNET_NO;
1295 test_valid_ident.test_failed = GNUNET_YES;
1297 test_addr = (char *) api->address_to_string (api->cls,addr_head->addr,addr_head->addrlen);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Phase 0\n\n"));
1300 run_connection_tests(0, NULL);
1302 /* testing finished */
1309 * The main function for the transport service.
1311 * @param argc number of arguments from the command line
1312 * @param argv command line arguments
1313 * @return 0 ok, 1 on error
1316 main (int argc, char *const *argv)
1319 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1320 GNUNET_GETOPT_OPTION_END
1323 char *const argv_prog[] = {
1324 "test_plugin_transport_http",
1326 "test_plugin_transport_data_http.conf",
1335 GNUNET_log_setup ("test_plugin_transport_http",
1344 GNUNET_PROGRAM_run (5,
1346 "test_plugin_transport_http",
1347 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1349 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1354 /* end of test_plugin_transport_http.c */