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"
34 #define VERBOSE GNUNET_NO
36 #define START_DATASTORE GNUNET_YES
39 * How long until we give up on transmitting the message?
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
43 #define ITERATIONS 256
45 static struct GNUNET_DATASTORE_Handle *datastore;
47 static struct GNUNET_TIME_Absolute now;
52 * Name of plugin under test.
54 static const char *plugin_name;
66 static char buf[60000];
67 memset (buf, i, 8 * i);
93 static struct GNUNET_TIME_Absolute
94 get_expiration (int i)
96 struct GNUNET_TIME_Absolute av;
98 av.abs_value = now.abs_value + 20000000 - i * 1000;
112 RP_PUT_MULTIPLE_NEXT,
114 RP_GET_MULTIPLE_NEXT, /* 10 */
115 RP_GET_MULTIPLE_DONE,
117 RP_UPDATE_VALIDATE, /* 13 */
128 const struct GNUNET_CONFIGURATION_Handle *cfg;
132 unsigned long long uid;
137 run_continuation (void *cls,
138 const struct GNUNET_SCHEDULER_TaskContext *tc);
142 check_success (void *cls,
146 struct CpsRunContext *crc = cls;
147 if (GNUNET_OK != success)
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
152 GNUNET_SCHEDULER_shutdown ();
155 GNUNET_free_non_null (crc->data);
157 GNUNET_SCHEDULER_add_continuation (&run_continuation,
159 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
164 get_reserved (void *cls,
168 struct CpsRunContext *crc = cls;
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 GNUNET_assert (0 < success);
174 GNUNET_SCHEDULER_add_continuation (&run_continuation,
176 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
181 check_value (void *cls,
182 const GNUNET_HashCode * key,
185 enum GNUNET_BLOCK_Type type,
188 struct GNUNET_TIME_Absolute
189 expiration, uint64_t uid)
191 struct CpsRunContext *crc = cls;
201 GNUNET_SCHEDULER_add_continuation (&run_continuation,
203 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
207 GNUNET_assert (size == get_size (i));
208 GNUNET_assert (0 == memcmp (data, get_data(i), size));
209 GNUNET_assert (type == get_type (i));
210 GNUNET_assert (priority == get_priority (i));
211 GNUNET_assert (anonymity == get_anonymity(i));
212 GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value);
213 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
218 delete_value (void *cls,
219 const GNUNET_HashCode * key,
222 enum GNUNET_BLOCK_Type type,
225 struct GNUNET_TIME_Absolute
226 expiration, uint64_t uid)
228 struct CpsRunContext *crc = cls;
231 if (crc->data == NULL)
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234 "Content %u not found!\n",
236 crc->phase = RP_ERROR;
240 crc->phase = RP_DO_DEL;
242 GNUNET_SCHEDULER_add_continuation (&run_continuation,
244 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
247 GNUNET_assert (crc->data == NULL);
250 crc->data = GNUNET_malloc (size);
251 memcpy (crc->data, data, size);
252 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
257 check_nothing (void *cls,
258 const GNUNET_HashCode * key,
261 enum GNUNET_BLOCK_Type type,
264 struct GNUNET_TIME_Absolute
265 expiration, uint64_t uid)
267 struct CpsRunContext *crc = cls;
268 GNUNET_assert (key == NULL);
271 crc->phase = RP_RESERVE;
273 GNUNET_SCHEDULER_add_continuation (&run_continuation,
275 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
280 check_multiple (void *cls,
281 const GNUNET_HashCode * key,
284 enum GNUNET_BLOCK_Type type,
287 struct GNUNET_TIME_Absolute
288 expiration, uint64_t uid)
290 struct CpsRunContext *crc = cls;
294 if (crc->phase != RP_GET_MULTIPLE_DONE)
300 crc->phase = RP_ERROR;
304 crc->phase = RP_UPDATE;
306 GNUNET_SCHEDULER_add_continuation (&run_continuation,
308 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
313 case RP_GET_MULTIPLE:
314 crc->phase = RP_GET_MULTIPLE_NEXT;
316 case RP_GET_MULTIPLE_NEXT:
317 crc->phase = RP_GET_MULTIPLE_DONE;
319 case RP_GET_MULTIPLE_DONE:
320 /* do not advance further */
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "Test in phase %u\n", crc->phase);
330 if (priority == get_priority (42))
332 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
337 check_update (void *cls,
338 const GNUNET_HashCode * key,
341 enum GNUNET_BLOCK_Type type,
344 struct GNUNET_TIME_Absolute
345 expiration, uint64_t uid)
347 struct CpsRunContext *crc = cls;
351 if (crc->phase != RP_UPDATE_DONE)
354 crc->phase = RP_ERROR;
358 crc->phase = RP_DONE;
360 GNUNET_SCHEDULER_add_continuation (&run_continuation,
362 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
365 if ( (anonymity == get_anonymity (42)) &&
366 (size == get_size (42)) &&
367 (priority == get_priority (42) + 100) )
369 crc->phase = RP_UPDATE_DONE;
372 GNUNET_assert (size == get_size (43));
373 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
378 run_continuation (void *cls,
379 const struct GNUNET_SCHEDULER_TaskContext *tc)
381 struct CpsRunContext *crc = cls;
382 ok = (int) crc->phase;
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Test in phase %u\n", crc->phase);
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Executing `%s' number %u\n",
396 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
397 GNUNET_DATASTORE_put (datastore,
403 get_priority (crc->i),
404 get_anonymity (crc->i),
405 get_expiration (crc->i),
410 if (crc->i == ITERATIONS)
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Executing `%s' number %u\n",
421 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
422 GNUNET_DATASTORE_get (datastore,
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Executing `%s' number %u\n",
438 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
439 GNUNET_DATASTORE_get (datastore,
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Executing `%s' number %u\n",
456 crc->phase = RP_DELVALIDATE;
462 GNUNET_DATASTORE_remove (datastore,
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Executing `%s' number %u\n",
478 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
479 GNUNET_DATASTORE_get (datastore,
487 crc->phase = RP_PUT_MULTIPLE;
488 GNUNET_DATASTORE_reserve (datastore,
495 case RP_PUT_MULTIPLE:
496 crc->phase = RP_PUT_MULTIPLE_NEXT;
497 GNUNET_DATASTORE_put (datastore,
510 case RP_PUT_MULTIPLE_NEXT:
511 crc->phase = RP_GET_MULTIPLE;
512 GNUNET_DATASTORE_put (datastore,
525 case RP_GET_MULTIPLE:
526 GNUNET_DATASTORE_get (datastore,
533 case RP_GET_MULTIPLE_NEXT:
534 case RP_GET_MULTIPLE_DONE:
538 GNUNET_assert (crc->uid > 0);
539 crc->phase = RP_UPDATE_VALIDATE;
540 GNUNET_DATASTORE_update (datastore,
548 case RP_UPDATE_VALIDATE:
549 GNUNET_DATASTORE_get (datastore,
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "Finished, disconnecting\n");
564 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
569 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
578 run_tests (void *cls,
582 struct CpsRunContext *crc = cls;
584 if (success != GNUNET_YES)
587 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
592 GNUNET_SCHEDULER_add_continuation (&run_continuation,
594 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
602 const struct GNUNET_CONFIGURATION_Handle *cfg)
604 struct CpsRunContext *crc;
605 static GNUNET_HashCode zkey;
607 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
610 now = GNUNET_TIME_absolute_get ();
611 datastore = GNUNET_DATASTORE_connect (cfg);
613 GNUNET_DATASTORE_put (datastore, 0,
615 GNUNET_BLOCK_TYPE_TEST,
616 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
617 0, 1, GNUNET_TIME_UNIT_MINUTES,
621 "Test 'put' operation failed.\n");
633 struct GNUNET_OS_Process *proc;
635 char *const argv[] = {
636 "test-datastore-api",
644 struct GNUNET_GETOPT_CommandLineOption options[] = {
645 GNUNET_GETOPT_OPTION_END
647 GNUNET_snprintf (cfg_name,
649 "test_datastore_api_data_%s.conf",
652 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
653 "gnunet-service-arm",
657 "-c", cfg_name, NULL);
659 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
660 argv, "test-datastore-api", "nohelp",
661 options, &run, NULL);
663 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
665 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
668 GNUNET_OS_process_wait (proc);
669 GNUNET_OS_process_close (proc);
673 fprintf (stderr, "Missed some testcases: %u\n", ok);
678 main (int argc, char *argv[])
684 /* determine name of plugin to use */
685 plugin_name = argv[0];
686 while (NULL != (pos = strstr(plugin_name, "_")))
689 GNUNET_snprintf (dir_name,
691 "/tmp/test-gnunet-datastore-%s",
693 GNUNET_DISK_directory_remove (dir_name);
694 GNUNET_log_setup ("test-datastore-api",
702 GNUNET_DISK_directory_remove (dir_name);
706 /* end of test_datastore_api.c */