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