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,
151 "Operation not successfull: `%s'\n", msg);
152 crc->phase = RP_ERROR;
153 GNUNET_SCHEDULER_add_continuation (&run_continuation,
155 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
158 GNUNET_free_non_null (crc->data);
160 GNUNET_SCHEDULER_add_continuation (&run_continuation,
162 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
167 get_reserved (void *cls,
171 struct CpsRunContext *crc = cls;
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 GNUNET_assert (0 < success);
177 GNUNET_SCHEDULER_add_continuation (&run_continuation,
179 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
184 check_value (void *cls,
185 const GNUNET_HashCode * key,
188 enum GNUNET_BLOCK_Type type,
191 struct GNUNET_TIME_Absolute
192 expiration, uint64_t uid)
195 struct CpsRunContext *crc = cls;
205 GNUNET_assert (matched == GNUNET_YES);
207 GNUNET_SCHEDULER_add_continuation (&run_continuation,
209 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
213 GNUNET_assert (size == get_size (i));
214 GNUNET_assert (0 == memcmp (data, get_data(i), size));
215 GNUNET_assert (type == get_type (i));
216 GNUNET_assert (priority == get_priority (i));
217 GNUNET_assert (anonymity == get_anonymity(i));
218 GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value);
219 matched = GNUNET_YES;
220 GNUNET_DATASTORE_iterate_get_next (datastore);
225 delete_value (void *cls,
226 const GNUNET_HashCode * key,
229 enum GNUNET_BLOCK_Type type,
232 struct GNUNET_TIME_Absolute
233 expiration, uint64_t uid)
235 struct CpsRunContext *crc = cls;
238 if (crc->data == NULL)
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
241 "Content %u not found!\n",
243 crc->phase = RP_ERROR;
247 crc->phase = RP_DO_DEL;
249 GNUNET_SCHEDULER_add_continuation (&run_continuation,
251 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
254 GNUNET_assert (crc->data == NULL);
257 crc->data = GNUNET_malloc (size);
258 memcpy (crc->data, data, size);
259 GNUNET_DATASTORE_iterate_get_next (datastore);
264 check_nothing (void *cls,
265 const GNUNET_HashCode * key,
268 enum GNUNET_BLOCK_Type type,
271 struct GNUNET_TIME_Absolute
272 expiration, uint64_t uid)
274 struct CpsRunContext *crc = cls;
275 GNUNET_assert (key == NULL);
278 crc->phase = RP_RESERVE;
280 GNUNET_SCHEDULER_add_continuation (&run_continuation,
282 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
287 check_multiple (void *cls,
288 const GNUNET_HashCode * key,
291 enum GNUNET_BLOCK_Type type,
294 struct GNUNET_TIME_Absolute
295 expiration, uint64_t uid)
297 struct CpsRunContext *crc = cls;
301 if (crc->phase != RP_GET_MULTIPLE_DONE)
307 crc->phase = RP_ERROR;
311 crc->phase = RP_UPDATE;
313 GNUNET_SCHEDULER_add_continuation (&run_continuation,
315 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
320 case RP_GET_MULTIPLE:
321 crc->phase = RP_GET_MULTIPLE_NEXT;
323 case RP_GET_MULTIPLE_NEXT:
324 crc->phase = RP_GET_MULTIPLE_DONE;
326 case RP_GET_MULTIPLE_DONE:
327 /* do not advance further */
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Test in phase %u\n", crc->phase);
337 if (priority == get_priority (42))
339 GNUNET_DATASTORE_iterate_get_next (datastore);
344 check_update (void *cls,
345 const GNUNET_HashCode * key,
348 enum GNUNET_BLOCK_Type type,
351 struct GNUNET_TIME_Absolute
352 expiration, uint64_t uid)
354 struct CpsRunContext *crc = cls;
358 if (crc->phase != RP_UPDATE_DONE)
361 crc->phase = RP_ERROR;
365 crc->phase = RP_DONE;
367 GNUNET_SCHEDULER_add_continuation (&run_continuation,
369 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
372 if ( (anonymity == get_anonymity (42)) &&
373 (size == get_size (42)) &&
374 (priority == get_priority (42) + 100) )
376 crc->phase = RP_UPDATE_DONE;
379 GNUNET_assert (size == get_size (43));
380 GNUNET_DATASTORE_iterate_get_next (datastore);
385 run_continuation (void *cls,
386 const struct GNUNET_SCHEDULER_TaskContext *tc)
388 struct CpsRunContext *crc = cls;
389 ok = (int) crc->phase;
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Test in phase %u\n", crc->phase);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Executing `%s' number %u\n",
403 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
404 GNUNET_DATASTORE_put (datastore,
410 get_priority (crc->i),
411 get_anonymity (crc->i),
413 get_expiration (crc->i),
418 if (crc->i == ITERATIONS)
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Executing `%s' number %u\n",
429 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
430 GNUNET_DATASTORE_iterate_key (datastore,
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Executing `%s' number %u\n",
446 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
447 GNUNET_DATASTORE_iterate_key (datastore,
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Executing `%s' number %u\n",
464 crc->phase = RP_DELVALIDATE;
470 GNUNET_DATASTORE_remove (datastore,
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Executing `%s' number %u\n",
486 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
487 GNUNET_DATASTORE_iterate_key (datastore,
495 crc->phase = RP_PUT_MULTIPLE;
496 GNUNET_DATASTORE_reserve (datastore,
503 case RP_PUT_MULTIPLE:
504 crc->phase = RP_PUT_MULTIPLE_NEXT;
505 GNUNET_DATASTORE_put (datastore,
519 case RP_PUT_MULTIPLE_NEXT:
520 crc->phase = RP_GET_MULTIPLE;
521 GNUNET_DATASTORE_put (datastore,
535 case RP_GET_MULTIPLE:
536 GNUNET_DATASTORE_iterate_key (datastore,
543 case RP_GET_MULTIPLE_NEXT:
544 case RP_GET_MULTIPLE_DONE:
548 GNUNET_assert (crc->uid > 0);
549 crc->phase = RP_UPDATE_VALIDATE;
550 GNUNET_DATASTORE_update (datastore,
558 case RP_UPDATE_VALIDATE:
559 GNUNET_DATASTORE_iterate_key (datastore,
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572 "Finished, disconnecting\n");
574 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
579 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
588 run_tests (void *cls,
592 struct CpsRunContext *crc = cls;
597 GNUNET_SCHEDULER_add_continuation (&run_continuation,
599 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
603 "Test 'put' operation failed, key already exists (!?)\n");
608 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
622 const struct GNUNET_CONFIGURATION_Handle *cfg)
624 struct CpsRunContext *crc;
625 static GNUNET_HashCode zkey;
627 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
630 now = GNUNET_TIME_absolute_get ();
631 datastore = GNUNET_DATASTORE_connect (cfg);
633 GNUNET_DATASTORE_put (datastore, 0,
635 GNUNET_BLOCK_TYPE_TEST,
636 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
637 0, 1, GNUNET_TIME_UNIT_MINUTES,
641 "Test 'put' operation failed.\n");
653 struct GNUNET_OS_Process *proc;
655 char *const argv[] = {
656 "test-datastore-api",
664 struct GNUNET_GETOPT_CommandLineOption options[] = {
665 GNUNET_GETOPT_OPTION_END
667 GNUNET_snprintf (cfg_name,
669 "test_datastore_api_data_%s.conf",
672 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
673 "gnunet-service-arm",
677 "-c", cfg_name, NULL);
679 GNUNET_assert (NULL != proc);
680 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
681 argv, "test-datastore-api", "nohelp",
682 options, &run, NULL);
684 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
686 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
689 GNUNET_OS_process_wait (proc);
690 GNUNET_OS_process_close (proc);
694 fprintf (stderr, "Missed some testcases: %u\n", ok);
699 main (int argc, char *argv[])
705 /* determine name of plugin to use */
706 plugin_name = argv[0];
707 while (NULL != (pos = strstr(plugin_name, "_")))
709 if (NULL != (pos = strstr(plugin_name, ".")))
712 pos = (char *) plugin_name;
714 GNUNET_snprintf (dir_name,
716 "/tmp/test-gnunet-datastore-%s",
718 GNUNET_DISK_directory_remove (dir_name);
719 GNUNET_log_setup ("test-datastore-api",
727 if (pos != plugin_name)
729 GNUNET_DISK_directory_remove (dir_name);
733 /* end of test_datastore_api.c */