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