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