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