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)
192 struct CpsRunContext *crc = cls;
202 GNUNET_assert (matched == GNUNET_YES);
204 GNUNET_SCHEDULER_add_continuation (&run_continuation,
206 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
210 GNUNET_assert (size == get_size (i));
211 GNUNET_assert (0 == memcmp (data, get_data(i), size));
212 GNUNET_assert (type == get_type (i));
213 GNUNET_assert (priority == get_priority (i));
214 GNUNET_assert (anonymity == get_anonymity(i));
215 GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value);
216 matched = GNUNET_YES;
217 GNUNET_DATASTORE_iterate_get_next (datastore);
222 delete_value (void *cls,
223 const GNUNET_HashCode * key,
226 enum GNUNET_BLOCK_Type type,
229 struct GNUNET_TIME_Absolute
230 expiration, uint64_t uid)
232 struct CpsRunContext *crc = cls;
235 if (crc->data == NULL)
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 "Content %u not found!\n",
240 crc->phase = RP_ERROR;
244 crc->phase = RP_DO_DEL;
246 GNUNET_SCHEDULER_add_continuation (&run_continuation,
248 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
251 GNUNET_assert (crc->data == NULL);
254 crc->data = GNUNET_malloc (size);
255 memcpy (crc->data, data, size);
256 GNUNET_DATASTORE_iterate_get_next (datastore);
261 check_nothing (void *cls,
262 const GNUNET_HashCode * key,
265 enum GNUNET_BLOCK_Type type,
268 struct GNUNET_TIME_Absolute
269 expiration, uint64_t uid)
271 struct CpsRunContext *crc = cls;
272 GNUNET_assert (key == NULL);
275 crc->phase = RP_RESERVE;
277 GNUNET_SCHEDULER_add_continuation (&run_continuation,
279 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
284 check_multiple (void *cls,
285 const GNUNET_HashCode * key,
288 enum GNUNET_BLOCK_Type type,
291 struct GNUNET_TIME_Absolute
292 expiration, uint64_t uid)
294 struct CpsRunContext *crc = cls;
298 if (crc->phase != RP_GET_MULTIPLE_DONE)
304 crc->phase = RP_ERROR;
308 crc->phase = RP_UPDATE;
310 GNUNET_SCHEDULER_add_continuation (&run_continuation,
312 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
317 case RP_GET_MULTIPLE:
318 crc->phase = RP_GET_MULTIPLE_NEXT;
320 case RP_GET_MULTIPLE_NEXT:
321 crc->phase = RP_GET_MULTIPLE_DONE;
323 case RP_GET_MULTIPLE_DONE:
324 /* do not advance further */
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Test in phase %u\n", crc->phase);
334 if (priority == get_priority (42))
336 GNUNET_DATASTORE_iterate_get_next (datastore);
341 check_update (void *cls,
342 const GNUNET_HashCode * key,
345 enum GNUNET_BLOCK_Type type,
348 struct GNUNET_TIME_Absolute
349 expiration, uint64_t uid)
351 struct CpsRunContext *crc = cls;
355 if (crc->phase != RP_UPDATE_DONE)
358 crc->phase = RP_ERROR;
362 crc->phase = RP_DONE;
364 GNUNET_SCHEDULER_add_continuation (&run_continuation,
366 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
369 if ( (anonymity == get_anonymity (42)) &&
370 (size == get_size (42)) &&
371 (priority == get_priority (42) + 100) )
373 crc->phase = RP_UPDATE_DONE;
376 GNUNET_assert (size == get_size (43));
377 GNUNET_DATASTORE_iterate_get_next (datastore);
382 run_continuation (void *cls,
383 const struct GNUNET_SCHEDULER_TaskContext *tc)
385 struct CpsRunContext *crc = cls;
386 ok = (int) crc->phase;
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Test in phase %u\n", crc->phase);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Executing `%s' number %u\n",
400 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
401 GNUNET_DATASTORE_put (datastore,
407 get_priority (crc->i),
408 get_anonymity (crc->i),
410 get_expiration (crc->i),
415 if (crc->i == ITERATIONS)
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Executing `%s' number %u\n",
426 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
427 GNUNET_DATASTORE_iterate_key (datastore,
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "Executing `%s' number %u\n",
443 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
444 GNUNET_DATASTORE_iterate_key (datastore,
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 "Executing `%s' number %u\n",
461 crc->phase = RP_DELVALIDATE;
467 GNUNET_DATASTORE_remove (datastore,
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Executing `%s' number %u\n",
483 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
484 GNUNET_DATASTORE_iterate_key (datastore,
492 crc->phase = RP_PUT_MULTIPLE;
493 GNUNET_DATASTORE_reserve (datastore,
500 case RP_PUT_MULTIPLE:
501 crc->phase = RP_PUT_MULTIPLE_NEXT;
502 GNUNET_DATASTORE_put (datastore,
516 case RP_PUT_MULTIPLE_NEXT:
517 crc->phase = RP_GET_MULTIPLE;
518 GNUNET_DATASTORE_put (datastore,
532 case RP_GET_MULTIPLE:
533 GNUNET_DATASTORE_iterate_key (datastore,
540 case RP_GET_MULTIPLE_NEXT:
541 case RP_GET_MULTIPLE_DONE:
545 GNUNET_assert (crc->uid > 0);
546 crc->phase = RP_UPDATE_VALIDATE;
547 GNUNET_DATASTORE_update (datastore,
555 case RP_UPDATE_VALIDATE:
556 GNUNET_DATASTORE_iterate_key (datastore,
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Finished, disconnecting\n");
571 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
576 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
585 run_tests (void *cls,
589 struct CpsRunContext *crc = cls;
594 GNUNET_SCHEDULER_add_continuation (&run_continuation,
596 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
600 "Test 'put' operation failed, key already exists (!?)\n");
605 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
619 const struct GNUNET_CONFIGURATION_Handle *cfg)
621 struct CpsRunContext *crc;
622 static GNUNET_HashCode zkey;
624 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
627 now = GNUNET_TIME_absolute_get ();
628 datastore = GNUNET_DATASTORE_connect (cfg);
630 GNUNET_DATASTORE_put (datastore, 0,
632 GNUNET_BLOCK_TYPE_TEST,
633 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
634 0, 1, GNUNET_TIME_UNIT_MINUTES,
638 "Test 'put' operation failed.\n");
650 struct GNUNET_OS_Process *proc;
652 char *const argv[] = {
653 "test-datastore-api",
661 struct GNUNET_GETOPT_CommandLineOption options[] = {
662 GNUNET_GETOPT_OPTION_END
664 GNUNET_snprintf (cfg_name,
666 "test_datastore_api_data_%s.conf",
669 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
670 "gnunet-service-arm",
674 "-c", cfg_name, NULL);
676 GNUNET_assert (NULL != proc);
677 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
678 argv, "test-datastore-api", "nohelp",
679 options, &run, NULL);
681 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
683 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
686 GNUNET_OS_process_wait (proc);
687 GNUNET_OS_process_close (proc);
691 fprintf (stderr, "Missed some testcases: %u\n", ok);
696 main (int argc, char *argv[])
702 /* determine name of plugin to use */
703 plugin_name = argv[0];
704 while (NULL != (pos = strstr(plugin_name, "_")))
706 if (NULL != (pos = strstr(plugin_name, ".")))
709 pos = (char *) plugin_name;
711 GNUNET_snprintf (dir_name,
713 "/tmp/test-gnunet-datastore-%s",
715 GNUNET_DISK_directory_remove (dir_name);
716 GNUNET_log_setup ("test-datastore-api",
724 if (pos != plugin_name)
726 GNUNET_DISK_directory_remove (dir_name);
730 /* end of test_datastore_api.c */