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];
68 memset (buf, i, 8 * i);
94 static struct GNUNET_TIME_Absolute
95 get_expiration (int i)
97 struct GNUNET_TIME_Absolute av;
99 av.abs_value = now.abs_value + 20000000 - i * 1000;
113 RP_PUT_MULTIPLE_NEXT = 8,
115 RP_GET_MULTIPLE_NEXT = 10,
117 RP_UPDATE_VALIDATE = 12,
127 const struct GNUNET_CONFIGURATION_Handle *cfg;
138 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
142 check_success (void *cls, int success, const char *msg)
144 struct CpsRunContext *crc = cls;
146 if (GNUNET_OK != success)
148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
149 "Operation %d/%d not successfull: `%s'\n",
150 crc->phase, crc->i, msg);
151 crc->phase = RP_ERROR;
153 GNUNET_free_non_null (crc->data);
155 GNUNET_SCHEDULER_add_continuation (&run_continuation,
156 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
161 get_reserved (void *cls, int success, const char *msg)
163 struct CpsRunContext *crc = cls;
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167 "Error obtaining reservation: `%s'\n", msg);
168 GNUNET_assert (0 < success);
170 GNUNET_SCHEDULER_add_continuation (&run_continuation,
171 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
176 check_value (void *cls,
177 const GNUNET_HashCode * key,
180 enum GNUNET_BLOCK_Type type,
183 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
185 struct CpsRunContext *crc = cls;
191 "Check value got `%s' of size %u, type %d, expire %llu\n",
194 type, (unsigned long long) expiration.abs_value);
196 "Check value iteration %d wants size %u, type %d, expire %llu\n",
198 (unsigned int) get_size (i),
199 get_type (i), (unsigned long long) get_expiration (i).abs_value);
201 GNUNET_assert (size == get_size (i));
202 GNUNET_assert (0 == memcmp (data, get_data (i), size));
203 GNUNET_assert (type == get_type (i));
204 GNUNET_assert (priority == get_priority (i));
205 GNUNET_assert (anonymity == get_anonymity (i));
206 GNUNET_assert (expiration.abs_value == get_expiration (i).abs_value);
213 GNUNET_SCHEDULER_add_continuation (&run_continuation,
214 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
219 delete_value (void *cls,
220 const GNUNET_HashCode * key,
223 enum GNUNET_BLOCK_Type type,
226 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
228 struct CpsRunContext *crc = cls;
230 GNUNET_assert (crc->data == NULL);
231 GNUNET_assert (NULL != key);
234 crc->data = GNUNET_malloc (size);
235 memcpy (crc->data, data, size);
236 crc->phase = RP_DO_DEL;
237 GNUNET_SCHEDULER_add_continuation (&run_continuation,
238 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
243 check_nothing (void *cls,
244 const GNUNET_HashCode * key,
247 enum GNUNET_BLOCK_Type type,
250 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
252 struct CpsRunContext *crc = cls;
254 GNUNET_assert (key == NULL);
256 crc->phase = RP_RESERVE;
257 GNUNET_SCHEDULER_add_continuation (&run_continuation,
258 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
263 check_multiple (void *cls,
264 const GNUNET_HashCode * key,
267 enum GNUNET_BLOCK_Type type,
270 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
272 struct CpsRunContext *crc = cls;
274 GNUNET_assert (key != NULL);
277 case RP_GET_MULTIPLE:
278 crc->phase = RP_GET_MULTIPLE_NEXT;
279 crc->first_uid = uid;
282 case RP_GET_MULTIPLE_NEXT:
283 GNUNET_assert (uid != crc->first_uid);
284 crc->phase = RP_UPDATE;
288 crc->phase = RP_ERROR;
291 if (priority == get_priority (42))
293 GNUNET_SCHEDULER_add_continuation (&run_continuation,
294 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
299 check_update (void *cls,
300 const GNUNET_HashCode * key,
303 enum GNUNET_BLOCK_Type type,
306 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
308 struct CpsRunContext *crc = cls;
310 GNUNET_assert (key != NULL);
311 if ((anonymity == get_anonymity (42)) &&
312 (size == get_size (42)) && (priority == get_priority (42) + 100))
313 crc->phase = RP_DONE;
316 GNUNET_assert (size == get_size (43));
319 GNUNET_SCHEDULER_add_continuation (&run_continuation,
320 crc, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
325 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
327 struct CpsRunContext *crc = cls;
329 ok = (int) crc->phase;
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test in phase %u\n", crc->phase);
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Executing `%s' number %u\n", "PUT", crc->i);
340 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
341 GNUNET_DATASTORE_put (datastore,
347 get_priority (crc->i),
348 get_anonymity (crc->i),
350 get_expiration (crc->i),
351 1, 1, TIMEOUT, &check_success, crc);
353 if (crc->i == ITERATIONS)
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Executing `%s' number %u\n", "GET", crc->i);
362 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
363 GNUNET_DATASTORE_get_key (datastore,
367 1, 1, TIMEOUT, &check_value, crc);
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Executing `%s' number %u\n", "DEL", crc->i);
376 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
377 GNUNET_assert (NULL !=
378 GNUNET_DATASTORE_get_key (datastore,
383 &delete_value, crc));
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Executing `%s' number %u\n", "DO_DEL", crc->i);
393 crc->phase = RP_DELVALIDATE;
399 GNUNET_assert (NULL !=
400 GNUNET_DATASTORE_remove (datastore,
405 &check_success, crc));
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Executing `%s' number %u\n", "DEL-VALIDATE", crc->i);
413 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
414 GNUNET_assert (NULL !=
415 GNUNET_DATASTORE_get_key (datastore,
420 &check_nothing, crc));
423 crc->phase = RP_PUT_MULTIPLE;
424 GNUNET_DATASTORE_reserve (datastore,
425 128 * 1024, 2, 1, 1, TIMEOUT, &get_reserved, crc);
427 case RP_PUT_MULTIPLE:
428 crc->phase = RP_PUT_MULTIPLE_NEXT;
429 GNUNET_DATASTORE_put (datastore,
439 1, 1, TIMEOUT, &check_success, crc);
441 case RP_PUT_MULTIPLE_NEXT:
442 crc->phase = RP_GET_MULTIPLE;
443 GNUNET_DATASTORE_put (datastore,
453 1, 1, TIMEOUT, &check_success, crc);
455 case RP_GET_MULTIPLE:
456 GNUNET_assert (NULL !=
457 GNUNET_DATASTORE_get_key (datastore,
462 &check_multiple, crc));
464 case RP_GET_MULTIPLE_NEXT:
465 GNUNET_assert (NULL !=
466 GNUNET_DATASTORE_get_key (datastore,
471 &check_multiple, crc));
474 GNUNET_assert (crc->uid > 0);
475 crc->phase = RP_UPDATE_VALIDATE;
476 GNUNET_DATASTORE_update (datastore,
480 1, 1, TIMEOUT, &check_success, crc);
482 case RP_UPDATE_VALIDATE:
483 GNUNET_assert (NULL !=
484 GNUNET_DATASTORE_get_key (datastore,
489 &check_update, crc));
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
495 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
500 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
509 run_tests (void *cls, int32_t success, const char *msg)
511 struct CpsRunContext *crc = cls;
516 GNUNET_SCHEDULER_add_continuation (&run_continuation,
518 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
521 fprintf (stderr, "Test 'put' operation failed, key already exists (!?)\n");
526 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
539 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
541 struct CpsRunContext *crc;
542 static GNUNET_HashCode zkey;
544 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
547 now = GNUNET_TIME_absolute_get ();
548 datastore = GNUNET_DATASTORE_connect (cfg);
550 GNUNET_DATASTORE_put (datastore, 0,
552 GNUNET_BLOCK_TYPE_TEST,
554 GNUNET_TIME_relative_to_absolute
555 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
556 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
558 fprintf (stderr, "Test 'put' operation failed.\n");
571 struct GNUNET_OS_Process *proc;
573 char *const argv[] = {
574 "test-datastore-api",
582 struct GNUNET_GETOPT_CommandLineOption options[] = {
583 GNUNET_GETOPT_OPTION_END
585 GNUNET_snprintf (cfg_name,
587 "test_datastore_api_data_%s.conf", plugin_name);
589 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
590 "gnunet-service-arm",
594 "-c", cfg_name, NULL);
596 GNUNET_assert (NULL != proc);
597 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
598 argv, "test-datastore-api", "nohelp",
599 options, &run, NULL);
601 sleep (1); /* give datastore chance to receive 'DROP' request */
602 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
604 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
607 GNUNET_OS_process_wait (proc);
608 GNUNET_OS_process_close (proc);
612 fprintf (stderr, "Missed some testcases: %u\n", ok);
617 main (int argc, char *argv[])
624 /* determine name of plugin to use */
625 plugin_name = argv[0];
626 while (NULL != (pos = strstr (plugin_name, "_")))
627 plugin_name = pos + 1;
628 if (NULL != (pos = strstr (plugin_name, ".")))
631 pos = (char *) plugin_name;
633 GNUNET_snprintf (dir_name,
635 "/tmp/test-gnunet-datastore-%s", plugin_name);
636 GNUNET_DISK_directory_remove (dir_name);
637 GNUNET_log_setup ("test-datastore-api",
645 if (pos != plugin_name)
647 GNUNET_DISK_directory_remove (dir_name);
651 /* end of test_datastore_api.c */