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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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;
64 static struct GNUNET_DATASTORE_Handle *datastore;
66 static struct GNUNET_TIME_Absolute now;
70 static const char *plugin_name;
76 return 8 + 8 * (i % 256);
83 static char buf[60000];
85 memset (buf, i, 8 + 8 * (i % 256));
105 get_anonymity (int i)
111 static struct GNUNET_TIME_Absolute
112 get_expiration (int i)
114 struct GNUNET_TIME_Absolute av;
116 av.abs_value_us = now.abs_value_us + i * 1000 * 1000LL;
122 run_continuation (void *cls);
126 check_success (void *cls, int success, struct GNUNET_TIME_Absolute
127 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,
142 const struct GNUNET_HashCode *key,
145 enum GNUNET_BLOCK_Type type,
148 uint32_t replication,
149 struct GNUNET_TIME_Absolute expiration,
152 struct CpsRunContext *crc = cls;
157 crc->phase = RP_GET_FAIL;
158 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
162 GNUNET_assert (size == get_size (i));
163 GNUNET_assert (0 == memcmp (data, get_data (i), size));
164 GNUNET_assert (type == get_type (i));
165 GNUNET_assert (priority == get_priority (i));
166 GNUNET_assert (anonymity == get_anonymity (i));
167 GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
170 crc->phase = RP_DONE;
171 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
176 check_nothing (void *cls,
177 const struct GNUNET_HashCode *key,
180 enum GNUNET_BLOCK_Type type,
183 uint32_t replication,
184 struct GNUNET_TIME_Absolute expiration,
187 struct CpsRunContext *crc = cls;
189 GNUNET_assert (key == NULL);
191 crc->phase = RP_DONE;
192 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
197 run_continuation (void *cls)
199 struct CpsRunContext *crc = cls;
201 ok = (int) crc->phase;
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
207 GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key);
208 GNUNET_DATASTORE_put (datastore,
214 get_priority (crc->i),
215 get_anonymity (crc->i),
217 get_expiration (crc->i),
220 &check_success, crc);
222 if (crc->i == ITERATIONS)
224 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
225 "Sleeping to give datastore time to clean up\n");
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
235 GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key);
236 GNUNET_DATASTORE_get_key (datastore,
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)",
250 GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key);
251 GNUNET_DATASTORE_get_key (datastore,
263 GNUNET_assert (0 == crc->i);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
265 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
273 run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration,
276 struct CpsRunContext *crc = cls;
278 if (success != GNUNET_YES)
281 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
283 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
287 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
293 const struct GNUNET_CONFIGURATION_Handle *cfg,
294 struct GNUNET_TESTING_Peer *peer)
296 struct CpsRunContext *crc;
297 static struct GNUNET_HashCode zkey;
299 crc = GNUNET_new (struct CpsRunContext);
302 now = GNUNET_TIME_absolute_get ();
303 datastore = GNUNET_DATASTORE_connect (cfg);
305 GNUNET_DATASTORE_put (datastore,
310 GNUNET_BLOCK_TYPE_TEST,
312 GNUNET_TIME_relative_to_absolute (
313 GNUNET_TIME_UNIT_SECONDS),
319 fprintf (stderr, "%s", "Test 'put' operation failed.\n");
327 * Function called when disk utilization changes, does nothing.
330 * @param delta change in utilization
333 ignore_payload_cb (void *cls,
341 * check if plugin is actually working
344 test_plugin (const char *cfg_name)
346 char libname[PATH_MAX];
347 struct GNUNET_CONFIGURATION_Handle *cfg;
348 struct GNUNET_DATASTORE_PluginFunctions *api;
349 struct GNUNET_DATASTORE_PluginEnvironment env;
351 cfg = GNUNET_CONFIGURATION_create ();
353 GNUNET_CONFIGURATION_load (cfg,
356 GNUNET_CONFIGURATION_destroy (cfg);
358 "Failed to load configuration %s\n",
362 memset (&env, 0, sizeof(env));
364 env.duc = &ignore_payload_cb;
365 GNUNET_snprintf (libname,
367 "libgnunet_plugin_datastore_%s",
369 api = GNUNET_PLUGIN_load (libname, &env);
372 GNUNET_CONFIGURATION_destroy (cfg);
374 "Failed to load plugin `%s'\n",
378 GNUNET_PLUGIN_unload (libname, api);
379 GNUNET_CONFIGURATION_destroy (cfg);
385 main (int argc, char *argv[])
387 char cfg_name[PATH_MAX];
390 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
391 GNUNET_snprintf (cfg_name,
393 "test_datastore_api_data_%s.conf",
395 ret = test_plugin (cfg_name);
399 GNUNET_TESTING_peer_run ("test-gnunet-datastore-management",
408 /* end of test_datastore_api_management.c */