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