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