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 it
6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your 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 * Affero General Public License for more details.
17 * @file psycstore/test_psycstore.c
18 * @brief Test for the PSYCstore service.
19 * @author Gabor X Toth
20 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_common.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_psycstore_service.h"
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
35 * Return value from 'main'.
40 * Handle to PSYCstore service.
42 static struct GNUNET_PSYCSTORE_Handle *h;
45 * Handle to PSYCstore operation.
47 static struct GNUNET_PSYCSTORE_OperationHandle *op;
50 * Handle for task for timeout termination.
52 static struct GNUNET_SCHEDULER_Task *end_badly_task;
54 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
55 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
57 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
58 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
60 static struct FragmentClosure
65 struct GNUNET_MULTICAST_MessageHeader *msg[16];
72 size_t value_size[16];
75 static struct GNUNET_PSYC_Modifier modifiers[16];
78 * Clean up all resources used.
85 GNUNET_PSYCSTORE_operation_cancel (op);
90 GNUNET_PSYCSTORE_disconnect (h);
93 if (NULL != channel_key)
95 GNUNET_free (channel_key);
98 if (NULL != slave_key)
100 GNUNET_free (slave_key);
103 GNUNET_SCHEDULER_shutdown ();
108 * Terminate the testcase (failure).
113 end_badly (void *cls)
121 * Terminate the testcase (success).
126 end_normally (void *cls)
134 * Finish the testcase (successfully).
139 if (NULL != end_badly_task)
141 GNUNET_SCHEDULER_cancel (end_badly_task);
142 end_badly_task = NULL;
144 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
145 &end_normally, NULL);
150 state_reset_result (void *cls,
153 uint16_t err_msg_size)
156 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
157 "state_reset_result:\t%d\n",
159 GNUNET_assert (GNUNET_OK == result);
161 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
162 &state_reset_result, cls);
163 GNUNET_PSYCSTORE_operation_cancel (op);
170 state_result (void *cls,
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,
185 " variable %s matches\n",
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192 " variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
193 name, (int) value_size, (char*) value, (int) val_size, (char*) val);
195 return GNUNET_SYSERR;
201 state_get_prefix_result (void *cls, int64_t result,
202 const char *err_msg, uint16_t err_msg_size)
204 struct StateClosure *scls = cls;
206 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_prefix_result:\t%ld\n", (long int) result);
207 GNUNET_assert (GNUNET_OK == result && 2 == scls->n);
209 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
210 &state_reset_result, cls);
215 state_get_result (void *cls, int64_t result,
216 const char *err_msg, uint16_t err_msg_size)
219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%ld\n", (long int) result);
220 GNUNET_assert (GNUNET_OK == result);
224 scls.name[0] = "_sync_bar";
225 scls.value[0] = "ten eleven twelve";
226 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
228 scls.name[1] = "_sync_foo";
229 scls.value[1] = "three two one";
230 scls.value_size[1] = sizeof ("three two one") - 1;
232 op = GNUNET_PSYCSTORE_state_get_prefix (h, &channel_pub_key, "_sync",
234 &state_get_prefix_result, &scls);
239 counters_result (void *cls, int status, uint64_t max_fragment_id,
240 uint64_t max_message_id, uint64_t max_group_generation,
241 uint64_t max_state_message_id)
243 struct FragmentClosure *fcls = cls;
247 if (GNUNET_OK == status
248 && max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
249 && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
250 && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
251 && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
255 GNUNET_assert (result == 1);
258 scls.name[0] = "_sync_bar";
259 scls.value[0] = "ten eleven twelve";
260 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
262 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_sync_bar_x_yy_zzz",
263 &state_result, &state_get_result, &scls);
268 state_modify_result (void *cls, int64_t result,
269 const char *err_msg, uint16_t err_msg_size)
272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%ld\n", (long int) result);
273 GNUNET_assert (GNUNET_OK == result);
275 op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
276 &counters_result, cls);
281 state_sync_result (void *cls, int64_t result,
282 const char *err_msg, uint16_t err_msg_size)
284 struct FragmentClosure *fcls = cls;
286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%ld\n", (long int) result);
287 GNUNET_assert (GNUNET_OK == result);
289 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
290 GNUNET_ntohll (fcls->msg[0]->message_id),
291 0, state_modify_result, fcls);
296 fragment_result (void *cls,
297 struct GNUNET_MULTICAST_MessageHeader *msg,
298 enum GNUNET_PSYCSTORE_MessageFlags flags)
300 struct FragmentClosure *fcls = cls;
301 GNUNET_assert (fcls->n < fcls->n_expected);
302 struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
303 uint64_t flags0 = fcls->flags[fcls->n++];
305 if (flags == flags0 && msg->header.size == msg0->header.size
306 && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " fragment %" PRIu64 " matches\n",
309 GNUNET_ntohll (msg->fragment_id));
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
315 " fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
316 GNUNET_ntohll (msg0->fragment_id),
317 GNUNET_ntohll (msg->fragment_id));
319 return GNUNET_SYSERR;
325 message_get_latest_result (void *cls, int64_t result,
326 const char *err_msg, uint16_t err_msg_size)
328 struct FragmentClosure *fcls = cls;
330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%ld\n", (long int) result);
331 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
333 modifiers[0] = (struct GNUNET_PSYC_Modifier) {
336 .value = "three two one",
337 .value_size = sizeof ("three two one") - 1
339 modifiers[1] = (struct GNUNET_PSYC_Modifier) {
342 .value = "ten eleven twelve",
343 .value_size = sizeof ("ten eleven twelve") - 1
346 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
347 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
348 GNUNET_ntohll (fcls->msg[0]->message_id) + 2,
349 2, modifiers, state_sync_result, fcls);
354 message_get_result (void *cls, int64_t result,
355 const char *err_msg, uint16_t err_msg_size)
357 struct FragmentClosure *fcls = cls;
359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%ld\n", (long int) result);
360 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
363 fcls->n_expected = 3;
364 op = GNUNET_PSYCSTORE_message_get_latest (h, &channel_pub_key, &slave_pub_key,
365 1, "", &fragment_result,
366 &message_get_latest_result, fcls);
371 message_get_fragment_result (void *cls, int64_t result,
372 const char *err_msg, uint16_t err_msg_size)
374 struct FragmentClosure *fcls = cls;
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%ld\n", (long int) result);
377 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
380 fcls->n_expected = 3;
381 uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
382 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
383 message_id, message_id, 0, "",
385 &message_get_result, fcls);
390 fragment_get_latest_result (void *cls, int64_t result,
391 const char *err_msg, uint16_t err_msg_size)
393 struct FragmentClosure *fcls = cls;
395 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%ld\n", (long int) result);
396 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
399 fcls->n_expected = 2;
400 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
401 GNUNET_ntohll (fcls->msg[1]->message_id),
402 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
404 &message_get_fragment_result, fcls);
409 fragment_get_result (void *cls, int64_t result,
410 const char *err_msg, uint16_t err_msg_size)
412 struct FragmentClosure *fcls = cls;
414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
415 "fragment_get:\t%d\n",
417 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
420 fcls->n_expected = 3;
421 op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
422 &slave_pub_key, fcls->n_expected,
424 &fragment_get_latest_result, fcls);
429 fragment_store_result (void *cls, int64_t result,
430 const char *err_msg, uint16_t err_msg_size)
433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%ld\n", (long int) result);
434 GNUNET_assert (GNUNET_OK == result);
436 if ((intptr_t) cls == GNUNET_YES)
437 { /* last fragment */
440 uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
441 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
442 fragment_id, fragment_id,
444 &fragment_get_result, &fcls);
452 struct GNUNET_MULTICAST_MessageHeader *msg;
453 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
454 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
455 GNUNET_assert (msg != NULL);
457 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
458 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
460 msg->hop_counter = htonl (9);
461 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
462 msg->fragment_offset = GNUNET_htonll (0);
463 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
464 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
465 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
467 GNUNET_memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
469 msg->purpose.size = htonl (ntohs (msg->header.size)
470 - sizeof (msg->header)
471 - sizeof (msg->hop_counter)
472 - sizeof (msg->signature));
473 msg->purpose.purpose = htonl (234);
474 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
477 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
478 &fragment_store_result, GNUNET_NO);
480 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
481 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
482 GNUNET_memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
483 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
484 msg->fragment_offset = GNUNET_htonll (1024);
486 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
487 &fragment_store_result, GNUNET_NO);
489 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
490 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
491 GNUNET_memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
492 msg->fragment_id = GNUNET_htonll (INT64_MAX);
493 msg->fragment_offset = GNUNET_htonll (16384);
495 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
496 &fragment_store_result, (void *) GNUNET_YES);
501 membership_test_result (void *cls, int64_t result,
502 const char *err_msg, uint16_t err_msg_size)
505 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%ld\n", (long int) result);
506 GNUNET_assert (GNUNET_OK == result);
513 membership_store_result (void *cls, int64_t result,
514 const char *err_msg, uint16_t err_msg_size)
517 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%ld\n", (long int) result);
518 GNUNET_assert (GNUNET_OK == result);
520 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
522 &membership_test_result, NULL);
527 * Main function of the test, run from scheduler.
530 * @param cfg configuration we use (also to connect to PSYCstore service)
531 * @param peer handle to access more of the peer (not used)
534 #if DEBUG_TEST_PSYCSTORE
535 run (void *cls, char *const *args, const char *cfgfile,
536 const struct GNUNET_CONFIGURATION_Handle *cfg)
539 const struct GNUNET_CONFIGURATION_Handle *cfg,
540 struct GNUNET_TESTING_Peer *peer)
543 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
545 h = GNUNET_PSYCSTORE_connect (cfg);
546 GNUNET_assert (NULL != h);
548 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
549 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
551 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
552 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
554 op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
555 GNUNET_YES, INT64_MAX - 5,
557 &membership_store_result, NULL);
562 main (int argc, char *argv[])
565 #if DEBUG_TEST_PSYCSTORE
566 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
567 GNUNET_GETOPT_OPTION_END
569 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
570 "test-psycstore [options]",
574 if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
575 "test_psycstore.conf", &run, NULL))
581 /* end of test_psycstore.c */