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