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 * @author Gabor X Toth
21 * @author Christian Grothoff
24 * Test for the PSYCstore plugins.
30 #include "gnunet_util_lib.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_psycstore_plugin.h"
33 #include "gnunet_psycstore_service.h"
34 #include "gnunet_multicast_service.h"
36 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
38 # define LOG_LEVEL "DEBUG"
40 # define LOG_LEVEL "WARNING"
43 #define C2ARG(str) str, (sizeof (str) - 1)
45 #define LOG(kind,...) \
46 GNUNET_log_from (kind, "test-plugin-psycstore", __VA_ARGS__)
51 * Name of plugin under test.
53 static const char *plugin_name;
55 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
56 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
58 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
59 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
62 * Function called when the service shuts down. Unloads our psycstore
65 * @param api api to unload
68 unload_plugin (struct GNUNET_PSYCSTORE_PluginFunctions *api)
72 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
73 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
74 GNUNET_free (libname);
79 * Load the psycstore plugin.
81 * @param cfg configuration to pass
82 * @return NULL on error
84 static struct GNUNET_PSYCSTORE_PluginFunctions *
85 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
87 struct GNUNET_PSYCSTORE_PluginFunctions *ret;
90 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' psycstore plugin\n"),
92 GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
93 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
95 FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
98 GNUNET_free (libname);
105 struct FragmentClosure
108 uint64_t flags[MAX_MSG];
109 struct GNUNET_MULTICAST_MessageHeader *msg[MAX_MSG];
113 fragment_cb (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg2,
114 enum GNUNET_PSYCSTORE_MessageFlags flags)
116 struct FragmentClosure *fcls = cls;
117 struct GNUNET_MULTICAST_MessageHeader *msg1;
121 if (fcls->n >= MAX_MSG)
124 return GNUNET_SYSERR;
126 msg1 = fcls->msg[fcls->n];
127 flags1 = fcls->flags[fcls->n++];
131 return GNUNET_SYSERR;
134 if (flags1 == flags && msg1->header.size == msg2->header.size
135 && 0 == memcmp (msg1, msg2, ntohs (msg1->header.size)))
137 LOG (GNUNET_ERROR_TYPE_DEBUG, "Fragment %llu matches\n",
138 GNUNET_ntohll (msg1->fragment_id));
143 LOG (GNUNET_ERROR_TYPE_ERROR, "Fragment %llu differs\n",
144 GNUNET_ntohll (msg1->fragment_id));
153 struct StateClosure {
157 size_t value_size[16];
161 state_cb (void *cls, const char *name, const void *value, uint32_t value_size)
163 struct StateClosure *scls = cls;
164 const void *val = scls->value[scls->n]; // FIXME: check for n out-of-bounds FIRST!
165 size_t val_size = scls->value_size[scls->n++];
167 /* FIXME: check name */
169 LOG (GNUNET_ERROR_TYPE_DEBUG,
170 " name = %s, value_size = %u\n",
174 return value_size == val_size && 0 == memcmp (value, val, val_size)
181 run (void *cls, char *const *args, const char *cfgfile,
182 const struct GNUNET_CONFIGURATION_Handle *cfg)
184 struct GNUNET_PSYCSTORE_PluginFunctions *db;
187 db = load_plugin (cfg);
192 "Failed to initialize PSYCstore. "
193 "Database likely not setup, skipping test.\n");
198 /* Store & test membership */
200 LOG (GNUNET_ERROR_TYPE_INFO, "MEMBERSHIP\n");
202 channel_key = GNUNET_CRYPTO_eddsa_key_create ();
203 slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
205 GNUNET_CRYPTO_eddsa_key_get_public (channel_key,
207 GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
209 LOG (GNUNET_ERROR_TYPE_INFO, "membership_store()\n");
211 GNUNET_assert (GNUNET_OK == db->membership_store (db->cls, &channel_pub_key,
212 &slave_pub_key, GNUNET_YES,
215 LOG (GNUNET_ERROR_TYPE_INFO, "membership_test()\n");
217 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
220 GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
223 GNUNET_assert (GNUNET_NO == db->membership_test (db->cls, &channel_pub_key,
226 /* Store & get messages */
228 LOG (GNUNET_ERROR_TYPE_INFO, "MESSAGES\n");
230 struct GNUNET_MULTICAST_MessageHeader *msg
231 = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
232 GNUNET_assert (msg != NULL);
234 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
235 msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
237 uint64_t fragment_id = INT64_MAX - 1;
238 msg->fragment_id = GNUNET_htonll (fragment_id);
240 uint64_t message_id = INT64_MAX - 10;
241 msg->message_id = GNUNET_htonll (message_id);
243 uint64_t group_generation = INT64_MAX - 3;
244 msg->group_generation = GNUNET_htonll (group_generation);
246 msg->hop_counter = htonl (9);
247 msg->fragment_offset = GNUNET_htonll (0);
248 msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
250 GNUNET_memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
252 msg->purpose.size = htonl (ntohs (msg->header.size)
253 - sizeof (msg->header)
254 - sizeof (msg->hop_counter)
255 - sizeof (msg->signature));
256 msg->purpose.purpose = htonl (234);
257 GNUNET_assert (GNUNET_OK ==
258 GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose, &msg->signature));
260 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_store()\n");
262 struct FragmentClosure fcls = { 0 };
265 fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
268 GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg,
271 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_get(%" PRIu64 ")\n", fragment_id);
273 uint64_t ret_frags = 0;
275 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
276 fragment_id, fragment_id,
277 &ret_frags, fragment_cb, &fcls));
278 GNUNET_assert (fcls.n == 1);
280 LOG (GNUNET_ERROR_TYPE_INFO, "message_get_fragment()\n");
284 GNUNET_OK == db->message_get_fragment (db->cls, &channel_pub_key,
285 GNUNET_ntohll (msg->message_id),
286 GNUNET_ntohll (msg->fragment_offset),
287 fragment_cb, &fcls));
288 GNUNET_assert (fcls.n == 1);
290 LOG (GNUNET_ERROR_TYPE_INFO, "message_add_flags()\n");
292 GNUNET_OK == db->message_add_flags (db->cls, &channel_pub_key,
293 GNUNET_ntohll (msg->message_id),
294 GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED));
295 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_get(%" PRIu64 ")\n", fragment_id);
298 fcls.flags[0] |= GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
301 GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
302 fragment_id, fragment_id,
303 &ret_frags, fragment_cb, &fcls));
305 GNUNET_assert (fcls.n == 1);
307 LOG (GNUNET_ERROR_TYPE_INFO, "fragment_store()\n");
309 struct GNUNET_MULTICAST_MessageHeader *msg1
310 = GNUNET_malloc (sizeof (*msg1) + sizeof (channel_pub_key));
312 GNUNET_memcpy (msg1, msg, sizeof (*msg1) + sizeof (channel_pub_key));
314 msg1->fragment_id = GNUNET_htonll (INT64_MAX);
315 msg1->fragment_offset = GNUNET_htonll (32768);
319 fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
321 GNUNET_assert (GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg1,
324 LOG (GNUNET_ERROR_TYPE_INFO, "message_get()\n");
327 GNUNET_OK == db->message_get (db->cls, &channel_pub_key,
328 message_id, message_id, 0,
329 &ret_frags, fragment_cb, &fcls));
330 GNUNET_assert (fcls.n == 2 && ret_frags == 2);
332 /* Message counters */
334 LOG (GNUNET_ERROR_TYPE_INFO, "counters_message_get()\n");
338 group_generation = 0;
340 GNUNET_OK == db->counters_message_get (db->cls, &channel_pub_key,
341 &fragment_id, &message_id,
343 && fragment_id == GNUNET_ntohll (msg1->fragment_id)
344 && message_id == GNUNET_ntohll (msg1->message_id)
345 && group_generation == GNUNET_ntohll (msg1->group_generation));
349 LOG (GNUNET_ERROR_TYPE_INFO, "STATE\n");
351 LOG (GNUNET_ERROR_TYPE_INFO, "state_modify_*()\n");
353 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 1;
354 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
357 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
358 GNUNET_PSYC_OP_ASSIGN,
360 C2ARG("one two three")));
362 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
363 GNUNET_PSYC_OP_ASSIGN,
364 "_foo_bar", slave_key,
365 sizeof (*slave_key)));
367 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
370 LOG (GNUNET_ERROR_TYPE_INFO, "state_get()\n");
372 struct StateClosure scls = { 0 };
374 scls.value[0] = "one two three";
375 scls.value_size[0] = strlen ("one two three");
377 GNUNET_assert (GNUNET_OK == db->state_get (db->cls, &channel_pub_key, "_foo",
379 GNUNET_assert (scls.n == 1);
381 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_prefix()\n");
384 scls.value[1] = slave_key;
385 scls.value_size[1] = sizeof (*slave_key);
387 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
388 "_foo", state_cb, &scls));
389 GNUNET_assert (scls.n == 2);
391 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_signed()\n");
394 GNUNET_assert (GNUNET_NO == db->state_get_signed (db->cls, &channel_pub_key,
396 GNUNET_assert (scls.n == 0);
398 LOG (GNUNET_ERROR_TYPE_INFO, "state_update_signed()\n");
400 GNUNET_assert (GNUNET_OK == db->state_update_signed (db->cls,
403 LOG (GNUNET_ERROR_TYPE_INFO, "state_get_signed()\n");
406 GNUNET_assert (GNUNET_YES == db->state_get_signed (db->cls, &channel_pub_key,
408 GNUNET_assert (scls.n == 2);
412 LOG (GNUNET_ERROR_TYPE_INFO, "counters_state_get()\n");
414 uint64_t max_state_msg_id = 0;
415 GNUNET_assert (GNUNET_OK == db->counters_state_get (db->cls, &channel_pub_key,
417 && max_state_msg_id == message_id);
421 LOG (GNUNET_ERROR_TYPE_INFO, "state_sync_*()\n");
424 scls.value[0] = channel_key;
425 scls.value_size[0] = sizeof (*channel_key);
426 scls.value[1] = "three two one";
427 scls.value_size[1] = strlen ("three two one");
429 GNUNET_assert (GNUNET_OK == db->state_sync_begin (db->cls, &channel_pub_key));
431 GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
432 "_sync_bar", scls.value[0],
433 scls.value_size[0]));
435 GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
436 "_sync_foo", scls.value[1],
437 scls.value_size[1]));
439 GNUNET_assert (GNUNET_OK == db->state_sync_end (db->cls, &channel_pub_key,
443 GNUNET_assert (GNUNET_NO == db->state_get_prefix (db->cls, &channel_pub_key,
444 "_foo", state_cb, &scls));
445 GNUNET_assert (scls.n == 0);
447 GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
448 "_sync", state_cb, &scls));
449 GNUNET_assert (scls.n == 2);
452 GNUNET_assert (GNUNET_OK == db->state_get_signed (db->cls, &channel_pub_key,
454 GNUNET_assert (scls.n == 2);
456 /* Modify state after sync */
458 LOG (GNUNET_ERROR_TYPE_INFO, "state_modify_*()\n");
460 message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 6;
461 GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
463 message_id - max_state_msg_id));
465 GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
466 GNUNET_PSYC_OP_ASSIGN,
468 C2ARG("five six seven")));
470 GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
475 LOG (GNUNET_ERROR_TYPE_INFO, "state_reset()\n");
478 GNUNET_assert (GNUNET_OK == db->state_reset (db->cls, &channel_pub_key));
479 GNUNET_assert (scls.n == 0);
483 if (NULL != channel_key)
485 GNUNET_free (channel_key);
488 if (NULL != slave_key)
490 GNUNET_free (slave_key);
499 main (int argc, char *argv[])
502 char *const xargv[] = {
503 "test-plugin-psycstore",
508 struct GNUNET_GETOPT_CommandLineOption options[] = {
509 GNUNET_GETOPT_OPTION_END
511 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
512 GNUNET_log_setup ("test-plugin-psycstore", LOG_LEVEL, NULL);
513 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
514 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_psycstore_%s.conf",
516 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
517 "test-plugin-psycstore", "nohelp", options, &run, NULL);
521 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
523 #if ! DEBUG_PSYCSTORE
524 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
530 /* end of test_plugin_psycstore.c */