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