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,
122 struct GNUNET_SCHEDULER_Handle *sched;
123 const struct GNUNET_CONFIGURATION_Handle *cfg;
127 unsigned long long uid;
132 run_continuation (void *cls,
133 const struct GNUNET_SCHEDULER_TaskContext *tc);
137 check_success (void *cls,
141 struct CpsRunContext *crc = cls;
142 if (GNUNET_OK != success)
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 GNUNET_assert (GNUNET_OK == success);
146 GNUNET_free_non_null (crc->data);
148 GNUNET_SCHEDULER_add_continuation (crc->sched,
152 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
157 get_reserved (void *cls,
161 struct CpsRunContext *crc = cls;
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 GNUNET_assert (0 < success);
167 GNUNET_SCHEDULER_add_continuation (crc->sched,
171 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
176 check_value (void *cls,
177 const GNUNET_HashCode * key,
183 struct GNUNET_TIME_Absolute
184 expiration, uint64_t uid)
186 struct CpsRunContext *crc = cls;
196 GNUNET_SCHEDULER_add_continuation (crc->sched,
200 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
204 GNUNET_assert (size == get_size (i));
205 GNUNET_assert (0 == memcmp (data, get_data(i), size));
206 GNUNET_assert (type == get_type (i));
207 GNUNET_assert (priority == get_priority (i));
208 GNUNET_assert (anonymity == get_anonymity(i));
209 GNUNET_assert (expiration.value == get_expiration(i).value);
210 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
215 delete_value (void *cls,
216 const GNUNET_HashCode * key,
222 struct GNUNET_TIME_Absolute
223 expiration, uint64_t uid)
225 struct CpsRunContext *crc = cls;
228 crc->phase = RP_DO_DEL;
229 GNUNET_SCHEDULER_add_continuation (crc->sched,
233 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
236 GNUNET_assert (crc->data == NULL);
239 crc->data = GNUNET_malloc (size);
240 memcpy (crc->data, data, size);
241 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
246 check_nothing (void *cls,
247 const GNUNET_HashCode * key,
253 struct GNUNET_TIME_Absolute
254 expiration, uint64_t uid)
256 struct CpsRunContext *crc = cls;
257 GNUNET_assert (key == NULL);
260 crc->phase = RP_RESERVE;
262 GNUNET_SCHEDULER_add_continuation (crc->sched,
266 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
271 check_multiple (void *cls,
272 const GNUNET_HashCode * key,
278 struct GNUNET_TIME_Absolute
279 expiration, uint64_t uid)
281 struct CpsRunContext *crc = cls;
285 GNUNET_assert (crc->phase == RP_GET_MULTIPLE_DONE);
286 crc->phase = RP_UPDATE;
287 GNUNET_SCHEDULER_add_continuation (crc->sched,
291 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
295 if (priority == get_priority (42))
297 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
302 check_update (void *cls,
303 const GNUNET_HashCode * key,
309 struct GNUNET_TIME_Absolute
310 expiration, uint64_t uid)
312 struct CpsRunContext *crc = cls;
316 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
317 crc->phase = RP_DONE;
318 GNUNET_SCHEDULER_add_continuation (crc->sched,
322 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
325 if ( (anonymity == get_anonymity (42)) &&
326 (size == get_size (42)) &&
327 (priority == get_priority (42) + 100) )
329 crc->phase = RP_UPDATE_DONE;
332 GNUNET_assert (size == get_size (43));
333 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
338 run_continuation (void *cls,
339 const struct GNUNET_SCHEDULER_TaskContext *tc)
341 struct CpsRunContext *crc = cls;
342 ok = (int) crc->phase;
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Test in phase %u\n", crc->phase);
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Executing `%s' number %u\n",
356 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
357 GNUNET_DATASTORE_put (datastore,
363 get_priority (crc->i),
364 get_anonymity (crc->i),
365 get_expiration (crc->i),
370 if (crc->i == ITERATIONS)
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Executing `%s' number %u\n",
381 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
382 GNUNET_DATASTORE_get (datastore,
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Executing `%s' number %u\n",
397 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
398 GNUNET_DATASTORE_get (datastore,
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Executing `%s' number %u\n",
415 crc->phase = RP_DELVALIDATE;
421 GNUNET_DATASTORE_remove (datastore,
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Executing `%s' number %u\n",
437 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
438 GNUNET_DATASTORE_get (datastore,
446 crc->phase = RP_PUT_MULTIPLE;
447 GNUNET_DATASTORE_reserve (datastore,
454 case RP_PUT_MULTIPLE:
455 crc->phase = RP_PUT_MULTIPLE_NEXT;
456 GNUNET_DATASTORE_put (datastore,
469 case RP_PUT_MULTIPLE_NEXT:
470 crc->phase = RP_GET_MULTIPLE;
471 GNUNET_DATASTORE_put (datastore,
484 case RP_GET_MULTIPLE:
485 GNUNET_DATASTORE_get (datastore,
492 case RP_GET_MULTIPLE_NEXT:
493 case RP_GET_MULTIPLE_DONE:
497 GNUNET_assert (crc->uid > 0);
498 crc->phase = RP_UPDATE_VALIDATE;
499 GNUNET_DATASTORE_update (datastore,
507 case RP_UPDATE_VALIDATE:
508 GNUNET_DATASTORE_get (datastore,
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Finished, disconnecting\n");
523 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
532 struct GNUNET_SCHEDULER_Handle *sched,
535 const struct GNUNET_CONFIGURATION_Handle *cfg)
537 struct CpsRunContext *crc;
539 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
543 now = GNUNET_TIME_absolute_get ();
544 datastore = GNUNET_DATASTORE_connect (cfg, sched);
545 GNUNET_SCHEDULER_add_continuation (crc->sched,
549 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
559 char *const argv[] = { "test-datastore-api",
561 "test_datastore_api_data.conf",
567 struct GNUNET_GETOPT_CommandLineOption options[] = {
568 GNUNET_GETOPT_OPTION_END
570 pid = GNUNET_OS_start_process ("gnunet-service-datastore",
571 "gnunet-service-datastore",
575 "-c", "test_datastore_api_data.conf", NULL);
577 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
578 argv, "test-datastore-api", "nohelp",
579 options, &run, NULL);
580 if (0 != PLIBC_KILL (pid, SIGTERM))
582 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
585 GNUNET_OS_process_wait(pid);
587 fprintf (stderr, "Missed some testcases: %u\n", ok);
592 main (int argc, char *argv[])
596 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
597 GNUNET_log_setup ("test-datastore-api",
605 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
612 /* end of test_datastore_api.c */