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)
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 not found!\n");
233 crc->phase = RP_ERROR;
237 crc->phase = RP_DO_DEL;
239 GNUNET_SCHEDULER_add_continuation (crc->sched,
242 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
245 GNUNET_assert (crc->data == NULL);
248 crc->data = GNUNET_malloc (size);
249 memcpy (crc->data, data, size);
250 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
255 check_nothing (void *cls,
256 const GNUNET_HashCode * key,
259 enum GNUNET_BLOCK_Type type,
262 struct GNUNET_TIME_Absolute
263 expiration, uint64_t uid)
265 struct CpsRunContext *crc = cls;
266 GNUNET_assert (key == NULL);
269 crc->phase = RP_RESERVE;
271 GNUNET_SCHEDULER_add_continuation (crc->sched,
274 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
279 check_multiple (void *cls,
280 const GNUNET_HashCode * key,
283 enum GNUNET_BLOCK_Type type,
286 struct GNUNET_TIME_Absolute
287 expiration, uint64_t uid)
289 struct CpsRunContext *crc = cls;
293 GNUNET_assert (crc->phase == RP_GET_MULTIPLE_DONE);
294 crc->phase = RP_UPDATE;
295 GNUNET_SCHEDULER_add_continuation (crc->sched,
298 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
302 if (priority == get_priority (42))
304 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
309 check_update (void *cls,
310 const GNUNET_HashCode * key,
313 enum GNUNET_BLOCK_Type type,
316 struct GNUNET_TIME_Absolute
317 expiration, uint64_t uid)
319 struct CpsRunContext *crc = cls;
323 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
324 crc->phase = RP_DONE;
325 GNUNET_SCHEDULER_add_continuation (crc->sched,
328 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
331 if ( (anonymity == get_anonymity (42)) &&
332 (size == get_size (42)) &&
333 (priority == get_priority (42) + 100) )
335 crc->phase = RP_UPDATE_DONE;
338 GNUNET_assert (size == get_size (43));
339 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
344 run_continuation (void *cls,
345 const struct GNUNET_SCHEDULER_TaskContext *tc)
347 struct CpsRunContext *crc = cls;
348 ok = (int) crc->phase;
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Test in phase %u\n", crc->phase);
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Executing `%s' number %u\n",
362 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
363 GNUNET_DATASTORE_put (datastore,
369 get_priority (crc->i),
370 get_anonymity (crc->i),
371 get_expiration (crc->i),
376 if (crc->i == ITERATIONS)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Executing `%s' number %u\n",
387 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
388 GNUNET_DATASTORE_get (datastore,
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Executing `%s' number %u\n",
404 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
405 GNUNET_DATASTORE_get (datastore,
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Executing `%s' number %u\n",
422 crc->phase = RP_DELVALIDATE;
428 GNUNET_DATASTORE_remove (datastore,
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Executing `%s' number %u\n",
444 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
445 GNUNET_DATASTORE_get (datastore,
453 crc->phase = RP_PUT_MULTIPLE;
454 GNUNET_DATASTORE_reserve (datastore,
461 case RP_PUT_MULTIPLE:
462 crc->phase = RP_PUT_MULTIPLE_NEXT;
463 GNUNET_DATASTORE_put (datastore,
476 case RP_PUT_MULTIPLE_NEXT:
477 crc->phase = RP_GET_MULTIPLE;
478 GNUNET_DATASTORE_put (datastore,
491 case RP_GET_MULTIPLE:
492 GNUNET_DATASTORE_get (datastore,
499 case RP_GET_MULTIPLE_NEXT:
500 case RP_GET_MULTIPLE_DONE:
504 GNUNET_assert (crc->uid > 0);
505 crc->phase = RP_UPDATE_VALIDATE;
506 GNUNET_DATASTORE_update (datastore,
514 case RP_UPDATE_VALIDATE:
515 GNUNET_DATASTORE_get (datastore,
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Finished, disconnecting\n");
530 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
535 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
545 struct GNUNET_SCHEDULER_Handle *sched,
548 const struct GNUNET_CONFIGURATION_Handle *cfg)
550 struct CpsRunContext *crc;
552 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
556 now = GNUNET_TIME_absolute_get ();
557 datastore = GNUNET_DATASTORE_connect (cfg, sched);
558 GNUNET_SCHEDULER_add_continuation (crc->sched,
561 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
571 char *const argv[] = { "test-datastore-api",
573 "test_datastore_api_data.conf",
579 struct GNUNET_GETOPT_CommandLineOption options[] = {
580 GNUNET_GETOPT_OPTION_END
582 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
583 "gnunet-service-arm",
587 "-c", "test_datastore_api_data.conf", NULL);
588 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
589 argv, "test-datastore-api", "nohelp",
590 options, &run, NULL);
591 if (0 != PLIBC_KILL (pid, SIGTERM))
593 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
596 GNUNET_OS_process_wait(pid);
598 fprintf (stderr, "Missed some testcases: %u\n", ok);
603 main (int argc, char *argv[])
607 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
608 GNUNET_log_setup ("test-datastore-api",
616 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
623 /* end of test_datastore_api.c */