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