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);
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);
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);
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);
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),
406 get_expiration (crc->i),
411 if (crc->i == ITERATIONS)
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Executing `%s' number %u\n",
422 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
423 GNUNET_DATASTORE_get (datastore,
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Executing `%s' number %u\n",
439 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
440 GNUNET_DATASTORE_get (datastore,
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "Executing `%s' number %u\n",
457 crc->phase = RP_DELVALIDATE;
463 GNUNET_DATASTORE_remove (datastore,
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Executing `%s' number %u\n",
479 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
480 GNUNET_DATASTORE_get (datastore,
488 crc->phase = RP_PUT_MULTIPLE;
489 GNUNET_DATASTORE_reserve (datastore,
496 case RP_PUT_MULTIPLE:
497 crc->phase = RP_PUT_MULTIPLE_NEXT;
498 GNUNET_DATASTORE_put (datastore,
512 case RP_PUT_MULTIPLE_NEXT:
513 crc->phase = RP_GET_MULTIPLE;
514 GNUNET_DATASTORE_put (datastore,
528 case RP_GET_MULTIPLE:
529 GNUNET_DATASTORE_get (datastore,
536 case RP_GET_MULTIPLE_NEXT:
537 case RP_GET_MULTIPLE_DONE:
541 GNUNET_assert (crc->uid > 0);
542 crc->phase = RP_UPDATE_VALIDATE;
543 GNUNET_DATASTORE_update (datastore,
551 case RP_UPDATE_VALIDATE:
552 GNUNET_DATASTORE_get (datastore,
564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
565 "Finished, disconnecting\n");
567 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
572 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
581 run_tests (void *cls,
585 struct CpsRunContext *crc = cls;
587 if (success != GNUNET_YES)
590 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
595 GNUNET_SCHEDULER_add_continuation (&run_continuation,
597 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
605 const struct GNUNET_CONFIGURATION_Handle *cfg)
607 struct CpsRunContext *crc;
608 static GNUNET_HashCode zkey;
610 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
613 now = GNUNET_TIME_absolute_get ();
614 datastore = GNUNET_DATASTORE_connect (cfg);
616 GNUNET_DATASTORE_put (datastore, 0,
618 GNUNET_BLOCK_TYPE_TEST,
619 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
620 0, 1, GNUNET_TIME_UNIT_MINUTES,
624 "Test 'put' operation failed.\n");
636 struct GNUNET_OS_Process *proc;
638 char *const argv[] = {
639 "test-datastore-api",
647 struct GNUNET_GETOPT_CommandLineOption options[] = {
648 GNUNET_GETOPT_OPTION_END
650 GNUNET_snprintf (cfg_name,
652 "test_datastore_api_data_%s.conf",
655 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
656 "gnunet-service-arm",
660 "-c", cfg_name, NULL);
662 GNUNET_assert (NULL != proc);
663 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
664 argv, "test-datastore-api", "nohelp",
665 options, &run, NULL);
667 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
669 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
672 GNUNET_OS_process_wait (proc);
673 GNUNET_OS_process_close (proc);
677 fprintf (stderr, "Missed some testcases: %u\n", ok);
682 main (int argc, char *argv[])
688 /* determine name of plugin to use */
689 plugin_name = argv[0];
690 while (NULL != (pos = strstr(plugin_name, "_")))
692 if (NULL != (pos = strstr(plugin_name, ".")))
695 pos = (char *) plugin_name;
697 GNUNET_snprintf (dir_name,
699 "/tmp/test-gnunet-datastore-%s",
701 GNUNET_DISK_directory_remove (dir_name);
702 GNUNET_log_setup ("test-datastore-api",
710 if (pos != plugin_name)
712 GNUNET_DISK_directory_remove (dir_name);
716 /* end of test_datastore_api.c */