dace6a5e985fc55b9e9f53baeeb5b37a2b1c6690
[oweals/gnunet.git] / src / transport / test_transport_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 2, 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
39 #define VERBOSE GNUNET_NO
40
41 #define VERBOSE_ARM GNUNET_NO
42
43 #define START_ARM GNUNET_YES
44
45 /**
46  * How long until we give up on transmitting the message?
47  */
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
49
50 #define MTYPE 12345
51
52 struct PeerContext
53 {
54   struct GNUNET_CONFIGURATION_Handle *cfg;
55   struct GNUNET_TRANSPORT_Handle *th;
56   struct GNUNET_PeerIdentity id;
57 #if START_ARM
58   pid_t arm_pid;
59 #endif
60 };
61
62 static struct PeerContext p1;
63
64 static struct PeerContext p2;
65
66 static struct GNUNET_SCHEDULER_Handle *sched;
67
68 static int ok;
69
70 static int is_tcp;
71
72 static int is_udp;
73
74 static int is_udp_nat;
75
76 static int is_http;
77
78 static  GNUNET_SCHEDULER_TaskIdentifier die_task;
79
80 #if VERBOSE
81 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
82 #else
83 #define OKPP do { ok++; } while (0)
84 #endif
85
86
87 static void
88 end ()
89 {
90   /* do work here */
91   GNUNET_assert (ok == 6);
92   GNUNET_SCHEDULER_cancel (sched, die_task);
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
94   GNUNET_TRANSPORT_disconnect (p1.th);
95   GNUNET_TRANSPORT_disconnect (p2.th);
96
97   die_task = GNUNET_SCHEDULER_NO_TASK;
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
99   ok = 0;
100 }
101
102 static void
103 stop_arm (struct PeerContext *p)
104 {
105 #if START_ARM
106   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
107     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
108   GNUNET_OS_process_wait (p->arm_pid);
109 #endif
110   GNUNET_CONFIGURATION_destroy (p->cfg);
111 }
112
113
114 static void
115 end_badly ()
116 {
117   GNUNET_break (0);
118   GNUNET_TRANSPORT_disconnect (p1.th);
119   GNUNET_TRANSPORT_disconnect (p2.th);
120   ok = 1;
121 }
122
123 static void
124 notify_receive (void *cls,
125                 const struct GNUNET_PeerIdentity *peer,
126                 const struct GNUNET_MessageHeader *message,
127                 struct GNUNET_TIME_Relative latency,
128                 uint32_t distance)
129 {
130   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
131               ok);
132
133   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
134                 ntohs(message->type), cls);
135
136   GNUNET_assert (ok == 5);
137   OKPP;
138
139   GNUNET_assert (MTYPE == ntohs (message->type));
140   GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
141                  ntohs (message->size));
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
143               cls);
144   end ();
145 }
146
147
148 static size_t
149 notify_ready (void *cls, size_t size, void *buf)
150 {
151   struct GNUNET_MessageHeader *hdr;
152
153   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154               "Transmitting message to peer (%p) - %u!\n", cls, size);
155   GNUNET_assert (size >= 256);
156   GNUNET_assert (ok == 4);
157   OKPP;
158   if (buf != NULL)
159   {
160     hdr = buf;
161     hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
162     hdr->type = htons (MTYPE);
163   }
164
165   return sizeof (struct GNUNET_MessageHeader);
166 }
167
168
169 static void
170 notify_connect (void *cls,
171                 const struct GNUNET_PeerIdentity *peer,
172                 struct GNUNET_TIME_Relative latency,
173                 uint32_t distance)
174 {
175   if (cls == &p1)
176     {
177       GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
178                                               &p2.id,
179                                               256, 0, TIMEOUT, &notify_ready,
180                                               &p1);
181     }
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183               "Peer `%4s' connected to us (%p)!\n", 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,
191               "Peer `%4s' disconnected (%p)!\n",
192               GNUNET_i2s (peer), cls);
193 }
194
195
196 static void
197 setup_peer (struct PeerContext *p, const char *cfgname)
198 {
199   p->cfg = GNUNET_CONFIGURATION_create ();
200 #if START_ARM
201   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
202                                         "gnunet-service-arm",
203 #if VERBOSE_ARM
204                                         "-L", "DEBUG",
205 #endif
206                                         "-c", cfgname, NULL);
207 #endif
208   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
209
210   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
211                                     p,
212                                     &notify_receive,
213                                     &notify_connect, &notify_disconnect);
214   GNUNET_assert (p->th != NULL);
215 }
216
217
218 static void
219 exchange_hello_last (void *cls,
220                      const struct GNUNET_MessageHeader *message)
221 {
222   struct PeerContext *me = cls;
223
224   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226               "Exchanging HELLO with peer (%p)!\n", cls);
227   GNUNET_assert (ok >= 3);
228   OKPP;
229   GNUNET_assert (message != NULL);
230   GNUNET_assert (GNUNET_OK ==
231                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
232                                       message, &me->id));
233
234   /* Can't we get away with only offering one hello? */
235   /* GNUNET_TRANSPORT_offer_hello (p1.th, message); */
236
237   /*sleep(1);*/ /* Make sure we are not falling prey to the "favorable timing" bug... */
238
239   /* both HELLOs exchanged, get ready to test transmission! */
240   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241               "Finished exchanging HELLOs, now waiting for transmission!\n");
242 }
243
244 static void
245 exchange_hello (void *cls,
246                 const struct GNUNET_MessageHeader *message)
247 {
248   struct PeerContext *me = cls;
249
250   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252               "Exchanging HELLO with peer (%p)!\n", cls);
253   GNUNET_assert (ok >= 2);
254   OKPP;
255   GNUNET_assert (message != NULL);
256   GNUNET_assert (GNUNET_OK ==
257                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
258                                       message, &me->id));
259
260   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261               "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
262
263   GNUNET_TRANSPORT_offer_hello (p2.th, message);
264   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
265 }
266
267 static void
268 run (void *cls,
269      struct GNUNET_SCHEDULER_Handle *s,
270      char *const *args,
271      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
272 {
273   GNUNET_assert (ok == 1);
274   OKPP;
275   sched = s;
276
277   die_task = GNUNET_SCHEDULER_add_delayed (sched,
278       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
279
280   if (is_udp)
281     {
282       setup_peer (&p1, "test_transport_api_udp_peer1.conf");
283       setup_peer (&p2, "test_transport_api_udp_peer2.conf");
284     }
285   else if (is_tcp)
286     {
287       setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
288       setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
289     }
290   if (is_udp_nat)
291     {
292       setup_peer (&p1, "test_transport_api_udp_nat_peer1.conf");
293       setup_peer (&p2, "test_transport_api_udp_nat_peer2.conf");
294     }
295   if (is_http)
296     {
297       setup_peer (&p1, "test_transport_api_http_peer1.conf");
298       setup_peer (&p2, "test_transport_api_http_peer2.conf");
299     }
300
301   GNUNET_assert(p1.th != NULL);
302   GNUNET_assert(p2.th != NULL);
303
304   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
305 }
306
307 static int
308 check ()
309 {
310
311   char *const argv[] = { "test-transport-api",
312     "-c",
313     "test_transport_api_data.conf",
314 #if VERBOSE
315     "-L", "DEBUG",
316 #endif
317     NULL
318   };
319
320 #if WRITECONFIG
321   setTransportOptions("test_transport_api_data.conf");
322 #endif
323
324   struct GNUNET_GETOPT_CommandLineOption options[] = {
325     GNUNET_GETOPT_OPTION_END
326   };
327
328   ok = 1;
329   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
330                       argv, "test-transport-api", "nohelp",
331                       options, &run, &ok);
332   stop_arm (&p1);
333   stop_arm (&p2);
334   return ok;
335 }
336
337
338 static char *
339 get_path_from_PATH ()
340 {
341   char *path;
342   char *pos;
343   char *end;
344   char *buf;
345   const char *p;
346
347   p = getenv ("PATH");
348   if (p == NULL)
349     return NULL;
350   path = GNUNET_strdup (p);     /* because we write on it */
351   buf = GNUNET_malloc (strlen (path) + 20);
352   pos = path;
353
354   while (NULL != (end = strchr (pos, ':')))
355     {
356       *end = '\0';
357       sprintf (buf, "%s/%s", pos, "gnunet-nat-server");
358       if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
359         {
360           GNUNET_free (path);
361           return buf;
362         }
363       pos = end + 1;
364     }
365   sprintf (buf, "%s/%s", pos, "gnunet-nat-server");
366   if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
367     {
368       GNUNET_free (path);
369       return buf;
370     }
371   GNUNET_free (buf);
372   GNUNET_free (path);
373   return NULL;
374 }
375
376
377 static int 
378 check_gnunet_nat_server()
379 {
380   struct stat statbuf;
381   char *p;
382
383   p = get_path_from_PATH ();
384   if (p == NULL)
385     return GNUNET_NO;
386   if (0 != STAT (p, &statbuf))
387     {
388       GNUNET_free (p);
389       return GNUNET_SYSERR;
390     }
391   GNUNET_free (p);
392   if ( (0 != (statbuf.st_mode & S_ISUID)) && 
393        (statbuf.st_uid == 0) )    
394     return GNUNET_YES;
395   return GNUNET_NO;
396 }
397
398 int
399 main (int argc, char *argv[])
400 {
401   int ret;
402 #ifdef MINGW
403   return GNUNET_SYSERR;
404 #endif
405   if (strstr(argv[0], "tcp") != NULL)
406     {
407       is_tcp = GNUNET_YES;
408     }
409   else if (strstr(argv[0], "udp_nat") != NULL)
410     {
411       is_udp_nat = GNUNET_YES;
412       if (check_gnunet_nat_server() != GNUNET_OK)
413         {
414           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
415                       "`%s' not properly installed, cannot run NAT test!\n",
416                       "gnunet-nat-server");
417           return 0;
418         }
419     }
420   else if (strstr(argv[0], "udp") != NULL)
421     {
422       is_udp = GNUNET_YES;
423     }
424   else if (strstr(argv[0], "http") != NULL)
425     {
426       is_http = GNUNET_YES;
427     }
428
429
430   GNUNET_log_setup ("test-transport-api",
431 #if VERBOSE
432                     "DEBUG",
433 #else
434                     "WARNING",
435 #endif
436                     NULL);
437   ret = check ();
438   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
439   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
440   return ret;
441 }
442
443 /* end of test_transport_api.c */