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