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,
206 GNUNET_DATASTORE_remove (datastore,
212 GNUNET_SCHEDULER_add_continuation (crc->sched,
216 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
222 check_nothing (void *cls,
223 const GNUNET_HashCode * key,
229 struct GNUNET_TIME_Absolute
230 expiration, uint64_t uid)
232 struct CpsRunContext *crc = cls;
233 GNUNET_assert (key == NULL);
234 GNUNET_SCHEDULER_add_continuation (crc->sched,
238 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
243 run_continuation (void *cls,
244 const struct GNUNET_SCHEDULER_TaskContext *tc)
246 struct CpsRunContext *crc = cls;
247 ok = (int) crc->phase;
251 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
252 GNUNET_DATASTORE_put (datastore,
258 get_priority (crc->i),
259 get_anonymity (crc->i),
260 get_expiration (crc->i),
270 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
271 GNUNET_DATASTORE_get (datastore,
284 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
285 GNUNET_DATASTORE_get (datastore,
292 crc->phase = RP_DELVALIDATE;
298 memset (&crc->key, 256 - crc->i, sizeof (GNUNET_HashCode));
299 GNUNET_DATASTORE_get (datastore,
306 crc->phase = RP_DONE;
309 /* check reservations */
311 /* test multiple results */
313 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
321 struct GNUNET_SCHEDULER_Handle *sched,
323 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
325 struct CpsRunContext *crc;
327 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
332 datastore = GNUNET_DATASTORE_connect (cfg, sched);
333 GNUNET_SCHEDULER_add_continuation (crc->sched,
337 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
347 char *const argv[] = { "test-datastore-api",
349 "test_datastore_api_data.conf",
355 struct GNUNET_GETOPT_CommandLineOption options[] = {
356 GNUNET_GETOPT_OPTION_END
358 pid = GNUNET_OS_start_process ("gnunet-service-datastore",
359 "gnunet-service-datastore",
363 "-c", "test_datastore_api_data.conf", NULL);
365 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
366 argv, "test-datastore-api", "nohelp",
367 options, &run, NULL);
368 if (0 != PLIBC_KILL (pid, SIGTERM))
370 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
373 GNUNET_OS_process_wait(pid);
375 fprintf (stderr, "Missed some testcases: %u\n", ok);
380 main (int argc, char *argv[])
384 GNUNET_log_setup ("test-datastore-api",
398 /* end of test_datastore_api.c */