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 basic datastore API.
23 * @author Christian Grothoff
26 * - test reservation failure
30 #include "gnunet_util_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_datastore_service.h"
34 #define VERBOSE GNUNET_NO
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
41 #define ITERATIONS 256
43 static struct GNUNET_DATASTORE_Handle *datastore;
45 static struct GNUNET_TIME_Absolute now;
60 static char buf[60000];
61 memset (buf, i, 8 * i);
87 static struct GNUNET_TIME_Absolute
88 get_expiration (int i)
90 struct GNUNET_TIME_Absolute av;
92 av.value = now.value + 200000 - i * 1000;
106 RP_PUT_MULTIPLE_NEXT,
108 RP_GET_MULTIPLE_NEXT,
109 RP_GET_MULTIPLE_DONE,
121 struct GNUNET_SCHEDULER_Handle *sched;
122 const struct GNUNET_CONFIGURATION_Handle *cfg;
126 unsigned long long uid;
131 run_continuation (void *cls,
132 const struct GNUNET_SCHEDULER_TaskContext *tc);
136 check_success (void *cls,
140 struct CpsRunContext *crc = cls;
141 if (GNUNET_OK != success)
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
144 GNUNET_assert (GNUNET_OK == success);
145 GNUNET_free_non_null (crc->data);
147 GNUNET_SCHEDULER_add_continuation (crc->sched,
150 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
155 get_reserved (void *cls,
159 struct CpsRunContext *crc = cls;
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 GNUNET_assert (0 < success);
165 GNUNET_SCHEDULER_add_continuation (crc->sched,
168 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
173 check_value (void *cls,
174 const GNUNET_HashCode * key,
177 enum GNUNET_BLOCK_Type type,
180 struct GNUNET_TIME_Absolute
181 expiration, uint64_t uid)
183 struct CpsRunContext *crc = cls;
193 GNUNET_SCHEDULER_add_continuation (crc->sched,
196 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
200 GNUNET_assert (size == get_size (i));
201 GNUNET_assert (0 == memcmp (data, get_data(i), size));
202 GNUNET_assert (type == get_type (i));
203 GNUNET_assert (priority == get_priority (i));
204 GNUNET_assert (anonymity == get_anonymity(i));
205 GNUNET_assert (expiration.value == get_expiration(i).value);
206 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
211 delete_value (void *cls,
212 const GNUNET_HashCode * key,
215 enum GNUNET_BLOCK_Type type,
218 struct GNUNET_TIME_Absolute
219 expiration, uint64_t uid)
221 struct CpsRunContext *crc = cls;
224 crc->phase = RP_DO_DEL;
225 GNUNET_SCHEDULER_add_continuation (crc->sched,
228 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
231 GNUNET_assert (crc->data == NULL);
234 crc->data = GNUNET_malloc (size);
235 memcpy (crc->data, data, size);
236 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
241 check_nothing (void *cls,
242 const GNUNET_HashCode * key,
245 enum GNUNET_BLOCK_Type type,
248 struct GNUNET_TIME_Absolute
249 expiration, uint64_t uid)
251 struct CpsRunContext *crc = cls;
252 GNUNET_assert (key == NULL);
255 crc->phase = RP_RESERVE;
257 GNUNET_SCHEDULER_add_continuation (crc->sched,
260 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
265 check_multiple (void *cls,
266 const GNUNET_HashCode * key,
269 enum GNUNET_BLOCK_Type type,
272 struct GNUNET_TIME_Absolute
273 expiration, uint64_t uid)
275 struct CpsRunContext *crc = cls;
279 GNUNET_assert (crc->phase == RP_GET_MULTIPLE_DONE);
280 crc->phase = RP_UPDATE;
281 GNUNET_SCHEDULER_add_continuation (crc->sched,
284 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
288 if (priority == get_priority (42))
290 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
295 check_update (void *cls,
296 const GNUNET_HashCode * key,
299 enum GNUNET_BLOCK_Type type,
302 struct GNUNET_TIME_Absolute
303 expiration, uint64_t uid)
305 struct CpsRunContext *crc = cls;
309 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
310 crc->phase = RP_DONE;
311 GNUNET_SCHEDULER_add_continuation (crc->sched,
314 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
317 if ( (anonymity == get_anonymity (42)) &&
318 (size == get_size (42)) &&
319 (priority == get_priority (42) + 100) )
321 crc->phase = RP_UPDATE_DONE;
324 GNUNET_assert (size == get_size (43));
325 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
330 run_continuation (void *cls,
331 const struct GNUNET_SCHEDULER_TaskContext *tc)
333 struct CpsRunContext *crc = cls;
334 ok = (int) crc->phase;
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Test in phase %u\n", crc->phase);
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "Executing `%s' number %u\n",
348 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
349 GNUNET_DATASTORE_put (datastore,
355 get_priority (crc->i),
356 get_anonymity (crc->i),
357 get_expiration (crc->i),
362 if (crc->i == ITERATIONS)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Executing `%s' number %u\n",
373 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
374 GNUNET_DATASTORE_get (datastore,
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Executing `%s' number %u\n",
389 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
390 GNUNET_DATASTORE_get (datastore,
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Executing `%s' number %u\n",
407 crc->phase = RP_DELVALIDATE;
413 GNUNET_DATASTORE_remove (datastore,
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Executing `%s' number %u\n",
429 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
430 GNUNET_DATASTORE_get (datastore,
438 crc->phase = RP_PUT_MULTIPLE;
439 GNUNET_DATASTORE_reserve (datastore,
446 case RP_PUT_MULTIPLE:
447 crc->phase = RP_PUT_MULTIPLE_NEXT;
448 GNUNET_DATASTORE_put (datastore,
461 case RP_PUT_MULTIPLE_NEXT:
462 crc->phase = RP_GET_MULTIPLE;
463 GNUNET_DATASTORE_put (datastore,
476 case RP_GET_MULTIPLE:
477 GNUNET_DATASTORE_get (datastore,
484 case RP_GET_MULTIPLE_NEXT:
485 case RP_GET_MULTIPLE_DONE:
489 GNUNET_assert (crc->uid > 0);
490 crc->phase = RP_UPDATE_VALIDATE;
491 GNUNET_DATASTORE_update (datastore,
499 case RP_UPDATE_VALIDATE:
500 GNUNET_DATASTORE_get (datastore,
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Finished, disconnecting\n");
515 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
524 struct GNUNET_SCHEDULER_Handle *sched,
527 const struct GNUNET_CONFIGURATION_Handle *cfg)
529 struct CpsRunContext *crc;
531 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
535 now = GNUNET_TIME_absolute_get ();
536 datastore = GNUNET_DATASTORE_connect (cfg, sched);
537 GNUNET_SCHEDULER_add_continuation (crc->sched,
540 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
550 char *const argv[] = { "test-datastore-api",
552 "test_datastore_api_data.conf",
558 struct GNUNET_GETOPT_CommandLineOption options[] = {
559 GNUNET_GETOPT_OPTION_END
561 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
562 "gnunet-service-arm",
566 "-c", "test_datastore_api_data.conf", NULL);
567 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
568 argv, "test-datastore-api", "nohelp",
569 options, &run, NULL);
570 if (0 != PLIBC_KILL (pid, SIGTERM))
572 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
575 GNUNET_OS_process_wait(pid);
577 fprintf (stderr, "Missed some testcases: %u\n", ok);
582 main (int argc, char *argv[])
586 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
587 GNUNET_log_setup ("test-datastore-api",
595 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
602 /* end of test_datastore_api.c */