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;
50 * Name of plugin under test.
52 static const char *plugin_name;
64 static char buf[60000];
65 memset (buf, i, 8 * i);
91 static struct GNUNET_TIME_Absolute
92 get_expiration (int i)
94 struct GNUNET_TIME_Absolute av;
96 av.value = now.value + 20000000 - i * 1000;
110 RP_PUT_MULTIPLE_NEXT,
112 RP_GET_MULTIPLE_NEXT,
113 RP_GET_MULTIPLE_DONE,
126 struct GNUNET_SCHEDULER_Handle *sched;
127 const struct GNUNET_CONFIGURATION_Handle *cfg;
131 unsigned long long uid;
136 run_continuation (void *cls,
137 const struct GNUNET_SCHEDULER_TaskContext *tc);
141 check_success (void *cls,
145 struct CpsRunContext *crc = cls;
146 if (GNUNET_OK != success)
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 GNUNET_SCHEDULER_shutdown (crc->sched);
154 GNUNET_free_non_null (crc->data);
156 GNUNET_SCHEDULER_add_continuation (crc->sched,
159 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
164 get_reserved (void *cls,
168 struct CpsRunContext *crc = cls;
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 GNUNET_assert (0 < success);
174 GNUNET_SCHEDULER_add_continuation (crc->sched,
177 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
182 check_value (void *cls,
183 const GNUNET_HashCode * key,
186 enum GNUNET_BLOCK_Type type,
189 struct GNUNET_TIME_Absolute
190 expiration, uint64_t uid)
192 struct CpsRunContext *crc = cls;
202 GNUNET_SCHEDULER_add_continuation (crc->sched,
205 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
209 GNUNET_assert (size == get_size (i));
210 GNUNET_assert (0 == memcmp (data, get_data(i), size));
211 GNUNET_assert (type == get_type (i));
212 GNUNET_assert (priority == get_priority (i));
213 GNUNET_assert (anonymity == get_anonymity(i));
214 GNUNET_assert (expiration.value == get_expiration(i).value);
215 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
220 delete_value (void *cls,
221 const GNUNET_HashCode * key,
224 enum GNUNET_BLOCK_Type type,
227 struct GNUNET_TIME_Absolute
228 expiration, uint64_t uid)
230 struct CpsRunContext *crc = cls;
233 if (crc->data == NULL)
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 "Content %u not found!\n",
238 crc->phase = RP_ERROR;
242 crc->phase = RP_DO_DEL;
244 GNUNET_SCHEDULER_add_continuation (crc->sched,
247 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
250 GNUNET_assert (crc->data == NULL);
253 crc->data = GNUNET_malloc (size);
254 memcpy (crc->data, data, size);
255 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
260 check_nothing (void *cls,
261 const GNUNET_HashCode * key,
264 enum GNUNET_BLOCK_Type type,
267 struct GNUNET_TIME_Absolute
268 expiration, uint64_t uid)
270 struct CpsRunContext *crc = cls;
271 GNUNET_assert (key == NULL);
274 crc->phase = RP_RESERVE;
276 GNUNET_SCHEDULER_add_continuation (crc->sched,
279 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
284 check_multiple (void *cls,
285 const GNUNET_HashCode * key,
288 enum GNUNET_BLOCK_Type type,
291 struct GNUNET_TIME_Absolute
292 expiration, uint64_t uid)
294 struct CpsRunContext *crc = cls;
298 if (crc->phase != RP_GET_MULTIPLE_DONE)
301 crc->phase = RP_ERROR;
305 crc->phase = RP_UPDATE;
307 GNUNET_SCHEDULER_add_continuation (crc->sched,
310 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
315 case RP_GET_MULTIPLE:
316 crc->phase = RP_GET_MULTIPLE_NEXT;
318 case RP_GET_MULTIPLE_NEXT:
319 crc->phase = RP_GET_MULTIPLE_DONE;
321 case RP_GET_MULTIPLE_DONE:
322 /* do not advance further */
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Test in phase %u\n", crc->phase);
332 if (priority == get_priority (42))
334 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
339 check_update (void *cls,
340 const GNUNET_HashCode * key,
343 enum GNUNET_BLOCK_Type type,
346 struct GNUNET_TIME_Absolute
347 expiration, uint64_t uid)
349 struct CpsRunContext *crc = cls;
353 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
354 crc->phase = RP_DONE;
355 GNUNET_SCHEDULER_add_continuation (crc->sched,
358 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
361 if ( (anonymity == get_anonymity (42)) &&
362 (size == get_size (42)) &&
363 (priority == get_priority (42) + 100) )
365 crc->phase = RP_UPDATE_DONE;
368 GNUNET_assert (size == get_size (43));
369 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
374 run_continuation (void *cls,
375 const struct GNUNET_SCHEDULER_TaskContext *tc)
377 struct CpsRunContext *crc = cls;
378 ok = (int) crc->phase;
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Test in phase %u\n", crc->phase);
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Executing `%s' number %u\n",
392 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
393 GNUNET_DATASTORE_put (datastore,
399 get_priority (crc->i),
400 get_anonymity (crc->i),
401 get_expiration (crc->i),
406 if (crc->i == ITERATIONS)
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Executing `%s' number %u\n",
417 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
418 GNUNET_DATASTORE_get (datastore,
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Executing `%s' number %u\n",
434 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
435 GNUNET_DATASTORE_get (datastore,
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Executing `%s' number %u\n",
452 crc->phase = RP_DELVALIDATE;
458 GNUNET_DATASTORE_remove (datastore,
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Executing `%s' number %u\n",
474 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
475 GNUNET_DATASTORE_get (datastore,
483 crc->phase = RP_PUT_MULTIPLE;
484 GNUNET_DATASTORE_reserve (datastore,
491 case RP_PUT_MULTIPLE:
492 crc->phase = RP_PUT_MULTIPLE_NEXT;
493 GNUNET_DATASTORE_put (datastore,
506 case RP_PUT_MULTIPLE_NEXT:
507 crc->phase = RP_GET_MULTIPLE;
508 GNUNET_DATASTORE_put (datastore,
521 case RP_GET_MULTIPLE:
522 GNUNET_DATASTORE_get (datastore,
529 case RP_GET_MULTIPLE_NEXT:
530 case RP_GET_MULTIPLE_DONE:
534 GNUNET_assert (crc->uid > 0);
535 crc->phase = RP_UPDATE_VALIDATE;
536 GNUNET_DATASTORE_update (datastore,
544 case RP_UPDATE_VALIDATE:
545 GNUNET_DATASTORE_get (datastore,
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 "Finished, disconnecting\n");
560 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
565 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
575 struct GNUNET_SCHEDULER_Handle *sched,
578 const struct GNUNET_CONFIGURATION_Handle *cfg)
580 struct CpsRunContext *crc;
582 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
586 now = GNUNET_TIME_absolute_get ();
587 datastore = GNUNET_DATASTORE_connect (cfg, sched);
588 GNUNET_SCHEDULER_add_continuation (crc->sched,
591 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
602 char *const argv[] = { "test-datastore-api",
604 "test_datastore_api_data.conf",
610 struct GNUNET_GETOPT_CommandLineOption options[] = {
611 GNUNET_GETOPT_OPTION_END
613 GNUNET_snprintf (cfg_name,
615 "test_datastore_api_data_%s.conf",
617 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
618 "gnunet-service-arm",
622 "-c", cfg_name, NULL);
623 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
624 argv, "test-datastore-api", "nohelp",
625 options, &run, NULL);
626 if (0 != PLIBC_KILL (pid, SIGTERM))
628 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
631 GNUNET_OS_process_wait(pid);
633 fprintf (stderr, "Missed some testcases: %u\n", ok);
638 main (int argc, char *argv[])
644 /* determine name of plugin to use */
645 plugin_name = argv[0];
646 while (NULL != (pos = strstr(plugin_name, "_")))
649 GNUNET_snprintf (dir_name,
651 "/tmp/test-gnunet-datastore-%s",
653 GNUNET_DISK_directory_remove (dir_name);
654 GNUNET_log_setup ("test-datastore-api",
662 GNUNET_DISK_directory_remove (dir_name);
666 /* end of test_datastore_api.c */