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>
45 #define VERBOSE GNUNET_NO
46 #define DEBUG_CURL GNUNET_NO
47 #define HTTP_BUFFER_SIZE 2048
49 #define PLUGIN libgnunet_plugin_transport_template
51 #define PROTOCOL_PREFIX "https"
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;
206 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
211 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
216 static struct GNUNET_PeerIdentity my_identity;
221 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
226 static long long unsigned int port;
231 static char * test_addr;
236 struct GNUNET_SCHEDULER_Handle *sched;
239 * Our statistics handle.
241 struct GNUNET_STATISTICS_Handle *stats;
247 const struct GNUNET_CONFIGURATION_Handle *cfg;
250 * Number of neighbours we'd like to have.
252 static uint32_t max_connect_per_transport;
255 * Environment for this plugin.
257 static struct GNUNET_TRANSPORT_PluginEnvironment env;
260 *handle for the api provided by this plugin
262 static struct GNUNET_TRANSPORT_PluginFunctions *api;
265 * ID of the task controlling the testcase timeout
267 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
269 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
271 //const struct GNUNET_PeerIdentity * p;
274 * buffer for data to send
276 static struct HTTP_Message buffer_out;
279 * buffer for data to recieve
281 static struct HTTP_Message buffer_in;
284 struct Plugin_Address * addr_head;
287 * Did the test pass or fail?
289 static int fail_notify_address;
291 * Did the test pass or fail?
293 static int fail_notify_address_count;
296 * Did the test pass or fail?
298 static int fail_pretty_printer;
301 * Did the test pass or fail?
303 static int fail_pretty_printer_count;
306 * Did the test pass or fail?
308 static int fail_addr_to_str;
311 * No. of msgs transmitted successfully to local addresses
313 static int fail_msgs_transmited_to_local_addrs;
316 * Test: transmit msg of max. size
318 static int fail_msg_transmited_bigger_max_size;
321 * Test: transmit msg of max. size
323 static int fail_msg_transmited_max_size;
326 * Test: transmit 2 msgs. in in send operation
328 static int fail_multiple_msgs_in_transmission;
331 * Test: connect to peer without peer identification
333 static struct HTTP_Transfer test_no_ident;
336 * Test: connect to peer without peer identification
338 static struct HTTP_Transfer test_too_short_ident;
341 * Test: connect to peer without peer identification
343 static struct HTTP_Transfer test_too_long_ident;
346 * Test: connect to peer with valid peer identification
348 static struct HTTP_Transfer test_valid_ident;
351 * Test: session selection, use any existing
353 static int fail_session_selection_any;
356 * Test: session selection, use existing inbound session
358 static int fail_session_selection_session;
361 * Test: session selection, use existing inbound session
362 * max message, not fitting in send & recv buffers at one time
364 static int fail_session_selection_session_big;
367 * Test: session selection, use reliable existing
369 static int fail_session_selection_reliable;
372 * Did the test pass or fail?
377 * Number of local addresses
379 static unsigned int count_str_addr;
386 static CURLM *multi_handle;
389 * The task sending data
391 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
394 static char * key_file;
395 static char * cert_file;
403 struct Plugin_Address * cur;
404 struct Plugin_Address * tmp;
406 /* Evaluate results */
408 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Phase 0: Test plugin functions failed\n"));
413 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))
415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Phase 1: Test connect with wrong data failed\n"));
418 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))
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Phase 2: Test session selection failed\n"));
423 if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_multiple_msgs_in_transmission != 2) || (fail_msg_transmited_max_size == GNUNET_YES))
425 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Phase 3: Test sending with plugin failed\n"));
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
433 api->disconnect(api->cls,&my_identity);
435 curl_multi_cleanup(multi_handle);
437 if (NULL != curl_handle)
438 curl_easy_cleanup (curl_handle);
440 /* cleaning addresses */
441 while (addr_head != NULL)
444 tmp = addr_head->next;
445 GNUNET_free (addr_head->addr);
446 GNUNET_free (addr_head);
450 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
452 GNUNET_SCHEDULER_cancel(sched,ti_send);
453 ti_send = GNUNET_SCHEDULER_NO_TASK;
456 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
458 GNUNET_SCHEDULER_cancel(sched,http_task_send);
459 http_task_send = GNUNET_SCHEDULER_NO_TASK;
462 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
464 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
465 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
468 GNUNET_free(test_addr);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading https plugin\n");
470 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_https", api));
472 GNUNET_SCHEDULER_shutdown(sched);
473 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
476 if (0 == stat (cert_file, &sbuf ))
478 if (0 == remove(cert_file))
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file);
481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file);
484 if (0 == stat (key_file, &sbuf ))
486 if (0 == remove(key_file))
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file);
489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file);
492 GNUNET_free (key_file);
493 GNUNET_free (cert_file);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
502 * Continuation called after plugin send message
505 * @result GNUNET_OK or GNUNET_SYSERR
508 static void task_send_cont (void *cls,
509 const struct GNUNET_PeerIdentity * target,
512 struct Plugin_Address * tmp_addr;
513 tmp_addr = addr_head;
515 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
518 fail_msg_transmited_bigger_max_size = GNUNET_NO;
522 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
525 fail_msg_transmited_max_size = GNUNET_NO;
530 static void run_connection_tests( int phase , void * cls);
533 * Recieves messages from plugin, in real world transport
535 static struct GNUNET_TIME_Relative
537 const struct GNUNET_PeerIdentity * peer,
538 const struct GNUNET_MessageHeader * message,
540 struct Session *session,
541 const char *sender_address,
542 uint16_t sender_address_len)
544 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);
546 if ((ntohs(message->type)>=10) && (ntohs(message->type)<20))
548 fail_msgs_transmited_to_local_addrs++;
549 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
550 run_connection_tests(2, session);
554 if ((ntohs(message->type)==20))
556 fail_session_selection_reliable = GNUNET_NO;
559 if ((ntohs(message->type)==21))
561 fail_session_selection_any = GNUNET_NO;
563 if ((ntohs(message->type)==22))
565 fail_session_selection_session = GNUNET_NO;
568 if ((ntohs(message->type)==23))
570 fail_session_selection_session_big = GNUNET_NO;
571 run_connection_tests(3, NULL);
574 if ((ntohs(message->type)==30) || (ntohs(message->type)==31))
576 fail_multiple_msgs_in_transmission ++;
579 if ((ntohs(message->type)==32) && (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1))
581 fail_msg_transmited_max_size = GNUNET_NO;
585 return GNUNET_TIME_UNIT_ZERO;
588 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
592 len = buffer_out.len;
594 if (( buffer_out.pos == len) || (len > (size * nmemb)))
596 memcpy(stream, buffer_out.buf, len);
597 buffer_out.pos = len;
601 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
604 if (buffer_in.pos + size * nmemb > buffer_in.size)
605 return 0; /* overflow */
607 buffer_in.len = size * nmemb;
608 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
609 buffer_in.pos += size * nmemb;
610 buffer_in.len = buffer_in.pos;
611 buffer_in.buf[buffer_in.pos] = '\0';
612 return buffer_in.pos;
615 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
617 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
619 unsigned int len = size * nmemb;
621 tmp = GNUNET_malloc ( len+1 );
623 if (tmp[len-2] == 13)
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
628 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
630 res->http_result_code=100;
632 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
634 res->http_result_code=200;
636 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
638 res->http_result_code=400;
640 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
642 res->http_result_code=404;
644 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
646 res->http_result_code=413;
653 static size_t send_prepare( struct HTTP_Transfer * result);
657 static void send_execute (void *cls,
658 const struct GNUNET_SCHEDULER_TaskContext *tc)
660 struct HTTP_Transfer *res;
666 res = (struct HTTP_Transfer *) cls;
667 http_task_send = GNUNET_SCHEDULER_NO_TASK;
668 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
674 mret = curl_multi_perform (multi_handle, &running);
680 msg = curl_multi_info_read (multi_handle, &running);
683 /* get session for affected curl handle */
684 //cs = find_session_by_curlhandle (msg->easy_handle);
685 //GNUNET_assert ( cs != NULL );
690 if ( (msg->data.result != CURLE_OK) &&
691 (msg->data.result != CURLE_GOT_NOTHING) )
694 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
695 _("curl failed for `%s' at %s:%d: `%s'\n"),
696 "curl_multi_perform",
699 curl_easy_strerror (msg->data.result));
700 /* sending msg failed*/
701 curl_easy_cleanup(curl_handle);
704 run_connection_tests(0, NULL);
706 if (res == &test_no_ident)
708 if ((res->http_result_code==404) && (buffer_in.len==208))
710 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to peer without any peer identification: test passed\n");
711 res->test_failed = GNUNET_NO;
714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer without any peer identification: test failed\n"));
716 if (res == &test_too_short_ident)
718 if ((res->http_result_code==404) && (buffer_in.len==208))
720 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to peer with too short peer identification: test passed\n");
721 res->test_failed = GNUNET_NO;
724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with too short peer identification: test failed\n"));
726 if (res == &test_too_long_ident)
728 if ((res->http_result_code==404) && (buffer_in.len==208))
730 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to peer with too long peer identification: test passed\n");
731 res->test_failed = GNUNET_NO;
734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with too long peer identification: test failed\n"));
736 if (res == &test_valid_ident)
738 if ((res->http_result_code==200))
740 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to peer with valid peer identification: test passed\n");
741 res->test_failed = GNUNET_NO;
744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with valid peer identification: test failed\n"));
746 curl_easy_cleanup(curl_handle);
748 if ((res == &test_valid_ident) && (res->test_failed == GNUNET_NO))
749 run_connection_tests(1, NULL);
750 run_connection_tests(0, NULL);
757 while ( (running > 0) );
760 while (mret == CURLM_CALL_MULTI_PERFORM);
765 * Function setting up file descriptors and scheduling task to run
766 * @param ses session to send data to
767 * @return bytes sent to peer
769 static size_t send_prepare( struct HTTP_Transfer * result)
775 struct GNUNET_NETWORK_FDSet *grs;
776 struct GNUNET_NETWORK_FDSet *gws;
784 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
785 if (mret != CURLM_OK)
787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
788 _("%s failed at %s:%d: `%s'\n"),
789 "curl_multi_fdset", __FILE__, __LINE__,
790 curl_multi_strerror (mret));
793 mret = curl_multi_timeout (multi_handle, &to);
794 if (mret != CURLM_OK)
796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
797 _("%s failed at %s:%d: `%s'\n"),
798 "curl_multi_timeout", __FILE__, __LINE__,
799 curl_multi_strerror (mret));
803 grs = GNUNET_NETWORK_fdset_create ();
804 gws = GNUNET_NETWORK_fdset_create ();
805 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
806 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
807 http_task_send = GNUNET_SCHEDULER_add_select (sched,
808 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
809 GNUNET_SCHEDULER_NO_TASK,
810 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
815 GNUNET_NETWORK_fdset_destroy (gws);
816 GNUNET_NETWORK_fdset_destroy (grs);
818 /* FIXME: return bytes REALLY sent */
823 * function to send data to server
825 static int send_data( struct HTTP_Transfer * result, char * url)
828 curl_handle = curl_easy_init();
829 if( NULL == curl_handle)
831 printf("easy_init failed \n");
832 return GNUNET_SYSERR;
835 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
837 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
838 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
839 //curl_easy_setopt(curl_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
840 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
841 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
842 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
843 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
844 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
845 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
846 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
847 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
848 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
849 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
850 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
852 curl_multi_add_handle(multi_handle, curl_handle);
854 send_prepare(result);
860 * Plugin notifies transport (aka testcase) about its addresses
863 notify_address (void *cls,
867 struct GNUNET_TIME_Relative expires)
869 char address[INET6_ADDRSTRLEN];
871 struct Plugin_Address * pl_addr;
872 struct Plugin_Address * cur;
874 if (addrlen == (sizeof (struct IPv4HttpAddress)))
876 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
877 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
879 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
881 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
882 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 _("Unknown address size: ipv6 has %u ipv4 has %u but this has %u\n"),
888 sizeof (struct IPv6HttpAddress),
889 sizeof (struct IPv4HttpAddress),
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 _("Transport plugin notification for address: `%s':%u\n"),
897 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
898 pl_addr->addrlen = addrlen;
899 pl_addr->addr = GNUNET_malloc(addrlen);
900 memcpy(pl_addr->addr,addr,addrlen);
901 pl_addr->next = NULL;
903 if ( NULL == addr_head)
910 while (NULL != cur->next)
916 fail_notify_address_count++;
917 fail_notify_address = GNUNET_NO;
921 plugin_env_session_end (void *cls,
922 const struct GNUNET_PeerIdentity *peer,
923 struct Session *session)
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Pluging tells me: session %X to peer `%s' ended\n", session, GNUNET_i2s(peer));
930 * Setup plugin environment
933 setup_plugin_environment ()
938 env.my_identity = &my_identity;
940 env.receive = &receive;
941 env.notify_address = ¬ify_address;
942 env.max_connections = max_connect_per_transport;
943 env.session_end = &plugin_env_session_end;
948 * Task shutting down testcase if it a timeout occurs
951 task_timeout (void *cls,
952 const struct GNUNET_SCHEDULER_TaskContext *tc)
954 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
955 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
964 static void pretty_printer_cb (void *cls,
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
970 fail_pretty_printer_count++;
974 * Runs every single test to test the plugin
976 static void run_connection_tests( int phase , void * cls)
978 struct GNUNET_MessageHeader * msg;
983 char * host_str = NULL;
984 /* resetting buffers */
985 buffer_in.size = HTTP_BUFFER_SIZE;
989 buffer_out.size = HTTP_BUFFER_SIZE;
993 if (test_no_ident.test_executed == GNUNET_NO)
995 /* Connecting to peer without identification */
997 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
998 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
999 test_no_ident.test_executed = GNUNET_YES;
1000 send_data ( &test_no_ident, host_str);
1001 GNUNET_free (host_str);
1004 if (test_too_short_ident.test_executed == GNUNET_NO)
1006 char * ident = "AAAAAAAAAA";
1007 /* Connecting to peer with too short identification */
1008 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
1009 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
1010 test_too_short_ident.test_executed = GNUNET_YES;
1011 send_data ( &test_too_short_ident, host_str);
1012 GNUNET_free (host_str);
1016 if (test_too_long_ident.test_executed == GNUNET_NO)
1018 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1020 /* Connecting to peer with too long identification */
1021 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
1022 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
1023 test_too_long_ident.test_executed = GNUNET_YES;
1024 send_data ( &test_too_long_ident, host_str);
1025 GNUNET_free (host_str);
1028 if (test_valid_ident.test_executed == GNUNET_NO)
1030 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
1031 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
1032 GNUNET_asprintf (&host_str, "%s://%s/%s%s",PROTOCOL_PREFIX, test_addr,(char *) &ident,";0");
1033 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
1034 test_valid_ident.test_executed = GNUNET_YES;
1035 send_data ( &test_valid_ident, host_str);
1036 GNUNET_free (host_str);
1042 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 1: transmit data to all suggested addresses\n\n"));
1043 /* Using one of the addresses the plugin proposed */
1044 GNUNET_assert (addr_head->addr != NULL);
1046 struct Plugin_Address * tmp_addr;
1047 struct GNUNET_MessageHeader msg;
1048 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
1049 char address[INET6_ADDRSTRLEN];
1051 unsigned int type = 10;
1053 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
1054 tmp_addr = addr_head;
1055 /* send a message to all addresses advertised by plugin */
1058 while (tmp_addr != NULL)
1060 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
1062 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
1063 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
1066 if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
1068 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
1069 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
1072 msg.type=htons(type);
1073 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1074 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);
1075 tmp_addr = tmp_addr->next;
1086 struct Session * session = cls;
1087 msg = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
1089 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 2: session selection\n\n"));
1090 size = sizeof(struct GNUNET_MessageHeader);
1091 msg->size=htons(size);
1092 msg->type = htons(20);
1093 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1095 msg->type = htons(21);
1096 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1098 /* answer on session*/
1099 size = sizeof( struct GNUNET_MessageHeader);
1100 msg->size = htons(size);
1101 msg->type = htons(22);
1102 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1106 /* answer on session with big message not fitting in mhd send buffer*/
1107 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1108 msg = GNUNET_malloc (size);
1109 msg->size=htons(size);
1110 msg->type = htons(23);
1111 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1119 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 3: send multiple or big messages after disconnect\n\n"));
1120 /* disconnect from peer, so new connections are created */
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Disconnect from peer: `%s'\n", GNUNET_i2s(&my_identity));
1122 api->disconnect(api->cls, &my_identity);
1124 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Phase 3: sending messages\n"));
1125 /* send a multiple GNUNET_messages at a time*/
1126 size = 2 * sizeof(struct GNUNET_MessageHeader);
1127 msg = GNUNET_malloc( 2* size);
1128 msg->size = htons(size);
1129 msg->type = htons(30);
1130 struct GNUNET_MessageHeader * msg2 = &msg[2];
1131 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1132 msg2->type = htons(31);
1133 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);
1135 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1137 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1138 msg = GNUNET_malloc(size);
1139 msg->size = htons(size);
1140 msg->type = htons(32);
1141 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);
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1151 * @param cls closure
1152 * @param s scheduler to use
1153 * @param c configuration to use
1157 struct GNUNET_SCHEDULER_Handle *s,
1159 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1165 unsigned long long tneigh;
1166 struct Plugin_Address * cur;
1167 const char * addr_str;
1169 unsigned int suggest_res;
1171 fail_pretty_printer = GNUNET_YES;
1172 fail_notify_address = GNUNET_YES;
1173 fail_addr_to_str = GNUNET_YES;
1174 fail_msgs_transmited_to_local_addrs = 0;
1175 fail_msg_transmited_max_size = GNUNET_YES;
1176 fail_multiple_msgs_in_transmission = 0;
1177 fail_session_selection_reliable = GNUNET_YES;
1178 fail_session_selection_reliable = GNUNET_YES;
1179 fail_session_selection_session = GNUNET_YES;
1180 fail_session_selection_session_big = GNUNET_YES;
1184 /* parse configuration */
1186 GNUNET_CONFIGURATION_get_value_number (c,
1191 GNUNET_CONFIGURATION_get_value_filename (c,
1193 "HOSTKEY", &keyfile)))
1195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1197 ("Transport service is lacking key configuration settings. Exiting.\n"));
1198 GNUNET_SCHEDULER_shutdown (s);
1204 GNUNET_CONFIGURATION_get_value_number (cfg,
1208 (port > 65535) || (port == 0))
1210 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1213 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1217 /* Get private key file from config */
1218 if (GNUNET_CONFIGURATION_have_value (cfg,
1219 "transport-https", "KEY_FILE"))
1221 GNUNET_CONFIGURATION_get_value_string (cfg,
1226 if (key_file == NULL)
1227 GNUNET_asprintf(&key_file,"https.key");
1229 if (0 == stat (key_file, &sbuf ))
1231 if (0 == remove(key_file))
1232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file);
1234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file);
1237 /* Get private key file from config */
1238 if (GNUNET_CONFIGURATION_have_value (cfg,
1239 "transport-https", "CERT_FILE"))
1241 GNUNET_CONFIGURATION_get_value_string (cfg,
1246 if (cert_file == NULL)
1247 GNUNET_asprintf(&cert_file,"https.cert");
1249 if (0 == stat (cert_file, &sbuf ))
1251 if (0 == remove(cert_file))
1252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file);
1254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file);
1257 max_connect_per_transport = (uint32_t) tneigh;
1258 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1259 GNUNET_free (keyfile);
1260 if (my_private_key == NULL)
1262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1263 _("Transport service could not access hostkey. Exiting.\n"));
1264 GNUNET_SCHEDULER_shutdown (s);
1269 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1270 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1272 /* assertions before start */
1273 GNUNET_assert ((port > 0) && (port <= 65535));
1274 GNUNET_assert(&my_public_key != NULL);
1275 GNUNET_assert(&my_identity.hashPubKey != NULL);
1277 /* load plugins... */
1278 setup_plugin_environment ();
1279 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_https");
1280 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTPS transport plugin `%s'\n"),libname);
1281 api = GNUNET_PLUGIN_load (libname, &env);
1282 GNUNET_free (libname);
1285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1286 _("Failed to load transport plugin for https\n"));
1291 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1293 /* testing plugin functionality */
1294 GNUNET_assert (0!=fail_notify_address_count);
1295 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1297 /* testing pretty printer with all addresses obtained from the plugin*/
1302 api->address_pretty_printer (api->cls, "http",cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT, &pretty_printer_cb,NULL);
1303 addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1304 suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1306 GNUNET_assert (GNUNET_OK == suggest_res);
1307 GNUNET_assert (NULL != addr_str);
1309 GNUNET_free ( (char *) addr_str);
1312 GNUNET_assert (fail_pretty_printer_count > 0);
1313 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1314 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1315 fail_pretty_printer=GNUNET_NO;
1316 fail_addr_to_str=GNUNET_NO;
1318 struct IPv4HttpAddress failing_addr;
1320 /* Suggesting addresses with wrong size*/
1321 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1322 failing_addr.u_port = htons(0);
1323 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv6HttpAddress));
1324 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1326 /* Suggesting addresses with wrong address*/
1327 failing_addr.ipv4_addr = htonl(0xffc00000);
1328 failing_addr.u_port = htons(12389);
1329 suggest_res = api->check_address (api->cls,&failing_addr,100);
1330 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1332 /* test sending to client */
1333 multi_handle = curl_multi_init();
1335 /* Setting up buffers */
1336 buffer_in.size = HTTP_BUFFER_SIZE;
1340 buffer_out.size = HTTP_BUFFER_SIZE;
1344 /* Setting up connection tests */
1346 /* Test: connecting without a peer identification */
1347 test_no_ident.test_executed = GNUNET_NO;
1348 test_no_ident.test_failed = GNUNET_YES;
1350 /* Test: connecting with too short peer identification */
1351 test_too_short_ident.test_executed = GNUNET_NO;
1352 test_too_short_ident.test_failed = GNUNET_YES;
1354 /* Test: connecting with too long peer identification */
1355 test_too_long_ident.test_executed = GNUNET_NO;
1356 test_too_long_ident.test_failed = GNUNET_YES;
1358 /* Test: connecting with valid identification */
1359 test_valid_ident.test_executed = GNUNET_NO;
1360 test_valid_ident.test_failed = GNUNET_YES;
1362 test_addr = (char *) api->address_to_string (api->cls,addr_head->addr,addr_head->addrlen);
1364 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 0\n\n"));
1365 run_connection_tests(0, NULL);
1367 /* testing finished */
1374 * The main function for the transport service.
1376 * @param argc number of arguments from the command line
1377 * @param argv command line arguments
1378 * @return 0 ok, 1 on error
1381 main (int argc, char *const *argv)
1384 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1385 GNUNET_GETOPT_OPTION_END
1388 char *const argv_prog[] = {
1389 "test_plugin_transport_https",
1391 "test_plugin_transport_data_http.conf",
1400 GNUNET_log_setup ("test_plugin_transport_https",
1409 GNUNET_PROGRAM_run (5,
1411 "test_plugin_transport_https",
1412 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1414 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_https");
1419 /* end of test_plugin_transport_http.c */