2 * This file is part of GNUnet
3 * Copyright (C) 2013 GNUnet e.V.
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_PSYC_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).
118 end_badly (void *cls)
126 * Terminate the testcase (success).
131 end_normally (void *cls)
139 * Finish the testcase (successfully).
144 if (NULL != end_badly_task)
146 GNUNET_SCHEDULER_cancel (end_badly_task);
147 end_badly_task = NULL;
149 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
150 &end_normally, NULL);
155 state_reset_result (void *cls, int64_t result,
156 const char *err_msg, uint16_t err_msg_size)
159 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_reset_result:\t%d\n", result);
160 GNUNET_assert (GNUNET_OK == result);
162 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
163 &state_reset_result, cls);
164 GNUNET_PSYCSTORE_operation_cancel (op);
171 state_result (void *cls, const char *name, const void *value, uint32_t value_size)
173 struct StateClosure *scls = cls;
174 const char *nam = scls->name[scls->n];
175 const void *val = scls->value[scls->n];
176 size_t val_size = scls->value_size[scls->n++];
178 if (value_size == val_size
179 && 0 == memcmp (value, val, val_size)
180 && 0 == strcmp (name, nam))
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " variable %s matches\n", name);
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
188 " variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
189 name, value_size, value, val_size, val);
191 return GNUNET_SYSERR;
197 state_get_prefix_result (void *cls, int64_t result,
198 const char *err_msg, uint16_t err_msg_size)
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,
212 const char *err_msg, uint16_t err_msg_size)
215 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%d\n", result);
216 GNUNET_assert (GNUNET_OK == result);
220 scls.name[0] = "_sync_bar";
221 scls.value[0] = "ten eleven twelve";
222 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
224 scls.name[1] = "_sync_foo";
225 scls.value[1] = "three two one";
226 scls.value_size[1] = sizeof ("three two one") - 1;
228 op = GNUNET_PSYCSTORE_state_get_prefix (h, &channel_pub_key, "_sync",
230 &state_get_prefix_result, &scls);
235 counters_result (void *cls, int status, uint64_t max_fragment_id,
236 uint64_t max_message_id, uint64_t max_group_generation,
237 uint64_t max_state_message_id)
239 struct FragmentClosure *fcls = cls;
243 if (GNUNET_OK == status
244 && max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
245 && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
246 && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
247 && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
251 GNUNET_assert (result == 1);
254 scls.name[0] = "_sync_bar";
255 scls.value[0] = "ten eleven twelve";
256 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
258 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_sync_bar_x_yy_zzz",
259 &state_result, &state_get_result, &scls);
264 state_modify_result (void *cls, int64_t result,
265 const char *err_msg, uint16_t err_msg_size)
268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%d\n", result);
269 GNUNET_assert (GNUNET_OK == result);
271 op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
272 &counters_result, cls);
277 state_sync_result (void *cls, int64_t result,
278 const char *err_msg, uint16_t err_msg_size)
280 struct FragmentClosure *fcls = cls;
282 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%d\n", result);
283 GNUNET_assert (GNUNET_OK == result);
285 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
286 GNUNET_ntohll (fcls->msg[0]->message_id),
287 0, state_modify_result, fcls);
292 fragment_result (void *cls,
293 struct GNUNET_MULTICAST_MessageHeader *msg,
294 enum GNUNET_PSYCSTORE_MessageFlags flags)
296 struct FragmentClosure *fcls = cls;
297 GNUNET_assert (fcls->n < fcls->n_expected);
298 struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
299 uint64_t flags0 = fcls->flags[fcls->n++];
301 if (flags == flags0 && msg->header.size == msg0->header.size
302 && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " fragment %" PRIu64 " matches\n",
305 GNUNET_ntohll (msg->fragment_id));
310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
311 " fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
312 GNUNET_ntohll (msg0->fragment_id),
313 GNUNET_ntohll (msg->fragment_id));
315 return GNUNET_SYSERR;
321 message_get_latest_result (void *cls, int64_t result,
322 const char *err_msg, uint16_t err_msg_size)
324 struct FragmentClosure *fcls = cls;
326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%d\n", result);
327 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
329 modifiers[0] = (struct GNUNET_PSYC_Modifier) {
332 .value = "three two one",
333 .value_size = sizeof ("three two one") - 1
335 modifiers[1] = (struct GNUNET_PSYC_Modifier) {
338 .value = "ten eleven twelve",
339 .value_size = sizeof ("ten eleven twelve") - 1
342 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
343 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
344 GNUNET_ntohll (fcls->msg[0]->message_id) + 2,
345 2, modifiers, state_sync_result, fcls);
350 message_get_result (void *cls, int64_t result,
351 const char *err_msg, uint16_t err_msg_size)
353 struct FragmentClosure *fcls = cls;
355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%d\n", result);
356 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
359 fcls->n_expected = 3;
360 op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
361 1, "", &fragment_result,
362 &message_get_latest_result, fcls);
367 message_get_fragment_result (void *cls, int64_t result,
368 const char *err_msg, uint16_t err_msg_size)
370 struct FragmentClosure *fcls = cls;
372 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
373 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
376 fcls->n_expected = 3;
377 uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
378 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
379 message_id, message_id, 0, "",
381 &message_get_result, fcls);
386 fragment_get_latest_result (void *cls, int64_t result,
387 const char *err_msg, uint16_t err_msg_size)
389 struct FragmentClosure *fcls = cls;
391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%d\n", result);
392 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
395 fcls->n_expected = 2;
396 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
397 GNUNET_ntohll (fcls->msg[1]->message_id),
398 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
400 &message_get_fragment_result, fcls);
405 fragment_get_result (void *cls, int64_t result,
406 const char *err_msg, uint16_t err_msg_size)
408 struct FragmentClosure *fcls = cls;
410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
411 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
414 fcls->n_expected = 3;
415 op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
416 &slave_pub_key, fcls->n_expected,
418 &fragment_get_latest_result, fcls);
423 fragment_store_result (void *cls, int64_t result,
424 const char *err_msg, uint16_t err_msg_size)
427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
428 GNUNET_assert (GNUNET_OK == result);
430 if ((intptr_t) cls == GNUNET_YES)
431 { /* last fragment */
434 uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
435 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
436 fragment_id, fragment_id,
438 &fragment_get_result, &fcls);
446 struct GNUNET_MULTICAST_MessageHeader *msg;
447 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
448 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
449 GNUNET_assert (msg != NULL);
451 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
452 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
454 msg->hop_counter = htonl (9);
455 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
456 msg->fragment_offset = GNUNET_htonll (0);
457 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
458 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
459 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
461 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
463 msg->purpose.size = htonl (ntohs (msg->header.size)
464 - sizeof (msg->header)
465 - sizeof (msg->hop_counter)
466 - sizeof (msg->signature));
467 msg->purpose.purpose = htonl (234);
468 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
471 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
472 &fragment_store_result, GNUNET_NO);
474 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
475 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
476 memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
477 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
478 msg->fragment_offset = GNUNET_htonll (1024);
480 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
481 &fragment_store_result, GNUNET_NO);
483 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
484 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
485 memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
486 msg->fragment_id = GNUNET_htonll (INT64_MAX);
487 msg->fragment_offset = GNUNET_htonll (16384);
489 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
490 &fragment_store_result, (void *) GNUNET_YES);
495 membership_test_result (void *cls, int64_t result,
496 const char *err_msg, uint16_t err_msg_size)
499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
500 GNUNET_assert (GNUNET_OK == result);
507 membership_store_result (void *cls, int64_t result,
508 const char *err_msg, uint16_t err_msg_size)
511 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
512 GNUNET_assert (GNUNET_OK == result);
514 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
516 &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 */