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 static struct GNUNET_DATASTORE_Handle *datastore;
38 static struct GNUNET_TIME_Absolute now;
53 static char buf[60000];
54 memset (buf, i, 8 * i);
80 static struct GNUNET_TIME_Absolute
81 get_expiration (int i)
83 struct GNUNET_TIME_Absolute av;
85 av.value = now.value - i * 1000;
104 struct GNUNET_SCHEDULER_Handle *sched;
105 struct GNUNET_CONFIGURATION_Handle *cfg;
111 run_continuation (void *cls,
112 const struct GNUNET_SCHEDULER_TaskContext *tc);
116 check_success (void *cls,
120 struct CpsRunContext *crc = cls;
121 if (GNUNET_OK != success)
122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
124 GNUNET_assert (GNUNET_OK == success);
125 GNUNET_SCHEDULER_add_continuation (crc->sched,
129 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
134 check_failure (void *cls,
138 struct CpsRunContext *crc = cls;
139 GNUNET_assert (GNUNET_OK != success);
140 GNUNET_assert (NULL != msg);
141 GNUNET_SCHEDULER_add_continuation (crc->sched,
145 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
150 check_value (void *cls,
151 const GNUNET_HashCode * key,
157 struct GNUNET_TIME_Absolute
158 expiration, uint64_t uid)
160 struct CpsRunContext *crc = cls;
166 GNUNET_assert (size == get_size (i));
167 GNUNET_assert (0 == memcmp (data, get_data(i), size));
168 GNUNET_assert (type == get_type (i));
169 GNUNET_assert (priority == get_priority (i));
170 GNUNET_assert (anonymity == get_anonymity(i));
171 GNUNET_assert (expiration.value == get_expiration(i).value);
172 GNUNET_SCHEDULER_add_continuation (crc->sched,
176 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
181 delete_value (void *cls,
182 const GNUNET_HashCode * key,
188 struct GNUNET_TIME_Absolute
189 expiration, uint64_t uid)
191 struct CpsRunContext *crc = cls;
194 GNUNET_DATASTORE_remove (datastore,
201 GNUNET_DATASTORE_remove (datastore,
207 GNUNET_SCHEDULER_add_continuation (crc->sched,
211 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
217 check_nothing (void *cls,
218 const GNUNET_HashCode * key,
224 struct GNUNET_TIME_Absolute
225 expiration, uint64_t uid)
227 struct CpsRunContext *crc = cls;
228 GNUNET_assert (key == NULL);
229 GNUNET_SCHEDULER_add_continuation (crc->sched,
233 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
238 run_continuation (void *cls,
239 const struct GNUNET_SCHEDULER_TaskContext *tc)
241 struct CpsRunContext *crc = cls;
242 ok = (int) crc->phase;
246 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
247 GNUNET_DATASTORE_put (datastore,
253 get_priority (crc->i),
254 get_anonymity (crc->i),
255 get_expiration (crc->i),
264 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
265 GNUNET_DATASTORE_get (datastore,
278 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
279 GNUNET_DATASTORE_get (datastore,
286 crc->phase = RP_DELVALIDATE;
292 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
293 GNUNET_DATASTORE_get (datastore,
300 crc->phase = RP_DONE;
303 /* check reservations */
305 /* test multiple results */
307 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
315 struct GNUNET_SCHEDULER_Handle *sched,
317 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
319 struct CpsRunContext *crc;
321 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
326 datastore = GNUNET_DATASTORE_connect (cfg, sched);
327 GNUNET_SCHEDULER_add_continuation (crc->sched,
331 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
341 char *const argv[] = { "test-datastore-api",
343 "test_datastore_api_data.conf",
349 struct GNUNET_GETOPT_CommandLineOption options[] = {
350 GNUNET_GETOPT_OPTION_END
352 pid = GNUNET_OS_start_process ("gnunet-service-datastore",
353 "gnunet-service-datastore",
357 "-c", "test_datastore_api_data.conf", NULL);
359 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
360 argv, "test-datastore-api", "nohelp",
361 options, &run, NULL);
362 if (0 != PLIBC_KILL (pid, SIGTERM))
364 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
367 GNUNET_OS_process_wait(pid);
369 fprintf (stderr, "Missed some testcases: %u\n", ok);
374 main (int argc, char *argv[])
378 GNUNET_log_setup ("test-datastore-api",
392 /* end of test_datastore_api.c */