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-new.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 = now.abs_value + 20000000 - i * 1000;
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);
194 FPRINTF (stderr, "Check value got `%s' of size %u, type %d, expire %llu\n",
195 GNUNET_h2s (key), (unsigned int) size, type,
196 (unsigned long long) expiration.abs_value);
198 "Check value iteration %d wants size %u, type %d, expire %llu\n", i,
199 (unsigned int) get_size (i), get_type (i),
200 (unsigned long long) get_expiration (i).abs_value);
202 GNUNET_assert (size == get_size (i));
203 GNUNET_assert (0 == memcmp (data, get_data (i), size));
204 GNUNET_assert (type == get_type (i));
205 GNUNET_assert (priority == get_priority (i));
206 GNUNET_assert (anonymity == get_anonymity (i));
207 GNUNET_assert (expiration.abs_value == get_expiration (i).abs_value);
214 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
215 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
220 delete_value (void *cls, const struct GNUNET_HashCode * key, size_t size,
221 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
222 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
225 struct CpsRunContext *crc = cls;
227 GNUNET_assert (crc->data == NULL);
228 GNUNET_assert (NULL != key);
231 crc->data = GNUNET_malloc (size);
232 memcpy (crc->data, data, size);
233 crc->phase = RP_DO_DEL;
234 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
235 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
240 check_nothing (void *cls, const struct GNUNET_HashCode * key, size_t size,
241 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
242 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
245 struct CpsRunContext *crc = cls;
247 GNUNET_assert (key == NULL);
249 crc->phase = RP_RESERVE;
250 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
251 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
256 check_multiple (void *cls, const struct GNUNET_HashCode * key, size_t size,
257 const void *data, enum GNUNET_BLOCK_Type type,
258 uint32_t priority, uint32_t anonymity,
259 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
261 struct CpsRunContext *crc = cls;
263 GNUNET_assert (key != NULL);
266 case RP_GET_MULTIPLE:
267 crc->phase = RP_GET_MULTIPLE_NEXT;
268 crc->first_uid = uid;
271 case RP_GET_MULTIPLE_NEXT:
272 GNUNET_assert (uid != crc->first_uid);
273 crc->phase = RP_UPDATE;
277 crc->phase = RP_ERROR;
280 if (priority == get_priority (42))
282 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
283 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
288 check_update (void *cls, const struct GNUNET_HashCode * key, size_t size,
289 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
290 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
293 struct CpsRunContext *crc = cls;
295 GNUNET_assert (key != NULL);
296 if ((anonymity == get_anonymity (42)) && (size == get_size (42)) &&
297 (priority == get_priority (42) + 100))
298 crc->phase = RP_DONE;
301 GNUNET_assert (size == get_size (43));
304 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
305 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
310 run_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
312 struct CpsRunContext *crc = cls;
314 ok = (int) crc->phase;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test in phase %u\n", crc->phase);
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "PUT",
321 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
322 GNUNET_DATASTORE_put (datastore, 0, &crc->key, get_size (crc->i),
323 get_data (crc->i), get_type (crc->i),
324 get_priority (crc->i), get_anonymity (crc->i), 0,
325 get_expiration (crc->i), 1, 1, TIMEOUT,
326 &check_success, crc);
328 if (crc->i == ITERATIONS)
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "GET",
335 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
336 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
337 get_type (crc->i), 1, 1, TIMEOUT, &check_value,
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "DEL",
345 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
346 GNUNET_assert (NULL !=
347 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
348 get_type (crc->i), 1, 1, TIMEOUT,
349 &delete_value, crc));
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n", "DO_DEL",
357 crc->phase = RP_DELVALIDATE;
363 GNUNET_assert (NULL !=
364 GNUNET_DATASTORE_remove (datastore, &crc->key, crc->size,
365 crc->data, 1, 1, TIMEOUT,
366 &check_success, crc));
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Executing `%s' number %u\n",
371 "DEL-VALIDATE", crc->i);
372 GNUNET_CRYPTO_hash (&crc->i, sizeof (int), &crc->key);
373 GNUNET_assert (NULL !=
374 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
375 get_type (crc->i), 1, 1, TIMEOUT,
376 &check_nothing, crc));
379 crc->phase = RP_PUT_MULTIPLE;
380 GNUNET_DATASTORE_reserve (datastore, 128 * 1024, 2, 1, 1, TIMEOUT,
383 case RP_PUT_MULTIPLE:
384 crc->phase = RP_PUT_MULTIPLE_NEXT;
385 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (42),
386 get_data (42), get_type (42), get_priority (42),
387 get_anonymity (42), 0, get_expiration (42), 1, 1,
388 TIMEOUT, &check_success, crc);
390 case RP_PUT_MULTIPLE_NEXT:
391 crc->phase = RP_GET_MULTIPLE;
392 GNUNET_DATASTORE_put (datastore, crc->rid, &crc->key, get_size (43),
393 get_data (43), get_type (42), get_priority (43),
394 get_anonymity (43), 0, get_expiration (43), 1, 1,
395 TIMEOUT, &check_success, crc);
397 case RP_GET_MULTIPLE:
398 GNUNET_assert (NULL !=
399 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
400 get_type (42), 1, 1, TIMEOUT,
401 &check_multiple, crc));
403 case RP_GET_MULTIPLE_NEXT:
404 GNUNET_assert (NULL !=
405 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
406 get_type (42), 1, 1, TIMEOUT,
407 &check_multiple, crc));
410 GNUNET_assert (crc->uid > 0);
411 crc->phase = RP_UPDATE_VALIDATE;
412 GNUNET_DATASTORE_update (datastore, crc->uid, 100, get_expiration (42), 1,
413 1, TIMEOUT, &check_success, crc);
415 case RP_UPDATE_VALIDATE:
416 GNUNET_assert (NULL !=
417 GNUNET_DATASTORE_get_key (datastore, crc->offset, &crc->key,
418 get_type (42), 1, 1, TIMEOUT,
419 &check_update, crc));
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished, disconnecting\n");
423 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
428 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
437 run_tests (void *cls, int32_t success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
439 struct CpsRunContext *crc = cls;
444 GNUNET_SCHEDULER_add_continuation (&run_continuation, crc,
445 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
448 FPRINTF (stderr, "%s", "Test 'put' operation failed, key already exists (!?)\n");
449 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
454 "Test 'put' operation failed with error `%s' database likely not setup, skipping test.\n",
456 GNUNET_DATASTORE_disconnect (datastore, GNUNET_YES);
467 const struct GNUNET_CONFIGURATION_Handle *cfg,
468 struct GNUNET_TESTING_Peer *peer)
470 struct CpsRunContext *crc;
471 static struct GNUNET_HashCode zkey;
473 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
476 now = GNUNET_TIME_absolute_get ();
477 datastore = GNUNET_DATASTORE_connect (cfg);
479 GNUNET_DATASTORE_put (datastore, 0, &zkey, 4, "TEST",
480 GNUNET_BLOCK_TYPE_TEST, 0, 0, 0,
481 GNUNET_TIME_relative_to_absolute
482 (GNUNET_TIME_UNIT_SECONDS), 0, 1,
483 GNUNET_TIME_UNIT_MINUTES, &run_tests, crc))
485 FPRINTF (stderr, "%s", "Test 'put' operation failed.\n");
493 main (int argc, char *argv[])
497 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
498 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
499 "test_datastore_api_data_%s.conf", plugin_name);
501 GNUNET_TESTING_peer_run ("test-gnunet-datastore",
509 /* end of test_datastore_api.c */