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_NO
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: connect to peer without peer identification
310 static struct HTTP_Transfer test_no_ident;
313 * Test: connect to peer without peer identification
315 static struct HTTP_Transfer test_too_short_ident;
318 * Test: connect to peer without peer identification
320 static struct HTTP_Transfer test_too_long_ident;
323 * Test: connect to peer with valid peer identification
325 static struct HTTP_Transfer test_valid_ident;
328 * Did the test pass or fail?
333 * Number of local addresses
335 static unsigned int count_str_addr;
342 static CURLM *multi_handle;
345 * The task sending data
347 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
355 struct Plugin_Address * cur;
356 struct Plugin_Address * tmp;
358 /* Evaluate results */
360 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test plugin functions failed\n");
365 if ((test_no_ident.test_failed == GNUNET_YES) || (test_too_short_ident.test_failed == GNUNET_YES) || (test_too_long_ident.test_failed == GNUNET_YES))
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test connect with wrong data failed\n");
370 if ((test_valid_ident.test_failed == GNUNET_YES) || (fail_msgs_transmited_to_local_addrs != count_str_addr))
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test sending with plugin failed\n");
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
380 curl_multi_cleanup(multi_handle);
382 if (NULL != curl_handle)
383 curl_easy_cleanup (curl_handle);
385 /* cleaning addresses */
386 while (addr_head != NULL)
389 tmp = addr_head->next;
390 GNUNET_free (addr_head->addr);
391 GNUNET_free (addr_head);
395 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
397 GNUNET_SCHEDULER_cancel(sched,ti_send);
398 ti_send = GNUNET_SCHEDULER_NO_TASK;
401 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
403 GNUNET_SCHEDULER_cancel(sched,http_task_send);
404 http_task_send = GNUNET_SCHEDULER_NO_TASK;
407 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
409 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
410 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
413 GNUNET_free(test_addr);
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
415 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
417 GNUNET_SCHEDULER_shutdown(sched);
418 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
427 * Continuation called after plugin send message
430 * @result GNUNET_OK or GNUNET_SYSERR
433 static void task_send_cont (void *cls,
434 const struct GNUNET_PeerIdentity * target,
437 struct Plugin_Address * tmp_addr;
438 tmp_addr = addr_head;
440 while (tmp_addr != NULL)
443 if (result == GNUNET_OK) fail_msgs_transmited_to_local_addrs++;
444 tmp_addr = tmp_addr->next;
447 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message sent to %u addresses!\n",fail_msgs_transmited_to_local_addrs);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
461 * Task sending recieved message back to peer
467 task_send (void *cls,
468 const struct GNUNET_SCHEDULER_TaskContext *tc)
470 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
471 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
474 if (GNUNET_YES==sent)
477 struct GNUNET_MessageHeader * msg = cls;
478 unsigned int len = ntohs(msg->size);
479 const char * msgc = (const char *) msg;
481 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
488 * Recieves messages from plugin, in real world transport
490 static struct GNUNET_TIME_Relative
492 const struct GNUNET_PeerIdentity * peer,
493 const struct GNUNET_MessageHeader * message,
495 struct Session *session,
496 const char *sender_address,
497 uint16_t sender_address_len)
499 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));
500 return GNUNET_TIME_UNIT_ZERO;
503 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
507 len = buffer_out.len;
509 if (( buffer_out.pos == len) || (len > (size * nmemb)))
511 memcpy(stream, buffer_out.buf, len);
512 buffer_out.pos = len;
516 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
519 if (buffer_in.pos + size * nmemb > buffer_in.size)
520 return 0; /* overflow */
522 buffer_in.len = size * nmemb;
523 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
524 buffer_in.pos += size * nmemb;
525 buffer_in.len = buffer_in.pos;
526 buffer_in.buf[buffer_in.pos] = '\0';
527 return buffer_in.pos;
530 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
532 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
534 unsigned int len = size * nmemb;
536 tmp = GNUNET_malloc ( len+1 );
538 if (tmp[len-2] == 13)
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
543 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
545 res->http_result_code=100;
547 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
549 res->http_result_code=200;
551 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
553 res->http_result_code=400;
555 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
557 res->http_result_code=404;
559 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
561 res->http_result_code=413;
568 static size_t send_prepare( struct HTTP_Transfer * result);
570 static void run_connection_tests( );
572 static void send_execute (void *cls,
573 const struct GNUNET_SCHEDULER_TaskContext *tc)
575 struct HTTP_Transfer *res;
581 res = (struct HTTP_Transfer *) cls;
582 http_task_send = GNUNET_SCHEDULER_NO_TASK;
583 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
589 mret = curl_multi_perform (multi_handle, &running);
595 msg = curl_multi_info_read (multi_handle, &running);
598 /* get session for affected curl handle */
599 //cs = find_session_by_curlhandle (msg->easy_handle);
600 //GNUNET_assert ( cs != NULL );
605 if ( (msg->data.result != CURLE_OK) &&
606 (msg->data.result != CURLE_GOT_NOTHING) )
609 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
610 _("curl failed for `%s' at %s:%d: `%s'\n"),
611 "curl_multi_perform",
614 curl_easy_strerror (msg->data.result));
615 /* sending msg failed*/
616 curl_easy_cleanup(curl_handle);
619 run_connection_tests();
621 if (res == &test_no_ident)
623 if ((res->http_result_code==404) && (buffer_in.len==208))
625 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
626 res->test_failed = GNUNET_NO;
629 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
631 if (res == &test_too_short_ident)
633 if ((res->http_result_code==404) && (buffer_in.len==208))
635 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
636 res->test_failed = GNUNET_NO;
639 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
641 if (res == &test_too_long_ident)
643 if ((res->http_result_code==404) && (buffer_in.len==208))
645 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
646 res->test_failed = GNUNET_NO;
649 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
651 if (res == &test_valid_ident)
653 if ((res->http_result_code==400))
655 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
656 res->test_failed = GNUNET_NO;
659 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
661 curl_easy_cleanup(curl_handle);
664 run_connection_tests();
671 while ( (running > 0) );
674 while (mret == CURLM_CALL_MULTI_PERFORM);
679 * Function setting up file descriptors and scheduling task to run
680 * @param ses session to send data to
681 * @return bytes sent to peer
683 static size_t send_prepare( struct HTTP_Transfer * result)
689 struct GNUNET_NETWORK_FDSet *grs;
690 struct GNUNET_NETWORK_FDSet *gws;
698 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
699 if (mret != CURLM_OK)
701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
702 _("%s failed at %s:%d: `%s'\n"),
703 "curl_multi_fdset", __FILE__, __LINE__,
704 curl_multi_strerror (mret));
707 mret = curl_multi_timeout (multi_handle, &to);
708 if (mret != CURLM_OK)
710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
711 _("%s failed at %s:%d: `%s'\n"),
712 "curl_multi_timeout", __FILE__, __LINE__,
713 curl_multi_strerror (mret));
717 grs = GNUNET_NETWORK_fdset_create ();
718 gws = GNUNET_NETWORK_fdset_create ();
719 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
720 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
721 http_task_send = GNUNET_SCHEDULER_add_select (sched,
722 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
723 GNUNET_SCHEDULER_NO_TASK,
724 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
729 GNUNET_NETWORK_fdset_destroy (gws);
730 GNUNET_NETWORK_fdset_destroy (grs);
732 /* FIXME: return bytes REALLY sent */
737 * function to send data to server
739 static int send_data( struct HTTP_Transfer * result, char * url)
742 curl_handle = curl_easy_init();
743 if( NULL == curl_handle)
745 printf("easy_init failed \n");
746 return GNUNET_SYSERR;
749 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
751 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
752 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
753 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
754 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
755 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
756 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
757 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
758 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
759 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
760 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
761 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
763 curl_multi_add_handle(multi_handle, curl_handle);
765 send_prepare(result);
772 * Plugin notifies transport (aka testcase) about its addresses
775 notify_address (void *cls,
779 struct GNUNET_TIME_Relative expires)
781 char address[INET6_ADDRSTRLEN];
783 struct Plugin_Address * pl_addr;
784 struct Plugin_Address * cur;
786 if (addrlen == (sizeof (struct IPv4HttpAddress)))
788 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
789 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
791 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
793 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
794 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 _("Transport plugin notification for address: `%s':%u\n"),
805 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
806 pl_addr->addrlen = addrlen;
807 pl_addr->addr = GNUNET_malloc(addrlen);
808 memcpy(pl_addr->addr,addr,addrlen);
809 pl_addr->next = NULL;
811 if ( NULL == addr_head)
818 while (NULL != cur->next)
824 fail_notify_address_count++;
825 fail_notify_address = GNUNET_NO;
829 * Setup plugin environment
832 setup_plugin_environment ()
837 env.my_identity = &my_identity;
839 env.receive = &receive;
840 env.notify_address = ¬ify_address;
841 env.max_connections = max_connect_per_transport;
846 * Task shutting down testcase if it a timeout occurs
849 task_timeout (void *cls,
850 const struct GNUNET_SCHEDULER_TaskContext *tc)
852 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
853 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
862 static void pretty_printer_cb (void *cls,
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
868 fail_pretty_printer_count++;
872 * Runs every single test to test the plugin
874 static void run_connection_tests( )
876 char * host_str = NULL;
877 GNUNET_log (GNUNET_ERROR_TYPE_INFO,"Addr: %s\n",test_addr);
878 /* resetting buffers */
879 buffer_in.size = HTTP_BUFFER_SIZE;
883 buffer_out.size = HTTP_BUFFER_SIZE;
888 if (test_no_ident.test_executed == GNUNET_NO)
890 /* Connecting to peer without identification */
892 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
893 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
894 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
895 test_no_ident.test_executed = GNUNET_YES;
896 send_data ( &test_no_ident, host_str);
897 GNUNET_free (host_str);
900 if (test_too_short_ident.test_executed == GNUNET_NO)
902 char * ident = "AAAAAAAAAA";
903 /* Connecting to peer with too short identification */
904 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
905 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
906 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
907 test_too_short_ident.test_executed = GNUNET_YES;
908 send_data ( &test_too_short_ident, host_str);
909 GNUNET_free (host_str);
913 if (test_too_long_ident.test_executed == GNUNET_NO)
915 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
917 /* Connecting to peer with too long identification */
918 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
919 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
920 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
921 test_too_long_ident.test_executed = GNUNET_YES;
922 send_data ( &test_too_long_ident, host_str);
923 GNUNET_free (host_str);
926 if (test_valid_ident.test_executed == GNUNET_NO)
928 struct GNUNET_CRYPTO_HashAsciiEncoded ident;
929 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
930 host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen ((char *) &ident));
931 GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,(char *) &ident);
933 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
934 test_valid_ident.test_executed = GNUNET_YES;
935 send_data ( &test_valid_ident, host_str);
936 GNUNET_free (host_str);
939 /* Using one of the addresses the plugin proposed */
940 GNUNET_assert (addr_head->addr != NULL);
942 struct Plugin_Address * tmp_addr;
943 struct GNUNET_MessageHeader msg;
944 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
945 char address[INET6_ADDRSTRLEN];
948 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
950 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
952 tmp_addr = addr_head;
953 /* send a message to all addresses advertised by plugin */
956 while (tmp_addr != NULL)
958 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
960 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
961 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
964 else if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
966 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
967 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
975 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, tmp_addr);
976 tmp_addr = tmp_addr->next;
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
988 * @param s scheduler to use
989 * @param c configuration to use
993 struct GNUNET_SCHEDULER_Handle *s,
995 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1001 unsigned long long tneigh;
1002 struct Plugin_Address * cur;
1003 const char * addr_str;
1006 unsigned int suggest_res;
1008 fail_pretty_printer = GNUNET_YES;
1009 fail_notify_address = GNUNET_YES;
1010 fail_addr_to_str = GNUNET_YES;
1011 fail_msgs_transmited_to_local_addrs = 0;
1015 /* parse configuration */
1017 GNUNET_CONFIGURATION_get_value_number (c,
1022 GNUNET_CONFIGURATION_get_value_filename (c,
1024 "HOSTKEY", &keyfile)))
1026 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1028 ("Transport service is lacking key configuration settings. Exiting.\n"));
1029 GNUNET_SCHEDULER_shutdown (s);
1035 GNUNET_CONFIGURATION_get_value_number (cfg,
1039 (port > 65535) || (port == 0))
1041 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1044 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1048 max_connect_per_transport = (uint32_t) tneigh;
1049 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1050 GNUNET_free (keyfile);
1051 if (my_private_key == NULL)
1053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1054 _("Transport service could not access hostkey. Exiting.\n"));
1055 GNUNET_SCHEDULER_shutdown (s);
1060 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1061 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1063 /* assertions before start */
1064 GNUNET_assert ((port > 0) && (port <= 65535));
1065 GNUNET_assert(&my_public_key != NULL);
1066 GNUNET_assert(&my_identity.hashPubKey != NULL);
1068 /* load plugins... */
1069 setup_plugin_environment ();
1070 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1071 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1072 api = GNUNET_PLUGIN_load (libname, &env);
1073 GNUNET_free (libname);
1076 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1077 _("Failed to load transport plugin for http\n"));
1082 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1084 /* testing plugin functionality */
1085 GNUNET_assert (0!=fail_notify_address_count);
1086 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1088 /* testing pretty printer with all addresses obtained from the plugin*/
1093 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
1094 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
1095 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
1097 GNUNET_assert (GNUNET_OK == suggest_res);
1098 GNUNET_assert (NULL != addr_str);
1100 GNUNET_free ( (char *) addr_str);
1103 GNUNET_assert (fail_pretty_printer_count > 0);
1104 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1105 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1106 fail_pretty_printer=GNUNET_NO;
1107 fail_addr_to_str=GNUNET_NO;
1109 /* Suggesting addresses with wrong port*/
1110 struct IPv4HttpAddress failing_addr;
1111 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1112 failing_addr.u_port = htons(0);
1113 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1114 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1116 /* Suggesting addresses with wrong size*/
1117 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1118 failing_addr.u_port = htons(0);
1119 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
1120 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1122 /* Suggesting addresses with wrong address*/
1123 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1124 failing_addr.u_port = htons(12389);
1125 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1126 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1128 /* test sending to client */
1129 multi_handle = curl_multi_init();
1131 /* Setting up buffers */
1132 buffer_in.size = HTTP_BUFFER_SIZE;
1136 buffer_out.size = HTTP_BUFFER_SIZE;
1140 /* Setting up connection tests */
1142 /* Test: connecting without a peer identification */
1143 test_no_ident.test_executed = GNUNET_NO;
1144 test_no_ident.test_failed = GNUNET_YES;
1146 /* Test: connecting with too short peer identification */
1147 test_too_short_ident.test_executed = GNUNET_NO;
1148 test_too_short_ident.test_failed = GNUNET_YES;
1150 /* Test: connecting with too long peer identification */
1151 test_too_long_ident.test_executed = GNUNET_NO;
1152 test_too_long_ident.test_failed = GNUNET_YES;
1154 /* Test: connecting with valid identification */
1155 test_valid_ident.test_executed = GNUNET_NO;
1156 test_valid_ident.test_failed = GNUNET_YES;
1158 test_addr = (char *) api->address_to_string (NULL,addr_head->addr,addr_head->addrlen);
1159 run_connection_tests();
1161 /* testing finished */
1168 * The main function for the transport service.
1170 * @param argc number of arguments from the command line
1171 * @param argv command line arguments
1172 * @return 0 ok, 1 on error
1175 main (int argc, char *const *argv)
1178 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1179 GNUNET_GETOPT_OPTION_END
1182 char *const argv_prog[] = {
1183 "test_plugin_transport_http",
1185 "test_plugin_transport_data_http.conf",
1194 GNUNET_log_setup ("test_plugin_transport_http",
1203 GNUNET_PROGRAM_run (5,
1205 "test_plugin_transport_http",
1206 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1208 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1213 /* end of test_plugin_transport_http.c */