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