2 This file is part of GNUnet.
3 Copyright (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
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;
72 * Function called by plugins to notify us about a
73 * change in their disk utilization.
75 * @param cls closure (NULL)
76 * @param delta change in disk utilization,
77 * 0 for "reset to empty"
80 disk_utilization_change_cb (void *cls, int delta)
88 const struct GNUNET_SCHEDULER_TaskContext *tc);
95 * @param key key for the item stored
96 * @param size size of the item stored
97 * @param status #GNUNET_OK or #GNUNET_SYSERROR
98 * @param msg error message on error
101 put_continuation (void *cls,
102 const struct GNUNET_HashCode *key,
107 struct CpsRunContext *crc = cls;
108 static unsigned long long os;
109 unsigned long long cs;
111 if (GNUNET_OK != status)
113 FPRINTF (stderr, "ERROR: `%s'\n", msg);
117 crc->api->estimate_size (crc->api->cls, &cs);
118 GNUNET_assert (os <= cs);
120 stored_bytes += size;
124 GNUNET_SCHEDULER_add_now (&test, crc);
129 gen_key (int i, struct GNUNET_HashCode * key)
131 memset (key, 0, sizeof (struct GNUNET_HashCode));
132 key->bits[0] = (unsigned int) i;
133 GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_HashCode), key);
138 do_put (struct CpsRunContext *crc)
142 struct GNUNET_HashCode key;
150 GNUNET_SCHEDULER_add_now (&test, crc);
153 /* most content is 32k */
156 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
157 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
158 size = size - (size & 7); /* always multiple of 8 */
160 /* generate random key */
162 memset (value, i, size);
164 memset (value, i - 255, size / 2);
166 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "putting type %u, anon %u under key %s\n", i + 1, i,
170 crc->api->put (crc->api->cls, &key, size, value, i + 1 /* type */ ,
171 prio, i /* anonymity */ ,
172 0 /* replication */ ,
173 GNUNET_TIME_relative_to_absolute
174 (GNUNET_TIME_relative_multiply
175 (GNUNET_TIME_UNIT_MILLISECONDS,
176 60 * 60 * 60 * 1000 +
177 GNUNET_CRYPTO_random_u32
178 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
179 put_continuation, crc);
184 static uint64_t guid;
188 iterate_one_shot (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
189 const void *data, enum GNUNET_BLOCK_Type type,
190 uint32_t priority, uint32_t anonymity,
191 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
193 struct CpsRunContext *crc = cls;
195 GNUNET_assert (key != NULL);
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Found result type=%u, priority=%u, size=%u, expire=%s, key %s\n",
200 type, priority, size,
201 GNUNET_STRINGS_absolute_time_to_string (expiration),
203 GNUNET_SCHEDULER_add_now (&test, crc);
209 * Function called when the service shuts
210 * down. Unloads our datastore plugin.
212 * @param api api to unload
213 * @param cfg configuration to use
216 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
217 const struct GNUNET_CONFIGURATION_Handle *cfg)
223 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
231 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
232 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
233 GNUNET_free (libname);
240 * Last task run during shutdown. Disconnects us from
241 * the transport and core.
244 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 struct CpsRunContext *crc = cls;
248 unload_plugin (crc->api, crc->cfg);
254 update_continuation (void *cls,
258 struct CpsRunContext *crc = cls;
260 GNUNET_assert (GNUNET_OK == status);
262 GNUNET_SCHEDULER_add_now (&test, crc);
267 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
269 struct CpsRunContext *crc = cls;
270 struct GNUNET_HashCode key;
272 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
274 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test aborted.\n");
275 crc->phase = RP_ERROR;
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
284 crc->api->drop (crc->api->cls);
285 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
295 GNUNET_SCHEDULER_add_now (&test, crc);
299 crc->api->get_key (crc->api->cls, crc->offset++, &key, NULL,
300 GNUNET_BLOCK_TYPE_ANY, &iterate_one_shot, crc);
303 crc->api->update (crc->api->cls,
306 GNUNET_TIME_UNIT_ZERO_ABS,
307 &update_continuation,
316 GNUNET_SCHEDULER_add_now (&test, crc);
319 crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
322 crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
325 crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
328 crc->api->drop (crc->api->cls);
329 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
336 * Load the datastore plugin.
338 static struct GNUNET_DATASTORE_PluginFunctions *
339 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
341 static struct GNUNET_DATASTORE_PluginEnvironment env;
342 struct GNUNET_DATASTORE_PluginFunctions *ret;
347 GNUNET_CONFIGURATION_get_value_string (cfg,
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 _("No `%s' specified for `%s' in configuration!\n"),
359 env.duc = &disk_utilization_change_cb;
361 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
363 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
364 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
366 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
367 GNUNET_free (libname);
369 ok = 77; /* mark test as skipped */
372 GNUNET_free (libname);
379 run (void *cls, char *const *args, const char *cfgfile,
380 const struct GNUNET_CONFIGURATION_Handle *c)
382 struct GNUNET_DATASTORE_PluginFunctions *api;
383 struct CpsRunContext *crc;
385 api = load_plugin (c);
389 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
392 crc = GNUNET_new (struct CpsRunContext);
396 GNUNET_SCHEDULER_add_now (&test, crc);
401 main (int argc, char *argv[])
405 char *const xargv[] = {
406 "test-plugin-datastore",
411 static struct GNUNET_GETOPT_CommandLineOption options[] = {
412 GNUNET_GETOPT_OPTION_END
415 /* determine name of plugin to use */
416 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
417 GNUNET_snprintf (dir_name, sizeof (dir_name),
418 "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
419 GNUNET_DISK_directory_remove (dir_name);
420 GNUNET_log_setup ("test-plugin-datastore",
423 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
424 "test_plugin_datastore_data_%s.conf", plugin_name);
425 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
426 "test-plugin-datastore", "nohelp", options, &run, NULL);
427 if ( (0 != ok) && (77 != ok) )
428 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
429 GNUNET_DISK_directory_remove (dir_name);
433 /* end of test_plugin_datastore.c */