fix
[oweals/gnunet.git] / src / nse / test_nse_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 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  * @file nse/test_nse_api.c
22  * @brief testcase for nse_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_nse_service.h"
31
32 #define DEBUG_NSE GNUNET_YES
33
34 #define START_ARM GNUNET_YES
35
36 static struct GNUNET_NSE_Handle *h;
37
38 static GNUNET_SCHEDULER_TaskIdentifier die_task;
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43 #if START_ARM
44   struct GNUNET_OS_Process *arm_proc;
45 #endif
46 };
47
48 static struct PeerContext p1;
49
50
51 static void
52 stop_arm (struct PeerContext *p)
53 {
54 #if START_ARM
55   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
56     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
57   GNUNET_OS_process_wait (p->arm_proc);
58   GNUNET_OS_process_close (p->arm_proc);
59   p->arm_proc = NULL;
60 #endif
61   GNUNET_CONFIGURATION_destroy (p->cfg);
62 }
63
64 /**
65  * Signature of the main function of a task.
66  *
67  * @param cls closure
68  * @param tc context information (why was this task triggered now)
69  */
70 static void
71 end_test (void *cls,
72           const struct GNUNET_SCHEDULER_TaskContext * tc)
73 {
74   if (h != NULL)
75     {
76       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
77                   "Disconnecting from NSE service.\n");
78       GNUNET_NSE_disconnect (h);
79     }
80 }
81
82 /**
83  * Callback to call when network size estimate is updated.
84  *
85  * @param cls unused
86  * @param estimate the value of the current network size estimate
87  * @param std_dev standard deviation (rounded down to nearest integer)
88  *                of the size estimation values seen
89  *
90  */
91 static void
92 check_nse_message (void *cls, double estimate, double std_dev)
93 {
94   int *ok = cls;
95
96   fprintf (stderr,
97            "Received NSE message, estimate %f, standard deviation %f.\n");
98   /* Fantastic check below. Expect NaN, the only thing not equal to itself. */
99   (*ok) = 0;
100   if (die_task != GNUNET_SCHEDULER_NO_TASK)
101     GNUNET_SCHEDULER_cancel(die_task);
102   die_task = GNUNET_SCHEDULER_add_now(&end_test, NULL);
103 }
104
105
106 static void
107 setup_peer (struct PeerContext *p, const char *cfgname)
108 {
109   p->cfg = GNUNET_CONFIGURATION_create ();
110 #if START_ARM
111   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
112                                          "gnunet-service-arm",
113                                         "gnunet-service-arm",
114 #if VERBOSE_ARM
115                                         "-L", "DEBUG",
116 #endif
117                                         "-c", cfgname, NULL);
118 #endif
119   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
120
121 }
122
123
124
125 static void
126 run (void *cls,
127      char *const *args,
128      const char *cfgfile,
129      const struct GNUNET_CONFIGURATION_Handle *cfg)
130 {
131   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
132                                            (GNUNET_TIME_UNIT_MINUTES, 1),
133                                            &end_test, NULL);
134
135   setup_peer (&p1, cfgfile);
136   h = GNUNET_NSE_connect (cfg, &check_nse_message, cls);
137   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138               "Connecting to NSE service.\n");
139   GNUNET_assert (h != NULL);
140 }
141
142
143 static int
144 check ()
145 {
146   int ok = 1;
147   char *const argv[] = { "test-nse-api",
148     "-c",
149     "test_nse.conf",
150 #if DEBUG_NSE
151                          "-L", "DEBUG",
152 #else
153                          "-L", "WARNING",
154 #endif
155     NULL
156   };
157   struct GNUNET_GETOPT_CommandLineOption options[] = {
158     GNUNET_GETOPT_OPTION_END
159   };
160
161   GNUNET_PROGRAM_run (5, argv, "test-nse-api", "nohelp",
162                       options, &run, &ok);
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164               "Stopping arm.\n");
165   stop_arm (&p1);
166   return ok;
167 }
168
169
170 int
171 main (int argc, char *argv[])
172 {
173   int ret;
174
175   GNUNET_log_setup ("test_nse_api",
176 #if DEBUG_NSE
177                     "DEBUG",
178 #else
179                     "WARNING",
180 #endif
181                     NULL);
182   ret = check ();
183
184   return ret;
185 }
186
187 /* end of test_nse_api.c */