-improve indentation, reduce duplication of PIDs in core's neighbour map
[oweals/gnunet.git] / src / nat / test_nat_test.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2011, 2014 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file nat/test_nat_test.c
22  * @brief Testcase for NAT testing functions
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_nat_lib.h"
28
29 /**
30  * Time to wait before stopping NAT test, in seconds
31  */
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
33
34
35 static int ret = 1;
36
37 static struct GNUNET_NAT_Test *tst;
38
39 static struct GNUNET_SCHEDULER_Task * tsk;
40
41
42 static void
43 report_result (void *cls,
44                 enum GNUNET_NAT_StatusCode aret)
45 {
46   if (GNUNET_NAT_ERROR_TIMEOUT == aret)
47     fprintf (stderr,
48              "NAT test timed out\n");
49   else if (GNUNET_NAT_ERROR_SUCCESS != aret)
50     fprintf (stderr,
51              "NAT test reported error %d\n", aret);
52   else
53     ret = 0;
54   GNUNET_NAT_test_stop (tst);
55   tst = NULL;
56   GNUNET_SCHEDULER_cancel (tsk);
57   tsk = NULL;
58 }
59
60
61 static void
62 failed_timeout (void *cls,
63                 const struct GNUNET_SCHEDULER_TaskContext *tc)
64 {
65   tsk = NULL;
66   fprintf (stderr,
67            "NAT test failed to terminate on timeout\n");
68   ret = 2;
69   GNUNET_NAT_test_stop (tst);
70   tst = NULL;
71 }
72
73
74 /**
75  * Main function run with scheduler.
76  */
77 static void
78 run (void *cls, char *const *args, const char *cfgfile,
79      const struct GNUNET_CONFIGURATION_Handle *cfg)
80 {
81   tst =
82       GNUNET_NAT_test_start (cfg, GNUNET_YES, 1285, 1285, TIMEOUT,
83                              &report_result,
84                              NULL);
85   tsk = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (TIMEOUT,
86                                                                      2),
87                                       &failed_timeout,
88                                       NULL);
89   
90 }
91
92
93 int
94 main (int argc, char *const argv[])
95 {
96   struct GNUNET_GETOPT_CommandLineOption options[] = {
97     GNUNET_GETOPT_OPTION_END
98   };
99   struct GNUNET_OS_Process *gns;
100   int nat_res;
101   char *const argv_prog[] = {
102     "test-nat-test",
103     "-c",
104     "test_nat_test_data.conf",
105     NULL
106   };
107
108   GNUNET_log_setup ("test-nat-test",
109                     "WARNING",
110                     NULL);
111
112   nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server", GNUNET_NO, NULL);
113   if (GNUNET_SYSERR == nat_res)
114   {
115     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
116                 "Cannot run NAT test: `%s' file not found\n",
117                 "gnunet-nat-server");
118     return 0;
119   }
120
121   gns = GNUNET_OS_start_process (GNUNET_YES,
122                                  GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
123                                  NULL, NULL, NULL,
124                                  "gnunet-nat-server",
125                                  "gnunet-nat-server",
126                                  "-c", "test_nat_test_data.conf",
127                                  "12345", NULL);
128   GNUNET_assert (NULL != gns);
129   GNUNET_PROGRAM_run (3, argv_prog,
130                       "test-nat-test", "nohelp", 
131                       options, &run,
132                       NULL);
133   GNUNET_break (0 == GNUNET_OS_process_kill (gns, GNUNET_TERM_SIG));
134   GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns));
135   GNUNET_OS_process_destroy (gns);
136   if (0 != ret)
137     fprintf (stderr,
138              "NAT test failed to report success\n");
139   return ret;
140 }
141
142 /* end of test_nat_test.c */