2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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"
31 #define VERBOSE GNUNET_NO
34 * Number of put operations to perform.
38 static unsigned long long stored_bytes;
40 static unsigned long long stored_entries;
42 static unsigned long long stored_ops;
44 static const char *plugin_name;
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
64 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,
91 memset (key, 0, sizeof (GNUNET_HashCode));
92 key->bits[0] = (unsigned int) i;
93 GNUNET_CRYPTO_hash (key, sizeof (GNUNET_HashCode), key);
98 put_value (struct GNUNET_DATASTORE_PluginFunctions * api,
108 /* most content is 32k */
111 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
112 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
113 size = size - (size & 7); /* always multiple of 8 */
115 /* generate random key */
117 memset (value, i, size);
119 memset (value, i - 255, size / 2);
122 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
123 if (GNUNET_OK != api->put (api->cls,
131 GNUNET_TIME_relative_to_absolute
132 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
133 60 * 60 * 60 * 1000 +
134 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
137 fprintf (stderr, "ERROR: `%s'\n", msg);
138 GNUNET_free_non_null (msg);
141 stored_bytes += size;
149 const struct GNUNET_SCHEDULER_TaskContext *tc);
153 iterate_one_shot (void *cls,
155 const GNUNET_HashCode * key,
158 enum GNUNET_BLOCK_Type type,
161 struct GNUNET_TIME_Absolute
165 struct CpsRunContext *crc = cls;
167 GNUNET_assert (NULL == next_cls);
168 GNUNET_assert (key != NULL);
172 "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
173 type, priority, size,
174 (unsigned long long) expiration.abs_value);
176 GNUNET_SCHEDULER_add_now (&test, crc);
181 static uint64_t guid;
184 iterate_with_next (void *cls,
186 const GNUNET_HashCode * key,
189 enum GNUNET_BLOCK_Type type,
192 struct GNUNET_TIME_Absolute
196 struct CpsRunContext *crc = cls;
201 GNUNET_SCHEDULER_add_now (&test, crc);
207 "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
208 type, priority, size,
209 (unsigned long long) expiration.abs_value);
212 crc->api->next_request (next_cls,
219 * Function called when the service shuts
220 * down. Unloads our datastore plugin.
222 * @param api api to unload
223 * @param cfg configuration to use
226 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
227 const struct GNUNET_CONFIGURATION_Handle *cfg)
233 GNUNET_CONFIGURATION_get_value_string (cfg,
234 "DATASTORE", "DATABASE", &name))
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 _("No `%s' specified for `%s' in configuration!\n"),
242 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
243 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
244 GNUNET_free (libname);
251 * Last task run during shutdown. Disconnects us from
252 * the transport and core.
255 cleaning_task (void *cls,
256 const struct GNUNET_SCHEDULER_TaskContext *tc)
258 struct CpsRunContext *crc = cls;
260 unload_plugin (crc->api, crc->cfg);
267 const struct GNUNET_SCHEDULER_TaskContext *tc)
269 struct CpsRunContext *crc = cls;
271 unsigned long long os;
272 unsigned long long cs;
275 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
277 crc->phase = RP_ERROR;
284 crc->api->drop (crc->api->cls);
285 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
289 for (j=0;j<PUT_10;j++)
291 put_value (crc->api, j, crc->i);
292 cs = crc->api->get_size (crc->api->cls);
293 GNUNET_assert (os < cs);
297 GNUNET_SCHEDULER_add_now (&test, crc);
304 GNUNET_SCHEDULER_add_now (&test, crc);
308 crc->api->get (crc->api->cls,
310 GNUNET_BLOCK_TYPE_ANY,
315 GNUNET_assert (GNUNET_OK ==
316 crc->api->update (crc->api->cls,
318 GNUNET_TIME_UNIT_ZERO_ABS,
321 GNUNET_SCHEDULER_add_now (&test, crc);
329 GNUNET_SCHEDULER_add_now (&test, crc);
332 crc->api->iter_zero_anonymity (crc->api->cls,
338 crc->api->replication_get (crc->api->cls,
343 crc->api->expiration_get (crc->api->cls,
348 crc->api->drop (crc->api->cls);
349 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
356 * Load the datastore plugin.
358 static struct GNUNET_DATASTORE_PluginFunctions *
359 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
361 static struct GNUNET_DATASTORE_PluginEnvironment env;
362 struct GNUNET_DATASTORE_PluginFunctions * ret;
367 GNUNET_CONFIGURATION_get_value_string (cfg,
368 "DATASTORE", "DATABASE", &name))
370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371 _("No `%s' specified for `%s' in configuration!\n"),
377 env.duc = &disk_utilization_change_cb;
379 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
380 _("Loading `%s' datastore plugin\n"), name);
381 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
382 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
385 "Failed to load plugin `%s'!\n",
389 GNUNET_free (libname);
399 const struct GNUNET_CONFIGURATION_Handle *c)
401 struct GNUNET_DATASTORE_PluginFunctions *api;
402 struct CpsRunContext *crc;
404 api = load_plugin (c);
408 "Could not initialize plugin, assuming database not configured. Test not run!\n");
411 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
415 GNUNET_SCHEDULER_add_now (&test, crc);
423 char *const argv[] = {
424 "test-plugin-datastore",
432 struct GNUNET_GETOPT_CommandLineOption options[] = {
433 GNUNET_GETOPT_OPTION_END
436 GNUNET_snprintf (cfg_name,
438 "test_plugin_datastore_data_%s.conf",
440 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
441 argv, "test-plugin-datastore", "nohelp",
442 options, &run, NULL);
444 fprintf (stderr, "Missed some testcases: %u\n", ok);
450 main (int argc, char *argv[])
456 /* determine name of plugin to use */
457 plugin_name = argv[0];
458 while (NULL != (pos = strstr(plugin_name, "_")))
460 if (NULL != (pos = strstr(plugin_name, ".")))
463 pos = (char *) plugin_name;
465 GNUNET_snprintf (dir_name,
467 "/tmp/test-gnunet-datastore-%s",
469 GNUNET_DISK_directory_remove (dir_name);
470 GNUNET_log_setup ("test-plugin-datastore",
478 if (pos != plugin_name)
480 GNUNET_DISK_directory_remove (dir_name);
485 /* end of test_plugin_datastore.c */