fixing assertion transport_api_new.c:379
[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         GNUNET_SCHEDULER_cancel (die_task);
534       if (GNUNET_SCHEDULER_NO_TASK != die_task)
535         GNUNET_SCHEDULER_cancel (tct);
536       tct = GNUNET_SCHEDULER_NO_TASK;
537
538       if (p2_hello_canceled == GNUNET_NO)
539       {
540         GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
541         p2_hello_canceled = GNUNET_YES;
542       }
543       if (p1_hello_canceled == GNUNET_NO)
544       {
545         GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
546         p1_hello_canceled = GNUNET_YES;
547       }
548
549       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
550                                                &end_badly, NULL);
551       GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
552                                               &p1.id,
553                                               get_size (0), 0, TIMEOUT,
554                                               &notify_ready,
555                                               NULL);
556       
557     }
558 }
559
560
561 static void
562 setup_peer (struct PeerContext *p, const char *cfgname)
563 {
564   p->cfg = GNUNET_CONFIGURATION_create ();
565
566   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
567   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
568       GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
569   GNUNET_DISK_directory_remove (p->servicehome);
570
571
572 #if START_ARM
573   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
574                                         "gnunet-service-arm",
575                                         "gnunet-service-arm",
576 #if VERBOSE_ARM
577                                         "-L", "DEBUG",
578 #endif
579                                         "-c", cfgname, NULL);
580 #endif
581
582   p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
583                                     p,
584                                     &notify_receive,
585                                     &notify_connect,
586                                     &notify_disconnect);
587   GNUNET_assert (p->th != NULL);
588 }
589
590
591 /**
592  * Return the actual path to a file found in the current
593  * PATH environment variable.
594  *
595  * @param binary the name of the file to find
596  */
597 static char *
598 get_path_from_PATH (char *binary)
599 {
600   char *path;
601   char *pos;
602   char *end;
603   char *buf;
604   const char *p;
605
606   p = getenv ("PATH");
607   if (p == NULL)
608     {
609       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
610                   _("PATH environment variable is unset.\n"));
611       return NULL;
612     }
613   path = GNUNET_strdup (p);     /* because we write on it */
614   buf = GNUNET_malloc (strlen (path) + 20);
615   pos = path;
616
617   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
618     {
619       *end = '\0';
620       sprintf (buf, "%s/%s", pos, binary);
621       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
622         {
623           GNUNET_free (path);
624           return buf;
625         }
626       pos = end + 1;
627     }
628   sprintf (buf, "%s/%s", pos, binary);
629   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
630     {
631       GNUNET_free (path);
632       return buf;
633     }
634   GNUNET_free (buf);
635   GNUNET_free (path);
636   return NULL;
637 }
638
639 /**
640  * Check whether the suid bit is set on a file.
641  * Attempts to find the file using the current
642  * PATH environment variable as a search path.
643  *
644  * @param binary the name of the file to check
645  *
646  * @return GNUNET_YES if the binary is found and
647  *         can be run properly, GNUNET_NO otherwise
648  */
649 static int
650 check_gnunet_nat_binary(char *binary)
651 {
652   struct stat statbuf;
653   char *p;
654 #ifdef MINGW
655   SOCKET rawsock;
656 #endif
657
658 #ifdef MINGW
659   char *binaryexe;
660   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
661   p = get_path_from_PATH (binaryexe);
662   free (binaryexe);
663 #else
664   p = get_path_from_PATH (binary);
665 #endif
666   if (p == NULL)
667     {
668       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
669                   _("Could not find binary `%s' in PATH!\n"),
670                   binary);
671       return GNUNET_NO;
672     }
673   if (0 != STAT (p, &statbuf))
674     {
675       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
676                   _("stat (%s) failed: %s\n"),
677                   p,
678                   STRERROR (errno));
679       GNUNET_free (p);
680       return GNUNET_SYSERR;
681     }
682   GNUNET_free (p);
683 #ifndef MINGW
684   if ( (0 != (statbuf.st_mode & S_ISUID)) &&
685        (statbuf.st_uid == 0) )
686     return GNUNET_YES;
687   return GNUNET_NO;
688 #else
689   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
690   if (INVALID_SOCKET == rawsock)
691     {
692       DWORD err = GetLastError ();
693       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
694                   "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
695       return GNUNET_NO; /* not running as administrator */
696     }
697   closesocket (rawsock);
698   return GNUNET_YES;
699 #endif
700 }
701
702
703 static void
704 try_connect (void *cls,
705              const struct GNUNET_SCHEDULER_TaskContext *tc)
706 {
707   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708               "Asking peers to connect...\n");
709   GNUNET_TRANSPORT_try_connect (p2.th,
710                                 &p1.id);
711   GNUNET_TRANSPORT_try_connect (p1.th,
712                                 &p2.id);
713   tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
714                                       &try_connect,
715                                       NULL);
716 }
717
718
719
720 static void
721 run (void *cls,
722      char *const *args,
723      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
724 {
725   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
726                                            &end_badly,
727                                            NULL);
728   if (is_tcp)
729     {
730       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
731       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
732     }
733   else if (is_http)
734     {
735       setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
736       setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
737     }
738   else if (is_https)
739     {
740       setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
741       setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
742     }
743   else if (is_udp)
744     {
745       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
746       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
747     }
748   else if (is_unix)
749     {
750       setup_peer (&p1, "test_transport_api_unix_peer1.conf");
751       setup_peer (&p2, "test_transport_api_unix_peer2.conf");
752     }
753   else if (is_tcp_nat)
754     {
755       setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
756       setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
757     }
758   else if (is_wlan)
759     {
760       setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
761       setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
762     }
763   else
764     GNUNET_assert (0);
765   GNUNET_assert(p1.th != NULL);
766   GNUNET_assert(p2.th != NULL);
767   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
768   p1_hello_canceled = GNUNET_NO;
769   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
770   p2_hello_canceled = GNUNET_NO;
771   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
772 }
773
774
775 static int
776 check ()
777 {
778   char *const argv[] = { "test-transport-api-reliability",
779     "-c",
780     "test_transport_api_data.conf",
781 #if VERBOSE
782     "-L", "DEBUG",
783 #endif
784     NULL
785   };
786   struct GNUNET_GETOPT_CommandLineOption options[] = {
787     GNUNET_GETOPT_OPTION_END
788   };
789
790 #if WRITECONFIG
791   setTransportOptions("test_transport_api_data.conf");
792 #endif
793   ok = 1;
794
795   if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
796     {
797       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
798       return 0;
799     }
800
801   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
802                       argv, "test-transport-api-reliability", "nohelp",
803                       options, &run, &ok);
804   stop_arm (&p1);
805   stop_arm (&p2);
806
807   if (is_https)
808   {
809     struct stat sbuf;
810     if (0 == stat (cert_file_p1, &sbuf ))
811     {
812       if (0 == remove(cert_file_p1))
813         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
814       else
815         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
816     }
817
818     if (0 == stat (key_file_p1, &sbuf ))
819     {
820       if (0 == remove(key_file_p1))
821         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
822       else
823         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
824     }
825
826     if (0 == stat (cert_file_p2, &sbuf ))
827     {
828       if (0 == remove(cert_file_p2))
829         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
830       else
831         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
832     }
833
834     if (0 == stat (key_file_p2, &sbuf ))
835     {
836       if (0 == remove(key_file_p2))
837         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
838       else
839         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
840     }
841     GNUNET_free(key_file_p1);
842     GNUNET_free(key_file_p2);
843     GNUNET_free(cert_file_p1);
844     GNUNET_free(cert_file_p2);
845   }
846
847   if ((p1.servicehome != NULL) && (p2.servicehome != NULL))
848   {
849     GNUNET_DISK_directory_remove (p1.servicehome);
850     GNUNET_DISK_directory_remove (p2.servicehome);
851     GNUNET_free(p1.servicehome);
852     GNUNET_free(p2.servicehome);
853   }
854   return ok;
855
856   return ok;
857 }
858
859
860 int
861 main (int argc, char *argv[])
862 {
863   int ret;
864 #ifdef MINGW
865   return GNUNET_SYSERR;
866 #endif
867
868   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
869   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
870
871   if (strstr(argv[0], "tcp_nat") != NULL)
872     {
873       is_tcp_nat = GNUNET_YES;
874       GNUNET_asprintf(&test_name, "tcp_nat");
875     }
876   else if (strstr(argv[0], "tcp") != NULL)
877     {
878       is_tcp = GNUNET_YES;
879       GNUNET_asprintf(&test_name, "tcp");
880     }
881   else if (strstr(argv[0], "https") != NULL)
882     {
883       is_https = GNUNET_YES;
884       GNUNET_asprintf(&test_name, "https");
885     }
886   else if (strstr(argv[0], "http") != NULL)
887     {
888       is_http = GNUNET_YES;
889       GNUNET_asprintf(&test_name, "http");
890     }
891   else if (strstr(argv[0], "udp") != NULL)
892     {
893       is_udp = GNUNET_YES;
894       GNUNET_asprintf(&test_name, "udp");
895     }
896   else if (strstr(argv[0], "unix") != NULL)
897     {
898       is_unix = GNUNET_YES;
899       GNUNET_asprintf(&test_name, "unix");
900     }
901   else if (strstr(argv[0], "wlan") != NULL)
902     {
903        is_wlan = GNUNET_YES;
904     }
905   GNUNET_log_setup ("test-transport-api-reliability",
906 #if VERBOSE
907                     "DEBUG",
908 #else
909                     "WARNING",
910 #endif
911                     NULL);
912   ret = check ();
913
914   GNUNET_free_non_null(test_name);
915   return ret;
916 }
917
918 /* end of test_transport_api_reliability.c */