(no commit message)
[oweals/gnunet.git] / src / transport / test_plugin_transport_http.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20 /**
21  * @file transport/test_plugin_transport_http.c
22  * @brief testcase for plugin_transport_http.c
23  * @author Matthias Wachs
24  */
25
26 #include "platform.h"
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"
39
40 #include "plugin_transport.h"
41 #include "gnunet_statistics_service.h"
42 #include "transport.h"
43 #include <curl/curl.h>
44
45 #define VERBOSE GNUNET_YES
46 #define DEBUG GNUNET_NO
47 #define DEBUG_CURL GNUNET_NO
48 #define HTTP_BUFFER_SIZE 2048
49
50 #define PLUGIN libgnunet_plugin_transport_template
51
52 /**
53  * How long until we give up on transmitting the message?
54  */
55 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
56
57 /**
58  * How long until we give up on transmitting the message?
59  */
60 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
61
62 /**
63  * How long between recieve and send?
64  */
65 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
66
67
68
69 /**
70  *  Struct for plugin addresses
71  */
72 struct Plugin_Address
73 {
74   /**
75    * Next field for linked list
76    */
77   struct Plugin_Address * next;
78
79   /**
80    * buffer containing data to send
81    */
82   void * addr;
83
84   /**
85    * amount of data to sent
86    */
87   size_t addrlen;
88 };
89
90 /**
91  *  Message to send using http
92  */
93 struct HTTP_Message
94 {
95   /**
96    * buffer
97    */
98   unsigned char buf[HTTP_BUFFER_SIZE];
99
100   /**
101    * current position in buffer
102    */
103   size_t pos;
104
105   /**
106    * buffer size
107    */
108   size_t size;
109
110   /**
111    * data size
112    */
113   size_t len;
114 };
115
116
117 /**
118  *  Struct for plugin addresses
119  */
120 struct HTTP_Transfer
121 {
122   /**
123    * amount of bytes we recieved
124    */
125   size_t data_size;
126
127   /**
128    * buffer for http transfers
129    */
130   unsigned char buf[HTTP_BUFFER_SIZE];
131
132   /**
133    * buffer size this transfer
134    */
135   size_t size;
136
137   /**
138    * amount of bytes we recieved
139    */
140   size_t pos;
141
142   /**
143    * HTTP Header result for transfer
144    */
145   unsigned int http_result_code;
146
147   /**
148    * did the test fail?
149    */
150   unsigned int test_failed;
151
152   /**
153    * was this test already executed?
154    */
155   unsigned int test_executed;
156 };
157
158
159 /**
160  * Network format for IPv4 addresses.
161  */
162 struct IPv4HttpAddress
163 {
164   /**
165    * IPv4 address, in network byte order.
166    */
167   uint32_t ipv4_addr GNUNET_PACKED;
168
169   /**
170    * Port number, in network byte order.
171    */
172   uint16_t u_port GNUNET_PACKED;
173
174 };
175
176
177 /**
178  * Network format for IPv6 addresses.
179  */
180 struct IPv6HttpAddress
181 {
182   /**
183    * IPv6 address.
184    */
185   struct in6_addr ipv6_addr GNUNET_PACKED;
186
187   /**
188    * Port number, in network byte order.
189    */
190   uint16_t u6_port GNUNET_PACKED;
191
192 };
193
194 /**
195  * Our public key.
196  */
197 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
198
199 /**
200  * Our public key.
201  */
202 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
203
204 /**
205  * Our identity.
206  */
207 static struct GNUNET_PeerIdentity my_identity;
208
209 /**
210  * Our private key.
211  */
212 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
213
214 /**
215  * Peer's port
216  */
217 static long long unsigned int port;
218
219 /**
220  * Peer's port
221  */
222 static char * test_addr;
223
224 /**
225  * Our scheduler.
226  */
227 struct GNUNET_SCHEDULER_Handle *sched;
228
229 /**
230  * Our statistics handle.
231  */
232 struct GNUNET_STATISTICS_Handle *stats;
233
234
235 /**
236  * Our configuration.
237  */
238 const struct GNUNET_CONFIGURATION_Handle *cfg;
239
240 /**
241  * Number of neighbours we'd like to have.
242  */
243 static uint32_t max_connect_per_transport;
244
245 /**
246  * Environment for this plugin.
247  */
248 static struct GNUNET_TRANSPORT_PluginEnvironment env;
249
250 /**
251  *handle for the api provided by this plugin
252  */
253 static struct GNUNET_TRANSPORT_PluginFunctions *api;
254
255 /**
256  * ID of the task controlling the testcase timeout
257  */
258 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
259
260 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
261
262 //const struct GNUNET_PeerIdentity * p;
263
264 /**
265  * buffer for data to send
266  */
267 static struct HTTP_Message buffer_out;
268
269 /**
270  * buffer for data to recieve
271  */
272 static struct HTTP_Message buffer_in;
273
274
275 struct Plugin_Address * addr_head;
276
277 /**
278  * Did the test pass or fail?
279  */
280 static int fail_notify_address;
281 /**
282  * Did the test pass or fail?
283  */
284 static int fail_notify_address_count;
285
286 /**
287  * Did the test pass or fail?
288  */
289 static int fail_pretty_printer;
290
291 /**
292  * Did the test pass or fail?
293  */
294 static int fail_pretty_printer_count;
295
296 /**
297  * Did the test pass or fail?
298  */
299 static int fail_addr_to_str;
300
301 /**
302  * No. of msgs transmitted successfully to local addresses
303  */
304 static int fail_msgs_transmited_to_local_addrs;
305
306 /**
307  * Test: transmit msg of max. size
308  */
309 static int fail_msg_transmited_bigger_max_size;
310
311 /**
312  * Test: transmit msg of max. size
313  */
314 static int fail_msg_transmited_max_size;
315
316 /**
317  * Test: transmit 2 msgs. in in send operation
318  */
319 static int fail_multiple_msgs_in_transmission;
320
321 /**
322  * Test: connect to peer without peer identification
323  */
324 static struct HTTP_Transfer test_no_ident;
325
326 /**
327  * Test: connect to peer without peer identification
328  */
329 static struct HTTP_Transfer test_too_short_ident;
330
331 /**
332  * Test: connect to peer without peer identification
333  */
334 static struct HTTP_Transfer test_too_long_ident;
335
336 /**
337  * Test: connect to peer with valid peer identification
338  */
339 static struct HTTP_Transfer test_valid_ident;
340
341 /**
342  * Did the test pass or fail?
343  */
344 static int fail;
345
346 /**
347  * Number of local addresses
348  */
349 static unsigned int count_str_addr;
350
351 CURL *curl_handle;
352
353 /**
354  * cURL Multihandle
355  */
356 static CURLM *multi_handle;
357
358 /**
359  * The task sending data
360  */
361 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
362
363 /**
364  * Shutdown testcase
365  */
366 static void
367 shutdown_clean ()
368 {
369   struct Plugin_Address * cur;
370   struct Plugin_Address * tmp;
371
372   /* Evaluate results  */
373   fail = 0;
374   if ((fail_notify_address == GNUNET_YES) || (fail_pretty_printer == GNUNET_YES) || (fail_addr_to_str == GNUNET_YES))
375   {
376     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test plugin functions failed\n");
377     fail = 1;
378   }
379   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))
380   {
381     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test connect with wrong data failed\n");
382     fail = 1;
383   }
384   if ((fail_msgs_transmited_to_local_addrs != count_str_addr) || (fail_multiple_msgs_in_transmission != 2) || (fail_msg_transmited_max_size == GNUNET_YES))
385   {
386     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test sending with plugin failed\n");
387     fail = 1;
388   }
389   if (fail != 1)
390   {
391     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All tests successful\n");
392   }
393
394   api->disconnect(api->cls,&my_identity);
395
396   curl_multi_cleanup(multi_handle);
397
398   if (NULL != curl_handle)
399     curl_easy_cleanup (curl_handle);
400
401   /* cleaning addresses */
402   while (addr_head != NULL)
403   {
404     cur = addr_head;
405     tmp = addr_head->next;
406     GNUNET_free (addr_head->addr);
407     GNUNET_free (addr_head);
408     addr_head=tmp;
409   }
410
411   if (ti_send != GNUNET_SCHEDULER_NO_TASK)
412   {
413     GNUNET_SCHEDULER_cancel(sched,ti_send);
414     ti_send = GNUNET_SCHEDULER_NO_TASK;
415   }
416
417   if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
418   {
419     GNUNET_SCHEDULER_cancel(sched,http_task_send);
420     http_task_send = GNUNET_SCHEDULER_NO_TASK;
421   }
422
423   if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
424   {
425     GNUNET_SCHEDULER_cancel(sched,ti_timeout);
426     ti_timeout = GNUNET_SCHEDULER_NO_TASK;
427   }
428
429   GNUNET_free(test_addr);
430   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
431   GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
432
433   GNUNET_SCHEDULER_shutdown(sched);
434   GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
435
436   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
437   exit(fail);
438   return;
439 }
440
441
442 /**
443  * Continuation called after plugin send message
444  * @cls closure
445  * @target target
446  * @result GNUNET_OK or GNUNET_SYSERR
447  */
448
449 static void task_send_cont (void *cls,
450                             const struct GNUNET_PeerIdentity * target,
451                             int result)
452 {
453   struct Plugin_Address * tmp_addr;
454   tmp_addr = addr_head;
455
456   if ((cls == &fail_msg_transmited_bigger_max_size) && (result == GNUNET_SYSERR))
457   {
458     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message bigger max msg size was not sent!\n");
459     fail_msg_transmited_bigger_max_size = GNUNET_NO;
460     return;
461   }
462
463   if ((cls == &fail_msg_transmited_max_size) && (result == GNUNET_OK))
464   {
465     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message with max msg size succesfully sent!\n",fail_msgs_transmited_to_local_addrs);
466     fail_msg_transmited_max_size = GNUNET_NO;
467   }
468 }
469
470
471 static void run_connection_tests( int );
472
473 /**
474  * Recieves messages from plugin, in real world transport
475  */
476 static struct GNUNET_TIME_Relative
477 receive (void *cls,
478          const struct GNUNET_PeerIdentity * peer,
479          const struct GNUNET_MessageHeader * message,
480          uint32_t distance,
481          struct Session *session,
482          const char *sender_address,
483          uint16_t sender_address_len)
484 {
485   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));
486
487   if ((ntohs(message->type)>=10) && (ntohs(message->type)<=20))
488   {
489     fail_msgs_transmited_to_local_addrs++;
490     if (fail_msgs_transmited_to_local_addrs == count_str_addr)
491       run_connection_tests(2);
492   }
493
494   if ((ntohs(message->type)==40) || (ntohs(message->type)==41))
495   {
496     fail_multiple_msgs_in_transmission ++;
497   }
498
499   if (ntohs(message->size) == GNUNET_SERVER_MAX_MESSAGE_SIZE-1)
500   {
501     fail_msg_transmited_max_size = GNUNET_NO;
502     shutdown_clean();
503   }
504
505   return GNUNET_TIME_UNIT_ZERO;
506 }
507
508 static size_t send_function (void *stream, size_t size, size_t nmemb, void *ptr)
509 {
510   unsigned int len;
511
512   len = buffer_out.len;
513
514   if (( buffer_out.pos == len) || (len > (size * nmemb)))
515     return 0;
516   memcpy(stream, buffer_out.buf, len);
517   buffer_out.pos = len;
518   return len;
519 }
520
521 static size_t recv_function (void *ptr, size_t size, size_t nmemb, void *ctx)
522 {
523
524   if (buffer_in.pos + size * nmemb > buffer_in.size)
525     return 0;                   /* overflow */
526
527   buffer_in.len = size * nmemb;
528   memcpy (&buffer_in.buf[buffer_in.pos], ptr, size * nmemb);
529   buffer_in.pos += size * nmemb;
530   buffer_in.len = buffer_in.pos;
531   buffer_in.buf[buffer_in.pos] = '\0';
532   return buffer_in.pos;
533 }
534
535 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
536 {
537   struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
538   char * tmp;
539   unsigned int len = size * nmemb;
540
541   tmp = GNUNET_malloc (  len+1 );
542   memcpy(tmp,ptr,len);
543   if (tmp[len-2] == 13)
544     tmp[len-2]= '\0';
545 #if DEBUG_CURL
546   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
547 #endif
548   if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
549   {
550     res->http_result_code=100;
551   }
552   if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
553   {
554     res->http_result_code=200;
555   }
556   if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
557   {
558     res->http_result_code=400;
559   }
560   if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
561   {
562     res->http_result_code=404;
563   }
564   if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
565   {
566     res->http_result_code=413;
567   }
568
569   GNUNET_free (tmp);
570   return size * nmemb;
571 }
572
573 static size_t send_prepare( struct HTTP_Transfer * result);
574
575 static void run_connection_tests( );
576
577 static void send_execute (void *cls,
578              const struct GNUNET_SCHEDULER_TaskContext *tc)
579 {
580   struct HTTP_Transfer *res;
581
582   int running;
583   struct CURLMsg *msg;
584   CURLMcode mret;
585
586   res = (struct HTTP_Transfer *) cls;
587   http_task_send = GNUNET_SCHEDULER_NO_TASK;
588   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
589     return;
590
591   do
592     {
593       running = 0;
594       mret = curl_multi_perform (multi_handle, &running);
595       if (running == 0)
596         {
597           do
598             {
599
600               msg = curl_multi_info_read (multi_handle, &running);
601               if (msg == NULL)
602                 break;
603               /* get session for affected curl handle */
604               //cs = find_session_by_curlhandle (msg->easy_handle);
605               //GNUNET_assert ( cs != NULL );
606               switch (msg->msg)
607                 {
608
609                 case CURLMSG_DONE:
610                   if ( (msg->data.result != CURLE_OK) &&
611                        (msg->data.result != CURLE_GOT_NOTHING) )
612                     {
613
614                     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
615                                _("curl failed for `%s' at %s:%d: `%s'\n"),
616                                "curl_multi_perform",
617                                __FILE__,
618                                __LINE__,
619                                curl_easy_strerror (msg->data.result));
620                     /* sending msg failed*/
621                     curl_easy_cleanup(curl_handle);
622                     curl_handle=NULL;
623
624                     run_connection_tests(1);
625                     }
626                   if (res == &test_no_ident)
627                   {
628                     if  ((res->http_result_code==404) && (buffer_in.len==208))
629                     {
630                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test passed\n"));
631                       res->test_failed = GNUNET_NO;
632                     }
633                     else
634                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification: test failed\n"));
635                   }
636                   if (res == &test_too_short_ident)
637                   {
638                     if  ((res->http_result_code==404) && (buffer_in.len==208))
639                     {
640                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test passed\n"));
641                       res->test_failed = GNUNET_NO;
642                     }
643                     else
644                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too short peer identification: test failed\n"));
645                   }
646                   if (res == &test_too_long_ident)
647                   {
648                     if  ((res->http_result_code==404) && (buffer_in.len==208))
649                       {
650                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test passed\n"));
651                       res->test_failed = GNUNET_NO;
652                       }
653                     else
654                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification: test failed\n"));
655                   }
656                   if (res == &test_valid_ident)
657                   {
658                     if  ((res->http_result_code==200))
659                     {
660                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test passed\n"));
661                       res->test_failed = GNUNET_NO;
662                     }
663                     else
664                       GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification: test failed\n"));
665                   }
666                   curl_easy_cleanup(curl_handle);
667                   curl_handle=NULL;
668
669                   run_connection_tests(1);
670                   return;
671                 default:
672                   break;
673                 }
674
675             }
676           while ( (running > 0) );
677         }
678     }
679   while (mret == CURLM_CALL_MULTI_PERFORM);
680   send_prepare(cls);
681 }
682
683 /**
684  * Function setting up file descriptors and scheduling task to run
685  * @param ses session to send data to
686  * @return bytes sent to peer
687  */
688 static size_t send_prepare( struct HTTP_Transfer * result)
689 {
690   fd_set rs;
691   fd_set ws;
692   fd_set es;
693   int max;
694   struct GNUNET_NETWORK_FDSet *grs;
695   struct GNUNET_NETWORK_FDSet *gws;
696   long to;
697   CURLMcode mret;
698
699   max = -1;
700   FD_ZERO (&rs);
701   FD_ZERO (&ws);
702   FD_ZERO (&es);
703   mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
704   if (mret != CURLM_OK)
705     {
706       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
707                   _("%s failed at %s:%d: `%s'\n"),
708                   "curl_multi_fdset", __FILE__, __LINE__,
709                   curl_multi_strerror (mret));
710       return -1;
711     }
712   mret = curl_multi_timeout (multi_handle, &to);
713   if (mret != CURLM_OK)
714     {
715       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
716                   _("%s failed at %s:%d: `%s'\n"),
717                   "curl_multi_timeout", __FILE__, __LINE__,
718                   curl_multi_strerror (mret));
719       return -1;
720     }
721
722   grs = GNUNET_NETWORK_fdset_create ();
723   gws = GNUNET_NETWORK_fdset_create ();
724   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
725   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
726   http_task_send = GNUNET_SCHEDULER_add_select (sched,
727                                    GNUNET_SCHEDULER_PRIORITY_DEFAULT,
728                                    GNUNET_SCHEDULER_NO_TASK,
729                                    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
730                                    grs,
731                                    gws,
732                                    &send_execute,
733                                    result);
734   GNUNET_NETWORK_fdset_destroy (gws);
735   GNUNET_NETWORK_fdset_destroy (grs);
736
737   /* FIXME: return bytes REALLY sent */
738   return 0;
739 }
740
741 /**
742  * function to send data to server
743  */
744 static int send_data( struct HTTP_Transfer * result, char * url)
745 {
746
747   curl_handle = curl_easy_init();
748   if( NULL == curl_handle)
749   {
750     printf("easy_init failed \n");
751     return GNUNET_SYSERR;
752   }
753 #if DEBUG_CURL
754   curl_easy_setopt(put_curl_handle, CURLOPT_VERBOSE, 1L);
755 #endif
756   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
757   curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
758   curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
759   curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
760   curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &recv_function);
761   curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
762   curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &send_function);
763   curl_easy_setopt (curl_handle, CURLOPT_READDATA, result);
764   curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) buffer_out.len);
765   curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
766   curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
767
768   curl_multi_add_handle(multi_handle, curl_handle);
769
770   send_prepare(result);
771
772   return GNUNET_OK;
773 }
774
775 /**
776  * Plugin notifies transport (aka testcase) about its addresses
777  */
778 void
779 notify_address (void *cls,
780                 const char *name,
781                 const void *addr,
782                 uint16_t addrlen,
783                 struct GNUNET_TIME_Relative expires)
784 {
785   char address[INET6_ADDRSTRLEN];
786   unsigned int port;
787   struct Plugin_Address * pl_addr;
788   struct Plugin_Address * cur;
789
790   if (addrlen == (sizeof (struct IPv4HttpAddress)))
791     {
792       inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
793       port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
794     }
795   else if (addrlen == (sizeof (struct IPv6HttpAddress)))
796     {
797       inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
798       port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
799     }
800   else
801     {
802       GNUNET_break (0);
803       return;
804     }
805   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
806               _("Transport plugin notification for address: `%s':%u\n"),
807               address,
808               port);
809   pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
810   pl_addr->addrlen = addrlen;
811   pl_addr->addr = GNUNET_malloc(addrlen);
812   memcpy(pl_addr->addr,addr,addrlen);
813   pl_addr->next = NULL;
814
815   if ( NULL == addr_head)
816     {
817       addr_head = pl_addr;
818     }
819   else
820     {
821       cur = addr_head;
822       while (NULL != cur->next)
823         {
824           cur = cur->next;
825         }
826       cur->next = pl_addr;
827     }
828   fail_notify_address_count++;
829   fail_notify_address = GNUNET_NO;
830 }
831
832 /**
833  * Setup plugin environment
834  */
835 static void
836 setup_plugin_environment ()
837 {
838   env.cfg = cfg;
839   env.sched = sched;
840   env.stats = stats;
841   env.my_identity = &my_identity;
842   env.cls = &env;
843   env.receive = &receive;
844   env.notify_address = &notify_address;
845   env.max_connections = max_connect_per_transport;
846 }
847
848
849 /**
850  * Task shutting down testcase if it a timeout occurs
851  */
852 static void
853 task_timeout (void *cls,
854             const struct GNUNET_SCHEDULER_TaskContext *tc)
855 {
856   ti_timeout = GNUNET_SCHEDULER_NO_TASK;
857   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
858     return;
859
860   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
861   fail = GNUNET_YES;
862   shutdown_clean();
863   return;
864 }
865
866 static void pretty_printer_cb (void *cls,
867                                const char *address)
868 {
869   if (NULL==address)
870     return;
871   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
872   fail_pretty_printer_count++;
873 }
874
875 /**
876  * Runs every single test to test the plugin
877  */
878 static void run_connection_tests( int phase )
879 {
880
881   char * host_str = NULL;
882   /* resetting buffers */
883   buffer_in.size = HTTP_BUFFER_SIZE;
884   buffer_in.pos = 0;
885   buffer_in.len = 0;
886
887   buffer_out.size = HTTP_BUFFER_SIZE;
888   buffer_out.pos = 0;
889   buffer_out.len = 0;
890
891   if (test_no_ident.test_executed == GNUNET_NO)
892   {
893     /* Connecting to peer without identification */
894     char * ident = "";
895     host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
896     GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
897     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer without any peer identification.\n"));
898     test_no_ident.test_executed = GNUNET_YES;
899     send_data ( &test_no_ident, host_str);
900     GNUNET_free (host_str);
901     return;
902   }
903   if (test_too_short_ident.test_executed == GNUNET_NO)
904   {
905     char * ident = "AAAAAAAAAA";
906     /* Connecting to peer with too short 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 with too short peer identification.\n"));
910     test_too_short_ident.test_executed = GNUNET_YES;
911     send_data ( &test_too_short_ident, host_str);
912     GNUNET_free (host_str);
913     return;
914   }
915
916   if (test_too_long_ident.test_executed == GNUNET_NO)
917   {
918     char * ident = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
919
920     /* Connecting to peer with too long identification */
921     host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen (ident));
922     GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,ident);
923     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with too long peer identification.\n"));
924     test_too_long_ident.test_executed = GNUNET_YES;
925     send_data ( &test_too_long_ident, host_str);
926     GNUNET_free (host_str);
927     return;
928   }
929   if (test_valid_ident.test_executed == GNUNET_NO)
930   {
931     struct GNUNET_CRYPTO_HashAsciiEncoded ident;
932     GNUNET_CRYPTO_hash_to_enc(&my_identity.hashPubKey,&ident);
933     host_str = GNUNET_malloc (strlen ("http:///")+ 1 + strlen (test_addr)+ strlen ((char *) &ident));
934     GNUNET_asprintf (&host_str, "http://%s/%s",test_addr,(char *) &ident);
935
936     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connecting to peer with valid peer identification.\n"));
937     test_valid_ident.test_executed = GNUNET_YES;
938     send_data ( &test_valid_ident, host_str);
939     GNUNET_free (host_str);
940     return;
941   }
942
943   if (phase==1)
944   {
945     /* Using one of the addresses the plugin proposed */
946     GNUNET_assert (addr_head->addr != NULL);
947
948     struct Plugin_Address * tmp_addr;
949     struct GNUNET_MessageHeader msg;
950     char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
951     char address[INET6_ADDRSTRLEN];
952     unsigned int port;
953     unsigned int type = 10;
954
955     msg.size=htons(sizeof(struct GNUNET_MessageHeader));
956     tmp_addr = addr_head;
957     /* send a message to all addresses advertised by plugin */
958
959     int count = 0;
960     while (tmp_addr != NULL)
961     {
962       if (tmp_addr->addrlen == (sizeof (struct IPv4HttpAddress)))
963         {
964           inet_ntop(AF_INET, (struct in_addr *) tmp_addr->addr,address,INET_ADDRSTRLEN);
965           port = ntohs(((struct IPv4HttpAddress *) tmp_addr->addr)->u_port);
966           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address, port);
967         }
968       if (tmp_addr->addrlen == (sizeof (struct IPv6HttpAddress)))
969         {
970           inet_ntop(AF_INET6, (struct in6_addr *) tmp_addr->addr,address,INET6_ADDRSTRLEN);
971           port = ntohs(((struct IPv6HttpAddress *) tmp_addr->addr)->u6_port);
972           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending message to addres no. %u: `%s':%u\n", count,address,port);
973         }
974       msg.type=htons(type);
975       memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
976       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);
977       tmp_addr = tmp_addr->next;
978
979       count ++;
980       type ++;
981     }
982     return;
983   }
984
985   if (phase==2)
986   {
987     /* disconnect from peer, so new connections are created */
988     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Disconnect from peer: `%s'\n", GNUNET_i2s(&my_identity));
989     api->disconnect(api->cls, &my_identity);
990
991     struct GNUNET_MessageHeader msg;
992     char * tmp = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
993     /* send a multiple GNUNET_messages at a time*/
994     GNUNET_free(tmp);
995     tmp = GNUNET_malloc(4 * sizeof(struct GNUNET_MessageHeader));
996     struct GNUNET_MessageHeader * msg1 = (struct GNUNET_MessageHeader *) tmp;
997     msg1->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
998     msg1->type = htons(40);
999     struct GNUNET_MessageHeader * msg2 = &msg1[2];
1000     msg2->size = htons(2 * sizeof(struct GNUNET_MessageHeader));
1001     msg2->type = htons(41);
1002     api->send(api->cls, &my_identity, tmp, 4 * sizeof(struct GNUNET_MessageHeader), 0, TIMEOUT, NULL,addr_head->addr, addr_head->addrlen, GNUNET_YES, &task_send_cont, &fail_multiple_msgs_in_transmission);
1003
1004     /* send a message with size GNUNET_SERVER_MAX_MESSAGE_SIZE-1  */
1005     GNUNET_free(tmp);
1006     tmp = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE-1);
1007     uint16_t t = (uint16_t)GNUNET_SERVER_MAX_MESSAGE_SIZE-1;
1008     msg.size = htons(t);
1009     memcpy(tmp,&msg,sizeof(struct GNUNET_MessageHeader));
1010     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);
1011     GNUNET_free(tmp);
1012   }
1013   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No more tests to run\n");
1014 }
1015
1016
1017 /**
1018  * Runs the test.
1019  *
1020  * @param cls closure
1021  * @param s scheduler to use
1022  * @param c configuration to use
1023  */
1024 static void
1025 run (void *cls,
1026      struct GNUNET_SCHEDULER_Handle *s,
1027      char *const *args,
1028      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
1029 {
1030   char * libname;
1031   sched = s;
1032   cfg = c;
1033   char *keyfile;
1034   unsigned long long tneigh;
1035   struct Plugin_Address * cur;
1036   const char * addr_str;
1037
1038
1039   unsigned int suggest_res;
1040
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;
1046   fail_multiple_msgs_in_transmission = 0;
1047
1048   addr_head = NULL;
1049   count_str_addr = 0;
1050   /* parse configuration */
1051   if ((GNUNET_OK !=
1052        GNUNET_CONFIGURATION_get_value_number (c,
1053                                               "TRANSPORT",
1054                                               "NEIGHBOUR_LIMIT",
1055                                               &tneigh)) ||
1056       (GNUNET_OK !=
1057        GNUNET_CONFIGURATION_get_value_filename (c,
1058                                                 "GNUNETD",
1059                                                 "HOSTKEY", &keyfile)))
1060     {
1061       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1062                   _
1063                   ("Transport service is lacking key configuration settings.  Exiting.\n"));
1064       GNUNET_SCHEDULER_shutdown (s);
1065       fail = 1;
1066       return;
1067     }
1068
1069   if ((GNUNET_OK !=
1070       GNUNET_CONFIGURATION_get_value_number (cfg,
1071                                              "transport-http",
1072                                              "PORT",
1073                                              &port)) ||
1074      (port > 65535) || (port == 0))
1075   {
1076     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1077                      "http",
1078                      _
1079                      ("Require valid port number for transport plugin `%s' in configuration!\n"),
1080                      "transport-http");
1081   }
1082
1083   max_connect_per_transport = (uint32_t) tneigh;
1084   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
1085   GNUNET_free (keyfile);
1086   if (my_private_key == NULL)
1087     {
1088       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1089                   _("Transport service could not access hostkey.  Exiting.\n"));
1090       GNUNET_SCHEDULER_shutdown (s);
1091       fail = 1;
1092       return;
1093     }
1094
1095   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
1096   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &my_identity.hashPubKey);
1097
1098   /* assertions before start */
1099   GNUNET_assert ((port > 0) && (port <= 65535));
1100   GNUNET_assert(&my_public_key != NULL);
1101   GNUNET_assert(&my_identity.hashPubKey != NULL);
1102
1103   /* load plugins... */
1104   setup_plugin_environment ();
1105   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
1106   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
1107   api = GNUNET_PLUGIN_load (libname, &env);
1108   GNUNET_free (libname);
1109   if (api == NULL)
1110   {
1111     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1112                 _("Failed to load transport plugin for http\n"));
1113     fail = 1;
1114     return;
1115   }
1116
1117   ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
1118
1119   /* testing plugin functionality */
1120   GNUNET_assert (0!=fail_notify_address_count);
1121   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"),  fail_notify_address_count);
1122
1123   /* testing pretty printer with all addresses obtained from the plugin*/
1124   cur = addr_head;
1125   while (cur != NULL)
1126   {
1127
1128     api->address_pretty_printer (api->cls, "http",cur->addr,cur->addrlen, GNUNET_NO,TEST_TIMEOUT, &pretty_printer_cb,NULL);
1129     addr_str = api->address_to_string (api->cls, cur->addr, cur->addrlen);
1130     suggest_res = api->check_address (api->cls, cur->addr, cur->addrlen);
1131
1132     GNUNET_assert (GNUNET_OK == suggest_res);
1133     GNUNET_assert (NULL != addr_str);
1134     count_str_addr++;
1135     GNUNET_free ( (char *) addr_str);
1136     cur = cur->next;
1137   }
1138   GNUNET_assert (fail_pretty_printer_count > 0);
1139   GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
1140   GNUNET_assert (fail_pretty_printer_count==count_str_addr);
1141   fail_pretty_printer=GNUNET_NO;
1142   fail_addr_to_str=GNUNET_NO;
1143
1144   /* Suggesting addresses with wrong port*/
1145   struct IPv4HttpAddress failing_addr;
1146   failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1147   failing_addr.u_port = htons(0);
1148   suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv4HttpAddress));
1149   GNUNET_assert (GNUNET_SYSERR == suggest_res);
1150
1151   /* Suggesting addresses with wrong size*/
1152   failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1153   failing_addr.u_port = htons(0);
1154   suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv6HttpAddress));
1155   GNUNET_assert (GNUNET_SYSERR == suggest_res);
1156
1157   /* Suggesting addresses with wrong address*/
1158   failing_addr.ipv4_addr = htonl(INADDR_LOOPBACK);
1159   failing_addr.u_port = htons(12389);
1160   suggest_res = api->check_address (api->cls,&failing_addr,sizeof (struct IPv4HttpAddress));
1161   GNUNET_assert (GNUNET_SYSERR == suggest_res);
1162
1163   /* test sending to client */
1164   multi_handle = curl_multi_init();
1165
1166   /* Setting up buffers */
1167   buffer_in.size = HTTP_BUFFER_SIZE;
1168   buffer_in.pos = 0;
1169   buffer_in.len = 0;
1170
1171   buffer_out.size = HTTP_BUFFER_SIZE;
1172   buffer_out.pos = 0;
1173   buffer_out.len = 0;
1174
1175   /* Setting up connection tests */
1176
1177   /* Test: connecting without a peer identification */
1178   test_no_ident.test_executed = GNUNET_NO;
1179   test_no_ident.test_failed = GNUNET_YES;
1180
1181   /* Test: connecting with too short peer identification */
1182   test_too_short_ident.test_executed = GNUNET_NO;
1183   test_too_short_ident.test_failed = GNUNET_YES;
1184
1185   /* Test: connecting with too long peer identification */
1186   test_too_long_ident.test_executed = GNUNET_NO;
1187   test_too_long_ident.test_failed = GNUNET_YES;
1188
1189   /* Test: connecting with valid identification */
1190   test_valid_ident.test_executed = GNUNET_NO;
1191   test_valid_ident.test_failed = GNUNET_YES;
1192
1193   test_addr = (char *) api->address_to_string (api->cls,addr_head->addr,addr_head->addrlen);
1194   run_connection_tests(0);
1195
1196   /* testing finished */
1197
1198   return;
1199 }
1200
1201
1202 /**
1203  * The main function for the transport service.
1204  *
1205  * @param argc number of arguments from the command line
1206  * @param argv command line arguments
1207  * @return 0 ok, 1 on error
1208  */
1209 int
1210 main (int argc, char *const *argv)
1211 {
1212
1213   static struct GNUNET_GETOPT_CommandLineOption options[] = {
1214     GNUNET_GETOPT_OPTION_END
1215   };
1216   int ret;
1217   char *const argv_prog[] = {
1218     "test_plugin_transport_http",
1219     "-c",
1220     "test_plugin_transport_data_http.conf",
1221     "-L",
1222 #if VERBOSE
1223     "DEBUG",
1224 #else
1225     "WARNING",
1226 #endif
1227     NULL
1228   };
1229   GNUNET_log_setup ("test_plugin_transport_http",
1230 #if VERBOSE
1231                     "DEBUG",
1232 #else
1233                     "WARNING",
1234 #endif
1235                     NULL);
1236
1237   ret = (GNUNET_OK ==
1238          GNUNET_PROGRAM_run (5,
1239                              argv_prog,
1240                              "test_plugin_transport_http",
1241                              "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
1242
1243     GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
1244
1245   return fail;
1246 }
1247
1248 /* end of test_plugin_transport_http.c */