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_management.c
22 * @brief Test for the space management functions of the datastore implementation.
23 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_service.h"
31 #define VERBOSE GNUNET_NO
34 * How long until we give up on transmitting the message?
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
39 * Number of iterations to run; must be large enough
40 * so that the quota will be exceeded!
42 #define ITERATIONS 5000
44 static struct GNUNET_DATASTORE_Handle *datastore;
46 static struct GNUNET_TIME_Absolute now;
50 static const char* plugin_name;
55 return 8 + 8 * (i % 256);
62 static char buf[60000];
63 memset (buf, i, 8 + 8 * (i % 256));
89 static struct GNUNET_TIME_Absolute
90 get_expiration (int i)
92 struct GNUNET_TIME_Absolute av;
94 av.abs_value = now.abs_value + i * 1000;
112 struct GNUNET_SCHEDULER_Handle *sched;
113 const struct GNUNET_CONFIGURATION_Handle *cfg;
120 run_continuation (void *cls,
121 const struct GNUNET_SCHEDULER_TaskContext *tc);
125 check_success (void *cls,
129 struct CpsRunContext *crc = cls;
130 if (GNUNET_OK != success)
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 GNUNET_assert (GNUNET_OK == success);
134 GNUNET_free_non_null (crc->data);
136 GNUNET_SCHEDULER_add_continuation (crc->sched,
139 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
144 check_value (void *cls,
145 const GNUNET_HashCode * key,
148 enum GNUNET_BLOCK_Type type,
151 struct GNUNET_TIME_Absolute
152 expiration, uint64_t uid)
154 struct CpsRunContext *crc = cls;
160 if (crc->found == GNUNET_YES)
163 crc->found = GNUNET_NO;
168 "First not found was %u\n", crc->i);
169 crc->phase = RP_GET_FAIL;
172 crc->phase = RP_DONE;
173 GNUNET_SCHEDULER_add_continuation (crc->sched,
176 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
180 crc->found = GNUNET_YES;
181 GNUNET_assert (size == get_size (i));
182 GNUNET_assert (0 == memcmp (data, get_data(i), size));
183 GNUNET_assert (type == get_type (i));
184 GNUNET_assert (priority == get_priority (i));
185 GNUNET_assert (anonymity == get_anonymity(i));
186 GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value);
187 GNUNET_DATASTORE_get_next (datastore, GNUNET_YES);
192 check_nothing (void *cls,
193 const GNUNET_HashCode * key,
196 enum GNUNET_BLOCK_Type type,
199 struct GNUNET_TIME_Absolute
200 expiration, uint64_t uid)
202 struct CpsRunContext *crc = cls;
204 GNUNET_assert (key == NULL);
206 crc->phase = RP_DONE;
207 GNUNET_SCHEDULER_add_continuation (crc->sched,
210 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
215 run_continuation (void *cls,
216 const struct GNUNET_SCHEDULER_TaskContext *tc)
218 struct CpsRunContext *crc = cls;
219 ok = (int) crc->phase;
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Executing `%s' number %u\n",
229 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
230 GNUNET_DATASTORE_put (datastore,
236 get_priority (crc->i),
237 get_anonymity (crc->i),
238 get_expiration (crc->i),
243 if (crc->i == ITERATIONS)
245 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
246 "Sleeping to give datastore time to clean up\n");
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Executing `%s' number %u\n",
259 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
260 GNUNET_DATASTORE_get (datastore,
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "Executing `%s' number %u\n",
274 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
275 GNUNET_DATASTORE_get (datastore,
283 GNUNET_assert (0 == crc->i);
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286 "Finished, disconnecting\n");
288 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
296 run_tests (void *cls,
300 struct CpsRunContext *crc = cls;
302 if (success != GNUNET_YES)
305 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
310 GNUNET_SCHEDULER_add_continuation (crc->sched,
313 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
319 struct GNUNET_SCHEDULER_Handle *sched,
322 const struct GNUNET_CONFIGURATION_Handle *cfg)
324 struct CpsRunContext *crc;
325 static GNUNET_HashCode zkey;
327 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
331 now = GNUNET_TIME_absolute_get ();
332 datastore = GNUNET_DATASTORE_connect (cfg, sched);
334 GNUNET_DATASTORE_put (datastore, 0,
336 GNUNET_BLOCK_TYPE_TEST,
337 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
338 0, 1, GNUNET_TIME_UNIT_MINUTES,
342 "Test 'put' operation failed.\n");
353 GNUNET_OS_Process *proc;
355 char *const argv[] = {
356 "test-datastore-api-management",
364 struct GNUNET_GETOPT_CommandLineOption options[] = {
365 GNUNET_GETOPT_OPTION_END
367 GNUNET_snprintf (cfg_name,
369 "test_datastore_api_data_%s.conf",
371 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
372 "gnunet-service-arm",
376 "-c", cfg_name, NULL);
377 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
378 argv, "test-datastore-api", "nohelp",
379 options, &run, NULL);
380 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
382 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
385 GNUNET_OS_process_wait (proc);
386 GNUNET_OS_process_close (proc);
389 fprintf (stderr, "Missed some testcases: %u\n", ok);
394 main (int argc, char *argv[])
401 /* determine name of plugin to use */
402 plugin_name = argv[0];
403 while (NULL != (pos = strstr(plugin_name, "_")))
406 GNUNET_snprintf (dir_name,
408 "/tmp/test-gnunet-datastore-%s",
410 GNUNET_DISK_directory_remove (dir_name);
411 GNUNET_log_setup ("test-datastore-api-management",
419 GNUNET_DISK_directory_remove (dir_name);
423 /* end of test_datastore_api_management.c */