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 2, 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 datastore implementation.
23 * @author Christian Grothoff
26 * - test multiple values under same key
28 * - test storage reservations
32 #include "gnunet_util_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_datastore_service.h"
36 #define VERBOSE GNUNET_NO
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;
62 static char buf[60000];
63 memset (buf, i, 8 * i);
89 static struct GNUNET_TIME_Absolute
90 get_expiration (int i)
92 struct GNUNET_TIME_Absolute av;
94 av.value = now.value - i * 1000;
114 struct GNUNET_SCHEDULER_Handle *sched;
115 struct GNUNET_CONFIGURATION_Handle *cfg;
123 run_continuation (void *cls,
124 const struct GNUNET_SCHEDULER_TaskContext *tc);
128 check_success (void *cls,
132 struct CpsRunContext *crc = cls;
133 if (GNUNET_OK != success)
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
136 GNUNET_assert (GNUNET_OK == success);
137 GNUNET_free_non_null (crc->data);
139 GNUNET_SCHEDULER_add_continuation (crc->sched,
143 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
148 check_value (void *cls,
149 const GNUNET_HashCode * key,
155 struct GNUNET_TIME_Absolute
156 expiration, uint64_t uid)
158 struct CpsRunContext *crc = cls;
168 GNUNET_SCHEDULER_add_continuation (crc->sched,
172 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
176 GNUNET_assert (size == get_size (i));
177 GNUNET_assert (0 == memcmp (data, get_data(i), size));
178 GNUNET_assert (type == get_type (i));
179 GNUNET_assert (priority == get_priority (i));
180 GNUNET_assert (anonymity == get_anonymity(i));
181 GNUNET_assert (expiration.value == get_expiration(i).value);
186 delete_value (void *cls,
187 const GNUNET_HashCode * key,
193 struct GNUNET_TIME_Absolute
194 expiration, uint64_t uid)
196 struct CpsRunContext *crc = cls;
199 crc->phase = RP_DO_DEL;
200 GNUNET_SCHEDULER_add_continuation (crc->sched,
204 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
207 GNUNET_assert (crc->data == NULL);
210 crc->data = GNUNET_malloc (size);
211 memcpy (crc->data, data, size);
216 check_nothing (void *cls,
217 const GNUNET_HashCode * key,
223 struct GNUNET_TIME_Absolute
224 expiration, uint64_t uid)
226 struct CpsRunContext *crc = cls;
227 GNUNET_assert (key == NULL);
230 crc->phase = RP_DONE;
232 GNUNET_SCHEDULER_add_continuation (crc->sched,
236 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
241 run_continuation (void *cls,
242 const struct GNUNET_SCHEDULER_TaskContext *tc)
244 struct CpsRunContext *crc = cls;
245 ok = (int) crc->phase;
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Executing `%s' number %u\n",
255 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
256 GNUNET_DATASTORE_put (datastore,
262 get_priority (crc->i),
263 get_anonymity (crc->i),
264 get_expiration (crc->i),
269 if (crc->i == ITERATIONS)
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Executing `%s' number %u\n",
280 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
281 GNUNET_DATASTORE_get (datastore,
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Executing `%s' number %u\n",
296 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
297 GNUNET_DATASTORE_get (datastore,
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Executing `%s' number %u\n",
314 crc->phase = RP_DELVALIDATE;
320 GNUNET_DATASTORE_remove (datastore,
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Executing `%s' number %u\n",
336 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
337 GNUNET_DATASTORE_get (datastore,
344 /* check reservations */
346 /* test multiple results */
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Finished, disconnecting\n");
352 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
360 struct GNUNET_SCHEDULER_Handle *sched,
362 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
364 struct CpsRunContext *crc;
366 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
371 datastore = GNUNET_DATASTORE_connect (cfg, sched);
372 GNUNET_SCHEDULER_add_continuation (crc->sched,
376 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
386 char *const argv[] = { "test-datastore-api",
388 "test_datastore_api_data.conf",
394 struct GNUNET_GETOPT_CommandLineOption options[] = {
395 GNUNET_GETOPT_OPTION_END
397 pid = GNUNET_OS_start_process ("gnunet-service-datastore",
398 "gnunet-service-datastore",
402 "-c", "test_datastore_api_data.conf", NULL);
404 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
405 argv, "test-datastore-api", "nohelp",
406 options, &run, NULL);
407 if (0 != PLIBC_KILL (pid, SIGTERM))
409 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
412 GNUNET_OS_process_wait(pid);
414 fprintf (stderr, "Missed some testcases: %u\n", ok);
419 main (int argc, char *argv[])
423 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
424 GNUNET_log_setup ("test-datastore-api",
438 /* end of test_datastore_api.c */