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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, 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, const char *err_msg)
160 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_reset_result:\t%d\n", result);
161 GNUNET_assert (GNUNET_OK == result);
163 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
164 &state_reset_result, cls);
165 GNUNET_PSYCSTORE_operation_cancel (op);
172 state_result (void *cls, const char *name, const void *value, size_t value_size)
174 struct StateClosure *scls = cls;
175 const char *nam = scls->name[scls->n];
176 const void *val = scls->value[scls->n];
177 size_t val_size = scls->value_size[scls->n++];
179 if (value_size == val_size
180 && 0 == memcmp (value, val, val_size)
181 && 0 == strcmp (name, nam))
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " variable %s matches\n", name);
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 " variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
190 name, value_size, value, val_size, val);
192 return GNUNET_SYSERR;
198 state_get_prefix_result (void *cls, int64_t result, const char *err_msg)
200 struct StateClosure *scls = cls;
202 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_prefix_result:\t%d\n", result);
203 GNUNET_assert (GNUNET_OK == result && 2 == scls->n);
205 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
206 &state_reset_result, cls);
211 state_get_result (void *cls, int64_t result, const char *err_msg)
214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%d\n", result);
215 GNUNET_assert (GNUNET_OK == result);
219 scls.name[0] = "_sync_bar";
220 scls.value[0] = "ten eleven twelve";
221 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
223 scls.name[1] = "_sync_foo";
224 scls.value[1] = "one two three";
225 scls.value_size[1] = sizeof ("one two three") - 1;
227 op = GNUNET_PSYCSTORE_state_get_prefix (h, &channel_pub_key, "_sync",
229 &state_get_prefix_result, &scls);
234 counters_result (void *cls, int status, uint64_t max_fragment_id,
235 uint64_t max_message_id, uint64_t max_group_generation,
236 uint64_t max_state_message_id)
238 struct FragmentClosure *fcls = cls;
242 if (GNUNET_OK == status
243 && max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
244 && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
245 && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
246 && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
250 GNUNET_assert (result == 1);
253 scls.name[0] = "_bar";
254 scls.value[0] = "four five six";
255 scls.value_size[0] = sizeof ("four five six") - 1;
257 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_bar_x_yy_zzz",
258 &state_result, &state_get_result, &scls);
263 state_modify_result (void *cls, int64_t result, const char *err_msg)
266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%d\n", result);
267 GNUNET_assert (GNUNET_OK == result);
269 op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
270 &counters_result, cls);
275 state_sync_result (void *cls, int64_t result, const char *err_msg)
277 struct FragmentClosure *fcls = cls;
279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%d\n", result);
280 GNUNET_assert (GNUNET_OK == result);
282 modifiers[0] = (struct GNUNET_ENV_Modifier) {
285 .value = "one two three",
286 .value_size = sizeof ("one two three") - 1
288 modifiers[1] = (struct GNUNET_ENV_Modifier) {
291 .value = "four five six",
292 .value_size = sizeof ("four five six") - 1
295 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
296 GNUNET_ntohll (fcls->msg[0]->message_id), 0,
297 2, modifiers, state_modify_result, fcls);
302 fragment_result (void *cls,
303 struct GNUNET_MULTICAST_MessageHeader *msg,
304 enum GNUNET_PSYCSTORE_MessageFlags flags)
306 struct FragmentClosure *fcls = cls;
307 GNUNET_assert (fcls->n < fcls->n_expected);
308 struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
309 uint64_t flags0 = fcls->flags[fcls->n++];
311 if (flags == flags0 && msg->header.size == msg0->header.size
312 && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " fragment %" PRIu64 " matches\n",
315 GNUNET_ntohll (msg->fragment_id));
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321 " fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
322 GNUNET_ntohll (msg0->fragment_id),
323 GNUNET_ntohll (msg->fragment_id));
325 return GNUNET_SYSERR;
331 message_get_latest_result (void *cls, int64_t result, const char *err_msg)
333 struct FragmentClosure *fcls = cls;
335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%d\n", result);
336 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
338 modifiers[0] = (struct GNUNET_ENV_Modifier) {
341 .value = "three two one",
342 .value_size = sizeof ("three two one") - 1
344 modifiers[1] = (struct GNUNET_ENV_Modifier) {
347 .value = "ten eleven twelve",
348 .value_size = sizeof ("ten eleven twelve") - 1
351 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
352 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
353 2, modifiers, state_sync_result, fcls);
358 message_get_result (void *cls, int64_t result, const char *err_msg)
360 struct FragmentClosure *fcls = cls;
362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%d\n", result);
363 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
366 fcls->n_expected = 3;
367 op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
369 &message_get_latest_result, fcls);
374 message_get_fragment_result (void *cls, int64_t result, const char *err_msg)
376 struct FragmentClosure *fcls = cls;
378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
379 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
382 fcls->n_expected = 3;
383 uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
384 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
385 message_id, message_id,
387 &message_get_result, fcls);
392 fragment_get_latest_result (void *cls, int64_t result, const char *err_msg)
394 struct FragmentClosure *fcls = cls;
396 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%d\n", result);
397 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
400 fcls->n_expected = 2;
401 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
402 GNUNET_ntohll (fcls->msg[1]->message_id),
403 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
405 &message_get_fragment_result, fcls);
410 fragment_get_result (void *cls, int64_t result, const char *err_msg)
412 struct FragmentClosure *fcls = cls;
414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
415 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
418 fcls->n_expected = 3;
419 op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
420 &slave_pub_key, fcls->n_expected,
422 &fragment_get_latest_result, fcls);
427 fragment_store_result (void *cls, int64_t result, const char *err_msg)
430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
431 GNUNET_assert (GNUNET_OK == result);
433 if ((intptr_t) cls == GNUNET_YES)
434 { /* last fragment */
437 uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
438 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
439 fragment_id, fragment_id,
441 &fragment_get_result, &fcls);
449 struct GNUNET_MULTICAST_MessageHeader *msg;
450 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
451 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
452 GNUNET_assert (msg != NULL);
454 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
455 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
457 msg->hop_counter = htonl (9);
458 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
459 msg->fragment_offset = GNUNET_htonll (0);
460 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
461 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
462 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
464 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
466 msg->purpose.size = htonl (ntohs (msg->header.size)
467 - sizeof (msg->header)
468 - sizeof (msg->hop_counter)
469 - sizeof (msg->signature));
470 msg->purpose.purpose = htonl (234);
471 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
474 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
475 &fragment_store_result, GNUNET_NO);
477 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
478 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
479 memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
480 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
481 msg->fragment_offset = GNUNET_htonll (1024);
483 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
484 &fragment_store_result, GNUNET_NO);
486 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
487 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
488 memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
489 msg->fragment_id = GNUNET_htonll (INT64_MAX);
490 msg->fragment_offset = GNUNET_htonll (16384);
492 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
493 &fragment_store_result, (void *) GNUNET_YES);
498 membership_test_result (void *cls, int64_t result, const char *err_msg)
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, const char *err_msg)
512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
513 GNUNET_assert (GNUNET_OK == result);
515 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
517 &membership_test_result, NULL);
521 * Main function of the test, run from scheduler.
524 * @param cfg configuration we use (also to connect to PSYCstore service)
525 * @param peer handle to access more of the peer (not used)
528 #if DEBUG_TEST_PSYCSTORE
529 run (void *cls, char *const *args, const char *cfgfile,
530 const struct GNUNET_CONFIGURATION_Handle *cfg)
533 const struct GNUNET_CONFIGURATION_Handle *cfg,
534 struct GNUNET_TESTING_Peer *peer)
537 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
539 h = GNUNET_PSYCSTORE_connect (cfg);
540 GNUNET_assert (NULL != h);
542 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
543 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
545 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
546 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
548 op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
549 GNUNET_YES, INT64_MAX - 5,
551 &membership_store_result, NULL);
556 main (int argc, char *argv[])
559 #if DEBUG_TEST_PSYCSTORE
560 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
561 GNUNET_GETOPT_OPTION_END
563 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
564 "test-psycstore [options]",
568 if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
569 "test_psycstore.conf", &run, NULL))
575 /* end of test_psycstore.c */