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