Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / datastore / test_plugin_datastore.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 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 test_plugin_datastore.c
22  * @brief Test database plugin directly, calling each API function once
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_plugin.h"
30 #include "gnunet_testing_lib.h"
31
32 /**
33  * Number of put operations to perform.
34  */
35 #define PUT_10 10
36
37 static unsigned long long stored_bytes;
38
39 static unsigned long long stored_entries;
40
41 static unsigned long long stored_ops;
42
43 static const char *plugin_name;
44
45 static int ok;
46
47 enum RunPhase
48 {
49   RP_ERROR = 0,
50   RP_PUT,
51   RP_GET,
52   RP_UPDATE,
53   RP_ITER_ZERO,
54   RP_REPL_GET,
55   RP_EXPI_GET,
56   RP_DROP
57 };
58
59
60 struct CpsRunContext
61 {
62   const struct GNUNET_CONFIGURATION_Handle *cfg;
63   struct GNUNET_DATASTORE_PluginFunctions *api;
64   enum RunPhase phase;
65   unsigned int cnt;
66   unsigned int i;
67   uint64_t offset;
68 };
69
70
71 /**
72  * Function called by plugins to notify us about a
73  * change in their disk utilization.
74  *
75  * @param cls closure (NULL)
76  * @param delta change in disk utilization,
77  *        0 for "reset to empty"
78  */
79 static void
80 disk_utilization_change_cb (void *cls, int delta)
81 {
82   /* do nothing */
83 }
84
85
86 static void
87 test (void *cls);
88
89
90 /**
91  * Put continuation.
92  *
93  * @param cls closure
94  * @param key key for the item stored
95  * @param size size of the item stored
96  * @param status #GNUNET_OK or #GNUNET_SYSERROR
97  * @param msg error message on error
98  */
99 static void
100 put_continuation (void *cls,
101                   const struct GNUNET_HashCode *key,
102                   uint32_t size,
103                   int status,
104                   const char *msg)
105 {
106   struct CpsRunContext *crc = cls;
107   static unsigned long long os;
108   unsigned long long cs;
109
110   if (GNUNET_OK != status)
111   {
112     FPRINTF (stderr,
113              "ERROR: `%s'\n",
114              msg);
115   }
116   else
117   {
118     crc->api->estimate_size (crc->api->cls,
119                              &cs);
120     GNUNET_assert (os <= cs);
121     os = cs;
122     stored_bytes += size;
123     stored_ops++;
124     stored_entries++;
125   }
126   GNUNET_SCHEDULER_add_now (&test, crc);
127 }
128
129
130 static void
131 gen_key (int i, struct GNUNET_HashCode * key)
132 {
133   memset (key, 0, sizeof (struct GNUNET_HashCode));
134   key->bits[0] = (unsigned int) i;
135   GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_HashCode), key);
136 }
137
138
139 static void
140 do_put (struct CpsRunContext *crc)
141 {
142   char value[65536];
143   size_t size;
144   struct GNUNET_HashCode key;
145   unsigned int prio;
146   static int i;
147
148   if (PUT_10 == i)
149   {
150     i = 0;
151     crc->phase++;
152     GNUNET_SCHEDULER_add_now (&test, crc);
153     return;
154   }
155   /* most content is 32k */
156   size = 32 * 1024;
157
158   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)   /* but some of it is less! */
159     size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
160   size = size - (size & 7);     /* always multiple of 8 */
161
162   /* generate random key */
163   gen_key (i, &key);
164   memset (value, i, size);
165   if (i > 255)
166     memset (value, i - 255, size / 2);
167   value[0] = crc->i;
168   prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170               "putting type %u, anon %u under key %s\n", i + 1, i,
171               GNUNET_h2s (&key));
172   crc->api->put (crc->api->cls, &key, size, value, i + 1 /* type */ ,
173                  prio, i /* anonymity */ ,
174                  0 /* replication */ ,
175                  GNUNET_TIME_relative_to_absolute
176                    (GNUNET_TIME_relative_multiply
177                      (GNUNET_TIME_UNIT_MILLISECONDS,
178                       60 * 60 * 60 * 1000 +
179                       GNUNET_CRYPTO_random_u32
180                       (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
181                  put_continuation, crc);
182   i++;
183 }
184
185
186 static uint64_t guid;
187
188
189 static int
190 iterate_one_shot (void *cls,
191                   const struct GNUNET_HashCode *key,
192                   uint32_t size,
193                   const void *data,
194                   enum GNUNET_BLOCK_Type type,
195                   uint32_t priority,
196                   uint32_t anonymity,
197                   struct GNUNET_TIME_Absolute expiration,
198                   uint64_t uid)
199 {
200   struct CpsRunContext *crc = cls;
201
202   GNUNET_assert (NULL != key);
203   guid = uid;
204   crc->phase++;
205   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206               "Found result type=%u, priority=%u, size=%u, expire=%s, key %s\n",
207               (unsigned int) type,
208               (unsigned int) priority,
209               (unsigned int) size,
210               GNUNET_STRINGS_absolute_time_to_string (expiration),
211               GNUNET_h2s (key));
212   GNUNET_SCHEDULER_add_now (&test,
213                             crc);
214   return GNUNET_OK;
215 }
216
217
218 /**
219  * Function called when the service shuts
220  * down.  Unloads our datastore plugin.
221  *
222  * @param api api to unload
223  * @param cfg configuration to use
224  */
225 static void
226 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
227                const struct GNUNET_CONFIGURATION_Handle *cfg)
228 {
229   char *name;
230   char *libname;
231
232   if (GNUNET_OK !=
233       GNUNET_CONFIGURATION_get_value_string (cfg,
234                                              "DATASTORE",
235                                              "DATABASE",
236                                              &name))
237   {
238     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239                 _("No `%s' specified for `%s' in configuration!\n"),
240                 "DATABASE",
241                 "DATASTORE");
242     return;
243   }
244   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
245   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
246   GNUNET_free (libname);
247   GNUNET_free (name);
248 }
249
250
251
252 /**
253  * Last task run during shutdown.  Disconnects us from
254  * the transport and core.
255  */
256 static void
257 cleaning_task (void *cls)
258 {
259   struct CpsRunContext *crc = cls;
260
261   unload_plugin (crc->api, crc->cfg);
262   GNUNET_free (crc);
263 }
264
265
266 static void
267 update_continuation (void *cls,
268                      int status,
269                      const char *msg)
270 {
271   struct CpsRunContext *crc = cls;
272
273   GNUNET_assert (GNUNET_OK == status);
274   crc->phase++;
275   GNUNET_SCHEDULER_add_now (&test, crc);
276 }
277
278
279 static void
280 test (void *cls)
281 {
282   struct CpsRunContext *crc = cls;
283   struct GNUNET_HashCode key;
284
285   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286               "In phase %d, iteration %u\n", crc->phase, crc->cnt);
287   switch (crc->phase)
288   {
289   case RP_ERROR:
290     ok = 1;
291     GNUNET_break (0);
292     crc->api->drop (crc->api->cls);
293     GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
294     break;
295   case RP_PUT:
296     do_put (crc);
297     break;
298   case RP_GET:
299     if (crc->cnt == 1)
300     {
301       crc->cnt = 0;
302       crc->phase++;
303       GNUNET_SCHEDULER_add_now (&test, crc);
304       break;
305     }
306     gen_key (5, &key);
307     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308                 "Looking for %s\n",
309                 GNUNET_h2s (&key));
310     crc->api->get_key (crc->api->cls,
311                        crc->offset++,
312                        &key,
313                        NULL,
314                        GNUNET_BLOCK_TYPE_ANY,
315                        &iterate_one_shot,
316                        crc);
317     break;
318   case RP_UPDATE:
319     crc->api->update (crc->api->cls,
320                       guid,
321                       1,
322                       GNUNET_TIME_UNIT_ZERO_ABS,
323                       &update_continuation,
324                       crc);
325     break;
326
327   case RP_ITER_ZERO:
328     if (crc->cnt == 1)
329     {
330       crc->cnt = 0;
331       crc->phase++;
332       GNUNET_SCHEDULER_add_now (&test, crc);
333       break;
334     }
335     crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
336     break;
337   case RP_REPL_GET:
338     crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
339     break;
340   case RP_EXPI_GET:
341     crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
342     break;
343   case RP_DROP:
344     crc->api->drop (crc->api->cls);
345     GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
346     break;
347   }
348 }
349
350
351 /**
352  * Load the datastore plugin.
353  */
354 static struct GNUNET_DATASTORE_PluginFunctions *
355 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
356 {
357   static struct GNUNET_DATASTORE_PluginEnvironment env;
358   struct GNUNET_DATASTORE_PluginFunctions *ret;
359   char *name;
360   char *libname;
361
362   if (GNUNET_OK !=
363       GNUNET_CONFIGURATION_get_value_string (cfg,
364                                              "DATASTORE",
365                                              "DATABASE",
366                                              &name))
367   {
368     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369                 _("No `%s' specified for `%s' in configuration!\n"),
370                 "DATABASE",
371                 "DATASTORE");
372     return NULL;
373   }
374   env.cfg = cfg;
375   env.duc = &disk_utilization_change_cb;
376   env.cls = NULL;
377   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
378               name);
379   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
380   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
381   {
382     FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
383     GNUNET_free (libname);
384     GNUNET_free (name);
385     ok = 77; /* mark test as skipped */
386     return NULL;
387   }
388   GNUNET_free (libname);
389   GNUNET_free (name);
390   return ret;
391 }
392
393
394 static void
395 run (void *cls, char *const *args, const char *cfgfile,
396      const struct GNUNET_CONFIGURATION_Handle *c)
397 {
398   struct GNUNET_DATASTORE_PluginFunctions *api;
399   struct CpsRunContext *crc;
400
401   api = load_plugin (c);
402   if (api == NULL)
403   {
404     FPRINTF (stderr,
405              "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
406     return;
407   }
408   crc = GNUNET_new (struct CpsRunContext);
409   crc->api = api;
410   crc->cfg = c;
411   crc->phase = RP_PUT;
412   GNUNET_SCHEDULER_add_now (&test, crc);
413 }
414
415
416 int
417 main (int argc, char *argv[])
418 {
419   char dir_name[128];
420   char cfg_name[128];
421   char *const xargv[] = {
422     "test-plugin-datastore",
423     "-c",
424     cfg_name,
425     NULL
426   };
427   static struct GNUNET_GETOPT_CommandLineOption options[] = {
428     GNUNET_GETOPT_OPTION_END
429   };
430
431   /* determine name of plugin to use */
432   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
433   GNUNET_snprintf (dir_name, sizeof (dir_name),
434                    "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
435   GNUNET_DISK_directory_remove (dir_name);
436   GNUNET_log_setup ("test-plugin-datastore",
437                     "WARNING",
438                     NULL);
439   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
440                    "test_plugin_datastore_data_%s.conf", plugin_name);
441   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
442                       "test-plugin-datastore", "nohelp", options, &run, NULL);
443   if ( (0 != ok) && (77 != ok) )
444     FPRINTF (stderr, "Missed some testcases: %u\n", ok);
445   GNUNET_DISK_directory_remove (dir_name);
446   return ok;
447 }
448
449 /* end of test_plugin_datastore.c */