2 * This file is part of GNUnet
3 * Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file psycstore/test_psycstore.c
23 * @brief Test for the PSYCstore service.
24 * @author Gabor X Toth
25 * @author Christian Grothoff
31 #include "gnunet_util_lib.h"
32 #include "gnunet_common.h"
33 #include "gnunet_testing_lib.h"
34 #include "gnunet_psycstore_service.h"
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
40 * Return value from 'main'.
45 * Handle to PSYCstore service.
47 static struct GNUNET_PSYCSTORE_Handle *h;
50 * Handle to PSYCstore operation.
52 static struct GNUNET_PSYCSTORE_OperationHandle *op;
55 * Handle for task for timeout termination.
57 static struct GNUNET_SCHEDULER_Task * end_badly_task;
59 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
60 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
62 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
63 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
65 static struct FragmentClosure
70 struct GNUNET_MULTICAST_MessageHeader *msg[16];
77 size_t value_size[16];
80 static struct GNUNET_ENV_Modifier modifiers[16];
83 * Clean up all resources used.
90 GNUNET_PSYCSTORE_operation_cancel (op);
95 GNUNET_PSYCSTORE_disconnect (h);
98 if (NULL != channel_key)
100 GNUNET_free (channel_key);
103 if (NULL != slave_key)
105 GNUNET_free (slave_key);
108 GNUNET_SCHEDULER_shutdown ();
113 * Terminate the testcase (failure).
116 * @param tc scheduler context
119 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
127 * Terminate the testcase (success).
130 * @param tc scheduler context
133 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
141 * Finish the testcase (successfully).
146 if (end_badly_task != NULL)
148 GNUNET_SCHEDULER_cancel (end_badly_task);
149 end_badly_task = NULL;
151 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
152 &end_normally, NULL);
157 state_reset_result (void *cls, int64_t result,
158 const char *err_msg, uint16_t err_msg_size)
161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_reset_result:\t%d\n", result);
162 GNUNET_assert (GNUNET_OK == result);
164 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
165 &state_reset_result, cls);
166 GNUNET_PSYCSTORE_operation_cancel (op);
173 state_result (void *cls, const char *name, const void *value, uint32_t value_size)
175 struct StateClosure *scls = cls;
176 const char *nam = scls->name[scls->n];
177 const void *val = scls->value[scls->n];
178 size_t val_size = scls->value_size[scls->n++];
180 if (value_size == val_size
181 && 0 == memcmp (value, val, val_size)
182 && 0 == strcmp (name, nam))
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " variable %s matches\n", name);
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 " variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
191 name, value_size, value, val_size, val);
193 return GNUNET_SYSERR;
199 state_get_prefix_result (void *cls, int64_t result,
200 const char *err_msg, uint16_t err_msg_size)
202 struct StateClosure *scls = cls;
204 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_prefix_result:\t%d\n", result);
205 GNUNET_assert (GNUNET_OK == result && 2 == scls->n);
207 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
208 &state_reset_result, cls);
213 state_get_result (void *cls, int64_t result,
214 const char *err_msg, uint16_t err_msg_size)
217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%d\n", result);
218 GNUNET_assert (GNUNET_OK == result);
222 scls.name[0] = "_sync_bar";
223 scls.value[0] = "ten eleven twelve";
224 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
226 scls.name[1] = "_sync_foo";
227 scls.value[1] = "three two one";
228 scls.value_size[1] = sizeof ("three two one") - 1;
230 op = GNUNET_PSYCSTORE_state_get_prefix (h, &channel_pub_key, "_sync",
232 &state_get_prefix_result, &scls);
237 counters_result (void *cls, int status, uint64_t max_fragment_id,
238 uint64_t max_message_id, uint64_t max_group_generation,
239 uint64_t max_state_message_id)
241 struct FragmentClosure *fcls = cls;
245 if (GNUNET_OK == status
246 && max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
247 && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
248 && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
249 && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
253 GNUNET_assert (result == 1);
256 scls.name[0] = "_sync_bar";
257 scls.value[0] = "ten eleven twelve";
258 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
260 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_sync_bar_x_yy_zzz",
261 &state_result, &state_get_result, &scls);
266 state_modify_result (void *cls, int64_t result,
267 const char *err_msg, uint16_t err_msg_size)
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%d\n", result);
271 GNUNET_assert (GNUNET_OK == result);
273 op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
274 &counters_result, cls);
279 state_sync_result (void *cls, int64_t result,
280 const char *err_msg, uint16_t err_msg_size)
282 struct FragmentClosure *fcls = cls;
284 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%d\n", result);
285 GNUNET_assert (GNUNET_OK == result);
287 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
288 GNUNET_ntohll (fcls->msg[0]->message_id),
289 0, state_modify_result, fcls);
294 fragment_result (void *cls,
295 struct GNUNET_MULTICAST_MessageHeader *msg,
296 enum GNUNET_PSYCSTORE_MessageFlags flags)
298 struct FragmentClosure *fcls = cls;
299 GNUNET_assert (fcls->n < fcls->n_expected);
300 struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
301 uint64_t flags0 = fcls->flags[fcls->n++];
303 if (flags == flags0 && msg->header.size == msg0->header.size
304 && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " fragment %" PRIu64 " matches\n",
307 GNUNET_ntohll (msg->fragment_id));
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313 " fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
314 GNUNET_ntohll (msg0->fragment_id),
315 GNUNET_ntohll (msg->fragment_id));
317 return GNUNET_SYSERR;
323 message_get_latest_result (void *cls, int64_t result,
324 const char *err_msg, uint16_t err_msg_size)
326 struct FragmentClosure *fcls = cls;
328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%d\n", result);
329 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
331 modifiers[0] = (struct GNUNET_ENV_Modifier) {
334 .value = "three two one",
335 .value_size = sizeof ("three two one") - 1
337 modifiers[1] = (struct GNUNET_ENV_Modifier) {
340 .value = "ten eleven twelve",
341 .value_size = sizeof ("ten eleven twelve") - 1
344 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
345 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
346 GNUNET_ntohll (fcls->msg[0]->message_id) + 2,
347 2, modifiers, state_sync_result, fcls);
352 message_get_result (void *cls, int64_t result,
353 const char *err_msg, uint16_t err_msg_size)
355 struct FragmentClosure *fcls = cls;
357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%d\n", result);
358 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
361 fcls->n_expected = 3;
362 op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
363 1, "", &fragment_result,
364 &message_get_latest_result, fcls);
369 message_get_fragment_result (void *cls, int64_t result,
370 const char *err_msg, uint16_t err_msg_size)
372 struct FragmentClosure *fcls = cls;
374 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
375 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
378 fcls->n_expected = 3;
379 uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
380 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
381 message_id, message_id, "",
383 &message_get_result, fcls);
388 fragment_get_latest_result (void *cls, int64_t result,
389 const char *err_msg, uint16_t err_msg_size)
391 struct FragmentClosure *fcls = cls;
393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%d\n", result);
394 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
397 fcls->n_expected = 2;
398 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
399 GNUNET_ntohll (fcls->msg[1]->message_id),
400 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
402 &message_get_fragment_result, fcls);
407 fragment_get_result (void *cls, int64_t result,
408 const char *err_msg, uint16_t err_msg_size)
410 struct FragmentClosure *fcls = cls;
412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
413 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
416 fcls->n_expected = 3;
417 op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
418 &slave_pub_key, fcls->n_expected,
420 &fragment_get_latest_result, fcls);
425 fragment_store_result (void *cls, int64_t result,
426 const char *err_msg, uint16_t err_msg_size)
429 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
430 GNUNET_assert (GNUNET_OK == result);
432 if ((intptr_t) cls == GNUNET_YES)
433 { /* last fragment */
436 uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
437 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
438 fragment_id, fragment_id,
440 &fragment_get_result, &fcls);
448 struct GNUNET_MULTICAST_MessageHeader *msg;
449 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
450 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
451 GNUNET_assert (msg != NULL);
453 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
454 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
456 msg->hop_counter = htonl (9);
457 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
458 msg->fragment_offset = GNUNET_htonll (0);
459 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
460 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
461 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
463 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
465 msg->purpose.size = htonl (ntohs (msg->header.size)
466 - sizeof (msg->header)
467 - sizeof (msg->hop_counter)
468 - sizeof (msg->signature));
469 msg->purpose.purpose = htonl (234);
470 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
473 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
474 &fragment_store_result, GNUNET_NO);
476 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
477 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
478 memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
479 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
480 msg->fragment_offset = GNUNET_htonll (1024);
482 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
483 &fragment_store_result, GNUNET_NO);
485 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
486 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
487 memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
488 msg->fragment_id = GNUNET_htonll (INT64_MAX);
489 msg->fragment_offset = GNUNET_htonll (16384);
491 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
492 &fragment_store_result, (void *) GNUNET_YES);
497 membership_test_result (void *cls, int64_t result,
498 const char *err_msg, uint16_t err_msg_size)
501 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
502 GNUNET_assert (GNUNET_OK == result);
509 membership_store_result (void *cls, int64_t result,
510 const char *err_msg, uint16_t err_msg_size)
513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
514 GNUNET_assert (GNUNET_OK == result);
516 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
518 &membership_test_result, NULL);
523 * Main function of the test, run from scheduler.
526 * @param cfg configuration we use (also to connect to PSYCstore service)
527 * @param peer handle to access more of the peer (not used)
530 #if DEBUG_TEST_PSYCSTORE
531 run (void *cls, char *const *args, const char *cfgfile,
532 const struct GNUNET_CONFIGURATION_Handle *cfg)
535 const struct GNUNET_CONFIGURATION_Handle *cfg,
536 struct GNUNET_TESTING_Peer *peer)
539 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
541 h = GNUNET_PSYCSTORE_connect (cfg);
542 GNUNET_assert (NULL != h);
544 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
545 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
547 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
548 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
550 op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
551 GNUNET_YES, INT64_MAX - 5,
553 &membership_store_result, NULL);
558 main (int argc, char *argv[])
561 #if DEBUG_TEST_PSYCSTORE
562 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
563 GNUNET_GETOPT_OPTION_END
565 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
566 "test-psycstore [options]",
570 if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
571 "test_psycstore.conf", &run, NULL))
577 /* end of test_psycstore.c */