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_YES
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 struct GNUNET_SCHEDULER_Handle *sched;
226 * Our statistics handle.
228 struct GNUNET_STATISTICS_Handle *stats;
234 const struct GNUNET_CONFIGURATION_Handle *cfg;
237 * Number of neighbours we'd like to have.
239 static uint32_t max_connect_per_transport;
242 * Environment for this plugin.
244 static struct GNUNET_TRANSPORT_PluginEnvironment env;
247 *handle for the api provided by this plugin
249 static struct GNUNET_TRANSPORT_PluginFunctions *api;
252 * ID of the task controlling the testcase timeout
254 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
256 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
258 //const struct GNUNET_PeerIdentity * p;
261 * buffer for data to send
263 static struct HTTP_Message buffer_out;
266 * buffer for data to recieve
268 static struct HTTP_Message buffer_in;
271 struct Plugin_Address * addr_head;
274 * Did the test pass or fail?
276 static int fail_notify_address;
278 * Did the test pass or fail?
280 static int fail_notify_address_count;
283 * Did the test pass or fail?
285 static int fail_pretty_printer;
288 * Did the test pass or fail?
290 static int fail_pretty_printer_count;
293 * Did the test pass or fail?
295 static int fail_addr_to_str;
298 * No. of msgs transmitted successfully to local addresses
300 static int fail_msgs_transmited_to_local_addrs;
303 * Test: connect to peer without peer identification
305 static struct HTTP_Transfer test_no_ident;
308 * Test: connect to peer without peer identification
310 static struct HTTP_Transfer test_too_short_ident;
313 * Test: connect to peer without peer identification
315 static struct HTTP_Transfer test_too_long_ident;
318 * Test: connect to peer with valid peer identification
320 static struct HTTP_Transfer test_valid_ident;
323 * Did the test pass or fail?
328 * Number of local addresses
330 static unsigned int count_str_addr;
337 static CURLM *multi_handle;
340 * The task sending data
342 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
350 struct Plugin_Address * cur;
351 struct Plugin_Address * tmp;
353 /* Evaluate results */
355 if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test plugin functions failed\n");
360 if ((test_no_ident.test_failed == GNUNET_YES) || (test_too_short_ident.test_failed == GNUNET_YES) || (test_too_long_ident.test_failed == GNUNET_YES))
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test connect with wrong data failed\n");
365 if ((test_valid_ident.test_failed == GNUNET_YES) || (fail_msgs_transmited_to_local_addrs != count_str_addr))
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test sending with plugin failed\n");
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
375 curl_multi_cleanup(multi_handle);
377 if (NULL != curl_handle)
378 curl_easy_cleanup (curl_handle);
380 /* cleaning addresses */
381 while (addr_head != NULL)
384 tmp = addr_head->next;
385 GNUNET_free (addr_head->addr);
386 GNUNET_free (addr_head);
390 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
392 GNUNET_SCHEDULER_cancel(sched,ti_send);
393 ti_send = GNUNET_SCHEDULER_NO_TASK;
396 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
398 GNUNET_SCHEDULER_cancel(sched,http_task_send);
399 http_task_send = GNUNET_SCHEDULER_NO_TASK;
402 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
404 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
405 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
408 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
410 GNUNET_SCHEDULER_shutdown(sched);
411 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
420 * Continuation called after plugin send message
423 * @result GNUNET_OK or GNUNET_SYSERR
426 static void task_send_cont (void *cls,
427 const struct GNUNET_PeerIdentity * target,
430 struct Plugin_Address * tmp_addr;
431 tmp_addr = addr_head;
433 while (tmp_addr != NULL)
436 if (result == GNUNET_OK) fail_msgs_transmited_to_local_addrs++;
437 tmp_addr = tmp_addr->next;
440 if (fail_msgs_transmited_to_local_addrs == count_str_addr)
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message sent to %u addresses!\n",fail_msgs_transmited_to_local_addrs);
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
454 * Task sending recieved message back to peer
460 task_send (void *cls,
461 const struct GNUNET_SCHEDULER_TaskContext *tc)
463 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
464 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
467 if (GNUNET_YES==sent)
470 struct GNUNET_MessageHeader * msg = cls;
471 unsigned int len = ntohs(msg->size);
472 const char * msgc = (const char *) msg;
474 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
481 * Recieves messages from plugin, in real world transport
483 static struct GNUNET_TIME_Relative
485 const struct GNUNET_PeerIdentity * peer,
486 const struct GNUNET_MessageHeader * message,
488 struct Session *session,
489 const char *sender_address,
490 uint16_t sender_address_len)
492 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));
493 return GNUNET_TIME_UNIT_ZERO;
496 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
500 len = buffer_out.len;
502 if (( buffer_out.pos == len) || (len > (size * nmemb)))
504 memcpy(stream, buffer_out.buf, len);
505 buffer_out.pos = len;
509 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
512 if (buffer_in.pos + size * nmemb > buffer_in.size)
513 return 0; /* overflow */
515 buffer_in.len = size * nmemb;
516 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
517 buffer_in.pos += size * nmemb;
518 buffer_in.len = buffer_in.pos;
519 buffer_in.buf[buffer_in.pos] = '\0';
520 return buffer_in.pos;
523 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
525 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
527 unsigned int len = size * nmemb;
529 tmp = GNUNET_malloc ( len+1 );
531 if (tmp[len-2] == 13)
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
536 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
538 res->http_result_code=100;
540 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
542 res->http_result_code=200;
544 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
546 res->http_result_code=400;
548 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
550 res->http_result_code=404;
552 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
554 res->http_result_code=413;
561 static size_t send_prepare( struct HTTP_Transfer * result);
563 static void run_connection_tests( void );
565 static void send_execute (void *cls,
566 const struct GNUNET_SCHEDULER_TaskContext *tc)
568 struct HTTP_Transfer *res;
574 res = (struct HTTP_Transfer *) cls;
575 http_task_send = GNUNET_SCHEDULER_NO_TASK;
576 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
582 mret = curl_multi_perform (multi_handle, &running);
588 msg = curl_multi_info_read (multi_handle, &running);
591 /* get session for affected curl handle */
592 //cs = find_session_by_curlhandle (msg->easy_handle);
593 //GNUNET_assert ( cs != NULL );
598 if ( (msg->data.result != CURLE_OK) &&
599 (msg->data.result != CURLE_GOT_NOTHING) )
602 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
603 _("curl failed for `%s' at %s:%d: `%s'\n"),
604 "curl_multi_perform",
607 curl_easy_strerror (msg->data.result));
608 /* sending msg failed*/
609 curl_easy_cleanup(curl_handle);
612 run_connection_tests();
614 if (res == &test_no_ident)
616 if ((res->http_result_code==404) && (buffer_in.len==208))
618 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
619 res->test_failed = GNUNET_NO;
622 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
624 if (res == &test_too_short_ident)
626 if ((res->http_result_code==404) && (buffer_in.len==208))
628 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
629 res->test_failed = GNUNET_NO;
632 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
634 if (res == &test_too_long_ident)
636 if ((res->http_result_code==404) && (buffer_in.len==208))
638 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
639 res->test_failed = GNUNET_NO;
642 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
644 if (res == &test_valid_ident)
646 if ((res->http_result_code==400))
648 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
649 res->test_failed = GNUNET_NO;
652 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
654 curl_easy_cleanup(curl_handle);
657 run_connection_tests();
664 while ( (running > 0) );
667 while (mret == CURLM_CALL_MULTI_PERFORM);
672 * Function setting up file descriptors and scheduling task to run
673 * @param ses session to send data to
674 * @return bytes sent to peer
676 static size_t send_prepare( struct HTTP_Transfer * result)
682 struct GNUNET_NETWORK_FDSet *grs;
683 struct GNUNET_NETWORK_FDSet *gws;
691 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
692 if (mret != CURLM_OK)
694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
695 _("%s failed at %s:%d: `%s'\n"),
696 "curl_multi_fdset", __FILE__, __LINE__,
697 curl_multi_strerror (mret));
700 mret = curl_multi_timeout (multi_handle, &to);
701 if (mret != CURLM_OK)
703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
704 _("%s failed at %s:%d: `%s'\n"),
705 "curl_multi_timeout", __FILE__, __LINE__,
706 curl_multi_strerror (mret));
710 grs = GNUNET_NETWORK_fdset_create ();
711 gws = GNUNET_NETWORK_fdset_create ();
712 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
713 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
714 http_task_send = GNUNET_SCHEDULER_add_select (sched,
715 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
716 GNUNET_SCHEDULER_NO_TASK,
717 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
722 GNUNET_NETWORK_fdset_destroy (gws);
723 GNUNET_NETWORK_fdset_destroy (grs);
725 /* FIXME: return bytes REALLY sent */
730 * function to send data to server
732 static int send_data( struct HTTP_Transfer * result, char * url)
735 curl_handle = curl_easy_init();
736 if( NULL == curl_handle)
738 printf("easy_init failed \n");
739 return GNUNET_SYSERR;
742 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
744 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
745 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
746 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
747 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
748 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
749 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
750 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
751 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
752 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
753 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
754 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
756 curl_multi_add_handle(multi_handle, curl_handle);
758 send_prepare(result);
765 * Plugin notifies transport (aka testcase) about its addresses
768 notify_address (void *cls,
772 struct GNUNET_TIME_Relative expires)
774 char address[INET6_ADDRSTRLEN];
776 struct Plugin_Address * pl_addr;
777 struct Plugin_Address * cur;
779 if (addrlen == (sizeof (struct IPv4HttpAddress)))
781 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
782 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
784 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
786 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
787 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 _("Transport plugin notification for address: `%s':%u\n"),
798 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
799 pl_addr->addrlen = addrlen;
800 pl_addr->addr = GNUNET_malloc(addrlen);
801 memcpy(pl_addr->addr,addr,addrlen);
802 pl_addr->next = NULL;
804 if ( NULL == addr_head)
811 while (NULL != cur->next)
817 fail_notify_address_count++;
818 fail_notify_address = GNUNET_NO;
822 * Setup plugin environment
825 setup_plugin_environment ()
830 env.my_identity = &my_identity;
832 env.receive = &receive;
833 env.notify_address = ¬ify_address;
834 env.max_connections = max_connect_per_transport;
839 * Task shutting down testcase if it a timeout occurs
842 task_timeout (void *cls,
843 const struct GNUNET_SCHEDULER_TaskContext *tc)
845 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
846 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
855 static void pretty_printer_cb (void *cls,
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
861 fail_pretty_printer_count++;
865 * Runs every single test to test the plugin
867 static void run_connection_tests( void )
869 static char * host_str;
871 /* resetting buffers */
872 buffer_in.size = HTTP_BUFFER_SIZE;
876 buffer_out.size = HTTP_BUFFER_SIZE;
881 if (test_no_ident.test_executed == GNUNET_NO)
883 /* Connecting to peer without identification */
885 host_str = GNUNET_malloc (strlen ("http://localhost:12389/")+1);
886 GNUNET_asprintf (&host_str, "http://localhost:%u/",port);
887 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
888 test_no_ident.test_executed = GNUNET_YES;
889 send_data ( &test_no_ident, host_str);
890 GNUNET_free (host_str);
893 if (test_too_short_ident.test_executed == GNUNET_NO)
895 char * ident = "AAAAAAAAAA";
896 /* Connecting to peer with too short identification */
897 host_str = GNUNET_malloc (strlen ("http://localhost:12389/") + strlen (ident));
898 GNUNET_asprintf (&host_str, "http://localhost:%u/%s",port,ident);
899 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
900 test_too_short_ident.test_executed = GNUNET_YES;
901 send_data ( &test_too_short_ident, host_str);
902 GNUNET_free (host_str);
906 if (test_too_long_ident.test_executed == GNUNET_NO)
908 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
910 /* Connecting to peer with too long identification */
911 host_str = GNUNET_malloc (strlen ("http://localhost:12389/") + strlen (ident));
912 GNUNET_asprintf (&host_str, "http://localhost:%u/%s",port,ident);
913 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
914 test_too_long_ident.test_executed = GNUNET_YES;
915 send_data ( &test_too_long_ident, host_str);
916 GNUNET_free (host_str);
919 if (test_valid_ident.test_executed == GNUNET_NO)
921 struct GNUNET_CRYPTO_HashAsciiEncoded result;
922 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&result);
923 host_str = GNUNET_malloc (strlen ("http://localhost:12389/") + strlen ((const char *) &result));
924 GNUNET_asprintf (&host_str, "http://localhost:%u/%s",port,(char *) &result);
926 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
927 test_valid_ident.test_executed = GNUNET_YES;
928 send_data ( &test_valid_ident, host_str);
929 GNUNET_free (host_str);
932 /* Using one of the addresses the plugin proposed */
933 GNUNET_assert (addr_head->addr != NULL);
935 struct Plugin_Address * tmp_addr;
936 struct GNUNET_MessageHeader msg;
937 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
938 char address[INET6_ADDRSTRLEN];
941 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
943 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
945 tmp_addr = addr_head;
946 /* send a message to all addresses advertised by plugin */
949 while (tmp_addr != NULL)
951 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
953 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
954 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
957 else if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
959 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
960 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
968 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);
969 tmp_addr = tmp_addr->next;
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
981 * @param s scheduler to use
982 * @param c configuration to use
986 struct GNUNET_SCHEDULER_Handle *s,
988 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
994 unsigned long long tneigh;
995 struct Plugin_Address * cur;
996 const char * addr_str;
999 unsigned int suggest_res;
1001 fail_pretty_printer = GNUNET_YES;
1002 fail_notify_address = GNUNET_YES;
1003 fail_addr_to_str = GNUNET_YES;
1004 fail_msgs_transmited_to_local_addrs = 0;
1008 /* parse configuration */
1010 GNUNET_CONFIGURATION_get_value_number (c,
1015 GNUNET_CONFIGURATION_get_value_filename (c,
1017 "HOSTKEY", &keyfile)))
1019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1021 ("Transport service is lacking key configuration settings. Exiting.\n"));
1022 GNUNET_SCHEDULER_shutdown (s);
1028 GNUNET_CONFIGURATION_get_value_number (cfg,
1032 (port > 65535) || (port == 0))
1034 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1037 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1041 max_connect_per_transport = (uint32_t) tneigh;
1042 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1043 GNUNET_free (keyfile);
1044 if (my_private_key == NULL)
1046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1047 _("Transport service could not access hostkey. Exiting.\n"));
1048 GNUNET_SCHEDULER_shutdown (s);
1053 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1054 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1056 /* assertions before start */
1057 GNUNET_assert ((port > 0) && (port <= 65535));
1058 GNUNET_assert(&my_public_key != NULL);
1059 GNUNET_assert(&my_identity.hashPubKey != NULL);
1061 /* load plugins... */
1062 setup_plugin_environment ();
1063 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1064 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1065 api = GNUNET_PLUGIN_load (libname, &env);
1066 GNUNET_free (libname);
1069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1070 _("Failed to load transport plugin for http\n"));
1075 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1077 /* testing plugin functionality */
1078 GNUNET_assert (0!=fail_notify_address_count);
1079 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1081 /* testing pretty printer with all addresses obtained from the plugin*/
1086 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
1087 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
1088 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
1090 GNUNET_assert (GNUNET_OK == suggest_res);
1091 GNUNET_assert (NULL != addr_str);
1098 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1099 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1100 fail_pretty_printer=GNUNET_NO;
1101 fail_addr_to_str=GNUNET_NO;
1103 /* Suggesting addresses with wrong port*/
1104 struct IPv4HttpAddress failing_addr;
1105 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1106 failing_addr.u_port = htons(0);
1107 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1108 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1110 /* Suggesting addresses with wrong size*/
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 IPv6HttpAddress));
1114 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1116 /* Suggesting addresses with wrong address*/
1117 failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1118 failing_addr.u_port = htons(12389);
1119 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1120 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1122 /* test sending to client */
1123 multi_handle = curl_multi_init();
1125 /* Setting up buffers */
1126 buffer_in.size = HTTP_BUFFER_SIZE;
1130 buffer_out.size = HTTP_BUFFER_SIZE;
1134 /* Setting up connection tests */
1136 /* Test: connecting without a peer identification */
1137 test_no_ident.test_executed = GNUNET_NO;
1138 test_no_ident.test_failed = GNUNET_YES;
1140 /* Test: connecting with too short peer identification */
1141 test_too_short_ident.test_executed = GNUNET_NO;
1142 test_too_short_ident.test_failed = GNUNET_YES;
1144 /* Test: connecting with too long peer identification */
1145 test_too_long_ident.test_executed = GNUNET_NO;
1146 test_too_long_ident.test_failed = GNUNET_YES;
1148 /* Test: connecting with valid identification */
1149 test_valid_ident.test_executed = GNUNET_NO;
1150 test_valid_ident.test_failed = GNUNET_YES;
1152 run_connection_tests();
1154 /* testing finished */
1161 * The main function for the transport service.
1163 * @param argc number of arguments from the command line
1164 * @param argv command line arguments
1165 * @return 0 ok, 1 on error
1168 main (int argc, char *const *argv)
1171 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1172 GNUNET_GETOPT_OPTION_END
1175 char *const argv_prog[] = {
1176 "test_plugin_transport_http",
1178 "test_plugin_transport_data_http.conf",
1187 GNUNET_log_setup ("test_plugin_transport_http",
1196 GNUNET_PROGRAM_run (5,
1198 "test_plugin_transport_http",
1199 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1201 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1206 /* end of test_plugin_transport_http.c */