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);
214 delete_value (void *cls,
215 const GNUNET_HashCode * key,
221 struct GNUNET_TIME_Absolute
222 expiration, uint64_t uid)
224 struct CpsRunContext *crc = cls;
227 crc->phase = RP_DO_DEL;
228 GNUNET_SCHEDULER_add_continuation (crc->sched,
232 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
235 GNUNET_assert (crc->data == NULL);
238 crc->data = GNUNET_malloc (size);
239 memcpy (crc->data, data, size);
244 check_nothing (void *cls,
245 const GNUNET_HashCode * key,
251 struct GNUNET_TIME_Absolute
252 expiration, uint64_t uid)
254 struct CpsRunContext *crc = cls;
255 GNUNET_assert (key == NULL);
258 crc->phase = RP_RESERVE;
260 GNUNET_SCHEDULER_add_continuation (crc->sched,
264 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
269 check_multiple (void *cls,
270 const GNUNET_HashCode * key,
276 struct GNUNET_TIME_Absolute
277 expiration, uint64_t uid)
279 struct CpsRunContext *crc = cls;
283 GNUNET_assert (crc->phase == RP_GET_MULTIPLE_DONE);
284 crc->phase = RP_UPDATE;
285 GNUNET_SCHEDULER_add_continuation (crc->sched,
289 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
293 if (priority == get_priority (42))
299 check_update (void *cls,
300 const GNUNET_HashCode * key,
306 struct GNUNET_TIME_Absolute
307 expiration, uint64_t uid)
309 struct CpsRunContext *crc = cls;
313 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
314 crc->phase = RP_DONE;
315 GNUNET_SCHEDULER_add_continuation (crc->sched,
319 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
322 if ( (anonymity == get_anonymity (42)) &&
323 (size == get_size (42)) &&
324 (priority == get_priority (42) + 100) )
326 crc->phase = RP_UPDATE_DONE;
329 GNUNET_assert (size == get_size (43));
334 run_continuation (void *cls,
335 const struct GNUNET_SCHEDULER_TaskContext *tc)
337 struct CpsRunContext *crc = cls;
338 ok = (int) crc->phase;
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Test in phase %u\n", crc->phase);
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Executing `%s' number %u\n",
352 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
353 GNUNET_DATASTORE_put (datastore,
359 get_priority (crc->i),
360 get_anonymity (crc->i),
361 get_expiration (crc->i),
366 if (crc->i == ITERATIONS)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Executing `%s' number %u\n",
377 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
378 GNUNET_DATASTORE_get (datastore,
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Executing `%s' number %u\n",
393 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
394 GNUNET_DATASTORE_get (datastore,
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Executing `%s' number %u\n",
411 crc->phase = RP_DELVALIDATE;
417 GNUNET_DATASTORE_remove (datastore,
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Executing `%s' number %u\n",
433 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
434 GNUNET_DATASTORE_get (datastore,
442 crc->phase = RP_PUT_MULTIPLE;
443 GNUNET_DATASTORE_reserve (datastore,
450 case RP_PUT_MULTIPLE:
451 crc->phase = RP_PUT_MULTIPLE_NEXT;
452 GNUNET_DATASTORE_put (datastore,
465 case RP_PUT_MULTIPLE_NEXT:
466 crc->phase = RP_GET_MULTIPLE;
467 GNUNET_DATASTORE_put (datastore,
480 case RP_GET_MULTIPLE:
481 GNUNET_DATASTORE_get (datastore,
488 case RP_GET_MULTIPLE_NEXT:
489 case RP_GET_MULTIPLE_DONE:
493 GNUNET_assert (crc->uid > 0);
494 crc->phase = RP_UPDATE_VALIDATE;
495 GNUNET_DATASTORE_update (datastore,
503 case RP_UPDATE_VALIDATE:
504 GNUNET_DATASTORE_get (datastore,
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "Finished, disconnecting\n");
519 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
528 struct GNUNET_SCHEDULER_Handle *sched,
531 const struct GNUNET_CONFIGURATION_Handle *cfg)
533 struct CpsRunContext *crc;
535 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
539 now = GNUNET_TIME_absolute_get ();
540 datastore = GNUNET_DATASTORE_connect (cfg, sched);
541 GNUNET_SCHEDULER_add_continuation (crc->sched,
545 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
555 char *const argv[] = { "test-datastore-api",
557 "test_datastore_api_data.conf",
563 struct GNUNET_GETOPT_CommandLineOption options[] = {
564 GNUNET_GETOPT_OPTION_END
566 pid = GNUNET_OS_start_process ("gnunet-service-datastore",
567 "gnunet-service-datastore",
571 "-c", "test_datastore_api_data.conf", NULL);
573 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
574 argv, "test-datastore-api", "nohelp",
575 options, &run, NULL);
576 if (0 != PLIBC_KILL (pid, SIGTERM))
578 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
581 GNUNET_OS_process_wait(pid);
583 fprintf (stderr, "Missed some testcases: %u\n", ok);
588 main (int argc, char *argv[])
592 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
593 GNUNET_log_setup ("test-datastore-api",
601 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-datastore");
608 /* end of test_datastore_api.c */