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
36 #define START_DATASTORE GNUNET_YES
39 * How long until we give up on transmitting the message?
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
43 #define ITERATIONS 256
45 static struct GNUNET_DATASTORE_Handle *datastore;
47 static struct GNUNET_TIME_Absolute now;
52 * Name of plugin under test.
54 static const char *plugin_name;
66 static char buf[60000];
67 memset (buf, i, 8 * i);
93 static struct GNUNET_TIME_Absolute
94 get_expiration (int i)
96 struct GNUNET_TIME_Absolute av;
98 av.value = now.value + 20000000 - i * 1000;
112 RP_PUT_MULTIPLE_NEXT,
114 RP_GET_MULTIPLE_NEXT, /* 10 */
115 RP_GET_MULTIPLE_DONE,
117 RP_UPDATE_VALIDATE, /* 13 */
128 struct GNUNET_SCHEDULER_Handle *sched;
129 const struct GNUNET_CONFIGURATION_Handle *cfg;
133 unsigned long long uid;
138 run_continuation (void *cls,
139 const struct GNUNET_SCHEDULER_TaskContext *tc);
143 check_success (void *cls,
147 struct CpsRunContext *crc = cls;
148 if (GNUNET_OK != success)
151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 GNUNET_SCHEDULER_shutdown (crc->sched);
156 GNUNET_free_non_null (crc->data);
158 GNUNET_SCHEDULER_add_continuation (crc->sched,
161 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
166 get_reserved (void *cls,
170 struct CpsRunContext *crc = cls;
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 GNUNET_assert (0 < success);
176 GNUNET_SCHEDULER_add_continuation (crc->sched,
179 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
184 check_value (void *cls,
185 const GNUNET_HashCode * key,
188 enum GNUNET_BLOCK_Type type,
191 struct GNUNET_TIME_Absolute
192 expiration, uint64_t uid)
194 struct CpsRunContext *crc = cls;
204 GNUNET_SCHEDULER_add_continuation (crc->sched,
207 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
211 GNUNET_assert (size == get_size (i));
212 GNUNET_assert (0 == memcmp (data, get_data(i), size));
213 GNUNET_assert (type == get_type (i));
214 GNUNET_assert (priority == get_priority (i));
215 GNUNET_assert (anonymity == get_anonymity(i));
216 GNUNET_assert (expiration.value == get_expiration(i).value);
217 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
222 delete_value (void *cls,
223 const GNUNET_HashCode * key,
226 enum GNUNET_BLOCK_Type type,
229 struct GNUNET_TIME_Absolute
230 expiration, uint64_t uid)
232 struct CpsRunContext *crc = cls;
235 if (crc->data == NULL)
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238 "Content %u not found!\n",
240 crc->phase = RP_ERROR;
244 crc->phase = RP_DO_DEL;
246 GNUNET_SCHEDULER_add_continuation (crc->sched,
249 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
252 GNUNET_assert (crc->data == NULL);
255 crc->data = GNUNET_malloc (size);
256 memcpy (crc->data, data, size);
257 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
262 check_nothing (void *cls,
263 const GNUNET_HashCode * key,
266 enum GNUNET_BLOCK_Type type,
269 struct GNUNET_TIME_Absolute
270 expiration, uint64_t uid)
272 struct CpsRunContext *crc = cls;
273 GNUNET_assert (key == NULL);
276 crc->phase = RP_RESERVE;
278 GNUNET_SCHEDULER_add_continuation (crc->sched,
281 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
286 check_multiple (void *cls,
287 const GNUNET_HashCode * key,
290 enum GNUNET_BLOCK_Type type,
293 struct GNUNET_TIME_Absolute
294 expiration, uint64_t uid)
296 struct CpsRunContext *crc = cls;
300 if (crc->phase != RP_GET_MULTIPLE_DONE)
306 crc->phase = RP_ERROR;
310 crc->phase = RP_UPDATE;
312 GNUNET_SCHEDULER_add_continuation (crc->sched,
315 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
320 case RP_GET_MULTIPLE:
321 crc->phase = RP_GET_MULTIPLE_NEXT;
323 case RP_GET_MULTIPLE_NEXT:
324 crc->phase = RP_GET_MULTIPLE_DONE;
326 case RP_GET_MULTIPLE_DONE:
327 /* do not advance further */
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Test in phase %u\n", crc->phase);
337 if (priority == get_priority (42))
339 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
344 check_update (void *cls,
345 const GNUNET_HashCode * key,
348 enum GNUNET_BLOCK_Type type,
351 struct GNUNET_TIME_Absolute
352 expiration, uint64_t uid)
354 struct CpsRunContext *crc = cls;
358 if (crc->phase != RP_UPDATE_DONE)
361 crc->phase = RP_ERROR;
365 crc->phase = RP_DONE;
367 GNUNET_SCHEDULER_add_continuation (crc->sched,
370 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
373 if ( (anonymity == get_anonymity (42)) &&
374 (size == get_size (42)) &&
375 (priority == get_priority (42) + 100) )
377 crc->phase = RP_UPDATE_DONE;
380 GNUNET_assert (size == get_size (43));
381 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
386 run_continuation (void *cls,
387 const struct GNUNET_SCHEDULER_TaskContext *tc)
389 struct CpsRunContext *crc = cls;
390 ok = (int) crc->phase;
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Test in phase %u\n", crc->phase);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Executing `%s' number %u\n",
404 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
405 GNUNET_DATASTORE_put (datastore,
411 get_priority (crc->i),
412 get_anonymity (crc->i),
413 get_expiration (crc->i),
418 if (crc->i == ITERATIONS)
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Executing `%s' number %u\n",
429 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
430 GNUNET_DATASTORE_get (datastore,
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Executing `%s' number %u\n",
446 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
447 GNUNET_DATASTORE_get (datastore,
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Executing `%s' number %u\n",
464 crc->phase = RP_DELVALIDATE;
470 GNUNET_DATASTORE_remove (datastore,
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Executing `%s' number %u\n",
486 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
487 GNUNET_DATASTORE_get (datastore,
495 crc->phase = RP_PUT_MULTIPLE;
496 GNUNET_DATASTORE_reserve (datastore,
503 case RP_PUT_MULTIPLE:
504 crc->phase = RP_PUT_MULTIPLE_NEXT;
505 GNUNET_DATASTORE_put (datastore,
518 case RP_PUT_MULTIPLE_NEXT:
519 crc->phase = RP_GET_MULTIPLE;
520 GNUNET_DATASTORE_put (datastore,
533 case RP_GET_MULTIPLE:
534 GNUNET_DATASTORE_get (datastore,
541 case RP_GET_MULTIPLE_NEXT:
542 case RP_GET_MULTIPLE_DONE:
546 GNUNET_assert (crc->uid > 0);
547 crc->phase = RP_UPDATE_VALIDATE;
548 GNUNET_DATASTORE_update (datastore,
556 case RP_UPDATE_VALIDATE:
557 GNUNET_DATASTORE_get (datastore,
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
570 "Finished, disconnecting\n");
572 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
577 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
587 struct GNUNET_SCHEDULER_Handle *sched,
590 const struct GNUNET_CONFIGURATION_Handle *cfg)
592 struct CpsRunContext *crc;
594 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
598 now = GNUNET_TIME_absolute_get ();
599 datastore = GNUNET_DATASTORE_connect (cfg, sched);
600 GNUNET_SCHEDULER_add_continuation (crc->sched,
603 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
616 char *const argv[] = {
617 "test-datastore-api",
625 struct GNUNET_GETOPT_CommandLineOption options[] = {
626 GNUNET_GETOPT_OPTION_END
628 GNUNET_snprintf (cfg_name,
630 "test_datastore_api_data_%s.conf",
633 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
634 "gnunet-service-arm",
638 "-c", cfg_name, NULL);
640 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
641 argv, "test-datastore-api", "nohelp",
642 options, &run, NULL);
644 if (0 != PLIBC_KILL (pid, SIGTERM))
646 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
649 GNUNET_OS_process_wait(pid);
652 fprintf (stderr, "Missed some testcases: %u\n", ok);
657 main (int argc, char *argv[])
663 /* determine name of plugin to use */
664 plugin_name = argv[0];
665 while (NULL != (pos = strstr(plugin_name, "_")))
668 GNUNET_snprintf (dir_name,
670 "/tmp/test-gnunet-datastore-%s",
672 GNUNET_DISK_directory_remove (dir_name);
673 GNUNET_log_setup ("test-datastore-api",
681 GNUNET_DISK_directory_remove (dir_name);
685 /* end of test_datastore_api.c */