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