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, struct GNUNET_TIME_Absolute min_expiration, 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", crc->phase, crc->i,
151 crc->phase = RP_ERROR;
153 GNUNET_free_non_null (crc->data);
155 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
156 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
161 get_reserved (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
163 struct CpsRunContext *crc = cls;
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error obtaining reservation: `%s'\n",
168 GNUNET_assert (0 < success);
170 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
171 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
176 check_value (void *cls, const GNUNET_HashCode * key, size_t size,
177 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
178 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
181 struct CpsRunContext *crc = cls;
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
188 "Value check failed (got NULL key) in %d/%d\n", crc->phase,
190 crc->phase = RP_ERROR;
191 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
192 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
196 FPRINTF (stderr, "Check value got `%s' of size %u, type %d, expire %llu\n",
197 GNUNET_h2s (key), (unsigned int) size, type,
198 (unsigned long long) expiration.abs_value);
200 "Check value iteration %d wants size %u, type %d, expire %llu\n", i,
201 (unsigned int) get_size (i), get_type (i),
202 (unsigned long long) get_expiration (i).abs_value);
204 GNUNET_assert (size == get_size (i));
205 GNUNET_assert (0 == memcmp (data, get_data (i), size));
206 GNUNET_assert (type == get_type (i));
207 GNUNET_assert (priority == get_priority (i));
208 GNUNET_assert (anonymity == get_anonymity (i));
209 GNUNET_assert (expiration.abs_value == get_expiration (i).abs_value);
216 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
217 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
222 delete_value (void *cls, const GNUNET_HashCode * key, size_t size,
223 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
224 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
227 struct CpsRunContext *crc = cls;
229 GNUNET_assert (crc->data == NULL);
230 GNUNET_assert (NULL != key);
233 crc->data = GNUNET_malloc (size);
234 memcpy (crc->data, data, size);
235 crc->phase = RP_DO_DEL;
236 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
237 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
242 check_nothing (void *cls, const GNUNET_HashCode * key, size_t size,
243 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
244 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
247 struct CpsRunContext *crc = cls;
249 GNUNET_assert (key == NULL);
251 crc->phase = RP_RESERVE;
252 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
253 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
258 check_multiple (void *cls, const GNUNET_HashCode * key, size_t size,
259 const void *data, enum GNUNET_BLOCK_Type type,
260 uint32_t priority, uint32_t anonymity,
261 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
263 struct CpsRunContext *crc = cls;
265 GNUNET_assert (key != NULL);
268 case RP_GET_MULTIPLE:
269 crc->phase = RP_GET_MULTIPLE_NEXT;
270 crc->first_uid = uid;
273 case RP_GET_MULTIPLE_NEXT:
274 GNUNET_assert (uid != crc->first_uid);
275 crc->phase = RP_UPDATE;
279 crc->phase = RP_ERROR;
282 if (priority == get_priority (42))
284 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
285 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
290 check_update (void *cls, const GNUNET_HashCode * key, size_t size,
291 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
292 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
295 struct CpsRunContext *crc = cls;
297 GNUNET_assert (key != NULL);
298 if ((anonymity == get_anonymity (42)) && (size == get_size (42)) &&
299 (priority == get_priority (42) + 100))
300 crc->phase = RP_DONE;
303 GNUNET_assert (size == get_size (43));
306 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
307 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
312 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
314 struct CpsRunContext *crc = cls;
316 ok = (int) crc->phase;
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test in phase %u\n", crc->phase);
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
327 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
328 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
329 get_data (crc->i), get_type (crc->i),
330 get_priority (crc->i), get_anonymity (crc->i), 0,
331 get_expiration (crc->i), 1, 1, TIMEOUT,
332 &check_success, crc);
334 if (crc->i == ITERATIONS)
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
343 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
344 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
345 get_type (crc->i), 1, 1, TIMEOUT, &check_value,
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "DEL",
355 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
356 GNUNET_assert (NULL !=
357 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
358 get_type (crc->i), 1, 1, TIMEOUT,
359 &delete_value, crc));
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "DO_DEL",
369 crc->phase = RP_DELVALIDATE;
375 GNUNET_assert (NULL !=
376 GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
377 crc->data, 1, 1, TIMEOUT,
378 &check_success, crc));
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n",
384 "DEL-VALIDATE", crc->i);
386 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
387 GNUNET_assert (NULL !=
388 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
389 get_type (crc->i), 1, 1, TIMEOUT,
390 &check_nothing, crc));
393 crc->phase = RP_PUT_MULTIPLE;
394 GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2, 1, 1, TIMEOUT,
397 case RP_PUT_MULTIPLE:
398 crc->phase = RP_PUT_MULTIPLE_NEXT;
399 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
400 get_data (42), get_type (42), get_priority (42),
401 get_anonymity (42), 0, get_expiration (42), 1, 1,
402 TIMEOUT, &check_success, crc);
404 case RP_PUT_MULTIPLE_NEXT:
405 crc->phase = RP_GET_MULTIPLE;
406 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (43),
407 get_data (43), get_type (42), get_priority (43),
408 get_anonymity (43), 0, get_expiration (43), 1, 1,
409 TIMEOUT, &check_success, crc);
411 case RP_GET_MULTIPLE:
412 GNUNET_assert (NULL !=
413 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
414 get_type (42), 1, 1, TIMEOUT,
415 &check_multiple, crc));
417 case RP_GET_MULTIPLE_NEXT:
418 GNUNET_assert (NULL !=
419 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
420 get_type (42), 1, 1, TIMEOUT,
421 &check_multiple, crc));
424 GNUNET_assert (crc->uid > 0);
425 crc->phase = RP_UPDATE_VALIDATE;
426 GNUNET_DATASTORE_update (datastore, crc->uid, 100, get_expiration (42), 1,
427 1, TIMEOUT, &check_success, crc);
429 case RP_UPDATE_VALIDATE:
430 GNUNET_assert (NULL !=
431 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
432 get_type (42), 1, 1, TIMEOUT,
433 &check_update, crc));
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
439 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
444 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
453 run_tests (void *cls, int32_t success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
455 struct CpsRunContext *crc = cls;
460 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
461 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
464 FPRINTF (stderr, "%s", "Test 'put' operation failed, key already exists (!?)\n");
465 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
470 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
472 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
482 run (void *cls, char *const *args, const char *cfgfile,
483 const struct GNUNET_CONFIGURATION_Handle *cfg)
485 struct CpsRunContext *crc;
486 static GNUNET_HashCode zkey;
488 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
491 now = GNUNET_TIME_absolute_get ();
492 datastore = GNUNET_DATASTORE_connect (cfg);
494 GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST",
495 GNUNET_BLOCK_TYPE_TEST, 0, 0, 0,
496 GNUNET_TIME_relative_to_absolute
497 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
498 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
500 FPRINTF (stderr, "%s", "Test 'put' operation failed.\n");
513 struct GNUNET_OS_Process *proc;
515 char *const argv[] = {
516 "test-datastore-api",
524 struct GNUNET_GETOPT_CommandLineOption options[] = {
525 GNUNET_GETOPT_OPTION_END
527 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
528 "test_datastore_api_data_%s.conf", plugin_name);
531 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
532 "gnunet-service-arm",
536 "-c", cfg_name, NULL);
538 GNUNET_assert (NULL != proc);
539 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
540 "test-datastore-api", "nohelp", options, &run, NULL);
542 sleep (1); /* give datastore chance to receive 'DROP' request */
543 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
545 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
548 GNUNET_OS_process_wait (proc);
549 GNUNET_OS_process_destroy (proc);
553 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
558 main (int argc, char *argv[])
565 /* determine name of plugin to use */
566 plugin_name = argv[0];
567 while (NULL != (pos = strstr (plugin_name, "_")))
568 plugin_name = pos + 1;
569 if (NULL != (pos = strstr (plugin_name, ".")))
572 pos = (char *) plugin_name;
574 GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/test-gnunet-datastore-%s",
576 GNUNET_DISK_directory_remove (dir_name);
577 GNUNET_log_setup ("test-datastore-api",
585 if (pos != plugin_name)
587 GNUNET_DISK_directory_remove (dir_name);
591 /* end of test_datastore_api.c */