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_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;
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;
113 struct GNUNET_SCHEDULER_Handle *sched;
114 struct GNUNET_CONFIGURATION_Handle *cfg;
120 run_continuation (void *cls,
121 const struct GNUNET_SCHEDULER_TaskContext *tc);
125 check_success (void *cls,
129 struct CpsRunContext *crc = cls;
130 if (GNUNET_OK != success)
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 GNUNET_assert (GNUNET_OK == success);
134 GNUNET_SCHEDULER_add_continuation (crc->sched,
138 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
143 check_failure (void *cls,
147 struct CpsRunContext *crc = cls;
148 GNUNET_assert (GNUNET_OK != success);
149 GNUNET_assert (NULL != msg);
150 GNUNET_SCHEDULER_add_continuation (crc->sched,
154 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
159 check_value (void *cls,
160 const GNUNET_HashCode * key,
166 struct GNUNET_TIME_Absolute
167 expiration, uint64_t uid)
169 struct CpsRunContext *crc = cls;
175 GNUNET_assert (size == get_size (i));
176 GNUNET_assert (0 == memcmp (data, get_data(i), size));
177 GNUNET_assert (type == get_type (i));
178 GNUNET_assert (priority == get_priority (i));
179 GNUNET_assert (anonymity == get_anonymity(i));
180 GNUNET_assert (expiration.value == get_expiration(i).value);
181 GNUNET_SCHEDULER_add_continuation (crc->sched,
185 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
190 delete_value (void *cls,
191 const GNUNET_HashCode * key,
197 struct GNUNET_TIME_Absolute
198 expiration, uint64_t uid)
200 struct CpsRunContext *crc = cls;
203 GNUNET_DATASTORE_remove (datastore,
211 GNUNET_DATASTORE_remove (datastore,
218 GNUNET_SCHEDULER_add_continuation (crc->sched,
222 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
228 check_nothing (void *cls,
229 const GNUNET_HashCode * key,
235 struct GNUNET_TIME_Absolute
236 expiration, uint64_t uid)
238 struct CpsRunContext *crc = cls;
239 GNUNET_assert (key == NULL);
240 GNUNET_SCHEDULER_add_continuation (crc->sched,
244 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
249 run_continuation (void *cls,
250 const struct GNUNET_SCHEDULER_TaskContext *tc)
252 struct CpsRunContext *crc = cls;
253 ok = (int) crc->phase;
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Executing `%s' number %u\n",
263 memset (&crc->key, ITERATIONS - crc->i, sizeof (GNUNET_HashCode));
264 GNUNET_DATASTORE_put (datastore,
270 get_priority (crc->i),
271 get_anonymity (crc->i),
272 get_expiration (crc->i),
277 if (crc->i == ITERATIONS)
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Executing `%s' number %u\n",
288 memset (&crc->key, ITERATIONS - crc->i, sizeof (GNUNET_HashCode));
289 GNUNET_DATASTORE_get (datastore,
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "Executing `%s' number %u\n",
309 memset (&crc->key, ITERATIONS - crc->i, sizeof (GNUNET_HashCode));
310 GNUNET_DATASTORE_get (datastore,
318 crc->phase = RP_DELVALIDATE;
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Executing `%s' number %u\n",
330 memset (&crc->key, ITERATIONS - crc->i, sizeof (GNUNET_HashCode));
331 GNUNET_DATASTORE_get (datastore,
339 crc->phase = RP_DONE;
342 /* check reservations */
344 /* test multiple results */
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Finished, disconnecting\n");
350 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
358 struct GNUNET_SCHEDULER_Handle *sched,
360 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
362 struct CpsRunContext *crc;
364 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
369 datastore = GNUNET_DATASTORE_connect (cfg, sched);
370 GNUNET_SCHEDULER_add_continuation (crc->sched,
374 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
384 char *const argv[] = { "test-datastore-api",
386 "test_datastore_api_data.conf",
392 struct GNUNET_GETOPT_CommandLineOption options[] = {
393 GNUNET_GETOPT_OPTION_END
395 pid = GNUNET_OS_start_process ("gnunet-service-datastore",
396 "gnunet-service-datastore",
400 "-c", "test_datastore_api_data.conf", NULL);
402 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
403 argv, "test-datastore-api", "nohelp",
404 options, &run, NULL);
405 if (0 != PLIBC_KILL (pid, SIGTERM))
407 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
410 GNUNET_OS_process_wait(pid);
412 fprintf (stderr, "Missed some testcases: %u\n", ok);
417 main (int argc, char *argv[])
421 GNUNET_log_setup ("test-datastore-api",
435 /* end of test_datastore_api.c */