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_psycstore_service.h"
32 #include "gnunet_testing_lib.h"
34 #define ASSERT(x) do { if (! (x)) { printf ("Error at %s:%d\n", __FILE__, __LINE__); cleanup (); return; } } while (0)
35 #define ASSERRT(x) do { if (! (x)) { printf ("Error at %s:%d\n", __FILE__, __LINE__); cleanup (); return GNUNET_SYSERR; } } while (0)
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
39 #define DEBUG_SERVICE 0
43 * Return value from 'main'.
48 * Handle to PSYCstore service.
50 static struct GNUNET_PSYCSTORE_Handle *h;
53 * Handle to PSYCstore operation.
55 static struct GNUNET_PSYCSTORE_OperationHandle *op;
58 * Handle for task for timeout termination.
60 static GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
62 static struct GNUNET_CRYPTO_EccPrivateKey *channel_key;
63 static struct GNUNET_CRYPTO_EccPrivateKey *slave_key;
65 static struct GNUNET_CRYPTO_EccPublicSignKey channel_pub_key;
66 static struct GNUNET_CRYPTO_EccPublicSignKey slave_pub_key;
68 static struct FragmentClosure
73 struct GNUNET_MULTICAST_MessageHeader *msg[16];
80 size_t value_size[16];
83 static struct GNUNET_ENV_Modifier modifiers[16];
86 * Clean up all resources used.
93 GNUNET_PSYCSTORE_operation_cancel (op);
98 GNUNET_PSYCSTORE_disconnect (h);
101 if (NULL != channel_key)
103 GNUNET_free (channel_key);
106 if (NULL != slave_key)
108 GNUNET_free (slave_key);
111 GNUNET_SCHEDULER_shutdown ();
116 * Terminate the testcase (failure).
119 * @param tc scheduler context
122 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 * Terminate the testcase (success).
133 * @param tc scheduler context
136 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 * Finish the testcase (successfully).
149 if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
151 GNUNET_SCHEDULER_cancel (end_badly_task);
152 end_badly_task = GNUNET_SCHEDULER_NO_TASK;
154 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
155 &end_normally, NULL);
160 state_reset_result (void *cls, int64_t result, const char *err_msg)
163 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_reset_result:\t%d\n", result);
164 ASSERT (GNUNET_OK == result);
166 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
167 &state_reset_result, cls);
168 GNUNET_PSYCSTORE_operation_cancel (op);
175 state_result (void *cls, const char *name, const void *value, size_t value_size)
177 struct StateClosure *scls = cls;
178 const char *nam = scls->name[scls->n];
179 const void *val = scls->value[scls->n];
180 size_t val_size = scls->value_size[scls->n++];
182 if (value_size == val_size
183 && 0 == memcmp (value, val, val_size)
184 && 0 == strcmp (name, nam))
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " variable %s matches\n", name);
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192 " variable %s differs\nReceived: %.*s\nExpected: %.*s\n",
193 name, value_size, value, val_size, val);
195 return GNUNET_SYSERR;
201 state_get_prefix_result (void *cls, int64_t result, const char *err_msg)
203 struct StateClosure *scls = cls;
205 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_prefix_result:\t%d\n", result);
206 ASSERT (GNUNET_OK == result && 2 == scls->n);
208 op = GNUNET_PSYCSTORE_state_reset (h, &channel_pub_key,
209 &state_reset_result, cls);
214 state_get_result (void *cls, int64_t result, const char *err_msg)
217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_get_result:\t%d\n", result);
218 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_slave_result (void *cls, uint64_t max_state_msg_id)
239 struct FragmentClosure *fcls = cls;
243 if (max_state_msg_id == GNUNET_ntohll (fcls->msg[0]->message_id))
246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get_slave:\t%d\n", result);
247 ASSERT (result == 1);
250 scls.name[0] = "_bar";
251 scls.value[0] = "four five six";
252 scls.value_size[0] = sizeof ("four five six") - 1;
254 op = GNUNET_PSYCSTORE_state_get (h, &channel_pub_key, "_bar_x_yy_zzz",
255 &state_result, &state_get_result, &scls);
260 state_modify_result (void *cls, int64_t result, const char *err_msg)
263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_modify_result:\t%d\n", result);
264 ASSERT (GNUNET_OK == result);
266 op = GNUNET_PSYCSTORE_counters_get_slave (h, &channel_pub_key,
267 &counters_slave_result, cls);
272 state_sync_result (void *cls, int64_t result, const char *err_msg)
274 struct FragmentClosure *fcls = cls;
276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "state_sync_result:\t%d\n", result);
277 ASSERT (GNUNET_OK == result);
279 modifiers[0] = (struct GNUNET_ENV_Modifier) {
282 .value = "one two three",
283 .value_size = sizeof ("one two three") - 1
285 modifiers[1] = (struct GNUNET_ENV_Modifier) {
288 .value = "four five six",
289 .value_size = sizeof ("four five six") - 1
292 op = GNUNET_PSYCSTORE_state_modify (h, &channel_pub_key,
293 GNUNET_ntohll (fcls->msg[0]->message_id), 0,
294 2, modifiers, state_modify_result, fcls);
299 counters_master_result (void *cls, uint64_t fragment_id, uint64_t message_id,
300 uint64_t group_generation)
302 struct FragmentClosure *fcls = cls;
306 if (fragment_id == GNUNET_ntohll (fcls->msg[2]->fragment_id) &&
307 message_id == GNUNET_ntohll (fcls->msg[2]->message_id) &&
308 group_generation == GNUNET_ntohll (fcls->msg[2]->group_generation))
311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "counters_get_master:\t%d\n", result);
312 ASSERT (result == 1);
314 modifiers[0] = (struct GNUNET_ENV_Modifier) {
317 .value = "three two one",
318 .value_size = sizeof ("three two one") - 1
320 modifiers[1] = (struct GNUNET_ENV_Modifier) {
323 .value = "ten eleven twelve",
324 .value_size = sizeof ("ten eleven twelve") - 1
327 op = GNUNET_PSYCSTORE_state_sync (h, &channel_pub_key,
328 GNUNET_ntohll (fcls->msg[0]->message_id) + 1,
329 2, modifiers, state_sync_result, fcls);
334 fragment_result (void *cls,
335 struct GNUNET_MULTICAST_MessageHeader *msg,
336 enum GNUNET_PSYCSTORE_MessageFlags flags)
338 struct FragmentClosure *fcls = cls;
339 struct GNUNET_MULTICAST_MessageHeader *msg0 = fcls->msg[fcls->n];
340 uint64_t flags0 = fcls->flags[fcls->n++];
342 if (flags == flags0 && msg->header.size == msg0->header.size
343 && 0 == memcmp (msg, msg0, ntohs (msg->header.size)))
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " fragment %llu matches\n",
346 GNUNET_ntohll (msg->fragment_id));
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " fragment %llu differs\n",
352 GNUNET_ntohll (msg->fragment_id));
354 return GNUNET_SYSERR;
360 message_get_result (void *cls, int64_t result, const char *err_msg)
362 struct FragmentClosure *fcls = cls;
364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get:\t%d\n", result);
365 ASSERT (result > 0 && fcls->n && fcls->n_expected);
367 op = GNUNET_PSYCSTORE_counters_get_master (h, &channel_pub_key,
368 &counters_master_result, fcls);
373 message_get_fragment_result (void *cls, int64_t result, const char *err_msg)
375 struct FragmentClosure *fcls = cls;
377 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "message_get_fragment:\t%d\n", result);
378 ASSERT (result > 0 && fcls->n && fcls->n_expected);
381 fcls->n_expected = 3;
382 op = GNUNET_PSYCSTORE_message_get (h, &channel_pub_key,
383 GNUNET_ntohll (fcls->msg[0]->message_id),
385 &message_get_result, fcls);
390 fragment_get_result (void *cls, int64_t result, const char *err_msg)
392 struct FragmentClosure *fcls = cls;
394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_get:\t%d\n", result);
395 ASSERT (result > 0 && fcls->n && fcls->n_expected);
398 fcls->n_expected = 2;
399 op = GNUNET_PSYCSTORE_message_get_fragment (h, &channel_pub_key,
400 GNUNET_ntohll (fcls->msg[1]->message_id),
401 GNUNET_ntohll (fcls->msg[1]->fragment_offset),
403 &message_get_fragment_result,
410 fragment_store_result (void *cls, int64_t result, const char *err_msg)
413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "fragment_store:\t%d\n", result);
414 ASSERT (GNUNET_OK == result);
416 if ((intptr_t) cls == GNUNET_YES)
420 op = GNUNET_PSYCSTORE_fragment_get (h, &channel_pub_key,
421 GNUNET_ntohll (fcls.msg[0]->fragment_id),
423 &fragment_get_result, &fcls);
429 membership_test_result (void *cls, int64_t result, const char *err_msg)
432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_test:\t%d\n", result);
433 ASSERT (GNUNET_OK == result);
435 struct GNUNET_MULTICAST_MessageHeader *msg;
436 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
437 fcls.msg[0] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
438 ASSERT (msg != NULL);
440 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
441 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
443 msg->hop_counter = htonl (9);
444 msg->fragment_id = GNUNET_htonll (INT64_MAX - 8);
445 msg->fragment_offset = GNUNET_htonll (0);
446 msg->message_id = GNUNET_htonll (INT64_MAX - 10);
447 msg->group_generation = GNUNET_htonll (INT64_MAX - 3);
448 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
450 memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
452 msg->purpose.size = htonl (ntohs (msg->header.size)
453 - sizeof (msg->header)
454 - sizeof (msg->hop_counter)
455 - sizeof (msg->signature));
456 msg->purpose.purpose = htonl (234);
457 GNUNET_CRYPTO_ecc_sign (slave_key, &msg->purpose, &msg->signature);
459 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[0],
460 &fragment_store_result, GNUNET_NO);
462 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
463 fcls.msg[1] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
464 memcpy (msg, fcls.msg[0], sizeof (*msg) + sizeof (channel_pub_key));
465 msg->fragment_id = GNUNET_htonll (INT64_MAX - 4);
466 msg->fragment_offset = GNUNET_htonll (1024);
468 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[1],
469 &fragment_store_result, GNUNET_NO);
471 fcls.flags[2] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
472 fcls.msg[2] = msg = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
473 memcpy (msg, fcls.msg[1], sizeof (*msg) + sizeof (channel_pub_key));
474 msg->fragment_id = GNUNET_htonll (INT64_MAX);
475 msg->fragment_offset = GNUNET_htonll (16384);
477 op = GNUNET_PSYCSTORE_fragment_store (h, &channel_pub_key, msg, fcls.flags[2],
478 &fragment_store_result, (void *) GNUNET_YES);
482 membership_store_result (void *cls, int64_t result, const char *err_msg)
485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "membership_store:\t%d\n", result);
486 ASSERT (GNUNET_OK == result);
488 op = GNUNET_PSYCSTORE_membership_test (h, &channel_pub_key, &slave_pub_key,
490 &membership_test_result, NULL);
494 * Main function of the test, run from scheduler.
497 * @param cfg configuration we use (also to connect to PSYCstore service)
498 * @param peer handle to access more of the peer (not used)
502 run (void *cls, char *const *args, const char *cfgfile,
503 const struct GNUNET_CONFIGURATION_Handle *cfg)
506 const struct GNUNET_CONFIGURATION_Handle *cfg,
507 struct GNUNET_TESTING_Peer *peer)
510 end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
512 h = GNUNET_PSYCSTORE_connect (cfg);
515 channel_key = GNUNET_CRYPTO_ecc_key_create ();
516 slave_key = GNUNET_CRYPTO_ecc_key_create ();
518 GNUNET_CRYPTO_ecc_key_get_public_for_signature (channel_key, &channel_pub_key);
519 GNUNET_CRYPTO_ecc_key_get_public_for_signature (slave_key, &slave_pub_key);
521 op = GNUNET_PSYCSTORE_membership_store (h, &channel_pub_key, &slave_pub_key,
523 &membership_store_result, NULL);
528 main (int argc, char *argv[])
532 const struct GNUNET_GETOPT_CommandLineOption opts[] = {
533 GNUNET_GETOPT_OPTION_END
535 if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psycstore",
536 "test-psycstore [options]",
540 if (0 != GNUNET_TESTING_service_run ("test-psycstore", "psycstore",
541 "test_psycstore.conf", &run, NULL))
547 /* end of test_psycstore.c */