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