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 %u not found!\n",
234 crc->phase = RP_ERROR;
238 crc->phase = RP_DO_DEL;
240 GNUNET_SCHEDULER_add_continuation (crc->sched,
243 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
246 GNUNET_assert (crc->data == NULL);
249 crc->data = GNUNET_malloc (size);
250 memcpy (crc->data, data, size);
251 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
256 check_nothing (void *cls,
257 const GNUNET_HashCode * key,
260 enum GNUNET_BLOCK_Type type,
263 struct GNUNET_TIME_Absolute
264 expiration, uint64_t uid)
266 struct CpsRunContext *crc = cls;
267 GNUNET_assert (key == NULL);
270 crc->phase = RP_RESERVE;
272 GNUNET_SCHEDULER_add_continuation (crc->sched,
275 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
280 check_multiple (void *cls,
281 const GNUNET_HashCode * key,
284 enum GNUNET_BLOCK_Type type,
287 struct GNUNET_TIME_Absolute
288 expiration, uint64_t uid)
290 struct CpsRunContext *crc = cls;
294 GNUNET_assert (crc->phase == RP_GET_MULTIPLE_DONE);
295 crc->phase = RP_UPDATE;
296 GNUNET_SCHEDULER_add_continuation (crc->sched,
299 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
303 if (priority == get_priority (42))
305 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
310 check_update (void *cls,
311 const GNUNET_HashCode * key,
314 enum GNUNET_BLOCK_Type type,
317 struct GNUNET_TIME_Absolute
318 expiration, uint64_t uid)
320 struct CpsRunContext *crc = cls;
324 GNUNET_assert (crc->phase == RP_UPDATE_DONE);
325 crc->phase = RP_DONE;
326 GNUNET_SCHEDULER_add_continuation (crc->sched,
329 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
332 if ( (anonymity == get_anonymity (42)) &&
333 (size == get_size (42)) &&
334 (priority == get_priority (42) + 100) )
336 crc->phase = RP_UPDATE_DONE;
339 GNUNET_assert (size == get_size (43));
340 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
345 run_continuation (void *cls,
346 const struct GNUNET_SCHEDULER_TaskContext *tc)
348 struct CpsRunContext *crc = cls;
349 ok = (int) crc->phase;
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Test in phase %u\n", crc->phase);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "Executing `%s' number %u\n",
363 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
364 GNUNET_DATASTORE_put (datastore,
370 get_priority (crc->i),
371 get_anonymity (crc->i),
372 get_expiration (crc->i),
377 if (crc->i == ITERATIONS)
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Executing `%s' number %u\n",
388 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
389 GNUNET_DATASTORE_get (datastore,
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Executing `%s' number %u\n",
405 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
406 GNUNET_DATASTORE_get (datastore,
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 "Executing `%s' number %u\n",
423 crc->phase = RP_DELVALIDATE;
429 GNUNET_DATASTORE_remove (datastore,
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Executing `%s' number %u\n",
445 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
446 GNUNET_DATASTORE_get (datastore,
454 crc->phase = RP_PUT_MULTIPLE;
455 GNUNET_DATASTORE_reserve (datastore,
462 case RP_PUT_MULTIPLE:
463 crc->phase = RP_PUT_MULTIPLE_NEXT;
464 GNUNET_DATASTORE_put (datastore,
477 case RP_PUT_MULTIPLE_NEXT:
478 crc->phase = RP_GET_MULTIPLE;
479 GNUNET_DATASTORE_put (datastore,
492 case RP_GET_MULTIPLE:
493 GNUNET_DATASTORE_get (datastore,
500 case RP_GET_MULTIPLE_NEXT:
501 case RP_GET_MULTIPLE_DONE:
505 GNUNET_assert (crc->uid > 0);
506 crc->phase = RP_UPDATE_VALIDATE;
507 GNUNET_DATASTORE_update (datastore,
515 case RP_UPDATE_VALIDATE:
516 GNUNET_DATASTORE_get (datastore,
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "Finished, disconnecting\n");
531 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
536 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
546 struct GNUNET_SCHEDULER_Handle *sched,
549 const struct GNUNET_CONFIGURATION_Handle *cfg)
551 struct CpsRunContext *crc;
553 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
557 now = GNUNET_TIME_absolute_get ();
558 datastore = GNUNET_DATASTORE_connect (cfg, sched);
559 GNUNET_SCHEDULER_add_continuation (crc->sched,
562 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
572 char *const argv[] = { "test-datastore-api",
574 "test_datastore_api_data.conf",
580 struct GNUNET_GETOPT_CommandLineOption options[] = {
581 GNUNET_GETOPT_OPTION_END
583 pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
584 "gnunet-service-arm",
588 "-c", "test_datastore_api_data.conf", NULL);
589 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
590 argv, "test-datastore-api", "nohelp",
591 options, &run, NULL);
592 if (0 != PLIBC_KILL (pid, SIGTERM))
594 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
597 GNUNET_OS_process_wait(pid);
599 fprintf (stderr, "Missed some testcases: %u\n", ok);
604 main (int argc, char *argv[])
608 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
609 GNUNET_log_setup ("test-datastore-api",
617 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-datastore");
624 /* end of test_datastore_api.c */