e7cb00274aa642fb1fb665b5a409e7fd95a0b010
[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   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_assert (message != NULL);
268   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269               "Exchanging HELLO of size %d with peer (%s)!\n", 
270               (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
271               GNUNET_i2s (&me->id));
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   ret = (iter * iter * iter);
355   return sizeof (struct TestMessage) + (ret % 60000);
356 }
357
358
359 static void
360 notify_receive (void *cls,
361                 const struct GNUNET_PeerIdentity *peer,
362                 const struct GNUNET_MessageHeader *message,
363                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
364                 uint32_t ats_count)
365 {
366   static int n;
367   unsigned int s;
368   char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
369   const struct TestMessage *hdr;
370
371   hdr = (const struct TestMessage*) message;
372
373   if (MTYPE != ntohs (message->type))
374     return;
375   msg_recv_expected = n;
376   msg_recv = ntohl(hdr->num);
377   s = get_size (ntohl(hdr->num));
378
379   if (ntohs (message->size) != s)
380     {
381       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382                   "Expected message %u of size %u, got %u bytes of message %u\n",
383                   ntohl(hdr->num), s,
384                   ntohs (message->size),
385                   ntohl (hdr->num));
386       if (GNUNET_SCHEDULER_NO_TASK != die_task)
387         GNUNET_SCHEDULER_cancel (die_task);
388       test_failed = GNUNET_YES;
389       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
390       return;
391     }
392
393   memset (cbuf, ntohl(hdr->num), s - sizeof (struct TestMessage));
394   if (0 != memcmp (cbuf,
395                    &hdr[1],
396                    s - sizeof (struct TestMessage)))
397     {
398       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
399                   "Expected message %u with bits %u, but body did not match\n",
400                   ntohl(hdr->num), (unsigned char) n);
401       if (GNUNET_SCHEDULER_NO_TASK != die_task)
402         GNUNET_SCHEDULER_cancel (die_task);
403       test_failed = GNUNET_YES;
404       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
405       return;
406     }
407 #if VERBOSE
408   if (ntohl(hdr->num) % 5 == 0)
409     {
410       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411                   "Got message %u of size %u\n",
412                   ntohl (hdr->num),
413                   ntohs (message->size));
414     }
415 #endif
416   n++;
417   set_bit(ntohl(hdr->num));
418   if (0 == (n % (TOTAL_MSGS / 100)))
419     {
420       fprintf (stderr, ".");
421       if (GNUNET_SCHEDULER_NO_TASK != die_task)
422         GNUNET_SCHEDULER_cancel (die_task);
423       test_failed = GNUNET_YES;
424       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
425                                                &end_badly,
426                                                NULL);
427     }
428   if (n == TOTAL_MSGS)
429     end ();
430 }
431
432
433 static size_t
434 notify_ready (void *cls, size_t size, void *buf)
435 {
436   static int n;
437   char *cbuf = buf;
438   struct TestMessage hdr;
439   unsigned int s;
440   unsigned int ret;
441
442   th_p2 = NULL;
443
444   if (buf == NULL)
445     {
446       GNUNET_break (0);
447       ok = 42;
448       return 0;
449     }
450   ret = 0;
451   s = get_size (n);
452   GNUNET_assert (size >= s);
453   GNUNET_assert (buf != NULL);
454   cbuf = buf;
455   do
456     {
457       hdr.header.size = htons (s);
458       hdr.header.type = htons (MTYPE);
459       hdr.num = htonl (n);
460       msg_sent = n;
461       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
462       ret += sizeof (struct TestMessage);
463       memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
464       ret += s - sizeof (struct TestMessage);
465 #if VERBOSE
466       if (n % 5000 == 0)
467         {
468           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469                       "Sending message %u of size %u\n",
470                       n,
471                       s);
472         }
473 #endif
474       n++;
475       s = get_size (n);
476       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
477         break; /* sometimes pack buffer full, sometimes not */
478     }
479   while (size - ret >= s);
480   if (n < TOTAL_MSGS)
481   {
482     th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
483                                             &p1.id,
484                                             s, 0, TIMEOUT,
485                                             &notify_ready,
486                                             NULL);
487     msg_scheduled = n;
488   }
489   else
490     {
491       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent!!\n");
492       if (GNUNET_SCHEDULER_NO_TASK != die_task)
493         GNUNET_SCHEDULER_cancel(die_task);
494       die_task = GNUNET_SCHEDULER_add_delayed (UNRELIABLE_TIMEOUT, &end_unreliably, NULL);
495     }
496   if (n % 5000 == 0)
497     {
498       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499                   "Returning total message block of size %u\n",
500                   ret);
501     }
502   total_bytes += ret;
503   return ret;
504 }
505
506
507
508 static void
509 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
510 {
511   connected--;
512 #if VERBOSE
513   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514               "Peer `%4s' disconnected (%p)!\n",
515               GNUNET_i2s (peer), cls);
516 #endif
517   if (th_p2 != NULL)
518     {
519       GNUNET_TRANSPORT_notify_transmit_ready_cancel (th_p2);
520       th_p2 = NULL;
521     }
522 }
523
524
525
526 static void
527 notify_connect (void *cls,
528                 const struct GNUNET_PeerIdentity *peer,
529                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
530                 uint32_t ats_count)
531 {
532   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533               "Peer `%4s' connected to us (%p)!\n", 
534               GNUNET_i2s (peer), 
535               cls);
536   if (cls == &p1)
537     {
538       GNUNET_TRANSPORT_set_quota (p1.th,
539                                   &p2.id,
540                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
541                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
542       start_time = GNUNET_TIME_absolute_get ();
543       connected++;
544     }
545   else
546     {
547       GNUNET_TRANSPORT_set_quota (p2.th,
548                                   &p1.id,
549                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
550                                   GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
551       connected++;
552     }
553   if (2 == connected)
554     {
555       if (GNUNET_SCHEDULER_NO_TASK != die_task)
556         {
557           GNUNET_SCHEDULER_cancel (die_task);     
558         }
559       if (GNUNET_SCHEDULER_NO_TASK != tct)
560         {
561           GNUNET_SCHEDULER_cancel (tct);
562           tct = GNUNET_SCHEDULER_NO_TASK;
563         }
564       if (p2_hello_canceled == GNUNET_NO)
565         {
566           GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
567           p2_hello_canceled = GNUNET_YES;
568         }
569       if (p1_hello_canceled == GNUNET_NO)
570         {
571           GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
572           p1_hello_canceled = GNUNET_YES;
573         }
574
575       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
576                                                &end_badly, NULL);
577       GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
578                                               &p1.id,
579                                               get_size (0), 0, TIMEOUT,
580                                               &notify_ready,
581                                               NULL);
582       
583     }
584 }
585
586
587 static void
588 setup_peer (struct PeerContext *p, const char *cfgname)
589 {
590   memset (p, 0, sizeof (*p));
591   p->cfg = GNUNET_CONFIGURATION_create ();
592
593   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
594   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
595     {
596       GNUNET_assert (GNUNET_OK ==
597                      GNUNET_CONFIGURATION_get_value_string (p->cfg,
598                                                             "PATHS", "SERVICEHOME", 
599                                                             &p->servicehome));
600       GNUNET_DISK_directory_remove (p->servicehome);
601     }
602
603
604 #if START_ARM
605   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
606                                         "gnunet-service-arm",
607                                         "gnunet-service-arm",
608 #if VERBOSE_ARM
609                                         "-L", "DEBUG",
610 #endif
611                                         "-c", cfgname, NULL);
612 #endif
613
614   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
615                                     p,
616                                     &notify_receive,
617                                     &notify_connect,
618                                     &notify_disconnect);
619   GNUNET_assert (p->th != NULL);
620 }
621
622
623 /**
624  * Return the actual path to a file found in the current
625  * PATH environment variable.
626  *
627  * @param binary the name of the file to find
628  */
629 static char *
630 get_path_from_PATH (char *binary)
631 {
632   char *path;
633   char *pos;
634   char *end;
635   char *buf;
636   const char *p;
637
638   p = getenv ("PATH");
639   if (p == NULL)
640     {
641       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
642                   _("PATH environment variable is unset.\n"));
643       return NULL;
644     }
645   path = GNUNET_strdup (p);     /* because we write on it */
646   buf = GNUNET_malloc (strlen (path) + 20);
647   pos = path;
648
649   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
650     {
651       *end = '\0';
652       sprintf (buf, "%s/%s", pos, binary);
653       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
654         {
655           GNUNET_free (path);
656           return buf;
657         }
658       pos = end + 1;
659     }
660   sprintf (buf, "%s/%s", pos, binary);
661   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
662     {
663       GNUNET_free (path);
664       return buf;
665     }
666   GNUNET_free (buf);
667   GNUNET_free (path);
668   return NULL;
669 }
670
671 /**
672  * Check whether the suid bit is set on a file.
673  * Attempts to find the file using the current
674  * PATH environment variable as a search path.
675  *
676  * @param binary the name of the file to check
677  *
678  * @return GNUNET_YES if the binary is found and
679  *         can be run properly, GNUNET_NO otherwise
680  */
681 static int
682 check_gnunet_nat_binary(char *binary)
683 {
684   struct stat statbuf;
685   char *p;
686 #ifdef MINGW
687   SOCKET rawsock;
688 #endif
689
690 #ifdef MINGW
691   char *binaryexe;
692   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
693   p = get_path_from_PATH (binaryexe);
694   free (binaryexe);
695 #else
696   p = get_path_from_PATH (binary);
697 #endif
698   if (p == NULL)
699     {
700       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
701                   _("Could not find binary `%s' in PATH!\n"),
702                   binary);
703       return GNUNET_NO;
704     }
705   if (0 != STAT (p, &statbuf))
706     {
707       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
708                   _("stat (%s) failed: %s\n"),
709                   p,
710                   STRERROR (errno));
711       GNUNET_free (p);
712       return GNUNET_SYSERR;
713     }
714   GNUNET_free (p);
715 #ifndef MINGW
716   if ( (0 != (statbuf.st_mode & S_ISUID)) &&
717        (statbuf.st_uid == 0) )
718     return GNUNET_YES;
719   return GNUNET_NO;
720 #else
721   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
722   if (INVALID_SOCKET == rawsock)
723     {
724       DWORD err = GetLastError ();
725       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
726                   "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
727       return GNUNET_NO; /* not running as administrator */
728     }
729   closesocket (rawsock);
730   return GNUNET_YES;
731 #endif
732 }
733
734
735 static void
736 try_connect (void *cls,
737              const struct GNUNET_SCHEDULER_TaskContext *tc)
738 {
739   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740               "Asking peers to connect...\n");
741   GNUNET_TRANSPORT_try_connect (p2.th,
742                                 &p1.id);
743   GNUNET_TRANSPORT_try_connect (p1.th,
744                                 &p2.id);
745   tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
746                                       &try_connect,
747                                       NULL);
748 }
749
750
751
752 static void
753 run (void *cls,
754      char *const *args,
755      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
756 {
757   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
758                                            &end_badly,
759                                            NULL);
760   if (is_tcp)
761     {
762       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
763       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
764     }
765   else if (is_http)
766     {
767       setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
768       setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
769     }
770   else if (is_https)
771     {
772       setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
773       setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
774     }
775   else if (is_udp)
776     {
777       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
778       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
779     }
780   else if (is_unix)
781     {
782       setup_peer (&p1, "test_transport_api_unix_peer1.conf");
783       setup_peer (&p2, "test_transport_api_unix_peer2.conf");
784     }
785   else if (is_tcp_nat)
786     {
787       setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
788       setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
789     }
790   else if (is_wlan)
791     {
792       setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
793       setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
794     }
795   else
796     GNUNET_assert (0);
797   GNUNET_assert(p1.th != NULL);
798   GNUNET_assert(p2.th != NULL);
799   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
800   p1_hello_canceled = GNUNET_NO;
801   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
802   p2_hello_canceled = GNUNET_NO;
803   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
804 }
805
806
807 static int
808 check ()
809 {
810   char *const argv[] = { "test-transport-api-unreliability",
811     "-c",
812     "test_transport_api_data.conf",
813 #if VERBOSE
814     "-L", "DEBUG",
815 #endif
816     NULL
817   };
818   struct GNUNET_GETOPT_CommandLineOption options[] = {
819     GNUNET_GETOPT_OPTION_END
820   };
821
822 #if WRITECONFIG
823   setTransportOptions("test_transport_api_data.conf");
824 #endif
825   ok = 1;
826
827   if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
828     {
829       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
830       return 0;
831     }
832
833   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
834                       argv, "test-transport-api-unreliability", "nohelp",
835                       options, &run, &ok);
836   stop_arm (&p1);
837   stop_arm (&p2);
838
839   if (is_https)
840   {
841     struct stat sbuf;
842     if (0 == stat (cert_file_p1, &sbuf ))
843     {
844       if (0 == remove(cert_file_p1))
845         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
846       else
847         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
848     }
849
850     if (0 == stat (key_file_p1, &sbuf ))
851     {
852       if (0 == remove(key_file_p1))
853         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
854       else
855         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
856     }
857
858     if (0 == stat (cert_file_p2, &sbuf ))
859     {
860       if (0 == remove(cert_file_p2))
861         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
862       else
863         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
864     }
865
866     if (0 == stat (key_file_p2, &sbuf ))
867     {
868       if (0 == remove(key_file_p2))
869         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
870       else
871         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
872     }
873     GNUNET_free(key_file_p1);
874     GNUNET_free(key_file_p2);
875     GNUNET_free(cert_file_p1);
876     GNUNET_free(cert_file_p2);
877   }
878
879   if (p1.servicehome != NULL) 
880     {
881       GNUNET_DISK_directory_remove (p1.servicehome);
882       GNUNET_free(p1.servicehome);
883     }
884   if (p2.servicehome != NULL)
885     {
886       GNUNET_DISK_directory_remove (p2.servicehome); 
887       GNUNET_free(p2.servicehome);
888     }
889   return ok;
890 }
891
892
893 int
894 main (int argc, char *argv[])
895 {
896   int ret;
897
898   test_failed = GNUNET_NO;
899
900   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
901   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
902
903   if (strstr(argv[0], "tcp_nat") != NULL)
904     {
905       is_tcp_nat = GNUNET_YES;
906       GNUNET_asprintf(&test_name, "tcp_nat");
907     }
908   else if (strstr(argv[0], "tcp") != NULL)
909     {
910       is_tcp = GNUNET_YES;
911       GNUNET_asprintf(&test_name, "tcp");
912     }
913   else if (strstr(argv[0], "https") != NULL)
914     {
915       is_https = GNUNET_YES;
916       GNUNET_asprintf(&test_name, "https");
917     }
918   else if (strstr(argv[0], "http") != NULL)
919     {
920       is_http = GNUNET_YES;
921       GNUNET_asprintf(&test_name, "http");
922     }
923   else if (strstr(argv[0], "udp") != NULL)
924     {
925       is_udp = GNUNET_YES;
926       GNUNET_asprintf(&test_name, "udp");
927     }
928   else if (strstr(argv[0], "unix") != NULL)
929     {
930       is_unix = GNUNET_YES;
931       GNUNET_asprintf(&test_name, "unix");
932     }
933   else if (strstr(argv[0], "wlan") != NULL)
934     {
935        is_wlan = GNUNET_YES;
936     }
937   GNUNET_log_setup ("test-transport-api-unreliability",
938 #if VERBOSE
939                     "DEBUG",
940 #else
941                     "WARNING",
942 #endif
943                     NULL);
944   ret = check ();
945
946   GNUNET_free_non_null(test_name);
947   return ret;
948 }
949
950 /* end of test_transport_api_unreliability.c */