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