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: connect to peer without peer identification
320 static struct HTTP_Transfer test_no_ident;
323 * Test: connect to peer without peer identification
325 static struct HTTP_Transfer test_too_short_ident;
328 * Test: connect to peer without peer identification
330 static struct HTTP_Transfer test_too_long_ident;
333 * Test: connect to peer with valid peer identification
335 static struct HTTP_Transfer test_valid_ident;
338 * Did the test pass or fail?
343 * Number of local addresses
345 static unsigned int count_str_addr;
352 static CURLM *multi_handle;
355 * The task sending data
357 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
365 struct Plugin_Address * cur;
366 struct Plugin_Address * tmp;
368 /* Evaluate results */
370 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test plugin functions failed\n");
375 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))
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test connect with wrong data failed\n");
380 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))
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test sending with plugin failed\n");
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
390 curl_multi_cleanup(multi_handle);
392 if (NULL != curl_handle)
393 curl_easy_cleanup (curl_handle);
395 /* cleaning addresses */
396 while (addr_head != NULL)
399 tmp = addr_head->next;
400 GNUNET_free (addr_head->addr);
401 GNUNET_free (addr_head);
405 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
407 GNUNET_SCHEDULER_cancel(sched,ti_send);
408 ti_send = GNUNET_SCHEDULER_NO_TASK;
411 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
413 GNUNET_SCHEDULER_cancel(sched,http_task_send);
414 http_task_send = GNUNET_SCHEDULER_NO_TASK;
417 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
419 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
420 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
423 GNUNET_free(test_addr);
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
425 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
427 GNUNET_SCHEDULER_shutdown(sched);
428 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
437 * Continuation called after plugin send message
440 * @result GNUNET_OK or GNUNET_SYSERR
443 static void task_send_cont (void *cls,
444 const struct GNUNET_PeerIdentity * target,
447 struct Plugin_Address * tmp_addr;
448 tmp_addr = addr_head;
450 if ((cls == &fail_msgs_transmited_to_local_addrs) && (result == GNUNET_OK))
452 fail_msgs_transmited_to_local_addrs++;
453 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message sent to %u addresses!\n",fail_msgs_transmited_to_local_addrs);
460 if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
463 fail_msg_transmited_bigger_max_size = GNUNET_NO;
467 if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
470 fail_msg_transmited_max_size = GNUNET_NO;
477 * Task sending recieved message back to peer
483 task_send (void *cls,
484 const struct GNUNET_SCHEDULER_TaskContext *tc)
486 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
487 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
490 if (GNUNET_YES==sent)
493 struct GNUNET_MessageHeader * msg = cls;
494 unsigned int len = ntohs(msg->size);
495 const char * msgc = (const char *) msg;
497 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
504 * Recieves messages from plugin, in real world transport
506 static struct GNUNET_TIME_Relative
508 const struct GNUNET_PeerIdentity * peer,
509 const struct GNUNET_MessageHeader * message,
511 struct Session *session,
512 const char *sender_address,
513 uint16_t sender_address_len)
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase recieved new message from peer `%s' (`%s') with type %u and length %u\n", GNUNET_i2s(peer), sender_address, ntohs(message->type), ntohs(message->size));
516 return GNUNET_TIME_UNIT_ZERO;
519 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
523 len = buffer_out.len;
525 if (( buffer_out.pos == len) || (len > (size * nmemb)))
527 memcpy(stream, buffer_out.buf, len);
528 buffer_out.pos = len;
532 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
535 if (buffer_in.pos + size * nmemb > buffer_in.size)
536 return 0; /* overflow */
538 buffer_in.len = size * nmemb;
539 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
540 buffer_in.pos += size * nmemb;
541 buffer_in.len = buffer_in.pos;
542 buffer_in.buf[buffer_in.pos] = '\0';
543 return buffer_in.pos;
546 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
548 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
550 unsigned int len = size * nmemb;
552 tmp = GNUNET_malloc ( len+1 );
554 if (tmp[len-2] == 13)
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
559 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
561 res->http_result_code=100;
563 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
565 res->http_result_code=200;
567 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
569 res->http_result_code=400;
571 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
573 res->http_result_code=404;
575 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
577 res->http_result_code=413;
584 static size_t send_prepare( struct HTTP_Transfer * result);
586 static void run_connection_tests( );
588 static void send_execute (void *cls,
589 const struct GNUNET_SCHEDULER_TaskContext *tc)
591 struct HTTP_Transfer *res;
597 res = (struct HTTP_Transfer *) cls;
598 http_task_send = GNUNET_SCHEDULER_NO_TASK;
599 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
605 mret = curl_multi_perform (multi_handle, &running);
611 msg = curl_multi_info_read (multi_handle, &running);
614 /* get session for affected curl handle */
615 //cs = find_session_by_curlhandle (msg->easy_handle);
616 //GNUNET_assert ( cs != NULL );
621 if ( (msg->data.result != CURLE_OK) &&
622 (msg->data.result != CURLE_GOT_NOTHING) )
625 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
626 _("curl failed for `%s' at %s:%d: `%s'\n"),
627 "curl_multi_perform",
630 curl_easy_strerror (msg->data.result));
631 /* sending msg failed*/
632 curl_easy_cleanup(curl_handle);
635 run_connection_tests();
637 if (res == &test_no_ident)
639 if ((res->http_result_code==404) && (buffer_in.len==208))
641 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
642 res->test_failed = GNUNET_NO;
645 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
647 if (res == &test_too_short_ident)
649 if ((res->http_result_code==404) && (buffer_in.len==208))
651 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
652 res->test_failed = GNUNET_NO;
655 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
657 if (res == &test_too_long_ident)
659 if ((res->http_result_code==404) && (buffer_in.len==208))
661 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
662 res->test_failed = GNUNET_NO;
665 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
667 if (res == &test_valid_ident)
669 if ((res->http_result_code==400))
671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
672 res->test_failed = GNUNET_NO;
675 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
677 curl_easy_cleanup(curl_handle);
680 run_connection_tests();
687 while ( (running > 0) );
690 while (mret == CURLM_CALL_MULTI_PERFORM);
695 * Function setting up file descriptors and scheduling task to run
696 * @param ses session to send data to
697 * @return bytes sent to peer
699 static size_t send_prepare( struct HTTP_Transfer * result)
705 struct GNUNET_NETWORK_FDSet *grs;
706 struct GNUNET_NETWORK_FDSet *gws;
714 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
715 if (mret != CURLM_OK)
717 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
718 _("%s failed at %s:%d: `%s'\n"),
719 "curl_multi_fdset", __FILE__, __LINE__,
720 curl_multi_strerror (mret));
723 mret = curl_multi_timeout (multi_handle, &to);
724 if (mret != CURLM_OK)
726 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
727 _("%s failed at %s:%d: `%s'\n"),
728 "curl_multi_timeout", __FILE__, __LINE__,
729 curl_multi_strerror (mret));
733 grs = GNUNET_NETWORK_fdset_create ();
734 gws = GNUNET_NETWORK_fdset_create ();
735 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
736 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
737 http_task_send = GNUNET_SCHEDULER_add_select (sched,
738 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
739 GNUNET_SCHEDULER_NO_TASK,
740 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
745 GNUNET_NETWORK_fdset_destroy (gws);
746 GNUNET_NETWORK_fdset_destroy (grs);
748 /* FIXME: return bytes REALLY sent */
753 * function to send data to server
755 static int send_data( struct HTTP_Transfer * result, char * url)
758 curl_handle = curl_easy_init();
759 if( NULL == curl_handle)
761 printf("easy_init failed \n");
762 return GNUNET_SYSERR;
765 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
767 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
768 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
769 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
770 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
771 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
772 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
773 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
774 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
775 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
776 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
777 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
779 curl_multi_add_handle(multi_handle, curl_handle);
781 send_prepare(result);
788 * Plugin notifies transport (aka testcase) about its addresses
791 notify_address (void *cls,
795 struct GNUNET_TIME_Relative expires)
797 char address[INET6_ADDRSTRLEN];
799 struct Plugin_Address * pl_addr;
800 struct Plugin_Address * cur;
802 if (addrlen == (sizeof (struct IPv4HttpAddress)))
804 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
805 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
807 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
809 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
810 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 _("Transport plugin notification for address: `%s':%u\n"),
821 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
822 pl_addr->addrlen = addrlen;
823 pl_addr->addr = GNUNET_malloc(addrlen);
824 memcpy(pl_addr->addr,addr,addrlen);
825 pl_addr->next = NULL;
827 if ( NULL == addr_head)
834 while (NULL != cur->next)
840 fail_notify_address_count++;
841 fail_notify_address = GNUNET_NO;
845 * Setup plugin environment
848 setup_plugin_environment ()
853 env.my_identity = &my_identity;
855 env.receive = &receive;
856 env.notify_address = ¬ify_address;
857 env.max_connections = max_connect_per_transport;
862 * Task shutting down testcase if it a timeout occurs
865 task_timeout (void *cls,
866 const struct GNUNET_SCHEDULER_TaskContext *tc)
868 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
869 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
878 static void pretty_printer_cb (void *cls,
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
884 fail_pretty_printer_count++;
888 * Runs every single test to test the plugin
890 static void run_connection_tests( )
892 char * host_str = NULL;
893 /* resetting buffers */
894 buffer_in.size = HTTP_BUFFER_SIZE;
898 buffer_out.size = HTTP_BUFFER_SIZE;
903 if (test_no_ident.test_executed == GNUNET_NO)
905 /* Connecting to peer without identification */
907 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
908 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
909 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
910 test_no_ident.test_executed = GNUNET_YES;
911 send_data ( &test_no_ident, host_str);
912 GNUNET_free (host_str);
915 if (test_too_short_ident.test_executed == GNUNET_NO)
917 char * ident = "AAAAAAAAAA";
918 /* Connecting to peer with too short identification */
919 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
920 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
921 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
922 test_too_short_ident.test_executed = GNUNET_YES;
923 send_data ( &test_too_short_ident, host_str);
924 GNUNET_free (host_str);
928 if (test_too_long_ident.test_executed == GNUNET_NO)
930 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
932 /* Connecting to peer with too long identification */
933 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
934 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
935 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
936 test_too_long_ident.test_executed = GNUNET_YES;
937 send_data ( &test_too_long_ident, host_str);
938 GNUNET_free (host_str);
941 if (test_valid_ident.test_executed == GNUNET_NO)
943 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
944 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
945 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen ((char *) &ident));
946 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,(char *) &ident);
948 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
949 test_valid_ident.test_executed = GNUNET_YES;
950 send_data ( &test_valid_ident, host_str);
951 GNUNET_free (host_str);
954 /* Using one of the addresses the plugin proposed */
955 GNUNET_assert (addr_head->addr != NULL);
957 struct Plugin_Address * tmp_addr;
958 struct GNUNET_MessageHeader msg;
959 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
960 char address[INET6_ADDRSTRLEN];
962 unsigned int type = 10;
964 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
965 tmp_addr = addr_head;
966 /* send a message to all addresses advertised by plugin */
969 while (tmp_addr != NULL)
971 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
973 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
974 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
977 else if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
979 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
980 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
988 msg.type=htons(type);
989 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
990 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);
991 tmp_addr = tmp_addr->next;
996 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE )*/
998 tmp = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE);
999 uint16_t t2 = (uint16_t)GNUNET_SERVER_MAX_MESSAGE_SIZE;
1000 msg.size = htons(t2);
1001 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1002 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);
1005 /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1 */
1007 tmp = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE-1);
1008 uint16_t t = (uint16_t)GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1009 msg.size = htons(t);
1010 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1011 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);
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1020 * @param cls closure
1021 * @param s scheduler to use
1022 * @param c configuration to use
1026 struct GNUNET_SCHEDULER_Handle *s,
1028 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1034 unsigned long long tneigh;
1035 struct Plugin_Address * cur;
1036 const char * addr_str;
1039 unsigned int suggest_res;
1041 fail_pretty_printer = GNUNET_YES;
1042 fail_notify_address = GNUNET_YES;
1043 fail_addr_to_str = GNUNET_YES;
1044 fail_msgs_transmited_to_local_addrs = 0;
1045 fail_msg_transmited_max_size = GNUNET_YES;
1049 /* parse configuration */
1051 GNUNET_CONFIGURATION_get_value_number (c,
1056 GNUNET_CONFIGURATION_get_value_filename (c,
1058 "HOSTKEY", &keyfile)))
1060 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1062 ("Transport service is lacking key configuration settings. Exiting.\n"));
1063 GNUNET_SCHEDULER_shutdown (s);
1069 GNUNET_CONFIGURATION_get_value_number (cfg,
1073 (port > 65535) || (port == 0))
1075 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1078 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1082 max_connect_per_transport = (uint32_t) tneigh;
1083 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1084 GNUNET_free (keyfile);
1085 if (my_private_key == NULL)
1087 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1088 _("Transport service could not access hostkey. Exiting.\n"));
1089 GNUNET_SCHEDULER_shutdown (s);
1094 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1095 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1097 /* assertions before start */
1098 GNUNET_assert ((port > 0) && (port <= 65535));
1099 GNUNET_assert(&my_public_key != NULL);
1100 GNUNET_assert(&my_identity.hashPubKey != NULL);
1102 /* load plugins... */
1103 setup_plugin_environment ();
1104 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1105 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1106 api = GNUNET_PLUGIN_load (libname, &env);
1107 GNUNET_free (libname);
1110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1111 _("Failed to load transport plugin for http\n"));
1116 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1118 /* testing plugin functionality */
1119 GNUNET_assert (0!=fail_notify_address_count);
1120 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1122 /* testing pretty printer with all addresses obtained from the plugin*/
1127 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
1128 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
1129 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
1131 GNUNET_assert (GNUNET_OK == suggest_res);
1132 GNUNET_assert (NULL != addr_str);
1134 GNUNET_free ( (char *) addr_str);
1137 GNUNET_assert (fail_pretty_printer_count > 0);
1138 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1139 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1140 fail_pretty_printer=GNUNET_NO;
1141 fail_addr_to_str=GNUNET_NO;
1143 /* Suggesting addresses with wrong port*/
1144 struct IPv4HttpAddress failing_addr;
1145 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1146 failing_addr.u_port = htons(0);
1147 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1148 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1150 /* Suggesting addresses with wrong size*/
1151 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1152 failing_addr.u_port = htons(0);
1153 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
1154 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1156 /* Suggesting addresses with wrong address*/
1157 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1158 failing_addr.u_port = htons(12389);
1159 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1160 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1162 /* test sending to client */
1163 multi_handle = curl_multi_init();
1165 /* Setting up buffers */
1166 buffer_in.size = HTTP_BUFFER_SIZE;
1170 buffer_out.size = HTTP_BUFFER_SIZE;
1174 /* Setting up connection tests */
1176 /* Test: connecting without a peer identification */
1177 test_no_ident.test_executed = GNUNET_NO;
1178 test_no_ident.test_failed = GNUNET_YES;
1180 /* Test: connecting with too short peer identification */
1181 test_too_short_ident.test_executed = GNUNET_NO;
1182 test_too_short_ident.test_failed = GNUNET_YES;
1184 /* Test: connecting with too long peer identification */
1185 test_too_long_ident.test_executed = GNUNET_NO;
1186 test_too_long_ident.test_failed = GNUNET_YES;
1188 /* Test: connecting with valid identification */
1189 test_valid_ident.test_executed = GNUNET_NO;
1190 test_valid_ident.test_failed = GNUNET_YES;
1192 test_addr = (char *) api->address_to_string (NULL,addr_head->addr,addr_head->addrlen);
1193 run_connection_tests();
1195 /* testing finished */
1202 * The main function for the transport service.
1204 * @param argc number of arguments from the command line
1205 * @param argv command line arguments
1206 * @return 0 ok, 1 on error
1209 main (int argc, char *const *argv)
1212 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1213 GNUNET_GETOPT_OPTION_END
1216 char *const argv_prog[] = {
1217 "test_plugin_transport_http",
1219 "test_plugin_transport_data_http.conf",
1228 GNUNET_log_setup ("test_plugin_transport_http",
1237 GNUNET_PROGRAM_run (5,
1239 "test_plugin_transport_http",
1240 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1242 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1247 /* end of test_plugin_transport_http.c */