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