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 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file transport/test_plugin_transport_http.c
22 * @brief testcase for plugin_transport_http.c
23 * @author Matthias Wachs
27 #include "gnunet_constants.h"
28 #include "gnunet_common.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_plugin_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_program_lib.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_service_lib.h"
38 #include "gnunet_crypto_lib.h"
40 #include "plugin_transport.h"
41 #include "gnunet_statistics_service.h"
42 #include "transport.h"
43 #include <curl/curl.h>
45 #define VERBOSE GNUNET_YES
46 #define DEBUG GNUNET_NO
47 #define DEBUG_CURL GNUNET_NO
48 #define HTTP_BUFFER_SIZE 2048
50 #define PLUGIN libgnunet_plugin_transport_template
53 * How long until we give up on transmitting the message?
55 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
58 * How long until we give up on transmitting the message?
60 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
63 * How long between recieve and send?
65 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
70 * Struct for plugin addresses
75 * Next field for linked list
77 struct Plugin_Address * next;
80 * buffer containing data to send
85 * amount of data to sent
91 * Message to send using http
98 unsigned char buf[HTTP_BUFFER_SIZE];
101 * current position in buffer
118 * Struct for plugin addresses
123 * amount of bytes we recieved
128 * buffer for http transfers
130 unsigned char buf[HTTP_BUFFER_SIZE];
133 * buffer size this transfer
138 * amount of bytes we recieved
143 * HTTP Header result for transfer
145 unsigned int http_result_code;
150 unsigned int test_failed;
153 * was this test already executed?
155 unsigned int test_executed;
160 * Network format for IPv4 addresses.
162 struct IPv4HttpAddress
165 * IPv4 address, in network byte order.
170 * Port number, in network byte order.
178 * Network format for IPv6 addresses.
180 struct IPv6HttpAddress
185 struct in6_addr ipv6_addr;
188 * Port number, in network byte order.
198 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
203 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
208 static struct GNUNET_PeerIdentity my_identity;
213 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
218 static long long unsigned int port;
223 static char * test_addr;
228 struct GNUNET_SCHEDULER_Handle *sched;
231 * Our statistics handle.
233 struct GNUNET_STATISTICS_Handle *stats;
239 const struct GNUNET_CONFIGURATION_Handle *cfg;
242 * Number of neighbours we'd like to have.
244 static uint32_t max_connect_per_transport;
247 * Environment for this plugin.
249 static struct GNUNET_TRANSPORT_PluginEnvironment env;
252 *handle for the api provided by this plugin
254 static struct GNUNET_TRANSPORT_PluginFunctions *api;
257 * ID of the task controlling the testcase timeout
259 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
261 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
263 //const struct GNUNET_PeerIdentity * p;
266 * buffer for data to send
268 static struct HTTP_Message buffer_out;
271 * buffer for data to recieve
273 static struct HTTP_Message buffer_in;
276 struct Plugin_Address * addr_head;
279 * Did the test pass or fail?
281 static int fail_notify_address;
283 * Did the test pass or fail?
285 static int fail_notify_address_count;
288 * Did the test pass or fail?
290 static int fail_pretty_printer;
293 * Did the test pass or fail?
295 static int fail_pretty_printer_count;
298 * Did the test pass or fail?
300 static int fail_addr_to_str;
303 * No. of msgs transmitted successfully to local addresses
305 static int fail_msgs_transmited_to_local_addrs;
308 * Test: transmit msg of max. size
310 static int fail_msg_transmited_bigger_max_size;
313 * Test: transmit msg of max. size
315 static int fail_msg_transmited_max_size;
318 * Test: transmit 2 msgs. in in send operation
320 static int fail_multiple_msgs_in_transmission;
323 * Test: connect to peer without peer identification
325 static struct HTTP_Transfer test_no_ident;
328 * Test: connect to peer without peer identification
330 static struct HTTP_Transfer test_too_short_ident;
333 * Test: connect to peer without peer identification
335 static struct HTTP_Transfer test_too_long_ident;
338 * Test: connect to peer with valid peer identification
340 static struct HTTP_Transfer test_valid_ident;
343 * Did the test pass or fail?
348 * Number of local addresses
350 static unsigned int count_str_addr;
357 static CURLM *multi_handle;
360 * The task sending data
362 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
370 struct Plugin_Address * cur;
371 struct Plugin_Address * tmp;
373 /* Evaluate results */
375 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test plugin functions failed\n");
380 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))
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test connect with wrong data failed\n");
385 if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_msg_transmited_max_size == GNUNET_YES) || (fail_msg_transmited_bigger_max_size == GNUNET_YES) || (fail_multiple_msgs_in_transmission != GNUNET_NO))
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test sending with plugin failed\n");
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
395 curl_multi_cleanup(multi_handle);
397 if (NULL != curl_handle)
398 curl_easy_cleanup (curl_handle);
400 /* cleaning addresses */
401 while (addr_head != NULL)
404 tmp = addr_head->next;
405 GNUNET_free (addr_head->addr);
406 GNUNET_free (addr_head);
410 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
412 GNUNET_SCHEDULER_cancel(sched,ti_send);
413 ti_send = GNUNET_SCHEDULER_NO_TASK;
416 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
418 GNUNET_SCHEDULER_cancel(sched,http_task_send);
419 http_task_send = GNUNET_SCHEDULER_NO_TASK;
422 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
424 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
425 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
428 GNUNET_free(test_addr);
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
430 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
432 GNUNET_SCHEDULER_shutdown(sched);
433 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
442 * Continuation called after plugin send message
445 * @result GNUNET_OK or GNUNET_SYSERR
448 static void task_send_cont (void *cls,
449 const struct GNUNET_PeerIdentity * target,
452 struct Plugin_Address * tmp_addr;
453 tmp_addr = addr_head;
455 if ((cls == &fail_msgs_transmited_to_local_addrs) && (result == GNUNET_OK))
457 fail_msgs_transmited_to_local_addrs++;
458 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message sent to %u addresses!\n",fail_msgs_transmited_to_local_addrs);
465 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
468 fail_msg_transmited_bigger_max_size = GNUNET_NO;
472 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
475 fail_msg_transmited_max_size = GNUNET_NO;
482 * Task sending recieved message back to peer
488 task_send (void *cls,
489 const struct GNUNET_SCHEDULER_TaskContext *tc)
491 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
492 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
495 if (GNUNET_YES==sent)
498 struct GNUNET_MessageHeader * msg = cls;
499 unsigned int len = ntohs(msg->size);
500 const char * msgc = (const char *) msg;
502 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
509 * Recieves messages from plugin, in real world transport
511 static struct GNUNET_TIME_Relative
513 const struct GNUNET_PeerIdentity * peer,
514 const struct GNUNET_MessageHeader * message,
516 struct Session *session,
517 const char *sender_address,
518 uint16_t sender_address_len)
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase recieved new message from peer `%s' with type %u and length %u\n", GNUNET_i2s(peer), ntohs(message->type), ntohs(message->size));
521 if ((ntohs(message->type) == 40) && (fail_multiple_msgs_in_transmission == 1))
522 fail_multiple_msgs_in_transmission++;
523 if ((ntohs(message->type) == 41) && (fail_multiple_msgs_in_transmission == 2))
524 fail_multiple_msgs_in_transmission = GNUNET_NO;
525 return GNUNET_TIME_UNIT_ZERO;
528 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
532 len = buffer_out.len;
534 if (( buffer_out.pos == len) || (len > (size * nmemb)))
536 memcpy(stream, buffer_out.buf, len);
537 buffer_out.pos = len;
541 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
544 if (buffer_in.pos + size * nmemb > buffer_in.size)
545 return 0; /* overflow */
547 buffer_in.len = size * nmemb;
548 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
549 buffer_in.pos += size * nmemb;
550 buffer_in.len = buffer_in.pos;
551 buffer_in.buf[buffer_in.pos] = '\0';
552 return buffer_in.pos;
555 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
557 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
559 unsigned int len = size * nmemb;
561 tmp = GNUNET_malloc ( len+1 );
563 if (tmp[len-2] == 13)
566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
568 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
570 res->http_result_code=100;
572 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
574 res->http_result_code=200;
576 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
578 res->http_result_code=400;
580 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
582 res->http_result_code=404;
584 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
586 res->http_result_code=413;
593 static size_t send_prepare( struct HTTP_Transfer * result);
595 static void run_connection_tests( );
597 static void send_execute (void *cls,
598 const struct GNUNET_SCHEDULER_TaskContext *tc)
600 struct HTTP_Transfer *res;
606 res = (struct HTTP_Transfer *) cls;
607 http_task_send = GNUNET_SCHEDULER_NO_TASK;
608 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
614 mret = curl_multi_perform (multi_handle, &running);
620 msg = curl_multi_info_read (multi_handle, &running);
623 /* get session for affected curl handle */
624 //cs = find_session_by_curlhandle (msg->easy_handle);
625 //GNUNET_assert ( cs != NULL );
630 if ( (msg->data.result != CURLE_OK) &&
631 (msg->data.result != CURLE_GOT_NOTHING) )
634 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
635 _("curl failed for `%s' at %s:%d: `%s'\n"),
636 "curl_multi_perform",
639 curl_easy_strerror (msg->data.result));
640 /* sending msg failed*/
641 curl_easy_cleanup(curl_handle);
644 run_connection_tests();
646 if (res == &test_no_ident)
648 if ((res->http_result_code==404) && (buffer_in.len==208))
650 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
651 res->test_failed = GNUNET_NO;
654 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
656 if (res == &test_too_short_ident)
658 if ((res->http_result_code==404) && (buffer_in.len==208))
660 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
661 res->test_failed = GNUNET_NO;
664 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
666 if (res == &test_too_long_ident)
668 if ((res->http_result_code==404) && (buffer_in.len==208))
670 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
671 res->test_failed = GNUNET_NO;
674 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
676 if (res == &test_valid_ident)
678 if ((res->http_result_code==400))
680 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
681 res->test_failed = GNUNET_NO;
684 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
686 curl_easy_cleanup(curl_handle);
689 run_connection_tests();
696 while ( (running > 0) );
699 while (mret == CURLM_CALL_MULTI_PERFORM);
704 * Function setting up file descriptors and scheduling task to run
705 * @param ses session to send data to
706 * @return bytes sent to peer
708 static size_t send_prepare( struct HTTP_Transfer * result)
714 struct GNUNET_NETWORK_FDSet *grs;
715 struct GNUNET_NETWORK_FDSet *gws;
723 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
724 if (mret != CURLM_OK)
726 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
727 _("%s failed at %s:%d: `%s'\n"),
728 "curl_multi_fdset", __FILE__, __LINE__,
729 curl_multi_strerror (mret));
732 mret = curl_multi_timeout (multi_handle, &to);
733 if (mret != CURLM_OK)
735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
736 _("%s failed at %s:%d: `%s'\n"),
737 "curl_multi_timeout", __FILE__, __LINE__,
738 curl_multi_strerror (mret));
742 grs = GNUNET_NETWORK_fdset_create ();
743 gws = GNUNET_NETWORK_fdset_create ();
744 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
745 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
746 http_task_send = GNUNET_SCHEDULER_add_select (sched,
747 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
748 GNUNET_SCHEDULER_NO_TASK,
749 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
754 GNUNET_NETWORK_fdset_destroy (gws);
755 GNUNET_NETWORK_fdset_destroy (grs);
757 /* FIXME: return bytes REALLY sent */
762 * function to send data to server
764 static int send_data( struct HTTP_Transfer * result, char * url)
767 curl_handle = curl_easy_init();
768 if( NULL == curl_handle)
770 printf("easy_init failed \n");
771 return GNUNET_SYSERR;
774 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
776 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
777 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
778 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
779 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
780 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
781 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
782 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
783 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
784 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
785 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
786 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
788 curl_multi_add_handle(multi_handle, curl_handle);
790 send_prepare(result);
797 * Plugin notifies transport (aka testcase) about its addresses
800 notify_address (void *cls,
804 struct GNUNET_TIME_Relative expires)
806 char address[INET6_ADDRSTRLEN];
808 struct Plugin_Address * pl_addr;
809 struct Plugin_Address * cur;
811 if (addrlen == (sizeof (struct IPv4HttpAddress)))
813 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
814 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
816 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
818 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
819 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
827 _("Transport plugin notification for address: `%s':%u\n"),
830 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
831 pl_addr->addrlen = addrlen;
832 pl_addr->addr = GNUNET_malloc(addrlen);
833 memcpy(pl_addr->addr,addr,addrlen);
834 pl_addr->next = NULL;
836 if ( NULL == addr_head)
843 while (NULL != cur->next)
849 fail_notify_address_count++;
850 fail_notify_address = GNUNET_NO;
854 * Setup plugin environment
857 setup_plugin_environment ()
862 env.my_identity = &my_identity;
864 env.receive = &receive;
865 env.notify_address = ¬ify_address;
866 env.max_connections = max_connect_per_transport;
871 * Task shutting down testcase if it a timeout occurs
874 task_timeout (void *cls,
875 const struct GNUNET_SCHEDULER_TaskContext *tc)
877 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
878 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
887 static void pretty_printer_cb (void *cls,
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
893 fail_pretty_printer_count++;
897 * Runs every single test to test the plugin
899 static void run_connection_tests( )
901 char * host_str = NULL;
902 /* resetting buffers */
903 buffer_in.size = HTTP_BUFFER_SIZE;
907 buffer_out.size = HTTP_BUFFER_SIZE;
912 if (test_no_ident.test_executed == GNUNET_NO)
914 /* Connecting to peer without identification */
916 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
917 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
918 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
919 test_no_ident.test_executed = GNUNET_YES;
920 send_data ( &test_no_ident, host_str);
921 GNUNET_free (host_str);
924 if (test_too_short_ident.test_executed == GNUNET_NO)
926 char * ident = "AAAAAAAAAA";
927 /* Connecting to peer with too short identification */
928 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
929 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
930 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
931 test_too_short_ident.test_executed = GNUNET_YES;
932 send_data ( &test_too_short_ident, host_str);
933 GNUNET_free (host_str);
937 if (test_too_long_ident.test_executed == GNUNET_NO)
939 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
941 /* Connecting to peer with too long identification */
942 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
943 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
944 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
945 test_too_long_ident.test_executed = GNUNET_YES;
946 send_data ( &test_too_long_ident, host_str);
947 GNUNET_free (host_str);
950 if (test_valid_ident.test_executed == GNUNET_NO)
952 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
953 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
954 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen ((char *) &ident));
955 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,(char *) &ident);
957 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
958 test_valid_ident.test_executed = GNUNET_YES;
959 send_data ( &test_valid_ident, host_str);
960 GNUNET_free (host_str);
963 /* Using one of the addresses the plugin proposed */
964 GNUNET_assert (addr_head->addr != NULL);
966 struct Plugin_Address * tmp_addr;
967 struct GNUNET_MessageHeader msg;
968 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
969 char address[INET6_ADDRSTRLEN];
971 unsigned int type = 10;
973 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
974 tmp_addr = addr_head;
975 /* send a message to all addresses advertised by plugin */
978 while (tmp_addr != NULL)
980 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
982 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
983 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
986 else if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
988 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
989 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
997 msg.type=htons(type);
998 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
999 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);
1000 tmp_addr = tmp_addr->next;
1005 /* send a multiple GNUNET_messages at a time*/
1007 tmp = GNUNET_malloc(4 * sizeof(struct GNUNET_MessageHeader));
1008 struct GNUNET_MessageHeader * msg1 = (struct GNUNET_MessageHeader *) tmp;
1009 msg1->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1010 msg1->type = htons(40);
1011 struct GNUNET_MessageHeader * msg2 = &msg1[2];
1012 msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1013 msg2->type = htons(41);
1014 api->send(api->cls, &my_identity, tmp, 4 * sizeof(struct GNUNET_MessageHeader), 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_YES, &task_send_cont, &fail_multiple_msgs_in_transmission);
1017 /* send a multiple GNUNET_messages at a time, second message has incorrect size*/
1019 tmp = GNUNET_malloc(4 * sizeof(struct GNUNET_MessageHeader));
1020 msg1 = (struct GNUNET_MessageHeader *) tmp;
1021 msg1->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1022 msg1->type = htons(40);
1024 msg2->size = htons(3 * sizeof(struct GNUNET_MessageHeader));
1025 msg2->type = htons(41);
1026 api->send(api->cls, &my_identity, tmp, 4 * sizeof(struct GNUNET_MessageHeader), 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_YES, &task_send_cont, NULL);
1029 /* send a multiple GNUNET_messages at a time, second message has incorrect size*/
1030 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Ping Hello Message\n");
1032 tmp = GNUNET_malloc(425);
1033 msg1 = (struct GNUNET_MessageHeader *) tmp;
1034 msg1->size = htons(353);
1035 msg1->type = htons(16);
1037 msg2->size = htons(72);
1038 msg2->type = htons(32);
1039 api->send(api->cls, &my_identity, tmp, 425, 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_YES, &task_send_cont, NULL);*/
1042 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE )*/
1044 tmp = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE);
1045 uint16_t t2 = (uint16_t)GNUNET_SERVER_MAX_MESSAGE_SIZE;
1046 msg.size = htons(t2);
1047 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1048 api->send(api->cls, &my_identity, tmp, GNUNET_SERVER_MAX_MESSAGE_SIZE, 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_YES, &task_send_cont, &fail_msg_transmited_bigger_max_size);
1050 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1052 tmp = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE-1);
1053 uint16_t t = (uint16_t)GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1054 msg.size = htons(t);
1055 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1056 api->send(api->cls, &my_identity, tmp, GNUNET_SERVER_MAX_MESSAGE_SIZE-1, 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_YES, &task_send_cont, &fail_msg_transmited_max_size);
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1065 * @param cls closure
1066 * @param s scheduler to use
1067 * @param c configuration to use
1071 struct GNUNET_SCHEDULER_Handle *s,
1073 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1079 unsigned long long tneigh;
1080 struct Plugin_Address * cur;
1081 const char * addr_str;
1084 unsigned int suggest_res;
1086 fail_pretty_printer = GNUNET_YES;
1087 fail_notify_address = GNUNET_YES;
1088 fail_addr_to_str = GNUNET_YES;
1089 fail_msgs_transmited_to_local_addrs = 0;
1090 fail_msg_transmited_max_size = GNUNET_YES;
1091 fail_multiple_msgs_in_transmission = GNUNET_YES;
1095 /* parse configuration */
1097 GNUNET_CONFIGURATION_get_value_number (c,
1102 GNUNET_CONFIGURATION_get_value_filename (c,
1104 "HOSTKEY", &keyfile)))
1106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1108 ("Transport service is lacking key configuration settings. Exiting.\n"));
1109 GNUNET_SCHEDULER_shutdown (s);
1115 GNUNET_CONFIGURATION_get_value_number (cfg,
1119 (port > 65535) || (port == 0))
1121 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1124 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1128 max_connect_per_transport = (uint32_t) tneigh;
1129 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1130 GNUNET_free (keyfile);
1131 if (my_private_key == NULL)
1133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1134 _("Transport service could not access hostkey. Exiting.\n"));
1135 GNUNET_SCHEDULER_shutdown (s);
1140 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1141 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1143 /* assertions before start */
1144 GNUNET_assert ((port > 0) && (port <= 65535));
1145 GNUNET_assert(&my_public_key != NULL);
1146 GNUNET_assert(&my_identity.hashPubKey != NULL);
1148 /* load plugins... */
1149 setup_plugin_environment ();
1150 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1151 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1152 api = GNUNET_PLUGIN_load (libname, &env);
1153 GNUNET_free (libname);
1156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1157 _("Failed to load transport plugin for http\n"));
1162 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1164 /* testing plugin functionality */
1165 GNUNET_assert (0!=fail_notify_address_count);
1166 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1168 /* testing pretty printer with all addresses obtained from the plugin*/
1173 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
1174 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
1175 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
1177 GNUNET_assert (GNUNET_OK == suggest_res);
1178 GNUNET_assert (NULL != addr_str);
1180 GNUNET_free ( (char *) addr_str);
1183 GNUNET_assert (fail_pretty_printer_count > 0);
1184 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1185 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1186 fail_pretty_printer=GNUNET_NO;
1187 fail_addr_to_str=GNUNET_NO;
1189 /* Suggesting addresses with wrong port*/
1190 struct IPv4HttpAddress failing_addr;
1191 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1192 failing_addr.u_port = htons(0);
1193 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1194 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1196 /* Suggesting addresses with wrong size*/
1197 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1198 failing_addr.u_port = htons(0);
1199 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
1200 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1202 /* Suggesting addresses with wrong address*/
1203 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1204 failing_addr.u_port = htons(12389);
1205 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1206 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1208 /* test sending to client */
1209 multi_handle = curl_multi_init();
1211 /* Setting up buffers */
1212 buffer_in.size = HTTP_BUFFER_SIZE;
1216 buffer_out.size = HTTP_BUFFER_SIZE;
1220 /* Setting up connection tests */
1222 /* Test: connecting without a peer identification */
1223 test_no_ident.test_executed = GNUNET_NO;
1224 test_no_ident.test_failed = GNUNET_YES;
1226 /* Test: connecting with too short peer identification */
1227 test_too_short_ident.test_executed = GNUNET_NO;
1228 test_too_short_ident.test_failed = GNUNET_YES;
1230 /* Test: connecting with too long peer identification */
1231 test_too_long_ident.test_executed = GNUNET_NO;
1232 test_too_long_ident.test_failed = GNUNET_YES;
1234 /* Test: connecting with valid identification */
1235 test_valid_ident.test_executed = GNUNET_NO;
1236 test_valid_ident.test_failed = GNUNET_YES;
1238 test_addr = (char *) api->address_to_string (NULL,addr_head->addr,addr_head->addrlen);
1239 run_connection_tests();
1241 /* testing finished */
1248 * The main function for the transport service.
1250 * @param argc number of arguments from the command line
1251 * @param argv command line arguments
1252 * @return 0 ok, 1 on error
1255 main (int argc, char *const *argv)
1258 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1259 GNUNET_GETOPT_OPTION_END
1262 char *const argv_prog[] = {
1263 "test_plugin_transport_http",
1265 "test_plugin_transport_data_http.conf",
1274 GNUNET_log_setup ("test_plugin_transport_http",
1283 GNUNET_PROGRAM_run (5,
1285 "test_plugin_transport_http",
1286 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1288 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1293 /* end of test_plugin_transport_http.c */