fix
[oweals/gnunet.git] / src / transport / test_transport_api_unreliability.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file transport/test_transport_api_unreliability.c
22  * @brief test case for transports; ensures messages get
23  *        through, regardless of order
24  *
25  * This test case serves as a base for unreliable
26  * transport test cases to check that the transports
27  * achieve reliable message delivery.
28  */
29 #include "platform.h"
30 #include "gnunet_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gauger.h"
39 #include "transport.h"
40 #include "transport-testing.h"
41
42 #define VERBOSE GNUNET_NO
43
44 #define VERBOSE_ARM GNUNET_NO
45
46 #define START_ARM GNUNET_YES
47
48 /**
49  * Note that this value must not significantly exceed
50  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
51  * messages may be dropped even for a reliable transport.
52  */
53 #define TOTAL_MSGS (80000 * 3) /* Total messages should be divisible by 8, so we can make a nice bitmap */
54
55 /**
56  * How long until we give up on transmitting the message?
57  */
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
59
60 #define UNRELIABLE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
61
62 #define MTYPE 12345
63
64 static struct PeerContext p1;
65
66 static struct PeerContext p2;
67
68 static int ok;
69
70 static int is_tcp;
71
72 static int is_tcp_nat;
73
74 static int is_http;
75
76 static int is_https;
77
78 static int is_udp;
79
80 static int is_unix;
81
82 static int is_wlan;
83
84 static int connected;
85
86 static unsigned long long total_bytes;
87
88 static struct GNUNET_TIME_Absolute start_time;
89
90 static GNUNET_SCHEDULER_TaskIdentifier die_task;
91
92 static GNUNET_SCHEDULER_TaskIdentifier tct;
93
94 struct GNUNET_TRANSPORT_TransmitHandle * th_p2;
95
96 static char *key_file_p1;
97 static char *cert_file_p1;
98
99 static char *key_file_p2;
100 static char *cert_file_p2;
101
102 static char *test_name;
103
104 static char bitmap[TOTAL_MSGS / 8];
105
106 static int msg_scheduled;
107 static int msg_sent;
108 static int msg_recv_expected;
109 static int msg_recv;
110
111 static int p1_hello_canceled;
112 static int p2_hello_canceled;
113
114 /**
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
542       if (p2_hello_canceled == GNUNET_NO)
543       {
544         GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
545         p2_hello_canceled = GNUNET_YES;
546       }
547       if (p1_hello_canceled == GNUNET_NO)
548       {
549         GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
550         p1_hello_canceled = GNUNET_YES;
551       }
552
553       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
554                                                &end_badly, NULL);
555       GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
556                                               &p1.id,
557                                               get_size (0), 0, TIMEOUT,
558                                               &notify_ready,
559                                               NULL);
560       
561     }
562 }
563
564
565 static void
566 setup_peer (struct PeerContext *p, const char *cfgname)
567 {
568   p->cfg = GNUNET_CONFIGURATION_create ();
569
570   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
571   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
572       GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
573   GNUNET_DISK_directory_remove (p->servicehome);
574
575
576 #if START_ARM
577   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
578                                         "gnunet-service-arm",
579                                         "gnunet-service-arm",
580 #if VERBOSE_ARM
581                                         "-L", "DEBUG",
582 #endif
583                                         "-c", cfgname, NULL);
584 #endif
585
586   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
587                                     p,
588                                     &notify_receive,
589                                     &notify_connect,
590                                     &notify_disconnect);
591   GNUNET_assert (p->th != NULL);
592 }
593
594
595 /**
596  * Return the actual path to a file found in the current
597  * PATH environment variable.
598  *
599  * @param binary the name of the file to find
600  */
601 static char *
602 get_path_from_PATH (char *binary)
603 {
604   char *path;
605   char *pos;
606   char *end;
607   char *buf;
608   const char *p;
609
610   p = getenv ("PATH");
611   if (p == NULL)
612     {
613       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
614                   _("PATH environment variable is unset.\n"));
615       return NULL;
616     }
617   path = GNUNET_strdup (p);     /* because we write on it */
618   buf = GNUNET_malloc (strlen (path) + 20);
619   pos = path;
620
621   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
622     {
623       *end = '\0';
624       sprintf (buf, "%s/%s", pos, binary);
625       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
626         {
627           GNUNET_free (path);
628           return buf;
629         }
630       pos = end + 1;
631     }
632   sprintf (buf, "%s/%s", pos, binary);
633   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
634     {
635       GNUNET_free (path);
636       return buf;
637     }
638   GNUNET_free (buf);
639   GNUNET_free (path);
640   return NULL;
641 }
642
643 /**
644  * Check whether the suid bit is set on a file.
645  * Attempts to find the file using the current
646  * PATH environment variable as a search path.
647  *
648  * @param binary the name of the file to check
649  *
650  * @return GNUNET_YES if the binary is found and
651  *         can be run properly, GNUNET_NO otherwise
652  */
653 static int
654 check_gnunet_nat_binary(char *binary)
655 {
656   struct stat statbuf;
657   char *p;
658 #ifdef MINGW
659   SOCKET rawsock;
660 #endif
661
662 #ifdef MINGW
663   char *binaryexe;
664   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
665   p = get_path_from_PATH (binaryexe);
666   free (binaryexe);
667 #else
668   p = get_path_from_PATH (binary);
669 #endif
670   if (p == NULL)
671     {
672       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673                   _("Could not find binary `%s' in PATH!\n"),
674                   binary);
675       return GNUNET_NO;
676     }
677   if (0 != STAT (p, &statbuf))
678     {
679       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
680                   _("stat (%s) failed: %s\n"),
681                   p,
682                   STRERROR (errno));
683       GNUNET_free (p);
684       return GNUNET_SYSERR;
685     }
686   GNUNET_free (p);
687 #ifndef MINGW
688   if ( (0 != (statbuf.st_mode & S_ISUID)) &&
689        (statbuf.st_uid == 0) )
690     return GNUNET_YES;
691   return GNUNET_NO;
692 #else
693   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
694   if (INVALID_SOCKET == rawsock)
695     {
696       DWORD err = GetLastError ();
697       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
698                   "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
699       return GNUNET_NO; /* not running as administrator */
700     }
701   closesocket (rawsock);
702   return GNUNET_YES;
703 #endif
704 }
705
706
707 static void
708 try_connect (void *cls,
709              const struct GNUNET_SCHEDULER_TaskContext *tc)
710 {
711   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712               "Asking peers to connect...\n");
713   GNUNET_TRANSPORT_try_connect (p2.th,
714                                 &p1.id);
715   GNUNET_TRANSPORT_try_connect (p1.th,
716                                 &p2.id);
717   tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
718                                       &try_connect,
719                                       NULL);
720 }
721
722
723
724 static void
725 run (void *cls,
726      char *const *args,
727      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
728 {
729   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
730                                            &end_badly,
731                                            NULL);
732   if (is_tcp)
733     {
734       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
735       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
736     }
737   else if (is_http)
738     {
739       setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
740       setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
741     }
742   else if (is_https)
743     {
744       setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
745       setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
746     }
747   else if (is_udp)
748     {
749       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
750       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
751     }
752   else if (is_unix)
753     {
754       setup_peer (&p1, "test_transport_api_unix_peer1.conf");
755       setup_peer (&p2, "test_transport_api_unix_peer2.conf");
756     }
757   else if (is_tcp_nat)
758     {
759       setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
760       setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
761     }
762   else if (is_wlan)
763     {
764       setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
765       setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
766     }
767   else
768     GNUNET_assert (0);
769   GNUNET_assert(p1.th != NULL);
770   GNUNET_assert(p2.th != NULL);
771   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
772   p1_hello_canceled = GNUNET_NO;
773   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
774   p2_hello_canceled = GNUNET_NO;
775   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
776 }
777
778
779 static int
780 check ()
781 {
782   char *const argv[] = { "test-transport-api-unreliability",
783     "-c",
784     "test_transport_api_data.conf",
785 #if VERBOSE
786     "-L", "DEBUG",
787 #endif
788     NULL
789   };
790   struct GNUNET_GETOPT_CommandLineOption options[] = {
791     GNUNET_GETOPT_OPTION_END
792   };
793
794 #if WRITECONFIG
795   setTransportOptions("test_transport_api_data.conf");
796 #endif
797   ok = 1;
798
799   if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
800     {
801       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
802       return 0;
803     }
804
805   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
806                       argv, "test-transport-api-unreliability", "nohelp",
807                       options, &run, &ok);
808   stop_arm (&p1);
809   stop_arm (&p2);
810
811   if (is_https)
812   {
813     struct stat sbuf;
814     if (0 == stat (cert_file_p1, &sbuf ))
815     {
816       if (0 == remove(cert_file_p1))
817         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
818       else
819         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
820     }
821
822     if (0 == stat (key_file_p1, &sbuf ))
823     {
824       if (0 == remove(key_file_p1))
825         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
826       else
827         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
828     }
829
830     if (0 == stat (cert_file_p2, &sbuf ))
831     {
832       if (0 == remove(cert_file_p2))
833         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
834       else
835         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
836     }
837
838     if (0 == stat (key_file_p2, &sbuf ))
839     {
840       if (0 == remove(key_file_p2))
841         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
842       else
843         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
844     }
845     GNUNET_free(key_file_p1);
846     GNUNET_free(key_file_p2);
847     GNUNET_free(cert_file_p1);
848     GNUNET_free(cert_file_p2);
849   }
850
851   if ((p1.servicehome != NULL) && (p2.servicehome != NULL))
852   {
853     GNUNET_DISK_directory_remove (p1.servicehome);
854     GNUNET_DISK_directory_remove (p2.servicehome);
855     GNUNET_free(p1.servicehome);
856     GNUNET_free(p2.servicehome);
857   }
858   return ok;
859
860   return ok;
861 }
862
863
864 int
865 main (int argc, char *argv[])
866 {
867   int ret;
868 #ifdef MINGW
869   return GNUNET_SYSERR;
870 #endif
871
872   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
873   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
874
875   if (strstr(argv[0], "tcp_nat") != NULL)
876     {
877       is_tcp_nat = GNUNET_YES;
878       GNUNET_asprintf(&test_name, "tcp_nat");
879     }
880   else if (strstr(argv[0], "tcp") != NULL)
881     {
882       is_tcp = GNUNET_YES;
883       GNUNET_asprintf(&test_name, "tcp");
884     }
885   else if (strstr(argv[0], "https") != NULL)
886     {
887       is_https = GNUNET_YES;
888       GNUNET_asprintf(&test_name, "https");
889     }
890   else if (strstr(argv[0], "http") != NULL)
891     {
892       is_http = GNUNET_YES;
893       GNUNET_asprintf(&test_name, "http");
894     }
895   else if (strstr(argv[0], "udp") != NULL)
896     {
897       is_udp = GNUNET_YES;
898       GNUNET_asprintf(&test_name, "udp");
899     }
900   else if (strstr(argv[0], "unix") != NULL)
901     {
902       is_unix = GNUNET_YES;
903       GNUNET_asprintf(&test_name, "unix");
904     }
905   else if (strstr(argv[0], "wlan") != NULL)
906     {
907        is_wlan = GNUNET_YES;
908     }
909   GNUNET_log_setup ("test-transport-api-unreliability",
910 #if VERBOSE
911                     "DEBUG",
912 #else
913                     "WARNING",
914 #endif
915                     NULL);
916   ret = check ();
917
918   GNUNET_free_non_null(test_name);
919   return ret;
920 }
921
922 /* end of test_transport_api_unreliability.c */