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_https.c
22 * @brief testcase for plugin_transport_https.c
23 * @author Matthias Wachs
27 #include "gnunet_constants.h"
28 #include "gnunet_common.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_plugin_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_program_lib.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_crypto_lib.h"
40 #include "plugin_transport.h"
41 #include "gnunet_statistics_service.h"
42 #include "transport.h"
43 #include <curl/curl.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
47 #define VERBOSE GNUNET_YES
48 #define DEBUG GNUNET_YES
49 #define DEBUG_CURL GNUNET_YES
50 #define HTTP_BUFFER_SIZE 2048
52 #define PLUGIN libgnunet_plugin_transport_template
55 * How long until we give up on transmitting the message?
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
62 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
65 * How long between recieve and send?
67 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
72 * Struct for plugin addresses
77 * Next field for linked list
79 struct Plugin_Address * next;
82 * buffer containing data to send
87 * amount of data to sent
93 * Message to send using http
100 unsigned char buf[HTTP_BUFFER_SIZE];
103 * current position in buffer
120 * Struct for plugin addresses
125 * amount of bytes we recieved
130 * buffer for http transfers
132 unsigned char buf[HTTP_BUFFER_SIZE];
135 * buffer size this transfer
140 * amount of bytes we recieved
145 * HTTP Header result for transfer
147 unsigned int http_result_code;
152 unsigned int test_failed;
155 * was this test already executed?
157 unsigned int test_executed;
162 * Network format for IPv4 addresses.
164 struct IPv4HttpAddress
167 * IPv4 address, in network byte order.
169 uint32_t ipv4_addr GNUNET_PACKED;
172 * Port number, in network byte order.
174 uint16_t u_port GNUNET_PACKED;
180 * Network format for IPv6 addresses.
182 struct IPv6HttpAddress
187 struct in6_addr ipv6_addr GNUNET_PACKED;
190 * Port number, in network byte order.
192 uint16_t u6_port GNUNET_PACKED;
199 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
204 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
209 static struct GNUNET_PeerIdentity my_identity;
214 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
219 static long long unsigned int port;
224 static char * test_addr;
229 struct GNUNET_SCHEDULER_Handle *sched;
232 * Our statistics handle.
234 struct GNUNET_STATISTICS_Handle *stats;
240 const struct GNUNET_CONFIGURATION_Handle *cfg;
243 * Number of neighbours we'd like to have.
245 static uint32_t max_connect_per_transport;
248 * Environment for this plugin.
250 static struct GNUNET_TRANSPORT_PluginEnvironment env;
253 *handle for the api provided by this plugin
255 static struct GNUNET_TRANSPORT_PluginFunctions *api;
258 * ID of the task controlling the testcase timeout
260 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
262 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
264 //const struct GNUNET_PeerIdentity * p;
267 * buffer for data to send
269 static struct HTTP_Message buffer_out;
272 * buffer for data to recieve
274 static struct HTTP_Message buffer_in;
277 struct Plugin_Address * addr_head;
280 * Did the test pass or fail?
282 static int fail_notify_address;
284 * Did the test pass or fail?
286 static int fail_notify_address_count;
289 * Did the test pass or fail?
291 static int fail_pretty_printer;
294 * Did the test pass or fail?
296 static int fail_pretty_printer_count;
299 * Did the test pass or fail?
301 static int fail_addr_to_str;
304 * No. of msgs transmitted successfully to local addresses
306 static int fail_msgs_transmited_to_local_addrs;
309 * Test: transmit msg of max. size
311 static int fail_msg_transmited_bigger_max_size;
314 * Test: transmit msg of max. size
316 static int fail_msg_transmited_max_size;
319 * Test: transmit 2 msgs. in in send operation
321 static int fail_multiple_msgs_in_transmission;
324 * Test: connect to peer without peer identification
326 static struct HTTP_Transfer test_no_ident;
329 * Test: connect to peer without peer identification
331 static struct HTTP_Transfer test_too_short_ident;
334 * Test: connect to peer without peer identification
336 static struct HTTP_Transfer test_too_long_ident;
339 * Test: connect to peer with valid peer identification
341 static struct HTTP_Transfer test_valid_ident;
344 * Test: session selection, use any existing
346 static int fail_session_selection_any;
349 * Test: session selection, use existing inbound session
351 static int fail_session_selection_session;
354 * Test: session selection, use existing inbound session
355 * max message, not fitting in send & recv buffers at one time
357 static int fail_session_selection_session_big;
360 * Test: session selection, use reliable existing
362 static int fail_session_selection_reliable;
365 * Did the test pass or fail?
370 * Number of local addresses
372 static unsigned int count_str_addr;
379 static CURLM *multi_handle;
382 * The task sending data
384 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
392 struct Plugin_Address * cur;
393 struct Plugin_Address * tmp;
395 /* Evaluate results */
397 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 0: Test plugin functions failed\n");
402 if ((test_no_ident.test_failed == GNUNET_YES) || (test_too_short_ident.test_failed == GNUNET_YES) || (test_too_long_ident.test_failed == GNUNET_YES) || (test_valid_ident.test_failed == GNUNET_YES))
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 1: Test connect with wrong data failed\n");
407 if ((fail_session_selection_any != GNUNET_NO) || (fail_session_selection_reliable != GNUNET_NO) || (fail_session_selection_session != GNUNET_NO) || (fail_session_selection_session_big != GNUNET_NO))
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 2: Test session selection failed\n");
412 if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_multiple_msgs_in_transmission != 2) || (fail_msg_transmited_max_size == GNUNET_YES))
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Phase 3: Test sending with plugin failed\n");
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
422 api->disconnect(api->cls,&my_identity);
424 curl_multi_cleanup(multi_handle);
426 if (NULL != curl_handle)
427 curl_easy_cleanup (curl_handle);
429 /* cleaning addresses */
430 while (addr_head != NULL)
433 tmp = addr_head->next;
434 GNUNET_free (addr_head->addr);
435 GNUNET_free (addr_head);
439 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
441 GNUNET_SCHEDULER_cancel(sched,ti_send);
442 ti_send = GNUNET_SCHEDULER_NO_TASK;
445 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
447 GNUNET_SCHEDULER_cancel(sched,http_task_send);
448 http_task_send = GNUNET_SCHEDULER_NO_TASK;
451 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
453 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
454 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
457 GNUNET_free(test_addr);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
459 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
461 GNUNET_SCHEDULER_shutdown(sched);
462 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
471 * Continuation called after plugin send message
474 * @result GNUNET_OK or GNUNET_SYSERR
477 static void task_send_cont (void *cls,
478 const struct GNUNET_PeerIdentity * target,
481 struct Plugin_Address * tmp_addr;
482 tmp_addr = addr_head;
484 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
487 fail_msg_transmited_bigger_max_size = GNUNET_NO;
491 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
494 fail_msg_transmited_max_size = GNUNET_NO;
499 static void run_connection_tests( int phase , void * cls);
502 * Recieves messages from plugin, in real world transport
504 static struct GNUNET_TIME_Relative
506 const struct GNUNET_PeerIdentity * peer,
507 const struct GNUNET_MessageHeader * message,
509 struct Session *session,
510 const char *sender_address,
511 uint16_t sender_address_len)
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase recieved new message from peer `%s' with type %u and length %u, session %X\n", GNUNET_i2s(peer), ntohs(message->type), ntohs(message->size),session);
515 if ((ntohs(message->type)>=10) && (ntohs(message->type)<20))
517 fail_msgs_transmited_to_local_addrs++;
518 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
519 run_connection_tests(2, session);
523 if ((ntohs(message->type)==20))
525 fail_session_selection_reliable = GNUNET_NO;
528 if ((ntohs(message->type)==21))
530 fail_session_selection_any = GNUNET_NO;
532 if ((ntohs(message->type)==22))
534 fail_session_selection_session = GNUNET_NO;
537 if ((ntohs(message->type)==23))
539 fail_session_selection_session_big = GNUNET_NO;
540 run_connection_tests(3, NULL);
543 if ((ntohs(message->type)==30) || (ntohs(message->type)==31))
545 fail_multiple_msgs_in_transmission ++;
548 if ((ntohs(message->type)==32) && (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1))
550 fail_msg_transmited_max_size = GNUNET_NO;
554 return GNUNET_TIME_UNIT_ZERO;
557 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
561 len = buffer_out.len;
563 if (( buffer_out.pos == len) || (len > (size * nmemb)))
565 memcpy(stream, buffer_out.buf, len);
566 buffer_out.pos = len;
570 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
573 if (buffer_in.pos + size * nmemb > buffer_in.size)
574 return 0; /* overflow */
576 buffer_in.len = size * nmemb;
577 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
578 buffer_in.pos += size * nmemb;
579 buffer_in.len = buffer_in.pos;
580 buffer_in.buf[buffer_in.pos] = '\0';
581 return buffer_in.pos;
584 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
586 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
588 unsigned int len = size * nmemb;
590 tmp = GNUNET_malloc ( len+1 );
592 if (tmp[len-2] == 13)
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
597 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
599 res->http_result_code=100;
601 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
603 res->http_result_code=200;
605 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
607 res->http_result_code=400;
609 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
611 res->http_result_code=404;
613 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
615 res->http_result_code=413;
622 static size_t send_prepare( struct HTTP_Transfer * result);
626 static void send_execute (void *cls,
627 const struct GNUNET_SCHEDULER_TaskContext *tc)
629 struct HTTP_Transfer *res;
635 res = (struct HTTP_Transfer *) cls;
636 http_task_send = GNUNET_SCHEDULER_NO_TASK;
637 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
643 mret = curl_multi_perform (multi_handle, &running);
649 msg = curl_multi_info_read (multi_handle, &running);
652 /* get session for affected curl handle */
653 //cs = find_session_by_curlhandle (msg->easy_handle);
654 //GNUNET_assert ( cs != NULL );
659 if ( (msg->data.result != CURLE_OK) &&
660 (msg->data.result != CURLE_GOT_NOTHING) )
663 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
664 _("curl failed for `%s' at %s:%d: `%s'\n"),
665 "curl_multi_perform",
668 curl_easy_strerror (msg->data.result));
669 /* sending msg failed*/
670 curl_easy_cleanup(curl_handle);
673 run_connection_tests(0, NULL);
675 if (res == &test_no_ident)
677 if ((res->http_result_code==404) && (buffer_in.len==208))
679 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
680 res->test_failed = GNUNET_NO;
683 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
685 if (res == &test_too_short_ident)
687 if ((res->http_result_code==404) && (buffer_in.len==208))
689 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
690 res->test_failed = GNUNET_NO;
693 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
695 if (res == &test_too_long_ident)
697 if ((res->http_result_code==404) && (buffer_in.len==208))
699 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
700 res->test_failed = GNUNET_NO;
703 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
705 if (res == &test_valid_ident)
707 if ((res->http_result_code==200))
709 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
710 res->test_failed = GNUNET_NO;
713 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
715 curl_easy_cleanup(curl_handle);
717 if ((res == &test_valid_ident) && (res->test_failed == GNUNET_NO))
718 run_connection_tests(1, NULL);
719 run_connection_tests(0, NULL);
726 while ( (running > 0) );
729 while (mret == CURLM_CALL_MULTI_PERFORM);
734 * Function setting up file descriptors and scheduling task to run
735 * @param ses session to send data to
736 * @return bytes sent to peer
738 static size_t send_prepare( struct HTTP_Transfer * result)
744 struct GNUNET_NETWORK_FDSet *grs;
745 struct GNUNET_NETWORK_FDSet *gws;
753 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
754 if (mret != CURLM_OK)
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757 _("%s failed at %s:%d: `%s'\n"),
758 "curl_multi_fdset", __FILE__, __LINE__,
759 curl_multi_strerror (mret));
762 mret = curl_multi_timeout (multi_handle, &to);
763 if (mret != CURLM_OK)
765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
766 _("%s failed at %s:%d: `%s'\n"),
767 "curl_multi_timeout", __FILE__, __LINE__,
768 curl_multi_strerror (mret));
772 grs = GNUNET_NETWORK_fdset_create ();
773 gws = GNUNET_NETWORK_fdset_create ();
774 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
775 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
776 http_task_send = GNUNET_SCHEDULER_add_select (sched,
777 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
778 GNUNET_SCHEDULER_NO_TASK,
779 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
784 GNUNET_NETWORK_fdset_destroy (gws);
785 GNUNET_NETWORK_fdset_destroy (grs);
787 /* FIXME: return bytes REALLY sent */
792 * function to send data to server
794 static int send_data( struct HTTP_Transfer * result, char * url)
797 curl_handle = curl_easy_init();
798 if( NULL == curl_handle)
800 printf("easy_init failed \n");
801 return GNUNET_SYSERR;
804 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
806 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
807 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
808 //curl_easy_setopt(curl_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
809 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
810 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
811 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
812 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
813 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
814 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
815 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
816 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
817 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
818 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
819 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
821 curl_multi_add_handle(multi_handle, curl_handle);
823 send_prepare(result);
829 * Plugin notifies transport (aka testcase) about its addresses
832 notify_address (void *cls,
836 struct GNUNET_TIME_Relative expires)
838 char address[INET6_ADDRSTRLEN];
840 struct Plugin_Address * pl_addr;
841 struct Plugin_Address * cur;
843 if (addrlen == (sizeof (struct IPv4HttpAddress)))
845 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
846 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
848 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
850 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
851 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 _("Unknown address size: ipv6 has %u ipv4 has %u but this has %u\n"),
857 sizeof (struct IPv6HttpAddress),
858 sizeof (struct IPv4HttpAddress),
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 _("Transport plugin notification for address: `%s':%u\n"),
866 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
867 pl_addr->addrlen = addrlen;
868 pl_addr->addr = GNUNET_malloc(addrlen);
869 memcpy(pl_addr->addr,addr,addrlen);
870 pl_addr->next = NULL;
872 if ( NULL == addr_head)
879 while (NULL != cur->next)
885 fail_notify_address_count++;
886 fail_notify_address = GNUNET_NO;
890 plugin_env_session_end (void *cls,
891 const struct GNUNET_PeerIdentity *peer,
892 struct Session *session)
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Pluging tells me: session %X to peer `%s' ended\n", session, GNUNET_i2s(peer));
899 * Setup plugin environment
902 setup_plugin_environment ()
907 env.my_identity = &my_identity;
909 env.receive = &receive;
910 env.notify_address = ¬ify_address;
911 env.max_connections = max_connect_per_transport;
912 env.session_end = &plugin_env_session_end;
917 * Task shutting down testcase if it a timeout occurs
920 task_timeout (void *cls,
921 const struct GNUNET_SCHEDULER_TaskContext *tc)
923 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
924 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
933 static void pretty_printer_cb (void *cls,
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
939 fail_pretty_printer_count++;
943 * Runs every single test to test the plugin
945 static void run_connection_tests( int phase , void * cls)
947 struct GNUNET_MessageHeader * msg;
952 char * host_str = NULL;
953 /* resetting buffers */
954 buffer_in.size = HTTP_BUFFER_SIZE;
958 buffer_out.size = HTTP_BUFFER_SIZE;
962 if (test_no_ident.test_executed == GNUNET_NO)
964 /* Connecting to peer without identification */
966 GNUNET_asprintf (&host_str, "https://%s/%s",test_addr,ident);
967 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
968 test_no_ident.test_executed = GNUNET_YES;
969 send_data ( &test_no_ident, host_str);
970 GNUNET_free (host_str);
973 if (test_too_short_ident.test_executed == GNUNET_NO)
975 char * ident = "AAAAAAAAAA";
976 /* Connecting to peer with too short identification */
977 GNUNET_asprintf (&host_str, "https://%s/%s",test_addr,ident);
978 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
979 test_too_short_ident.test_executed = GNUNET_YES;
980 send_data ( &test_too_short_ident, host_str);
981 GNUNET_free (host_str);
985 if (test_too_long_ident.test_executed == GNUNET_NO)
987 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
989 /* Connecting to peer with too long identification */
990 GNUNET_asprintf (&host_str, "https://%s/%s",test_addr,ident);
991 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
992 test_too_long_ident.test_executed = GNUNET_YES;
993 send_data ( &test_too_long_ident, host_str);
994 GNUNET_free (host_str);
997 if (test_valid_ident.test_executed == GNUNET_NO)
999 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
1000 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
1001 GNUNET_asprintf (&host_str, "https://%s/%s%s",test_addr,(char *) &ident,";0");
1002 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
1003 test_valid_ident.test_executed = GNUNET_YES;
1004 send_data ( &test_valid_ident, host_str);
1005 GNUNET_free (host_str);
1011 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 1: transmit data to all suggested addresses\n\n"));
1012 /* Using one of the addresses the plugin proposed */
1013 GNUNET_assert (addr_head->addr != NULL);
1015 struct Plugin_Address * tmp_addr;
1016 struct GNUNET_MessageHeader msg;
1017 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
1018 char address[INET6_ADDRSTRLEN];
1020 unsigned int type = 10;
1022 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
1023 tmp_addr = addr_head;
1024 /* send a message to all addresses advertised by plugin */
1027 while (tmp_addr != NULL)
1029 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
1031 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
1032 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
1035 if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
1037 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
1038 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
1041 msg.type=htons(type);
1042 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1043 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);
1044 tmp_addr = tmp_addr->next;
1055 struct Session * session = cls;
1056 msg = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
1058 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 2: session selection\n\n"));
1059 size = sizeof(struct GNUNET_MessageHeader);
1060 msg->size=htons(size);
1061 msg->type = htons(20);
1062 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1064 msg->type = htons(21);
1065 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1067 /* answer on session*/
1068 size = sizeof( struct GNUNET_MessageHeader);
1069 msg->size = htons(size);
1070 msg->type = htons(22);
1071 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1075 /* answer on session with big message not fitting in mhd send buffer*/
1076 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1077 msg = GNUNET_malloc (size);
1078 msg->size=htons(size);
1079 msg->type = htons(23);
1080 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1088 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 3: send multiple or big messages after disconnect\n\n"));
1089 /* disconnect from peer, so new connections are created */
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Disconnect from peer: `%s'\n", GNUNET_i2s(&my_identity));
1091 api->disconnect(api->cls, &my_identity);
1093 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Phase 3: sending messages\n"));
1094 /* send a multiple GNUNET_messages at a time*/
1095 size = 2 * sizeof(struct GNUNET_MessageHeader);
1096 msg = GNUNET_malloc( 2* size);
1097 msg->size = htons(size);
1098 msg->type = htons(30);
1099 struct GNUNET_MessageHeader * msg2 = &msg[2];
1100 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1101 msg2->type = htons(31);
1102 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);
1104 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1106 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1107 msg = GNUNET_malloc(size);
1108 msg->size = htons(size);
1109 msg->type = htons(32);
1110 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);
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1120 * @param cls closure
1121 * @param s scheduler to use
1122 * @param c configuration to use
1126 struct GNUNET_SCHEDULER_Handle *s,
1128 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1134 unsigned long long tneigh;
1135 struct Plugin_Address * cur;
1136 const char * addr_str;
1139 unsigned int suggest_res;
1141 fail_pretty_printer = GNUNET_YES;
1142 fail_notify_address = GNUNET_YES;
1143 fail_addr_to_str = GNUNET_YES;
1144 fail_msgs_transmited_to_local_addrs = 0;
1145 fail_msg_transmited_max_size = GNUNET_YES;
1146 fail_multiple_msgs_in_transmission = 0;
1147 fail_session_selection_reliable = GNUNET_YES;
1148 fail_session_selection_reliable = GNUNET_YES;
1149 fail_session_selection_session = GNUNET_YES;
1150 fail_session_selection_session_big = GNUNET_YES;
1154 /* parse configuration */
1156 GNUNET_CONFIGURATION_get_value_number (c,
1161 GNUNET_CONFIGURATION_get_value_filename (c,
1163 "HOSTKEY", &keyfile)))
1165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1167 ("Transport service is lacking key configuration settings. Exiting.\n"));
1168 GNUNET_SCHEDULER_shutdown (s);
1174 GNUNET_CONFIGURATION_get_value_number (cfg,
1178 (port > 65535) || (port == 0))
1180 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1183 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1187 max_connect_per_transport = (uint32_t) tneigh;
1188 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1189 GNUNET_free (keyfile);
1190 if (my_private_key == NULL)
1192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1193 _("Transport service could not access hostkey. Exiting.\n"));
1194 GNUNET_SCHEDULER_shutdown (s);
1199 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1200 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1202 /* assertions before start */
1203 GNUNET_assert ((port > 0) && (port <= 65535));
1204 GNUNET_assert(&my_public_key != NULL);
1205 GNUNET_assert(&my_identity.hashPubKey != NULL);
1207 /* load plugins... */
1208 setup_plugin_environment ();
1209 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTPS transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1210 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_https");
1211 api = GNUNET_PLUGIN_load (libname, &env);
1212 GNUNET_free (libname);
1215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1216 _("Failed to load transport plugin for https\n"));
1221 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1223 /* testing plugin functionality */
1224 GNUNET_assert (0!=fail_notify_address_count);
1225 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1227 /* testing pretty printer with all addresses obtained from the plugin*/
1232 api->address_pretty_printer (api->cls, "http",cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT, &pretty_printer_cb,NULL);
1233 addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1234 suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1236 GNUNET_assert (GNUNET_OK == suggest_res);
1237 GNUNET_assert (NULL != addr_str);
1239 GNUNET_free ( (char *) addr_str);
1242 GNUNET_assert (fail_pretty_printer_count > 0);
1243 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1244 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1245 fail_pretty_printer=GNUNET_NO;
1246 fail_addr_to_str=GNUNET_NO;
1248 /* Suggesting addresses with wrong port*/
1249 struct IPv4HttpAddress failing_addr;
1250 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1251 failing_addr.u_port = htons(0);
1252 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv4HttpAddress));
1253 GNUNET_assert (GNUNET_SYSERR == suggest_res);
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_INFO, _("\nPhase 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_https",
1326 "test_plugin_transport_data_http.conf",
1335 GNUNET_log_setup ("test_plugin_transport_https",
1344 GNUNET_PROGRAM_run (5,
1346 "test_plugin_transport_https",
1347 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1349 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_https");
1354 /* end of test_plugin_transport_http.c */