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_NO
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,
115 RP_GET_MULTIPLE_DONE,
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)
303 crc->phase = RP_ERROR;
307 crc->phase = RP_UPDATE;
309 GNUNET_SCHEDULER_add_continuation (crc->sched,
312 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
317 case RP_GET_MULTIPLE:
318 crc->phase = RP_GET_MULTIPLE_NEXT;
320 case RP_GET_MULTIPLE_NEXT:
321 crc->phase = RP_GET_MULTIPLE_DONE;
323 case RP_GET_MULTIPLE_DONE:
324 /* do not advance further */
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Test in phase %u\n", crc->phase);
334 if (priority == get_priority (42))
336 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
341 check_update (void *cls,
342 const GNUNET_HashCode * key,
345 enum GNUNET_BLOCK_Type type,
348 struct GNUNET_TIME_Absolute
349 expiration, uint64_t uid)
351 struct CpsRunContext *crc = cls;
355 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
356 crc->phase = RP_DONE;
357 GNUNET_SCHEDULER_add_continuation (crc->sched,
360 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
363 if ( (anonymity == get_anonymity (42)) &&
364 (size == get_size (42)) &&
365 (priority == get_priority (42) + 100) )
367 crc->phase = RP_UPDATE_DONE;
370 GNUNET_assert (size == get_size (43));
371 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
376 run_continuation (void *cls,
377 const struct GNUNET_SCHEDULER_TaskContext *tc)
379 struct CpsRunContext *crc = cls;
380 ok = (int) crc->phase;
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Test in phase %u\n", crc->phase);
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Executing `%s' number %u\n",
394 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
395 GNUNET_DATASTORE_put (datastore,
401 get_priority (crc->i),
402 get_anonymity (crc->i),
403 get_expiration (crc->i),
408 if (crc->i == ITERATIONS)
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Executing `%s' number %u\n",
419 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
420 GNUNET_DATASTORE_get (datastore,
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Executing `%s' number %u\n",
436 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
437 GNUNET_DATASTORE_get (datastore,
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Executing `%s' number %u\n",
454 crc->phase = RP_DELVALIDATE;
460 GNUNET_DATASTORE_remove (datastore,
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Executing `%s' number %u\n",
476 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
477 GNUNET_DATASTORE_get (datastore,
485 crc->phase = RP_PUT_MULTIPLE;
486 GNUNET_DATASTORE_reserve (datastore,
493 case RP_PUT_MULTIPLE:
494 crc->phase = RP_PUT_MULTIPLE_NEXT;
495 GNUNET_DATASTORE_put (datastore,
508 case RP_PUT_MULTIPLE_NEXT:
509 crc->phase = RP_GET_MULTIPLE;
510 GNUNET_DATASTORE_put (datastore,
523 case RP_GET_MULTIPLE:
524 GNUNET_DATASTORE_get (datastore,
531 case RP_GET_MULTIPLE_NEXT:
532 case RP_GET_MULTIPLE_DONE:
536 GNUNET_assert (crc->uid > 0);
537 crc->phase = RP_UPDATE_VALIDATE;
538 GNUNET_DATASTORE_update (datastore,
546 case RP_UPDATE_VALIDATE:
547 GNUNET_DATASTORE_get (datastore,
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Finished, disconnecting\n");
562 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
567 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
577 struct GNUNET_SCHEDULER_Handle *sched,
580 const struct GNUNET_CONFIGURATION_Handle *cfg)
582 struct CpsRunContext *crc;
584 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
588 now = GNUNET_TIME_absolute_get ();
589 datastore = GNUNET_DATASTORE_connect (cfg, sched);
590 GNUNET_SCHEDULER_add_continuation (crc->sched,
593 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
606 char *const argv[] = {
607 "test-datastore-api",
615 struct GNUNET_GETOPT_CommandLineOption options[] = {
616 GNUNET_GETOPT_OPTION_END
618 GNUNET_snprintf (cfg_name,
620 "test_datastore_api_data_%s.conf",
623 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
624 "gnunet-service-arm",
628 "-c", cfg_name, NULL);
630 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
631 argv, "test-datastore-api", "nohelp",
632 options, &run, NULL);
634 if (0 != PLIBC_KILL (pid, SIGTERM))
636 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
639 GNUNET_OS_process_wait(pid);
642 fprintf (stderr, "Missed some testcases: %u\n", ok);
647 main (int argc, char *argv[])
653 /* determine name of plugin to use */
654 plugin_name = argv[0];
655 while (NULL != (pos = strstr(plugin_name, "_")))
658 GNUNET_snprintf (dir_name,
660 "/tmp/test-gnunet-datastore-%s",
662 GNUNET_DISK_directory_remove (dir_name);
663 GNUNET_log_setup ("test-datastore-api",
671 GNUNET_DISK_directory_remove (dir_name);
675 /* end of test_datastore_api.c */