paragraph for gnunet devs that don't know how to use the web
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /*
19  * @file test_plugin_datastore.c
20  * @brief Test database plugin directly, calling each API function once
21  * @author Christian Grothoff
22  */
23
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_datastore_plugin.h"
28 #include "gnunet_testing_lib.h"
29
30 /**
31  * Number of put operations to perform.
32  */
33 #define PUT_10 10
34
35 static unsigned long long stored_bytes;
36
37 static unsigned long long stored_entries;
38
39 static unsigned long long stored_ops;
40
41 static const char *plugin_name;
42
43 static int ok;
44
45 enum RunPhase
46 {
47   RP_ERROR = 0,
48   RP_PUT,
49   RP_GET,
50   RP_ITER_ZERO,
51   RP_REPL_GET,
52   RP_EXPI_GET,
53   RP_REMOVE,
54   RP_DROP
55 };
56
57
58 struct CpsRunContext
59 {
60   const struct GNUNET_CONFIGURATION_Handle *cfg;
61   struct GNUNET_DATASTORE_PluginFunctions *api;
62   enum RunPhase phase;
63   unsigned int cnt;
64   unsigned int i;
65 };
66
67
68 /**
69  * Function called by plugins to notify us about a
70  * change in their disk utilization.
71  *
72  * @param cls closure (NULL)
73  * @param delta change in disk utilization,
74  *        0 for "reset to empty"
75  */
76 static void
77 disk_utilization_change_cb (void *cls, int delta)
78 {
79   /* do nothing */
80 }
81
82
83 static void
84 test (void *cls);
85
86
87 /**
88  * Put continuation.
89  *
90  * @param cls closure
91  * @param key key for the item stored
92  * @param size size of the item stored
93  * @param status #GNUNET_OK or #GNUNET_SYSERROR
94  * @param msg error message on error
95  */
96 static void
97 put_continuation (void *cls,
98                   const struct GNUNET_HashCode *key,
99                   uint32_t size,
100                   int status,
101                   const char *msg)
102 {
103   struct CpsRunContext *crc = cls;
104   static unsigned long long os;
105   unsigned long long cs;
106
107   if (GNUNET_OK != status)
108   {
109     FPRINTF (stderr,
110              "ERROR: `%s'\n",
111              msg);
112   }
113   else
114   {
115     crc->api->estimate_size (crc->api->cls,
116                              &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 (0 != i && 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,
170                  &key,
171                  false /* absent */,
172                  size,
173                  value, i + 1 /* type */ ,
174                  prio,
175                  i /* anonymity */ ,
176                  0 /* replication */ ,
177                  GNUNET_TIME_relative_to_absolute
178                    (GNUNET_TIME_relative_multiply
179                      (GNUNET_TIME_UNIT_MILLISECONDS,
180                       60 * 60 * 60 * 1000 +
181                       GNUNET_CRYPTO_random_u32
182                       (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
183                  put_continuation,
184                  crc);
185   i++;
186 }
187
188
189 static uint64_t guid;
190
191
192 static int
193 iterate_one_shot (void *cls,
194                   const struct GNUNET_HashCode *key,
195                   uint32_t size,
196                   const void *data,
197                   enum GNUNET_BLOCK_Type type,
198                   uint32_t priority,
199                   uint32_t anonymity,
200                   uint32_t replication,
201                   struct GNUNET_TIME_Absolute expiration,
202                   uint64_t uid)
203 {
204   struct CpsRunContext *crc = cls;
205
206   GNUNET_assert (NULL != key);
207   guid = uid;
208   crc->phase++;
209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210               "Found result type=%u, priority=%u, size=%u, expire=%s, key %s\n",
211               (unsigned int) type,
212               (unsigned int) priority,
213               (unsigned int) size,
214               GNUNET_STRINGS_absolute_time_to_string (expiration),
215               GNUNET_h2s (key));
216   GNUNET_SCHEDULER_add_now (&test,
217                             crc);
218   return GNUNET_OK;
219 }
220
221
222 static void
223 remove_continuation (void *cls,
224                      const struct GNUNET_HashCode *key,
225                      uint32_t size,
226                      int status,
227                      const char *msg)
228 {
229   struct CpsRunContext *crc = cls;
230
231   GNUNET_assert (NULL != key);
232   GNUNET_assert (32768 == size);
233   GNUNET_assert (GNUNET_OK == status);
234   GNUNET_assert (NULL == msg);
235   crc->phase++;
236   GNUNET_SCHEDULER_add_now (&test,
237                             crc);
238 }
239
240
241 /**
242  * Function called when the service shuts
243  * down.  Unloads our datastore plugin.
244  *
245  * @param api api to unload
246  * @param cfg configuration to use
247  */
248 static void
249 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
250                const struct GNUNET_CONFIGURATION_Handle *cfg)
251 {
252   char *name;
253   char *libname;
254
255   if (GNUNET_OK !=
256       GNUNET_CONFIGURATION_get_value_string (cfg,
257                                              "DATASTORE",
258                                              "DATABASE",
259                                              &name))
260   {
261     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262                 _("No `%s' specified for `%s' in configuration!\n"),
263                 "DATABASE",
264                 "DATASTORE");
265     return;
266   }
267   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
268   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
269   GNUNET_free (libname);
270   GNUNET_free (name);
271 }
272
273
274
275 /**
276  * Last task run during shutdown.  Disconnects us from
277  * the transport and core.
278  */
279 static void
280 cleaning_task (void *cls)
281 {
282   struct CpsRunContext *crc = cls;
283
284   unload_plugin (crc->api, crc->cfg);
285   GNUNET_free (crc);
286 }
287
288
289 static void
290 test (void *cls)
291 {
292   struct CpsRunContext *crc = cls;
293   struct GNUNET_HashCode key;
294
295   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296               "In phase %d, iteration %u\n", crc->phase, crc->cnt);
297   switch (crc->phase)
298   {
299   case RP_ERROR:
300     ok = 1;
301     GNUNET_break (0);
302     crc->api->drop (crc->api->cls);
303     GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
304     break;
305   case RP_PUT:
306     do_put (crc);
307     break;
308   case RP_GET:
309     if (crc->cnt == 1)
310     {
311       crc->cnt = 0;
312       crc->phase++;
313       GNUNET_SCHEDULER_add_now (&test, crc);
314       break;
315     }
316     gen_key (5, &key);
317     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318                 "Looking for %s\n",
319                 GNUNET_h2s (&key));
320     crc->api->get_key (crc->api->cls,
321                        0,
322                        false,
323                        &key,
324                        GNUNET_BLOCK_TYPE_ANY,
325                        &iterate_one_shot,
326                        crc);
327     break;
328   case RP_ITER_ZERO:
329     if (crc->cnt == 1)
330     {
331       crc->cnt = 0;
332       crc->phase++;
333       GNUNET_SCHEDULER_add_now (&test, crc);
334       break;
335     }
336     crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
337     break;
338   case RP_REPL_GET:
339     crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
340     break;
341   case RP_EXPI_GET:
342     crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
343     break;
344   case RP_REMOVE:
345     {
346       struct GNUNET_HashCode key;
347       uint32_t size = 32768;
348       char value[size];
349
350       gen_key (0, &key);
351       memset (value, 0, size);
352       value[0] = crc->i;
353       crc->api->remove_key (crc->api->cls,
354                             &key,
355                             size,
356                             value,
357                             &remove_continuation,
358                             crc);
359       break;
360     }
361   case RP_DROP:
362     crc->api->drop (crc->api->cls);
363     GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
364     break;
365   }
366 }
367
368
369 /**
370  * Load the datastore plugin.
371  */
372 static struct GNUNET_DATASTORE_PluginFunctions *
373 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
374 {
375   static struct GNUNET_DATASTORE_PluginEnvironment env;
376   struct GNUNET_DATASTORE_PluginFunctions *ret;
377   char *name;
378   char *libname;
379
380   if (GNUNET_OK !=
381       GNUNET_CONFIGURATION_get_value_string (cfg,
382                                              "DATASTORE",
383                                              "DATABASE",
384                                              &name))
385   {
386     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
387                 _("No `%s' specified for `%s' in configuration!\n"),
388                 "DATABASE",
389                 "DATASTORE");
390     return NULL;
391   }
392   env.cfg = cfg;
393   env.duc = &disk_utilization_change_cb;
394   env.cls = NULL;
395   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
396               name);
397   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
398   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
399   {
400     FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
401     GNUNET_free (libname);
402     GNUNET_free (name);
403     ok = 77; /* mark test as skipped */
404     return NULL;
405   }
406   GNUNET_free (libname);
407   GNUNET_free (name);
408   return ret;
409 }
410
411
412 static void
413 run (void *cls, char *const *args, const char *cfgfile,
414      const struct GNUNET_CONFIGURATION_Handle *c)
415 {
416   struct GNUNET_DATASTORE_PluginFunctions *api;
417   struct CpsRunContext *crc;
418
419   api = load_plugin (c);
420   if (api == NULL)
421   {
422     FPRINTF (stderr,
423              "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
424     return;
425   }
426   crc = GNUNET_new (struct CpsRunContext);
427   crc->api = api;
428   crc->cfg = c;
429   crc->phase = RP_PUT;
430   GNUNET_SCHEDULER_add_now (&test, crc);
431 }
432
433
434 int
435 main (int argc, char *argv[])
436 {
437   char dir_name[128];
438   char cfg_name[128];
439   char *const xargv[] = {
440     "test-plugin-datastore",
441     "-c",
442     cfg_name,
443     NULL
444   };
445   static struct GNUNET_GETOPT_CommandLineOption options[] = {
446     GNUNET_GETOPT_OPTION_END
447   };
448
449   /* determine name of plugin to use */
450   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
451   GNUNET_snprintf (dir_name, sizeof (dir_name),
452                    "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
453   GNUNET_DISK_directory_remove (dir_name);
454   GNUNET_log_setup ("test-plugin-datastore",
455                     "WARNING",
456                     NULL);
457   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
458                    "test_plugin_datastore_data_%s.conf", plugin_name);
459   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
460                       "test-plugin-datastore", "nohelp", options, &run, NULL);
461   if ( (0 != ok) && (77 != ok) )
462     FPRINTF (stderr, "Missed some testcases: %u\n", ok);
463   GNUNET_DISK_directory_remove (dir_name);
464   return ok;
465 }
466
467 /* end of test_plugin_datastore.c */