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