a7cbaeab4d32dd987e226798278999ebb2a57a92
[oweals/gnunet.git] / src / nat / test_nat_test.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2011, 2014 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file nat/test_nat_test.c
20  * @brief Testcase for NAT testing functions
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_nat_lib.h"
26
27 /**
28  * Time to wait before stopping NAT test, in seconds
29  */
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
31
32
33 static int ret = 1;
34
35 static struct GNUNET_NAT_Test *tst;
36
37 static struct GNUNET_SCHEDULER_Task * tsk;
38
39
40 static void
41 report_result (void *cls,
42                 enum GNUNET_NAT_StatusCode aret)
43 {
44   if (GNUNET_NAT_ERROR_TIMEOUT == aret)
45     fprintf (stderr,
46              "NAT test timed out\n");
47   else if (GNUNET_NAT_ERROR_SUCCESS != aret)
48     fprintf (stderr,
49              "NAT test reported error %d\n", aret);
50   else
51     ret = 0;
52   GNUNET_NAT_test_stop (tst);
53   tst = NULL;
54   GNUNET_SCHEDULER_cancel (tsk);
55   tsk = NULL;
56 }
57
58
59 static void
60 failed_timeout (void *cls)
61 {
62   tsk = NULL;
63   fprintf (stderr,
64            "NAT test failed to terminate on timeout\n");
65   ret = 2;
66   GNUNET_NAT_test_stop (tst);
67   tst = NULL;
68 }
69
70
71 /**
72  * Main function run with scheduler.
73  */
74 static void
75 run (void *cls, char *const *args, const char *cfgfile,
76      const struct GNUNET_CONFIGURATION_Handle *cfg)
77 {
78   tst =
79       GNUNET_NAT_test_start (cfg, GNUNET_YES, 1285, 1285, TIMEOUT,
80                              &report_result,
81                              NULL);
82   tsk = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (TIMEOUT,
83                                                                      2),
84                                       &failed_timeout,
85                                       NULL);
86
87 }
88
89
90 int
91 main (int argc, char *const argv[])
92 {
93   struct GNUNET_GETOPT_CommandLineOption options[] = {
94     GNUNET_GETOPT_OPTION_END
95   };
96   struct GNUNET_OS_Process *gns;
97   int nat_res;
98   char *const argv_prog[] = {
99     "test-nat-test",
100     "-c",
101     "test_nat_test_data.conf",
102     NULL
103   };
104
105   GNUNET_log_setup ("test-nat-test",
106                     "WARNING",
107                     NULL);
108
109   nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server", GNUNET_NO, NULL);
110   if (GNUNET_SYSERR == nat_res)
111   {
112     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
113                 "Cannot run NAT test: `%s' file not found\n",
114                 "gnunet-nat-server");
115     return 0;
116   }
117
118   gns = GNUNET_OS_start_process (GNUNET_YES,
119                                  GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
120                                  NULL, NULL, NULL,
121                                  "gnunet-nat-server",
122                                  "gnunet-nat-server",
123                                  "-c", "test_nat_test_data.conf",
124                                  "12345", NULL);
125   GNUNET_assert (NULL != gns);
126   GNUNET_PROGRAM_run (3, argv_prog,
127                       "test-nat-test", "nohelp",
128                       options, &run,
129                       NULL);
130   GNUNET_break (0 == GNUNET_OS_process_kill (gns, GNUNET_TERM_SIG));
131   GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns));
132   GNUNET_OS_process_destroy (gns);
133   if (0 != ret)
134     fprintf (stderr,
135              "NAT test failed to report success\n");
136   return ret;
137 }
138
139 /* end of test_nat_test.c */