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