-fix
[oweals/gnunet.git] / src / datastore / test_datastore_api_management.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007, 2009, 2011 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 datastore/test_datastore_api_management.c
22  * @brief Test for the space management functions of the datastore implementation.
23  * @author Christian Grothoff
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_service.h"
30
31 #define VERBOSE GNUNET_NO
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
37
38 /**
39  * Number of iterations to run; must be large enough
40  * so that the quota will be exceeded!
41  */
42 #define ITERATIONS 5000
43
44 static struct GNUNET_DATASTORE_Handle *datastore;
45
46 static struct GNUNET_TIME_Absolute now;
47
48 static int ok;
49
50 static const char *plugin_name;
51
52 static size_t
53 get_size (int i)
54 {
55   return 8 + 8 * (i % 256);
56 }
57
58
59 static const void *
60 get_data (int i)
61 {
62   static char buf[60000];
63
64   memset (buf, i, 8 + 8 * (i % 256));
65   return buf;
66 }
67
68
69 static int
70 get_type (int i)
71 {
72   return 1;
73 }
74
75
76 static int
77 get_priority (int i)
78 {
79   return i + 1;
80 }
81
82
83 static int
84 get_anonymity (int i)
85 {
86   return i;
87 }
88
89
90 static struct GNUNET_TIME_Absolute
91 get_expiration (int i)
92 {
93   struct GNUNET_TIME_Absolute av;
94
95   av.abs_value = now.abs_value + i * 1000;
96   return av;
97 }
98
99 enum RunPhase
100 {
101   RP_PUT,
102   RP_GET,
103   RP_DONE,
104   RP_GET_FAIL
105 };
106
107
108 struct CpsRunContext
109 {
110   GNUNET_HashCode key;
111   int i;
112   int found;
113   const struct GNUNET_CONFIGURATION_Handle *cfg;
114   void *data;
115   enum RunPhase phase;
116   uint64_t offset;
117 };
118
119
120 static void
121 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
122
123
124 static void
125 check_success (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
126 {
127   struct CpsRunContext *crc = cls;
128
129   if (GNUNET_OK != success)
130     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", msg);
131   GNUNET_assert (GNUNET_OK == success);
132   GNUNET_free_non_null (crc->data);
133   crc->data = NULL;
134   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
135                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
136 }
137
138
139 static void
140 check_value (void *cls, const GNUNET_HashCode * key, size_t size,
141              const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
142              uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
143              uint64_t uid)
144 {
145   struct CpsRunContext *crc = cls;
146   int i;
147
148   if (NULL == key)
149   {
150     crc->phase = RP_GET_FAIL;
151     GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
152                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
153     return;
154   }
155   i = crc->i;
156   GNUNET_assert (size == get_size (i));
157   GNUNET_assert (0 == memcmp (data, get_data (i), size));
158   GNUNET_assert (type == get_type (i));
159   GNUNET_assert (priority == get_priority (i));
160   GNUNET_assert (anonymity == get_anonymity (i));
161   GNUNET_assert (expiration.abs_value == get_expiration (i).abs_value);
162   crc->offset++;
163   crc->i--;
164   if (crc->i == 0)
165     crc->phase = RP_DONE;
166   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
167                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
168 }
169
170
171 static void
172 check_nothing (void *cls, const GNUNET_HashCode * key, size_t size,
173                const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
174                uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
175                uint64_t uid)
176 {
177   struct CpsRunContext *crc = cls;
178
179   GNUNET_assert (key == NULL);
180   if (0 == --crc->i)
181     crc->phase = RP_DONE;
182   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
183                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
184 }
185
186
187 static void
188 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 {
190   struct CpsRunContext *crc = cls;
191
192   ok = (int) crc->phase;
193   switch (crc->phase)
194   {
195   case RP_PUT:
196 #if VERBOSE
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
198                 crc->i);
199 #endif
200     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
201     GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
202                           get_data (crc->i), get_type (crc->i),
203                           get_priority (crc->i), get_anonymity (crc->i), 0,
204                           get_expiration (crc->i), 1, 1, TIMEOUT,
205                           &check_success, crc);
206     crc->i++;
207     if (crc->i == ITERATIONS)
208     {
209       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
210                   "Sleeping to give datastore time to clean up\n");
211       sleep (1);
212       crc->phase = RP_GET;
213       crc->i--;
214     }
215     break;
216   case RP_GET:
217 #if VERBOSE
218     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
219                 crc->i);
220 #endif
221     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
222     GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
223                               get_type (crc->i), 1, 1, TIMEOUT, &check_value,
224                               crc);
225     break;
226   case RP_GET_FAIL:
227 #if VERBOSE
228     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)",
229                 crc->i);
230 #endif
231     GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
232     GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
233                               get_type (crc->i), 1, 1, TIMEOUT, &check_nothing,
234                               crc);
235     break;
236   case RP_DONE:
237     GNUNET_assert (0 == crc->i);
238 #if VERBOSE
239     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
240 #endif
241     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
242     GNUNET_free (crc);
243     ok = 0;
244   }
245 }
246
247
248 static void
249 run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
250 {
251   struct CpsRunContext *crc = cls;
252
253   if (success != GNUNET_YES)
254   {
255     FPRINTF (stderr,
256              "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
257              msg);
258     GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
259     GNUNET_free (crc);
260     return;
261   }
262   GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
263                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
264 }
265
266
267 static void
268 run (void *cls, char *const *args, const char *cfgfile,
269      const struct GNUNET_CONFIGURATION_Handle *cfg)
270 {
271   struct CpsRunContext *crc;
272   static GNUNET_HashCode zkey;
273
274   crc = GNUNET_malloc (sizeof (struct CpsRunContext));
275   crc->cfg = cfg;
276   crc->phase = RP_PUT;
277   now = GNUNET_TIME_absolute_get ();
278   datastore = GNUNET_DATASTORE_connect (cfg);
279   if (NULL ==
280       GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST",
281                             GNUNET_BLOCK_TYPE_TEST, 0, 0, 0,
282                             GNUNET_TIME_relative_to_absolute
283                             (GNUNET_TIME_UNIT_SECONDS), 0, 1,
284                             GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
285   {
286     FPRINTF (stderr, "%s",  "Test 'put' operation failed.\n");
287     GNUNET_free (crc);
288     ok = 1;
289   }
290 }
291
292
293
294 static int
295 check ()
296 {
297   struct GNUNET_OS_Process *proc;
298   char cfg_name[128];
299
300   char *const argv[] = {
301     "test-datastore-api-management",
302     "-c",
303     cfg_name,
304 #if VERBOSE
305     "-L", "DEBUG",
306 #endif
307     NULL
308   };
309   struct GNUNET_GETOPT_CommandLineOption options[] = {
310     GNUNET_GETOPT_OPTION_END
311   };
312   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
313                    "test_datastore_api_data_%s.conf", plugin_name);
314   proc =
315     GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
316                                "gnunet-service-arm",
317 #if VERBOSE
318                                "-L", "DEBUG",
319 #endif
320                                "-c", cfg_name, NULL);
321   GNUNET_assert (NULL != proc);
322   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
323                       "test-datastore-api-management", "nohelp", options, &run,
324                       NULL);
325   sleep (1);                    /* give datastore chance to process 'DROP' request */
326   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
327   {
328     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
329     ok = 1;
330   }
331   GNUNET_OS_process_wait (proc);
332   GNUNET_OS_process_close (proc);
333   proc = NULL;
334   if (ok != 0)
335     FPRINTF (stderr, "Missed some testcases: %u\n", ok);
336   return ok;
337 }
338
339 int
340 main (int argc, char *argv[])
341 {
342   int ret;
343
344   char *pos;
345   char dir_name[128];
346
347   sleep (1);
348   /* determine name of plugin to use */
349   plugin_name = argv[0];
350   while (NULL != (pos = strstr (plugin_name, "_")))
351     plugin_name = pos + 1;
352   if (NULL != (pos = strstr (plugin_name, ".")))
353     pos[0] = 0;
354   else
355     pos = (char *) plugin_name;
356
357   GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/test-gnunet-datastore-%s",
358                    plugin_name);
359   GNUNET_DISK_directory_remove (dir_name);
360   GNUNET_log_setup ("test-datastore-api-management",
361 #if VERBOSE
362                     "DEBUG",
363 #else
364                     "WARNING",
365 #endif
366                     NULL);
367   ret = check ();
368   if (pos != plugin_name)
369     pos[0] = '.';
370   GNUNET_DISK_directory_remove (dir_name);
371   return ret;
372 }
373
374 /* end of test_datastore_api_management.c */