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 = 8,
114 RP_GET_MULTIPLE_NEXT = 10,
116 RP_UPDATE_VALIDATE = 12,
126 const struct GNUNET_CONFIGURATION_Handle *cfg;
137 run_continuation (void *cls,
138 const struct GNUNET_SCHEDULER_TaskContext *tc);
142 check_success (void *cls,
146 struct CpsRunContext *crc = cls;
148 if (GNUNET_OK != success)
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 "Operation %d/%d not successfull: `%s'\n",
155 crc->phase = RP_ERROR;
157 GNUNET_free_non_null (crc->data);
159 GNUNET_SCHEDULER_add_continuation (&run_continuation,
161 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
166 get_reserved (void *cls,
170 struct CpsRunContext *crc = cls;
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173 "Error obtaining reservation: `%s'\n",
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 expiration,
194 struct CpsRunContext *crc = cls;
200 "Check value got `%s' of size %u, type %d, expire %llu\n",
204 (unsigned long long) expiration.abs_value);
206 "Check value iteration %d wants size %u, type %d, expire %llu\n",
208 (unsigned int) get_size (i),
210 (unsigned long long) get_expiration(i).abs_value);
212 GNUNET_assert (size == get_size (i));
213 GNUNET_assert (0 == memcmp (data, get_data(i), size));
214 GNUNET_assert (type == get_type (i));
215 GNUNET_assert (priority == get_priority (i));
216 GNUNET_assert (anonymity == get_anonymity(i));
217 GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value);
224 GNUNET_SCHEDULER_add_continuation (&run_continuation,
226 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
231 delete_value (void *cls,
232 const GNUNET_HashCode *key,
235 enum GNUNET_BLOCK_Type type,
238 struct GNUNET_TIME_Absolute
239 expiration, uint64_t uid)
241 struct CpsRunContext *crc = cls;
243 GNUNET_assert (crc->data == NULL);
244 GNUNET_assert (NULL != key);
247 crc->data = GNUNET_malloc (size);
248 memcpy (crc->data, data, size);
249 crc->phase = RP_DO_DEL;
250 GNUNET_SCHEDULER_add_continuation (&run_continuation,
252 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
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;
269 GNUNET_assert (key == NULL);
271 crc->phase = RP_RESERVE;
272 GNUNET_SCHEDULER_add_continuation (&run_continuation,
274 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
279 check_multiple (void *cls,
280 const GNUNET_HashCode * key,
283 enum GNUNET_BLOCK_Type type,
286 struct GNUNET_TIME_Absolute
287 expiration, uint64_t uid)
289 struct CpsRunContext *crc = cls;
291 GNUNET_assert (key != NULL);
294 case RP_GET_MULTIPLE:
295 crc->phase = RP_GET_MULTIPLE_NEXT;
296 crc->first_uid = uid;
299 case RP_GET_MULTIPLE_NEXT:
300 GNUNET_assert (uid != crc->first_uid);
301 crc->phase = RP_UPDATE;
305 crc->phase = RP_ERROR;
308 if (priority == get_priority (42))
310 GNUNET_SCHEDULER_add_continuation (&run_continuation,
312 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
317 check_update (void *cls,
318 const GNUNET_HashCode * key,
321 enum GNUNET_BLOCK_Type type,
324 struct GNUNET_TIME_Absolute
325 expiration, uint64_t uid)
327 struct CpsRunContext *crc = cls;
329 GNUNET_assert (key != NULL);
330 if ( (anonymity == get_anonymity (42)) &&
331 (size == get_size (42)) &&
332 (priority == get_priority (42) + 100) )
333 crc->phase = RP_DONE;
336 GNUNET_assert (size == get_size (43));
339 GNUNET_SCHEDULER_add_continuation (&run_continuation,
341 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
346 run_continuation (void *cls,
347 const struct GNUNET_SCHEDULER_TaskContext *tc)
349 struct CpsRunContext *crc = cls;
350 ok = (int) crc->phase;
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Test in phase %u\n", crc->phase);
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Executing `%s' number %u\n",
364 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
365 GNUNET_DATASTORE_put (datastore,
371 get_priority (crc->i),
372 get_anonymity (crc->i),
374 get_expiration (crc->i),
379 if (crc->i == ITERATIONS)
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Executing `%s' number %u\n",
390 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
391 GNUNET_DATASTORE_get_key (datastore,
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Executing `%s' number %u\n",
408 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
409 GNUNET_assert (NULL !=
410 GNUNET_DATASTORE_get_key (datastore,
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Executing `%s' number %u\n",
428 crc->phase = RP_DELVALIDATE;
434 GNUNET_assert (NULL !=
435 GNUNET_DATASTORE_remove (datastore,
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Executing `%s' number %u\n",
451 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
452 GNUNET_assert (NULL !=
453 GNUNET_DATASTORE_get_key (datastore,
462 crc->phase = RP_PUT_MULTIPLE;
463 GNUNET_DATASTORE_reserve (datastore,
470 case RP_PUT_MULTIPLE:
471 crc->phase = RP_PUT_MULTIPLE_NEXT;
472 GNUNET_DATASTORE_put (datastore,
486 case RP_PUT_MULTIPLE_NEXT:
487 crc->phase = RP_GET_MULTIPLE;
488 GNUNET_DATASTORE_put (datastore,
502 case RP_GET_MULTIPLE:
503 GNUNET_assert (NULL !=
504 GNUNET_DATASTORE_get_key (datastore,
512 case RP_GET_MULTIPLE_NEXT:
513 GNUNET_assert (NULL !=
514 GNUNET_DATASTORE_get_key (datastore,
523 GNUNET_assert (crc->uid > 0);
524 crc->phase = RP_UPDATE_VALIDATE;
525 GNUNET_DATASTORE_update (datastore,
533 case RP_UPDATE_VALIDATE:
534 GNUNET_assert (NULL !=
535 GNUNET_DATASTORE_get_key (datastore,
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Finished, disconnecting\n");
548 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
553 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
562 run_tests (void *cls,
566 struct CpsRunContext *crc = cls;
571 GNUNET_SCHEDULER_add_continuation (&run_continuation,
573 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
577 "Test 'put' operation failed, key already exists (!?)\n");
582 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
596 const struct GNUNET_CONFIGURATION_Handle *cfg)
598 struct CpsRunContext *crc;
599 static GNUNET_HashCode zkey;
601 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
604 now = GNUNET_TIME_absolute_get ();
605 datastore = GNUNET_DATASTORE_connect (cfg);
607 GNUNET_DATASTORE_put (datastore, 0,
609 GNUNET_BLOCK_TYPE_TEST,
610 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
611 0, 1, GNUNET_TIME_UNIT_MINUTES,
615 "Test 'put' operation failed.\n");
627 struct GNUNET_OS_Process *proc;
629 char *const argv[] = {
630 "test-datastore-api",
638 struct GNUNET_GETOPT_CommandLineOption options[] = {
639 GNUNET_GETOPT_OPTION_END
641 GNUNET_snprintf (cfg_name,
643 "test_datastore_api_data_%s.conf",
646 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
647 "gnunet-service-arm",
651 "-c", cfg_name, NULL);
653 GNUNET_assert (NULL != proc);
654 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
655 argv, "test-datastore-api", "nohelp",
656 options, &run, NULL);
658 sleep (1); /* give datastore chance to receive 'DROP' request */
659 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
661 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
664 GNUNET_OS_process_wait (proc);
665 GNUNET_OS_process_close (proc);
669 fprintf (stderr, "Missed some testcases: %u\n", ok);
674 main (int argc, char *argv[])
680 /* determine name of plugin to use */
681 plugin_name = argv[0];
682 while (NULL != (pos = strstr(plugin_name, "_")))
684 if (NULL != (pos = strstr(plugin_name, ".")))
687 pos = (char *) 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 if (pos != plugin_name)
704 GNUNET_DISK_directory_remove (dir_name);
708 /* end of test_datastore_api.c */