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 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 * Test: connect to peer without peer identification
300 static struct HTTP_Transfer test_no_ident;
303 * Test: connect to peer without peer identification
305 static struct HTTP_Transfer test_too_short_ident;
308 * Test: connect to peer without peer identification
310 static struct HTTP_Transfer test_too_long_ident;
313 * Test: connect to peer with valid peer identification
315 static struct HTTP_Transfer test_valid_ident;
318 * Did the test pass or fail?
327 static CURLM *multi_handle;
330 * The task sending data
332 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
340 struct Plugin_Address * cur;
341 struct Plugin_Address * tmp;
343 /* Evaluate results */
344 if ((fail_notify_address == GNUNET_NO) && (fail_pretty_printer == GNUNET_NO) && (fail_addr_to_str == GNUNET_NO) &&
345 (test_no_ident.test_failed == GNUNET_NO) && (test_too_short_ident.test_failed == GNUNET_NO) && (test_too_long_ident.test_failed == GNUNET_NO) &&
346 (test_valid_ident.test_failed == GNUNET_NO))
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tests successful\n");
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Tests failed\n");
357 curl_multi_cleanup(multi_handle);
359 if (NULL != curl_handle)
360 curl_easy_cleanup (curl_handle);
362 /* cleaning addresses */
363 while (addr_head != NULL)
366 tmp = addr_head->next;
367 GNUNET_free (addr_head->addr);
368 GNUNET_free (addr_head);
372 if (ti_send != GNUNET_SCHEDULER_NO_TASK)
374 GNUNET_SCHEDULER_cancel(sched,ti_send);
375 ti_send = GNUNET_SCHEDULER_NO_TASK;
378 if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
380 GNUNET_SCHEDULER_cancel(sched,http_task_send);
381 http_task_send = GNUNET_SCHEDULER_NO_TASK;
384 if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
386 GNUNET_SCHEDULER_cancel(sched,ti_timeout);
387 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
390 GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
392 GNUNET_SCHEDULER_shutdown(sched);
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
401 * Continuation called after plugin send message
404 * @result GNUNET_OK or GNUNET_SYSERR
407 static void task_send_cont (void *cls,
408 const struct GNUNET_PeerIdentity * target,
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
418 * Task sending recieved message back to peer
424 task_send (void *cls,
425 const struct GNUNET_SCHEDULER_TaskContext *tc)
427 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
428 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
431 if (GNUNET_YES==sent)
434 struct GNUNET_MessageHeader * msg = cls;
435 unsigned int len = ntohs(msg->size);
436 const char * msgc = (const char *) msg;
438 api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
445 * Recieves messages from plugin, in real world transport
447 static struct GNUNET_TIME_Relative
449 const struct GNUNET_PeerIdentity * peer,
450 const struct GNUNET_MessageHeader * message,
452 struct Session *session,
453 const char *sender_address,
454 uint16_t sender_address_len)
456 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));
457 return GNUNET_TIME_UNIT_ZERO;
460 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
464 len = buffer_out.len;
466 if (( buffer_out.pos == len) || (len > (size * nmemb)))
468 memcpy(stream, buffer_out.buf, len);
469 buffer_out.pos = len;
473 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
476 if (buffer_in.pos + size * nmemb > buffer_in.size)
477 return 0; /* overflow */
479 buffer_in.len = size * nmemb;
480 memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
481 buffer_in.pos += size * nmemb;
482 buffer_in.len = buffer_in.pos;
483 buffer_in.buf[buffer_in.pos] = '\0';
484 return buffer_in.pos;
487 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
489 struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
491 unsigned int len = size * nmemb;
493 tmp = GNUNET_malloc ( len+1 );
495 if (tmp[len-2] == 13)
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
500 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
502 res->http_result_code=100;
504 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
506 res->http_result_code=200;
508 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
510 res->http_result_code=400;
512 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
514 res->http_result_code=404;
516 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
518 res->http_result_code=413;
525 static size_t send_prepare( struct HTTP_Transfer * result);
527 static void run_connection_tests( void );
529 static void send_execute (void *cls,
530 const struct GNUNET_SCHEDULER_TaskContext *tc)
532 struct HTTP_Transfer *res;
538 res = (struct HTTP_Transfer *) cls;
539 http_task_send = GNUNET_SCHEDULER_NO_TASK;
540 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
546 mret = curl_multi_perform (multi_handle, &running);
552 msg = curl_multi_info_read (multi_handle, &running);
553 GNUNET_break (msg != NULL);
556 /* get session for affected curl handle */
557 //cs = find_session_by_curlhandle (msg->easy_handle);
558 //GNUNET_assert ( cs != NULL );
563 if ( (msg->data.result != CURLE_OK) &&
564 (msg->data.result != CURLE_GOT_NOTHING) )
567 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
568 _("curl failed for `%s' at %s:%d: `%s'\n"),
569 "curl_multi_perform",
572 curl_easy_strerror (msg->data.result));
573 /* sending msg failed*/
575 if (res == &test_no_ident)
577 if ((res->http_result_code==404) && (buffer_in.len==208))
579 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
580 res->test_failed = GNUNET_NO;
583 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
585 if (res == &test_too_short_ident)
587 if ((res->http_result_code==404) && (buffer_in.len==208))
589 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
590 res->test_failed = GNUNET_NO;
593 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
595 if (res == &test_too_long_ident)
597 if ((res->http_result_code==404) && (buffer_in.len==208))
599 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
600 res->test_failed = GNUNET_NO;
603 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
605 if (res == &test_valid_ident)
607 if ((res->http_result_code==400))
609 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
610 res->test_failed = GNUNET_NO;
613 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
615 curl_easy_cleanup(curl_handle);
618 run_connection_tests();
625 while ( (running > 0) );
628 while (mret == CURLM_CALL_MULTI_PERFORM);
633 * Function setting up file descriptors and scheduling task to run
634 * @param ses session to send data to
635 * @return bytes sent to peer
637 static size_t send_prepare( struct HTTP_Transfer * result)
643 struct GNUNET_NETWORK_FDSet *grs;
644 struct GNUNET_NETWORK_FDSet *gws;
652 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
653 if (mret != CURLM_OK)
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 _("%s failed at %s:%d: `%s'\n"),
657 "curl_multi_fdset", __FILE__, __LINE__,
658 curl_multi_strerror (mret));
661 mret = curl_multi_timeout (multi_handle, &to);
662 if (mret != CURLM_OK)
664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
665 _("%s failed at %s:%d: `%s'\n"),
666 "curl_multi_timeout", __FILE__, __LINE__,
667 curl_multi_strerror (mret));
671 grs = GNUNET_NETWORK_fdset_create ();
672 gws = GNUNET_NETWORK_fdset_create ();
673 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
674 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
675 http_task_send = GNUNET_SCHEDULER_add_select (sched,
676 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
677 GNUNET_SCHEDULER_NO_TASK,
678 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
683 GNUNET_NETWORK_fdset_destroy (gws);
684 GNUNET_NETWORK_fdset_destroy (grs);
686 /* FIXME: return bytes REALLY sent */
691 * function to send data to server
693 static int send_data( struct HTTP_Transfer * result, char * url)
696 curl_handle = curl_easy_init();
697 if( NULL == curl_handle)
699 printf("easy_init failed \n");
700 return GNUNET_SYSERR;
703 curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
705 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
706 curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
707 curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
708 curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
709 curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
710 curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
711 curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
712 curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
713 curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
714 curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
715 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
717 curl_multi_add_handle(multi_handle, curl_handle);
719 send_prepare(result);
726 * Plugin notifies transport (aka testcase) about its addresses
729 notify_address (void *cls,
733 struct GNUNET_TIME_Relative expires)
735 char address[INET6_ADDRSTRLEN];
737 struct Plugin_Address * pl_addr;
738 struct Plugin_Address * cur;
740 if (addrlen == (sizeof (struct IPv4HttpAddress)))
742 inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
743 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
745 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
747 inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
748 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 _("Transport plugin notification for address: `%s':%u\n"),
759 pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
760 pl_addr->addrlen = addrlen;
761 pl_addr->addr = GNUNET_malloc(addrlen);
762 memcpy(pl_addr->addr,addr,addrlen);
763 pl_addr->next = NULL;
765 if ( NULL == addr_head)
772 while (NULL != cur->next)
778 fail_notify_address_count++;
779 fail_notify_address = GNUNET_NO;
783 * Setup plugin environment
786 setup_plugin_environment ()
791 env.my_identity = &my_identity;
793 env.receive = &receive;
794 env.notify_address = ¬ify_address;
795 env.max_connections = max_connect_per_transport;
800 * Task shutting down testcase if it a timeout occurs
803 task_timeout (void *cls,
804 const struct GNUNET_SCHEDULER_TaskContext *tc)
806 ti_timeout = GNUNET_SCHEDULER_NO_TASK;
807 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
816 static void pretty_printer_cb (void *cls,
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
822 fail_pretty_printer_count++;
826 * Runs every single test to test the plugin
828 static void run_connection_tests( void )
832 /* resetting buffers */
833 buffer_in.size = HTTP_BUFFER_SIZE;
837 buffer_out.size = HTTP_BUFFER_SIZE;
842 if (test_no_ident.test_executed == GNUNET_NO)
844 /* Connecting to peer without identification */
845 host_str = GNUNET_malloc (strlen ("http://localhost:12389/")+1);
846 GNUNET_asprintf (&host_str, "http://localhost:%u/",port);
847 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
848 test_no_ident.test_executed = GNUNET_YES;
849 send_data ( &test_no_ident, host_str);
850 GNUNET_free (host_str);
855 if (test_too_short_ident.test_executed == GNUNET_NO)
857 char * ident = "AAAAAAAAAA";
858 /* Connecting to peer with too short identification */
859 host_str = GNUNET_malloc (strlen ("http://localhost:12389/") + strlen (ident));
860 GNUNET_asprintf (&host_str, "http://localhost:%u/%s",port,ident);
861 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification.\n"));
862 test_too_short_ident.test_executed = GNUNET_YES;
863 send_data ( &test_too_short_ident, host_str);
864 GNUNET_free (host_str);
869 if (test_too_long_ident.test_executed == GNUNET_NO)
871 char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
873 /* Connecting to peer with too long identification */
874 host_str = GNUNET_malloc (strlen ("http://localhost:12389/") + strlen (ident));
875 GNUNET_asprintf (&host_str, "http://localhost:%u/%s",port,ident);
877 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
878 test_too_long_ident.test_executed = GNUNET_YES;
879 send_data ( &test_too_long_ident, host_str);
880 GNUNET_free (host_str);
884 if (test_valid_ident.test_executed == GNUNET_NO)
886 struct GNUNET_CRYPTO_HashAsciiEncoded result;
888 GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&result);
889 host_str = GNUNET_malloc (strlen ("http://localhost:12389/") + strlen ((const char *) &result));
890 GNUNET_asprintf (&host_str, "http://localhost:%u/%s",port,(char *) &result);
892 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
893 test_valid_ident.test_executed = GNUNET_YES;
894 send_data ( &test_valid_ident, host_str);
895 GNUNET_free (host_str);
899 /* Using one of the addresses the plugin proposed */
900 GNUNET_assert (addr_head->addr != NULL);
902 struct Plugin_Address * tmp_addr;
903 struct GNUNET_MessageHeader msg;
904 char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
905 char address[INET6_ADDRSTRLEN];
908 msg.size=htons(sizeof(struct GNUNET_MessageHeader));
910 memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
912 tmp_addr = addr_head;
913 /* send a message to all addresses advertised by plugin */
916 while (tmp_addr != NULL)
920 if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
922 inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
923 port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
926 else if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
928 inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
929 port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
937 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, NULL);
938 tmp_addr = tmp_addr->next;
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
950 * @param s scheduler to use
951 * @param c configuration to use
955 struct GNUNET_SCHEDULER_Handle *s,
957 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
963 unsigned long long tneigh;
964 struct Plugin_Address * cur;
965 const char * addr_str;
967 unsigned int count_str_addr;
968 unsigned int suggest_res;
970 fail_pretty_printer = GNUNET_YES;
971 fail_notify_address = GNUNET_YES;
972 fail_addr_to_str = GNUNET_YES;
976 /* parse configuration */
978 GNUNET_CONFIGURATION_get_value_number (c,
983 GNUNET_CONFIGURATION_get_value_filename (c,
985 "HOSTKEY", &keyfile)))
987 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
989 ("Transport service is lacking key configuration settings. Exiting.\n"));
990 GNUNET_SCHEDULER_shutdown (s);
996 GNUNET_CONFIGURATION_get_value_number (cfg,
1002 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1005 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1008 max_connect_per_transport = (uint32_t) tneigh;
1009 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1010 GNUNET_free (keyfile);
1011 if (my_private_key == NULL)
1013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1014 _("Transport service could not access hostkey. Exiting.\n"));
1015 GNUNET_SCHEDULER_shutdown (s);
1019 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1020 GNUNET_CRYPTO_hash (&my_public_key,
1021 sizeof (my_public_key), &my_identity.hashPubKey);
1023 /* load plugins... */
1024 setup_plugin_environment ();
1025 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1026 GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1027 api = GNUNET_PLUGIN_load (libname, &env);
1028 GNUNET_free (libname);
1031 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1032 _("Failed to load transport plugin for http\n"));
1037 ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1039 /* testing plugin functionality */
1040 GNUNET_assert (0!=fail_notify_address_count);
1041 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"), fail_notify_address_count);
1043 /* testing pretty printer with all addresses obtained from the plugin*/
1048 api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
1049 addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
1050 suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
1052 GNUNET_assert (GNUNET_OK == suggest_res);
1053 GNUNET_assert (NULL != addr_str);
1060 GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1061 GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1062 fail_pretty_printer=GNUNET_NO;
1063 fail_addr_to_str=GNUNET_NO;
1065 /* Suggesting addresses with wrong port*/
1066 struct IPv4HttpAddress failing_addr;
1067 failing_addr.ipv4_addr = INADDR_LOOPBACK;
1068 failing_addr.u_port = 0;
1069 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1070 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1072 /* Suggesting addresses with wrong size*/
1073 failing_addr.ipv4_addr = INADDR_LOOPBACK;
1074 failing_addr.u_port = 0;
1075 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
1076 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1078 /* Suggesting addresses with wrong address*/
1079 failing_addr.ipv4_addr = 0;
1080 failing_addr.u_port = 12389;
1081 suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
1082 GNUNET_assert (GNUNET_SYSERR == suggest_res);
1084 /* test sending to client */
1085 multi_handle = curl_multi_init();
1087 /* Setting up buffers */
1088 buffer_in.size = HTTP_BUFFER_SIZE;
1092 buffer_out.size = HTTP_BUFFER_SIZE;
1096 /* Setting up connection tests */
1098 /* Test: connecting without a peer identification */
1099 test_no_ident.test_executed = GNUNET_NO;
1100 test_no_ident.test_failed = GNUNET_YES;
1102 /* Test: connecting with too short peer identification */
1103 test_too_short_ident.test_executed = GNUNET_NO;
1104 test_too_short_ident.test_failed = GNUNET_YES;
1106 /* Test: connecting with too long peer identification */
1107 test_too_long_ident.test_executed = GNUNET_NO;
1108 test_too_long_ident.test_failed = GNUNET_YES;
1110 /* Test: connecting with valid identification */
1111 test_valid_ident.test_executed = GNUNET_NO;
1112 test_valid_ident.test_failed = GNUNET_YES;
1114 run_connection_tests();
1116 /* testing finished */
1123 * The main function for the transport service.
1125 * @param argc number of arguments from the command line
1126 * @param argv command line arguments
1127 * @return 0 ok, 1 on error
1130 main (int argc, char *const *argv)
1133 static struct GNUNET_GETOPT_CommandLineOption options[] = {
1134 GNUNET_GETOPT_OPTION_END
1137 char *const argv_prog[] = {
1138 "test_plugin_transport_http",
1140 "test_plugin_transport_data_http.conf",
1149 GNUNET_log_setup ("test_plugin_transport_http",
1158 GNUNET_PROGRAM_run (5,
1160 "test_plugin_transport_http",
1161 "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1163 GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1168 /* end of test_plugin_transport_http.c */