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