- fixing channel functions
[oweals/gnunet.git] / src / nat / test_nat_test.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2011 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 /**
22  * Testcase for the NAT testing code.
23  *
24  * @file nat/test_nat_test.c
25  * @brief Testcase for NAT testing functions
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_nat_lib.h"
31
32 /**
33  * Time to wait before stopping NAT test, in seconds
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
36
37
38 static int ret = 1;
39
40 static struct GNUNET_NAT_Test *tst;
41
42 static GNUNET_SCHEDULER_TaskIdentifier end;
43
44 static void
45 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
46 {
47   GNUNET_NAT_test_stop (tst);
48 }
49
50 static void
51 report_success (void *cls, int success)
52 {
53   GNUNET_assert (GNUNET_OK == success);
54   ret = 0;
55   GNUNET_SCHEDULER_cancel (end);
56   end = GNUNET_SCHEDULER_add_now (&end_test, NULL);
57 }
58
59 /**
60  * Main function run with scheduler.
61  */
62 static void
63 run (void *cls, char *const *args, const char *cfgfile,
64      const struct GNUNET_CONFIGURATION_Handle *cfg)
65 {
66   tst =
67       GNUNET_NAT_test_start (cfg, GNUNET_YES, 1285, 1285, &report_success,
68                              NULL);
69   if (NULL == tst)
70     return;
71   end = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
72 }
73
74
75 int
76 main (int argc, char *const argv[])
77 {
78   struct GNUNET_GETOPT_CommandLineOption options[] = {
79     GNUNET_GETOPT_OPTION_END
80   };
81   struct GNUNET_OS_Process *gns;
82   int nat_res;
83   char *const argv_prog[] = {
84     "test-nat-test",
85     "-c",
86     "test_nat_test_data.conf",
87     NULL
88   };
89
90   GNUNET_log_setup ("test-nat-test",
91                     "WARNING",
92                     NULL);
93
94   nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server", GNUNET_NO, NULL);
95   if (GNUNET_SYSERR == nat_res)
96   {
97     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
98                 "Cannot run NAT test: `%s' file not found\n",
99                 "gnunet-nat-server");
100     return 0;
101   }
102
103   gns =
104       GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-nat-server",
105                                "gnunet-nat-server",
106                                "-c", "test_nat_test_data.conf", "12345", NULL);
107   GNUNET_assert (NULL != gns);
108   GNUNET_PROGRAM_run (3, argv_prog, "test-nat-test", "nohelp", options, &run,
109                       NULL);
110   GNUNET_break (0 == GNUNET_OS_process_kill (gns, SIGTERM));
111   GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (gns));
112   GNUNET_OS_process_destroy (gns);
113   if (0 != ret)
114     fprintf (stderr, "NAT test failed to report success\n");
115   return ret;
116 }
117
118 /* end of test_nat_test.c */