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 const struct GNUNET_CONFIGURATION_Handle *cfg;
119 run_continuation (void *cls,
120 const struct GNUNET_SCHEDULER_TaskContext *tc);
124 check_success (void *cls,
128 struct CpsRunContext *crc = cls;
129 if (GNUNET_OK != success)
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132 GNUNET_assert (GNUNET_OK == success);
133 GNUNET_free_non_null (crc->data);
135 GNUNET_SCHEDULER_add_continuation (&run_continuation,
137 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
142 check_value (void *cls,
143 const GNUNET_HashCode * key,
146 enum GNUNET_BLOCK_Type type,
149 struct GNUNET_TIME_Absolute
150 expiration, uint64_t uid)
152 struct CpsRunContext *crc = cls;
158 if (crc->found == GNUNET_YES)
161 crc->found = GNUNET_NO;
166 "First not found was %u\n", crc->i);
167 crc->phase = RP_GET_FAIL;
170 crc->phase = RP_DONE;
171 GNUNET_SCHEDULER_add_continuation (&run_continuation,
173 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
177 crc->found = GNUNET_YES;
178 GNUNET_assert (size == get_size (i));
179 GNUNET_assert (0 == memcmp (data, get_data(i), size));
180 GNUNET_assert (type == get_type (i));
181 GNUNET_assert (priority == get_priority (i));
182 GNUNET_assert (anonymity == get_anonymity(i));
183 GNUNET_assert (expiration.abs_value == get_expiration(i).abs_value);
184 GNUNET_DATASTORE_iterate_get_next (datastore);
189 check_nothing (void *cls,
190 const GNUNET_HashCode * key,
193 enum GNUNET_BLOCK_Type type,
196 struct GNUNET_TIME_Absolute
197 expiration, uint64_t uid)
199 struct CpsRunContext *crc = cls;
201 GNUNET_assert (key == NULL);
203 crc->phase = RP_DONE;
204 GNUNET_SCHEDULER_add_continuation (&run_continuation,
206 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
211 run_continuation (void *cls,
212 const struct GNUNET_SCHEDULER_TaskContext *tc)
214 struct CpsRunContext *crc = cls;
215 ok = (int) crc->phase;
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Executing `%s' number %u\n",
225 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
226 GNUNET_DATASTORE_put (datastore,
232 get_priority (crc->i),
233 get_anonymity (crc->i),
235 get_expiration (crc->i),
240 if (crc->i == ITERATIONS)
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
243 "Sleeping to give datastore time to clean up\n");
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Executing `%s' number %u\n",
256 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
257 GNUNET_DATASTORE_iterate_key (datastore,
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267 "Executing `%s' number %u\n",
271 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
272 GNUNET_DATASTORE_iterate_key (datastore,
280 GNUNET_assert (0 == crc->i);
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Finished, disconnecting\n");
285 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
293 run_tests (void *cls,
297 struct CpsRunContext *crc = cls;
299 if (success != GNUNET_YES)
302 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.",
307 GNUNET_SCHEDULER_add_continuation (&run_continuation,
309 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
317 const struct GNUNET_CONFIGURATION_Handle *cfg)
319 struct CpsRunContext *crc;
320 static GNUNET_HashCode zkey;
322 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
325 now = GNUNET_TIME_absolute_get ();
326 datastore = GNUNET_DATASTORE_connect (cfg);
328 GNUNET_DATASTORE_put (datastore, 0,
330 GNUNET_BLOCK_TYPE_TEST,
331 0, 0, 0, GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_SECONDS),
332 0, 1, GNUNET_TIME_UNIT_MINUTES,
336 "Test 'put' operation failed.\n");
347 struct GNUNET_OS_Process *proc;
349 char *const argv[] = {
350 "test-datastore-api-management",
358 struct GNUNET_GETOPT_CommandLineOption options[] = {
359 GNUNET_GETOPT_OPTION_END
361 GNUNET_snprintf (cfg_name,
363 "test_datastore_api_data_%s.conf",
365 proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
366 "gnunet-service-arm",
370 "-c", cfg_name, NULL);
371 GNUNET_assert (NULL != proc);
372 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
373 argv, "test-datastore-api", "nohelp",
374 options, &run, NULL);
375 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
377 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
380 GNUNET_OS_process_wait (proc);
381 GNUNET_OS_process_close (proc);
384 fprintf (stderr, "Missed some testcases: %u\n", ok);
389 main (int argc, char *argv[])
396 /* determine name of plugin to use */
397 plugin_name = argv[0];
398 while (NULL != (pos = strstr(plugin_name, "_")))
400 if (NULL != (pos = strstr(plugin_name, ".")))
403 pos = (char *) plugin_name;
405 GNUNET_snprintf (dir_name,
407 "/tmp/test-gnunet-datastore-%s",
409 GNUNET_DISK_directory_remove (dir_name);
410 GNUNET_log_setup ("test-datastore-api-management",
418 if (pos != plugin_name)
420 GNUNET_DISK_directory_remove (dir_name);
424 /* end of test_datastore_api_management.c */