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