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) ==
158 0) ) /* but some of it is less! */
159 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
160 size = size - (size & 7); /* always multiple of 8 */
162 /* generate random key */
164 memset (value, i, size);
166 memset (value, i - 255, size / 2);
168 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "putting type %u, anon %u under key %s\n", i + 1, i,
172 crc->api->put (crc->api->cls,
176 value, i + 1 /* type */,
180 GNUNET_TIME_relative_to_absolute
181 (GNUNET_TIME_relative_multiply
182 (GNUNET_TIME_UNIT_MILLISECONDS,
184 + GNUNET_CRYPTO_random_u32
185 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
192 static uint64_t guid;
196 iterate_one_shot (void *cls,
197 const struct GNUNET_HashCode *key,
200 enum GNUNET_BLOCK_Type type,
203 uint32_t replication,
204 struct GNUNET_TIME_Absolute expiration,
207 struct CpsRunContext *crc = cls;
209 GNUNET_assert (NULL != key);
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Found result type=%u, priority=%u, size=%u, expire=%s, key %s\n",
215 (unsigned int) priority,
217 GNUNET_STRINGS_absolute_time_to_string (expiration),
219 GNUNET_SCHEDULER_add_now (&test,
226 remove_continuation (void *cls,
227 const struct GNUNET_HashCode *key,
232 struct CpsRunContext *crc = cls;
234 GNUNET_assert (NULL != key);
235 GNUNET_assert (32768 == size);
236 GNUNET_assert (GNUNET_OK == status);
237 GNUNET_assert (NULL == msg);
239 GNUNET_SCHEDULER_add_now (&test,
245 * Function called when the service shuts
246 * down. Unloads our datastore plugin.
248 * @param api api to unload
249 * @param cfg configuration to use
252 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
253 const struct GNUNET_CONFIGURATION_Handle *cfg)
259 GNUNET_CONFIGURATION_get_value_string (cfg,
264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
265 _ ("No `%s' specified for `%s' in configuration!\n"),
270 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
271 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
272 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);
317 GNUNET_SCHEDULER_add_now (&test, crc);
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 crc->api->get_key (crc->api->cls,
328 GNUNET_BLOCK_TYPE_ANY,
338 GNUNET_SCHEDULER_add_now (&test, crc);
341 crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
345 crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
349 crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
354 struct GNUNET_HashCode key;
355 uint32_t size = 32768;
359 memset (value, 0, size);
361 crc->api->remove_key (crc->api->cls,
365 &remove_continuation,
371 crc->api->drop (crc->api->cls);
372 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
379 * Load the datastore plugin.
381 static struct GNUNET_DATASTORE_PluginFunctions *
382 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
384 static struct GNUNET_DATASTORE_PluginEnvironment env;
385 struct GNUNET_DATASTORE_PluginFunctions *ret;
390 GNUNET_CONFIGURATION_get_value_string (cfg,
395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
396 _ ("No `%s' specified for `%s' in configuration!\n"),
402 env.duc = &disk_utilization_change_cb;
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datastore plugin\n"),
406 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
407 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
409 fprintf (stderr, "Failed to load plugin `%s'!\n", name);
410 GNUNET_free (libname);
412 ok = 77; /* mark test as skipped */
415 GNUNET_free (libname);
422 run (void *cls, char *const *args, const char *cfgfile,
423 const struct GNUNET_CONFIGURATION_Handle *c)
425 struct GNUNET_DATASTORE_PluginFunctions *api;
426 struct CpsRunContext *crc;
428 api = load_plugin (c);
433 "Could not initialize plugin, assuming database not configured. Test not run!\n");
436 crc = GNUNET_new (struct CpsRunContext);
440 GNUNET_SCHEDULER_add_now (&test, crc);
445 main (int argc, char *argv[])
447 char dir_name[PATH_MAX];
448 char cfg_name[PATH_MAX];
449 char *const xargv[] = {
450 "test-plugin-datastore",
455 static struct GNUNET_GETOPT_CommandLineOption options[] = {
456 GNUNET_GETOPT_OPTION_END
459 /* determine name of plugin to use */
460 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
461 GNUNET_snprintf (dir_name, sizeof(dir_name),
462 "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
463 GNUNET_DISK_directory_remove (dir_name);
464 GNUNET_log_setup ("test-plugin-datastore",
467 GNUNET_snprintf (cfg_name, sizeof(cfg_name),
468 "test_plugin_datastore_data_%s.conf", plugin_name);
469 GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1, xargv,
470 "test-plugin-datastore", "nohelp", options, &run, NULL);
471 if ((0 != ok) && (77 != ok))
472 fprintf (stderr, "Missed some testcases: %u\n", ok);
473 GNUNET_DISK_directory_remove (dir_name);
478 /* end of test_plugin_datastore.c */