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