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"
33 #include "gnunet_testing_lib.h"
37 * How long until we give up on transmitting the message?
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
41 #define ITERATIONS 256
43 static struct GNUNET_DATASTORE_Handle *datastore;
45 static struct GNUNET_TIME_Absolute now;
50 * Name of plugin under test.
52 static const char *plugin_name;
64 static char buf[60000];
66 memset (buf, i, 8 * i);
92 static struct GNUNET_TIME_Absolute
93 get_expiration (int i)
95 struct GNUNET_TIME_Absolute av;
97 av.abs_value_us = now.abs_value_us + 20000000000LL - i * 1000 * 1000LL;
111 RP_PUT_MULTIPLE_NEXT = 8,
113 RP_GET_MULTIPLE_NEXT = 10,
115 RP_UPDATE_VALIDATE = 12,
122 struct GNUNET_HashCode key;
125 const struct GNUNET_CONFIGURATION_Handle *cfg;
136 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
140 check_success (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
142 struct CpsRunContext *crc = cls;
144 if (GNUNET_OK != success)
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
147 "Operation %d/%d not successfull: `%s'\n", crc->phase, crc->i,
149 crc->phase = RP_ERROR;
151 GNUNET_free_non_null (crc->data);
153 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
154 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
159 get_reserved (void *cls, int success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
161 struct CpsRunContext *crc = cls;
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error obtaining reservation: `%s'\n",
166 GNUNET_assert (0 < success);
168 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
169 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
174 check_value (void *cls, const struct GNUNET_HashCode * key, size_t size,
175 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
176 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
179 struct CpsRunContext *crc = cls;
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 "Value check failed (got NULL key) in %d/%d\n", crc->phase,
188 crc->phase = RP_ERROR;
189 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
190 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
195 "Check value got `%s' of size %u, type %d, expire %s\n",
196 GNUNET_h2s (key), (unsigned int) size, type,
197 GNUNET_STRINGS_absolute_time_to_string (expiration));
199 "Check value iteration %d wants size %u, type %d, expire %s\n", i,
200 (unsigned int) get_size (i), get_type (i),
201 GNUNET_STRINGS_absolute_time_to_string (get_expiration(i)));
203 GNUNET_assert (size == get_size (i));
204 GNUNET_assert (0 == memcmp (data, get_data (i), size));
205 GNUNET_assert (type == get_type (i));
206 GNUNET_assert (priority == get_priority (i));
207 GNUNET_assert (anonymity == get_anonymity (i));
208 GNUNET_assert (expiration.abs_value_us == get_expiration (i).abs_value_us);
215 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
216 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
221 delete_value (void *cls, const struct GNUNET_HashCode * key, size_t size,
222 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
223 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
226 struct CpsRunContext *crc = cls;
228 GNUNET_assert (crc->data == NULL);
229 GNUNET_assert (NULL != key);
232 crc->data = GNUNET_malloc (size);
233 memcpy (crc->data, data, size);
234 crc->phase = RP_DO_DEL;
235 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
236 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
241 check_nothing (void *cls, const struct GNUNET_HashCode * key, size_t size,
242 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
243 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
246 struct CpsRunContext *crc = cls;
248 GNUNET_assert (key == NULL);
250 crc->phase = RP_RESERVE;
251 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
252 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
257 check_multiple (void *cls, const struct GNUNET_HashCode * key, size_t size,
258 const void *data, enum GNUNET_BLOCK_Type type,
259 uint32_t priority, uint32_t anonymity,
260 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
262 struct CpsRunContext *crc = cls;
264 GNUNET_assert (key != NULL);
267 case RP_GET_MULTIPLE:
268 crc->phase = RP_GET_MULTIPLE_NEXT;
269 crc->first_uid = uid;
272 case RP_GET_MULTIPLE_NEXT:
273 GNUNET_assert (uid != crc->first_uid);
274 crc->phase = RP_UPDATE;
278 crc->phase = RP_ERROR;
281 if (priority == get_priority (42))
283 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
284 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
289 check_update (void *cls, const struct GNUNET_HashCode * key, size_t size,
290 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
291 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
294 struct CpsRunContext *crc = cls;
296 GNUNET_assert (key != NULL);
297 if ((anonymity == get_anonymity (42)) && (size == get_size (42)) &&
298 (priority == get_priority (42) + 100))
299 crc->phase = RP_DONE;
302 GNUNET_assert (size == get_size (43));
305 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
306 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
311 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
313 struct CpsRunContext *crc = cls;
315 ok = (int) crc->phase;
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test in phase %u\n", crc->phase);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
322 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
323 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
324 get_data (crc->i), get_type (crc->i),
325 get_priority (crc->i), get_anonymity (crc->i), 0,
326 get_expiration (crc->i), 1, 1, TIMEOUT,
327 &check_success, crc);
329 if (crc->i == ITERATIONS)
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
336 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
337 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
338 get_type (crc->i), 1, 1, TIMEOUT, &check_value,
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "DEL",
346 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
347 GNUNET_assert (NULL !=
348 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
349 get_type (crc->i), 1, 1, TIMEOUT,
350 &delete_value, crc));
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "DO_DEL",
358 crc->phase = RP_DELVALIDATE;
364 GNUNET_assert (NULL !=
365 GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
366 crc->data, 1, 1, TIMEOUT,
367 &check_success, crc));
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n",
372 "DEL-VALIDATE", crc->i);
373 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
374 GNUNET_assert (NULL !=
375 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
376 get_type (crc->i), 1, 1, TIMEOUT,
377 &check_nothing, crc));
380 crc->phase = RP_PUT_MULTIPLE;
381 GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2, 1, 1, TIMEOUT,
384 case RP_PUT_MULTIPLE:
385 crc->phase = RP_PUT_MULTIPLE_NEXT;
386 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
387 get_data (42), get_type (42), get_priority (42),
388 get_anonymity (42), 0, get_expiration (42), 1, 1,
389 TIMEOUT, &check_success, crc);
391 case RP_PUT_MULTIPLE_NEXT:
392 crc->phase = RP_GET_MULTIPLE;
393 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (43),
394 get_data (43), get_type (42), get_priority (43),
395 get_anonymity (43), 0, get_expiration (43), 1, 1,
396 TIMEOUT, &check_success, crc);
398 case RP_GET_MULTIPLE:
399 GNUNET_assert (NULL !=
400 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
401 get_type (42), 1, 1, TIMEOUT,
402 &check_multiple, crc));
404 case RP_GET_MULTIPLE_NEXT:
405 GNUNET_assert (NULL !=
406 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
407 get_type (42), 1, 1, TIMEOUT,
408 &check_multiple, crc));
411 GNUNET_assert (crc->uid > 0);
412 crc->phase = RP_UPDATE_VALIDATE;
413 GNUNET_DATASTORE_update (datastore, crc->uid, 100, get_expiration (42), 1,
414 1, TIMEOUT, &check_success, crc);
416 case RP_UPDATE_VALIDATE:
417 GNUNET_assert (NULL !=
418 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
419 get_type (42), 1, 1, TIMEOUT,
420 &check_update, crc));
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
424 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
429 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
438 run_tests (void *cls, int32_t success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
440 struct CpsRunContext *crc = cls;
445 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
446 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
449 FPRINTF (stderr, "%s", "Test 'put' operation failed, key already exists (!?)\n");
450 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
455 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
457 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
468 const struct GNUNET_CONFIGURATION_Handle *cfg,
469 struct GNUNET_TESTING_Peer *peer)
471 struct CpsRunContext *crc;
472 static struct GNUNET_HashCode zkey;
474 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
477 now = GNUNET_TIME_absolute_get ();
478 datastore = GNUNET_DATASTORE_connect (cfg);
480 GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST",
481 GNUNET_BLOCK_TYPE_TEST, 0, 0, 0,
482 GNUNET_TIME_relative_to_absolute
483 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
484 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
486 FPRINTF (stderr, "%s", "Test 'put' operation failed.\n");
494 main (int argc, char *argv[])
498 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
499 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
500 "test_datastore_api_data_%s.conf", plugin_name);
502 GNUNET_TESTING_peer_run ("test-gnunet-datastore",
510 /* end of test_datastore_api.c */