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