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