2 * This file is part of GNUnet
3 * (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
29 #include "gnunet_common.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_psycstore_service.h"
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
36 #define DEBUG_SERVICE 0
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 GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
59 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
60 static struct GNUNET_CRYPTO_EddsaPrivateKey *slave_key;
62 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
63 static struct GNUNET_CRYPTO_EddsaPublicKey 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 != GNUNET_SCHEDULER_NO_TASK)
148 GNUNET_SCHEDULER_cancel (end_badly_task);
149 end_badly_task = GNUNET_SCHEDULER_NO_TASK;
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, uint64_t max_fragment_id, uint64_t max_message_id,
235 uint64_t max_group_generation, uint64_t max_state_message_id)
237 struct FragmentClosure *fcls = cls;
241 if (max_fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id)
242 && max_message_id == GNUNET_ntohll (fcls->msg[2]->message_id)
243 && max_group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation)
244 && max_state_message_id == GNUNET_ntohll (fcls->msg[0]->message_id))
247 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get:\t%d\n", result);
248 GNUNET_assert (result == 1);
251 scls.name[0] = "_bar";
252 scls.value[0] = "four five six";
253 scls.value_size[0] = sizeof ("four five six") - 1;
255 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_bar_x_yy_zzz",
256 &state_result, &state_get_result, &scls);
261 state_modify_result (void *cls, int64_t result, const char *err_msg)
264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%d\n", result);
265 GNUNET_assert (GNUNET_OK == result);
267 op = GNUNET_PSYCSTORE_counters_get (h, &channel_pub_key,
268 &counters_result, cls);
273 state_sync_result (void *cls, int64_t result, const char *err_msg)
275 struct FragmentClosure *fcls = cls;
277 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%d\n", result);
278 GNUNET_assert (GNUNET_OK == result);
280 modifiers[0] = (struct GNUNET_ENV_Modifier) {
283 .value = "one two three",
284 .value_size = sizeof ("one two three") - 1
286 modifiers[1] = (struct GNUNET_ENV_Modifier) {
289 .value = "four five six",
290 .value_size = sizeof ("four five six") - 1
293 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
294 GNUNET_ntohll (fcls->msg[0]->message_id), 0,
295 2, modifiers, state_modify_result, fcls);
300 fragment_result (void *cls,
301 struct GNUNET_MULTICAST_MessageHeader *msg,
302 enum GNUNET_PSYCSTORE_MessageFlags flags)
304 struct FragmentClosure *fcls = cls;
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 %llu matches\n",
312 GNUNET_ntohll (msg->fragment_id));
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " fragment %llu differs\n",
318 GNUNET_ntohll (msg->fragment_id));
320 return GNUNET_SYSERR;
326 message_get_result (void *cls, int64_t result, const char *err_msg)
328 struct FragmentClosure *fcls = cls;
330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%d\n", result);
331 GNUNET_assert (result > 0 && fcls->n && fcls->n_expected);
335 modifiers[0] = (struct GNUNET_ENV_Modifier) {
338 .value = "three two one",
339 .value_size = sizeof ("three two one") - 1
341 modifiers[1] = (struct GNUNET_ENV_Modifier) {
344 .value = "ten eleven twelve",
345 .value_size = sizeof ("ten eleven twelve") - 1
348 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
349 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
350 2, modifiers, state_sync_result, fcls);
355 message_get_fragment_result (void *cls, int64_t result, const char *err_msg)
357 struct FragmentClosure *fcls = cls;
359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
360 GNUNET_assert (result > 0 && fcls->n && fcls->n_expected);
363 fcls->n_expected = 3;
364 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key,
365 GNUNET_ntohll (fcls->msg[0]->message_id),
367 &message_get_result, fcls);
372 fragment_get_result (void *cls, int64_t result, const char *err_msg)
374 struct FragmentClosure *fcls = cls;
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
377 GNUNET_assert (result > 0 && fcls->n && fcls->n_expected);
380 fcls->n_expected = 2;
381 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key,
382 GNUNET_ntohll (fcls->msg[1]->message_id),
383 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
385 &message_get_fragment_result,
392 fragment_store_result (void *cls, int64_t result, const char *err_msg)
395 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
396 GNUNET_assert (GNUNET_OK == result);
398 if ((intptr_t) cls == GNUNET_YES)
402 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key,
403 GNUNET_ntohll (fcls.msg[0]->fragment_id),
405 &fragment_get_result, &fcls);
411 membership_test_result (void *cls, int64_t result, const char *err_msg)
414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
415 GNUNET_assert (GNUNET_OK == result);
417 struct GNUNET_MULTICAST_MessageHeader *msg;
418 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
419 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
420 GNUNET_assert (msg != NULL);
422 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
423 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
425 msg->hop_counter = htonl (9);
426 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
427 msg->fragment_offset = GNUNET_htonll (0);
428 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
429 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
430 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
432 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
434 msg->purpose.size = htonl (ntohs (msg->header.size)
435 - sizeof (msg->header)
436 - sizeof (msg->hop_counter)
437 - sizeof (msg->signature));
438 msg->purpose.purpose = htonl (234);
439 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (slave_key, &msg->purpose,
442 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
443 &fragment_store_result, GNUNET_NO);
445 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
446 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
447 memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
448 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
449 msg->fragment_offset = GNUNET_htonll (1024);
451 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
452 &fragment_store_result, GNUNET_NO);
454 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
455 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
456 memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
457 msg->fragment_id = GNUNET_htonll (INT64_MAX);
458 msg->fragment_offset = GNUNET_htonll (16384);
460 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
461 &fragment_store_result, (void *) GNUNET_YES);
465 membership_store_result (void *cls, int64_t result, const char *err_msg)
468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
469 GNUNET_assert (GNUNET_OK == result);
471 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
473 &membership_test_result, NULL);
477 * Main function of the test, run from scheduler.
480 * @param cfg configuration we use (also to connect to PSYCstore service)
481 * @param peer handle to access more of the peer (not used)
485 run (void *cls, char *const *args, const char *cfgfile,
486 const struct GNUNET_CONFIGURATION_Handle *cfg)
489 const struct GNUNET_CONFIGURATION_Handle *cfg,
490 struct GNUNET_TESTING_Peer *peer)
493 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
495 h = GNUNET_PSYCSTORE_connect (cfg);
496 GNUNET_assert (NULL != h);
498 channel_key = GNUNET_CRYPTO_ecc_key_create ();
499 slave_key = GNUNET_CRYPTO_ecc_key_create ();
501 GNUNET_CRYPTO_eddsa_key_get_public (channel_key, &channel_pub_key);
502 GNUNET_CRYPTO_eddsa_key_get_public (slave_key, &slave_pub_key);
504 op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
506 &membership_store_result, NULL);
511 main (int argc, char *argv[])
515 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
516 GNUNET_GETOPT_OPTION_END
518 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
519 "test-psycstore [options]",
523 if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
524 "test_psycstore.conf", &run, NULL))
530 /* end of test_psycstore.c */