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