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_NO
48 #define DEBUG_CURL GNUNET_NO
49 #define HTTP_BUFFER_SIZE 2048
51 #define PLUGIN libgnunet_plugin_transport_template
53 #define PROTOCOL_PREFIX "https"
56 * How long until we give up on transmitting the message?
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
63 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
66 * How long between recieve and send?
68 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
73 * Struct for plugin addresses
78 * Next field for linked list
80 struct Plugin_Address * next;
83 * buffer containing data to send
88 * amount of data to sent
94 * Message to send using http
101 unsigned char buf[HTTP_BUFFER_SIZE];
104 * current position in buffer
121 * Struct for plugin addresses
126 * amount of bytes we recieved
131 * buffer for http transfers
133 unsigned char buf[HTTP_BUFFER_SIZE];
136 * buffer size this transfer
141 * amount of bytes we recieved
146 * HTTP Header result for transfer
148 unsigned int http_result_code;
153 unsigned int test_failed;
156 * was this test already executed?
158 unsigned int test_executed;
163 * Network format for IPv4 addresses.
165 struct IPv4HttpAddress
168 * IPv4 address, in network byte order.
170 uint32_t ipv4_addr GNUNET_PACKED;
173 * Port number, in network byte order.
175 uint16_t u_port GNUNET_PACKED;
181 * Network format for IPv6 addresses.
183 struct IPv6HttpAddress
188 struct in6_addr ipv6_addr GNUNET_PACKED;
191 * Port number, in network byte order.
193 uint16_t u6_port GNUNET_PACKED;
200 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
205 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
210 static struct GNUNET_PeerIdentity my_identity;
215 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
220 static long long unsigned int port;
225 static char * test_addr;
230 struct GNUNET_SCHEDULER_Handle *sched;
233 * Our statistics handle.
235 struct GNUNET_STATISTICS_Handle *stats;
241 const struct GNUNET_CONFIGURATION_Handle *cfg;
244 * Number of neighbours we'd like to have.
246 static uint32_t max_connect_per_transport;
249 * Environment for this plugin.
251 static struct GNUNET_TRANSPORT_PluginEnvironment env;
254 *handle for the api provided by this plugin
256 static struct GNUNET_TRANSPORT_PluginFunctions *api;
259 * ID of the task controlling the testcase timeout
261 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
263 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
265 //const struct GNUNET_PeerIdentity * p;
268 * buffer for data to send
270 static struct HTTP_Message buffer_out;
273 * buffer for data to recieve
275 static struct HTTP_Message buffer_in;
278 struct Plugin_Address * addr_head;
281 * Did the test pass or fail?
283 static int fail_notify_address;
285 * Did the test pass or fail?
287 static int fail_notify_address_count;
290 * Did the test pass or fail?
292 static int fail_pretty_printer;
295 * Did the test pass or fail?
297 static int fail_pretty_printer_count;
300 * Did the test pass or fail?
302 static int fail_addr_to_str;
305 * No. of msgs transmitted successfully to local addresses
307 static int fail_msgs_transmited_to_local_addrs;
310 * Test: transmit msg of max. size
312 static int fail_msg_transmited_bigger_max_size;
315 * Test: transmit msg of max. size
317 static int fail_msg_transmited_max_size;
320 * Test: transmit 2 msgs. in in send operation
322 static int fail_multiple_msgs_in_transmission;
325 * Test: connect to peer without peer identification
327 static struct HTTP_Transfer test_no_ident;
330 * Test: connect to peer without peer identification
332 static struct HTTP_Transfer test_too_short_ident;
335 * Test: connect to peer without peer identification
337 static struct HTTP_Transfer test_too_long_ident;
340 * Test: connect to peer with valid peer identification
342 static struct HTTP_Transfer test_valid_ident;
345 * Test: session selection, use any existing
347 static int fail_session_selection_any;
350 * Test: session selection, use existing inbound session
352 static int fail_session_selection_session;
355 * Test: session selection, use existing inbound session
356 * max message, not fitting in send & recv buffers at one time
358 static int fail_session_selection_session_big;
361 * Test: session selection, use reliable existing
363 static int fail_session_selection_reliable;
366 * Did the test pass or fail?
371 * Number of local addresses
373 static unsigned int count_str_addr;
380 static CURLM *multi_handle;
383 * The task sending data
385 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
388 static char * key_file;
389 static char * cert_file;
397 struct Plugin_Address * cur;
398 struct Plugin_Address * tmp;
400 /* Evaluate results */
402 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Phase 0: Test plugin functions failed\n"));
407 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))
409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Phase 1: Test connect with wrong data failed\n"));
412 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))
414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Phase 2: Test session selection failed\n"));
417 if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_multiple_msgs_in_transmission != 2) || (fail_msg_transmited_max_size == GNUNET_YES))
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Phase 3: Test sending with plugin failed\n"));
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
427 api->disconnect(api->cls,&my_identity);
429 curl_multi_cleanup(multi_handle);
431 if (NULL != curl_handle)
432 curl_easy_cleanup (curl_handle);
434 /* cleaning addresses */
435 while (addr_head != NULL)
438 tmp = addr_head->next;
439 GNUNET_free (addr_head->addr);
440 GNUNET_free (addr_head);
444 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
446 GNUNET_SCHEDULER_cancel(sched,ti_send);
447 ti_send = GNUNET_SCHEDULER_NO_TASK;
450 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
452 GNUNET_SCHEDULER_cancel(sched,http_task_send);
453 http_task_send = GNUNET_SCHEDULER_NO_TASK;
456 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
458 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
459 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
462 GNUNET_free(test_addr);
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
464 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
466 GNUNET_SCHEDULER_shutdown(sched);
467 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
470 if (0 == stat (cert_file, &sbuf ))
472 if (0 == remove(cert_file))
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file);
475 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file);
478 if (0 == stat (key_file, &sbuf ))
480 if (0 == remove(key_file))
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file);
483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file);
486 GNUNET_free (key_file);
487 GNUNET_free (cert_file);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
496 * Continuation called after plugin send message
499 * @result GNUNET_OK or GNUNET_SYSERR
502 static void task_send_cont (void *cls,
503 const struct GNUNET_PeerIdentity * target,
506 struct Plugin_Address * tmp_addr;
507 tmp_addr = addr_head;
509 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
512 fail_msg_transmited_bigger_max_size = GNUNET_NO;
516 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
519 fail_msg_transmited_max_size = GNUNET_NO;
524 static void run_connection_tests( int phase , void * cls);
527 * Recieves messages from plugin, in real world transport
529 static struct GNUNET_TIME_Relative
531 const struct GNUNET_PeerIdentity * peer,
532 const struct GNUNET_MessageHeader * message,
534 struct Session *session,
535 const char *sender_address,
536 uint16_t sender_address_len)
538 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);
540 if ((ntohs(message->type)>=10) && (ntohs(message->type)<20))
542 fail_msgs_transmited_to_local_addrs++;
543 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
544 run_connection_tests(2, session);
548 if ((ntohs(message->type)==20))
550 fail_session_selection_reliable = GNUNET_NO;
553 if ((ntohs(message->type)==21))
555 fail_session_selection_any = GNUNET_NO;
557 if ((ntohs(message->type)==22))
559 fail_session_selection_session = GNUNET_NO;
562 if ((ntohs(message->type)==23))
564 fail_session_selection_session_big = GNUNET_NO;
565 run_connection_tests(3, NULL);
568 if ((ntohs(message->type)==30) || (ntohs(message->type)==31))
570 fail_multiple_msgs_in_transmission ++;
573 if ((ntohs(message->type)==32) && (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1))
575 fail_msg_transmited_max_size = GNUNET_NO;
579 return GNUNET_TIME_UNIT_ZERO;
582 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
586 len = buffer_out.len;
588 if (( buffer_out.pos == len) || (len > (size * nmemb)))
590 memcpy(stream, buffer_out.buf, len);
591 buffer_out.pos = len;
595 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
598 if (buffer_in.pos + size * nmemb > buffer_in.size)
599 return 0; /* overflow */
601 buffer_in.len = size * nmemb;
602 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
603 buffer_in.pos += size * nmemb;
604 buffer_in.len = buffer_in.pos;
605 buffer_in.buf[buffer_in.pos] = '\0';
606 return buffer_in.pos;
609 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
611 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
613 unsigned int len = size * nmemb;
615 tmp = GNUNET_malloc ( len+1 );
617 if (tmp[len-2] == 13)
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
622 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
624 res->http_result_code=100;
626 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
628 res->http_result_code=200;
630 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
632 res->http_result_code=400;
634 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
636 res->http_result_code=404;
638 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
640 res->http_result_code=413;
647 static size_t send_prepare( struct HTTP_Transfer * result);
651 static void send_execute (void *cls,
652 const struct GNUNET_SCHEDULER_TaskContext *tc)
654 struct HTTP_Transfer *res;
660 res = (struct HTTP_Transfer *) cls;
661 http_task_send = GNUNET_SCHEDULER_NO_TASK;
662 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
668 mret = curl_multi_perform (multi_handle, &running);
674 msg = curl_multi_info_read (multi_handle, &running);
677 /* get session for affected curl handle */
678 //cs = find_session_by_curlhandle (msg->easy_handle);
679 //GNUNET_assert ( cs != NULL );
684 if ( (msg->data.result != CURLE_OK) &&
685 (msg->data.result != CURLE_GOT_NOTHING) )
688 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
689 _("curl failed for `%s' at %s:%d: `%s'\n"),
690 "curl_multi_perform",
693 curl_easy_strerror (msg->data.result));
694 /* sending msg failed*/
695 curl_easy_cleanup(curl_handle);
698 run_connection_tests(0, NULL);
700 if (res == &test_no_ident)
702 if ((res->http_result_code==404) && (buffer_in.len==208))
704 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to peer without any peer identification: test passed\n");
705 res->test_failed = GNUNET_NO;
708 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer without any peer identification: test failed\n"));
710 if (res == &test_too_short_ident)
712 if ((res->http_result_code==404) && (buffer_in.len==208))
714 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to peer with too short peer identification: test passed\n");
715 res->test_failed = GNUNET_NO;
718 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with too short peer identification: test failed\n"));
720 if (res == &test_too_long_ident)
722 if ((res->http_result_code==404) && (buffer_in.len==208))
724 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to peer with too long peer identification: test passed\n");
725 res->test_failed = GNUNET_NO;
728 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with too long peer identification: test failed\n"));
730 if (res == &test_valid_ident)
732 if ((res->http_result_code==200))
734 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connecting to peer with valid peer identification: test passed\n");
735 res->test_failed = GNUNET_NO;
738 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Connecting to peer with valid peer identification: test failed\n"));
740 curl_easy_cleanup(curl_handle);
742 if ((res == &test_valid_ident) && (res->test_failed == GNUNET_NO))
743 run_connection_tests(1, NULL);
744 run_connection_tests(0, NULL);
751 while ( (running > 0) );
754 while (mret == CURLM_CALL_MULTI_PERFORM);
759 * Function setting up file descriptors and scheduling task to run
760 * @param ses session to send data to
761 * @return bytes sent to peer
763 static size_t send_prepare( struct HTTP_Transfer * result)
769 struct GNUNET_NETWORK_FDSet *grs;
770 struct GNUNET_NETWORK_FDSet *gws;
778 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
779 if (mret != CURLM_OK)
781 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
782 _("%s failed at %s:%d: `%s'\n"),
783 "curl_multi_fdset", __FILE__, __LINE__,
784 curl_multi_strerror (mret));
787 mret = curl_multi_timeout (multi_handle, &to);
788 if (mret != CURLM_OK)
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
791 _("%s failed at %s:%d: `%s'\n"),
792 "curl_multi_timeout", __FILE__, __LINE__,
793 curl_multi_strerror (mret));
797 grs = GNUNET_NETWORK_fdset_create ();
798 gws = GNUNET_NETWORK_fdset_create ();
799 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
800 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
801 http_task_send = GNUNET_SCHEDULER_add_select (sched,
802 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
803 GNUNET_SCHEDULER_NO_TASK,
804 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
809 GNUNET_NETWORK_fdset_destroy (gws);
810 GNUNET_NETWORK_fdset_destroy (grs);
812 /* FIXME: return bytes REALLY sent */
817 * function to send data to server
819 static int send_data( struct HTTP_Transfer * result, char * url)
822 curl_handle = curl_easy_init();
823 if( NULL == curl_handle)
825 printf("easy_init failed \n");
826 return GNUNET_SYSERR;
829 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
831 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
832 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
833 //curl_easy_setopt(curl_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
834 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
835 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
836 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
837 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
838 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
839 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
840 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
841 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
842 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
843 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
844 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
846 curl_multi_add_handle(multi_handle, curl_handle);
848 send_prepare(result);
854 * Plugin notifies transport (aka testcase) about its addresses
857 notify_address (void *cls,
861 struct GNUNET_TIME_Relative expires)
863 char address[INET6_ADDRSTRLEN];
865 struct Plugin_Address * pl_addr;
866 struct Plugin_Address * cur;
868 if (addrlen == (sizeof (struct IPv4HttpAddress)))
870 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
871 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
873 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
875 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
876 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 _("Unknown address size: ipv6 has %u ipv4 has %u but this has %u\n"),
882 sizeof (struct IPv6HttpAddress),
883 sizeof (struct IPv4HttpAddress),
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 _("Transport plugin notification for address: `%s':%u\n"),
891 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
892 pl_addr->addrlen = addrlen;
893 pl_addr->addr = GNUNET_malloc(addrlen);
894 memcpy(pl_addr->addr,addr,addrlen);
895 pl_addr->next = NULL;
897 if ( NULL == addr_head)
904 while (NULL != cur->next)
910 fail_notify_address_count++;
911 fail_notify_address = GNUNET_NO;
915 plugin_env_session_end (void *cls,
916 const struct GNUNET_PeerIdentity *peer,
917 struct Session *session)
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Pluging tells me: session %X to peer `%s' ended\n", session, GNUNET_i2s(peer));
924 * Setup plugin environment
927 setup_plugin_environment ()
932 env.my_identity = &my_identity;
934 env.receive = &receive;
935 env.notify_address = ¬ify_address;
936 env.max_connections = max_connect_per_transport;
937 env.session_end = &plugin_env_session_end;
942 * Task shutting down testcase if it a timeout occurs
945 task_timeout (void *cls,
946 const struct GNUNET_SCHEDULER_TaskContext *tc)
948 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
949 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
958 static void pretty_printer_cb (void *cls,
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
964 fail_pretty_printer_count++;
968 * Runs every single test to test the plugin
970 static void run_connection_tests( int phase , void * cls)
972 struct GNUNET_MessageHeader * msg;
977 char * host_str = NULL;
978 /* resetting buffers */
979 buffer_in.size = HTTP_BUFFER_SIZE;
983 buffer_out.size = HTTP_BUFFER_SIZE;
987 if (test_no_ident.test_executed == GNUNET_NO)
989 /* Connecting to peer without identification */
991 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
992 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
993 test_no_ident.test_executed = GNUNET_YES;
994 send_data ( &test_no_ident, host_str);
995 GNUNET_free (host_str);
998 if (test_too_short_ident.test_executed == GNUNET_NO)
1000 char * ident = "AAAAAAAAAA";
1001 /* Connecting to peer with too short identification */
1002 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
1003 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
1004 test_too_short_ident.test_executed = GNUNET_YES;
1005 send_data ( &test_too_short_ident, host_str);
1006 GNUNET_free (host_str);
1010 if (test_too_long_ident.test_executed == GNUNET_NO)
1012 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
1014 /* Connecting to peer with too long identification */
1015 GNUNET_asprintf (&host_str, "%s://%s/%s",PROTOCOL_PREFIX, test_addr,ident);
1016 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
1017 test_too_long_ident.test_executed = GNUNET_YES;
1018 send_data ( &test_too_long_ident, host_str);
1019 GNUNET_free (host_str);
1022 if (test_valid_ident.test_executed == GNUNET_NO)
1024 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
1025 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
1026 GNUNET_asprintf (&host_str, "%s://%s/%s%s",PROTOCOL_PREFIX, test_addr,(char *) &ident,";0");
1027 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
1028 test_valid_ident.test_executed = GNUNET_YES;
1029 send_data ( &test_valid_ident, host_str);
1030 GNUNET_free (host_str);
1036 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 1: transmit data to all suggested addresses\n\n"));
1037 /* Using one of the addresses the plugin proposed */
1038 GNUNET_assert (addr_head->addr != NULL);
1040 struct Plugin_Address * tmp_addr;
1041 struct GNUNET_MessageHeader msg;
1042 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
1043 char address[INET6_ADDRSTRLEN];
1045 unsigned int type = 10;
1047 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
1048 tmp_addr = addr_head;
1049 /* send a message to all addresses advertised by plugin */
1052 while (tmp_addr != NULL)
1054 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
1056 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
1057 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
1060 if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
1062 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
1063 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
1066 msg.type=htons(type);
1067 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1068 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);
1069 tmp_addr = tmp_addr->next;
1080 struct Session * session = cls;
1081 msg = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
1083 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 2: session selection\n\n"));
1084 size = sizeof(struct GNUNET_MessageHeader);
1085 msg->size=htons(size);
1086 msg->type = htons(20);
1087 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1089 msg->type = htons(21);
1090 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, NULL, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1092 /* answer on session*/
1093 size = sizeof( struct GNUNET_MessageHeader);
1094 msg->size = htons(size);
1095 msg->type = htons(22);
1096 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_SYSERR, &task_send_cont, NULL);
1100 /* answer on session with big message not fitting in mhd send buffer*/
1101 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1102 msg = GNUNET_malloc (size);
1103 msg->size=htons(size);
1104 msg->type = htons(23);
1105 api->send(api->cls, &my_identity, (const char *) msg, size, 0, TIMEOUT, session, NULL, 0, GNUNET_NO, &task_send_cont, NULL);
1113 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 3: send multiple or big messages after disconnect\n\n"));
1114 /* disconnect from peer, so new connections are created */
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Disconnect from peer: `%s'\n", GNUNET_i2s(&my_identity));
1116 api->disconnect(api->cls, &my_identity);
1118 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Phase 3: sending messages\n"));
1119 /* send a multiple GNUNET_messages at a time*/
1120 size = 2 * sizeof(struct GNUNET_MessageHeader);
1121 msg = GNUNET_malloc( 2* size);
1122 msg->size = htons(size);
1123 msg->type = htons(30);
1124 struct GNUNET_MessageHeader * msg2 = &msg[2];
1125 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1126 msg2->type = htons(31);
1127 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);
1129 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1131 size = GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1132 msg = GNUNET_malloc(size);
1133 msg->size = htons(size);
1134 msg->type = htons(32);
1135 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);
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1145 * @param cls closure
1146 * @param s scheduler to use
1147 * @param c configuration to use
1151 struct GNUNET_SCHEDULER_Handle *s,
1153 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1159 unsigned long long tneigh;
1160 struct Plugin_Address * cur;
1161 const char * addr_str;
1163 unsigned int suggest_res;
1165 fail_pretty_printer = GNUNET_YES;
1166 fail_notify_address = GNUNET_YES;
1167 fail_addr_to_str = GNUNET_YES;
1168 fail_msgs_transmited_to_local_addrs = 0;
1169 fail_msg_transmited_max_size = GNUNET_YES;
1170 fail_multiple_msgs_in_transmission = 0;
1171 fail_session_selection_reliable = GNUNET_YES;
1172 fail_session_selection_reliable = GNUNET_YES;
1173 fail_session_selection_session = GNUNET_YES;
1174 fail_session_selection_session_big = GNUNET_YES;
1178 /* parse configuration */
1180 GNUNET_CONFIGURATION_get_value_number (c,
1185 GNUNET_CONFIGURATION_get_value_filename (c,
1187 "HOSTKEY", &keyfile)))
1189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1191 ("Transport service is lacking key configuration settings. Exiting.\n"));
1192 GNUNET_SCHEDULER_shutdown (s);
1198 GNUNET_CONFIGURATION_get_value_number (cfg,
1202 (port > 65535) || (port == 0))
1204 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1207 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1211 /* Get private key file from config */
1212 if (GNUNET_CONFIGURATION_have_value (cfg,
1213 "transport-https", "KEY_FILE"))
1215 GNUNET_CONFIGURATION_get_value_string (cfg,
1220 if (key_file == NULL)
1221 GNUNET_asprintf(&key_file,"https.key");
1223 if (0 == stat (key_file, &sbuf ))
1225 if (0 == remove(key_file))
1226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file);
1228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file);
1231 /* Get private key file from config */
1232 if (GNUNET_CONFIGURATION_have_value (cfg,
1233 "transport-https", "CERT_FILE"))
1235 GNUNET_CONFIGURATION_get_value_string (cfg,
1240 if (cert_file == NULL)
1241 GNUNET_asprintf(&cert_file,"https.cert");
1243 if (0 == stat (cert_file, &sbuf ))
1245 if (0 == remove(cert_file))
1246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file);
1248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file);
1251 max_connect_per_transport = (uint32_t) tneigh;
1252 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1253 GNUNET_free (keyfile);
1254 if (my_private_key == NULL)
1256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1257 _("Transport service could not access hostkey. Exiting.\n"));
1258 GNUNET_SCHEDULER_shutdown (s);
1263 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1264 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1266 /* assertions before start */
1267 GNUNET_assert ((port > 0) && (port <= 65535));
1268 GNUNET_assert(&my_public_key != NULL);
1269 GNUNET_assert(&my_identity.hashPubKey != NULL);
1271 /* load plugins... */
1272 setup_plugin_environment ();
1273 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_https");
1274 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTPS transport plugin `%s'\n"),libname);
1275 api = GNUNET_PLUGIN_load (libname, &env);
1276 GNUNET_free (libname);
1279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1280 _("Failed to load transport plugin for https\n"));
1285 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1287 /* testing plugin functionality */
1288 GNUNET_assert (0!=fail_notify_address_count);
1289 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1291 /* testing pretty printer with all addresses obtained from the plugin*/
1296 api->address_pretty_printer (api->cls, "http",cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT, &pretty_printer_cb,NULL);
1297 addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1298 suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1300 GNUNET_assert (GNUNET_OK == suggest_res);
1301 GNUNET_assert (NULL != addr_str);
1303 GNUNET_free ( (char *) addr_str);
1306 GNUNET_assert (fail_pretty_printer_count > 0);
1307 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1308 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1309 fail_pretty_printer=GNUNET_NO;
1310 fail_addr_to_str=GNUNET_NO;
1312 /* Suggesting addresses with wrong port*/
1313 struct IPv4HttpAddress failing_addr;
1314 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1315 failing_addr.u_port = htons(0);
1316 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv4HttpAddress));
1317 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1319 /* Suggesting addresses with wrong size*/
1320 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1321 failing_addr.u_port = htons(0);
1322 suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv6HttpAddress));
1323 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1325 /* Suggesting addresses with wrong address*/
1326 failing_addr.ipv4_addr = htonl(0xffc00000);
1327 failing_addr.u_port = htons(12389);
1328 suggest_res = api->check_address (api->cls,&failing_addr,100);
1329 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1331 /* test sending to client */
1332 multi_handle = curl_multi_init();
1334 /* Setting up buffers */
1335 buffer_in.size = HTTP_BUFFER_SIZE;
1339 buffer_out.size = HTTP_BUFFER_SIZE;
1343 /* Setting up connection tests */
1345 /* Test: connecting without a peer identification */
1346 test_no_ident.test_executed = GNUNET_NO;
1347 test_no_ident.test_failed = GNUNET_YES;
1349 /* Test: connecting with too short peer identification */
1350 test_too_short_ident.test_executed = GNUNET_NO;
1351 test_too_short_ident.test_failed = GNUNET_YES;
1353 /* Test: connecting with too long peer identification */
1354 test_too_long_ident.test_executed = GNUNET_NO;
1355 test_too_long_ident.test_failed = GNUNET_YES;
1357 /* Test: connecting with valid identification */
1358 test_valid_ident.test_executed = GNUNET_NO;
1359 test_valid_ident.test_failed = GNUNET_YES;
1361 test_addr = (char *) api->address_to_string (api->cls,addr_head->addr,addr_head->addrlen);
1363 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("\nPhase 0\n\n"));
1364 run_connection_tests(0, NULL);
1366 /* testing finished */
1373 * The main function for the transport service.
1375 * @param argc number of arguments from the command line
1376 * @param argv command line arguments
1377 * @return 0 ok, 1 on error
1380 main (int argc, char *const *argv)
1383 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1384 GNUNET_GETOPT_OPTION_END
1387 char *const argv_prog[] = {
1388 "test_plugin_transport_https",
1390 "test_plugin_transport_data_http.conf",
1399 GNUNET_log_setup ("test_plugin_transport_https",
1408 GNUNET_PROGRAM_run (5,
1410 "test_plugin_transport_https",
1411 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1413 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_https");
1418 /* end of test_plugin_transport_http.c */