Fix: Cast closure to proper type
[oweals/gnunet.git] / src / statistics / test_statistics_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2012, 2016 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 statistics/test_statistics_api.c
20  * @brief testcase for statistics_api.c
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
26
27
28 static struct GNUNET_STATISTICS_Handle *h;
29
30 static struct GNUNET_STATISTICS_GetHandle *g;
31
32
33 static void
34 do_shutdown ()
35 {
36   if (NULL != g)
37   {
38     GNUNET_STATISTICS_get_cancel (g);
39     g = NULL;
40   }
41   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
42   h = NULL;
43 }
44
45
46 static int
47 check_1 (void *cls,
48          const char *subsystem,
49          const char *name,
50          uint64_t value,
51          int is_persistent)
52 {
53   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
54               "Received value %llu for `%s:%s\n",
55               (unsigned long long) value,
56               subsystem,
57               name);
58   GNUNET_assert (0 == strcmp (name, "test-1"));
59   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
60   GNUNET_assert (value == 1);
61   GNUNET_assert (is_persistent == GNUNET_NO);
62   return GNUNET_OK;
63 }
64
65
66 static int
67 check_2 (void *cls,
68          const char *subsystem,
69          const char *name,
70          uint64_t value,
71          int is_persistent)
72 {
73   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74               "Received value %llu for `%s:%s\n",
75               (unsigned long long) value,
76               subsystem,
77               name);
78   GNUNET_assert (0 == strcmp (name, "test-2"));
79   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
80   GNUNET_assert (value == 2);
81   GNUNET_assert (is_persistent == GNUNET_NO);
82   return GNUNET_OK;
83 }
84
85
86 static int
87 check_3 (void *cls,
88          const char *subsystem,
89          const char *name,
90          uint64_t value,
91          int is_persistent)
92 {
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94               "Received value %llu for `%s:%s\n",
95               (unsigned long long) value,
96               subsystem,
97               name);
98   GNUNET_assert (0 == strcmp (name, "test-3"));
99   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
100   GNUNET_assert (value == 3);
101   GNUNET_assert (is_persistent == GNUNET_YES);
102   return GNUNET_OK;
103 }
104
105
106 static void
107 next_fin (void *cls,
108           int success)
109 {
110   int *ok = cls;
111
112   g = NULL;
113   GNUNET_SCHEDULER_shutdown ();
114   GNUNET_assert (success == GNUNET_OK);
115   *ok = 0;
116 }
117
118
119 static void
120 next (void *cls, int success)
121 {
122   g = NULL;
123   GNUNET_assert (success == GNUNET_OK);
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125               "Issuing GET request\n");
126   GNUNET_break (NULL !=
127                 GNUNET_STATISTICS_get (h, NULL, "test-2",
128                                        &next_fin,
129                                        &check_2, cls));
130 }
131
132
133 static void
134 run (void *cls,
135      char *const *args,
136      const char *cfgfile,
137      const struct GNUNET_CONFIGURATION_Handle *cfg)
138 {
139   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
140   if (NULL == h)
141   {
142     GNUNET_break (0);
143     return;
144   }
145   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
146                                  NULL);
147   GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO);
148   GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO);
149   GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO);
150   GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES);
151   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152               "Issuing GET request\n");
153   GNUNET_break (NULL !=
154                 (g = GNUNET_STATISTICS_get (h, NULL, "test-1",
155                                             &next,
156                                             &check_1, cls)));
157 }
158
159
160 static void
161 run_more (void *cls,
162           char *const *args,
163           const char *cfgfile,
164           const struct GNUNET_CONFIGURATION_Handle *cfg)
165 {
166   h = GNUNET_STATISTICS_create ("test-statistics-api",
167                                 cfg);
168   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
169                                  NULL);
170   GNUNET_break (NULL !=
171                 (g = GNUNET_STATISTICS_get (h, NULL,
172                                             "test-3",
173                                             &next_fin,
174                                             &check_3, cls)));
175 }
176
177
178 int
179 main (int argc, char *argv_ign[])
180 {
181   int ok = 1;
182   char *const argv[] = { "test-statistics-api",
183     "-c",
184     "test_statistics_api_data.conf",
185     "-L", "WARNING",
186     NULL
187   };
188   struct GNUNET_GETOPT_CommandLineOption options[] = {
189     GNUNET_GETOPT_OPTION_END
190   };
191   struct GNUNET_OS_Process *proc;
192   char *binary;
193
194   GNUNET_log_setup ("test_statistics_api",
195                     "WARNING",
196                     NULL);
197   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
198   proc =
199       GNUNET_OS_start_process (GNUNET_YES,
200                                GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
201                                NULL, NULL, NULL,
202                                binary,
203                                "gnunet-service-statistics",
204                                "-c", "test_statistics_api_data.conf", NULL);
205   GNUNET_assert (NULL != proc);
206   GNUNET_PROGRAM_run (5, argv,
207                       "test-statistics-api", "nohelp",
208                       options, &run,
209                       &ok);
210   if (0 != GNUNET_OS_process_kill (proc,
211                                    GNUNET_TERM_SIG))
212   {
213     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
214     ok = 1;
215   }
216   GNUNET_OS_process_wait (proc);
217   GNUNET_OS_process_destroy (proc);
218   proc = NULL;
219   if (ok != 0)
220   {
221     GNUNET_free (binary);
222     return ok;
223   }
224   ok = 1;
225   /* restart to check persistence! */
226   proc =
227       GNUNET_OS_start_process (GNUNET_YES,
228                                GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
229                                NULL, NULL, NULL,
230                                binary,
231                                "gnunet-service-statistics",
232                                "-c", "test_statistics_api_data.conf",
233                                NULL);
234   GNUNET_PROGRAM_run (5, argv,
235                       "test-statistics-api", "nohelp",
236                       options,
237                       &run_more, &ok);
238   if (0 != GNUNET_OS_process_kill (proc,
239                                    GNUNET_TERM_SIG))
240   {
241     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
242     ok = 1;
243   }
244   GNUNET_OS_process_wait (proc);
245   GNUNET_OS_process_destroy (proc);
246   proc = NULL;
247   GNUNET_free (binary);
248   return ok;
249 }
250
251 /* end of test_statistics_api.c */