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 + 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);
310 if (priority == get_priority (42))
312 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
317 check_update (void *cls,
318 const GNUNET_HashCode * key,
321 enum GNUNET_BLOCK_Type type,
324 struct GNUNET_TIME_Absolute
325 expiration, uint64_t uid)
327 struct CpsRunContext *crc = cls;
331 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
332 crc->phase = RP_DONE;
333 GNUNET_SCHEDULER_add_continuation (crc->sched,
336 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
339 if ( (anonymity == get_anonymity (42)) &&
340 (size == get_size (42)) &&
341 (priority == get_priority (42) + 100) )
343 crc->phase = RP_UPDATE_DONE;
346 GNUNET_assert (size == get_size (43));
347 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
352 run_continuation (void *cls,
353 const struct GNUNET_SCHEDULER_TaskContext *tc)
355 struct CpsRunContext *crc = cls;
356 ok = (int) crc->phase;
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "Test in phase %u\n", crc->phase);
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Executing `%s' number %u\n",
370 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
371 GNUNET_DATASTORE_put (datastore,
377 get_priority (crc->i),
378 get_anonymity (crc->i),
379 get_expiration (crc->i),
384 if (crc->i == ITERATIONS)
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Executing `%s' number %u\n",
395 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
396 GNUNET_DATASTORE_get (datastore,
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Executing `%s' number %u\n",
412 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
413 GNUNET_DATASTORE_get (datastore,
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Executing `%s' number %u\n",
430 crc->phase = RP_DELVALIDATE;
436 GNUNET_DATASTORE_remove (datastore,
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 "Executing `%s' number %u\n",
452 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
453 GNUNET_DATASTORE_get (datastore,
461 crc->phase = RP_PUT_MULTIPLE;
462 GNUNET_DATASTORE_reserve (datastore,
469 case RP_PUT_MULTIPLE:
470 crc->phase = RP_PUT_MULTIPLE_NEXT;
471 GNUNET_DATASTORE_put (datastore,
484 case RP_PUT_MULTIPLE_NEXT:
485 crc->phase = RP_GET_MULTIPLE;
486 GNUNET_DATASTORE_put (datastore,
499 case RP_GET_MULTIPLE:
500 GNUNET_DATASTORE_get (datastore,
507 case RP_GET_MULTIPLE_NEXT:
508 case RP_GET_MULTIPLE_DONE:
512 GNUNET_assert (crc->uid > 0);
513 crc->phase = RP_UPDATE_VALIDATE;
514 GNUNET_DATASTORE_update (datastore,
522 case RP_UPDATE_VALIDATE:
523 GNUNET_DATASTORE_get (datastore,
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "Finished, disconnecting\n");
538 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
543 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
553 struct GNUNET_SCHEDULER_Handle *sched,
556 const struct GNUNET_CONFIGURATION_Handle *cfg)
558 struct CpsRunContext *crc;
560 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
564 now = GNUNET_TIME_absolute_get ();
565 datastore = GNUNET_DATASTORE_connect (cfg, sched);
566 GNUNET_SCHEDULER_add_continuation (crc->sched,
569 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
579 char *const argv[] = { "test-datastore-api",
581 "test_datastore_api_data.conf",
587 struct GNUNET_GETOPT_CommandLineOption options[] = {
588 GNUNET_GETOPT_OPTION_END
590 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
591 "gnunet-service-arm",
595 "-c", "test_datastore_api_data.conf", NULL);
596 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
597 argv, "test-datastore-api", "nohelp",
598 options, &run, NULL);
599 if (0 != PLIBC_KILL (pid, SIGTERM))
601 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
604 GNUNET_OS_process_wait(pid);
606 fprintf (stderr, "Missed some testcases: %u\n", ok);
611 main (int argc, char *argv[])
615 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
616 GNUNET_log_setup ("test-datastore-api",
624 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
631 /* end of test_datastore_api.c */