2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2007, 2009 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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
43 static struct GNUNET_DATASTORE_Handle *datastore;
45 static struct GNUNET_TIME_Absolute now;
50 * Name of plugin under test.
52 static const char *plugin_name;
64 static char buf[60000];
66 memset (buf, i, 8 * i);
92 static struct GNUNET_TIME_Absolute
93 get_expiration (int i)
95 struct GNUNET_TIME_Absolute av;
97 av.abs_value_us = now.abs_value_us + 20000000000LL - i * 1000 * 1000LL;
111 RP_PUT_MULTIPLE_NEXT = 8,
113 RP_GET_MULTIPLE_NEXT = 10,
115 RP_UPDATE_VALIDATE = 12,
122 struct GNUNET_HashCode key;
125 const struct GNUNET_CONFIGURATION_Handle *cfg;
136 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
140 check_success (void *cls,
142 struct GNUNET_TIME_Absolute min_expiration,
145 struct CpsRunContext *crc = cls;
147 if (GNUNET_OK != success)
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
150 "Operation %d/%d not successfull: `%s'\n", crc->phase, crc->i,
152 crc->phase = RP_ERROR;
154 GNUNET_free_non_null (crc->data);
156 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
157 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
162 get_reserved (void *cls,
164 struct GNUNET_TIME_Absolute min_expiration,
167 struct CpsRunContext *crc = cls;
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171 "Error obtaining reservation: `%s'\n",
173 GNUNET_assert (0 < success);
175 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
176 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
181 check_value (void *cls,
182 const struct GNUNET_HashCode *key,
185 enum GNUNET_BLOCK_Type type,
188 struct GNUNET_TIME_Absolute expiration,
191 struct CpsRunContext *crc = cls;
197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
198 "Value check failed (got NULL key) in %d/%d\n",
201 crc->phase = RP_ERROR;
202 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
203 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
208 "Check value got `%s' of size %u, type %d, expire %s\n",
209 GNUNET_h2s (key), (unsigned int) size, type,
210 GNUNET_STRINGS_absolute_time_to_string (expiration));
212 "Check value iteration %d wants size %u, type %d, expire %s\n", i,
213 (unsigned int) get_size (i), get_type (i),
214 GNUNET_STRINGS_absolute_time_to_string (get_expiration(i)));
216 GNUNET_assert (size == get_size (i));
217 GNUNET_assert (0 == memcmp (data, get_data (i), size));
218 GNUNET_assert (type == get_type (i));
219 GNUNET_assert (priority == get_priority (i));
220 GNUNET_assert (anonymity == get_anonymity (i));
221 GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
228 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
229 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
234 delete_value (void *cls,
235 const struct GNUNET_HashCode *key,
238 enum GNUNET_BLOCK_Type type,
241 struct GNUNET_TIME_Absolute expiration,
244 struct CpsRunContext *crc = cls;
246 GNUNET_assert (NULL == crc->data);
247 GNUNET_assert (NULL != key);
250 crc->data = GNUNET_malloc (size);
251 memcpy (crc->data, data, size);
252 crc->phase = RP_DO_DEL;
253 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
254 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
259 check_nothing (void *cls, const struct GNUNET_HashCode * key, size_t size,
260 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
261 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
264 struct CpsRunContext *crc = cls;
266 GNUNET_assert (key == NULL);
268 crc->phase = RP_RESERVE;
269 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
270 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
275 check_multiple (void *cls,
276 const struct GNUNET_HashCode *key,
279 enum GNUNET_BLOCK_Type type,
282 struct GNUNET_TIME_Absolute expiration,
285 struct CpsRunContext *crc = cls;
287 GNUNET_assert (key != NULL);
290 case RP_GET_MULTIPLE:
291 crc->phase = RP_GET_MULTIPLE_NEXT;
292 crc->first_uid = uid;
295 case RP_GET_MULTIPLE_NEXT:
296 GNUNET_assert (uid != crc->first_uid);
297 crc->phase = RP_UPDATE;
301 crc->phase = RP_ERROR;
304 if (priority == get_priority (42))
306 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
307 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
312 check_update (void *cls,
313 const struct GNUNET_HashCode *key,
316 enum GNUNET_BLOCK_Type type,
319 struct GNUNET_TIME_Absolute expiration,
322 struct CpsRunContext *crc = cls;
324 GNUNET_assert (key != NULL);
325 if ((anonymity == get_anonymity (42)) && (size == get_size (42)) &&
326 (priority == get_priority (42) + 100))
327 crc->phase = RP_DONE;
330 GNUNET_assert (size == get_size (43));
333 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
334 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
339 run_continuation (void *cls,
340 const struct GNUNET_SCHEDULER_TaskContext *tc)
342 struct CpsRunContext *crc = cls;
344 ok = (int) crc->phase;
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Test in phase %u\n",
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Executing `%s' number %u\n",
355 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
356 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
357 get_data (crc->i), get_type (crc->i),
358 get_priority (crc->i), get_anonymity (crc->i), 0,
359 get_expiration (crc->i), 1, 1, TIMEOUT,
360 &check_success, crc);
362 if (crc->i == ITERATIONS)
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Executing `%s' number %u\n",
371 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
372 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
373 get_type (crc->i), 1, 1, TIMEOUT, &check_value,
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 "Executing `%s' number %u\n",
383 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
384 GNUNET_assert (NULL !=
385 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
386 get_type (crc->i), 1, 1, TIMEOUT,
387 &delete_value, crc));
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Executing `%s' number %u\n",
397 crc->phase = RP_DELVALIDATE;
403 GNUNET_assert (NULL !=
404 GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
405 crc->data, 1, 1, TIMEOUT,
406 &check_success, crc));
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Executing `%s' number %u\n",
412 "DEL-VALIDATE", crc->i);
413 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
414 GNUNET_assert (NULL !=
415 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
416 get_type (crc->i), 1, 1, TIMEOUT,
417 &check_nothing, crc));
420 crc->phase = RP_PUT_MULTIPLE;
421 GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2, 1, 1, TIMEOUT,
424 case RP_PUT_MULTIPLE:
425 crc->phase = RP_PUT_MULTIPLE_NEXT;
426 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
427 get_data (42), get_type (42), get_priority (42),
428 get_anonymity (42), 0, get_expiration (42), 1, 1,
429 TIMEOUT, &check_success, crc);
431 case RP_PUT_MULTIPLE_NEXT:
432 crc->phase = RP_GET_MULTIPLE;
433 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (43),
434 get_data (43), get_type (42), get_priority (43),
435 get_anonymity (43), 0, get_expiration (43), 1, 1,
436 TIMEOUT, &check_success, crc);
438 case RP_GET_MULTIPLE:
439 GNUNET_assert (NULL !=
440 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
441 get_type (42), 1, 1, TIMEOUT,
442 &check_multiple, crc));
444 case RP_GET_MULTIPLE_NEXT:
445 GNUNET_assert (NULL !=
446 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
447 get_type (42), 1, 1, TIMEOUT,
448 &check_multiple, crc));
451 GNUNET_assert (crc->uid > 0);
452 crc->phase = RP_UPDATE_VALIDATE;
453 GNUNET_DATASTORE_update (datastore, crc->uid, 100, get_expiration (42), 1,
454 1, TIMEOUT, &check_success, crc);
456 case RP_UPDATE_VALIDATE:
457 GNUNET_assert (NULL !=
458 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
459 get_type (42), 1, 1, TIMEOUT,
460 &check_update, crc));
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
464 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
469 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
478 run_tests (void *cls,
480 struct GNUNET_TIME_Absolute min_expiration,
483 struct CpsRunContext *crc = cls;
488 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
489 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
493 "%s", "Test 'put' operation failed, key already exists (!?)\n");
494 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
499 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
501 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
512 const struct GNUNET_CONFIGURATION_Handle *cfg,
513 struct GNUNET_TESTING_Peer *peer)
515 struct CpsRunContext *crc;
516 static struct GNUNET_HashCode zkey;
518 crc = GNUNET_new (struct CpsRunContext);
521 now = GNUNET_TIME_absolute_get ();
522 datastore = GNUNET_DATASTORE_connect (cfg);
524 GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST",
525 GNUNET_BLOCK_TYPE_TEST, 0, 0, 0,
526 GNUNET_TIME_relative_to_absolute
527 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
528 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
530 FPRINTF (stderr, "%s", "Test 'put' operation failed.\n");
538 main (int argc, char *argv[])
542 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
543 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
544 "test_datastore_api_data_%s.conf", plugin_name);
546 GNUNET_TESTING_peer_run ("test-gnunet-datastore",
554 /* end of test_datastore_api.c */