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 * @author Gabor X Toth
23 * @author Christian Grothoff
26 * Test for the PSYCstore plugins.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testing_lib.h"
34 #include "gnunet_psycstore_plugin.h"
35 #include "gnunet_psycstore_service.h"
36 #include "gnunet_multicast_service.h"
38 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
40 # define LOG_LEVEL "DEBUG"
42 # define LOG_LEVEL "WARNING"
45 #define C2ARG(str) str, (sizeof (str) - 1)
47 #define LOG(kind,...) \
48 GNUNET_log_from (kind, "test-plugin-psycstore", __VA_ARGS__)
53 * Name of plugin under test.
55 static const char *plugin_name;
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;
64 * Function called when the service shuts down. Unloads our psycstore
67 * @param api api to unload
70 unload_plugin (struct GNUNET_PSYCSTORE_PluginFunctions *api)
74 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
75 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
76 GNUNET_free (libname);
81 * Load the psycstore plugin.
83 * @param cfg configuration to pass
84 * @return NULL on error
86 static struct GNUNET_PSYCSTORE_PluginFunctions *
87 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
89 struct GNUNET_PSYCSTORE_PluginFunctions *ret;
92 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' psycstore plugin\n"),
94 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
95 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
97 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
100 GNUNET_free (libname);
107 struct FragmentClosure
110 uint64_t flags[MAX_MSG];
111 struct GNUNET_MULTICAST_MessageHeader *msg[MAX_MSG];
115 fragment_cb (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg2,
116 enum GNUNET_PSYCSTORE_MessageFlags flags)
118 struct FragmentClosure *fcls = cls;
119 struct GNUNET_MULTICAST_MessageHeader *msg1;
123 if (fcls->n >= MAX_MSG)
126 return GNUNET_SYSERR;
128 msg1 = fcls->msg[fcls->n];
129 flags1 = fcls->flags[fcls->n++];
133 return GNUNET_SYSERR;
136 if (flags1 == flags && msg1->header.size == msg2->header.size
137 && 0 == memcmp (msg1, msg2, ntohs (msg1->header.size)))
139 LOG (GNUNET_ERROR_TYPE_DEBUG, "Fragment %llu matches\n",
140 GNUNET_ntohll (msg1->fragment_id));
145 LOG (GNUNET_ERROR_TYPE_ERROR, "Fragment %llu differs\n",
146 GNUNET_ntohll (msg1->fragment_id));
155 struct StateClosure {
159 size_t value_size[16];
163 state_cb (void *cls, const char *name, const void *value, uint32_t value_size)
165 struct StateClosure *scls = cls;
166 const void *val = scls->value[scls->n]; // FIXME: check for n out-of-bounds FIRST!
167 size_t val_size = scls->value_size[scls->n++];
169 /* FIXME: check name */
171 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 " name = %s, value_size = %u\n",
176 return value_size == val_size && 0 == memcmp (value, val, val_size)
183 run (void *cls, char *const *args, const char *cfgfile,
184 const struct GNUNET_CONFIGURATION_Handle *cfg)
186 struct GNUNET_PSYCSTORE_PluginFunctions *db;
189 db = load_plugin (cfg);
194 "Failed to initialize PSYCstore. "
195 "Database likely not setup, skipping test.\n");
200 /* Store & test membership */
202 LOG (GNUNET_ERROR_TYPE_INFO, "MEMBERSHIP\n");
204 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
205 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
207 GNUNET_CRYPTO_eddsa_key_get_public (channel_key,
209 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
211 LOG (GNUNET_ERROR_TYPE_INFO, "membership_store()\n");
213 GNUNET_assert (GNUNET_OK == db->membership_store (db->cls, &channel_pub_key,
214 &slave_pub_key, GNUNET_YES,
217 LOG (GNUNET_ERROR_TYPE_INFO, "membership_test()\n");
219 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
222 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
225 GNUNET_assert (GNUNET_NO == db->membership_test (db->cls, &channel_pub_key,
228 /* Store & get messages */
230 LOG (GNUNET_ERROR_TYPE_INFO, "MESSAGES\n");
232 struct GNUNET_MULTICAST_MessageHeader *msg
233 = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
234 GNUNET_assert (msg != NULL);
236 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
237 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
239 uint64_t fragment_id = INT64_MAX - 1;
240 msg->fragment_id = GNUNET_htonll (fragment_id);
242 uint64_t message_id = INT64_MAX - 10;
243 msg->message_id = GNUNET_htonll (message_id);
245 uint64_t group_generation = INT64_MAX - 3;
246 msg->group_generation = GNUNET_htonll (group_generation);
248 msg->hop_counter = htonl (9);
249 msg->fragment_offset = GNUNET_htonll (0);
250 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
252 GNUNET_memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
254 msg->purpose.size = htonl (ntohs (msg->header.size)
255 - sizeof (msg->header)
256 - sizeof (msg->hop_counter)
257 - sizeof (msg->signature));
258 msg->purpose.purpose = htonl (234);
259 GNUNET_assert (GNUNET_OK ==
260 GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose, &msg->signature));
262 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_store()\n");
264 struct FragmentClosure fcls = { 0 };
267 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
270 GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg,
273 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_get(%" PRIu64 ")\n", fragment_id);
275 uint64_t ret_frags = 0;
277 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
278 fragment_id, fragment_id,
279 &ret_frags, fragment_cb, &fcls));
280 GNUNET_assert (fcls.n == 1);
282 LOG (GNUNET_ERROR_TYPE_INFO, "message_get_fragment()\n");
286 GNUNET_OK == db->message_get_fragment (db->cls, &channel_pub_key,
287 GNUNET_ntohll (msg->message_id),
288 GNUNET_ntohll (msg->fragment_offset),
289 fragment_cb, &fcls));
290 GNUNET_assert (fcls.n == 1);
292 LOG (GNUNET_ERROR_TYPE_INFO, "message_add_flags()\n");
294 GNUNET_OK == db->message_add_flags (db->cls, &channel_pub_key,
295 GNUNET_ntohll (msg->message_id),
296 GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED));
297 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_get(%" PRIu64 ")\n", fragment_id);
300 fcls.flags[0] |= GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
303 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
304 fragment_id, fragment_id,
305 &ret_frags, fragment_cb, &fcls));
307 GNUNET_assert (fcls.n == 1);
309 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_store()\n");
311 struct GNUNET_MULTICAST_MessageHeader *msg1
312 = GNUNET_malloc (sizeof (*msg1) + sizeof (channel_pub_key));
314 GNUNET_memcpy (msg1, msg, sizeof (*msg1) + sizeof (channel_pub_key));
316 msg1->fragment_id = GNUNET_htonll (INT64_MAX);
317 msg1->fragment_offset = GNUNET_htonll (32768);
321 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
323 GNUNET_assert (GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg1,
326 LOG (GNUNET_ERROR_TYPE_INFO, "message_get()\n");
329 GNUNET_OK == db->message_get (db->cls, &channel_pub_key,
330 message_id, message_id, 0,
331 &ret_frags, fragment_cb, &fcls));
332 GNUNET_assert (fcls.n == 2 && ret_frags == 2);
334 /* Message counters */
336 LOG (GNUNET_ERROR_TYPE_INFO, "counters_message_get()\n");
340 group_generation = 0;
342 GNUNET_OK == db->counters_message_get (db->cls, &channel_pub_key,
343 &fragment_id, &message_id,
345 && fragment_id == GNUNET_ntohll (msg1->fragment_id)
346 && message_id == GNUNET_ntohll (msg1->message_id)
347 && group_generation == GNUNET_ntohll (msg1->group_generation));
351 LOG (GNUNET_ERROR_TYPE_INFO, "STATE\n");
353 LOG (GNUNET_ERROR_TYPE_INFO, "state_modify_*()\n");
355 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 1;
356 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
359 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
360 GNUNET_PSYC_OP_ASSIGN,
362 C2ARG("one two three")));
364 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
365 GNUNET_PSYC_OP_ASSIGN,
366 "_foo_bar", slave_key,
367 sizeof (*slave_key)));
369 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
372 LOG (GNUNET_ERROR_TYPE_INFO, "state_get()\n");
374 struct StateClosure scls = { 0 };
376 scls.value[0] = "one two three";
377 scls.value_size[0] = strlen ("one two three");
379 GNUNET_assert (GNUNET_OK == db->state_get (db->cls, &channel_pub_key, "_foo",
381 GNUNET_assert (scls.n == 1);
383 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_prefix()\n");
386 scls.value[1] = slave_key;
387 scls.value_size[1] = sizeof (*slave_key);
389 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
390 "_foo", state_cb, &scls));
391 GNUNET_assert (scls.n == 2);
393 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_signed()\n");
396 GNUNET_assert (GNUNET_NO == db->state_get_signed (db->cls, &channel_pub_key,
398 GNUNET_assert (scls.n == 0);
400 LOG (GNUNET_ERROR_TYPE_INFO, "state_update_signed()\n");
402 GNUNET_assert (GNUNET_OK == db->state_update_signed (db->cls,
405 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_signed()\n");
408 GNUNET_assert (GNUNET_YES == db->state_get_signed (db->cls, &channel_pub_key,
410 GNUNET_assert (scls.n == 2);
414 LOG (GNUNET_ERROR_TYPE_INFO, "counters_state_get()\n");
416 uint64_t max_state_msg_id = 0;
417 GNUNET_assert (GNUNET_OK == db->counters_state_get (db->cls, &channel_pub_key,
419 && max_state_msg_id == message_id);
423 LOG (GNUNET_ERROR_TYPE_INFO, "state_sync_*()\n");
426 scls.value[0] = channel_key;
427 scls.value_size[0] = sizeof (*channel_key);
428 scls.value[1] = "three two one";
429 scls.value_size[1] = strlen ("three two one");
431 GNUNET_assert (GNUNET_OK == db->state_sync_begin (db->cls, &channel_pub_key));
433 GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
434 "_sync_bar", scls.value[0],
435 scls.value_size[0]));
437 GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
438 "_sync_foo", scls.value[1],
439 scls.value_size[1]));
441 GNUNET_assert (GNUNET_OK == db->state_sync_end (db->cls, &channel_pub_key,
445 GNUNET_assert (GNUNET_NO == db->state_get_prefix (db->cls, &channel_pub_key,
446 "_foo", state_cb, &scls));
447 GNUNET_assert (scls.n == 0);
449 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
450 "_sync", state_cb, &scls));
451 GNUNET_assert (scls.n == 2);
454 GNUNET_assert (GNUNET_OK == db->state_get_signed (db->cls, &channel_pub_key,
456 GNUNET_assert (scls.n == 2);
458 /* Modify state after sync */
460 LOG (GNUNET_ERROR_TYPE_INFO, "state_modify_*()\n");
462 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 6;
463 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
465 message_id - max_state_msg_id));
467 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
468 GNUNET_PSYC_OP_ASSIGN,
470 C2ARG("five six seven")));
472 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
477 LOG (GNUNET_ERROR_TYPE_INFO, "state_reset()\n");
480 GNUNET_assert (GNUNET_OK == db->state_reset (db->cls, &channel_pub_key));
481 GNUNET_assert (scls.n == 0);
485 if (NULL != channel_key)
487 GNUNET_free (channel_key);
490 if (NULL != slave_key)
492 GNUNET_free (slave_key);
501 main (int argc, char *argv[])
504 char *const xargv[] = {
505 "test-plugin-psycstore",
510 struct GNUNET_GETOPT_CommandLineOption options[] = {
511 GNUNET_GETOPT_OPTION_END
513 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
514 GNUNET_log_setup ("test-plugin-psycstore", LOG_LEVEL, NULL);
515 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
516 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_psycstore_%s.conf",
518 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
519 "test-plugin-psycstore", "nohelp", options, &run, NULL);
523 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
525 #if ! DEBUG_PSYCSTORE
526 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
532 /* end of test_plugin_psycstore.c */