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