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;
73 * Function called by plugins to notify us about a
74 * change in their disk utilization.
76 * @param cls closure (NULL)
77 * @param delta change in disk utilization,
78 * 0 for "reset to empty"
81 disk_utilization_change_cb (void *cls,
92 memset (key, 0, sizeof (GNUNET_HashCode));
93 key->bits[0] = (unsigned int) i;
94 GNUNET_CRYPTO_hash (key, sizeof (GNUNET_HashCode), key);
99 put_value (struct GNUNET_DATASTORE_PluginFunctions * api,
109 /* most content is 32k */
112 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
113 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
114 size = size - (size & 7); /* always multiple of 8 */
116 /* generate random key */
118 memset (value, i, size);
120 memset (value, i - 255, size / 2);
123 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
126 "putting type %u, anon %u under key %s\n",
127 i+1, i, GNUNET_h2s (&key));
129 if (GNUNET_OK != api->put (api->cls,
137 GNUNET_TIME_relative_to_absolute
138 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
139 60 * 60 * 60 * 1000 +
140 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
143 fprintf (stderr, "ERROR: `%s'\n", msg);
144 GNUNET_free_non_null (msg);
147 stored_bytes += size;
155 const struct GNUNET_SCHEDULER_TaskContext *tc);
158 static uint64_t guid;
162 iterate_one_shot (void *cls,
163 const GNUNET_HashCode * key,
166 enum GNUNET_BLOCK_Type type,
169 struct GNUNET_TIME_Absolute
173 struct CpsRunContext *crc = cls;
175 GNUNET_assert (key != NULL);
180 "Found result type=%u, priority=%u, size=%u, expire=%llu, key %s\n",
181 type, priority, size,
182 (unsigned long long) expiration.abs_value,
185 GNUNET_SCHEDULER_add_now (&test, crc);
191 * Function called when the service shuts
192 * down. Unloads our datastore plugin.
194 * @param api api to unload
195 * @param cfg configuration to use
198 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
199 const struct GNUNET_CONFIGURATION_Handle *cfg)
205 GNUNET_CONFIGURATION_get_value_string (cfg,
206 "DATASTORE", "DATABASE", &name))
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
209 _("No `%s' specified for `%s' in configuration!\n"),
214 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
215 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
216 GNUNET_free (libname);
223 * Last task run during shutdown. Disconnects us from
224 * the transport and core.
227 cleaning_task (void *cls,
228 const struct GNUNET_SCHEDULER_TaskContext *tc)
230 struct CpsRunContext *crc = cls;
232 unload_plugin (crc->api, crc->cfg);
239 const struct GNUNET_SCHEDULER_TaskContext *tc)
241 struct CpsRunContext *crc = cls;
243 unsigned long long os;
244 unsigned long long cs;
247 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
251 crc->phase = RP_ERROR;
254 fprintf (stderr, "In phase %d, iteration %u\n",
263 crc->api->drop (crc->api->cls);
264 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
268 for (j=0;j<PUT_10;j++)
270 put_value (crc->api, j, crc->i);
271 cs = crc->api->estimate_size (crc->api->cls);
272 GNUNET_assert (os < cs);
276 GNUNET_SCHEDULER_add_now (&test, crc);
283 GNUNET_SCHEDULER_add_now (&test, crc);
287 crc->api->get_key (crc->api->cls,
290 GNUNET_BLOCK_TYPE_ANY,
295 GNUNET_assert (GNUNET_OK ==
296 crc->api->update (crc->api->cls,
298 GNUNET_TIME_UNIT_ZERO_ABS,
301 GNUNET_SCHEDULER_add_now (&test, crc);
309 GNUNET_SCHEDULER_add_now (&test, crc);
312 crc->api->get_zero_anonymity (crc->api->cls,
319 crc->api->get_replication (crc->api->cls,
324 crc->api->get_expiration (crc->api->cls,
329 crc->api->drop (crc->api->cls);
330 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
337 * Load the datastore plugin.
339 static struct GNUNET_DATASTORE_PluginFunctions *
340 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
342 static struct GNUNET_DATASTORE_PluginEnvironment env;
343 struct GNUNET_DATASTORE_PluginFunctions * ret;
348 GNUNET_CONFIGURATION_get_value_string (cfg,
349 "DATASTORE", "DATABASE", &name))
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 _("No `%s' specified for `%s' in configuration!\n"),
358 env.duc = &disk_utilization_change_cb;
360 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
361 _("Loading `%s' datastore plugin\n"), name);
362 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
363 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
366 "Failed to load plugin `%s'!\n",
370 GNUNET_free (libname);
380 const struct GNUNET_CONFIGURATION_Handle *c)
382 struct GNUNET_DATASTORE_PluginFunctions *api;
383 struct CpsRunContext *crc;
385 api = load_plugin (c);
389 "Could not initialize plugin, assuming database not configured. Test not run!\n");
392 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
396 GNUNET_SCHEDULER_add_now (&test, crc);
404 char *const argv[] = {
405 "test-plugin-datastore",
413 struct GNUNET_GETOPT_CommandLineOption options[] = {
414 GNUNET_GETOPT_OPTION_END
417 GNUNET_snprintf (cfg_name,
419 "test_plugin_datastore_data_%s.conf",
421 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
422 argv, "test-plugin-datastore", "nohelp",
423 options, &run, NULL);
425 fprintf (stderr, "Missed some testcases: %u\n", ok);
431 main (int argc, char *argv[])
438 /* determine name of plugin to use */
439 plugin_name = argv[0];
440 while (NULL != (pos = strstr(plugin_name, "_")))
442 if (NULL != (pos = strstr(plugin_name, ".")))
445 pos = (char *) plugin_name;
447 GNUNET_snprintf (dir_name,
449 "/tmp/test-gnunet-datastore-%s",
451 GNUNET_DISK_directory_remove (dir_name);
452 GNUNET_log_setup ("test-plugin-datastore",
460 if (pos != plugin_name)
462 GNUNET_DISK_directory_remove (dir_name);
467 /* end of test_plugin_datastore.c */