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