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 3, 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 + 20000000 - 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)
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 GNUNET_SCHEDULER_shutdown (crc->sched);
150 GNUNET_free_non_null (crc->data);
152 GNUNET_SCHEDULER_add_continuation (crc->sched,
155 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
160 get_reserved (void *cls,
164 struct CpsRunContext *crc = cls;
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
168 GNUNET_assert (0 < success);
170 GNUNET_SCHEDULER_add_continuation (crc->sched,
173 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
178 check_value (void *cls,
179 const GNUNET_HashCode * key,
182 enum GNUNET_BLOCK_Type type,
185 struct GNUNET_TIME_Absolute
186 expiration, uint64_t uid)
188 struct CpsRunContext *crc = cls;
198 GNUNET_SCHEDULER_add_continuation (crc->sched,
201 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
205 GNUNET_assert (size == get_size (i));
206 GNUNET_assert (0 == memcmp (data, get_data(i), size));
207 GNUNET_assert (type == get_type (i));
208 GNUNET_assert (priority == get_priority (i));
209 GNUNET_assert (anonymity == get_anonymity(i));
210 GNUNET_assert (expiration.value == get_expiration(i).value);
211 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
216 delete_value (void *cls,
217 const GNUNET_HashCode * key,
220 enum GNUNET_BLOCK_Type type,
223 struct GNUNET_TIME_Absolute
224 expiration, uint64_t uid)
226 struct CpsRunContext *crc = cls;
229 if (crc->data == NULL)
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232 "Content %u not found!\n",
234 crc->phase = RP_ERROR;
238 crc->phase = RP_DO_DEL;
240 GNUNET_SCHEDULER_add_continuation (crc->sched,
243 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
246 GNUNET_assert (crc->data == NULL);
249 crc->data = GNUNET_malloc (size);
250 memcpy (crc->data, data, size);
251 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
256 check_nothing (void *cls,
257 const GNUNET_HashCode * key,
260 enum GNUNET_BLOCK_Type type,
263 struct GNUNET_TIME_Absolute
264 expiration, uint64_t uid)
266 struct CpsRunContext *crc = cls;
267 GNUNET_assert (key == NULL);
270 crc->phase = RP_RESERVE;
272 GNUNET_SCHEDULER_add_continuation (crc->sched,
275 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
280 check_multiple (void *cls,
281 const GNUNET_HashCode * key,
284 enum GNUNET_BLOCK_Type type,
287 struct GNUNET_TIME_Absolute
288 expiration, uint64_t uid)
290 struct CpsRunContext *crc = cls;
294 if (crc->phase != RP_GET_MULTIPLE_DONE)
297 crc->phase = RP_ERROR;
301 crc->phase = RP_UPDATE;
303 GNUNET_SCHEDULER_add_continuation (crc->sched,
306 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
311 case RP_GET_MULTIPLE:
312 crc->phase = RP_GET_MULTIPLE_NEXT;
314 case RP_GET_MULTIPLE_NEXT:
315 crc->phase = RP_GET_MULTIPLE_DONE;
317 case RP_GET_MULTIPLE_DONE:
318 /* do not advance further */
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Test in phase %u\n", crc->phase);
328 if (priority == get_priority (42))
330 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
335 check_update (void *cls,
336 const GNUNET_HashCode * key,
339 enum GNUNET_BLOCK_Type type,
342 struct GNUNET_TIME_Absolute
343 expiration, uint64_t uid)
345 struct CpsRunContext *crc = cls;
349 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
350 crc->phase = RP_DONE;
351 GNUNET_SCHEDULER_add_continuation (crc->sched,
354 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
357 if ( (anonymity == get_anonymity (42)) &&
358 (size == get_size (42)) &&
359 (priority == get_priority (42) + 100) )
361 crc->phase = RP_UPDATE_DONE;
364 GNUNET_assert (size == get_size (43));
365 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
370 run_continuation (void *cls,
371 const struct GNUNET_SCHEDULER_TaskContext *tc)
373 struct CpsRunContext *crc = cls;
374 ok = (int) crc->phase;
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Test in phase %u\n", crc->phase);
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Executing `%s' number %u\n",
388 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
389 GNUNET_DATASTORE_put (datastore,
395 get_priority (crc->i),
396 get_anonymity (crc->i),
397 get_expiration (crc->i),
402 if (crc->i == ITERATIONS)
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Executing `%s' number %u\n",
413 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
414 GNUNET_DATASTORE_get (datastore,
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Executing `%s' number %u\n",
430 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
431 GNUNET_DATASTORE_get (datastore,
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Executing `%s' number %u\n",
448 crc->phase = RP_DELVALIDATE;
454 GNUNET_DATASTORE_remove (datastore,
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Executing `%s' number %u\n",
470 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
471 GNUNET_DATASTORE_get (datastore,
479 crc->phase = RP_PUT_MULTIPLE;
480 GNUNET_DATASTORE_reserve (datastore,
487 case RP_PUT_MULTIPLE:
488 crc->phase = RP_PUT_MULTIPLE_NEXT;
489 GNUNET_DATASTORE_put (datastore,
502 case RP_PUT_MULTIPLE_NEXT:
503 crc->phase = RP_GET_MULTIPLE;
504 GNUNET_DATASTORE_put (datastore,
517 case RP_GET_MULTIPLE:
518 GNUNET_DATASTORE_get (datastore,
525 case RP_GET_MULTIPLE_NEXT:
526 case RP_GET_MULTIPLE_DONE:
530 GNUNET_assert (crc->uid > 0);
531 crc->phase = RP_UPDATE_VALIDATE;
532 GNUNET_DATASTORE_update (datastore,
540 case RP_UPDATE_VALIDATE:
541 GNUNET_DATASTORE_get (datastore,
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "Finished, disconnecting\n");
556 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
561 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
571 struct GNUNET_SCHEDULER_Handle *sched,
574 const struct GNUNET_CONFIGURATION_Handle *cfg)
576 struct CpsRunContext *crc;
578 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
582 now = GNUNET_TIME_absolute_get ();
583 datastore = GNUNET_DATASTORE_connect (cfg, sched);
584 GNUNET_SCHEDULER_add_continuation (crc->sched,
587 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
597 char *const argv[] = { "test-datastore-api",
599 "test_datastore_api_data.conf",
605 struct GNUNET_GETOPT_CommandLineOption options[] = {
606 GNUNET_GETOPT_OPTION_END
608 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
609 "gnunet-service-arm",
613 "-c", "test_datastore_api_data.conf", NULL);
614 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
615 argv, "test-datastore-api", "nohelp",
616 options, &run, NULL);
617 if (0 != PLIBC_KILL (pid, SIGTERM))
619 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
622 GNUNET_OS_process_wait(pid);
624 fprintf (stderr, "Missed some testcases: %u\n", ok);
629 main (int argc, char *argv[])
633 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
634 GNUNET_log_setup ("test-datastore-api",
642 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
649 /* end of test_datastore_api.c */