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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file test_plugin_datastore.c
22 * @brief Test database plugin directly, calling each API function once
23 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_plugin.h"
30 #include "gnunet_testing_lib.h"
33 * Number of put operations to perform.
37 static unsigned long long stored_bytes;
39 static unsigned long long stored_entries;
41 static unsigned long long stored_ops;
43 static const char *plugin_name;
62 const struct GNUNET_CONFIGURATION_Handle *cfg;
63 struct GNUNET_DATASTORE_PluginFunctions *api;
71 * Function called by plugins to notify us about a
72 * change in their disk utilization.
74 * @param cls closure (NULL)
75 * @param delta change in disk utilization,
76 * 0 for "reset to empty"
79 disk_utilization_change_cb (void *cls, int delta)
93 * @param key key for the item stored
94 * @param size size of the item stored
95 * @param status #GNUNET_OK or #GNUNET_SYSERROR
96 * @param msg error message on error
99 put_continuation (void *cls,
100 const struct GNUNET_HashCode *key,
105 struct CpsRunContext *crc = cls;
106 static unsigned long long os;
107 unsigned long long cs;
109 if (GNUNET_OK != status)
117 crc->api->estimate_size (crc->api->cls,
119 GNUNET_assert (os <= cs);
121 stored_bytes += size;
125 GNUNET_SCHEDULER_add_now (&test, crc);
130 gen_key (int i, struct GNUNET_HashCode * key)
132 memset (key, 0, sizeof (struct GNUNET_HashCode));
133 key->bits[0] = (unsigned int) i;
134 GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_HashCode), key);
139 do_put (struct CpsRunContext *crc)
143 struct GNUNET_HashCode key;
151 GNUNET_SCHEDULER_add_now (&test, crc);
154 /* most content is 32k */
157 if (0 != i && GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
158 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
159 size = size - (size & 7); /* always multiple of 8 */
161 /* generate random key */
163 memset (value, i, size);
165 memset (value, i - 255, size / 2);
167 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "putting type %u, anon %u under key %s\n", i + 1, i,
171 crc->api->put (crc->api->cls,
175 value, i + 1 /* type */ ,
178 0 /* replication */ ,
179 GNUNET_TIME_relative_to_absolute
180 (GNUNET_TIME_relative_multiply
181 (GNUNET_TIME_UNIT_MILLISECONDS,
182 60 * 60 * 60 * 1000 +
183 GNUNET_CRYPTO_random_u32
184 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
191 static uint64_t guid;
195 iterate_one_shot (void *cls,
196 const struct GNUNET_HashCode *key,
199 enum GNUNET_BLOCK_Type type,
202 uint32_t replication,
203 struct GNUNET_TIME_Absolute expiration,
206 struct CpsRunContext *crc = cls;
208 GNUNET_assert (NULL != key);
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Found result type=%u, priority=%u, size=%u, expire=%s, key %s\n",
214 (unsigned int) priority,
216 GNUNET_STRINGS_absolute_time_to_string (expiration),
218 GNUNET_SCHEDULER_add_now (&test,
225 remove_continuation (void *cls,
226 const struct GNUNET_HashCode *key,
231 struct CpsRunContext *crc = cls;
233 GNUNET_assert (NULL != key);
234 GNUNET_assert (32768 == size);
235 GNUNET_assert (GNUNET_OK == status);
236 GNUNET_assert (NULL == msg);
238 GNUNET_SCHEDULER_add_now (&test,
244 * Function called when the service shuts
245 * down. Unloads our datastore plugin.
247 * @param api api to unload
248 * @param cfg configuration to use
251 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
252 const struct GNUNET_CONFIGURATION_Handle *cfg)
258 GNUNET_CONFIGURATION_get_value_string (cfg,
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264 _("No `%s' specified for `%s' in configuration!\n"),
269 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
270 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
271 GNUNET_free (libname);
278 * Last task run during shutdown. Disconnects us from
279 * the transport and core.
282 cleaning_task (void *cls)
284 struct CpsRunContext *crc = cls;
286 unload_plugin (crc->api, crc->cfg);
294 struct CpsRunContext *crc = cls;
295 struct GNUNET_HashCode key;
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
304 crc->api->drop (crc->api->cls);
305 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
315 GNUNET_SCHEDULER_add_now (&test, crc);
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 crc->api->get_key (crc->api->cls,
326 GNUNET_BLOCK_TYPE_ANY,
335 GNUNET_SCHEDULER_add_now (&test, crc);
338 crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
341 crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
344 crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
348 struct GNUNET_HashCode key;
349 uint32_t size = 32768;
353 memset (value, 0, size);
355 crc->api->remove_key (crc->api->cls,
359 &remove_continuation,
364 crc->api->drop (crc->api->cls);
365 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
372 * Load the datastore plugin.
374 static struct GNUNET_DATASTORE_PluginFunctions *
375 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
377 static struct GNUNET_DATASTORE_PluginEnvironment env;
378 struct GNUNET_DATASTORE_PluginFunctions *ret;
383 GNUNET_CONFIGURATION_get_value_string (cfg,
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 _("No `%s' specified for `%s' in configuration!\n"),
395 env.duc = &disk_utilization_change_cb;
397 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
399 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
400 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
402 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
403 GNUNET_free (libname);
405 ok = 77; /* mark test as skipped */
408 GNUNET_free (libname);
415 run (void *cls, char *const *args, const char *cfgfile,
416 const struct GNUNET_CONFIGURATION_Handle *c)
418 struct GNUNET_DATASTORE_PluginFunctions *api;
419 struct CpsRunContext *crc;
421 api = load_plugin (c);
425 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
428 crc = GNUNET_new (struct CpsRunContext);
432 GNUNET_SCHEDULER_add_now (&test, crc);
437 main (int argc, char *argv[])
441 char *const xargv[] = {
442 "test-plugin-datastore",
447 static struct GNUNET_GETOPT_CommandLineOption options[] = {
448 GNUNET_GETOPT_OPTION_END
451 /* determine name of plugin to use */
452 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
453 GNUNET_snprintf (dir_name, sizeof (dir_name),
454 "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
455 GNUNET_DISK_directory_remove (dir_name);
456 GNUNET_log_setup ("test-plugin-datastore",
459 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
460 "test_plugin_datastore_data_%s.conf", plugin_name);
461 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
462 "test-plugin-datastore", "nohelp", options, &run, NULL);
463 if ( (0 != ok) && (77 != ok) )
464 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
465 GNUNET_DISK_directory_remove (dir_name);
469 /* end of test_plugin_datastore.c */