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
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`
173 run_continuation (void *cls);
177 * Continuation called to notify client about result of an
178 * operation. Checks for errors, updates our iteration counters and
179 * continues execution with #run_continuation().
181 * @param cls the `struct CpsRunContext`
182 * @param success #GNUNET_SYSERR on failure
183 * @param min_expiration minimum expiration time required for content to be stored
184 * by the datacache at this time, zero for unknown
185 * @param msg NULL on success, otherwise an error message
188 check_success (void *cls,
190 struct GNUNET_TIME_Absolute min_expiration,
193 struct CpsRunContext *crc = cls;
195 if (GNUNET_OK != success)
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
198 "Operation %d/%d not successful: `%s'\n",
202 crc->phase = RP_ERROR;
204 GNUNET_free_non_null (crc->data);
206 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
211 get_reserved (void *cls,
213 struct GNUNET_TIME_Absolute min_expiration,
216 struct CpsRunContext *crc = cls;
219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220 "Error obtaining reservation: `%s'\n",
222 GNUNET_assert (0 < success);
224 GNUNET_SCHEDULER_add_now (&run_continuation,
230 check_value (void *cls,
231 const struct GNUNET_HashCode *key,
234 enum GNUNET_BLOCK_Type type,
237 struct GNUNET_TIME_Absolute expiration,
240 struct CpsRunContext *crc = cls;
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 "Value check failed (got NULL key) in %d/%d\n",
250 crc->phase = RP_ERROR;
251 GNUNET_SCHEDULER_add_now (&run_continuation,
257 "Check value got `%s' of size %u, type %d, expire %s\n",
258 GNUNET_h2s (key), (unsigned int) size, type,
259 GNUNET_STRINGS_absolute_time_to_string (expiration));
261 "Check value iteration %d wants size %u, type %d, expire %s\n", i,
262 (unsigned int) get_size (i), get_type (i),
263 GNUNET_STRINGS_absolute_time_to_string (get_expiration(i)));
265 GNUNET_assert (size == get_size (i));
266 GNUNET_assert (0 == memcmp (data, get_data (i), size));
267 GNUNET_assert (type == get_type (i));
268 GNUNET_assert (priority == get_priority (i));
269 GNUNET_assert (anonymity == get_anonymity (i));
270 GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
277 GNUNET_SCHEDULER_add_now (&run_continuation,
283 delete_value (void *cls,
284 const struct GNUNET_HashCode *key,
287 enum GNUNET_BLOCK_Type type,
290 struct GNUNET_TIME_Absolute expiration,
293 struct CpsRunContext *crc = cls;
295 GNUNET_assert (NULL == crc->data);
296 GNUNET_assert (NULL != key);
299 crc->data = GNUNET_malloc (size);
300 memcpy (crc->data, data, size);
301 crc->phase = RP_DO_DEL;
302 GNUNET_SCHEDULER_add_now (&run_continuation,
308 check_nothing (void *cls,
309 const struct GNUNET_HashCode *key,
312 enum GNUNET_BLOCK_Type type,
315 struct GNUNET_TIME_Absolute expiration,
318 struct CpsRunContext *crc = cls;
320 GNUNET_assert (key == NULL);
322 crc->phase = RP_RESERVE;
323 GNUNET_SCHEDULER_add_now (&run_continuation,
329 check_multiple (void *cls,
330 const struct GNUNET_HashCode *key,
333 enum GNUNET_BLOCK_Type type,
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_UPDATE;
355 crc->phase = RP_ERROR;
358 if (priority == get_priority (42))
360 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
365 check_update (void *cls,
366 const struct GNUNET_HashCode *key,
369 enum GNUNET_BLOCK_Type type,
372 struct GNUNET_TIME_Absolute expiration,
375 struct CpsRunContext *crc = cls;
377 GNUNET_assert (key != NULL);
378 if ((anonymity == get_anonymity (42)) && (size == get_size (42)) &&
379 (priority == get_priority (42) + 100))
380 crc->phase = RP_DONE;
383 GNUNET_assert (size == get_size (43));
386 GNUNET_SCHEDULER_add_now (&run_continuation, crc);
391 * Main state machine. Executes the next step of the test
392 * depending on the current state.
394 * @param cls the `struct CpsRunContext`
397 run_continuation (void *cls)
399 struct CpsRunContext *crc = cls;
401 ok = (int) crc->phase;
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Test in phase %u\n",
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Executing PUT number %u\n",
411 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
412 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
413 get_data (crc->i), get_type (crc->i),
414 get_priority (crc->i), get_anonymity (crc->i), 0,
415 get_expiration (crc->i), 1, 1, TIMEOUT,
416 &check_success, crc);
418 if (crc->i == ITERATIONS)
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Executing GET number %u\n",
426 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
427 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
428 get_type (crc->i), 1, 1, TIMEOUT,
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Executing DEL number %u\n",
437 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
438 GNUNET_assert (NULL !=
439 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
440 get_type (crc->i), 1, 1, TIMEOUT,
441 &delete_value, crc));
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Executing DO_DEL number %u\n",
450 crc->phase = RP_DELVALIDATE;
456 GNUNET_assert (NULL !=
457 GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
458 crc->data, 1, 1, TIMEOUT,
459 &check_success, crc));
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Executing DELVALIDATE number %u\n",
466 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
467 GNUNET_assert (NULL !=
468 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
469 get_type (crc->i), 1, 1, TIMEOUT,
470 &check_nothing, crc));
473 crc->phase = RP_PUT_MULTIPLE;
474 GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2,
477 case RP_PUT_MULTIPLE:
478 crc->phase = RP_PUT_MULTIPLE_NEXT;
479 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
480 get_data (42), get_type (42), get_priority (42),
481 get_anonymity (42), 0, get_expiration (42), 1, 1,
482 TIMEOUT, &check_success, crc);
484 case RP_PUT_MULTIPLE_NEXT:
485 crc->phase = RP_GET_MULTIPLE;
486 GNUNET_DATASTORE_put (datastore, crc->rid,
497 &check_success, crc);
499 case RP_GET_MULTIPLE:
500 GNUNET_assert (NULL !=
501 GNUNET_DATASTORE_get_key (datastore,
506 &check_multiple, crc));
508 case RP_GET_MULTIPLE_NEXT:
509 GNUNET_assert (NULL !=
510 GNUNET_DATASTORE_get_key (datastore,
516 &check_multiple, crc));
519 GNUNET_assert (crc->uid > 0);
520 crc->phase = RP_UPDATE_VALIDATE;
521 GNUNET_DATASTORE_update (datastore,
523 get_expiration (42), 1,
525 &check_success, crc);
527 case RP_UPDATE_VALIDATE:
528 GNUNET_assert (NULL !=
529 GNUNET_DATASTORE_get_key (datastore,
535 &check_update, crc));
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Finished, disconnecting\n");
540 GNUNET_DATASTORE_disconnect (datastore,
546 GNUNET_DATASTORE_disconnect (datastore,
556 * Function called with the result of the initial PUT operation. If
557 * the PUT succeeded, we start the actual benchmark loop, otherwise we
558 * bail out with an error.
562 * @param success #GNUNET_SYSERR on failure
563 * @param min_expiration minimum expiration time required for content to be stored
564 * by the datacache at this time, zero for unknown
565 * @param msg NULL on success, otherwise an error message
568 run_tests (void *cls,
570 struct GNUNET_TIME_Absolute min_expiration,
573 struct CpsRunContext *crc = cls;
578 GNUNET_SCHEDULER_add_now (&run_continuation,
583 "%s", "Test 'put' operation failed, key already exists (!?)\n");
584 GNUNET_DATASTORE_disconnect (datastore,
590 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
592 GNUNET_DATASTORE_disconnect (datastore,
603 * Beginning of the actual execution of the benchmark.
604 * Performs a first test operation (PUT) to verify that
605 * the plugin works at all.
608 * @param cfg configuration to use
609 * @param peer peer handle (unused)
613 const struct GNUNET_CONFIGURATION_Handle *cfg,
614 struct GNUNET_TESTING_Peer *peer)
616 struct CpsRunContext *crc;
617 static struct GNUNET_HashCode zkey;
619 crc = GNUNET_new (struct CpsRunContext);
621 now = GNUNET_TIME_absolute_get ();
622 datastore = GNUNET_DATASTORE_connect (cfg);
624 GNUNET_DATASTORE_put (datastore,
629 GNUNET_BLOCK_TYPE_TEST,
631 GNUNET_TIME_relative_to_absolute
632 (GNUNET_TIME_UNIT_SECONDS),
639 "Test 'put' operation failed.\n");
647 * Entry point into the test. Determines which configuration / plugin
648 * we are running with based on the name of the binary and starts
651 * @param argc should be 1
652 * @param argv used to determine plugin / configuration name.
653 * @return 0 on success
661 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
662 GNUNET_snprintf (cfg_name,
664 "test_datastore_api_data_%s.conf",
667 GNUNET_TESTING_peer_run ("test-gnunet-datastore",
675 /* end of test_datastore_api.c */