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