2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2011 GNUnet e.V.
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 datastore/test_datastore_api_management.c
22 * @brief Test for the space management functions of the datastore implementation.
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_protocols.h"
28 #include "gnunet_datastore_service.h"
29 #include "gnunet_datastore_plugin.h"
30 #include "gnunet_testing_lib.h"
34 * How long until we give up on transmitting the message?
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
39 * Number of iterations to run; must be large enough
40 * so that the quota will be exceeded!
42 #define ITERATIONS 5000
55 struct GNUNET_HashCode key;
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
65 static struct GNUNET_DATASTORE_Handle *datastore;
67 static struct GNUNET_TIME_Absolute now;
71 static const char *plugin_name;
77 return 8 + 8 * (i % 256);
84 static char buf[60000];
86 memset (buf, i, 8 + 8 * (i % 256));
106 get_anonymity (int i)
112 static struct GNUNET_TIME_Absolute
113 get_expiration (int i)
115 struct GNUNET_TIME_Absolute av;
117 av.abs_value_us = now.abs_value_us + i * 1000 * 1000LL;
123 run_continuation (void *cls);
127 check_success (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
129 struct CpsRunContext *crc = cls;
131 if (GNUNET_OK != success)
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", msg);
133 GNUNET_assert (GNUNET_OK == success);
134 GNUNET_free_non_null (crc->data);
136 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
141 check_value (void *cls, const struct GNUNET_HashCode * key, size_t size,
142 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
143 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
146 struct CpsRunContext *crc = cls;
151 crc->phase = RP_GET_FAIL;
152 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
156 GNUNET_assert (size == get_size (i));
157 GNUNET_assert (0 == memcmp (data, get_data (i), size));
158 GNUNET_assert (type == get_type (i));
159 GNUNET_assert (priority == get_priority (i));
160 GNUNET_assert (anonymity == get_anonymity (i));
161 GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
165 crc->phase = RP_DONE;
166 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
171 check_nothing (void *cls, const struct GNUNET_HashCode * key, size_t size,
172 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
173 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
176 struct CpsRunContext *crc = cls;
178 GNUNET_assert (key == NULL);
180 crc->phase = RP_DONE;
181 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
186 run_continuation (void *cls)
188 struct CpsRunContext *crc = cls;
190 ok = (int) crc->phase;
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
196 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
197 GNUNET_DATASTORE_put (datastore,
203 get_priority (crc->i),
204 get_anonymity (crc->i),
206 get_expiration (crc->i),
209 &check_success, crc);
211 if (crc->i == ITERATIONS)
213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
214 "Sleeping to give datastore time to clean up\n");
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
223 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
224 GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
225 get_type (crc->i), 1, 1,
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)",
232 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
233 GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
234 get_type (crc->i), 1, 1,
239 GNUNET_assert (0 == crc->i);
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
241 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
249 run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
251 struct CpsRunContext *crc = cls;
253 if (success != GNUNET_YES)
256 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
258 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
262 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
268 const struct GNUNET_CONFIGURATION_Handle *cfg,
269 struct GNUNET_TESTING_Peer *peer)
271 struct CpsRunContext *crc;
272 static struct GNUNET_HashCode zkey;
274 crc = GNUNET_new (struct CpsRunContext);
277 now = GNUNET_TIME_absolute_get ();
278 datastore = GNUNET_DATASTORE_connect (cfg);
280 GNUNET_DATASTORE_put (datastore,
285 GNUNET_BLOCK_TYPE_TEST,
287 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
293 FPRINTF (stderr, "%s", "Test 'put' operation failed.\n");
301 * Function called when disk utilization changes, does nothing.
304 * @param delta change in utilization
307 ignore_payload_cb (void *cls,
315 * check if plugin is actually working
318 test_plugin (const char *cfg_name)
321 struct GNUNET_CONFIGURATION_Handle *cfg;
322 struct GNUNET_DATASTORE_PluginFunctions *api;
323 struct GNUNET_DATASTORE_PluginEnvironment env;
325 cfg = GNUNET_CONFIGURATION_create ();
327 GNUNET_CONFIGURATION_load (cfg,
330 GNUNET_CONFIGURATION_destroy (cfg);
332 "Failed to load configuration %s\n",
336 memset (&env, 0, sizeof (env));
338 env.duc = &ignore_payload_cb;
339 GNUNET_snprintf (libname,
341 "libgnunet_plugin_datastore_%s",
343 api = GNUNET_PLUGIN_load (libname, &env);
346 GNUNET_CONFIGURATION_destroy (cfg);
348 "Failed to load plugin `%s'\n",
352 GNUNET_PLUGIN_unload (libname, api);
353 GNUNET_CONFIGURATION_destroy (cfg);
359 main (int argc, char *argv[])
364 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
365 GNUNET_snprintf (cfg_name,
367 "test_datastore_api_data_%s.conf",
369 ret = test_plugin (cfg_name);
373 GNUNET_TESTING_peer_run ("test-gnunet-datastore-management",
381 /* end of test_datastore_api_management.c */