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