-fix leak
[oweals/gnunet.git] / src / statistics / test_statistics_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file statistics/test_statistics_api.c
22  * @brief testcase for statistics_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_getopt_lib.h"
27 #include "gnunet_os_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_statistics_service.h"
31
32 #define DEBUG_STATISTICS GNUNET_EXTRA_LOGGING
33
34 #define START_SERVICE GNUNET_YES
35
36 static int
37 check_1 (void *cls, const char *subsystem, const char *name, uint64_t value,
38          int is_persistent)
39 {
40   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received value %llu for `%s:%s\n",
41               (unsigned long long) value, subsystem, name);
42   GNUNET_assert (0 == strcmp (name, "test-1"));
43   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
44   GNUNET_assert (value == 1);
45   GNUNET_assert (is_persistent == GNUNET_NO);
46   return GNUNET_OK;
47 }
48
49 static int
50 check_2 (void *cls, const char *subsystem, const char *name, uint64_t value,
51          int is_persistent)
52 {
53   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received value %llu for `%s:%s\n",
54               (unsigned long long) value, subsystem, name);
55   GNUNET_assert (0 == strcmp (name, "test-2"));
56   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
57   GNUNET_assert (value == 2);
58   GNUNET_assert (is_persistent == GNUNET_NO);
59   return GNUNET_OK;
60 }
61
62 static int
63 check_3 (void *cls, const char *subsystem, const char *name, uint64_t value,
64          int is_persistent)
65 {
66   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received value %llu for `%s:%s\n",
67               (unsigned long long) value, subsystem, name);
68   GNUNET_assert (0 == strcmp (name, "test-3"));
69   GNUNET_assert (0 == strcmp (subsystem, "test-statistics-api"));
70   GNUNET_assert (value == 3);
71   GNUNET_assert (is_persistent == GNUNET_YES);
72   return GNUNET_OK;
73 }
74
75 static struct GNUNET_STATISTICS_Handle *h;
76
77 static void
78 next_fin (void *cls, int success)
79 {
80   int *ok = cls;
81
82   GNUNET_STATISTICS_destroy (h, GNUNET_NO);
83   GNUNET_assert (success == GNUNET_OK);
84   *ok = 0;
85 }
86
87 static void
88 next (void *cls, int success)
89 {
90   GNUNET_assert (success == GNUNET_OK);
91   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Issuing GET request\n");
92   GNUNET_break (NULL !=
93                 GNUNET_STATISTICS_get (h, NULL, "test-2",
94                                        GNUNET_TIME_UNIT_SECONDS, &next_fin,
95                                        &check_2, cls));
96 }
97
98 static void
99 run (void *cls, char *const *args, const char *cfgfile,
100      const struct GNUNET_CONFIGURATION_Handle *cfg)
101 {
102   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
103   GNUNET_STATISTICS_set (h, "test-1", 1, GNUNET_NO);
104   GNUNET_STATISTICS_set (h, "test-2", 2, GNUNET_NO);
105   GNUNET_STATISTICS_set (h, "test-3", 2, GNUNET_NO);
106   GNUNET_STATISTICS_update (h, "test-3", 1, GNUNET_YES);
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Issuing GET request\n");
108   GNUNET_break (NULL !=
109                 GNUNET_STATISTICS_get (h, NULL, "test-1",
110                                        GNUNET_TIME_UNIT_SECONDS, &next,
111                                        &check_1, cls));
112 }
113
114 static void
115 run_more (void *cls, char *const *args, const char *cfgfile,
116           const struct GNUNET_CONFIGURATION_Handle *cfg)
117 {
118   h = GNUNET_STATISTICS_create ("test-statistics-api", cfg);
119   GNUNET_break (NULL !=
120                 GNUNET_STATISTICS_get (h, NULL, "test-3",
121                                        GNUNET_TIME_UNIT_SECONDS, &next_fin,
122                                        &check_3, cls));
123 }
124
125 static int
126 check ()
127 {
128   int ok = 1;
129
130   char *const argv[] = { "test-statistics-api",
131     "-c",
132     "test_statistics_api_data.conf",
133 #if DEBUG_STATISTICS
134     "-L", "DEBUG",
135 #else
136     "-L", "WARNING",
137 #endif
138     NULL
139   };
140   struct GNUNET_GETOPT_CommandLineOption options[] = {
141     GNUNET_GETOPT_OPTION_END
142   };
143 #if START_SERVICE
144   struct GNUNET_OS_Process *proc;
145
146   proc =
147       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
148                                "gnunet-service-statistics",
149 #if DEBUG_STATISTICS
150                                "-L", "DEBUG",
151 #endif
152                                "-c", "test_statistics_api_data.conf", NULL);
153 #endif
154   GNUNET_assert (NULL != proc);
155   GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp", options, &run,
156                       &ok);
157 #if START_SERVICE
158   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
159   {
160     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
161     ok = 1;
162   }
163   GNUNET_OS_process_wait (proc);
164   GNUNET_OS_process_close (proc);
165   proc = NULL;
166 #endif
167   if (ok != 0)
168     return ok;
169   ok = 1;
170 #if START_SERVICE
171   /* restart to check persistence! */
172   proc =
173       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-statistics",
174                                "gnunet-service-statistics",
175 #if DEBUG_STATISTICS
176                                "-L", "DEBUG",
177 #endif
178                                "-c", "test_statistics_api_data.conf", NULL);
179 #endif
180   GNUNET_PROGRAM_run (5, argv, "test-statistics-api", "nohelp", options,
181                       &run_more, &ok);
182 #if START_SERVICE
183   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
184   {
185     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
186     ok = 1;
187   }
188   GNUNET_OS_process_wait (proc);
189   GNUNET_OS_process_close (proc);
190   proc = NULL;
191 #endif
192   return ok;
193 }
194
195 int
196 main (int argc, char *argv[])
197 {
198   int ret;
199
200   GNUNET_log_setup ("test_statistics_api",
201 #if DEBUG_STATISTICS
202                     "DEBUG",
203 #else
204                     "WARNING",
205 #endif
206                     NULL);
207   ret = check ();
208
209   return ret;
210 }
211
212 /* end of test_statistics_api.c */