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