2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2015 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.c
22 * @brief Test for the basic datastore API.
23 * @author Christian Grothoff
26 * - test reservation failure
30 #include "gnunet_util_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_datastore_service.h"
33 #include "gnunet_datastore_plugin.h"
34 #include "gnunet_testing_lib.h"
38 * How long until we give up on transmitting the message?
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
42 #define ITERATIONS 256
45 * Handle to the datastore.
47 static struct GNUNET_DATASTORE_Handle *datastore;
49 static struct GNUNET_TIME_Absolute now;
52 * Value we return from #main().
57 * Name of plugin under test.
59 static const char *plugin_name;
72 static char buf[60000];
74 memset (buf, i, 8 * i);
100 static struct GNUNET_TIME_Absolute
101 get_expiration (int i)
103 struct GNUNET_TIME_Absolute av;
105 av.abs_value_us = now.abs_value_us + 20000000000LL - i * 1000 * 1000LL;
111 * Which phase of the process are we in?
116 * We are done (shutting down normally).
121 * We are adding new entries to the datastore.
130 RP_PUT_MULTIPLE_NEXT = 8,
132 RP_GET_MULTIPLE_NEXT = 10,
135 * Execution failed with some kind of error.
142 * Closure we give to all of the functions executing the
143 * benchmark. Could right now be global, but this allows
144 * us to theoretically run multiple clients "in parallel".
149 * Execution phase we are in.
153 struct GNUNET_HashCode key;
164 * Main state machine. Executes the next step of the test
165 * depending on the current state.
167 * @param cls the `struct CpsRunContext`
170 run_continuation (void *cls);
174 * Continuation called to notify client about result of an
175 * operation. Checks for errors, updates our iteration counters and
176 * continues execution with #run_continuation().
178 * @param cls the `struct CpsRunContext`
179 * @param success #GNUNET_SYSERR on failure
180 * @param min_expiration minimum expiration time required for content to be stored
181 * by the datacache at this time, zero for unknown
182 * @param msg NULL on success, otherwise an error message
185 check_success (void *cls,
187 struct GNUNET_TIME_Absolute min_expiration,
190 struct CpsRunContext *crc = cls;
192 if (GNUNET_OK != success)
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195 "Operation %d/%d not successful: `%s'\n",
199 crc->phase = RP_ERROR;
201 GNUNET_free_non_null (crc->data);
203 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
208 get_reserved (void *cls,
210 struct GNUNET_TIME_Absolute min_expiration,
213 struct CpsRunContext *crc = cls;
216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217 "Error obtaining reservation: `%s'\n",
219 GNUNET_assert (0 < success);
221 GNUNET_SCHEDULER_add_now (&run_continuation,
227 check_value (void *cls,
228 const struct GNUNET_HashCode *key,
231 enum GNUNET_BLOCK_Type type,
234 uint32_t replication,
235 struct GNUNET_TIME_Absolute expiration,
238 struct CpsRunContext *crc = cls;
244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
245 "Value check failed (got NULL key) in %d/%d\n",
248 crc->phase = RP_ERROR;
249 GNUNET_SCHEDULER_add_now (&run_continuation,
255 "Check value got `%s' of size %u, type %d, expire %s\n",
256 GNUNET_h2s (key), (unsigned int) size, type,
257 GNUNET_STRINGS_absolute_time_to_string (expiration));
259 "Check value iteration %d wants size %u, type %d, expire %s\n", i,
260 (unsigned int) get_size (i), get_type (i),
261 GNUNET_STRINGS_absolute_time_to_string (get_expiration (i)));
263 GNUNET_assert (size == get_size (i));
264 GNUNET_assert (0 == memcmp (data, get_data (i), size));
265 GNUNET_assert (type == get_type (i));
266 GNUNET_assert (priority == get_priority (i));
267 GNUNET_assert (anonymity == get_anonymity (i));
268 GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
274 GNUNET_SCHEDULER_add_now (&run_continuation,
280 delete_value (void *cls,
281 const struct GNUNET_HashCode *key,
284 enum GNUNET_BLOCK_Type type,
287 uint32_t replication,
288 struct GNUNET_TIME_Absolute expiration,
291 struct CpsRunContext *crc = cls;
293 GNUNET_assert (NULL == crc->data);
294 GNUNET_assert (NULL != key);
297 crc->data = GNUNET_malloc (size);
298 GNUNET_memcpy (crc->data, data, size);
299 crc->phase = RP_DO_DEL;
300 GNUNET_SCHEDULER_add_now (&run_continuation,
306 check_nothing (void *cls,
307 const struct GNUNET_HashCode *key,
310 enum GNUNET_BLOCK_Type type,
313 uint32_t replication,
314 struct GNUNET_TIME_Absolute expiration,
317 struct CpsRunContext *crc = cls;
319 GNUNET_assert (key == NULL);
321 crc->phase = RP_RESERVE;
322 GNUNET_SCHEDULER_add_now (&run_continuation,
328 check_multiple (void *cls,
329 const struct GNUNET_HashCode *key,
332 enum GNUNET_BLOCK_Type type,
335 uint32_t replication,
336 struct GNUNET_TIME_Absolute expiration,
339 struct CpsRunContext *crc = cls;
341 GNUNET_assert (key != NULL);
344 case RP_GET_MULTIPLE:
345 crc->phase = RP_GET_MULTIPLE_NEXT;
346 crc->first_uid = uid;
349 case RP_GET_MULTIPLE_NEXT:
350 GNUNET_assert (uid != crc->first_uid);
351 crc->phase = RP_DONE;
356 crc->phase = RP_ERROR;
359 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
364 * Main state machine. Executes the next step of the test
365 * depending on the current state.
367 * @param cls the `struct CpsRunContext`
370 run_continuation (void *cls)
372 struct CpsRunContext *crc = cls;
374 ok = (int) crc->phase;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Test in phase %u\n",
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Executing PUT number %u\n",
384 GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key);
385 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
386 get_data (crc->i), get_type (crc->i),
387 get_priority (crc->i), get_anonymity (crc->i), 0,
388 get_expiration (crc->i), 1, 1,
389 &check_success, crc);
391 if (crc->i == ITERATIONS)
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Executing GET number %u\n",
400 GNUNET_CRYPTO_hash (&crc->i,
403 GNUNET_DATASTORE_get_key (datastore,
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Executing DEL number %u\n",
420 GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key);
421 GNUNET_assert (NULL !=
422 GNUNET_DATASTORE_get_key (datastore,
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Executing DO_DEL number %u\n",
440 crc->phase = RP_DELVALIDATE;
446 GNUNET_assert (NULL !=
447 GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
449 &check_success, crc));
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Executing DELVALIDATE number %u\n",
457 GNUNET_CRYPTO_hash (&crc->i, sizeof(int), &crc->key);
458 GNUNET_assert (NULL !=
459 GNUNET_DATASTORE_get_key (datastore,
471 crc->phase = RP_PUT_MULTIPLE;
472 GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2,
476 case RP_PUT_MULTIPLE:
477 crc->phase = RP_PUT_MULTIPLE_NEXT;
478 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
479 get_data (42), get_type (42), get_priority (42),
480 get_anonymity (42), 0, get_expiration (42), 1, 1,
481 &check_success, crc);
484 case RP_PUT_MULTIPLE_NEXT:
485 crc->phase = RP_GET_MULTIPLE;
486 GNUNET_DATASTORE_put (datastore, crc->rid,
496 &check_success, crc);
499 case RP_GET_MULTIPLE:
500 GNUNET_assert (NULL !=
501 GNUNET_DATASTORE_get_key (datastore,
512 case RP_GET_MULTIPLE_NEXT:
513 GNUNET_assert (NULL !=
514 GNUNET_DATASTORE_get_key (datastore,
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Finished, disconnecting\n");
528 GNUNET_DATASTORE_disconnect (datastore,
535 GNUNET_DATASTORE_disconnect (datastore,
545 * Function called with the result of the initial PUT operation. If
546 * the PUT succeeded, we start the actual benchmark loop, otherwise we
547 * bail out with an error.
551 * @param success #GNUNET_SYSERR on failure
552 * @param min_expiration minimum expiration time required for content to be stored
553 * by the datacache at this time, zero for unknown
554 * @param msg NULL on success, otherwise an error message
557 run_tests (void *cls,
559 struct GNUNET_TIME_Absolute min_expiration,
562 struct CpsRunContext *crc = cls;
567 GNUNET_SCHEDULER_add_now (&run_continuation,
573 "%s", "Test 'put' operation failed, key already exists (!?)\n");
574 GNUNET_DATASTORE_disconnect (datastore,
581 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
583 GNUNET_DATASTORE_disconnect (datastore,
595 * Beginning of the actual execution of the benchmark.
596 * Performs a first test operation (PUT) to verify that
597 * the plugin works at all.
600 * @param cfg configuration to use
601 * @param peer peer handle (unused)
605 const struct GNUNET_CONFIGURATION_Handle *cfg,
606 struct GNUNET_TESTING_Peer *peer)
608 struct CpsRunContext *crc;
609 static struct GNUNET_HashCode zkey;
611 crc = GNUNET_new (struct CpsRunContext);
613 now = GNUNET_TIME_absolute_get ();
614 datastore = GNUNET_DATASTORE_connect (cfg);
616 GNUNET_DATASTORE_put (datastore,
621 GNUNET_BLOCK_TYPE_TEST,
623 GNUNET_TIME_relative_to_absolute
624 (GNUNET_TIME_UNIT_SECONDS),
630 "Test 'put' operation failed.\n");
638 * Function invoked to notify service of disk utilization
642 * @param delta change in disk utilization,
643 * 0 for "reset to empty"
646 duc_dummy (void *cls,
649 /* intentionally empty */
654 * check if plugin is actually working
657 test_plugin (const char *cfg_name)
660 struct GNUNET_CONFIGURATION_Handle *cfg;
661 struct GNUNET_DATASTORE_PluginFunctions *api;
662 struct GNUNET_DATASTORE_PluginEnvironment env;
664 cfg = GNUNET_CONFIGURATION_create ();
666 GNUNET_CONFIGURATION_load (cfg,
669 GNUNET_CONFIGURATION_destroy (cfg);
671 "Failed to load configuration %s\n",
675 memset (&env, 0, sizeof(env));
677 env.duc = &duc_dummy;
678 GNUNET_snprintf (libname,
680 "libgnunet_plugin_datastore_%s",
682 api = GNUNET_PLUGIN_load (libname, &env);
685 GNUNET_CONFIGURATION_destroy (cfg);
687 "Failed to load plugin `%s'\n",
691 GNUNET_PLUGIN_unload (libname, api);
692 GNUNET_CONFIGURATION_destroy (cfg);
698 * Entry point into the test. Determines which configuration / plugin
699 * we are running with based on the name of the binary and starts
702 * @param argc should be 1
703 * @param argv used to determine plugin / configuration name.
704 * @return 0 on success
710 char cfg_name[PATH_MAX];
713 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
714 GNUNET_snprintf (cfg_name,
716 "test_datastore_api_data_%s.conf",
718 ret = test_plugin (cfg_name);
721 /* run actual test */
723 GNUNET_TESTING_peer_run ("test-gnunet-datastore",
732 /* end of test_datastore_api.c */