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