4ce2b7f3126f884018ad145590146df39e1e33ec
[oweals/gnunet.git] / src / transport / test_transport_api.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.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 "transport.h"
38 #include "transport-testing.h"
39
40 #define VERBOSE GNUNET_NO
41
42 #define VERBOSE_ARM GNUNET_NO
43
44 #define START_ARM GNUNET_YES
45
46 /**
47  * How long until we give up on transmitting the message?
48  */
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
50
51 /**
52  * How long until we give up on transmitting the message?
53  */
54 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
55
56 #define MTYPE 12345
57
58 static int ok;
59
60 static GNUNET_SCHEDULER_TaskIdentifier die_task;
61
62 static GNUNET_SCHEDULER_TaskIdentifier send_task;
63
64 struct PeerContext *p1;
65
66 struct PeerContext *p2;
67
68 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
69
70 struct GNUNET_TRANSPORT_TransmitHandle *th;
71
72 char *cfg_file_p1;
73
74 char *cfg_file_p2;
75
76 #if VERBOSE
77 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
78 #else
79 #define OKPP do { ok++; } while (0)
80 #endif
81
82
83 static void
84 end ()
85 {
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
87
88   if (send_task != GNUNET_SCHEDULER_NO_TASK)
89     GNUNET_SCHEDULER_cancel (send_task);
90
91   if (die_task != GNUNET_SCHEDULER_NO_TASK)
92     GNUNET_SCHEDULER_cancel (die_task);
93
94   if (th != NULL)
95     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
96   th = NULL;
97
98   GNUNET_TRANSPORT_TESTING_stop_peer (p1);
99   GNUNET_TRANSPORT_TESTING_stop_peer (p2);
100 }
101
102 static void
103 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   die_task = GNUNET_SCHEDULER_NO_TASK;
106
107   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Stopping peers\n");
108
109   if (send_task != GNUNET_SCHEDULER_NO_TASK)
110     GNUNET_SCHEDULER_cancel (send_task);
111
112   if  (cc != NULL)
113     GNUNET_TRANSPORT_TESTING_connect_peers_cancel(cc);
114
115   if (th != NULL)
116     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
117   th = NULL;
118
119   if (p1 != NULL)
120     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
121   if (p2 != NULL)
122     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
123
124   ok = GNUNET_SYSERR;
125   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Stopping peers END\n");
126 }
127
128
129 static void
130 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
131                 const struct GNUNET_MessageHeader *message,
132                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
133                 uint32_t ats_count)
134 {
135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136               "Received message of type %d from peer %s!\n",
137               ntohs (message->type), GNUNET_i2s (peer));
138
139   if ((MTYPE == ntohs (message->type)) &&
140       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
141   {
142     ok = 0;
143     end ();
144   }
145   else
146   {
147     GNUNET_break (0);
148     ok = 1;
149     end ();
150   }
151 }
152
153
154 static size_t
155 notify_ready (void *cls, size_t size, void *buf)
156 {
157   struct PeerContext *p = cls;
158   struct GNUNET_MessageHeader *hdr;
159
160   th = NULL;
161
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163               "Transmitting message with %u bytes to peer %s\n",
164               sizeof (struct GNUNET_MessageHeader), GNUNET_i2s (&p->id));
165   GNUNET_assert (size >= 256);
166
167   if (buf != NULL)
168   {
169     hdr = buf;
170     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
171     hdr->type = htons (MTYPE);
172   }
173   return sizeof (struct GNUNET_MessageHeader);
174 }
175
176
177 static void
178 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
179                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
180                 uint32_t ats_count)
181 {
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
183               GNUNET_i2s (peer), cls);
184 }
185
186
187 static void
188 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
189 {
190   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
191               GNUNET_i2s (peer), cls);
192 }
193
194 static void
195 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 {
197   send_task = GNUNET_SCHEDULER_NO_TASK;
198
199   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
200     return;
201
202   th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, 256, 0, TIMEOUT,
203                                                &notify_ready, &p1);
204 }
205
206 static void
207 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
208 {
209   cc = NULL;
210   char *p1_c = strdup (GNUNET_i2s (&p1->id));
211
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
213               GNUNET_i2s (&p2->id));
214   GNUNET_free (p1_c);
215
216   // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
217   send_task =
218       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
219 }
220
221 static void
222 run (void *cls, char *const *args, const char *cfgfile,
223      const struct GNUNET_CONFIGURATION_Handle *cfg)
224 {
225   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
226
227   p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, &notify_receive,
228                                             &notify_connect, &notify_disconnect,
229                                             NULL);
230   p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, &notify_receive,
231                                             &notify_connect, &notify_disconnect,
232                                             NULL);
233   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NOW!\n");
234   cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
235 }
236
237 static int
238 check ()
239 {
240   static char *const argv[] = { "test-transport-api",
241     "-c",
242     "test_transport_api_data.conf",
243 #if VERBOSE
244     "-L", "DEBUG",
245 #endif
246     NULL
247   };
248   static struct GNUNET_GETOPT_CommandLineOption options[] = {
249     GNUNET_GETOPT_OPTION_END
250   };
251
252 #if WRITECONFIG
253   setTransportOptions ("test_transport_api_data.conf");
254 #endif
255   send_task = GNUNET_SCHEDULER_NO_TASK;
256
257   ok = 1;
258   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
259                       "test-transport-api", "nohelp", options, &run, &ok);
260
261   return ok;
262 }
263
264 /**
265  * Return the actual path to a file found in the current
266  * PATH environment variable.
267  *
268  * @param binary the name of the file to find
269  */
270 static char *
271 get_path_from_PATH (char *binary)
272 {
273   char *path;
274   char *pos;
275   char *end;
276   char *buf;
277   const char *p;
278
279   p = getenv ("PATH");
280   if (p == NULL)
281   {
282     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
283                 _("PATH environment variable is unset.\n"));
284     return NULL;
285   }
286   path = GNUNET_strdup (p);     /* because we write on it */
287   buf = GNUNET_malloc (strlen (path) + 20);
288   pos = path;
289
290   while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
291   {
292     *end = '\0';
293     sprintf (buf, "%s/%s", pos, binary);
294     if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
295     {
296       GNUNET_free (path);
297       return buf;
298     }
299     pos = end + 1;
300   }
301   sprintf (buf, "%s/%s", pos, binary);
302   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
303   {
304     GNUNET_free (path);
305     return buf;
306   }
307   GNUNET_free (buf);
308   GNUNET_free (path);
309   return NULL;
310 }
311
312 /**
313  * Check whether the suid bit is set on a file.
314  * Attempts to find the file using the current
315  * PATH environment variable as a search path.
316  *
317  * @param binary the name of the file to check
318  *
319  * @return GNUNET_YES if the binary is found and
320  *         can be run properly, GNUNET_NO otherwise
321  */
322 static int
323 check_gnunet_nat_binary (char *binary)
324 {
325   struct stat statbuf;
326   char *p;
327
328 #ifdef MINGW
329   SOCKET rawsock;
330 #endif
331
332 #ifdef MINGW
333   char *binaryexe;
334
335   GNUNET_asprintf (&binaryexe, "%s.exe", binary);
336   p = get_path_from_PATH (binaryexe);
337   free (binaryexe);
338 #else
339   p = get_path_from_PATH (binary);
340 #endif
341   if (p == NULL)
342   {
343     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344                 _("Could not find binary `%s' in PATH!\n"), binary);
345     return GNUNET_NO;
346   }
347   if (0 != STAT (p, &statbuf))
348   {
349     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
350                 STRERROR (errno));
351     GNUNET_free (p);
352     return GNUNET_SYSERR;
353   }
354   GNUNET_free (p);
355 #ifndef MINGW
356   if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
357     return GNUNET_YES;
358   return GNUNET_NO;
359 #else
360   rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
361   if (INVALID_SOCKET == rawsock)
362   {
363     DWORD err = GetLastError ();
364
365     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366                 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n",
367                 err);
368     return GNUNET_NO;           /* not running as administrator */
369   }
370   closesocket (rawsock);
371   return GNUNET_YES;
372 #endif
373 }
374
375 int
376 main (int argc, char *argv[])
377 {
378   int ret;
379
380   GNUNET_log_setup ("test-transport-api",
381 #if VERBOSE
382                     "DEBUG",
383 #else
384                     "WARNING",
385 #endif
386                     NULL);
387
388   char *pch = strdup (argv[0]);
389   char *backup = pch;
390   char *filename = NULL;
391   char *dotexe;
392
393   /* get executable filename */
394   pch = strtok (pch, "/");
395   while (pch != NULL)
396   {
397     pch = strtok (NULL, "/");
398     if (pch != NULL)
399       filename = pch;
400   }
401   /* remove "lt-" */
402   filename = strstr (filename, "tes");
403   if (NULL != (dotexe = strstr (filename, ".exe")))
404     dotexe[0] = '\0';
405
406   /* create cfg filename */
407   GNUNET_asprintf (&cfg_file_p1, "%s_peer1.conf", filename);
408   GNUNET_asprintf (&cfg_file_p2, "%s_peer2.conf", filename);
409   GNUNET_free (backup);
410
411   if (strstr (argv[0], "tcp_nat") != NULL)
412   {
413     if (GNUNET_YES != check_gnunet_nat_binary ("gnunet-nat-server"))
414     {
415       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
416                   "`%s' not properly installed, cannot run NAT test!\n",
417                   "gnunet-nat-server");
418       return 0;
419     }
420   }
421   else if (strstr (argv[0], "udp_nat") != NULL)
422   {
423     if (GNUNET_YES != check_gnunet_nat_binary ("gnunet-nat-server"))
424     {
425       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426                   "`%s' not properly installed, cannot run NAT test!\n",
427                   "gnunet-nat-server");
428       return 0;
429     }
430   }
431
432   ret = check ();
433
434   GNUNET_free (cfg_file_p1);
435   GNUNET_free (cfg_file_p2);
436
437   return ret;
438 }
439
440 /* end of test_transport_api.c */