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, int delta)
88 gen_key (int i, GNUNET_HashCode * key)
90 memset (key, 0, sizeof (GNUNET_HashCode));
91 key->bits[0] = (unsigned int) i;
92 GNUNET_CRYPTO_hash (key, sizeof (GNUNET_HashCode), key);
97 put_value (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
105 /* most content is 32k */
108 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
109 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
110 size = size - (size & 7); /* always multiple of 8 */
112 /* generate random key */
114 memset (value, i, size);
116 memset (value, i - 255, size / 2);
119 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
121 fprintf (stderr, "putting type %u, anon %u under key %s\n", i + 1, i,
124 if (GNUNET_OK != api->put (api->cls, &key, size, value, i + 1 /* type */ ,
125 prio, i /* anonymity */ ,
126 0 /* replication */ ,
127 GNUNET_TIME_relative_to_absolute
128 (GNUNET_TIME_relative_multiply
129 (GNUNET_TIME_UNIT_MILLISECONDS,
130 60 * 60 * 60 * 1000 +
131 GNUNET_CRYPTO_random_u32
132 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
134 fprintf (stderr, "ERROR: `%s'\n", msg);
135 GNUNET_free_non_null (msg);
138 stored_bytes += size;
144 static void test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
147 static uint64_t guid;
151 iterate_one_shot (void *cls, const GNUNET_HashCode * key, uint32_t size,
152 const void *data, enum GNUNET_BLOCK_Type type,
153 uint32_t priority, uint32_t anonymity,
154 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
156 struct CpsRunContext *crc = cls;
158 GNUNET_assert (key != NULL);
163 "Found result type=%u, priority=%u, size=%u, expire=%llu, key %s\n",
164 type, priority, size, (unsigned long long) expiration.abs_value,
167 GNUNET_SCHEDULER_add_now (&test, crc);
173 * Function called when the service shuts
174 * down. Unloads our datastore plugin.
176 * @param api api to unload
177 * @param cfg configuration to use
180 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
181 const struct GNUNET_CONFIGURATION_Handle *cfg)
187 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
195 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
196 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
197 GNUNET_free (libname);
204 * Last task run during shutdown. Disconnects us from
205 * the transport and core.
208 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
210 struct CpsRunContext *crc = cls;
212 unload_plugin (crc->api, crc->cfg);
218 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
220 struct CpsRunContext *crc = cls;
222 unsigned long long os;
223 unsigned long long cs;
226 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
228 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test aborted.\n");
229 crc->phase = RP_ERROR;
232 fprintf (stderr, "In phase %d, iteration %u\n", crc->phase, crc->cnt);
239 crc->api->drop (crc->api->cls);
240 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
244 for (j = 0; j < PUT_10; j++)
246 put_value (crc->api, j, crc->i);
247 cs = crc->api->estimate_size (crc->api->cls);
248 GNUNET_assert (os <= cs);
252 GNUNET_SCHEDULER_add_now (&test, crc);
259 GNUNET_SCHEDULER_add_now (&test, crc);
263 crc->api->get_key (crc->api->cls, crc->offset++, &key, NULL,
264 GNUNET_BLOCK_TYPE_ANY, &iterate_one_shot, crc);
267 GNUNET_assert (GNUNET_OK ==
268 crc->api->update (crc->api->cls, guid, 1,
269 GNUNET_TIME_UNIT_ZERO_ABS, NULL));
271 GNUNET_SCHEDULER_add_now (&test, crc);
279 GNUNET_SCHEDULER_add_now (&test, crc);
282 crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
285 crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
288 crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
291 crc->api->drop (crc->api->cls);
292 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
299 * Load the datastore plugin.
301 static struct GNUNET_DATASTORE_PluginFunctions *
302 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
304 static struct GNUNET_DATASTORE_PluginEnvironment env;
305 struct GNUNET_DATASTORE_PluginFunctions *ret;
310 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
319 env.duc = &disk_utilization_change_cb;
321 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
323 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
324 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
326 fprintf (stderr, "Failed to load plugin `%s'!\n", name);
329 GNUNET_free (libname);
336 run (void *cls, char *const *args, const char *cfgfile,
337 const struct GNUNET_CONFIGURATION_Handle *c)
339 struct GNUNET_DATASTORE_PluginFunctions *api;
340 struct CpsRunContext *crc;
342 api = load_plugin (c);
346 "Could not initialize plugin, assuming database not configured. Test not run!\n");
349 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
353 GNUNET_SCHEDULER_add_now (&test, crc);
362 char *const argv[] = {
363 "test-plugin-datastore",
371 struct GNUNET_GETOPT_CommandLineOption options[] = {
372 GNUNET_GETOPT_OPTION_END
375 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
376 "test_plugin_datastore_data_%s.conf", plugin_name);
377 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
378 "test-plugin-datastore", "nohelp", options, &run, NULL);
380 fprintf (stderr, "Missed some testcases: %u\n", ok);
386 main (int argc, char *argv[])
393 /* determine name of plugin to use */
394 plugin_name = argv[0];
395 while (NULL != (pos = strstr (plugin_name, "_")))
396 plugin_name = pos + 1;
397 if (NULL != (pos = strstr (plugin_name, ".")))
400 pos = (char *) plugin_name;
402 GNUNET_snprintf (dir_name, sizeof (dir_name),
403 "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
404 GNUNET_DISK_directory_remove (dir_name);
405 GNUNET_log_setup ("test-plugin-datastore",
413 if (pos != plugin_name)
415 GNUNET_DISK_directory_remove (dir_name);
420 /* end of test_plugin_datastore.c */