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