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