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"
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
41 static struct GNUNET_DATASTORE_Handle *datastore;
43 static struct GNUNET_TIME_Absolute now;
58 static char buf[60000];
59 memset (buf, i, 8 * i);
85 static struct GNUNET_TIME_Absolute
86 get_expiration (int i)
88 struct GNUNET_TIME_Absolute av;
90 av.value = now.value - i * 1000;
109 struct GNUNET_SCHEDULER_Handle *sched;
110 struct GNUNET_CONFIGURATION_Handle *cfg;
116 run_continuation (void *cls,
117 const struct GNUNET_SCHEDULER_TaskContext *tc);
121 check_success (void *cls,
125 struct CpsRunContext *crc = cls;
126 if (GNUNET_OK != success)
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 GNUNET_assert (GNUNET_OK == success);
130 GNUNET_SCHEDULER_add_continuation (crc->sched,
134 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
139 check_failure (void *cls,
143 struct CpsRunContext *crc = cls;
144 GNUNET_assert (GNUNET_OK != success);
145 GNUNET_assert (NULL != msg);
146 GNUNET_SCHEDULER_add_continuation (crc->sched,
150 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
155 check_value (void *cls,
156 const GNUNET_HashCode * key,
162 struct GNUNET_TIME_Absolute
163 expiration, uint64_t uid)
165 struct CpsRunContext *crc = cls;
171 GNUNET_assert (size == get_size (i));
172 GNUNET_assert (0 == memcmp (data, get_data(i), size));
173 GNUNET_assert (type == get_type (i));
174 GNUNET_assert (priority == get_priority (i));
175 GNUNET_assert (anonymity == get_anonymity(i));
176 GNUNET_assert (expiration.value == get_expiration(i).value);
177 GNUNET_SCHEDULER_add_continuation (crc->sched,
181 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
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 GNUNET_DATASTORE_remove (datastore,
207 GNUNET_DATASTORE_remove (datastore,
214 GNUNET_SCHEDULER_add_continuation (crc->sched,
218 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
224 check_nothing (void *cls,
225 const GNUNET_HashCode * key,
231 struct GNUNET_TIME_Absolute
232 expiration, uint64_t uid)
234 struct CpsRunContext *crc = cls;
235 GNUNET_assert (key == NULL);
236 GNUNET_SCHEDULER_add_continuation (crc->sched,
240 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
245 run_continuation (void *cls,
246 const struct GNUNET_SCHEDULER_TaskContext *tc)
248 struct CpsRunContext *crc = cls;
249 ok = (int) crc->phase;
253 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
254 GNUNET_DATASTORE_put (datastore,
260 get_priority (crc->i),
261 get_anonymity (crc->i),
262 get_expiration (crc->i),
272 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
273 GNUNET_DATASTORE_get (datastore,
287 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
288 GNUNET_DATASTORE_get (datastore,
296 crc->phase = RP_DELVALIDATE;
302 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
303 GNUNET_DATASTORE_get (datastore,
311 crc->phase = RP_DONE;
314 /* check reservations */
316 /* test multiple results */
318 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
326 struct GNUNET_SCHEDULER_Handle *sched,
328 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
330 struct CpsRunContext *crc;
332 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
337 datastore = GNUNET_DATASTORE_connect (cfg, sched);
338 GNUNET_SCHEDULER_add_continuation (crc->sched,
342 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
352 char *const argv[] = { "test-datastore-api",
354 "test_datastore_api_data.conf",
360 struct GNUNET_GETOPT_CommandLineOption options[] = {
361 GNUNET_GETOPT_OPTION_END
363 pid = GNUNET_OS_start_process ("gnunet-service-datastore",
364 "gnunet-service-datastore",
368 "-c", "test_datastore_api_data.conf", NULL);
370 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
371 argv, "test-datastore-api", "nohelp",
372 options, &run, NULL);
373 if (0 != PLIBC_KILL (pid, SIGTERM))
375 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
378 GNUNET_OS_process_wait(pid);
380 fprintf (stderr, "Missed some testcases: %u\n", ok);
385 main (int argc, char *argv[])
389 GNUNET_log_setup ("test-datastore-api",
403 /* end of test_datastore_api.c */