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;
348 case RP_GET_MULTIPLE_NEXT:
349 GNUNET_assert (uid != crc->first_uid);
350 crc->phase = RP_DONE;
354 crc->phase = RP_ERROR;
357 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
362 * Main state machine. Executes the next step of the test
363 * depending on the current state.
365 * @param cls the `struct CpsRunContext`
368 run_continuation (void *cls)
370 struct CpsRunContext *crc = cls;
372 ok = (int) crc->phase;
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "Test in phase %u\n",
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Executing PUT number %u\n",
382 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
383 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
384 get_data (crc->i), get_type (crc->i),
385 get_priority (crc->i), get_anonymity (crc->i), 0,
386 get_expiration (crc->i), 1, 1,
387 &check_success, crc);
389 if (crc->i == ITERATIONS)
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Executing GET number %u\n",
397 GNUNET_CRYPTO_hash (&crc->i,
400 GNUNET_DATASTORE_get_key (datastore,
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Executing DEL number %u\n",
416 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
417 GNUNET_assert (NULL !=
418 GNUNET_DATASTORE_get_key (datastore,
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "Executing DO_DEL number %u\n",
435 crc->phase = RP_DELVALIDATE;
441 GNUNET_assert (NULL !=
442 GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
444 &check_success, crc));
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Executing DELVALIDATE number %u\n",
451 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
452 GNUNET_assert (NULL !=
453 GNUNET_DATASTORE_get_key (datastore,
464 crc->phase = RP_PUT_MULTIPLE;
465 GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2,
468 case RP_PUT_MULTIPLE:
469 crc->phase = RP_PUT_MULTIPLE_NEXT;
470 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
471 get_data (42), get_type (42), get_priority (42),
472 get_anonymity (42), 0, get_expiration (42), 1, 1,
473 &check_success, crc);
475 case RP_PUT_MULTIPLE_NEXT:
476 crc->phase = RP_GET_MULTIPLE;
477 GNUNET_DATASTORE_put (datastore, crc->rid,
487 &check_success, crc);
489 case RP_GET_MULTIPLE:
490 GNUNET_assert (NULL !=
491 GNUNET_DATASTORE_get_key (datastore,
501 case RP_GET_MULTIPLE_NEXT:
502 GNUNET_assert (NULL !=
503 GNUNET_DATASTORE_get_key (datastore,
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Finished, disconnecting\n");
516 GNUNET_DATASTORE_disconnect (datastore,
522 GNUNET_DATASTORE_disconnect (datastore,
532 * Function called with the result of the initial PUT operation. If
533 * the PUT succeeded, we start the actual benchmark loop, otherwise we
534 * bail out with an error.
538 * @param success #GNUNET_SYSERR on failure
539 * @param min_expiration minimum expiration time required for content to be stored
540 * by the datacache at this time, zero for unknown
541 * @param msg NULL on success, otherwise an error message
544 run_tests (void *cls,
546 struct GNUNET_TIME_Absolute min_expiration,
549 struct CpsRunContext *crc = cls;
554 GNUNET_SCHEDULER_add_now (&run_continuation,
559 "%s", "Test 'put' operation failed, key already exists (!?)\n");
560 GNUNET_DATASTORE_disconnect (datastore,
566 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
568 GNUNET_DATASTORE_disconnect (datastore,
579 * Beginning of the actual execution of the benchmark.
580 * Performs a first test operation (PUT) to verify that
581 * the plugin works at all.
584 * @param cfg configuration to use
585 * @param peer peer handle (unused)
589 const struct GNUNET_CONFIGURATION_Handle *cfg,
590 struct GNUNET_TESTING_Peer *peer)
592 struct CpsRunContext *crc;
593 static struct GNUNET_HashCode zkey;
595 crc = GNUNET_new (struct CpsRunContext);
597 now = GNUNET_TIME_absolute_get ();
598 datastore = GNUNET_DATASTORE_connect (cfg);
600 GNUNET_DATASTORE_put (datastore,
605 GNUNET_BLOCK_TYPE_TEST,
607 GNUNET_TIME_relative_to_absolute
608 (GNUNET_TIME_UNIT_SECONDS),
614 "Test 'put' operation failed.\n");
622 * Function invoked to notify service of disk utilization
626 * @param delta change in disk utilization,
627 * 0 for "reset to empty"
630 duc_dummy (void *cls,
633 /* intentionally empty */
638 * check if plugin is actually working
641 test_plugin (const char *cfg_name)
644 struct GNUNET_CONFIGURATION_Handle *cfg;
645 struct GNUNET_DATASTORE_PluginFunctions *api;
646 struct GNUNET_DATASTORE_PluginEnvironment env;
648 cfg = GNUNET_CONFIGURATION_create ();
650 GNUNET_CONFIGURATION_load (cfg,
653 GNUNET_CONFIGURATION_destroy (cfg);
655 "Failed to load configuration %s\n",
659 memset (&env, 0, sizeof (env));
661 env.duc = &duc_dummy;
662 GNUNET_snprintf (libname,
664 "libgnunet_plugin_datastore_%s",
666 api = GNUNET_PLUGIN_load (libname, &env);
669 GNUNET_CONFIGURATION_destroy (cfg);
671 "Failed to load plugin `%s'\n",
675 GNUNET_PLUGIN_unload (libname, api);
676 GNUNET_CONFIGURATION_destroy (cfg);
682 * Entry point into the test. Determines which configuration / plugin
683 * we are running with based on the name of the binary and starts
686 * @param argc should be 1
687 * @param argv used to determine plugin / configuration name.
688 * @return 0 on success
694 char cfg_name[PATH_MAX];
697 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
698 GNUNET_snprintf (cfg_name,
700 "test_datastore_api_data_%s.conf",
702 ret = test_plugin (cfg_name);
705 /* run actual test */
707 GNUNET_TESTING_peer_run ("test-gnunet-datastore",
715 /* end of test_datastore_api.c */