2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2015 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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_testing_lib.h"
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
41 #define ITERATIONS 256
44 * Handle to the datastore.
46 static struct GNUNET_DATASTORE_Handle *datastore;
48 static struct GNUNET_TIME_Absolute now;
51 * Value we return from #main().
56 * Name of plugin under test.
58 static const char *plugin_name;
71 static char buf[60000];
73 memset (buf, i, 8 * i);
99 static struct GNUNET_TIME_Absolute
100 get_expiration (int i)
102 struct GNUNET_TIME_Absolute av;
104 av.abs_value_us = now.abs_value_us + 20000000000LL - i * 1000 * 1000LL;
110 * Which phase of the process are we in?
115 * We are done (shutting down normally).
120 * We are adding new entries to the datastore.
129 RP_PUT_MULTIPLE_NEXT = 8,
131 RP_GET_MULTIPLE_NEXT = 10,
133 RP_UPDATE_VALIDATE = 12,
136 * Execution failed with some kind of error.
143 * Closure we give to all of the functions executing the
144 * benchmark. Could right now be global, but this allows
145 * us to theoretically run multiple clients "in parallel".
150 * Execution phase we are in.
154 struct GNUNET_HashCode key;
167 * Main state machine. Executes the next step of the test
168 * depending on the current state.
170 * @param cls the `struct CpsRunContext`
171 * @param tc scheduler context (unused)
174 run_continuation (void *cls,
175 const struct GNUNET_SCHEDULER_TaskContext *tc);
179 * Continuation called to notify client about result of an
180 * operation. Checks for errors, updates our iteration counters and
181 * continues execution with #run_continuation().
183 * @param cls the `struct CpsRunContext`
184 * @param success #GNUNET_SYSERR on failure
185 * @param min_expiration minimum expiration time required for content to be stored
186 * by the datacache at this time, zero for unknown
187 * @param msg NULL on success, otherwise an error message
190 check_success (void *cls,
192 struct GNUNET_TIME_Absolute min_expiration,
195 struct CpsRunContext *crc = cls;
197 if (GNUNET_OK != success)
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200 "Operation %d/%d not successfull: `%s'\n",
204 crc->phase = RP_ERROR;
206 GNUNET_free_non_null (crc->data);
208 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
213 get_reserved (void *cls,
215 struct GNUNET_TIME_Absolute min_expiration,
218 struct CpsRunContext *crc = cls;
221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
222 "Error obtaining reservation: `%s'\n",
224 GNUNET_assert (0 < success);
226 GNUNET_SCHEDULER_add_now (&run_continuation,
232 check_value (void *cls,
233 const struct GNUNET_HashCode *key,
236 enum GNUNET_BLOCK_Type type,
239 struct GNUNET_TIME_Absolute expiration,
242 struct CpsRunContext *crc = cls;
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 "Value check failed (got NULL key) in %d/%d\n",
252 crc->phase = RP_ERROR;
253 GNUNET_SCHEDULER_add_now (&run_continuation,
259 "Check value got `%s' of size %u, type %d, expire %s\n",
260 GNUNET_h2s (key), (unsigned int) size, type,
261 GNUNET_STRINGS_absolute_time_to_string (expiration));
263 "Check value iteration %d wants size %u, type %d, expire %s\n", i,
264 (unsigned int) get_size (i), get_type (i),
265 GNUNET_STRINGS_absolute_time_to_string (get_expiration(i)));
267 GNUNET_assert (size == get_size (i));
268 GNUNET_assert (0 == memcmp (data, get_data (i), size));
269 GNUNET_assert (type == get_type (i));
270 GNUNET_assert (priority == get_priority (i));
271 GNUNET_assert (anonymity == get_anonymity (i));
272 GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
279 GNUNET_SCHEDULER_add_now (&run_continuation,
285 delete_value (void *cls,
286 const struct GNUNET_HashCode *key,
289 enum GNUNET_BLOCK_Type type,
292 struct GNUNET_TIME_Absolute expiration,
295 struct CpsRunContext *crc = cls;
297 GNUNET_assert (NULL == crc->data);
298 GNUNET_assert (NULL != key);
301 crc->data = GNUNET_malloc (size);
302 memcpy (crc->data, data, size);
303 crc->phase = RP_DO_DEL;
304 GNUNET_SCHEDULER_add_now (&run_continuation,
310 check_nothing (void *cls,
311 const struct GNUNET_HashCode *key,
314 enum GNUNET_BLOCK_Type type,
317 struct GNUNET_TIME_Absolute expiration,
320 struct CpsRunContext *crc = cls;
322 GNUNET_assert (key == NULL);
324 crc->phase = RP_RESERVE;
325 GNUNET_SCHEDULER_add_now (&run_continuation,
331 check_multiple (void *cls,
332 const struct GNUNET_HashCode *key,
335 enum GNUNET_BLOCK_Type type,
338 struct GNUNET_TIME_Absolute expiration,
341 struct CpsRunContext *crc = cls;
343 GNUNET_assert (key != NULL);
346 case RP_GET_MULTIPLE:
347 crc->phase = RP_GET_MULTIPLE_NEXT;
348 crc->first_uid = uid;
351 case RP_GET_MULTIPLE_NEXT:
352 GNUNET_assert (uid != crc->first_uid);
353 crc->phase = RP_UPDATE;
357 crc->phase = RP_ERROR;
360 if (priority == get_priority (42))
362 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
367 check_update (void *cls,
368 const struct GNUNET_HashCode *key,
371 enum GNUNET_BLOCK_Type type,
374 struct GNUNET_TIME_Absolute expiration,
377 struct CpsRunContext *crc = cls;
379 GNUNET_assert (key != NULL);
380 if ((anonymity == get_anonymity (42)) && (size == get_size (42)) &&
381 (priority == get_priority (42) + 100))
382 crc->phase = RP_DONE;
385 GNUNET_assert (size == get_size (43));
388 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
393 * Main state machine. Executes the next step of the test
394 * depending on the current state.
396 * @param cls the `struct CpsRunContext`
397 * @param tc scheduler context (unused)
400 run_continuation (void *cls,
401 const struct GNUNET_SCHEDULER_TaskContext *tc)
403 struct CpsRunContext *crc = cls;
405 ok = (int) crc->phase;
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Test in phase %u\n",
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Executing `%s' number %u\n",
416 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
417 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
418 get_data (crc->i), get_type (crc->i),
419 get_priority (crc->i), get_anonymity (crc->i), 0,
420 get_expiration (crc->i), 1, 1, TIMEOUT,
421 &check_success, crc);
423 if (crc->i == ITERATIONS)
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Executing `%s' number %u\n",
432 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
433 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
434 get_type (crc->i), 1, 1, TIMEOUT, &check_value,
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Executing `%s' number %u\n",
444 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
445 GNUNET_assert (NULL !=
446 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
447 get_type (crc->i), 1, 1, TIMEOUT,
448 &delete_value, crc));
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Executing `%s' number %u\n",
458 crc->phase = RP_DELVALIDATE;
464 GNUNET_assert (NULL !=
465 GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
466 crc->data, 1, 1, TIMEOUT,
467 &check_success, crc));
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Executing `%s' number %u\n",
473 "DEL-VALIDATE", crc->i);
474 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
475 GNUNET_assert (NULL !=
476 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
477 get_type (crc->i), 1, 1, TIMEOUT,
478 &check_nothing, crc));
481 crc->phase = RP_PUT_MULTIPLE;
482 GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2,
485 case RP_PUT_MULTIPLE:
486 crc->phase = RP_PUT_MULTIPLE_NEXT;
487 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
488 get_data (42), get_type (42), get_priority (42),
489 get_anonymity (42), 0, get_expiration (42), 1, 1,
490 TIMEOUT, &check_success, crc);
492 case RP_PUT_MULTIPLE_NEXT:
493 crc->phase = RP_GET_MULTIPLE;
494 GNUNET_DATASTORE_put (datastore, crc->rid,
505 &check_success, crc);
507 case RP_GET_MULTIPLE:
508 GNUNET_assert (NULL !=
509 GNUNET_DATASTORE_get_key (datastore,
514 &check_multiple, crc));
516 case RP_GET_MULTIPLE_NEXT:
517 GNUNET_assert (NULL !=
518 GNUNET_DATASTORE_get_key (datastore,
524 &check_multiple, crc));
527 GNUNET_assert (crc->uid > 0);
528 crc->phase = RP_UPDATE_VALIDATE;
529 GNUNET_DATASTORE_update (datastore,
531 get_expiration (42), 1,
533 &check_success, crc);
535 case RP_UPDATE_VALIDATE:
536 GNUNET_assert (NULL !=
537 GNUNET_DATASTORE_get_key (datastore,
543 &check_update, crc));
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Finished, disconnecting\n");
548 GNUNET_DATASTORE_disconnect (datastore,
554 GNUNET_DATASTORE_disconnect (datastore,
564 * Function called with the result of the initial PUT operation. If
565 * the PUT succeeded, we start the actual benchmark loop, otherwise we
566 * bail out with an error.
570 * @param success #GNUNET_SYSERR on failure
571 * @param min_expiration minimum expiration time required for content to be stored
572 * by the datacache at this time, zero for unknown
573 * @param msg NULL on success, otherwise an error message
576 run_tests (void *cls,
578 struct GNUNET_TIME_Absolute min_expiration,
581 struct CpsRunContext *crc = cls;
586 GNUNET_SCHEDULER_add_now (&run_continuation,
591 "%s", "Test 'put' operation failed, key already exists (!?)\n");
592 GNUNET_DATASTORE_disconnect (datastore,
598 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
600 GNUNET_DATASTORE_disconnect (datastore,
611 * Beginning of the actual execution of the benchmark.
612 * Performs a first test operation (PUT) to verify that
613 * the plugin works at all.
616 * @param cfg configuration to use
617 * @param peer peer handle (unused)
621 const struct GNUNET_CONFIGURATION_Handle *cfg,
622 struct GNUNET_TESTING_Peer *peer)
624 struct CpsRunContext *crc;
625 static struct GNUNET_HashCode zkey;
627 crc = GNUNET_new (struct CpsRunContext);
629 now = GNUNET_TIME_absolute_get ();
630 datastore = GNUNET_DATASTORE_connect (cfg);
632 GNUNET_DATASTORE_put (datastore,
637 GNUNET_BLOCK_TYPE_TEST,
639 GNUNET_TIME_relative_to_absolute
640 (GNUNET_TIME_UNIT_SECONDS),
647 "Test 'put' operation failed.\n");
655 * Entry point into the test. Determines which configuration / plugin
656 * we are running with based on the name of the binary and starts
659 * @param argc should be 1
660 * @param argv used to determine plugin / configuration name.
661 * @return 0 on success
669 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
670 GNUNET_snprintf (cfg_name,
672 "test_datastore_api_data_%s.conf",
675 GNUNET_TESTING_peer_run ("test-gnunet-datastore",
683 /* end of test_datastore_api.c */