(no commit message)
[oweals/gnunet.git] / src / transport / test_transport_api_unreliability.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file transport/test_transport_api_unreliability.c
22  * @brief test case for transports; ensures messages get
23  *        through, regardless of order
24  *
25  * This test case serves as a base for unreliable
26  * transport test cases to check that the transports
27  * achieve reliable message delivery.
28  */
29 #include "platform.h"
30 #include "gnunet_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gauger.h"
39 #include "transport.h"
40 #include "transport-testing.h"
41
42 #define VERBOSE GNUNET_NO
43
44 #define VERBOSE_ARM GNUNET_NO
45
46 #define START_ARM GNUNET_YES
47
48 /**
49  * Note that this value must not significantly exceed
50  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
51  * messages may be dropped even for a reliable transport.
52  */
53 #define TOTAL_MSGS (80000 * 3) /* Total messages should be divisible by 8, so we can make a nice bitmap */
54
55 /**
56  * How long until we give up on transmitting the message?
57  */
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
59
60 #define UNRELIABLE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
61
62 #define MTYPE 12345
63
64 static struct PeerContext p1;
65
66 static struct PeerContext p2;
67
68 static int ok;
69
70 static int is_tcp;
71
72 static int is_tcp_nat;
73
74 static int is_http;
75
76 static int is_https;
77
78 static int is_udp;
79
80 static int is_unix;
81
82 static int is_wlan;
83
84 static int connected;
85
86 static unsigned long long total_bytes;
87
88 static struct GNUNET_TIME_Absolute start_time;
89
90 static GNUNET_SCHEDULER_TaskIdentifier die_task;
91
92 static GNUNET_SCHEDULER_TaskIdentifier tct;
93
94 static char *key_file_p1;
95 static char *cert_file_p1;
96
97 static char *key_file_p2;
98 static char *cert_file_p2;
99
100 static char *test_name;
101
102 static char bitmap[TOTAL_MSGS / 8];
103
104 static int msg_scheduled;
105 static int msg_sent;
106 static int msg_recv_expected;
107 static int msg_recv;
108
109 /**
110  * Sets a bit active in the bitmap.
111  *
112  * @param bitIdx which bit to set
113  */
114 static void
115 set_bit (unsigned int bitIdx)
116 {
117   size_t arraySlot;
118   unsigned int targetBit;
119   if (bitIdx >= sizeof(bitmap) * 8)
120     {
121       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n", bitIdx, sizeof(bitmap) * 8);
122       return;
123     }
124   arraySlot = bitIdx / 8;
125   targetBit = (1L << (bitIdx % 8));
126   bitmap[arraySlot] |= targetBit;
127 }
128
129 /**
130  * Obtain a bit from bitmap.
131  * @param map the bitmap
132  * @param bit index from bitmap
133  *
134  * @return Bit \a bit from hashcode \a code
135  */
136 int
137 get_bit (const char *map, unsigned int bit)
138 {
139   if (bit >= TOTAL_MSGS)
140     {
141       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit, sizeof(bitmap) * 8);
142       return 0;
143     }
144   return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
145 }
146
147 static void
148 end ()
149 {
150   unsigned long long delta;
151   int i;
152   int result;
153   char *value_name;
154
155   result = 0;
156   for (i = 0; i < TOTAL_MSGS; i++)
157     {
158       if (get_bit(bitmap, i) == 0)
159         {
160           GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
161           result = -1;
162         }
163     }
164
165   GNUNET_SCHEDULER_cancel (die_task);
166   die_task = GNUNET_SCHEDULER_NO_TASK;
167 #if VERBOSE
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
169 #endif
170   GNUNET_TRANSPORT_disconnect (p1.th);
171   GNUNET_TRANSPORT_disconnect (p2.th);
172 #if VERBOSE
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174               "Transports disconnected, returning success!\n");
175 #endif
176   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
177   GNUNET_asprintf(&value_name, "unreliable_%s", test_name);
178   GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
179   GNUNET_free(value_name);
180   fprintf (stderr,
181            "\nThroughput was %llu kb/s\n",
182            total_bytes * 1000 / 1024 / delta);
183   ok = result;
184
185 }
186
187 static void
188 end_unreliably ()
189 {
190   unsigned long long delta;
191   int i;
192   int num_failed;
193   char *value_name;
194   num_failed = 0;
195   for (i = 0; i < TOTAL_MSGS; i++)
196     {
197       if (get_bit(bitmap, i) == 0)
198         {
199           GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Did not receive message %d\n", i);
200           num_failed++;
201         }
202     }
203
204   die_task = GNUNET_SCHEDULER_NO_TASK;
205 #if VERBOSE
206   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
207 #endif
208   GNUNET_TRANSPORT_disconnect (p1.th);
209   GNUNET_TRANSPORT_disconnect (p2.th);
210 #if VERBOSE
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212               "Transports disconnected, returning success!\n");
213 #endif
214   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
215   fprintf (stderr,
216            "\nThroughput was %llu kb/s\n",
217            total_bytes * 1000 / 1024 / delta);
218   GNUNET_asprintf(&value_name, "unreliable_%s", test_name);
219   GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
220   GNUNET_free(value_name);
221   GNUNET_asprintf(&value_name, "unreliable_failed_%s", test_name);
222   GAUGER ("TRANSPORT", value_name, (int)num_failed, "msgs");
223   GNUNET_free(value_name);
224   GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Had %d failed messages!\n", num_failed);
225   ok = 0;
226
227 }
228
229
230
231 static void
232 stop_arm (struct PeerContext *p)
233 {
234 #if START_ARM
235   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
236     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
237   GNUNET_OS_process_wait (p->arm_proc);
238   GNUNET_OS_process_close (p->arm_proc);
239   p->arm_proc = NULL;
240 #endif
241   GNUNET_CONFIGURATION_destroy (p->cfg);
242 }
243
244
245 static void
246 end_badly (void *cls,
247            const struct GNUNET_SCHEDULER_TaskContext *tc)
248 {
249   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250               "Reliability failed: Last message sent %u Next message scheduled %u Last message received %u Message expected %u\n", 
251               msg_sent,
252               msg_scheduled,
253               msg_recv, 
254               msg_recv_expected);
255   GNUNET_break (0);
256   GNUNET_TRANSPORT_disconnect (p1.th);
257   GNUNET_TRANSPORT_disconnect (p2.th);
258
259   if (GNUNET_SCHEDULER_NO_TASK != tct)
260     {
261       GNUNET_SCHEDULER_cancel (tct);
262       tct = GNUNET_SCHEDULER_NO_TASK;
263     }
264   ok = 1;
265 }
266
267
268 struct TestMessage
269 {
270   struct GNUNET_MessageHeader header;
271   uint32_t num;
272 };
273
274
275 static unsigned int
276 get_size (unsigned int iter)
277 {
278   unsigned int ret;
279
280   if (iter < 60000)
281     return iter + sizeof (struct TestMessage);
282   ret = (iter * iter * iter);
283   return sizeof (struct TestMessage) + (ret % 60000);
284 }
285
286
287 static void
288 notify_receive (void *cls,
289                 const struct GNUNET_PeerIdentity *peer,
290                 const struct GNUNET_MessageHeader *message,
291                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
292                 uint32_t ats_count)
293 {
294   static int n;
295   unsigned int s;
296   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
297   const struct TestMessage *hdr;
298
299   hdr = (const struct TestMessage*) message;
300
301   if (MTYPE != ntohs (message->type))
302     return;
303   msg_recv_expected = n;
304   msg_recv = ntohl(hdr->num);
305   s = get_size (ntohl(hdr->num));
306
307   if (ntohs (message->size) != s)
308     {
309       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310                   "Expected message %u of size %u, got %u bytes of message %u\n",
311                   ntohl(hdr->num), s,
312                   ntohs (message->size),
313                   ntohl (hdr->num));
314       GNUNET_SCHEDULER_cancel (die_task);
315       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
316       return;
317     }
318
319   memset (cbuf, ntohl(hdr->num), s - sizeof (struct TestMessage));
320   if (0 != memcmp (cbuf,
321                    &hdr[1],
322                    s - sizeof (struct TestMessage)))
323     {
324       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
325                   "Expected message %u with bits %u, but body did not match\n",
326                   ntohl(hdr->num), (unsigned char) n);
327       GNUNET_SCHEDULER_cancel (die_task);
328       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
329       return;
330     }
331 #if VERBOSE
332   if (ntohl(hdr->num) % 5 == 0)
333     {
334       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335                   "Got message %u of size %u\n",
336                   ntohl (hdr->num),
337                   ntohs (message->size));
338     }
339 #endif
340   n++;
341   set_bit(ntohl(hdr->num));
342   if (0 == (n % (5000)))
343     {
344       fprintf (stderr, ".");
345       GNUNET_SCHEDULER_cancel (die_task);
346       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
347                                                &end_badly,
348                                                NULL);
349     }
350   if (n == TOTAL_MSGS)
351     end ();
352 }
353
354
355 static size_t
356 notify_ready (void *cls, size_t size, void *buf)
357 {
358   static int n;
359   char *cbuf = buf;
360   struct TestMessage hdr;
361   unsigned int s;
362   unsigned int ret;
363
364   if (buf == NULL)
365     {
366       GNUNET_break (0);
367       ok = 42;
368       return 0;
369     }
370   ret = 0;
371   s = get_size (n);
372   GNUNET_assert (size >= s);
373   GNUNET_assert (buf != NULL);
374   cbuf = buf;
375   do
376     {
377       hdr.header.size = htons (s);
378       hdr.header.type = htons (MTYPE);
379       hdr.num = htonl (n);
380       msg_sent = n;
381       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
382       ret += sizeof (struct TestMessage);
383       memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
384       ret += s - sizeof (struct TestMessage);
385 #if VERBOSE
386       if (n % 5000 == 0)
387         {
388           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389                       "Sending message %u of size %u\n",
390                       n,
391                       s);
392         }
393 #endif
394       n++;
395       s = get_size (n);
396       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
397         break; /* sometimes pack buffer full, sometimes not */
398     }
399   while (size - ret >= s);
400   if (n < TOTAL_MSGS)
401   {
402     GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
403                                             &p1.id,
404                                             s, 0, TIMEOUT,
405                                             &notify_ready,
406                                             NULL);
407     msg_scheduled = n;
408   }
409   else
410     {
411       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent!!\n");
412       GNUNET_SCHEDULER_cancel(die_task);
413       die_task = GNUNET_SCHEDULER_add_delayed (UNRELIABLE_TIMEOUT, &end_unreliably, NULL);
414     }
415   if (n % 5000 == 0)
416     {
417       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418                   "Returning total message block of size %u\n",
419                   ret);
420     }
421   total_bytes += ret;
422   return ret;
423 }
424
425
426
427 static void
428 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
429 {
430   connected--;
431 #if VERBOSE
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433               "Peer `%4s' disconnected (%p)!\n",
434               GNUNET_i2s (peer), cls);
435 #endif
436 }
437
438
439
440 static void
441 exchange_hello_last (void *cls,
442                      const struct GNUNET_MessageHeader *message)
443 {
444   struct PeerContext *me = cls;
445
446   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447               "Exchanging HELLO of size %d with peer (%s)!\n", 
448               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
449               GNUNET_i2s (&me->id));
450   GNUNET_assert (message != NULL);
451   GNUNET_assert (GNUNET_OK ==
452                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
453                                       message, &me->id));
454   GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
455 }
456
457
458
459 static void
460 exchange_hello (void *cls,
461                 const struct GNUNET_MessageHeader *message)
462 {
463   struct PeerContext *me = cls;
464
465   GNUNET_assert (message != NULL);
466   GNUNET_assert (GNUNET_OK ==
467                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
468                                       message, &me->id));
469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470               "Exchanging HELLO of size %d from peer %s!\n", 
471               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
472               GNUNET_i2s (&me->id));
473   GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
474 }
475
476
477 static void
478 notify_connect (void *cls,
479                 const struct GNUNET_PeerIdentity *peer,
480                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
481                 uint32_t ats_count)
482 {
483   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484               "Peer `%4s' connected to us (%p)!\n", 
485               GNUNET_i2s (peer), 
486               cls);
487   if (cls == &p1)
488     {
489       GNUNET_TRANSPORT_set_quota (p1.th,
490                                   &p2.id,
491                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
492                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
493       start_time = GNUNET_TIME_absolute_get ();
494       connected++;
495     }
496   else
497     {
498       GNUNET_TRANSPORT_set_quota (p2.th,
499                                   &p1.id,
500                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
501                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
502       connected++;
503     }
504   if (2 == connected)
505     {
506       GNUNET_SCHEDULER_cancel (die_task);
507       GNUNET_SCHEDULER_cancel (tct);
508       tct = GNUNET_SCHEDULER_NO_TASK;
509       GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
510       GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
511       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
512                                                &end_badly, NULL);
513       GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
514                                               &p1.id,
515                                               get_size (0), 0, TIMEOUT,
516                                               &notify_ready,
517                                               NULL);
518       
519     }
520 }
521
522
523 static void
524 setup_peer (struct PeerContext *p, const char *cfgname)
525 {
526   p->cfg = GNUNET_CONFIGURATION_create ();
527
528   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
529   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
530       GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
531   GNUNET_DISK_directory_remove (p->servicehome);
532
533
534 #if START_ARM
535   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
536                                         "gnunet-service-arm",
537                                         "gnunet-service-arm",
538 #if VERBOSE_ARM
539                                         "-L", "DEBUG",
540 #endif
541                                         "-c", cfgname, NULL);
542 #endif
543
544   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
545                                     p,
546                                     &notify_receive,
547                                     &notify_connect,
548                                     &notify_disconnect);
549   GNUNET_assert (p->th != NULL);
550 }
551
552
553 /**
554  * Return the actual path to a file found in the current
555  * PATH environment variable.
556  *
557  * @param binary the name of the file to find
558  */
559 static char *
560 get_path_from_PATH (char *binary)
561 {
562   char *path;
563   char *pos;
564   char *end;
565   char *buf;
566   const char *p;
567
568   p = getenv ("PATH");
569   if (p == NULL)
570     {
571       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
572                   _("PATH environment variable is unset.\n"));
573       return NULL;
574     }
575   path = GNUNET_strdup (p);     /* because we write on it */
576   buf = GNUNET_malloc (strlen (path) + 20);
577   pos = path;
578
579   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
580     {
581       *end = '\0';
582       sprintf (buf, "%s/%s", pos, binary);
583       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
584         {
585           GNUNET_free (path);
586           return buf;
587         }
588       pos = end + 1;
589     }
590   sprintf (buf, "%s/%s", pos, binary);
591   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
592     {
593       GNUNET_free (path);
594       return buf;
595     }
596   GNUNET_free (buf);
597   GNUNET_free (path);
598   return NULL;
599 }
600
601 /**
602  * Check whether the suid bit is set on a file.
603  * Attempts to find the file using the current
604  * PATH environment variable as a search path.
605  *
606  * @param binary the name of the file to check
607  *
608  * @return GNUNET_YES if the binary is found and
609  *         can be run properly, GNUNET_NO otherwise
610  */
611 static int
612 check_gnunet_nat_binary(char *binary)
613 {
614   struct stat statbuf;
615   char *p;
616 #ifdef MINGW
617   SOCKET rawsock;
618 #endif
619
620 #ifdef MINGW
621   char *binaryexe;
622   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
623   p = get_path_from_PATH (binaryexe);
624   free (binaryexe);
625 #else
626   p = get_path_from_PATH (binary);
627 #endif
628   if (p == NULL)
629     {
630       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
631                   _("Could not find binary `%s' in PATH!\n"),
632                   binary);
633       return GNUNET_NO;
634     }
635   if (0 != STAT (p, &statbuf))
636     {
637       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
638                   _("stat (%s) failed: %s\n"),
639                   p,
640                   STRERROR (errno));
641       GNUNET_free (p);
642       return GNUNET_SYSERR;
643     }
644   GNUNET_free (p);
645 #ifndef MINGW
646   if ( (0 != (statbuf.st_mode & S_ISUID)) &&
647        (statbuf.st_uid == 0) )
648     return GNUNET_YES;
649   return GNUNET_NO;
650 #else
651   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
652   if (INVALID_SOCKET == rawsock)
653     {
654       DWORD err = GetLastError ();
655       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
656                   "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
657       return GNUNET_NO; /* not running as administrator */
658     }
659   closesocket (rawsock);
660   return GNUNET_YES;
661 #endif
662 }
663
664
665 static void
666 try_connect (void *cls,
667              const struct GNUNET_SCHEDULER_TaskContext *tc)
668 {
669   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670               "Asking peers to connect...\n");
671   GNUNET_TRANSPORT_try_connect (p2.th,
672                                 &p1.id);
673   GNUNET_TRANSPORT_try_connect (p1.th,
674                                 &p2.id);
675   tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
676                                       &try_connect,
677                                       NULL);
678 }
679
680
681
682 static void
683 run (void *cls,
684      char *const *args,
685      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
686 {
687   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
688                                            &end_badly,
689                                            NULL);
690   if (is_tcp)
691     {
692       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
693       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
694     }
695   else if (is_http)
696     {
697       setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
698       setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
699     }
700   else if (is_https)
701     {
702       setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
703       setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
704     }
705   else if (is_udp)
706     {
707       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
708       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
709     }
710   else if (is_unix)
711     {
712       setup_peer (&p1, "test_transport_api_unix_peer1.conf");
713       setup_peer (&p2, "test_transport_api_unix_peer2.conf");
714     }
715   else if (is_tcp_nat)
716     {
717       setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
718       setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
719     }
720   else if (is_wlan)
721     {
722       setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
723       setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
724     }
725   else
726     GNUNET_assert (0);
727   GNUNET_assert(p1.th != NULL);
728   GNUNET_assert(p2.th != NULL);
729   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
730   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
731   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
732 }
733
734
735 static int
736 check ()
737 {
738   char *const argv[] = { "test-transport-api-reliability",
739     "-c",
740     "test_transport_api_data.conf",
741 #if VERBOSE
742     "-L", "DEBUG",
743 #endif
744     NULL
745   };
746   struct GNUNET_GETOPT_CommandLineOption options[] = {
747     GNUNET_GETOPT_OPTION_END
748   };
749
750 #if WRITECONFIG
751   setTransportOptions("test_transport_api_data.conf");
752 #endif
753   ok = 1;
754
755   if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
756     {
757       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
758       return 0;
759     }
760
761   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
762                       argv, "test-transport-api-reliability", "nohelp",
763                       options, &run, &ok);
764   stop_arm (&p1);
765   stop_arm (&p2);
766
767   if (is_https)
768   {
769     struct stat sbuf;
770     if (0 == stat (cert_file_p1, &sbuf ))
771     {
772       if (0 == remove(cert_file_p1))
773         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
774       else
775         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
776     }
777
778     if (0 == stat (key_file_p1, &sbuf ))
779     {
780       if (0 == remove(key_file_p1))
781         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
782       else
783         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
784     }
785
786     if (0 == stat (cert_file_p2, &sbuf ))
787     {
788       if (0 == remove(cert_file_p2))
789         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
790       else
791         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
792     }
793
794     if (0 == stat (key_file_p2, &sbuf ))
795     {
796       if (0 == remove(key_file_p2))
797         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
798       else
799         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
800     }
801     GNUNET_free(key_file_p1);
802     GNUNET_free(key_file_p2);
803     GNUNET_free(cert_file_p1);
804     GNUNET_free(cert_file_p2);
805   }
806
807   if ((p1.servicehome != NULL) && (p2.servicehome != NULL))
808   {
809     GNUNET_DISK_directory_remove (p1.servicehome);
810     GNUNET_DISK_directory_remove (p2.servicehome);
811     GNUNET_free(p1.servicehome);
812     GNUNET_free(p2.servicehome);
813   }
814   return ok;
815
816   return ok;
817 }
818
819
820 int
821 main (int argc, char *argv[])
822 {
823   int ret;
824 #ifdef MINGW
825   return GNUNET_SYSERR;
826 #endif
827
828   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
829   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
830
831   if (strstr(argv[0], "tcp_nat") != NULL)
832     {
833       is_tcp_nat = GNUNET_YES;
834       GNUNET_asprintf(&test_name, "tcp_nat");
835     }
836   else if (strstr(argv[0], "tcp") != NULL)
837     {
838       is_tcp = GNUNET_YES;
839       GNUNET_asprintf(&test_name, "tcp");
840     }
841   else if (strstr(argv[0], "https") != NULL)
842     {
843       is_https = GNUNET_YES;
844       GNUNET_asprintf(&test_name, "https");
845     }
846   else if (strstr(argv[0], "http") != NULL)
847     {
848       is_http = GNUNET_YES;
849       GNUNET_asprintf(&test_name, "http");
850     }
851   else if (strstr(argv[0], "udp") != NULL)
852     {
853       is_udp = GNUNET_YES;
854       GNUNET_asprintf(&test_name, "udp");
855     }
856   else if (strstr(argv[0], "unix") != NULL)
857     {
858       is_unix = GNUNET_YES;
859       GNUNET_asprintf(&test_name, "unix");
860     }
861   else if (strstr(argv[0], "wlan") != NULL)
862     {
863        is_wlan = GNUNET_YES;
864     }
865   GNUNET_log_setup ("test-transport-api-reliability",
866 #if VERBOSE
867                     "DEBUG",
868 #else
869                     "WARNING",
870 #endif
871                     NULL);
872   ret = check ();
873
874   GNUNET_free_non_null(test_name);
875   return ret;
876 }
877
878 /* end of test_transport_api_reliability.c */