commented out wrong message 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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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   };
190   struct GNUNET_GETOPT_CommandLineOption options[] = {
191     GNUNET_GETOPT_OPTION_END
192   };
193   struct GNUNET_OS_Process *proc;
194   char *binary;
195
196   GNUNET_log_setup ("test_statistics_api",
197                     "WARNING",
198                     NULL);
199   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-statistics");
200   proc =
201       GNUNET_OS_start_process (GNUNET_YES,
202                                GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
203                                NULL, NULL, NULL,
204                                binary,
205                                "gnunet-service-statistics",
206                                "-c", "test_statistics_api_data.conf", NULL);
207   GNUNET_assert (NULL != proc);
208   GNUNET_PROGRAM_run (5, argv,
209                       "test-statistics-api", "nohelp",
210                       options, &run,
211                       &ok);
212   if (0 != GNUNET_OS_process_kill (proc,
213                                    GNUNET_TERM_SIG))
214   {
215     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
216     ok = 1;
217   }
218   GNUNET_OS_process_wait (proc);
219   GNUNET_OS_process_destroy (proc);
220   proc = NULL;
221   if (ok != 0)
222   {
223     GNUNET_free (binary);
224     return ok;
225   }
226   ok = 1;
227   /* restart to check persistence! */
228   proc =
229       GNUNET_OS_start_process (GNUNET_YES,
230                                GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
231                                NULL, NULL, NULL,
232                                binary,
233                                "gnunet-service-statistics",
234                                "-c", "test_statistics_api_data.conf",
235                                NULL);
236   GNUNET_PROGRAM_run (5, argv,
237                       "test-statistics-api", "nohelp",
238                       options,
239                       &run_more, &ok);
240   if (0 != GNUNET_OS_process_kill (proc,
241                                    GNUNET_TERM_SIG))
242   {
243     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
244     ok = 1;
245   }
246   GNUNET_OS_process_wait (proc);
247   GNUNET_OS_process_destroy (proc);
248   proc = NULL;
249   GNUNET_free (binary);
250   return ok;
251 }
252
253 /* end of test_statistics_api.c */