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"
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)
87 gen_key (int i, struct GNUNET_HashCode * key)
89 memset (key, 0, sizeof (struct GNUNET_HashCode));
90 key->bits[0] = (unsigned int) i;
91 GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_HashCode), key);
96 put_value (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
100 struct GNUNET_HashCode key;
104 /* most content is 32k */
107 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
108 size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
109 size = size - (size & 7); /* always multiple of 8 */
111 /* generate random key */
113 memset (value, i, size);
115 memset (value, i - 255, size / 2);
118 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 "putting type %u, anon %u under key %s\n", i + 1, i,
122 if (GNUNET_OK != api->put (api->cls, &key, size, value, i + 1 /* type */ ,
123 prio, i /* anonymity */ ,
124 0 /* replication */ ,
125 GNUNET_TIME_relative_to_absolute
126 (GNUNET_TIME_relative_multiply
127 (GNUNET_TIME_UNIT_MILLISECONDS,
128 60 * 60 * 60 * 1000 +
129 GNUNET_CRYPTO_random_u32
130 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
132 FPRINTF (stderr, "ERROR: `%s'\n", msg);
133 GNUNET_free_non_null (msg);
136 stored_bytes += size;
143 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
146 static uint64_t guid;
150 iterate_one_shot (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
151 const void *data, enum GNUNET_BLOCK_Type type,
152 uint32_t priority, uint32_t anonymity,
153 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
155 struct CpsRunContext *crc = cls;
157 GNUNET_assert (key != NULL);
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Found result type=%u, priority=%u, size=%u, expire=%s, key %s\n",
162 type, priority, size,
163 GNUNET_STRINGS_absolute_time_to_string (expiration),
165 GNUNET_SCHEDULER_add_now (&test, crc);
171 * Function called when the service shuts
172 * down. Unloads our datastore plugin.
174 * @param api api to unload
175 * @param cfg configuration to use
178 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
179 const struct GNUNET_CONFIGURATION_Handle *cfg)
185 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
193 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
194 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
195 GNUNET_free (libname);
202 * Last task run during shutdown. Disconnects us from
203 * the transport and core.
206 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 struct CpsRunContext *crc = cls;
210 unload_plugin (crc->api, crc->cfg);
216 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 struct CpsRunContext *crc = cls;
220 unsigned long long os;
221 unsigned long long cs;
222 struct GNUNET_HashCode key;
224 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test aborted.\n");
227 crc->phase = RP_ERROR;
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
236 crc->api->drop (crc->api->cls);
237 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
241 for (j = 0; j < PUT_10; j++)
243 put_value (crc->api, j, crc->i);
244 cs = crc->api->estimate_size (crc->api->cls);
245 GNUNET_assert (os <= cs);
249 GNUNET_SCHEDULER_add_now (&test, crc);
256 GNUNET_SCHEDULER_add_now (&test, crc);
260 crc->api->get_key (crc->api->cls, crc->offset++, &key, NULL,
261 GNUNET_BLOCK_TYPE_ANY, &iterate_one_shot, crc);
264 GNUNET_assert (GNUNET_OK ==
265 crc->api->update (crc->api->cls, guid, 1,
266 GNUNET_TIME_UNIT_ZERO_ABS, NULL));
268 GNUNET_SCHEDULER_add_now (&test, crc);
276 GNUNET_SCHEDULER_add_now (&test, crc);
279 crc->api->get_zero_anonymity (crc->api->cls, 0, 1, &iterate_one_shot, crc);
282 crc->api->get_replication (crc->api->cls, &iterate_one_shot, crc);
285 crc->api->get_expiration (crc->api->cls, &iterate_one_shot, crc);
288 crc->api->drop (crc->api->cls);
289 GNUNET_SCHEDULER_add_now (&cleaning_task, crc);
296 * Load the datastore plugin.
298 static struct GNUNET_DATASTORE_PluginFunctions *
299 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
301 static struct GNUNET_DATASTORE_PluginEnvironment env;
302 struct GNUNET_DATASTORE_PluginFunctions *ret;
307 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
311 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
316 env.duc = &disk_utilization_change_cb;
318 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
320 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
321 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
323 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
324 GNUNET_free (libname);
328 GNUNET_free (libname);
335 run (void *cls, char *const *args, const char *cfgfile,
336 const struct GNUNET_CONFIGURATION_Handle *c)
338 struct GNUNET_DATASTORE_PluginFunctions *api;
339 struct CpsRunContext *crc;
341 api = load_plugin (c);
345 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
348 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
352 GNUNET_SCHEDULER_add_now (&test, crc);
357 main (int argc, char *argv[])
361 char *const xargv[] = {
362 "test-plugin-datastore",
367 static struct GNUNET_GETOPT_CommandLineOption options[] = {
368 GNUNET_GETOPT_OPTION_END
371 /* determine name of plugin to use */
372 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
373 GNUNET_snprintf (dir_name, sizeof (dir_name),
374 "/tmp/test-gnunet-datastore-plugin-%s", plugin_name);
375 GNUNET_DISK_directory_remove (dir_name);
376 GNUNET_log_setup ("test-plugin-datastore",
379 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
380 "test_plugin_datastore_data_%s.conf", plugin_name);
381 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
382 "test-plugin-datastore", "nohelp", options, &run, NULL);
384 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
385 GNUNET_DISK_directory_remove (dir_name);
389 /* end of test_plugin_datastore.c */