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,
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, size_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] = "one two three";
228 scls.value_size[1] = sizeof ("one two three") - 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] = "_bar";
257 scls.value[0] = "four five six";
258 scls.value_size[0] = sizeof ("four five six") - 1;
260 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_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 modifiers[0] = (struct GNUNET_ENV_Modifier) {
290 .value = "one two three",
291 .value_size = sizeof ("one two three") - 1
293 modifiers[1] = (struct GNUNET_ENV_Modifier) {
296 .value = "four five six",
297 .value_size = sizeof ("four five six") - 1
300 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
301 GNUNET_ntohll (fcls->msg[0]->message_id), 0,
302 2, modifiers, state_modify_result, fcls);
307 fragment_result (void *cls,
308 struct GNUNET_MULTICAST_MessageHeader *msg,
309 enum GNUNET_PSYCSTORE_MessageFlags flags)
311 struct FragmentClosure *fcls = cls;
312 GNUNET_assert (fcls->n < fcls->n_expected);
313 struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
314 uint64_t flags0 = fcls->flags[fcls->n++];
316 if (flags == flags0 && msg->header.size == msg0->header.size
317 && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " fragment %" PRIu64 " matches\n",
320 GNUNET_ntohll (msg->fragment_id));
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326 " fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
327 GNUNET_ntohll (msg0->fragment_id),
328 GNUNET_ntohll (msg->fragment_id));
330 return GNUNET_SYSERR;
336 message_get_latest_result (void *cls, int64_t result,
337 const char *err_msg, uint16_t err_msg_size)
339 struct FragmentClosure *fcls = cls;
341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%d\n", result);
342 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
344 modifiers[0] = (struct GNUNET_ENV_Modifier) {
347 .value = "three two one",
348 .value_size = sizeof ("three two one") - 1
350 modifiers[1] = (struct GNUNET_ENV_Modifier) {
353 .value = "ten eleven twelve",
354 .value_size = sizeof ("ten eleven twelve") - 1
357 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
358 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
359 2, modifiers, state_sync_result, fcls);
364 message_get_result (void *cls, int64_t result,
365 const char *err_msg, uint16_t err_msg_size)
367 struct FragmentClosure *fcls = cls;
369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%d\n", result);
370 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
373 fcls->n_expected = 3;
374 op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
375 1, "", &fragment_result,
376 &message_get_latest_result, fcls);
381 message_get_fragment_result (void *cls, int64_t result,
382 const char *err_msg, uint16_t err_msg_size)
384 struct FragmentClosure *fcls = cls;
386 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
387 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
390 fcls->n_expected = 3;
391 uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
392 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
393 message_id, message_id, "",
395 &message_get_result, fcls);
400 fragment_get_latest_result (void *cls, int64_t result,
401 const char *err_msg, uint16_t err_msg_size)
403 struct FragmentClosure *fcls = cls;
405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%d\n", result);
406 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
409 fcls->n_expected = 2;
410 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
411 GNUNET_ntohll (fcls->msg[1]->message_id),
412 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
414 &message_get_fragment_result, fcls);
419 fragment_get_result (void *cls, int64_t result,
420 const char *err_msg, uint16_t err_msg_size)
422 struct FragmentClosure *fcls = cls;
424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
425 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
428 fcls->n_expected = 3;
429 op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
430 &slave_pub_key, fcls->n_expected,
432 &fragment_get_latest_result, fcls);
437 fragment_store_result (void *cls, int64_t result,
438 const char *err_msg, uint16_t err_msg_size)
441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
442 GNUNET_assert (GNUNET_OK == result);
444 if ((intptr_t) cls == GNUNET_YES)
445 { /* last fragment */
448 uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
449 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
450 fragment_id, fragment_id,
452 &fragment_get_result, &fcls);
460 struct GNUNET_MULTICAST_MessageHeader *msg;
461 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
462 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
463 GNUNET_assert (msg != NULL);
465 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
466 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
468 msg->hop_counter = htonl (9);
469 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
470 msg->fragment_offset = GNUNET_htonll (0);
471 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
472 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
473 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
475 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
477 msg->purpose.size = htonl (ntohs (msg->header.size)
478 - sizeof (msg->header)
479 - sizeof (msg->hop_counter)
480 - sizeof (msg->signature));
481 msg->purpose.purpose = htonl (234);
482 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
485 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
486 &fragment_store_result, GNUNET_NO);
488 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
489 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
490 memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
491 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
492 msg->fragment_offset = GNUNET_htonll (1024);
494 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
495 &fragment_store_result, GNUNET_NO);
497 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
498 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
499 memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
500 msg->fragment_id = GNUNET_htonll (INT64_MAX);
501 msg->fragment_offset = GNUNET_htonll (16384);
503 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
504 &fragment_store_result, (void *) GNUNET_YES);
509 membership_test_result (void *cls, int64_t result,
510 const char *err_msg, uint16_t err_msg_size)
513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
514 GNUNET_assert (GNUNET_OK == result);
521 membership_store_result (void *cls, int64_t result,
522 const char *err_msg, uint16_t err_msg_size)
525 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
526 GNUNET_assert (GNUNET_OK == result);
528 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
530 &membership_test_result, NULL);
535 * Main function of the test, run from scheduler.
538 * @param cfg configuration we use (also to connect to PSYCstore service)
539 * @param peer handle to access more of the peer (not used)
542 #if DEBUG_TEST_PSYCSTORE
543 run (void *cls, char *const *args, const char *cfgfile,
544 const struct GNUNET_CONFIGURATION_Handle *cfg)
547 const struct GNUNET_CONFIGURATION_Handle *cfg,
548 struct GNUNET_TESTING_Peer *peer)
551 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
553 h = GNUNET_PSYCSTORE_connect (cfg);
554 GNUNET_assert (NULL != h);
556 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
557 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
559 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
560 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
562 op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
563 GNUNET_YES, INT64_MAX - 5,
565 &membership_store_result, NULL);
570 main (int argc, char *argv[])
573 #if DEBUG_TEST_PSYCSTORE
574 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
575 GNUNET_GETOPT_OPTION_END
577 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
578 "test-psycstore [options]",
582 if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
583 "test_psycstore.conf", &run, NULL))
589 /* end of test_psycstore.c */