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.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file psycstore/test_psycstore.c
21 * @brief Test for the PSYCstore service.
22 * @author Gabor X Toth
23 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_common.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_psycstore_service.h"
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38 * Return value from 'main'.
43 * Handle to PSYCstore service.
45 static struct GNUNET_PSYCSTORE_Handle *h;
48 * Handle to PSYCstore operation.
50 static struct GNUNET_PSYCSTORE_OperationHandle *op;
53 * Handle for task for timeout termination.
55 static struct GNUNET_SCHEDULER_Task *end_badly_task;
57 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
58 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
60 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
61 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
63 static struct FragmentClosure
68 struct GNUNET_MULTICAST_MessageHeader *msg[16];
75 size_t value_size[16];
78 static struct GNUNET_PSYC_Modifier modifiers[16];
81 * Clean up all resources used.
88 GNUNET_PSYCSTORE_operation_cancel (op);
93 GNUNET_PSYCSTORE_disconnect (h);
96 if (NULL != channel_key)
98 GNUNET_free (channel_key);
101 if (NULL != slave_key)
103 GNUNET_free (slave_key);
106 GNUNET_SCHEDULER_shutdown ();
111 * Terminate the testcase (failure).
116 end_badly (void *cls)
124 * Terminate the testcase (success).
129 end_normally (void *cls)
137 * Finish the testcase (successfully).
142 if (NULL != end_badly_task)
144 GNUNET_SCHEDULER_cancel (end_badly_task);
145 end_badly_task = NULL;
147 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
148 &end_normally, NULL);
153 state_reset_result (void *cls,
156 uint16_t err_msg_size)
159 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
160 "state_reset_result:\t%d\n",
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,
178 struct StateClosure *scls = cls;
179 const char *nam = scls->name[scls->n];
180 const void *val = scls->value[scls->n];
181 size_t val_size = scls->value_size[scls->n++];
183 if (value_size == val_size
184 && 0 == memcmp (value, val, val_size)
185 && 0 == strcmp (name, nam))
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 " variable %s matches\n",
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195 " variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
196 name, (int) value_size, (char*) value, (int) val_size, (char*) val);
198 return GNUNET_SYSERR;
204 state_get_prefix_result (void *cls, int64_t result,
205 const char *err_msg, uint16_t err_msg_size)
207 struct StateClosure *scls = cls;
209 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_prefix_result:\t%ld\n", (long int) result);
210 GNUNET_assert (GNUNET_OK == result && 2 == scls->n);
212 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
213 &state_reset_result, cls);
218 state_get_result (void *cls, int64_t result,
219 const char *err_msg, uint16_t err_msg_size)
222 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%ld\n", (long int) result);
223 GNUNET_assert (GNUNET_OK == result);
227 scls.name[0] = "_sync_bar";
228 scls.value[0] = "ten eleven twelve";
229 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
231 scls.name[1] = "_sync_foo";
232 scls.value[1] = "three two one";
233 scls.value_size[1] = sizeof ("three two one") - 1;
235 op = GNUNET_PSYCSTORE_state_get_prefix (h, &channel_pub_key, "_sync",
237 &state_get_prefix_result, &scls);
242 counters_result (void *cls, int status, uint64_t max_fragment_id,
243 uint64_t max_message_id, uint64_t max_group_generation,
244 uint64_t max_state_message_id)
246 struct FragmentClosure *fcls = cls;
250 if (GNUNET_OK == status
251 && max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
252 && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
253 && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
254 && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
257 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
258 GNUNET_assert (result == 1);
261 scls.name[0] = "_sync_bar";
262 scls.value[0] = "ten eleven twelve";
263 scls.value_size[0] = sizeof ("ten eleven twelve") - 1;
265 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_sync_bar_x_yy_zzz",
266 &state_result, &state_get_result, &scls);
271 state_modify_result (void *cls, int64_t result,
272 const char *err_msg, uint16_t err_msg_size)
275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%ld\n", (long int) result);
276 GNUNET_assert (GNUNET_OK == result);
278 op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
279 &counters_result, cls);
284 state_sync_result (void *cls, int64_t result,
285 const char *err_msg, uint16_t err_msg_size)
287 struct FragmentClosure *fcls = cls;
289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%ld\n", (long int) result);
290 GNUNET_assert (GNUNET_OK == result);
292 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
293 GNUNET_ntohll (fcls->msg[0]->message_id),
294 0, state_modify_result, fcls);
299 fragment_result (void *cls,
300 struct GNUNET_MULTICAST_MessageHeader *msg,
301 enum GNUNET_PSYCSTORE_MessageFlags flags)
303 struct FragmentClosure *fcls = cls;
304 GNUNET_assert (fcls->n < fcls->n_expected);
305 struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
306 uint64_t flags0 = fcls->flags[fcls->n++];
308 if (flags == flags0 && msg->header.size == msg0->header.size
309 && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " fragment %" PRIu64 " matches\n",
312 GNUNET_ntohll (msg->fragment_id));
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
318 " fragment differs: expected %" PRIu64 ", got %" PRIu64 "\n",
319 GNUNET_ntohll (msg0->fragment_id),
320 GNUNET_ntohll (msg->fragment_id));
322 return GNUNET_SYSERR;
328 message_get_latest_result (void *cls, int64_t result,
329 const char *err_msg, uint16_t err_msg_size)
331 struct FragmentClosure *fcls = cls;
333 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_latest:\t%ld\n", (long int) result);
334 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
336 modifiers[0] = (struct GNUNET_PSYC_Modifier) {
339 .value = "three two one",
340 .value_size = sizeof ("three two one") - 1
342 modifiers[1] = (struct GNUNET_PSYC_Modifier) {
345 .value = "ten eleven twelve",
346 .value_size = sizeof ("ten eleven twelve") - 1
349 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
350 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
351 GNUNET_ntohll (fcls->msg[0]->message_id) + 2,
352 2, modifiers, state_sync_result, fcls);
357 message_get_result (void *cls, int64_t result,
358 const char *err_msg, uint16_t err_msg_size)
360 struct FragmentClosure *fcls = cls;
362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%ld\n", (long int) 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,
368 1, "", &fragment_result,
369 &message_get_latest_result, fcls);
374 message_get_fragment_result (void *cls, int64_t result,
375 const char *err_msg, uint16_t err_msg_size)
377 struct FragmentClosure *fcls = cls;
379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%ld\n", (long int) result);
380 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
383 fcls->n_expected = 3;
384 uint64_t message_id = GNUNET_ntohll (fcls->msg[0]->message_id);
385 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key, &slave_pub_key,
386 message_id, message_id, 0, "",
388 &message_get_result, fcls);
393 fragment_get_latest_result (void *cls, int64_t result,
394 const char *err_msg, uint16_t err_msg_size)
396 struct FragmentClosure *fcls = cls;
398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get_latest:\t%ld\n", (long int) result);
399 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
402 fcls->n_expected = 2;
403 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key, &slave_pub_key,
404 GNUNET_ntohll (fcls->msg[1]->message_id),
405 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
407 &message_get_fragment_result, fcls);
412 fragment_get_result (void *cls, int64_t result,
413 const char *err_msg, uint16_t err_msg_size)
415 struct FragmentClosure *fcls = cls;
417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
418 "fragment_get:\t%d\n",
420 GNUNET_assert (0 < result && fcls->n == fcls->n_expected);
423 fcls->n_expected = 3;
424 op = GNUNET_PSYCSTORE_fragment_get_latest (h, &channel_pub_key,
425 &slave_pub_key, fcls->n_expected,
427 &fragment_get_latest_result, fcls);
432 fragment_store_result (void *cls, int64_t result,
433 const char *err_msg, uint16_t err_msg_size)
436 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%ld\n", (long int) result);
437 GNUNET_assert (GNUNET_OK == result);
439 if ((intptr_t) cls == GNUNET_YES)
440 { /* last fragment */
443 uint64_t fragment_id = GNUNET_ntohll (fcls.msg[0]->fragment_id);
444 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key, &slave_pub_key,
445 fragment_id, fragment_id,
447 &fragment_get_result, &fcls);
455 struct GNUNET_MULTICAST_MessageHeader *msg;
456 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
457 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
458 GNUNET_assert (msg != NULL);
460 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
461 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
463 msg->hop_counter = htonl (9);
464 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
465 msg->fragment_offset = GNUNET_htonll (0);
466 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
467 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
468 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
470 GNUNET_memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
472 msg->purpose.size = htonl (ntohs (msg->header.size)
473 - sizeof (msg->header)
474 - sizeof (msg->hop_counter)
475 - sizeof (msg->signature));
476 msg->purpose.purpose = htonl (234);
477 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose,
480 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
481 &fragment_store_result, GNUNET_NO);
483 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
484 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
485 GNUNET_memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
486 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
487 msg->fragment_offset = GNUNET_htonll (1024);
489 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
490 &fragment_store_result, GNUNET_NO);
492 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
493 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
494 GNUNET_memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
495 msg->fragment_id = GNUNET_htonll (INT64_MAX);
496 msg->fragment_offset = GNUNET_htonll (16384);
498 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
499 &fragment_store_result, (void *) GNUNET_YES);
504 membership_test_result (void *cls, int64_t result,
505 const char *err_msg, uint16_t err_msg_size)
508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%ld\n", (long int) result);
509 GNUNET_assert (GNUNET_OK == result);
516 membership_store_result (void *cls, int64_t result,
517 const char *err_msg, uint16_t err_msg_size)
520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%ld\n", (long int) result);
521 GNUNET_assert (GNUNET_OK == result);
523 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
525 &membership_test_result, NULL);
530 * Main function of the test, run from scheduler.
533 * @param cfg configuration we use (also to connect to PSYCstore service)
534 * @param peer handle to access more of the peer (not used)
537 #if DEBUG_TEST_PSYCSTORE
538 run (void *cls, char *const *args, const char *cfgfile,
539 const struct GNUNET_CONFIGURATION_Handle *cfg)
542 const struct GNUNET_CONFIGURATION_Handle *cfg,
543 struct GNUNET_TESTING_Peer *peer)
546 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
548 h = GNUNET_PSYCSTORE_connect (cfg);
549 GNUNET_assert (NULL != h);
551 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
552 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
554 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
555 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
557 op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
558 GNUNET_YES, INT64_MAX - 5,
560 &membership_store_result, NULL);
565 main (int argc, char *argv[])
568 #if DEBUG_TEST_PSYCSTORE
569 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
570 GNUNET_GETOPT_OPTION_END
572 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
573 "test-psycstore [options]",
577 if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
578 "test_psycstore.conf", &run, NULL))
584 /* end of test_psycstore.c */