2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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.
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.
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/>.
19 * @file test_plugin_datastore.c
20 * @brief Test database plugin directly, calling each API function once
21 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_datastore_plugin.h"
28 #include "gnunet_testing_lib.h"
31 * Number of put operations to perform.
35 static unsigned long long stored_bytes;
37 static unsigned long long stored_entries;
39 static unsigned long long stored_ops;
41 static const char *plugin_name;
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
61 struct GNUNET_DATASTORE_PluginFunctions *api;
69 * Function called by plugins to notify us about a
70 * change in their disk utilization.
72 * @param cls closure (NULL)
73 * @param delta change in disk utilization,
74 * 0 for "reset to empty"
77 disk_utilization_change_cb (void *cls, int delta)
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
97 put_continuation (void *cls,
98 const struct GNUNET_HashCode *key,
103 struct CpsRunContext *crc = cls;
104 static unsigned long long os;
105 unsigned long long cs;
107 if (GNUNET_OK != status)
115 crc->api->estimate_size (crc->api->cls,
117 GNUNET_assert (os <= cs);
119 stored_bytes += size;
123 GNUNET_SCHEDULER_add_now (&test, crc);
128 gen_key (int i, struct GNUNET_HashCode * key)
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);
137 do_put (struct CpsRunContext *crc)
141 struct GNUNET_HashCode key;
149 GNUNET_SCHEDULER_add_now (&test, crc);
152 /* most content is 32k */
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 */
159 /* generate random key */
161 memset (value, i, size);
163 memset (value, i - 255, size / 2);
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,
169 crc->api->put (crc->api->cls,
173 value, i + 1 /* type */ ,
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))),
189 static uint64_t guid;
193 iterate_one_shot (void *cls,
194 const struct GNUNET_HashCode *key,
197 enum GNUNET_BLOCK_Type type,
200 uint32_t replication,
201 struct GNUNET_TIME_Absolute expiration,
204 struct CpsRunContext *crc = cls;
206 GNUNET_assert (NULL != key);
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Found result type=%u, priority=%u, size=%u, expire=%s, key %s\n",
212 (unsigned int) priority,
214 GNUNET_STRINGS_absolute_time_to_string (expiration),
216 GNUNET_SCHEDULER_add_now (&test,
223 remove_continuation (void *cls,
224 const struct GNUNET_HashCode *key,
229 struct CpsRunContext *crc = cls;
231 GNUNET_assert (NULL != key);
232 GNUNET_assert (32768 == size);
233 GNUNET_assert (GNUNET_OK == status);
234 GNUNET_assert (NULL == msg);
236 GNUNET_SCHEDULER_add_now (&test,
242 * Function called when the service shuts
243 * down. Unloads our datastore plugin.
245 * @param api api to unload
246 * @param cfg configuration to use
249 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
250 const struct GNUNET_CONFIGURATION_Handle *cfg)
256 GNUNET_CONFIGURATION_get_value_string (cfg,
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
262 _("No `%s' specified for `%s' in configuration!\n"),
267 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
268 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
269 GNUNET_free (libname);
276 * Last task run during shutdown. Disconnects us from
277 * the transport and core.
280 cleaning_task (void *cls)
282 struct CpsRunContext *crc = cls;
284 unload_plugin (crc->api, crc->cfg);
292 struct CpsRunContext *crc = cls;
293 struct GNUNET_HashCode key;
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
302 crc->api->drop (crc->api->cls);
303 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
313 GNUNET_SCHEDULER_add_now (&test, crc);
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 crc->api->get_key (crc->api->cls,
324 GNUNET_BLOCK_TYPE_ANY,
333 GNUNET_SCHEDULER_add_now (&test, crc);
336 crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
339 crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
342 crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
346 struct GNUNET_HashCode key;
347 uint32_t size = 32768;
351 memset (value, 0, size);
353 crc->api->remove_key (crc->api->cls,
357 &remove_continuation,
362 crc->api->drop (crc->api->cls);
363 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
370 * Load the datastore plugin.
372 static struct GNUNET_DATASTORE_PluginFunctions *
373 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
375 static struct GNUNET_DATASTORE_PluginEnvironment env;
376 struct GNUNET_DATASTORE_PluginFunctions *ret;
381 GNUNET_CONFIGURATION_get_value_string (cfg,
386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
387 _("No `%s' specified for `%s' in configuration!\n"),
393 env.duc = &disk_utilization_change_cb;
395 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
397 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
398 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
400 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
401 GNUNET_free (libname);
403 ok = 77; /* mark test as skipped */
406 GNUNET_free (libname);
413 run (void *cls, char *const *args, const char *cfgfile,
414 const struct GNUNET_CONFIGURATION_Handle *c)
416 struct GNUNET_DATASTORE_PluginFunctions *api;
417 struct CpsRunContext *crc;
419 api = load_plugin (c);
423 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
426 crc = GNUNET_new (struct CpsRunContext);
430 GNUNET_SCHEDULER_add_now (&test, crc);
435 main (int argc, char *argv[])
439 char *const xargv[] = {
440 "test-plugin-datastore",
445 static struct GNUNET_GETOPT_CommandLineOption options[] = {
446 GNUNET_GETOPT_OPTION_END
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",
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);
467 /* end of test_plugin_datastore.c */