ats
[oweals/gnunet.git] / src / transport / test_transport_ats.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_ats.c
22  * @brief base test case for transport implementations
23  *
24  * This test case serves as a base for tcp, udp, and udp-nat
25  * transport test cases.  Based on the executable being run
26  * the correct test case will be performed.  Conservation of
27  * C code apparently.
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_transport_service.h"
37 #include "gnunet_statistics_service.h"
38 #include "transport.h"
39 #include "gauger.h"
40
41 #define VERBOSE GNUNET_NO
42
43 #define VERBOSE_ARM GNUNET_NO
44
45 #define START_ARM GNUNET_YES
46
47 /**
48  * How long until we give up on transmitting the message?
49  */
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
51
52 /**
53  * How long until we give up on transmitting the message?
54  */
55 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
56
57 #define MTYPE 12345
58
59 struct PeerContext
60 {
61   struct GNUNET_CONFIGURATION_Handle *cfg;
62   struct GNUNET_TRANSPORT_Handle *th;
63   struct GNUNET_PeerIdentity id;
64   struct GNUNET_STATISTICS_Handle *stats;
65 #if START_ARM
66   struct GNUNET_OS_Process *arm_proc;
67 #endif
68 };
69
70 static struct PeerContext p1;
71
72 /**
73  * Handle for reporting statistics.
74  */
75
76
77
78 static struct PeerContext p2;
79
80 static int ok;
81
82 static int is_tcp;
83
84 static int is_tcp_nat;
85
86 static int is_udp;
87
88 static int is_unix;
89
90 static int is_udp_nat;
91
92 static int is_http;
93
94 static int is_https;
95
96 static int is_multi_protocol;
97
98 static int is_wlan;
99
100 static  GNUNET_SCHEDULER_TaskIdentifier die_task;
101
102
103 #if VERBOSE
104 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
105 #else
106 #define OKPP do { ok++; } while (0)
107 #endif
108
109
110 static void
111 end ()
112 {
113   /* do work here */
114   GNUNET_assert (ok == 6);
115   GNUNET_SCHEDULER_cancel (die_task);
116   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
117   GNUNET_TRANSPORT_disconnect (p1.th);
118   GNUNET_TRANSPORT_disconnect (p2.th);
119   if (p1.stats != NULL)
120           GNUNET_STATISTICS_destroy (p1.stats, GNUNET_NO);
121   die_task = GNUNET_SCHEDULER_NO_TASK;
122   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
123   ok = 0;
124 }
125
126 static void
127 stop_arm (struct PeerContext *p)
128 {
129 #if START_ARM
130   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
131     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
132   GNUNET_OS_process_wait (p->arm_proc);
133   GNUNET_OS_process_close (p->arm_proc);
134   p->arm_proc = NULL;
135 #endif
136   GNUNET_CONFIGURATION_destroy (p->cfg);
137 }
138
139
140 static void
141 end_badly ()
142 {
143   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
144   //GNUNET_break (0);
145   GNUNET_TRANSPORT_disconnect (p1.th);
146   if (p1.stats != NULL)
147           GNUNET_STATISTICS_destroy (p1.stats, GNUNET_NO);
148   //GNUNET_TRANSPORT_disconnect (p2.th);
149   ok = 1;
150 }
151
152 static void
153 notify_receive (void *cls,
154                 const struct GNUNET_PeerIdentity *peer,
155                 const struct GNUNET_MessageHeader *message,
156                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
157                 uint32_t ats_count)
158 {
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
160               ok);
161
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
163                 ntohs(message->type), cls);
164
165   GNUNET_assert (ok == 5);
166   OKPP;
167
168   GNUNET_assert (MTYPE == ntohs (message->type));
169   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
170                  ntohs (message->size));
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
172               cls);
173   end ();
174 }
175
176
177 static size_t
178 notify_ready (void *cls, size_t size, void *buf)
179 {
180   struct GNUNET_MessageHeader *hdr;
181
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183               "Transmitting message to peer (%p) - %u!\n", cls, sizeof (struct GNUNET_MessageHeader));
184   GNUNET_assert (size >= 256);
185   GNUNET_assert (ok == 4);
186   OKPP;
187
188   if (buf != NULL)
189   {
190     hdr = buf;
191     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
192     hdr->type = htons (MTYPE);
193   }
194
195   return sizeof (struct GNUNET_MessageHeader);
196 }
197
198 static size_t
199 notify_ready_connect (void *cls, size_t size, void *buf)
200 {
201   return 0;
202 }
203
204
205 static void
206 notify_connect (void *cls,
207                 const struct GNUNET_PeerIdentity *peer,
208                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
209                 uint32_t ats_count)
210 {
211   if (cls == &p1)
212     {
213
214       GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
215                                               &p2.id,
216                                               256, 0, TIMEOUT, &notify_ready,
217                                               &p1);
218     }
219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220               "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
221 }
222
223
224 static void
225 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
226 {
227   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228               "Peer `%4s' disconnected (%p)!\n",
229               GNUNET_i2s (peer), cls);
230 }
231
232 int stat_cb (void *cls,
233                    const char *subsystem,
234                    const char *name,
235                    uint64_t value,
236                    int is_persistent)
237 {
238           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239                       "Execution time for 100 Peers: %llu\n",value);
240           return GNUNET_NO;
241 }
242
243
244 static void
245 setup_peer (struct PeerContext *p, const char *cfgname)
246 {
247   p->cfg = GNUNET_CONFIGURATION_create ();
248 #if START_ARM
249   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
250                                         "gnunet-service-arm",
251 #if VERBOSE_ARM
252                                         "-L", "DEBUG",
253 #endif
254                                         "-c", cfgname, NULL);
255 #endif
256   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
257   p->stats = GNUNET_STATISTICS_create ("transport", p->cfg);
258   GNUNET_assert (p->stats != NULL);
259   GNUNET_assert (GNUNET_OK == GNUNET_STATISTICS_watch (p->stats,"transport","ATS execution time 100 peers", stat_cb, NULL));
260
261   p->th = GNUNET_TRANSPORT_connect (p->cfg,
262                                     NULL, p,
263                                     &notify_receive,
264                                     &notify_connect, &notify_disconnect);
265   GNUNET_assert (p->th != NULL);
266
267   GNUNET_SCHEDULER_cancel (die_task);
268   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
269                                        &end_badly, NULL);
270 }
271
272
273 static void
274 exchange_hello_last (void *cls,
275                      const struct GNUNET_MessageHeader *message)
276 {
277   struct PeerContext *me = cls;
278
279   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
280   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281               "Exchanging HELLO with peer (%p)!\n", cls);
282   GNUNET_assert (ok >= 3);
283   OKPP;
284   GNUNET_assert (message != NULL);
285   GNUNET_assert (GNUNET_OK ==
286                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
287                                       message, &me->id));
288
289   GNUNET_assert(NULL != GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
290                                           &p1.id,
291                                           sizeof (struct GNUNET_MessageHeader), 0,
292                                           TIMEOUT,
293                                           &notify_ready_connect,
294                                           NULL));
295
296   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297               "Finished exchanging HELLOs, now waiting for transmission!\n");
298 }
299
300 static void
301 exchange_hello (void *cls,
302                 const struct GNUNET_MessageHeader *message)
303 {
304   struct PeerContext *me = cls;
305
306   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308               "Exchanging HELLO with peer (%p)!\n", cls);
309   GNUNET_assert (ok >= 2);
310   OKPP;
311   GNUNET_assert (message != NULL);
312   GNUNET_assert (GNUNET_OK ==
313                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
314                                       message, &me->id));
315
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
318
319   GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
320   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
321 }
322
323
324 static void
325 run (void *cls,
326      char *const *args,
327      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
328 {
329   GNUNET_assert (ok == 1);
330   OKPP;
331   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
332                                            &end_badly, NULL);
333
334   setup_peer (&p1, "test_transport_ats_peer1.conf");
335   //setup_peer (&p2, "test_transport_ats_peer2.conf");
336
337   GNUNET_assert(p1.th != NULL);
338   //GNUNET_assert(p2.th != NULL);
339   return;
340   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
341 }
342
343 static int
344 check ()
345 {
346   static char *const argv[] = { "test-transport-api",
347     "-c",
348     "test_transport_api_data.conf",
349 #if VERBOSE
350     "-L", "DEBUG",
351 #endif
352     NULL
353   };
354   static struct GNUNET_GETOPT_CommandLineOption options[] = {
355     GNUNET_GETOPT_OPTION_END
356   };
357
358 #if WRITECONFIG
359   setTransportOptions("test_transport_api_data.conf");
360 #endif
361   ok = 1;
362   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
363                       argv, "test-transport-api", "nohelp",
364                       options, &run, &ok);
365   stop_arm (&p1);
366  // stop_arm (&p2);
367
368   return ok;
369 }
370
371 /**
372  * Return the actual path to a file found in the current
373  * PATH environment variable.
374  *
375  * @param binary the name of the file to find
376  */
377 static char *
378 get_path_from_PATH (char *binary)
379 {
380   char *path;
381   char *pos;
382   char *end;
383   char *buf;
384   const char *p;
385
386   p = getenv ("PATH");
387   if (p == NULL)
388     {
389       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
390                   _("PATH environment variable is unset.\n"));
391       return NULL;
392     }
393   path = GNUNET_strdup (p);     /* because we write on it */
394   buf = GNUNET_malloc (strlen (path) + 20);
395   pos = path;
396
397   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
398     {
399       *end = '\0';
400       sprintf (buf, "%s/%s", pos, binary);
401       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
402         {
403           GNUNET_free (path);
404           return buf;
405         }
406       pos = end + 1;
407     }
408   sprintf (buf, "%s/%s", pos, binary);
409   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
410     {
411       GNUNET_free (path);
412       return buf;
413     }
414   GNUNET_free (buf);
415   GNUNET_free (path);
416   return NULL;
417 }
418
419 /**
420  * Check whether the suid bit is set on a file.
421  * Attempts to find the file using the current
422  * PATH environment variable as a search path.
423  *
424  * @param binary the name of the file to check
425  *
426  * @return GNUNET_YES if the binary is found and
427  *         can be run properly, GNUNET_NO otherwise
428  */
429 static int
430 check_gnunet_nat_binary(char *binary)
431 {
432   struct stat statbuf;
433   char *p;
434 #ifdef MINGW
435   SOCKET rawsock;
436 #endif
437
438 #ifdef MINGW
439   char *binaryexe;
440   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
441   p = get_path_from_PATH (binaryexe);
442   free (binaryexe);
443 #else
444   p = get_path_from_PATH (binary);
445 #endif
446   if (p == NULL)
447     {
448       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
449                   _("Could not find binary `%s' in PATH!\n"),
450                   binary);
451       return GNUNET_NO;
452     }
453   if (0 != STAT (p, &statbuf))
454     {
455       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
456                   _("stat (%s) failed: %s\n"),
457                   p,
458                   STRERROR (errno));
459       GNUNET_free (p);
460       return GNUNET_SYSERR;
461     }
462   GNUNET_free (p);
463 #ifndef MINGW
464   if ( (0 != (statbuf.st_mode & S_ISUID)) &&
465        (statbuf.st_uid == 0) )
466     return GNUNET_YES;
467   return GNUNET_NO;
468 #else
469   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
470   if (INVALID_SOCKET == rawsock)
471     {
472       DWORD err = GetLastError ();
473       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
474                   "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
475       return GNUNET_NO; /* not running as administrator */
476     }
477   closesocket (rawsock);
478   return GNUNET_YES;
479 #endif
480 }
481
482 int
483 main (int argc, char *argv[])
484 {
485   int ret;
486 #ifdef MINGW
487   return GNUNET_SYSERR;
488 #endif
489
490   GNUNET_log_setup ("test-transport-ats",
491 #if VERBOSE
492                     "DEBUG",
493 #else
494                     "WARNING",
495 #endif
496                     NULL);
497
498   if (strstr(argv[0], "tcp_nat") != NULL)
499     {
500       is_tcp_nat = GNUNET_YES;
501       if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
502         {
503           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
504                       "`%s' not properly installed, cannot run NAT test!\n",
505                       "gnunet-nat-server");
506           return 0;
507         }
508     }
509   else if (strstr(argv[0], "tcp") != NULL)
510     {
511       is_tcp = GNUNET_YES;
512     }
513   else if (strstr(argv[0], "udp_nat") != NULL)
514     {
515       is_udp_nat = GNUNET_YES;
516       if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
517         {
518           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
519                       "`%s' not properly installed, cannot run NAT test!\n",
520                       "gnunet-nat-server");
521           return 0;
522         }
523     }
524   else if (strstr(argv[0], "udp") != NULL)
525     {
526       is_udp = GNUNET_YES;
527     }
528   else if (strstr(argv[0], "unix") != NULL)
529     {
530       is_unix = GNUNET_YES;
531     }
532   else if (strstr(argv[0], "https") != NULL)
533     {
534       is_https = GNUNET_YES;
535     }
536   else if (strstr(argv[0], "http") != NULL)
537     {
538       is_http = GNUNET_YES;
539     }
540   else if (strstr(argv[0], "wlan") != NULL)
541     {
542        is_wlan = GNUNET_YES;
543     }
544   else if (strstr(argv[0], "multi") != NULL)
545     {
546        is_multi_protocol = GNUNET_YES;
547     }
548
549   ret = check ();
550   if (is_multi_protocol)
551   {
552          GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-1/");
553          GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-2/");
554   }
555   else
556   {
557          GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
558          GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
559   }
560
561   //return ret;
562   /* GGG */
563   return 0;
564 }
565
566 /* end of test_transport_ats.c */