fixing memory leak
[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_getopt_lib.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_plugin_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_service_lib.h"
37 #include "plugin_transport.h"
38 #include "gnunet_statistics_service.h"
39 #include "transport.h"
40 #include <curl/curl.h>
41
42 #define VERBOSE GNUNET_NO
43 #define DEBUG GNUNET_NO
44 #define DEBUG_CURL GNUNET_NO
45
46 #define PLUGIN libgnunet_plugin_transport_template
47
48 /**
49  * How long until we give up on transmitting the message?
50  */
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
52
53 /**
54  * How long until we give up on transmitting the message?
55  */
56 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
57
58 /**
59  * How long between recieve and send?
60  */
61 #define WAIT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
62
63 /**
64  *  Message to send using http
65  */
66 struct HTTP_Message
67 {
68   char *buf;
69   size_t pos;
70   size_t size;
71   size_t len;
72 };
73
74 /**
75  * Our public key.
76  */
77 /* static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key; */
78
79 /**
80  * Our public key.
81  */
82 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
83
84 /**
85  * Our identity.
86  */
87 static struct GNUNET_PeerIdentity my_identity;
88
89 /**
90  * Our private key.
91  */
92 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
93
94
95 /**
96  * Our scheduler.
97  */
98 struct GNUNET_SCHEDULER_Handle *sched;
99
100 /**
101  * Our statistics handle.
102  */
103 struct GNUNET_STATISTICS_Handle *stats;
104
105
106 /**
107  * Our configuration.
108  */
109 const struct GNUNET_CONFIGURATION_Handle *cfg;
110
111 /**
112  * Number of neighbours we'd like to have.
113  */
114 static uint32_t max_connect_per_transport;
115
116 /**
117  * Environment for this plugin.
118  */
119 static struct GNUNET_TRANSPORT_PluginEnvironment env;
120
121 /**
122  *handle for the api provided by this plugin
123  */
124 static struct GNUNET_TRANSPORT_PluginFunctions *api;
125
126 /**
127  * ID of the task controlling the testcase timeout
128  */
129 static GNUNET_SCHEDULER_TaskIdentifier ti_timeout;
130
131 static GNUNET_SCHEDULER_TaskIdentifier ti_send;
132
133 const struct GNUNET_PeerIdentity * p;
134
135 /**
136  *  Struct for plugin addresses
137  */
138 struct Plugin_Address
139 {
140   /**
141    * Next field for linked list
142    */
143   struct Plugin_Address * next;
144
145   /**
146    * buffer containing data to send
147    */
148   void * addr;
149
150   /**
151    * amount of data to sent
152    */
153   size_t addrlen;
154 };
155
156 /**
157  *  Struct for plugin addresses
158  */
159 struct HTTP_Transfer
160 {
161   /**
162    * HTTP Header result for transfer
163    */
164   unsigned int http_result_code;
165
166   /**
167    * amount of bytes we recieved
168    */
169   size_t data_size;
170
171   unsigned char buf[2048];
172
173   /**
174    * amount of bytes we recieved
175    */
176   size_t pos;
177
178   size_t size;
179
180   unsigned int test_failed;
181
182 };
183
184 struct Plugin_Address * addr_head;
185
186 /**
187  * Did the test pass or fail?
188  */
189 static int fail_notify_address;
190 /**
191  * Did the test pass or fail?
192  */
193 static int fail_notify_address_count;
194
195 /**
196  * Did the test pass or fail?
197  */
198 static int fail_pretty_printer;
199
200 /**
201  * Did the test pass or fail?
202  */
203 static int fail_pretty_printer_count;
204
205 /**
206  * Did the test pass or fail?
207  */
208 static int fail_addr_to_str;
209
210 /**
211  * Did the test pass or fail?
212  */
213 static struct HTTP_Transfer testtransfer_no_ident;
214
215
216
217 /**
218  * Did the test pass or fail?
219  */
220 static int fail;
221
222 /**
223  * Recieved message already returned to sender?
224  */
225 static int sent;
226
227 CURL *curl_handle;
228
229 /**
230  * cURL Multihandle
231  */
232 static CURLM *multi_handle;
233
234 /**
235  * Test message to send
236  */
237 struct HTTP_Message * msg;
238
239 /**
240  * The task sending data
241  */
242 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
243
244 /**
245  * Shutdown testcase
246  */
247 static void
248 shutdown_clean ()
249 {
250   if ((fail_notify_address == GNUNET_NO) && (fail_pretty_printer == GNUNET_NO) && (fail_addr_to_str == GNUNET_NO) && (testtransfer_no_ident.test_failed == GNUNET_NO))
251     fail = 0;
252   else
253     fail = 1;
254
255   curl_multi_cleanup(multi_handle);
256
257   if (NULL != curl_handle)
258     curl_easy_cleanup (curl_handle);
259
260
261   if (ti_send != GNUNET_SCHEDULER_NO_TASK)
262   {
263     GNUNET_SCHEDULER_cancel(sched,ti_send);
264     ti_send = GNUNET_SCHEDULER_NO_TASK;
265   }
266
267   if (http_task_send != GNUNET_SCHEDULER_NO_TASK)
268   {
269     GNUNET_SCHEDULER_cancel(sched,http_task_send);
270     http_task_send = GNUNET_SCHEDULER_NO_TASK;
271   }
272
273   if (ti_timeout != GNUNET_SCHEDULER_NO_TASK)
274   {
275     GNUNET_SCHEDULER_cancel(sched,ti_timeout);
276     ti_timeout = GNUNET_SCHEDULER_NO_TASK;
277   }
278   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unloading http plugin\n");
279   GNUNET_assert (NULL == GNUNET_PLUGIN_unload ("libgnunet_plugin_transport_http", api));
280
281   GNUNET_SCHEDULER_shutdown(sched);
282
283   GNUNET_free(msg->buf);
284   GNUNET_free(msg);
285
286   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Exiting testcase\n");
287   exit(fail);
288   return;
289 }
290
291 /**
292  * Continuation called after plugin send message
293  * @cls closure
294  * @target target
295  * @result GNUNET_OK or GNUNET_SYSERR
296  */
297 static void task_send_cont (void *cls,
298                             const struct GNUNET_PeerIdentity * target,
299                             int result)
300 {
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message was sent!\n");
302   fail = GNUNET_NO;
303   shutdown_clean();
304 }
305
306 /**
307  * Task sending recieved message back to peer
308  * @cls closure
309  * @tc task context
310  */
311 static void
312 task_send (void *cls,
313             const struct GNUNET_SCHEDULER_TaskContext *tc)
314 {
315   ti_timeout = GNUNET_SCHEDULER_NO_TASK;
316   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
317     return;
318
319   if (GNUNET_YES==sent)
320     return;
321
322   struct GNUNET_MessageHeader * msg = cls;
323   unsigned int len = ntohs(msg->size);
324   const char * msgc = (const char *) msg;
325
326   api->send(api->cls, p, msgc, len, 0, TIMEOUT, NULL,NULL, 0, GNUNET_NO, &task_send_cont, NULL);
327   sent = GNUNET_YES;
328
329 }
330
331 /**
332  * Recieves messages from plugin, in real world transport
333  */
334 static struct GNUNET_TIME_Relative
335 receive (void *cls,
336          const struct GNUNET_PeerIdentity * peer,
337          const struct GNUNET_MessageHeader * message,
338          uint32_t distance,
339          struct Session *session,
340          const char *sender_address,
341          uint16_t sender_address_len)
342 {
343   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));
344
345   /* take recieved message and send it back to peer */
346   p = peer;
347   void * c = (void *) message;
348   ti_send =GNUNET_SCHEDULER_add_delayed (sched, WAIT_INTERVALL, &task_send, c);
349
350   return GNUNET_TIME_UNIT_ZERO;
351 }
352
353  int done;
354 static size_t
355 putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
356 {
357   unsigned int len;
358   struct HTTP_Message  * cbc = ptr;
359
360   len = cbc->len;
361
362   if (( cbc->pos == len) && (len < (size * nmemb)))
363     return 0;
364   memcpy(stream, cbc->buf, len);
365   cbc->pos = len;
366   return len;
367 }
368
369 static size_t copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
370 {
371   struct HTTP_Transfer * res = (struct HTTP_Transfer *) ctx;
372
373   res->data_size = size * nmemb;
374
375   if (res->pos + size * nmemb > res->size)
376     return 0;                   /* overflow */
377   memcpy (&res->buf[res->pos], ptr, size * nmemb);
378   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Send completed. %s\n",res->buf);
379   res->pos += size * nmemb;
380   return size * nmemb;
381 }
382
383 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
384 {
385   struct HTTP_Transfer * res = (struct HTTP_Transfer *) stream;
386   char * tmp;
387   unsigned int len = size * nmemb;
388
389   tmp = GNUNET_malloc (  len+1 );
390   memcpy(tmp,ptr,len);
391   if (tmp[len-2] == 13)
392     tmp[len-2]= '\0';
393   if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
394     {
395     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "404\n");
396     res->http_result_code=404;
397     }
398
399   GNUNET_free (tmp);
400   return size * nmemb;
401 }
402
403 static size_t send_prepare( struct HTTP_Transfer * result);
404
405 static void send_execute (void *cls,
406              const struct GNUNET_SCHEDULER_TaskContext *tc)
407 {
408   struct HTTP_Transfer *res;
409
410   int running;
411   struct CURLMsg *msg;
412   CURLMcode mret;
413
414   res = (struct HTTP_Transfer *) cls;
415   http_task_send = GNUNET_SCHEDULER_NO_TASK;
416   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
417     return;
418
419   do
420     {
421       running = 0;
422       mret = curl_multi_perform (multi_handle, &running);
423       if (running == 0)
424         {
425           do
426             {
427
428               msg = curl_multi_info_read (multi_handle, &running);
429               GNUNET_break (msg != NULL);
430               if (msg == NULL)
431                 break;
432               /* get session for affected curl handle */
433               //cs = find_session_by_curlhandle (msg->easy_handle);
434               //GNUNET_assert ( cs != NULL );
435               switch (msg->msg)
436                 {
437
438                 case CURLMSG_DONE:
439                   if ( (msg->data.result != CURLE_OK) &&
440                        (msg->data.result != CURLE_GOT_NOTHING) )
441                     {
442
443                     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
444                                _("curl failed for `%s' at %s:%d: `%s'\n"),
445                                "curl_multi_perform",
446                                __FILE__,
447                                __LINE__,
448                                curl_easy_strerror (msg->data.result));
449                     /* sending msg failed*/
450                     }
451                   else
452                     {
453                     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send completed with code %u\n", res->data_size);
454                     /* sending completed */
455                     }
456                   if ( (cls == &testtransfer_no_ident) && (res->http_result_code==404) && (res->data_size==208))
457                       res->test_failed = GNUNET_NO;
458
459                   curl_easy_cleanup(curl_handle);
460                   curl_handle=NULL;
461                   shutdown_clean();
462                   return;
463                 default:
464                   break;
465                 }
466
467             }
468           while ( (running > 0) );
469         }
470     }
471   while (mret == CURLM_CALL_MULTI_PERFORM);
472   send_prepare(cls);
473 }
474
475 /**
476  * Function setting up file descriptors and scheduling task to run
477  * @param ses session to send data to
478  * @return bytes sent to peer
479  */
480 static size_t send_prepare( struct HTTP_Transfer * result)
481 {
482   fd_set rs;
483   fd_set ws;
484   fd_set es;
485   int max;
486   struct GNUNET_NETWORK_FDSet *grs;
487   struct GNUNET_NETWORK_FDSet *gws;
488   long to;
489   CURLMcode mret;
490
491   max = -1;
492   FD_ZERO (&rs);
493   FD_ZERO (&ws);
494   FD_ZERO (&es);
495   mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
496   if (mret != CURLM_OK)
497     {
498       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499                   _("%s failed at %s:%d: `%s'\n"),
500                   "curl_multi_fdset", __FILE__, __LINE__,
501                   curl_multi_strerror (mret));
502       return -1;
503     }
504   mret = curl_multi_timeout (multi_handle, &to);
505   if (mret != CURLM_OK)
506     {
507       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
508                   _("%s failed at %s:%d: `%s'\n"),
509                   "curl_multi_timeout", __FILE__, __LINE__,
510                   curl_multi_strerror (mret));
511       return -1;
512     }
513
514   grs = GNUNET_NETWORK_fdset_create ();
515   gws = GNUNET_NETWORK_fdset_create ();
516   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
517   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
518   http_task_send = GNUNET_SCHEDULER_add_select (sched,
519                                    GNUNET_SCHEDULER_PRIORITY_DEFAULT,
520                                    GNUNET_SCHEDULER_NO_TASK,
521                                    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
522                                    grs,
523                                    gws,
524                                    &send_execute,
525                                    result);
526   GNUNET_NETWORK_fdset_destroy (gws);
527   GNUNET_NETWORK_fdset_destroy (grs);
528
529   /* FIXME: return bytes REALLY sent */
530   return 0;
531 }
532
533 /**
534  * function to send data to server
535  */
536 static int send_data(struct HTTP_Message *msg, struct HTTP_Transfer * result, char * url)
537 {
538
539   curl_handle = curl_easy_init();
540   if( NULL == curl_handle)
541   {
542     printf("easy_init failed \n");
543     return GNUNET_SYSERR;
544   }
545 #if DEBUG_CURL
546   curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);
547 #endif
548   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
549   curl_easy_setopt(curl_handle, CURLOPT_PUT, 1L);
550   curl_easy_setopt (curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
551   curl_easy_setopt (curl_handle, CURLOPT_WRITEHEADER, result);
552   curl_easy_setopt (curl_handle, CURLOPT_WRITEFUNCTION, &copyBuffer);
553   curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, result);
554   curl_easy_setopt (curl_handle, CURLOPT_READFUNCTION, &putBuffer);
555   curl_easy_setopt (curl_handle, CURLOPT_READDATA, msg);
556   curl_easy_setopt(curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
557   curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 30);
558   curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
559
560   curl_multi_add_handle(multi_handle, curl_handle);
561
562   send_prepare(result);
563
564   return GNUNET_OK;
565 }
566
567 /**
568  * Network format for IPv4 addresses.
569  */
570 struct IPv4HttpAddress
571 {
572   /**
573    * IPv4 address, in network byte order.
574    */
575   uint32_t ipv4_addr;
576
577   /**
578    * Port number, in network byte order.
579    */
580   uint16_t u_port;
581
582 };
583
584
585 /**
586  * Network format for IPv6 addresses.
587  */
588 struct IPv6HttpAddress
589 {
590   /**
591    * IPv6 address.
592    */
593   struct in6_addr ipv6_addr;
594
595   /**
596    * Port number, in network byte order.
597    */
598   uint16_t u6_port;
599
600 };
601
602 /**
603  * Plugin notifies transport (aka testcase) about its addresses
604  */
605 void
606 notify_address (void *cls,
607                 const char *name,
608                 const void *addr,
609                 uint16_t addrlen,
610                 struct GNUNET_TIME_Relative expires)
611 {
612   char address[INET6_ADDRSTRLEN];
613   unsigned int port;
614   struct Plugin_Address * pl_addr;
615   struct Plugin_Address * cur;
616
617   if (addrlen == (sizeof (struct IPv4HttpAddress)))
618     {
619       inet_ntop(AF_INET, (struct in_addr *) addr,address,INET_ADDRSTRLEN);
620       port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
621     }
622   if (addrlen == (sizeof (struct IPv6HttpAddress)))
623     {
624       inet_ntop(AF_INET6, (struct in6_addr *) addr,address,INET6_ADDRSTRLEN);
625       port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
626     }
627   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
628               _("Transport plugin notification for address: `%s':%u\n"),
629               address,
630               port);
631   pl_addr = GNUNET_malloc (sizeof (struct Plugin_Address) );
632   pl_addr->addrlen = addrlen;
633   pl_addr->addr = GNUNET_malloc(addrlen);
634   memcpy(pl_addr->addr,addr,addrlen);
635   pl_addr->next = NULL;
636
637   if ( NULL == addr_head)
638     {
639       addr_head = pl_addr;
640     }
641   else
642     {
643       cur = addr_head;
644       while (NULL != cur->next)
645         {
646           cur = cur->next;
647         }
648       cur->next = pl_addr;
649     }
650   fail_notify_address_count++;
651   fail_notify_address = GNUNET_NO;
652 }
653
654 /**
655  * Setup plugin environment
656  */
657 static void
658 setup_plugin_environment ()
659 {
660   env.cfg = cfg;
661   env.sched = sched;
662   env.stats = stats;
663   env.my_identity = &my_identity;
664   env.cls = &env;
665   env.receive = &receive;
666   env.notify_address = &notify_address;
667   env.max_connections = max_connect_per_transport;
668 }
669
670
671 /**
672  * Task shutting down testcase if it a timeout occurs
673  */
674 static void
675 task_timeout (void *cls,
676             const struct GNUNET_SCHEDULER_TaskContext *tc)
677 {
678   ti_timeout = GNUNET_SCHEDULER_NO_TASK;
679   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
680     return;
681
682   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testcase timeout\n");
683   fail = GNUNET_YES;
684   shutdown_clean();
685   return;
686 }
687
688 static void pretty_printer_cb (void *cls,
689                                const char *address)
690 {
691   if (NULL==address)
692     return;
693   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Plugin returned pretty address: `%s'\n",address);
694   fail_pretty_printer_count++;
695 }
696
697
698 /**
699  * Runs the test.
700  *
701  * @param cls closure
702  * @param s scheduler to use
703  * @param c configuration to use
704  */
705 static void
706 run (void *cls,
707      struct GNUNET_SCHEDULER_Handle *s,
708      char *const *args,
709      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
710 {
711   char * libname;
712   sched = s;
713   cfg = c;
714   char *keyfile;
715   unsigned long long tneigh;
716   struct Plugin_Address * cur;
717   struct Plugin_Address * tmp;
718   const char * addr_str;
719   unsigned int count_str_addr;
720   unsigned int suggest_res;
721   unsigned int res;
722
723   fail_pretty_printer = GNUNET_YES;
724   fail_notify_address = GNUNET_YES;
725   fail_addr_to_str = GNUNET_YES;
726
727   addr_head = NULL;
728   count_str_addr = 0;
729   /* parse configuration */
730   if ((GNUNET_OK !=
731        GNUNET_CONFIGURATION_get_value_number (c,
732                                               "TRANSPORT",
733                                               "NEIGHBOUR_LIMIT",
734                                               &tneigh)) ||
735       (GNUNET_OK !=
736        GNUNET_CONFIGURATION_get_value_filename (c,
737                                                 "GNUNETD",
738                                                 "HOSTKEY", &keyfile)))
739     {
740       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
741                   _
742                   ("Transport service is lacking key configuration settings.  Exiting.\n"));
743       GNUNET_SCHEDULER_shutdown (s);
744       fail = 1;
745       return;
746     }
747   max_connect_per_transport = (uint32_t) tneigh;
748   my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
749   GNUNET_free (keyfile);
750   if (my_private_key == NULL)
751     {
752       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
753                   _("Transport service could not access hostkey.  Exiting.\n"));
754       GNUNET_SCHEDULER_shutdown (s);
755       fail = 1;
756       return;
757     }
758   GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
759   GNUNET_CRYPTO_hash (&my_public_key,
760                       sizeof (my_public_key), &my_identity.hashPubKey);
761
762   /* load plugins... */
763   setup_plugin_environment ();
764   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading HTTP transport plugin `%s'\n"),"libgnunet_plugin_transport_http");
765   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_http");
766   api = GNUNET_PLUGIN_load (libname, &env);
767   GNUNET_free (libname);
768   if (api == NULL)
769   {
770     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
771                 _("Failed to load transport plugin for http\n"));
772     fail = 1;
773     return;
774   }
775
776   ti_timeout = GNUNET_SCHEDULER_add_delayed (sched, TEST_TIMEOUT, &task_timeout, NULL);
777
778   /* testing plugin functionality */
779   GNUNET_assert (0!=fail_notify_address_count);
780   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transport plugin returned %u addresses to connect to\n"),  fail_notify_address_count);
781
782   /* testing pretty printer with all addresses obtained from the plugin*/
783   while (addr_head != NULL)
784   {
785     cur = addr_head;
786
787     api->address_pretty_printer (NULL,"http",cur->addr,cur->addrlen,GNUNET_NO,TEST_TIMEOUT,&pretty_printer_cb,NULL);
788     addr_str = api->address_to_string (NULL,cur->addr,cur->addrlen);
789     suggest_res = api->check_address (NULL,cur->addr,cur->addrlen);
790
791     GNUNET_assert (GNUNET_OK == suggest_res);
792     GNUNET_assert (NULL != addr_str);
793     count_str_addr++;
794
795     tmp = addr_head->next;
796     GNUNET_free (addr_head->addr);
797     GNUNET_free (addr_head);
798     GNUNET_free ((char *) addr_str);
799     addr_head=tmp;
800   }
801   GNUNET_assert (fail_pretty_printer_count==fail_notify_address_count);
802   GNUNET_assert (fail_pretty_printer_count==count_str_addr);
803   fail_pretty_printer=GNUNET_NO;
804   fail_addr_to_str=GNUNET_NO;
805
806   /* Suggesting addresses with wrong port*/
807   struct IPv4HttpAddress failing_addr;
808   failing_addr.ipv4_addr = INADDR_LOOPBACK;
809   failing_addr.u_port = 0;
810   suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
811   GNUNET_assert (GNUNET_SYSERR == suggest_res);
812
813   /* Suggesting addresses with wrong size*/
814   failing_addr.ipv4_addr = INADDR_LOOPBACK;
815   failing_addr.u_port = 0;
816   suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv6HttpAddress));
817   GNUNET_assert (GNUNET_SYSERR == suggest_res);
818
819   /* Suggesting addresses with wrong address*/
820   failing_addr.ipv4_addr = 0;
821   failing_addr.u_port = 12389;
822   suggest_res = api->check_address (NULL,&failing_addr,sizeof (struct IPv4HttpAddress));
823   GNUNET_assert (GNUNET_SYSERR == suggest_res);
824
825   /* test sending to client */
826   multi_handle = curl_multi_init();
827
828   /*building messages */
829   msg = GNUNET_malloc (sizeof (struct HTTP_Message));
830   msg->size = 2048;
831   msg->pos = 0;
832   msg->buf = GNUNET_malloc (2048);
833   testtransfer_no_ident.size=2048;
834   testtransfer_no_ident.test_failed = GNUNET_YES;
835
836   /* Connecting to peer without identification */
837   res = send_data (msg, &testtransfer_no_ident, "http://localhost:12389/");
838
839   /* Add more tests */
840
841   /* testing finished */
842   return;
843 }
844
845
846 /**
847  * The main function for the transport service.
848  *
849  * @param argc number of arguments from the command line
850  * @param argv command line arguments
851  * @return 0 ok, 1 on error
852  */
853 int
854 main (int argc, char *const *argv)
855 {
856
857   static struct GNUNET_GETOPT_CommandLineOption options[] = {
858     GNUNET_GETOPT_OPTION_END
859   };
860   int ret;
861   char *const argv_prog[] = {
862     "test_plugin_transport_http",
863     "-c",
864     "test_plugin_transport_data_http.conf",
865     "-L",
866 #if VERBOSE
867     "DEBUG",
868 #else
869     "WARNING",
870 #endif
871     NULL
872   };
873   GNUNET_log_setup ("test_plugin_transport_http",
874 #if VERBOSE
875                     "DEBUG",
876 #else
877                     "WARNING",
878 #endif
879                     NULL);
880
881   ret = (GNUNET_OK ==
882          GNUNET_PROGRAM_run (5,
883                              argv_prog,
884                              "test_plugin_transport_http",
885                              "testcase", options, &run, NULL)) ? GNUNET_NO : GNUNET_YES;
886
887     GNUNET_DISK_directory_remove ("/tmp/test_plugin_transport_http");
888
889   return fail;
890 }
891
892 /* end of test_plugin_transport_http.c */