2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2007, 2009, 2011 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];
64 memset (buf, i, 8 + 8 * (i % 256));
90 static struct GNUNET_TIME_Absolute
91 get_expiration (int i)
93 struct GNUNET_TIME_Absolute av;
95 av.abs_value = now.abs_value + i * 1000;
113 const struct GNUNET_CONFIGURATION_Handle *cfg;
121 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
125 check_success (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
127 struct CpsRunContext *crc = cls;
129 if (GNUNET_OK != success)
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", msg);
131 GNUNET_assert (GNUNET_OK == success);
132 GNUNET_free_non_null (crc->data);
134 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
135 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
140 check_value (void *cls, const GNUNET_HashCode * key, size_t size,
141 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
142 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
145 struct CpsRunContext *crc = cls;
150 crc->phase = RP_GET_FAIL;
151 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
152 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
156 GNUNET_assert (size == get_size (i));
157 GNUNET_assert (0 == memcmp (data, get_data (i), size));
158 GNUNET_assert (type == get_type (i));
159 GNUNET_assert (priority == get_priority (i));
160 GNUNET_assert (anonymity == get_anonymity (i));
161 GNUNET_assert (expiration.abs_value == get_expiration (i).abs_value);
165 crc->phase = RP_DONE;
166 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
167 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
172 check_nothing (void *cls, const GNUNET_HashCode * key, size_t size,
173 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
174 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
177 struct CpsRunContext *crc = cls;
179 GNUNET_assert (key == NULL);
181 crc->phase = RP_DONE;
182 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
183 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
188 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 struct CpsRunContext *crc = cls;
192 ok = (int) crc->phase;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
200 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
201 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
202 get_data (crc->i), get_type (crc->i),
203 get_priority (crc->i), get_anonymity (crc->i), 0,
204 get_expiration (crc->i), 1, 1, TIMEOUT,
205 &check_success, crc);
207 if (crc->i == ITERATIONS)
209 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
210 "Sleeping to give datastore time to clean up\n");
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
221 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
222 GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
223 get_type (crc->i), 1, 1, TIMEOUT, &check_value,
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET(f)",
231 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
232 GNUNET_DATASTORE_get_key (datastore, crc->offset++, &crc->key,
233 get_type (crc->i), 1, 1, TIMEOUT, &check_nothing,
237 GNUNET_assert (0 == crc->i);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
241 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
249 run_tests (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
251 struct CpsRunContext *crc = cls;
253 if (success != GNUNET_YES)
256 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
258 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
262 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
263 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
268 run (void *cls, char *const *args, const char *cfgfile,
269 const struct GNUNET_CONFIGURATION_Handle *cfg)
271 struct CpsRunContext *crc;
272 static GNUNET_HashCode zkey;
274 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
277 now = GNUNET_TIME_absolute_get ();
278 datastore = GNUNET_DATASTORE_connect (cfg);
280 GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST",
281 GNUNET_BLOCK_TYPE_TEST, 0, 0, 0,
282 GNUNET_TIME_relative_to_absolute
283 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
284 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
286 FPRINTF (stderr, "%s", "Test 'put' operation failed.\n");
297 struct GNUNET_OS_Process *proc;
300 char *const argv[] = {
301 "test-datastore-api-management",
309 struct GNUNET_GETOPT_CommandLineOption options[] = {
310 GNUNET_GETOPT_OPTION_END
312 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
313 "test_datastore_api_data_%s.conf", plugin_name);
315 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
316 "gnunet-service-arm",
320 "-c", cfg_name, NULL);
321 GNUNET_assert (NULL != proc);
322 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
323 "test-datastore-api-management", "nohelp", options, &run,
325 sleep (1); /* give datastore chance to process 'DROP' request */
326 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
328 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
331 GNUNET_OS_process_wait (proc);
332 GNUNET_OS_process_close (proc);
335 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
340 main (int argc, char *argv[])
348 /* determine name of plugin to use */
349 plugin_name = argv[0];
350 while (NULL != (pos = strstr (plugin_name, "_")))
351 plugin_name = pos + 1;
352 if (NULL != (pos = strstr (plugin_name, ".")))
355 pos = (char *) plugin_name;
357 GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/test-gnunet-datastore-%s",
359 GNUNET_DISK_directory_remove (dir_name);
360 GNUNET_log_setup ("test-datastore-api-management",
368 if (pos != plugin_name)
370 GNUNET_DISK_directory_remove (dir_name);
374 /* end of test_datastore_api_management.c */